ai_controller.py 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  1. """
  2. Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
  3. Date: Fri Aug 05 16:08:35 2016
  4. Model author: Glenn De Jonghe
  5. Model name: AI Tank
  6. Model description:
  7. Handling the npc tank.
  8. """
  9. from sccd.runtime.statecharts_core import *
  10. from AIMap import AIMap
  11. import math
  12. from mymath import D1, D45, D360
  13. # package "AI Tank"
  14. class Main(RuntimeClassBase):
  15. def __init__(self, controller, tank):
  16. RuntimeClassBase.__init__(self, controller)
  17. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  18. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  19. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  20. self.semantics.priority = StatechartSemantics.SourceParent
  21. self.semantics.concurrency = StatechartSemantics.Single
  22. # build Statechart structure
  23. self.build_statechart_structure()
  24. # user defined attributes
  25. self.tank = None
  26. self.map = None
  27. # call user defined constructor
  28. Main.user_defined_constructor(self, tank)
  29. def user_defined_constructor(self, tank):
  30. self.tank = tank
  31. self.map = AIMap(tank.field.level)
  32. def user_defined_destructor(self):
  33. pass
  34. # builds Statechart structure
  35. def build_statechart_structure(self):
  36. # state <root>
  37. self.states[""] = State(0, self)
  38. # state /error
  39. self.states["/error"] = State(1, self)
  40. # state /creation
  41. self.states["/creation"] = State(2, self)
  42. # state /creation/state_1
  43. self.states["/creation/state_1"] = State(3, self)
  44. # state /creation/state_2
  45. self.states["/creation/state_2"] = State(4, self)
  46. # state /creation/state_3
  47. self.states["/creation/state_3"] = State(5, self)
  48. # state /creation/state_4
  49. self.states["/creation/state_4"] = State(6, self)
  50. # state /creation/state_5
  51. self.states["/creation/state_5"] = State(7, self)
  52. # state /creation/state_6
  53. self.states["/creation/state_6"] = State(8, self)
  54. # state /creation/state_7
  55. self.states["/creation/state_7"] = State(9, self)
  56. # state /creation/state_8
  57. self.states["/creation/state_8"] = State(10, self)
  58. # state /creation/state_9
  59. self.states["/creation/state_9"] = State(11, self)
  60. # state /creation/state_10
  61. self.states["/creation/state_10"] = State(12, self)
  62. # state /creation/state_11
  63. self.states["/creation/state_11"] = State(13, self)
  64. # state /creation/state_12
  65. self.states["/creation/state_12"] = State(14, self)
  66. # state /creation/state_13
  67. self.states["/creation/state_13"] = State(15, self)
  68. # state /creation/state_14
  69. self.states["/creation/state_14"] = State(16, self)
  70. # state /creation/state_15
  71. self.states["/creation/state_15"] = State(17, self)
  72. # state /creation/state_16
  73. self.states["/creation/state_16"] = State(18, self)
  74. # state /creation/state_17
  75. self.states["/creation/state_17"] = State(19, self)
  76. # state /creation/state_18
  77. self.states["/creation/state_18"] = State(20, self)
  78. # state /creation/state_19
  79. self.states["/creation/state_19"] = State(21, self)
  80. # state /creation/state_20
  81. self.states["/creation/state_20"] = State(22, self)
  82. # state /creation/state_21
  83. self.states["/creation/state_21"] = State(23, self)
  84. # state /creation/end
  85. self.states["/creation/end"] = State(24, self)
  86. # add children
  87. self.states[""].addChild(self.states["/error"])
  88. self.states[""].addChild(self.states["/creation"])
  89. self.states["/creation"].addChild(self.states["/creation/state_1"])
  90. self.states["/creation"].addChild(self.states["/creation/state_2"])
  91. self.states["/creation"].addChild(self.states["/creation/state_3"])
  92. self.states["/creation"].addChild(self.states["/creation/state_4"])
  93. self.states["/creation"].addChild(self.states["/creation/state_5"])
  94. self.states["/creation"].addChild(self.states["/creation/state_6"])
  95. self.states["/creation"].addChild(self.states["/creation/state_7"])
  96. self.states["/creation"].addChild(self.states["/creation/state_8"])
  97. self.states["/creation"].addChild(self.states["/creation/state_9"])
  98. self.states["/creation"].addChild(self.states["/creation/state_10"])
  99. self.states["/creation"].addChild(self.states["/creation/state_11"])
  100. self.states["/creation"].addChild(self.states["/creation/state_12"])
  101. self.states["/creation"].addChild(self.states["/creation/state_13"])
  102. self.states["/creation"].addChild(self.states["/creation/state_14"])
  103. self.states["/creation"].addChild(self.states["/creation/state_15"])
  104. self.states["/creation"].addChild(self.states["/creation/state_16"])
  105. self.states["/creation"].addChild(self.states["/creation/state_17"])
  106. self.states["/creation"].addChild(self.states["/creation/state_18"])
  107. self.states["/creation"].addChild(self.states["/creation/state_19"])
  108. self.states["/creation"].addChild(self.states["/creation/state_20"])
  109. self.states["/creation"].addChild(self.states["/creation/state_21"])
  110. self.states["/creation"].addChild(self.states["/creation/end"])
  111. self.states[""].fixTree()
  112. self.states[""].default_state = self.states["/creation"]
  113. self.states["/creation"].default_state = self.states["/creation/state_1"]
  114. # transition /creation/state_1
  115. _creation_state_1_0 = Transition(self, self.states["/creation/state_1"], [self.states["/creation/state_2"]])
  116. _creation_state_1_0.setAction(self._creation_state_1_0_exec)
  117. _creation_state_1_0.setTrigger(None)
  118. self.states["/creation/state_1"].addTransition(_creation_state_1_0)
  119. # transition /creation/state_2
  120. _creation_state_2_0 = Transition(self, self.states["/creation/state_2"], [self.states["/creation/state_3"]])
  121. _creation_state_2_0.setTrigger(Event("instance_created", None))
  122. self.states["/creation/state_2"].addTransition(_creation_state_2_0)
  123. # transition /creation/state_3
  124. _creation_state_3_0 = Transition(self, self.states["/creation/state_3"], [self.states["/creation/state_4"]])
  125. _creation_state_3_0.setAction(self._creation_state_3_0_exec)
  126. _creation_state_3_0.setTrigger(None)
  127. self.states["/creation/state_3"].addTransition(_creation_state_3_0)
  128. # transition /creation/state_4
  129. _creation_state_4_0 = Transition(self, self.states["/creation/state_4"], [self.states["/creation/state_5"]])
  130. _creation_state_4_0.setTrigger(Event("instance_created", None))
  131. self.states["/creation/state_4"].addTransition(_creation_state_4_0)
  132. # transition /creation/state_5
  133. _creation_state_5_0 = Transition(self, self.states["/creation/state_5"], [self.states["/creation/state_6"]])
  134. _creation_state_5_0.setAction(self._creation_state_5_0_exec)
  135. _creation_state_5_0.setTrigger(None)
  136. self.states["/creation/state_5"].addTransition(_creation_state_5_0)
  137. # transition /creation/state_6
  138. _creation_state_6_0 = Transition(self, self.states["/creation/state_6"], [self.states["/creation/state_7"]])
  139. _creation_state_6_0.setTrigger(Event("instance_created", None))
  140. self.states["/creation/state_6"].addTransition(_creation_state_6_0)
  141. # transition /creation/state_7
  142. _creation_state_7_0 = Transition(self, self.states["/creation/state_7"], [self.states["/creation/state_8"]])
  143. _creation_state_7_0.setAction(self._creation_state_7_0_exec)
  144. _creation_state_7_0.setTrigger(None)
  145. self.states["/creation/state_7"].addTransition(_creation_state_7_0)
  146. # transition /creation/state_8
  147. _creation_state_8_0 = Transition(self, self.states["/creation/state_8"], [self.states["/creation/state_9"]])
  148. _creation_state_8_0.setTrigger(Event("instance_created", None))
  149. self.states["/creation/state_8"].addTransition(_creation_state_8_0)
  150. # transition /creation/state_9
  151. _creation_state_9_0 = Transition(self, self.states["/creation/state_9"], [self.states["/creation/state_10"]])
  152. _creation_state_9_0.setAction(self._creation_state_9_0_exec)
  153. _creation_state_9_0.setTrigger(None)
  154. self.states["/creation/state_9"].addTransition(_creation_state_9_0)
  155. # transition /creation/state_10
  156. _creation_state_10_0 = Transition(self, self.states["/creation/state_10"], [self.states["/creation/state_11"]])
  157. _creation_state_10_0.setTrigger(Event("instance_created", None))
  158. self.states["/creation/state_10"].addTransition(_creation_state_10_0)
  159. # transition /creation/state_11
  160. _creation_state_11_0 = Transition(self, self.states["/creation/state_11"], [self.states["/creation/state_12"]])
  161. _creation_state_11_0.setAction(self._creation_state_11_0_exec)
  162. _creation_state_11_0.setTrigger(None)
  163. self.states["/creation/state_11"].addTransition(_creation_state_11_0)
  164. # transition /creation/state_12
  165. _creation_state_12_0 = Transition(self, self.states["/creation/state_12"], [self.states["/creation/state_13"]])
  166. _creation_state_12_0.setTrigger(Event("instance_created", None))
  167. self.states["/creation/state_12"].addTransition(_creation_state_12_0)
  168. # transition /creation/state_13
  169. _creation_state_13_0 = Transition(self, self.states["/creation/state_13"], [self.states["/creation/state_14"]])
  170. _creation_state_13_0.setAction(self._creation_state_13_0_exec)
  171. _creation_state_13_0.setTrigger(None)
  172. self.states["/creation/state_13"].addTransition(_creation_state_13_0)
  173. # transition /creation/state_14
  174. _creation_state_14_0 = Transition(self, self.states["/creation/state_14"], [self.states["/creation/state_15"]])
  175. _creation_state_14_0.setTrigger(Event("instance_created", None))
  176. self.states["/creation/state_14"].addTransition(_creation_state_14_0)
  177. # transition /creation/state_15
  178. _creation_state_15_0 = Transition(self, self.states["/creation/state_15"], [self.states["/creation/state_16"]])
  179. _creation_state_15_0.setAction(self._creation_state_15_0_exec)
  180. _creation_state_15_0.setTrigger(None)
  181. self.states["/creation/state_15"].addTransition(_creation_state_15_0)
  182. # transition /creation/state_16
  183. _creation_state_16_0 = Transition(self, self.states["/creation/state_16"], [self.states["/creation/state_17"]])
  184. _creation_state_16_0.setTrigger(Event("instance_created", None))
  185. self.states["/creation/state_16"].addTransition(_creation_state_16_0)
  186. # transition /creation/state_17
  187. _creation_state_17_0 = Transition(self, self.states["/creation/state_17"], [self.states["/creation/state_18"]])
  188. _creation_state_17_0.setAction(self._creation_state_17_0_exec)
  189. _creation_state_17_0.setTrigger(None)
  190. self.states["/creation/state_17"].addTransition(_creation_state_17_0)
  191. # transition /creation/state_18
  192. _creation_state_18_0 = Transition(self, self.states["/creation/state_18"], [self.states["/creation/state_19"]])
  193. _creation_state_18_0.setTrigger(Event("instance_created", None))
  194. self.states["/creation/state_18"].addTransition(_creation_state_18_0)
  195. # transition /creation/state_19
  196. _creation_state_19_0 = Transition(self, self.states["/creation/state_19"], [self.states["/creation/state_20"]])
  197. _creation_state_19_0.setAction(self._creation_state_19_0_exec)
  198. _creation_state_19_0.setTrigger(None)
  199. self.states["/creation/state_19"].addTransition(_creation_state_19_0)
  200. # transition /creation/state_20
  201. _creation_state_20_0 = Transition(self, self.states["/creation/state_20"], [self.states["/creation/state_21"]])
  202. _creation_state_20_0.setTrigger(Event("instance_created", None))
  203. self.states["/creation/state_20"].addTransition(_creation_state_20_0)
  204. # transition /creation/state_21
  205. _creation_state_21_0 = Transition(self, self.states["/creation/state_21"], [self.states["/creation/end"]])
  206. _creation_state_21_0.setAction(self._creation_state_21_0_exec)
  207. _creation_state_21_0.setTrigger(None)
  208. self.states["/creation/state_21"].addTransition(_creation_state_21_0)
  209. # transition /creation
  210. _creation_0 = Transition(self, self.states["/creation"], [self.states["/error"]])
  211. _creation_0.setAction(self._creation_0_exec)
  212. _creation_0.setTrigger(Event("instance_creation_error", None))
  213. self.states["/creation"].addTransition(_creation_0)
  214. _creation_1 = Transition(self, self.states["/creation"], [self.states["/error"]])
  215. _creation_1.setAction(self._creation_1_exec)
  216. _creation_1.setTrigger(Event("instance_association_error", None))
  217. self.states["/creation"].addTransition(_creation_1)
  218. def _creation_0_exec(self, parameters):
  219. print "Instance creation error!"
  220. def _creation_1_exec(self, parameters):
  221. print "Instance association error!"
  222. def _creation_state_1_0_exec(self, parameters):
  223. self.big_step.outputEventOM(Event("create_instance", None, [self, 'turret_control', 'TurretControl', self.tank]))
  224. def _creation_state_3_0_exec(self, parameters):
  225. self.big_step.outputEventOM(Event("create_instance", None, [self, 'motor_control', 'MotorControl', self.tank]))
  226. def _creation_state_5_0_exec(self, parameters):
  227. self.big_step.outputEventOM(Event("create_instance", None, [self, 'turret_steering', 'TurretSteering', self.tank]))
  228. def _creation_state_7_0_exec(self, parameters):
  229. self.big_step.outputEventOM(Event("create_instance", None, [self, 'steering', 'Steering', self.tank, self.map]))
  230. def _creation_state_9_0_exec(self, parameters):
  231. self.big_step.outputEventOM(Event("create_instance", None, [self, 'path_finder', 'PathFinder', self.tank, self.map]))
  232. def _creation_state_11_0_exec(self, parameters):
  233. self.big_step.outputEventOM(Event("create_instance", None, [self, 'attack_planner', 'AttackPlanner']))
  234. def _creation_state_13_0_exec(self, parameters):
  235. self.big_step.outputEventOM(Event("create_instance", None, [self, 'explore_planner', 'ExplorePlanner', self.tank, self.map]))
  236. def _creation_state_15_0_exec(self, parameters):
  237. self.big_step.outputEventOM(Event("create_instance", None, [self, 'pilot_strategy', 'PilotStrategy']))
  238. def _creation_state_17_0_exec(self, parameters):
  239. self.big_step.outputEventOM(Event("create_instance", None, [self, 'enemy_tracker', 'EnemyTracker']))
  240. def _creation_state_19_0_exec(self, parameters):
  241. self.big_step.outputEventOM(Event("create_instance", None, [self, 'radar', 'Radar', self.tank]))
  242. def _creation_state_21_0_exec(self, parameters):
  243. self.big_step.outputEventOM(Event("start_instance", None, [self, 'turret_control']))
  244. self.big_step.outputEventOM(Event("start_instance", None, [self, 'motor_control']))
  245. self.big_step.outputEventOM(Event("associate_instance", None, [self, 'turret_control', 'turret_steering/turret_control']))
  246. self.big_step.outputEventOM(Event("associate_instance", None, [self, 'attack_planner', 'turret_steering/attack_planner']))
  247. self.big_step.outputEventOM(Event("start_instance", None, [self, 'turret_steering']))
  248. self.big_step.outputEventOM(Event("associate_instance", None, [self, 'motor_control', 'steering/motor_control']))
  249. self.big_step.outputEventOM(Event("associate_instance", None, [self, 'path_finder', 'steering/path_finder']))
  250. self.big_step.outputEventOM(Event("start_instance", None, [self, 'steering']))
  251. self.big_step.outputEventOM(Event("associate_instance", None, [self, 'steering', 'path_finder/steering']))
  252. self.big_step.outputEventOM(Event("start_instance", None, [self, 'path_finder']))
  253. self.big_step.outputEventOM(Event("associate_instance", None, [self, 'turret_steering', 'attack_planner/turret_steering']))
  254. self.big_step.outputEventOM(Event("associate_instance", None, [self, 'turret_control', 'attack_planner/turret_control']))
  255. self.big_step.outputEventOM(Event("associate_instance", None, [self, 'path_finder', 'attack_planner/path_finder']))
  256. self.big_step.outputEventOM(Event("start_instance", None, [self, 'attack_planner']))
  257. self.big_step.outputEventOM(Event("associate_instance", None, [self, 'path_finder', 'explore_planner/path_finder']))
  258. self.big_step.outputEventOM(Event("start_instance", None, [self, 'explore_planner']))
  259. self.big_step.outputEventOM(Event("associate_instance", None, [self, 'explore_planner', 'pilot_strategy/explore_planner']))
  260. self.big_step.outputEventOM(Event("associate_instance", None, [self, 'attack_planner', 'pilot_strategy/attack_planner']))
  261. self.big_step.outputEventOM(Event("start_instance", None, [self, 'pilot_strategy']))
  262. self.big_step.outputEventOM(Event("associate_instance", None, [self, 'pilot_strategy', 'enemy_tracker/pilot_strategy']))
  263. self.big_step.outputEventOM(Event("associate_instance", None, [self, 'attack_planner', 'enemy_tracker/attack_planner']))
  264. self.big_step.outputEventOM(Event("start_instance", None, [self, 'enemy_tracker']))
  265. self.big_step.outputEventOM(Event("associate_instance", None, [self, 'pilot_strategy', 'radar/pilot_strategy']))
  266. self.big_step.outputEventOM(Event("associate_instance", None, [self, 'enemy_tracker', 'radar/enemy_tracker']))
  267. self.big_step.outputEventOM(Event("start_instance", None, [self, 'radar']))
  268. def initializeStatechart(self):
  269. # enter default state
  270. states = self.states["/creation"].getEffectiveTargetStates()
  271. self.updateConfiguration(states)
  272. for state in states:
  273. if state.enter:
  274. state.enter()
  275. class Radar(RuntimeClassBase):
  276. def __init__(self, controller, tank):
  277. RuntimeClassBase.__init__(self, controller)
  278. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  279. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  280. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  281. self.semantics.priority = StatechartSemantics.SourceParent
  282. self.semantics.concurrency = StatechartSemantics.Single
  283. # build Statechart structure
  284. self.build_statechart_structure()
  285. # user defined attributes
  286. self.tank = None
  287. self.range = 2000
  288. # call user defined constructor
  289. Radar.user_defined_constructor(self, tank)
  290. def user_defined_constructor(self, tank):
  291. self.tank = tank
  292. def user_defined_destructor(self):
  293. pass
  294. # user defined method
  295. def isEnemyVisible(self):
  296. sighted_list = self.tank.field.getSightedEnemies(self.tank, self.range)
  297. if len(sighted_list) > 0 :
  298. return True
  299. return False
  300. # user defined method
  301. def getEnemyPos(self):
  302. sighted_list = self.tank.field.getSightedEnemies(self.tank, self.range)
  303. if len(sighted_list) > 0 :
  304. sighted_list.sort(key=lambda x: x[1])
  305. return sighted_list[0][0]
  306. else :
  307. return (-1,-1)
  308. # builds Statechart structure
  309. def build_statechart_structure(self):
  310. # state <root>
  311. self.states[""] = State(0, self)
  312. # state /no_enemy
  313. self.states["/no_enemy"] = State(1, self)
  314. # state /enemy_in_sight
  315. self.states["/enemy_in_sight"] = State(2, self)
  316. # add children
  317. self.states[""].addChild(self.states["/no_enemy"])
  318. self.states[""].addChild(self.states["/enemy_in_sight"])
  319. self.states[""].fixTree()
  320. self.states[""].default_state = self.states["/no_enemy"]
  321. # transition /no_enemy
  322. _no_enemy_0 = Transition(self, self.states["/no_enemy"], [self.states["/enemy_in_sight"]])
  323. _no_enemy_0.setAction(self._no_enemy_0_exec)
  324. _no_enemy_0.setTrigger(None)
  325. _no_enemy_0.setGuard(self._no_enemy_0_guard)
  326. self.states["/no_enemy"].addTransition(_no_enemy_0)
  327. # transition /enemy_in_sight
  328. _enemy_in_sight_0 = Transition(self, self.states["/enemy_in_sight"], [self.states["/no_enemy"]])
  329. _enemy_in_sight_0.setAction(self._enemy_in_sight_0_exec)
  330. _enemy_in_sight_0.setTrigger(None)
  331. _enemy_in_sight_0.setGuard(self._enemy_in_sight_0_guard)
  332. self.states["/enemy_in_sight"].addTransition(_enemy_in_sight_0)
  333. _enemy_in_sight_1 = Transition(self, self.states["/enemy_in_sight"], [self.states["/enemy_in_sight"]])
  334. _enemy_in_sight_1.setAction(self._enemy_in_sight_1_exec)
  335. _enemy_in_sight_1.setTrigger(Event("update", "engine"))
  336. _enemy_in_sight_1.setGuard(self._enemy_in_sight_1_guard)
  337. self.states["/enemy_in_sight"].addTransition(_enemy_in_sight_1)
  338. def _no_enemy_0_exec(self, parameters):
  339. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'enemy_tracker', Event("enemy_sighted", None, [self.getEnemyPos()])]))
  340. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'pilot_strategy', Event("enemy_sighted", None, [self.getEnemyPos()])]))
  341. def _no_enemy_0_guard(self, parameters):
  342. return self.isEnemyVisible()
  343. def _enemy_in_sight_0_exec(self, parameters):
  344. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'enemy_tracker', Event("enemy_out_of_sight", None, [])]))
  345. def _enemy_in_sight_0_guard(self, parameters):
  346. return not self.isEnemyVisible()
  347. def _enemy_in_sight_1_exec(self, parameters):
  348. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'enemy_tracker', Event("enemy_pos", None, [self.getEnemyPos()])]))
  349. def _enemy_in_sight_1_guard(self, parameters):
  350. return self.isEnemyVisible()
  351. def initializeStatechart(self):
  352. # enter default state
  353. states = self.states["/no_enemy"].getEffectiveTargetStates()
  354. self.updateConfiguration(states)
  355. for state in states:
  356. if state.enter:
  357. state.enter()
  358. class EnemyTracker(RuntimeClassBase):
  359. def __init__(self, controller):
  360. RuntimeClassBase.__init__(self, controller)
  361. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  362. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  363. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  364. self.semantics.priority = StatechartSemantics.SourceParent
  365. self.semantics.concurrency = StatechartSemantics.Single
  366. # build Statechart structure
  367. self.build_statechart_structure()
  368. # user defined attributes
  369. self.enemy_pos = None
  370. # call user defined constructor
  371. EnemyTracker.user_defined_constructor(self)
  372. def user_defined_constructor(self):
  373. pass
  374. def user_defined_destructor(self):
  375. pass
  376. # user defined method
  377. def hasEnemyMoved(self, new_position):
  378. return new_position != self.enemy_pos
  379. # builds Statechart structure
  380. def build_statechart_structure(self):
  381. # state <root>
  382. self.states[""] = State(0, self)
  383. # state /no_enemy
  384. self.states["/no_enemy"] = State(1, self)
  385. # state /enemy_pos_known
  386. self.states["/enemy_pos_known"] = State(2, self)
  387. # state /enemy_pos_unsure
  388. self.states["/enemy_pos_unsure"] = State(3, self)
  389. # add children
  390. self.states[""].addChild(self.states["/no_enemy"])
  391. self.states[""].addChild(self.states["/enemy_pos_known"])
  392. self.states[""].addChild(self.states["/enemy_pos_unsure"])
  393. self.states[""].fixTree()
  394. self.states[""].default_state = self.states["/no_enemy"]
  395. # transition /no_enemy
  396. _no_enemy_0 = Transition(self, self.states["/no_enemy"], [self.states["/enemy_pos_known"]])
  397. _no_enemy_0.setAction(self._no_enemy_0_exec)
  398. _no_enemy_0.setTrigger(Event("enemy_sighted", None))
  399. self.states["/no_enemy"].addTransition(_no_enemy_0)
  400. # transition /enemy_pos_known
  401. _enemy_pos_known_0 = Transition(self, self.states["/enemy_pos_known"], [self.states["/enemy_pos_known"]])
  402. _enemy_pos_known_0.setAction(self._enemy_pos_known_0_exec)
  403. _enemy_pos_known_0.setTrigger(Event("enemy_pos", None))
  404. _enemy_pos_known_0.setGuard(self._enemy_pos_known_0_guard)
  405. self.states["/enemy_pos_known"].addTransition(_enemy_pos_known_0)
  406. _enemy_pos_known_1 = Transition(self, self.states["/enemy_pos_known"], [self.states["/enemy_pos_unsure"]])
  407. _enemy_pos_known_1.setAction(self._enemy_pos_known_1_exec)
  408. _enemy_pos_known_1.setTrigger(Event("enemy_out_of_sight", None))
  409. self.states["/enemy_pos_known"].addTransition(_enemy_pos_known_1)
  410. # transition /enemy_pos_unsure
  411. _enemy_pos_unsure_0 = Transition(self, self.states["/enemy_pos_unsure"], [self.states["/no_enemy"]])
  412. _enemy_pos_unsure_0.setAction(self._enemy_pos_unsure_0_exec)
  413. _enemy_pos_unsure_0.setTrigger(Event("destination_reached", None))
  414. self.states["/enemy_pos_unsure"].addTransition(_enemy_pos_unsure_0)
  415. _enemy_pos_unsure_1 = Transition(self, self.states["/enemy_pos_unsure"], [self.states["/enemy_pos_known"]])
  416. _enemy_pos_unsure_1.setAction(self._enemy_pos_unsure_1_exec)
  417. _enemy_pos_unsure_1.setTrigger(Event("enemy_sighted", None))
  418. self.states["/enemy_pos_unsure"].addTransition(_enemy_pos_unsure_1)
  419. def _no_enemy_0_exec(self, parameters):
  420. enemy_position = parameters[0]
  421. self.enemy_pos = enemy_position
  422. def _enemy_pos_known_0_exec(self, parameters):
  423. position = parameters[0]
  424. self.enemy_pos = position
  425. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'attack_planner', Event("enemy_pos_changed", None, [self.enemy_pos])]))
  426. def _enemy_pos_known_0_guard(self, parameters):
  427. position = parameters[0]
  428. return self.hasEnemyMoved(position)
  429. def _enemy_pos_known_1_exec(self, parameters):
  430. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'attack_planner', Event("enemy_out_of_sight", None, [])]))
  431. def _enemy_pos_unsure_0_exec(self, parameters):
  432. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'pilot_strategy', Event("enemy_lost", None, [])]))
  433. print "raise enemy_lost"
  434. def _enemy_pos_unsure_1_exec(self, parameters):
  435. position = parameters[0]
  436. self.enemy_pos = position
  437. def initializeStatechart(self):
  438. # enter default state
  439. states = self.states["/no_enemy"].getEffectiveTargetStates()
  440. self.updateConfiguration(states)
  441. for state in states:
  442. if state.enter:
  443. state.enter()
  444. class PilotStrategy(RuntimeClassBase):
  445. def __init__(self, controller):
  446. RuntimeClassBase.__init__(self, controller)
  447. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  448. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  449. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  450. self.semantics.priority = StatechartSemantics.SourceParent
  451. self.semantics.concurrency = StatechartSemantics.Single
  452. # build Statechart structure
  453. self.build_statechart_structure()
  454. # call user defined constructor
  455. PilotStrategy.user_defined_constructor(self)
  456. def user_defined_constructor(self):
  457. pass
  458. def user_defined_destructor(self):
  459. pass
  460. # builds Statechart structure
  461. def build_statechart_structure(self):
  462. # state <root>
  463. self.states[""] = State(0, self)
  464. # state /exploring
  465. self.states["/exploring"] = State(1, self)
  466. self.states["/exploring"].setEnter(self._exploring_enter)
  467. self.states["/exploring"].setExit(self._exploring_exit)
  468. # state /attacking
  469. self.states["/attacking"] = State(2, self)
  470. self.states["/attacking"].setExit(self._attacking_exit)
  471. # add children
  472. self.states[""].addChild(self.states["/exploring"])
  473. self.states[""].addChild(self.states["/attacking"])
  474. self.states[""].fixTree()
  475. self.states[""].default_state = self.states["/exploring"]
  476. # transition /exploring
  477. _exploring_0 = Transition(self, self.states["/exploring"], [self.states["/attacking"]])
  478. _exploring_0.setAction(self._exploring_0_exec)
  479. _exploring_0.setTrigger(Event("enemy_sighted", None))
  480. self.states["/exploring"].addTransition(_exploring_0)
  481. # transition /attacking
  482. _attacking_0 = Transition(self, self.states["/attacking"], [self.states["/exploring"]])
  483. _attacking_0.setAction(self._attacking_0_exec)
  484. _attacking_0.setTrigger(Event("enemy_lost", None))
  485. self.states["/attacking"].addTransition(_attacking_0)
  486. def _exploring_enter(self):
  487. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'explore_planner', Event("explore", None, [])]))
  488. def _exploring_exit(self):
  489. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'explore_planner', Event("stop_exploring", None, [])]))
  490. def _attacking_exit(self):
  491. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'attack_planner', Event("stop_attacking", None, [])]))
  492. print "raise stop_attacking"
  493. def _exploring_0_exec(self, parameters):
  494. position = parameters[0]
  495. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'attack_planner', Event("attack", None, [position])]))
  496. def _attacking_0_exec(self, parameters):
  497. print "received enemy lost"
  498. def initializeStatechart(self):
  499. # enter default state
  500. states = self.states["/exploring"].getEffectiveTargetStates()
  501. self.updateConfiguration(states)
  502. for state in states:
  503. if state.enter:
  504. state.enter()
  505. class ExplorePlanner(RuntimeClassBase):
  506. def __init__(self, controller, tank, aimap):
  507. RuntimeClassBase.__init__(self, controller)
  508. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  509. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  510. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  511. self.semantics.priority = StatechartSemantics.SourceParent
  512. self.semantics.concurrency = StatechartSemantics.Single
  513. # build Statechart structure
  514. self.build_statechart_structure()
  515. # user defined attributes
  516. self.map = None
  517. self.tank = None
  518. # call user defined constructor
  519. ExplorePlanner.user_defined_constructor(self, tank, aimap)
  520. def user_defined_constructor(self, tank, aimap):
  521. self.tank = tank
  522. self.map = aimap
  523. def user_defined_destructor(self):
  524. pass
  525. # builds Statechart structure
  526. def build_statechart_structure(self):
  527. # state <root>
  528. self.states[""] = State(0, self)
  529. # state /idle
  530. self.states["/idle"] = State(1, self)
  531. # state /exploring
  532. self.states["/exploring"] = State(2, self)
  533. # state /exploring/no_destination
  534. self.states["/exploring/no_destination"] = State(3, self)
  535. # state /exploring/destination_set
  536. self.states["/exploring/destination_set"] = State(4, self)
  537. # add children
  538. self.states[""].addChild(self.states["/idle"])
  539. self.states[""].addChild(self.states["/exploring"])
  540. self.states["/exploring"].addChild(self.states["/exploring/no_destination"])
  541. self.states["/exploring"].addChild(self.states["/exploring/destination_set"])
  542. self.states[""].fixTree()
  543. self.states[""].default_state = self.states["/idle"]
  544. self.states["/exploring"].default_state = self.states["/exploring/no_destination"]
  545. # transition /idle
  546. _idle_0 = Transition(self, self.states["/idle"], [self.states["/exploring"]])
  547. _idle_0.setTrigger(Event("explore", None))
  548. self.states["/idle"].addTransition(_idle_0)
  549. # transition /exploring/no_destination
  550. _exploring_no_destination_0 = Transition(self, self.states["/exploring/no_destination"], [self.states["/exploring/destination_set"]])
  551. _exploring_no_destination_0.setAction(self._exploring_no_destination_0_exec)
  552. _exploring_no_destination_0.setTrigger(None)
  553. self.states["/exploring/no_destination"].addTransition(_exploring_no_destination_0)
  554. # transition /exploring/destination_set
  555. _exploring_destination_set_0 = Transition(self, self.states["/exploring/destination_set"], [self.states["/exploring/no_destination"]])
  556. _exploring_destination_set_0.setTrigger(Event("destination_reached", None))
  557. self.states["/exploring/destination_set"].addTransition(_exploring_destination_set_0)
  558. # transition /exploring
  559. _exploring_0 = Transition(self, self.states["/exploring"], [self.states["/idle"]])
  560. _exploring_0.setAction(self._exploring_0_exec)
  561. _exploring_0.setTrigger(Event("stop_exploring", None))
  562. self.states["/exploring"].addTransition(_exploring_0)
  563. def _exploring_0_exec(self, parameters):
  564. print "stop_exploring received"
  565. def _exploring_no_destination_0_exec(self, parameters):
  566. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'path_finder', Event("new_destination", None, [self.map.getNewExplore(self.tank.getPosition(),self.tank.getAngle())])]))
  567. def initializeStatechart(self):
  568. # enter default state
  569. states = self.states["/idle"].getEffectiveTargetStates()
  570. self.updateConfiguration(states)
  571. for state in states:
  572. if state.enter:
  573. state.enter()
  574. class AttackPlanner(RuntimeClassBase):
  575. def __init__(self, controller):
  576. RuntimeClassBase.__init__(self, controller)
  577. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  578. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  579. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  580. self.semantics.priority = StatechartSemantics.SourceParent
  581. self.semantics.concurrency = StatechartSemantics.Single
  582. # build Statechart structure
  583. self.build_statechart_structure()
  584. # call user defined constructor
  585. AttackPlanner.user_defined_constructor(self)
  586. def user_defined_constructor(self):
  587. pass
  588. def user_defined_destructor(self):
  589. pass
  590. # builds Statechart structure
  591. def build_statechart_structure(self):
  592. # state <root>
  593. self.states[""] = State(0, self)
  594. # state /idle
  595. self.states["/idle"] = State(1, self)
  596. # state /action
  597. self.states["/action"] = ParallelState(2, self)
  598. # state /action/movement
  599. self.states["/action/movement"] = State(3, self)
  600. # state /action/movement/following
  601. self.states["/action/movement/following"] = State(4, self)
  602. # state /action/shooting
  603. self.states["/action/shooting"] = State(5, self)
  604. # state /action/shooting/loaded
  605. self.states["/action/shooting/loaded"] = State(6, self)
  606. # state /action/shooting/reloading
  607. self.states["/action/shooting/reloading"] = State(7, self)
  608. self.states["/action/shooting/reloading"].setEnter(self._action_shooting_reloading_enter)
  609. self.states["/action/shooting/reloading"].setExit(self._action_shooting_reloading_exit)
  610. # add children
  611. self.states[""].addChild(self.states["/idle"])
  612. self.states[""].addChild(self.states["/action"])
  613. self.states["/action"].addChild(self.states["/action/movement"])
  614. self.states["/action"].addChild(self.states["/action/shooting"])
  615. self.states["/action/movement"].addChild(self.states["/action/movement/following"])
  616. self.states["/action/shooting"].addChild(self.states["/action/shooting/loaded"])
  617. self.states["/action/shooting"].addChild(self.states["/action/shooting/reloading"])
  618. self.states[""].fixTree()
  619. self.states[""].default_state = self.states["/idle"]
  620. self.states["/action/movement"].default_state = self.states["/action/movement/following"]
  621. self.states["/action/shooting"].default_state = self.states["/action/shooting/loaded"]
  622. # transition /idle
  623. _idle_0 = Transition(self, self.states["/idle"], [self.states["/action"]])
  624. _idle_0.setAction(self._idle_0_exec)
  625. _idle_0.setTrigger(Event("attack", None))
  626. self.states["/idle"].addTransition(_idle_0)
  627. # transition /action/movement/following
  628. _action_movement_following_0 = Transition(self, self.states["/action/movement/following"], [self.states["/action/movement/following"]])
  629. _action_movement_following_0.setAction(self._action_movement_following_0_exec)
  630. _action_movement_following_0.setTrigger(Event("enemy_pos_changed", None))
  631. self.states["/action/movement/following"].addTransition(_action_movement_following_0)
  632. _action_movement_following_1 = Transition(self, self.states["/action/movement/following"], [self.states["/action/movement/following"]])
  633. _action_movement_following_1.setAction(self._action_movement_following_1_exec)
  634. _action_movement_following_1.setTrigger(Event("enemy_out_of_sight", None))
  635. self.states["/action/movement/following"].addTransition(_action_movement_following_1)
  636. # transition /action/shooting/loaded
  637. _action_shooting_loaded_0 = Transition(self, self.states["/action/shooting/loaded"], [self.states["/action/shooting/reloading"]])
  638. _action_shooting_loaded_0.setAction(self._action_shooting_loaded_0_exec)
  639. _action_shooting_loaded_0.setTrigger(Event("ready_to_shoot", None))
  640. self.states["/action/shooting/loaded"].addTransition(_action_shooting_loaded_0)
  641. # transition /action/shooting/reloading
  642. _action_shooting_reloading_0 = Transition(self, self.states["/action/shooting/reloading"], [self.states["/action/shooting/loaded"]])
  643. _action_shooting_reloading_0.setTrigger(Event("_0after"))
  644. self.states["/action/shooting/reloading"].addTransition(_action_shooting_reloading_0)
  645. # transition /action
  646. _action_0 = Transition(self, self.states["/action"], [self.states["/idle"]])
  647. _action_0.setAction(self._action_0_exec)
  648. _action_0.setTrigger(Event("stop_attacking", None))
  649. self.states["/action"].addTransition(_action_0)
  650. def _action_shooting_reloading_enter(self):
  651. self.addTimer(0, 0.5)
  652. def _action_shooting_reloading_exit(self):
  653. self.removeTimer(0)
  654. def _action_0_exec(self, parameters):
  655. print "received stop attack"
  656. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_steering', Event("stop_aiming", None, [])]))
  657. def _idle_0_exec(self, parameters):
  658. enemy_pos = parameters[0]
  659. print "received attack"
  660. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'path_finder', Event("new_destination", None, [enemy_pos])]))
  661. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_steering', Event("aim_at", None, [enemy_pos])]))
  662. def _action_movement_following_0_exec(self, parameters):
  663. enemy_pos = parameters[0]
  664. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'path_finder', Event("new_destination", None, [enemy_pos])]))
  665. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_steering', Event("aim_at", None, [enemy_pos])]))
  666. def _action_movement_following_1_exec(self, parameters):
  667. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_steering', Event("stop_aiming", None, [])]))
  668. def _action_shooting_loaded_0_exec(self, parameters):
  669. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_control', Event("shoot", None, [])]))
  670. print "raise shoot"
  671. def initializeStatechart(self):
  672. # enter default state
  673. states = self.states["/idle"].getEffectiveTargetStates()
  674. self.updateConfiguration(states)
  675. for state in states:
  676. if state.enter:
  677. state.enter()
  678. class PathFinder(RuntimeClassBase):
  679. def __init__(self, controller, tank, aimap):
  680. RuntimeClassBase.__init__(self, controller)
  681. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  682. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  683. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  684. self.semantics.priority = StatechartSemantics.SourceParent
  685. self.semantics.concurrency = StatechartSemantics.Single
  686. # build Statechart structure
  687. self.build_statechart_structure()
  688. # user defined attributes
  689. self.waypoints = []
  690. self.destination = (-1,-1)
  691. self.map = None
  692. self.tank = None
  693. # call user defined constructor
  694. PathFinder.user_defined_constructor(self, tank, aimap)
  695. def user_defined_constructor(self, tank, aimap):
  696. self.tank = tank
  697. self.map = aimap
  698. def user_defined_destructor(self):
  699. pass
  700. # user defined method
  701. def calculatePath(self):
  702. return self.map.calculatePath(self.tank.getPosition(), self.destination)
  703. # user defined method
  704. def requiresNewPath(self, new_destination):
  705. return self.map.calculateCell(self.destination) != self.map.calculateCell(new_destination)
  706. # user defined method
  707. def morePoints(self):
  708. return len(self.waypoints) > 0
  709. # builds Statechart structure
  710. def build_statechart_structure(self):
  711. # state <root>
  712. self.states[""] = State(0, self)
  713. # state /idle
  714. self.states["/idle"] = State(1, self)
  715. # state /check_points
  716. self.states["/check_points"] = State(2, self)
  717. # add children
  718. self.states[""].addChild(self.states["/idle"])
  719. self.states[""].addChild(self.states["/check_points"])
  720. self.states[""].fixTree()
  721. self.states[""].default_state = self.states["/idle"]
  722. # transition /idle
  723. _idle_0 = Transition(self, self.states["/idle"], [self.states["/check_points"]])
  724. _idle_0.setTrigger(Event("waypoint_reached", None))
  725. self.states["/idle"].addTransition(_idle_0)
  726. _idle_1 = Transition(self, self.states["/idle"], [self.states["/check_points"]])
  727. _idle_1.setAction(self._idle_1_exec)
  728. _idle_1.setTrigger(Event("new_destination", None))
  729. _idle_1.setGuard(self._idle_1_guard)
  730. self.states["/idle"].addTransition(_idle_1)
  731. # transition /check_points
  732. _check_points_0 = Transition(self, self.states["/check_points"], [self.states["/idle"]])
  733. _check_points_0.setAction(self._check_points_0_exec)
  734. _check_points_0.setTrigger(None)
  735. _check_points_0.setGuard(self._check_points_0_guard)
  736. self.states["/check_points"].addTransition(_check_points_0)
  737. _check_points_1 = Transition(self, self.states["/check_points"], [self.states["/idle"]])
  738. _check_points_1.setAction(self._check_points_1_exec)
  739. _check_points_1.setTrigger(None)
  740. _check_points_1.setGuard(self._check_points_1_guard)
  741. self.states["/check_points"].addTransition(_check_points_1)
  742. def _idle_1_exec(self, parameters):
  743. destination = parameters[0]
  744. self.destination = destination
  745. self.waypoints = self.calculatePath()
  746. def _idle_1_guard(self, parameters):
  747. destination = parameters[0]
  748. return self.requiresNewPath(destination)
  749. def _check_points_0_exec(self, parameters):
  750. next_waypoint = self.waypoints.pop(0)
  751. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'steering', Event("new_waypoint", None, [next_waypoint])]))
  752. def _check_points_0_guard(self, parameters):
  753. return self.morePoints()
  754. def _check_points_1_exec(self, parameters):
  755. self.big_step.outputEventOM(Event("broad_cast", None, [Event("destination_reached", None, [])]))
  756. def _check_points_1_guard(self, parameters):
  757. return not self.morePoints()
  758. def initializeStatechart(self):
  759. # enter default state
  760. states = self.states["/idle"].getEffectiveTargetStates()
  761. self.updateConfiguration(states)
  762. for state in states:
  763. if state.enter:
  764. state.enter()
  765. class Steering(RuntimeClassBase):
  766. def __init__(self, controller, tank, aimap):
  767. RuntimeClassBase.__init__(self, controller)
  768. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  769. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  770. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  771. self.semantics.priority = StatechartSemantics.SourceParent
  772. self.semantics.concurrency = StatechartSemantics.Single
  773. # build Statechart structure
  774. self.build_statechart_structure()
  775. # user defined attributes
  776. self.dest_waypoint = (-1,-1)
  777. self.dest_cell = (-1,-1)
  778. self.reaction_time = 0.05
  779. self.tank = None
  780. self.map = None
  781. self.margin = 0.2
  782. # call user defined constructor
  783. Steering.user_defined_constructor(self, tank, aimap)
  784. def user_defined_constructor(self, tank, aimap):
  785. self.tank = tank
  786. self.map = aimap
  787. def user_defined_destructor(self):
  788. pass
  789. # user defined method
  790. def pointReached(self):
  791. cell = self.map.calculateCell((self.tank.x, self.tank.y))
  792. return cell == self.dest_cell
  793. # user defined method
  794. def pointAhead(self):
  795. goal_angle = self.tank.angleToDest(self.dest_waypoint)
  796. diff = math.fabs(self.tank.angle - goal_angle)
  797. if diff <= (D45) :
  798. #self.tank.moveSpeed = int(math.ceil(((D45 - diff) / D45) * self.maxMoveSpeed))
  799. return True
  800. elif diff >= (D360 - D45) :
  801. #self.tank.moveSpeed = int(math.ceil(((diff - (D360 - D45)) / D45) * self.maxMoveSpeed))
  802. return True
  803. return False
  804. # user defined method
  805. def pointBehind(self):
  806. goal_angle = self.tank.angleToDest(self.dest_waypoint)
  807. diff = math.fabs(self.tank.angle - goal_angle)
  808. if diff <= (D45) or diff >= (D360 - D45) :
  809. return False
  810. return True
  811. # user defined method
  812. def pointRight(self):
  813. goal_angle = self.tank.angleToDest(self.dest_waypoint)
  814. diff = (self.tank.angle - goal_angle) % D360
  815. if diff >= self.margin and diff <= math.pi:
  816. return True
  817. return False
  818. # user defined method
  819. def pointLeft(self):
  820. goal_angle = self.tank.angleToDest(self.dest_waypoint)
  821. diff = (goal_angle - self.tank.angle) % D360
  822. if diff >= self.margin and diff <= math.pi:
  823. return True
  824. return False
  825. # user defined method
  826. def pointStraight(self):
  827. goal_angle = self.tank.angleToDest(self.dest_waypoint)
  828. diff = math.fabs(self.tank.angle - goal_angle)
  829. if diff < self.margin or diff > (D360- self.margin) :
  830. return True
  831. return False
  832. # builds Statechart structure
  833. def build_statechart_structure(self):
  834. # state <root>
  835. self.states[""] = State(0, self)
  836. # state /idle
  837. self.states["/idle"] = State(1, self)
  838. # state /steering
  839. self.states["/steering"] = State(2, self)
  840. # state /steering/forward_backward
  841. self.states["/steering/forward_backward"] = State(3, self)
  842. # state /steering/left_right
  843. self.states["/steering/left_right"] = State(4, self)
  844. # state /steering/wait
  845. self.states["/steering/wait"] = State(5, self)
  846. self.states["/steering/wait"].setEnter(self._steering_wait_enter)
  847. self.states["/steering/wait"].setExit(self._steering_wait_exit)
  848. # add children
  849. self.states[""].addChild(self.states["/idle"])
  850. self.states[""].addChild(self.states["/steering"])
  851. self.states["/steering"].addChild(self.states["/steering/forward_backward"])
  852. self.states["/steering"].addChild(self.states["/steering/left_right"])
  853. self.states["/steering"].addChild(self.states["/steering/wait"])
  854. self.states[""].fixTree()
  855. self.states[""].default_state = self.states["/idle"]
  856. self.states["/steering"].default_state = self.states["/steering/forward_backward"]
  857. # transition /idle
  858. _idle_0 = Transition(self, self.states["/idle"], [self.states["/steering"]])
  859. _idle_0.setAction(self._idle_0_exec)
  860. _idle_0.setTrigger(Event("new_waypoint", None))
  861. self.states["/idle"].addTransition(_idle_0)
  862. # transition /steering/forward_backward
  863. _steering_forward_backward_0 = Transition(self, self.states["/steering/forward_backward"], [self.states["/steering/left_right"]])
  864. _steering_forward_backward_0.setAction(self._steering_forward_backward_0_exec)
  865. _steering_forward_backward_0.setTrigger(None)
  866. _steering_forward_backward_0.setGuard(self._steering_forward_backward_0_guard)
  867. self.states["/steering/forward_backward"].addTransition(_steering_forward_backward_0)
  868. _steering_forward_backward_1 = Transition(self, self.states["/steering/forward_backward"], [self.states["/steering/left_right"]])
  869. _steering_forward_backward_1.setAction(self._steering_forward_backward_1_exec)
  870. _steering_forward_backward_1.setTrigger(None)
  871. _steering_forward_backward_1.setGuard(self._steering_forward_backward_1_guard)
  872. self.states["/steering/forward_backward"].addTransition(_steering_forward_backward_1)
  873. # transition /steering/left_right
  874. _steering_left_right_0 = Transition(self, self.states["/steering/left_right"], [self.states["/steering/wait"]])
  875. _steering_left_right_0.setAction(self._steering_left_right_0_exec)
  876. _steering_left_right_0.setTrigger(None)
  877. _steering_left_right_0.setGuard(self._steering_left_right_0_guard)
  878. self.states["/steering/left_right"].addTransition(_steering_left_right_0)
  879. _steering_left_right_1 = Transition(self, self.states["/steering/left_right"], [self.states["/steering/wait"]])
  880. _steering_left_right_1.setAction(self._steering_left_right_1_exec)
  881. _steering_left_right_1.setTrigger(None)
  882. _steering_left_right_1.setGuard(self._steering_left_right_1_guard)
  883. self.states["/steering/left_right"].addTransition(_steering_left_right_1)
  884. _steering_left_right_2 = Transition(self, self.states["/steering/left_right"], [self.states["/steering/wait"]])
  885. _steering_left_right_2.setAction(self._steering_left_right_2_exec)
  886. _steering_left_right_2.setTrigger(None)
  887. _steering_left_right_2.setGuard(self._steering_left_right_2_guard)
  888. self.states["/steering/left_right"].addTransition(_steering_left_right_2)
  889. # transition /steering/wait
  890. _steering_wait_0 = Transition(self, self.states["/steering/wait"], [self.states["/steering/forward_backward"]])
  891. _steering_wait_0.setTrigger(Event("_0after"))
  892. self.states["/steering/wait"].addTransition(_steering_wait_0)
  893. # transition /steering
  894. _steering_0 = Transition(self, self.states["/steering"], [self.states["/idle"]])
  895. _steering_0.setAction(self._steering_0_exec)
  896. _steering_0.setTrigger(None)
  897. _steering_0.setGuard(self._steering_0_guard)
  898. self.states["/steering"].addTransition(_steering_0)
  899. def _steering_wait_enter(self):
  900. self.addTimer(0, self.reaction_time)
  901. def _steering_wait_exit(self):
  902. self.removeTimer(0)
  903. def _steering_0_exec(self, parameters):
  904. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'path_finder', Event("waypoint_reached", None, [])]))
  905. def _steering_0_guard(self, parameters):
  906. return self.pointReached()
  907. def _idle_0_exec(self, parameters):
  908. dest_waypoint = parameters[0]
  909. self.dest_waypoint = dest_waypoint
  910. self.dest_cell = self.map.calculateCell(dest_waypoint)
  911. def _steering_forward_backward_0_exec(self, parameters):
  912. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'motor_control', Event("forward", None, [])]))
  913. def _steering_forward_backward_0_guard(self, parameters):
  914. return self.pointAhead()
  915. def _steering_forward_backward_1_exec(self, parameters):
  916. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'motor_control', Event("stop", None, [])]))
  917. def _steering_forward_backward_1_guard(self, parameters):
  918. return self.pointBehind()
  919. def _steering_left_right_0_exec(self, parameters):
  920. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'motor_control', Event("stop_turning", None, [])]))
  921. def _steering_left_right_0_guard(self, parameters):
  922. return self.pointStraight()
  923. def _steering_left_right_1_exec(self, parameters):
  924. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'motor_control', Event("turn_left", None, [])]))
  925. def _steering_left_right_1_guard(self, parameters):
  926. return self.pointLeft()
  927. def _steering_left_right_2_exec(self, parameters):
  928. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'motor_control', Event("turn_right", None, [])]))
  929. def _steering_left_right_2_guard(self, parameters):
  930. return self.pointRight()
  931. def initializeStatechart(self):
  932. # enter default state
  933. states = self.states["/idle"].getEffectiveTargetStates()
  934. self.updateConfiguration(states)
  935. for state in states:
  936. if state.enter:
  937. state.enter()
  938. class TurretSteering(RuntimeClassBase):
  939. def __init__(self, controller, tank):
  940. RuntimeClassBase.__init__(self, controller)
  941. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  942. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  943. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  944. self.semantics.priority = StatechartSemantics.SourceParent
  945. self.semantics.concurrency = StatechartSemantics.Single
  946. # build Statechart structure
  947. self.build_statechart_structure()
  948. # user defined attributes
  949. self.reaction_time = 0.05
  950. self.tank = None
  951. self.margin = None
  952. # call user defined constructor
  953. TurretSteering.user_defined_constructor(self, tank)
  954. def user_defined_constructor(self, tank):
  955. self.tank = tank
  956. self.margin = tank.cannonSpeed * D1
  957. def user_defined_destructor(self):
  958. pass
  959. # user defined method
  960. def pointRight(self):
  961. goal_angle = self.tank.angleToDest(self.target)
  962. diff = (self.tank.cannonAngle - goal_angle) % D360
  963. if diff >= self.margin and diff <= math.pi:
  964. return True
  965. return False
  966. # user defined method
  967. def pointLeft(self):
  968. goal_angle = self.tank.angleToDest(self.target)
  969. diff = (goal_angle - self.tank.cannonAngle) % D360
  970. if diff >= self.margin and diff <= math.pi:
  971. return True
  972. return False
  973. # user defined method
  974. def pointCorrect(self):
  975. goal_angle = self.tank.angleToDest(self.target)
  976. diff = math.fabs(goal_angle - self.tank.cannonAngle)
  977. if diff < self.margin or diff > (D360- self.margin):
  978. return True
  979. return False
  980. # builds Statechart structure
  981. def build_statechart_structure(self):
  982. # state <root>
  983. self.states[""] = State(0, self)
  984. # state /idle
  985. self.states["/idle"] = State(1, self)
  986. # state /aiming
  987. self.states["/aiming"] = State(2, self)
  988. # state /aiming/adjust
  989. self.states["/aiming/adjust"] = State(3, self)
  990. # state /aiming/wait
  991. self.states["/aiming/wait"] = State(4, self)
  992. self.states["/aiming/wait"].setEnter(self._aiming_wait_enter)
  993. self.states["/aiming/wait"].setExit(self._aiming_wait_exit)
  994. # add children
  995. self.states[""].addChild(self.states["/idle"])
  996. self.states[""].addChild(self.states["/aiming"])
  997. self.states["/aiming"].addChild(self.states["/aiming/adjust"])
  998. self.states["/aiming"].addChild(self.states["/aiming/wait"])
  999. self.states[""].fixTree()
  1000. self.states[""].default_state = self.states["/idle"]
  1001. self.states["/aiming"].default_state = self.states["/aiming/adjust"]
  1002. # transition /idle
  1003. _idle_0 = Transition(self, self.states["/idle"], [self.states["/aiming"]])
  1004. _idle_0.setAction(self._idle_0_exec)
  1005. _idle_0.setTrigger(Event("aim_at", None))
  1006. self.states["/idle"].addTransition(_idle_0)
  1007. # transition /aiming/adjust
  1008. _aiming_adjust_0 = Transition(self, self.states["/aiming/adjust"], [self.states["/aiming/wait"]])
  1009. _aiming_adjust_0.setAction(self._aiming_adjust_0_exec)
  1010. _aiming_adjust_0.setTrigger(None)
  1011. _aiming_adjust_0.setGuard(self._aiming_adjust_0_guard)
  1012. self.states["/aiming/adjust"].addTransition(_aiming_adjust_0)
  1013. _aiming_adjust_1 = Transition(self, self.states["/aiming/adjust"], [self.states["/aiming/wait"]])
  1014. _aiming_adjust_1.setAction(self._aiming_adjust_1_exec)
  1015. _aiming_adjust_1.setTrigger(None)
  1016. _aiming_adjust_1.setGuard(self._aiming_adjust_1_guard)
  1017. self.states["/aiming/adjust"].addTransition(_aiming_adjust_1)
  1018. _aiming_adjust_2 = Transition(self, self.states["/aiming/adjust"], [self.states["/aiming/wait"]])
  1019. _aiming_adjust_2.setAction(self._aiming_adjust_2_exec)
  1020. _aiming_adjust_2.setTrigger(None)
  1021. _aiming_adjust_2.setGuard(self._aiming_adjust_2_guard)
  1022. self.states["/aiming/adjust"].addTransition(_aiming_adjust_2)
  1023. # transition /aiming/wait
  1024. _aiming_wait_0 = Transition(self, self.states["/aiming/wait"], [self.states["/aiming/adjust"]])
  1025. _aiming_wait_0.setTrigger(Event("_0after"))
  1026. self.states["/aiming/wait"].addTransition(_aiming_wait_0)
  1027. # transition /aiming
  1028. _aiming_0 = Transition(self, self.states["/aiming"], [self.states["/idle"]])
  1029. _aiming_0.setAction(self._aiming_0_exec)
  1030. _aiming_0.setTrigger(Event("stop_aiming", None))
  1031. self.states["/aiming"].addTransition(_aiming_0)
  1032. _aiming_1 = Transition(self, self.states["/aiming"], [self.states["/aiming"]])
  1033. _aiming_1.setAction(self._aiming_1_exec)
  1034. _aiming_1.setTrigger(Event("aim_at", None))
  1035. self.states["/aiming"].addTransition(_aiming_1)
  1036. def _aiming_wait_enter(self):
  1037. self.addTimer(0, self.reaction_time)
  1038. def _aiming_wait_exit(self):
  1039. self.removeTimer(0)
  1040. def _aiming_0_exec(self, parameters):
  1041. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_control', Event("stop_turning", None, [])]))
  1042. def _aiming_1_exec(self, parameters):
  1043. target = parameters[0]
  1044. self.target = target
  1045. def _idle_0_exec(self, parameters):
  1046. target = parameters[0]
  1047. self.target = target
  1048. def _aiming_adjust_0_exec(self, parameters):
  1049. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_control', Event("turn_right", None, [])]))
  1050. def _aiming_adjust_0_guard(self, parameters):
  1051. return self.pointRight()
  1052. def _aiming_adjust_1_exec(self, parameters):
  1053. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_control', Event("turn_left", None, [])]))
  1054. def _aiming_adjust_1_guard(self, parameters):
  1055. return self.pointLeft()
  1056. def _aiming_adjust_2_exec(self, parameters):
  1057. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_control', Event("stop_turning", None, [])]))
  1058. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'attack_planner', Event("ready_to_shoot", None, [])]))
  1059. def _aiming_adjust_2_guard(self, parameters):
  1060. return self.pointCorrect()
  1061. def initializeStatechart(self):
  1062. # enter default state
  1063. states = self.states["/idle"].getEffectiveTargetStates()
  1064. self.updateConfiguration(states)
  1065. for state in states:
  1066. if state.enter:
  1067. state.enter()
  1068. class MotorControl(RuntimeClassBase):
  1069. def __init__(self, controller, tank):
  1070. RuntimeClassBase.__init__(self, controller)
  1071. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  1072. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  1073. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  1074. self.semantics.priority = StatechartSemantics.SourceParent
  1075. self.semantics.concurrency = StatechartSemantics.Single
  1076. # build Statechart structure
  1077. self.build_statechart_structure()
  1078. # user defined attributes
  1079. self.tank = None
  1080. # call user defined constructor
  1081. MotorControl.user_defined_constructor(self, tank)
  1082. def user_defined_constructor(self, tank):
  1083. self.tank = tank
  1084. def user_defined_destructor(self):
  1085. pass
  1086. # builds Statechart structure
  1087. def build_statechart_structure(self):
  1088. # state <root>
  1089. self.states[""] = State(0, self)
  1090. # state /control
  1091. self.states["/control"] = ParallelState(1, self)
  1092. # state /control/left_right
  1093. self.states["/control/left_right"] = State(2, self)
  1094. # state /control/left_right/stop
  1095. self.states["/control/left_right/stop"] = State(3, self)
  1096. # state /control/left_right/going_forward
  1097. self.states["/control/left_right/going_forward"] = State(4, self)
  1098. # state /control/left_right/going_backward
  1099. self.states["/control/left_right/going_backward"] = State(5, self)
  1100. # state /control/forward_backward
  1101. self.states["/control/forward_backward"] = State(6, self)
  1102. # state /control/forward_backward/straight
  1103. self.states["/control/forward_backward/straight"] = State(7, self)
  1104. # state /control/forward_backward/turning_left
  1105. self.states["/control/forward_backward/turning_left"] = State(8, self)
  1106. # state /control/forward_backward/turning_right
  1107. self.states["/control/forward_backward/turning_right"] = State(9, self)
  1108. # add children
  1109. self.states[""].addChild(self.states["/control"])
  1110. self.states["/control"].addChild(self.states["/control/left_right"])
  1111. self.states["/control"].addChild(self.states["/control/forward_backward"])
  1112. self.states["/control/left_right"].addChild(self.states["/control/left_right/stop"])
  1113. self.states["/control/left_right"].addChild(self.states["/control/left_right/going_forward"])
  1114. self.states["/control/left_right"].addChild(self.states["/control/left_right/going_backward"])
  1115. self.states["/control/forward_backward"].addChild(self.states["/control/forward_backward/straight"])
  1116. self.states["/control/forward_backward"].addChild(self.states["/control/forward_backward/turning_left"])
  1117. self.states["/control/forward_backward"].addChild(self.states["/control/forward_backward/turning_right"])
  1118. self.states[""].fixTree()
  1119. self.states[""].default_state = self.states["/control"]
  1120. self.states["/control/left_right"].default_state = self.states["/control/left_right/stop"]
  1121. self.states["/control/forward_backward"].default_state = self.states["/control/forward_backward/straight"]
  1122. # transition /control/left_right/stop
  1123. _control_left_right_stop_0 = Transition(self, self.states["/control/left_right/stop"], [self.states["/control/left_right/going_forward"]])
  1124. _control_left_right_stop_0.setTrigger(Event("forward", None))
  1125. self.states["/control/left_right/stop"].addTransition(_control_left_right_stop_0)
  1126. _control_left_right_stop_1 = Transition(self, self.states["/control/left_right/stop"], [self.states["/control/left_right/going_backward"]])
  1127. _control_left_right_stop_1.setTrigger(Event("backward", None))
  1128. self.states["/control/left_right/stop"].addTransition(_control_left_right_stop_1)
  1129. # transition /control/left_right/going_forward
  1130. _control_left_right_going_forward_0 = Transition(self, self.states["/control/left_right/going_forward"], [self.states["/control/left_right/stop"]])
  1131. _control_left_right_going_forward_0.setTrigger(Event("stop", None))
  1132. self.states["/control/left_right/going_forward"].addTransition(_control_left_right_going_forward_0)
  1133. _control_left_right_going_forward_1 = Transition(self, self.states["/control/left_right/going_forward"], [self.states["/control/left_right/going_backward"]])
  1134. _control_left_right_going_forward_1.setTrigger(Event("backward", None))
  1135. self.states["/control/left_right/going_forward"].addTransition(_control_left_right_going_forward_1)
  1136. _control_left_right_going_forward_2 = Transition(self, self.states["/control/left_right/going_forward"], [self.states["/control/left_right/going_forward"]])
  1137. _control_left_right_going_forward_2.setAction(self._control_left_right_going_forward_2_exec)
  1138. _control_left_right_going_forward_2.setTrigger(Event("update", "engine"))
  1139. self.states["/control/left_right/going_forward"].addTransition(_control_left_right_going_forward_2)
  1140. # transition /control/left_right/going_backward
  1141. _control_left_right_going_backward_0 = Transition(self, self.states["/control/left_right/going_backward"], [self.states["/control/left_right/stop"]])
  1142. _control_left_right_going_backward_0.setTrigger(Event("stop", None))
  1143. self.states["/control/left_right/going_backward"].addTransition(_control_left_right_going_backward_0)
  1144. _control_left_right_going_backward_1 = Transition(self, self.states["/control/left_right/going_backward"], [self.states["/control/left_right/going_forward"]])
  1145. _control_left_right_going_backward_1.setTrigger(Event("forward", None))
  1146. self.states["/control/left_right/going_backward"].addTransition(_control_left_right_going_backward_1)
  1147. _control_left_right_going_backward_2 = Transition(self, self.states["/control/left_right/going_backward"], [self.states["/control/left_right/going_backward"]])
  1148. _control_left_right_going_backward_2.setAction(self._control_left_right_going_backward_2_exec)
  1149. _control_left_right_going_backward_2.setTrigger(Event("update", "engine"))
  1150. self.states["/control/left_right/going_backward"].addTransition(_control_left_right_going_backward_2)
  1151. # transition /control/forward_backward/straight
  1152. _control_forward_backward_straight_0 = Transition(self, self.states["/control/forward_backward/straight"], [self.states["/control/forward_backward/turning_right"]])
  1153. _control_forward_backward_straight_0.setTrigger(Event("turn_right", None))
  1154. self.states["/control/forward_backward/straight"].addTransition(_control_forward_backward_straight_0)
  1155. _control_forward_backward_straight_1 = Transition(self, self.states["/control/forward_backward/straight"], [self.states["/control/forward_backward/turning_left"]])
  1156. _control_forward_backward_straight_1.setTrigger(Event("turn_left", None))
  1157. self.states["/control/forward_backward/straight"].addTransition(_control_forward_backward_straight_1)
  1158. # transition /control/forward_backward/turning_left
  1159. _control_forward_backward_turning_left_0 = Transition(self, self.states["/control/forward_backward/turning_left"], [self.states["/control/forward_backward/straight"]])
  1160. _control_forward_backward_turning_left_0.setTrigger(Event("stop_turning", None))
  1161. self.states["/control/forward_backward/turning_left"].addTransition(_control_forward_backward_turning_left_0)
  1162. _control_forward_backward_turning_left_1 = Transition(self, self.states["/control/forward_backward/turning_left"], [self.states["/control/forward_backward/turning_right"]])
  1163. _control_forward_backward_turning_left_1.setTrigger(Event("turn_right", None))
  1164. self.states["/control/forward_backward/turning_left"].addTransition(_control_forward_backward_turning_left_1)
  1165. _control_forward_backward_turning_left_2 = Transition(self, self.states["/control/forward_backward/turning_left"], [self.states["/control/forward_backward/turning_left"]])
  1166. _control_forward_backward_turning_left_2.setAction(self._control_forward_backward_turning_left_2_exec)
  1167. _control_forward_backward_turning_left_2.setTrigger(Event("update", "engine"))
  1168. self.states["/control/forward_backward/turning_left"].addTransition(_control_forward_backward_turning_left_2)
  1169. # transition /control/forward_backward/turning_right
  1170. _control_forward_backward_turning_right_0 = Transition(self, self.states["/control/forward_backward/turning_right"], [self.states["/control/forward_backward/straight"]])
  1171. _control_forward_backward_turning_right_0.setTrigger(Event("stop_turning", None))
  1172. self.states["/control/forward_backward/turning_right"].addTransition(_control_forward_backward_turning_right_0)
  1173. _control_forward_backward_turning_right_1 = Transition(self, self.states["/control/forward_backward/turning_right"], [self.states["/control/forward_backward/turning_left"]])
  1174. _control_forward_backward_turning_right_1.setTrigger(Event("turn_left", None))
  1175. self.states["/control/forward_backward/turning_right"].addTransition(_control_forward_backward_turning_right_1)
  1176. _control_forward_backward_turning_right_2 = Transition(self, self.states["/control/forward_backward/turning_right"], [self.states["/control/forward_backward/turning_right"]])
  1177. _control_forward_backward_turning_right_2.setAction(self._control_forward_backward_turning_right_2_exec)
  1178. _control_forward_backward_turning_right_2.setTrigger(Event("update", "engine"))
  1179. self.states["/control/forward_backward/turning_right"].addTransition(_control_forward_backward_turning_right_2)
  1180. def _control_left_right_going_forward_2_exec(self, parameters):
  1181. self.tank.moveUp()
  1182. def _control_left_right_going_backward_2_exec(self, parameters):
  1183. self.tank.moveDown()
  1184. def _control_forward_backward_turning_left_2_exec(self, parameters):
  1185. self.tank.turnLeft()
  1186. def _control_forward_backward_turning_right_2_exec(self, parameters):
  1187. self.tank.turnRight()
  1188. def initializeStatechart(self):
  1189. # enter default state
  1190. states = self.states["/control"].getEffectiveTargetStates()
  1191. self.updateConfiguration(states)
  1192. for state in states:
  1193. if state.enter:
  1194. state.enter()
  1195. class TurretControl(RuntimeClassBase):
  1196. def __init__(self, controller, tank):
  1197. RuntimeClassBase.__init__(self, controller)
  1198. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  1199. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  1200. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  1201. self.semantics.priority = StatechartSemantics.SourceParent
  1202. self.semantics.concurrency = StatechartSemantics.Single
  1203. # build Statechart structure
  1204. self.build_statechart_structure()
  1205. # user defined attributes
  1206. self.tank = None
  1207. # call user defined constructor
  1208. TurretControl.user_defined_constructor(self, tank)
  1209. def user_defined_constructor(self, tank):
  1210. self.tank = tank
  1211. def user_defined_destructor(self):
  1212. pass
  1213. # builds Statechart structure
  1214. def build_statechart_structure(self):
  1215. # state <root>
  1216. self.states[""] = State(0, self)
  1217. # state /turret
  1218. self.states["/turret"] = ParallelState(1, self)
  1219. # state /turret/rotation
  1220. self.states["/turret/rotation"] = State(2, self)
  1221. # state /turret/rotation/none
  1222. self.states["/turret/rotation/none"] = State(3, self)
  1223. # state /turret/rotation/turning_left
  1224. self.states["/turret/rotation/turning_left"] = State(4, self)
  1225. # state /turret/rotation/turning_right
  1226. self.states["/turret/rotation/turning_right"] = State(5, self)
  1227. # state /turret/shooting
  1228. self.states["/turret/shooting"] = State(6, self)
  1229. # state /turret/shooting/polling
  1230. self.states["/turret/shooting/polling"] = State(7, self)
  1231. # add children
  1232. self.states[""].addChild(self.states["/turret"])
  1233. self.states["/turret"].addChild(self.states["/turret/rotation"])
  1234. self.states["/turret"].addChild(self.states["/turret/shooting"])
  1235. self.states["/turret/rotation"].addChild(self.states["/turret/rotation/none"])
  1236. self.states["/turret/rotation"].addChild(self.states["/turret/rotation/turning_left"])
  1237. self.states["/turret/rotation"].addChild(self.states["/turret/rotation/turning_right"])
  1238. self.states["/turret/shooting"].addChild(self.states["/turret/shooting/polling"])
  1239. self.states[""].fixTree()
  1240. self.states[""].default_state = self.states["/turret"]
  1241. self.states["/turret/rotation"].default_state = self.states["/turret/rotation/none"]
  1242. self.states["/turret/shooting"].default_state = self.states["/turret/shooting/polling"]
  1243. # transition /turret/rotation/none
  1244. _turret_rotation_none_0 = Transition(self, self.states["/turret/rotation/none"], [self.states["/turret/rotation/turning_right"]])
  1245. _turret_rotation_none_0.setTrigger(Event("turn_right", None))
  1246. self.states["/turret/rotation/none"].addTransition(_turret_rotation_none_0)
  1247. _turret_rotation_none_1 = Transition(self, self.states["/turret/rotation/none"], [self.states["/turret/rotation/turning_left"]])
  1248. _turret_rotation_none_1.setTrigger(Event("turn_left", None))
  1249. self.states["/turret/rotation/none"].addTransition(_turret_rotation_none_1)
  1250. # transition /turret/rotation/turning_left
  1251. _turret_rotation_turning_left_0 = Transition(self, self.states["/turret/rotation/turning_left"], [self.states["/turret/rotation/none"]])
  1252. _turret_rotation_turning_left_0.setTrigger(Event("stop_turning", None))
  1253. self.states["/turret/rotation/turning_left"].addTransition(_turret_rotation_turning_left_0)
  1254. _turret_rotation_turning_left_1 = Transition(self, self.states["/turret/rotation/turning_left"], [self.states["/turret/rotation/turning_right"]])
  1255. _turret_rotation_turning_left_1.setTrigger(Event("turn_right", None))
  1256. self.states["/turret/rotation/turning_left"].addTransition(_turret_rotation_turning_left_1)
  1257. _turret_rotation_turning_left_2 = Transition(self, self.states["/turret/rotation/turning_left"], [self.states["/turret/rotation/turning_left"]])
  1258. _turret_rotation_turning_left_2.setAction(self._turret_rotation_turning_left_2_exec)
  1259. _turret_rotation_turning_left_2.setTrigger(Event("update", "engine"))
  1260. self.states["/turret/rotation/turning_left"].addTransition(_turret_rotation_turning_left_2)
  1261. # transition /turret/rotation/turning_right
  1262. _turret_rotation_turning_right_0 = Transition(self, self.states["/turret/rotation/turning_right"], [self.states["/turret/rotation/none"]])
  1263. _turret_rotation_turning_right_0.setTrigger(Event("stop_turning", None))
  1264. self.states["/turret/rotation/turning_right"].addTransition(_turret_rotation_turning_right_0)
  1265. _turret_rotation_turning_right_1 = Transition(self, self.states["/turret/rotation/turning_right"], [self.states["/turret/rotation/turning_left"]])
  1266. _turret_rotation_turning_right_1.setTrigger(Event("turn_left", None))
  1267. self.states["/turret/rotation/turning_right"].addTransition(_turret_rotation_turning_right_1)
  1268. _turret_rotation_turning_right_2 = Transition(self, self.states["/turret/rotation/turning_right"], [self.states["/turret/rotation/turning_right"]])
  1269. _turret_rotation_turning_right_2.setAction(self._turret_rotation_turning_right_2_exec)
  1270. _turret_rotation_turning_right_2.setTrigger(Event("update", "engine"))
  1271. self.states["/turret/rotation/turning_right"].addTransition(_turret_rotation_turning_right_2)
  1272. # transition /turret/shooting/polling
  1273. _turret_shooting_polling_0 = Transition(self, self.states["/turret/shooting/polling"], [self.states["/turret/shooting/polling"]])
  1274. _turret_shooting_polling_0.setAction(self._turret_shooting_polling_0_exec)
  1275. _turret_shooting_polling_0.setTrigger(Event("shoot", None))
  1276. self.states["/turret/shooting/polling"].addTransition(_turret_shooting_polling_0)
  1277. def _turret_rotation_turning_left_2_exec(self, parameters):
  1278. self.tank.turnCannonLeft()
  1279. def _turret_rotation_turning_right_2_exec(self, parameters):
  1280. self.tank.turnCannonRight()
  1281. def _turret_shooting_polling_0_exec(self, parameters):
  1282. self.tank.shoot()
  1283. def initializeStatechart(self):
  1284. # enter default state
  1285. states = self.states["/turret"].getEffectiveTargetStates()
  1286. self.updateConfiguration(states)
  1287. for state in states:
  1288. if state.enter:
  1289. state.enter()
  1290. class ObjectManager(ObjectManagerBase):
  1291. def __init__(self, controller):
  1292. ObjectManagerBase.__init__(self, controller)
  1293. def instantiate(self, class_name, construct_params):
  1294. if class_name == "Main":
  1295. instance = Main(self.controller, construct_params[0])
  1296. instance.associations = {}
  1297. instance.associations["radar"] = Association("Radar", 1, 1)
  1298. instance.associations["enemy_tracker"] = Association("EnemyTracker", 1, 1)
  1299. instance.associations["pilot_strategy"] = Association("PilotStrategy", 1, 1)
  1300. instance.associations["explore_planner"] = Association("ExplorePlanner", 1, 1)
  1301. instance.associations["attack_planner"] = Association("AttackPlanner", 1, 1)
  1302. instance.associations["path_finder"] = Association("PathFinder", 1, 1)
  1303. instance.associations["steering"] = Association("Steering", 1, 1)
  1304. instance.associations["turret_steering"] = Association("TurretSteering", 1, 1)
  1305. instance.associations["motor_control"] = Association("MotorControl", 1, 1)
  1306. instance.associations["turret_control"] = Association("TurretControl", 1, 1)
  1307. elif class_name == "Radar":
  1308. instance = Radar(self.controller, construct_params[0])
  1309. instance.associations = {}
  1310. instance.associations["enemy_tracker"] = Association("EnemyTracker", 1, 1)
  1311. instance.associations["pilot_strategy"] = Association("PilotStrategy", 1, 1)
  1312. elif class_name == "EnemyTracker":
  1313. instance = EnemyTracker(self.controller)
  1314. instance.associations = {}
  1315. instance.associations["pilot_strategy"] = Association("PilotStrategy", 1, 1)
  1316. instance.associations["attack_planner"] = Association("AttackPlanner", 1, 1)
  1317. elif class_name == "PilotStrategy":
  1318. instance = PilotStrategy(self.controller)
  1319. instance.associations = {}
  1320. instance.associations["explore_planner"] = Association("ExplorePlanner", 1, 1)
  1321. instance.associations["attack_planner"] = Association("AttackPlanner", 1, 1)
  1322. elif class_name == "ExplorePlanner":
  1323. instance = ExplorePlanner(self.controller, construct_params[0], construct_params[1])
  1324. instance.associations = {}
  1325. instance.associations["path_finder"] = Association("PathFinder", 1, 1)
  1326. elif class_name == "AttackPlanner":
  1327. instance = AttackPlanner(self.controller)
  1328. instance.associations = {}
  1329. instance.associations["turret_steering"] = Association("TurretSteering", 1, 1)
  1330. instance.associations["path_finder"] = Association("PathFinder", 1, 1)
  1331. instance.associations["turret_control"] = Association("TurretControl", 1, 1)
  1332. elif class_name == "PathFinder":
  1333. instance = PathFinder(self.controller, construct_params[0], construct_params[1])
  1334. instance.associations = {}
  1335. instance.associations["steering"] = Association("Steering", 1, 1)
  1336. elif class_name == "Steering":
  1337. instance = Steering(self.controller, construct_params[0], construct_params[1])
  1338. instance.associations = {}
  1339. instance.associations["motor_control"] = Association("MotorControl", 1, 1)
  1340. instance.associations["path_finder"] = Association("PathFinder", 1, 1)
  1341. elif class_name == "TurretSteering":
  1342. instance = TurretSteering(self.controller, construct_params[0])
  1343. instance.associations = {}
  1344. instance.associations["turret_control"] = Association("TurretControl", 1, 1)
  1345. instance.associations["attack_planner"] = Association("AttackPlanner", 1, 1)
  1346. elif class_name == "MotorControl":
  1347. instance = MotorControl(self.controller, construct_params[0])
  1348. instance.associations = {}
  1349. elif class_name == "TurretControl":
  1350. instance = TurretControl(self.controller, construct_params[0])
  1351. instance.associations = {}
  1352. else:
  1353. raise Exception("Cannot instantiate class " + class_name)
  1354. return instance
  1355. class Controller(EventLoopControllerBase):
  1356. def __init__(self, tank, event_loop_callbacks, finished_callback = None, behind_schedule_callback = None):
  1357. if finished_callback == None: finished_callback = None
  1358. if behind_schedule_callback == None: behind_schedule_callback = None
  1359. EventLoopControllerBase.__init__(self, ObjectManager(self), event_loop_callbacks, finished_callback, behind_schedule_callback)
  1360. self.addInputPort("engine")
  1361. self.object_manager.createInstance("Main", [tank])