ai_controller.py 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  1. """
  2. Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
  3. Date: Wed Aug 17 13:32:51 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. self.default_targets = self.states["/creation"].getEffectiveTargetStates()
  271. RuntimeClassBase.initializeStatechart(self)
  272. class Radar(RuntimeClassBase):
  273. def __init__(self, controller, tank):
  274. RuntimeClassBase.__init__(self, controller)
  275. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  276. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  277. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  278. self.semantics.priority = StatechartSemantics.SourceParent
  279. self.semantics.concurrency = StatechartSemantics.Single
  280. # build Statechart structure
  281. self.build_statechart_structure()
  282. # user defined attributes
  283. self.tank = None
  284. self.range = 2000
  285. # call user defined constructor
  286. Radar.user_defined_constructor(self, tank)
  287. def user_defined_constructor(self, tank):
  288. self.tank = tank
  289. def user_defined_destructor(self):
  290. pass
  291. # user defined method
  292. def isEnemyVisible(self):
  293. sighted_list = self.tank.field.getSightedEnemies(self.tank, self.range)
  294. if len(sighted_list) > 0 :
  295. return True
  296. return False
  297. # user defined method
  298. def getEnemyPos(self):
  299. sighted_list = self.tank.field.getSightedEnemies(self.tank, self.range)
  300. if len(sighted_list) > 0 :
  301. sighted_list.sort(key=lambda x: x[1])
  302. return sighted_list[0][0]
  303. else :
  304. return (-1,-1)
  305. # builds Statechart structure
  306. def build_statechart_structure(self):
  307. # state <root>
  308. self.states[""] = State(0, self)
  309. # state /no_enemy
  310. self.states["/no_enemy"] = State(1, self)
  311. # state /enemy_in_sight
  312. self.states["/enemy_in_sight"] = State(2, self)
  313. # add children
  314. self.states[""].addChild(self.states["/no_enemy"])
  315. self.states[""].addChild(self.states["/enemy_in_sight"])
  316. self.states[""].fixTree()
  317. self.states[""].default_state = self.states["/no_enemy"]
  318. # transition /no_enemy
  319. _no_enemy_0 = Transition(self, self.states["/no_enemy"], [self.states["/enemy_in_sight"]])
  320. _no_enemy_0.setAction(self._no_enemy_0_exec)
  321. _no_enemy_0.setTrigger(None)
  322. _no_enemy_0.setGuard(self._no_enemy_0_guard)
  323. self.states["/no_enemy"].addTransition(_no_enemy_0)
  324. # transition /enemy_in_sight
  325. _enemy_in_sight_0 = Transition(self, self.states["/enemy_in_sight"], [self.states["/no_enemy"]])
  326. _enemy_in_sight_0.setAction(self._enemy_in_sight_0_exec)
  327. _enemy_in_sight_0.setTrigger(None)
  328. _enemy_in_sight_0.setGuard(self._enemy_in_sight_0_guard)
  329. self.states["/enemy_in_sight"].addTransition(_enemy_in_sight_0)
  330. _enemy_in_sight_1 = Transition(self, self.states["/enemy_in_sight"], [self.states["/enemy_in_sight"]])
  331. _enemy_in_sight_1.setAction(self._enemy_in_sight_1_exec)
  332. _enemy_in_sight_1.setTrigger(Event("update", "engine"))
  333. _enemy_in_sight_1.setGuard(self._enemy_in_sight_1_guard)
  334. self.states["/enemy_in_sight"].addTransition(_enemy_in_sight_1)
  335. def _no_enemy_0_exec(self, parameters):
  336. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'enemy_tracker', Event("enemy_sighted", None, [self.getEnemyPos()])]))
  337. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'pilot_strategy', Event("enemy_sighted", None, [self.getEnemyPos()])]))
  338. def _no_enemy_0_guard(self, parameters):
  339. return self.isEnemyVisible()
  340. def _enemy_in_sight_0_exec(self, parameters):
  341. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'enemy_tracker', Event("enemy_out_of_sight", None, [])]))
  342. def _enemy_in_sight_0_guard(self, parameters):
  343. return not self.isEnemyVisible()
  344. def _enemy_in_sight_1_exec(self, parameters):
  345. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'enemy_tracker', Event("enemy_pos", None, [self.getEnemyPos()])]))
  346. def _enemy_in_sight_1_guard(self, parameters):
  347. return self.isEnemyVisible()
  348. def initializeStatechart(self):
  349. # enter default state
  350. self.default_targets = self.states["/no_enemy"].getEffectiveTargetStates()
  351. RuntimeClassBase.initializeStatechart(self)
  352. class EnemyTracker(RuntimeClassBase):
  353. def __init__(self, controller):
  354. RuntimeClassBase.__init__(self, controller)
  355. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  356. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  357. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  358. self.semantics.priority = StatechartSemantics.SourceParent
  359. self.semantics.concurrency = StatechartSemantics.Single
  360. # build Statechart structure
  361. self.build_statechart_structure()
  362. # user defined attributes
  363. self.enemy_pos = None
  364. # call user defined constructor
  365. EnemyTracker.user_defined_constructor(self)
  366. def user_defined_constructor(self):
  367. pass
  368. def user_defined_destructor(self):
  369. pass
  370. # user defined method
  371. def hasEnemyMoved(self, new_position):
  372. return new_position != self.enemy_pos
  373. # builds Statechart structure
  374. def build_statechart_structure(self):
  375. # state <root>
  376. self.states[""] = State(0, self)
  377. # state /no_enemy
  378. self.states["/no_enemy"] = State(1, self)
  379. # state /enemy_pos_known
  380. self.states["/enemy_pos_known"] = State(2, self)
  381. # state /enemy_pos_unsure
  382. self.states["/enemy_pos_unsure"] = State(3, self)
  383. # add children
  384. self.states[""].addChild(self.states["/no_enemy"])
  385. self.states[""].addChild(self.states["/enemy_pos_known"])
  386. self.states[""].addChild(self.states["/enemy_pos_unsure"])
  387. self.states[""].fixTree()
  388. self.states[""].default_state = self.states["/no_enemy"]
  389. # transition /no_enemy
  390. _no_enemy_0 = Transition(self, self.states["/no_enemy"], [self.states["/enemy_pos_known"]])
  391. _no_enemy_0.setAction(self._no_enemy_0_exec)
  392. _no_enemy_0.setTrigger(Event("enemy_sighted", None))
  393. self.states["/no_enemy"].addTransition(_no_enemy_0)
  394. # transition /enemy_pos_known
  395. _enemy_pos_known_0 = Transition(self, self.states["/enemy_pos_known"], [self.states["/enemy_pos_known"]])
  396. _enemy_pos_known_0.setAction(self._enemy_pos_known_0_exec)
  397. _enemy_pos_known_0.setTrigger(Event("enemy_pos", None))
  398. _enemy_pos_known_0.setGuard(self._enemy_pos_known_0_guard)
  399. self.states["/enemy_pos_known"].addTransition(_enemy_pos_known_0)
  400. _enemy_pos_known_1 = Transition(self, self.states["/enemy_pos_known"], [self.states["/enemy_pos_unsure"]])
  401. _enemy_pos_known_1.setAction(self._enemy_pos_known_1_exec)
  402. _enemy_pos_known_1.setTrigger(Event("enemy_out_of_sight", None))
  403. self.states["/enemy_pos_known"].addTransition(_enemy_pos_known_1)
  404. # transition /enemy_pos_unsure
  405. _enemy_pos_unsure_0 = Transition(self, self.states["/enemy_pos_unsure"], [self.states["/no_enemy"]])
  406. _enemy_pos_unsure_0.setAction(self._enemy_pos_unsure_0_exec)
  407. _enemy_pos_unsure_0.setTrigger(Event("destination_reached", None))
  408. self.states["/enemy_pos_unsure"].addTransition(_enemy_pos_unsure_0)
  409. _enemy_pos_unsure_1 = Transition(self, self.states["/enemy_pos_unsure"], [self.states["/enemy_pos_known"]])
  410. _enemy_pos_unsure_1.setAction(self._enemy_pos_unsure_1_exec)
  411. _enemy_pos_unsure_1.setTrigger(Event("enemy_sighted", None))
  412. self.states["/enemy_pos_unsure"].addTransition(_enemy_pos_unsure_1)
  413. def _no_enemy_0_exec(self, parameters):
  414. enemy_position = parameters[0]
  415. self.enemy_pos = enemy_position
  416. def _enemy_pos_known_0_exec(self, parameters):
  417. position = parameters[0]
  418. self.enemy_pos = position
  419. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'attack_planner', Event("enemy_pos_changed", None, [self.enemy_pos])]))
  420. def _enemy_pos_known_0_guard(self, parameters):
  421. position = parameters[0]
  422. return self.hasEnemyMoved(position)
  423. def _enemy_pos_known_1_exec(self, parameters):
  424. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'attack_planner', Event("enemy_out_of_sight", None, [])]))
  425. def _enemy_pos_unsure_0_exec(self, parameters):
  426. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'pilot_strategy', Event("enemy_lost", None, [])]))
  427. print "raise enemy_lost"
  428. def _enemy_pos_unsure_1_exec(self, parameters):
  429. position = parameters[0]
  430. self.enemy_pos = position
  431. def initializeStatechart(self):
  432. # enter default state
  433. self.default_targets = self.states["/no_enemy"].getEffectiveTargetStates()
  434. RuntimeClassBase.initializeStatechart(self)
  435. class PilotStrategy(RuntimeClassBase):
  436. def __init__(self, controller):
  437. RuntimeClassBase.__init__(self, controller)
  438. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  439. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  440. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  441. self.semantics.priority = StatechartSemantics.SourceParent
  442. self.semantics.concurrency = StatechartSemantics.Single
  443. # build Statechart structure
  444. self.build_statechart_structure()
  445. # call user defined constructor
  446. PilotStrategy.user_defined_constructor(self)
  447. def user_defined_constructor(self):
  448. pass
  449. def user_defined_destructor(self):
  450. pass
  451. # builds Statechart structure
  452. def build_statechart_structure(self):
  453. # state <root>
  454. self.states[""] = State(0, self)
  455. # state /exploring
  456. self.states["/exploring"] = State(1, self)
  457. self.states["/exploring"].setEnter(self._exploring_enter)
  458. self.states["/exploring"].setExit(self._exploring_exit)
  459. # state /attacking
  460. self.states["/attacking"] = State(2, self)
  461. self.states["/attacking"].setExit(self._attacking_exit)
  462. # add children
  463. self.states[""].addChild(self.states["/exploring"])
  464. self.states[""].addChild(self.states["/attacking"])
  465. self.states[""].fixTree()
  466. self.states[""].default_state = self.states["/exploring"]
  467. # transition /exploring
  468. _exploring_0 = Transition(self, self.states["/exploring"], [self.states["/attacking"]])
  469. _exploring_0.setAction(self._exploring_0_exec)
  470. _exploring_0.setTrigger(Event("enemy_sighted", None))
  471. self.states["/exploring"].addTransition(_exploring_0)
  472. # transition /attacking
  473. _attacking_0 = Transition(self, self.states["/attacking"], [self.states["/exploring"]])
  474. _attacking_0.setAction(self._attacking_0_exec)
  475. _attacking_0.setTrigger(Event("enemy_lost", None))
  476. self.states["/attacking"].addTransition(_attacking_0)
  477. def _exploring_enter(self):
  478. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'explore_planner', Event("explore", None, [])]))
  479. def _exploring_exit(self):
  480. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'explore_planner', Event("stop_exploring", None, [])]))
  481. def _attacking_exit(self):
  482. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'attack_planner', Event("stop_attacking", None, [])]))
  483. print "raise stop_attacking"
  484. def _exploring_0_exec(self, parameters):
  485. position = parameters[0]
  486. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'attack_planner', Event("attack", None, [position])]))
  487. def _attacking_0_exec(self, parameters):
  488. print "received enemy lost"
  489. def initializeStatechart(self):
  490. # enter default state
  491. self.default_targets = self.states["/exploring"].getEffectiveTargetStates()
  492. RuntimeClassBase.initializeStatechart(self)
  493. class ExplorePlanner(RuntimeClassBase):
  494. def __init__(self, controller, tank, aimap):
  495. RuntimeClassBase.__init__(self, controller)
  496. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  497. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  498. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  499. self.semantics.priority = StatechartSemantics.SourceParent
  500. self.semantics.concurrency = StatechartSemantics.Single
  501. # build Statechart structure
  502. self.build_statechart_structure()
  503. # user defined attributes
  504. self.map = None
  505. self.tank = None
  506. # call user defined constructor
  507. ExplorePlanner.user_defined_constructor(self, tank, aimap)
  508. def user_defined_constructor(self, tank, aimap):
  509. self.tank = tank
  510. self.map = aimap
  511. def user_defined_destructor(self):
  512. pass
  513. # builds Statechart structure
  514. def build_statechart_structure(self):
  515. # state <root>
  516. self.states[""] = State(0, self)
  517. # state /idle
  518. self.states["/idle"] = State(1, self)
  519. # state /exploring
  520. self.states["/exploring"] = State(2, self)
  521. # state /exploring/no_destination
  522. self.states["/exploring/no_destination"] = State(3, self)
  523. # state /exploring/destination_set
  524. self.states["/exploring/destination_set"] = State(4, self)
  525. # add children
  526. self.states[""].addChild(self.states["/idle"])
  527. self.states[""].addChild(self.states["/exploring"])
  528. self.states["/exploring"].addChild(self.states["/exploring/no_destination"])
  529. self.states["/exploring"].addChild(self.states["/exploring/destination_set"])
  530. self.states[""].fixTree()
  531. self.states[""].default_state = self.states["/idle"]
  532. self.states["/exploring"].default_state = self.states["/exploring/no_destination"]
  533. # transition /idle
  534. _idle_0 = Transition(self, self.states["/idle"], [self.states["/exploring"]])
  535. _idle_0.setTrigger(Event("explore", None))
  536. self.states["/idle"].addTransition(_idle_0)
  537. # transition /exploring/no_destination
  538. _exploring_no_destination_0 = Transition(self, self.states["/exploring/no_destination"], [self.states["/exploring/destination_set"]])
  539. _exploring_no_destination_0.setAction(self._exploring_no_destination_0_exec)
  540. _exploring_no_destination_0.setTrigger(None)
  541. self.states["/exploring/no_destination"].addTransition(_exploring_no_destination_0)
  542. # transition /exploring/destination_set
  543. _exploring_destination_set_0 = Transition(self, self.states["/exploring/destination_set"], [self.states["/exploring/no_destination"]])
  544. _exploring_destination_set_0.setTrigger(Event("destination_reached", None))
  545. self.states["/exploring/destination_set"].addTransition(_exploring_destination_set_0)
  546. # transition /exploring
  547. _exploring_0 = Transition(self, self.states["/exploring"], [self.states["/idle"]])
  548. _exploring_0.setAction(self._exploring_0_exec)
  549. _exploring_0.setTrigger(Event("stop_exploring", None))
  550. self.states["/exploring"].addTransition(_exploring_0)
  551. def _exploring_0_exec(self, parameters):
  552. print "stop_exploring received"
  553. def _exploring_no_destination_0_exec(self, parameters):
  554. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'path_finder', Event("new_destination", None, [self.map.getNewExplore(self.tank.getPosition(),self.tank.getAngle())])]))
  555. def initializeStatechart(self):
  556. # enter default state
  557. self.default_targets = self.states["/idle"].getEffectiveTargetStates()
  558. RuntimeClassBase.initializeStatechart(self)
  559. class AttackPlanner(RuntimeClassBase):
  560. def __init__(self, controller):
  561. RuntimeClassBase.__init__(self, controller)
  562. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  563. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  564. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  565. self.semantics.priority = StatechartSemantics.SourceParent
  566. self.semantics.concurrency = StatechartSemantics.Single
  567. # build Statechart structure
  568. self.build_statechart_structure()
  569. # call user defined constructor
  570. AttackPlanner.user_defined_constructor(self)
  571. def user_defined_constructor(self):
  572. pass
  573. def user_defined_destructor(self):
  574. pass
  575. # builds Statechart structure
  576. def build_statechart_structure(self):
  577. # state <root>
  578. self.states[""] = State(0, self)
  579. # state /idle
  580. self.states["/idle"] = State(1, self)
  581. # state /action
  582. self.states["/action"] = ParallelState(2, self)
  583. # state /action/movement
  584. self.states["/action/movement"] = State(3, self)
  585. # state /action/movement/following
  586. self.states["/action/movement/following"] = State(4, self)
  587. # state /action/shooting
  588. self.states["/action/shooting"] = State(5, self)
  589. # state /action/shooting/loaded
  590. self.states["/action/shooting/loaded"] = State(6, self)
  591. # state /action/shooting/reloading
  592. self.states["/action/shooting/reloading"] = State(7, self)
  593. self.states["/action/shooting/reloading"].setEnter(self._action_shooting_reloading_enter)
  594. self.states["/action/shooting/reloading"].setExit(self._action_shooting_reloading_exit)
  595. # add children
  596. self.states[""].addChild(self.states["/idle"])
  597. self.states[""].addChild(self.states["/action"])
  598. self.states["/action"].addChild(self.states["/action/movement"])
  599. self.states["/action"].addChild(self.states["/action/shooting"])
  600. self.states["/action/movement"].addChild(self.states["/action/movement/following"])
  601. self.states["/action/shooting"].addChild(self.states["/action/shooting/loaded"])
  602. self.states["/action/shooting"].addChild(self.states["/action/shooting/reloading"])
  603. self.states[""].fixTree()
  604. self.states[""].default_state = self.states["/idle"]
  605. self.states["/action/movement"].default_state = self.states["/action/movement/following"]
  606. self.states["/action/shooting"].default_state = self.states["/action/shooting/loaded"]
  607. # transition /idle
  608. _idle_0 = Transition(self, self.states["/idle"], [self.states["/action"]])
  609. _idle_0.setAction(self._idle_0_exec)
  610. _idle_0.setTrigger(Event("attack", None))
  611. self.states["/idle"].addTransition(_idle_0)
  612. # transition /action/movement/following
  613. _action_movement_following_0 = Transition(self, self.states["/action/movement/following"], [self.states["/action/movement/following"]])
  614. _action_movement_following_0.setAction(self._action_movement_following_0_exec)
  615. _action_movement_following_0.setTrigger(Event("enemy_pos_changed", None))
  616. self.states["/action/movement/following"].addTransition(_action_movement_following_0)
  617. _action_movement_following_1 = Transition(self, self.states["/action/movement/following"], [self.states["/action/movement/following"]])
  618. _action_movement_following_1.setAction(self._action_movement_following_1_exec)
  619. _action_movement_following_1.setTrigger(Event("enemy_out_of_sight", None))
  620. self.states["/action/movement/following"].addTransition(_action_movement_following_1)
  621. # transition /action/shooting/loaded
  622. _action_shooting_loaded_0 = Transition(self, self.states["/action/shooting/loaded"], [self.states["/action/shooting/reloading"]])
  623. _action_shooting_loaded_0.setAction(self._action_shooting_loaded_0_exec)
  624. _action_shooting_loaded_0.setTrigger(Event("ready_to_shoot", None))
  625. self.states["/action/shooting/loaded"].addTransition(_action_shooting_loaded_0)
  626. # transition /action/shooting/reloading
  627. _action_shooting_reloading_0 = Transition(self, self.states["/action/shooting/reloading"], [self.states["/action/shooting/loaded"]])
  628. _action_shooting_reloading_0.setTrigger(Event("_0after"))
  629. self.states["/action/shooting/reloading"].addTransition(_action_shooting_reloading_0)
  630. # transition /action
  631. _action_0 = Transition(self, self.states["/action"], [self.states["/idle"]])
  632. _action_0.setAction(self._action_0_exec)
  633. _action_0.setTrigger(Event("stop_attacking", None))
  634. self.states["/action"].addTransition(_action_0)
  635. def _action_shooting_reloading_enter(self):
  636. self.addTimer(0, 0.5)
  637. def _action_shooting_reloading_exit(self):
  638. self.removeTimer(0)
  639. def _action_0_exec(self, parameters):
  640. print "received stop attack"
  641. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_steering', Event("stop_aiming", None, [])]))
  642. def _idle_0_exec(self, parameters):
  643. enemy_pos = parameters[0]
  644. print "received attack"
  645. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'path_finder', Event("new_destination", None, [enemy_pos])]))
  646. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_steering', Event("aim_at", None, [enemy_pos])]))
  647. def _action_movement_following_0_exec(self, parameters):
  648. enemy_pos = parameters[0]
  649. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'path_finder', Event("new_destination", None, [enemy_pos])]))
  650. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_steering', Event("aim_at", None, [enemy_pos])]))
  651. def _action_movement_following_1_exec(self, parameters):
  652. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_steering', Event("stop_aiming", None, [])]))
  653. def _action_shooting_loaded_0_exec(self, parameters):
  654. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_control', Event("shoot", None, [])]))
  655. print "raise shoot"
  656. def initializeStatechart(self):
  657. # enter default state
  658. self.default_targets = self.states["/idle"].getEffectiveTargetStates()
  659. RuntimeClassBase.initializeStatechart(self)
  660. class PathFinder(RuntimeClassBase):
  661. def __init__(self, controller, tank, aimap):
  662. RuntimeClassBase.__init__(self, controller)
  663. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  664. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  665. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  666. self.semantics.priority = StatechartSemantics.SourceParent
  667. self.semantics.concurrency = StatechartSemantics.Single
  668. # build Statechart structure
  669. self.build_statechart_structure()
  670. # user defined attributes
  671. self.waypoints = []
  672. self.destination = (-1,-1)
  673. self.map = None
  674. self.tank = None
  675. # call user defined constructor
  676. PathFinder.user_defined_constructor(self, tank, aimap)
  677. def user_defined_constructor(self, tank, aimap):
  678. self.tank = tank
  679. self.map = aimap
  680. def user_defined_destructor(self):
  681. pass
  682. # user defined method
  683. def calculatePath(self):
  684. return self.map.calculatePath(self.tank.getPosition(), self.destination)
  685. # user defined method
  686. def requiresNewPath(self, new_destination):
  687. return self.map.calculateCell(self.destination) != self.map.calculateCell(new_destination)
  688. # user defined method
  689. def morePoints(self):
  690. return len(self.waypoints) > 0
  691. # builds Statechart structure
  692. def build_statechart_structure(self):
  693. # state <root>
  694. self.states[""] = State(0, self)
  695. # state /idle
  696. self.states["/idle"] = State(1, self)
  697. # state /check_points
  698. self.states["/check_points"] = State(2, self)
  699. # add children
  700. self.states[""].addChild(self.states["/idle"])
  701. self.states[""].addChild(self.states["/check_points"])
  702. self.states[""].fixTree()
  703. self.states[""].default_state = self.states["/idle"]
  704. # transition /idle
  705. _idle_0 = Transition(self, self.states["/idle"], [self.states["/check_points"]])
  706. _idle_0.setTrigger(Event("waypoint_reached", None))
  707. self.states["/idle"].addTransition(_idle_0)
  708. _idle_1 = Transition(self, self.states["/idle"], [self.states["/check_points"]])
  709. _idle_1.setAction(self._idle_1_exec)
  710. _idle_1.setTrigger(Event("new_destination", None))
  711. _idle_1.setGuard(self._idle_1_guard)
  712. self.states["/idle"].addTransition(_idle_1)
  713. # transition /check_points
  714. _check_points_0 = Transition(self, self.states["/check_points"], [self.states["/idle"]])
  715. _check_points_0.setAction(self._check_points_0_exec)
  716. _check_points_0.setTrigger(None)
  717. _check_points_0.setGuard(self._check_points_0_guard)
  718. self.states["/check_points"].addTransition(_check_points_0)
  719. _check_points_1 = Transition(self, self.states["/check_points"], [self.states["/idle"]])
  720. _check_points_1.setAction(self._check_points_1_exec)
  721. _check_points_1.setTrigger(None)
  722. _check_points_1.setGuard(self._check_points_1_guard)
  723. self.states["/check_points"].addTransition(_check_points_1)
  724. def _idle_1_exec(self, parameters):
  725. destination = parameters[0]
  726. self.destination = destination
  727. self.waypoints = self.calculatePath()
  728. def _idle_1_guard(self, parameters):
  729. destination = parameters[0]
  730. return self.requiresNewPath(destination)
  731. def _check_points_0_exec(self, parameters):
  732. next_waypoint = self.waypoints.pop(0)
  733. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'steering', Event("new_waypoint", None, [next_waypoint])]))
  734. def _check_points_0_guard(self, parameters):
  735. return self.morePoints()
  736. def _check_points_1_exec(self, parameters):
  737. self.big_step.outputEventOM(Event("broad_cast", None, [Event("destination_reached", None, [])]))
  738. def _check_points_1_guard(self, parameters):
  739. return not self.morePoints()
  740. def initializeStatechart(self):
  741. # enter default state
  742. self.default_targets = self.states["/idle"].getEffectiveTargetStates()
  743. RuntimeClassBase.initializeStatechart(self)
  744. class Steering(RuntimeClassBase):
  745. def __init__(self, controller, tank, aimap):
  746. RuntimeClassBase.__init__(self, controller)
  747. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  748. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  749. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  750. self.semantics.priority = StatechartSemantics.SourceParent
  751. self.semantics.concurrency = StatechartSemantics.Single
  752. # build Statechart structure
  753. self.build_statechart_structure()
  754. # user defined attributes
  755. self.dest_waypoint = (-1,-1)
  756. self.dest_cell = (-1,-1)
  757. self.reaction_time = 0.05
  758. self.tank = None
  759. self.map = None
  760. self.margin = 0.2
  761. # call user defined constructor
  762. Steering.user_defined_constructor(self, tank, aimap)
  763. def user_defined_constructor(self, tank, aimap):
  764. self.tank = tank
  765. self.map = aimap
  766. def user_defined_destructor(self):
  767. pass
  768. # user defined method
  769. def pointReached(self):
  770. cell = self.map.calculateCell((self.tank.x, self.tank.y))
  771. return cell == self.dest_cell
  772. # user defined method
  773. def pointAhead(self):
  774. goal_angle = self.tank.angleToDest(self.dest_waypoint)
  775. diff = math.fabs(self.tank.angle - goal_angle)
  776. if diff <= (D45) :
  777. #self.tank.moveSpeed = int(math.ceil(((D45 - diff) / D45) * self.maxMoveSpeed))
  778. return True
  779. elif diff >= (D360 - D45) :
  780. #self.tank.moveSpeed = int(math.ceil(((diff - (D360 - D45)) / D45) * self.maxMoveSpeed))
  781. return True
  782. return False
  783. # user defined method
  784. def pointBehind(self):
  785. goal_angle = self.tank.angleToDest(self.dest_waypoint)
  786. diff = math.fabs(self.tank.angle - goal_angle)
  787. if diff <= (D45) or diff >= (D360 - D45) :
  788. return False
  789. return True
  790. # user defined method
  791. def pointRight(self):
  792. goal_angle = self.tank.angleToDest(self.dest_waypoint)
  793. diff = (self.tank.angle - goal_angle) % D360
  794. if diff >= self.margin and diff <= math.pi:
  795. return True
  796. return False
  797. # user defined method
  798. def pointLeft(self):
  799. goal_angle = self.tank.angleToDest(self.dest_waypoint)
  800. diff = (goal_angle - self.tank.angle) % D360
  801. if diff >= self.margin and diff <= math.pi:
  802. return True
  803. return False
  804. # user defined method
  805. def pointStraight(self):
  806. goal_angle = self.tank.angleToDest(self.dest_waypoint)
  807. diff = math.fabs(self.tank.angle - goal_angle)
  808. if diff < self.margin or diff > (D360- self.margin) :
  809. return True
  810. return False
  811. # builds Statechart structure
  812. def build_statechart_structure(self):
  813. # state <root>
  814. self.states[""] = State(0, self)
  815. # state /idle
  816. self.states["/idle"] = State(1, self)
  817. # state /steering
  818. self.states["/steering"] = State(2, self)
  819. # state /steering/forward_backward
  820. self.states["/steering/forward_backward"] = State(3, self)
  821. # state /steering/left_right
  822. self.states["/steering/left_right"] = State(4, self)
  823. # state /steering/wait
  824. self.states["/steering/wait"] = State(5, self)
  825. self.states["/steering/wait"].setEnter(self._steering_wait_enter)
  826. self.states["/steering/wait"].setExit(self._steering_wait_exit)
  827. # add children
  828. self.states[""].addChild(self.states["/idle"])
  829. self.states[""].addChild(self.states["/steering"])
  830. self.states["/steering"].addChild(self.states["/steering/forward_backward"])
  831. self.states["/steering"].addChild(self.states["/steering/left_right"])
  832. self.states["/steering"].addChild(self.states["/steering/wait"])
  833. self.states[""].fixTree()
  834. self.states[""].default_state = self.states["/idle"]
  835. self.states["/steering"].default_state = self.states["/steering/forward_backward"]
  836. # transition /idle
  837. _idle_0 = Transition(self, self.states["/idle"], [self.states["/steering"]])
  838. _idle_0.setAction(self._idle_0_exec)
  839. _idle_0.setTrigger(Event("new_waypoint", None))
  840. self.states["/idle"].addTransition(_idle_0)
  841. # transition /steering/forward_backward
  842. _steering_forward_backward_0 = Transition(self, self.states["/steering/forward_backward"], [self.states["/steering/left_right"]])
  843. _steering_forward_backward_0.setAction(self._steering_forward_backward_0_exec)
  844. _steering_forward_backward_0.setTrigger(None)
  845. _steering_forward_backward_0.setGuard(self._steering_forward_backward_0_guard)
  846. self.states["/steering/forward_backward"].addTransition(_steering_forward_backward_0)
  847. _steering_forward_backward_1 = Transition(self, self.states["/steering/forward_backward"], [self.states["/steering/left_right"]])
  848. _steering_forward_backward_1.setAction(self._steering_forward_backward_1_exec)
  849. _steering_forward_backward_1.setTrigger(None)
  850. _steering_forward_backward_1.setGuard(self._steering_forward_backward_1_guard)
  851. self.states["/steering/forward_backward"].addTransition(_steering_forward_backward_1)
  852. # transition /steering/left_right
  853. _steering_left_right_0 = Transition(self, self.states["/steering/left_right"], [self.states["/steering/wait"]])
  854. _steering_left_right_0.setAction(self._steering_left_right_0_exec)
  855. _steering_left_right_0.setTrigger(None)
  856. _steering_left_right_0.setGuard(self._steering_left_right_0_guard)
  857. self.states["/steering/left_right"].addTransition(_steering_left_right_0)
  858. _steering_left_right_1 = Transition(self, self.states["/steering/left_right"], [self.states["/steering/wait"]])
  859. _steering_left_right_1.setAction(self._steering_left_right_1_exec)
  860. _steering_left_right_1.setTrigger(None)
  861. _steering_left_right_1.setGuard(self._steering_left_right_1_guard)
  862. self.states["/steering/left_right"].addTransition(_steering_left_right_1)
  863. _steering_left_right_2 = Transition(self, self.states["/steering/left_right"], [self.states["/steering/wait"]])
  864. _steering_left_right_2.setAction(self._steering_left_right_2_exec)
  865. _steering_left_right_2.setTrigger(None)
  866. _steering_left_right_2.setGuard(self._steering_left_right_2_guard)
  867. self.states["/steering/left_right"].addTransition(_steering_left_right_2)
  868. # transition /steering/wait
  869. _steering_wait_0 = Transition(self, self.states["/steering/wait"], [self.states["/steering/forward_backward"]])
  870. _steering_wait_0.setTrigger(Event("_0after"))
  871. self.states["/steering/wait"].addTransition(_steering_wait_0)
  872. # transition /steering
  873. _steering_0 = Transition(self, self.states["/steering"], [self.states["/idle"]])
  874. _steering_0.setAction(self._steering_0_exec)
  875. _steering_0.setTrigger(None)
  876. _steering_0.setGuard(self._steering_0_guard)
  877. self.states["/steering"].addTransition(_steering_0)
  878. def _steering_wait_enter(self):
  879. self.addTimer(0, self.reaction_time)
  880. def _steering_wait_exit(self):
  881. self.removeTimer(0)
  882. def _steering_0_exec(self, parameters):
  883. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'path_finder', Event("waypoint_reached", None, [])]))
  884. def _steering_0_guard(self, parameters):
  885. return self.pointReached()
  886. def _idle_0_exec(self, parameters):
  887. dest_waypoint = parameters[0]
  888. self.dest_waypoint = dest_waypoint
  889. self.dest_cell = self.map.calculateCell(dest_waypoint)
  890. def _steering_forward_backward_0_exec(self, parameters):
  891. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'motor_control', Event("forward", None, [])]))
  892. def _steering_forward_backward_0_guard(self, parameters):
  893. return self.pointAhead()
  894. def _steering_forward_backward_1_exec(self, parameters):
  895. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'motor_control', Event("stop", None, [])]))
  896. def _steering_forward_backward_1_guard(self, parameters):
  897. return self.pointBehind()
  898. def _steering_left_right_0_exec(self, parameters):
  899. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'motor_control', Event("stop_turning", None, [])]))
  900. def _steering_left_right_0_guard(self, parameters):
  901. return self.pointStraight()
  902. def _steering_left_right_1_exec(self, parameters):
  903. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'motor_control', Event("turn_left", None, [])]))
  904. def _steering_left_right_1_guard(self, parameters):
  905. return self.pointLeft()
  906. def _steering_left_right_2_exec(self, parameters):
  907. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'motor_control', Event("turn_right", None, [])]))
  908. def _steering_left_right_2_guard(self, parameters):
  909. return self.pointRight()
  910. def initializeStatechart(self):
  911. # enter default state
  912. self.default_targets = self.states["/idle"].getEffectiveTargetStates()
  913. RuntimeClassBase.initializeStatechart(self)
  914. class TurretSteering(RuntimeClassBase):
  915. def __init__(self, controller, tank):
  916. RuntimeClassBase.__init__(self, controller)
  917. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  918. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  919. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  920. self.semantics.priority = StatechartSemantics.SourceParent
  921. self.semantics.concurrency = StatechartSemantics.Single
  922. # build Statechart structure
  923. self.build_statechart_structure()
  924. # user defined attributes
  925. self.reaction_time = 0.05
  926. self.tank = None
  927. self.margin = None
  928. # call user defined constructor
  929. TurretSteering.user_defined_constructor(self, tank)
  930. def user_defined_constructor(self, tank):
  931. self.tank = tank
  932. self.margin = tank.cannonSpeed * D1
  933. def user_defined_destructor(self):
  934. pass
  935. # user defined method
  936. def pointRight(self):
  937. goal_angle = self.tank.angleToDest(self.target)
  938. diff = (self.tank.cannonAngle - goal_angle) % D360
  939. if diff >= self.margin and diff <= math.pi:
  940. return True
  941. return False
  942. # user defined method
  943. def pointLeft(self):
  944. goal_angle = self.tank.angleToDest(self.target)
  945. diff = (goal_angle - self.tank.cannonAngle) % D360
  946. if diff >= self.margin and diff <= math.pi:
  947. return True
  948. return False
  949. # user defined method
  950. def pointCorrect(self):
  951. goal_angle = self.tank.angleToDest(self.target)
  952. diff = math.fabs(goal_angle - self.tank.cannonAngle)
  953. if diff < self.margin or diff > (D360- self.margin):
  954. return True
  955. return False
  956. # builds Statechart structure
  957. def build_statechart_structure(self):
  958. # state <root>
  959. self.states[""] = State(0, self)
  960. # state /idle
  961. self.states["/idle"] = State(1, self)
  962. # state /aiming
  963. self.states["/aiming"] = State(2, self)
  964. # state /aiming/adjust
  965. self.states["/aiming/adjust"] = State(3, self)
  966. # state /aiming/wait
  967. self.states["/aiming/wait"] = State(4, self)
  968. self.states["/aiming/wait"].setEnter(self._aiming_wait_enter)
  969. self.states["/aiming/wait"].setExit(self._aiming_wait_exit)
  970. # add children
  971. self.states[""].addChild(self.states["/idle"])
  972. self.states[""].addChild(self.states["/aiming"])
  973. self.states["/aiming"].addChild(self.states["/aiming/adjust"])
  974. self.states["/aiming"].addChild(self.states["/aiming/wait"])
  975. self.states[""].fixTree()
  976. self.states[""].default_state = self.states["/idle"]
  977. self.states["/aiming"].default_state = self.states["/aiming/adjust"]
  978. # transition /idle
  979. _idle_0 = Transition(self, self.states["/idle"], [self.states["/aiming"]])
  980. _idle_0.setAction(self._idle_0_exec)
  981. _idle_0.setTrigger(Event("aim_at", None))
  982. self.states["/idle"].addTransition(_idle_0)
  983. # transition /aiming/adjust
  984. _aiming_adjust_0 = Transition(self, self.states["/aiming/adjust"], [self.states["/aiming/wait"]])
  985. _aiming_adjust_0.setAction(self._aiming_adjust_0_exec)
  986. _aiming_adjust_0.setTrigger(None)
  987. _aiming_adjust_0.setGuard(self._aiming_adjust_0_guard)
  988. self.states["/aiming/adjust"].addTransition(_aiming_adjust_0)
  989. _aiming_adjust_1 = Transition(self, self.states["/aiming/adjust"], [self.states["/aiming/wait"]])
  990. _aiming_adjust_1.setAction(self._aiming_adjust_1_exec)
  991. _aiming_adjust_1.setTrigger(None)
  992. _aiming_adjust_1.setGuard(self._aiming_adjust_1_guard)
  993. self.states["/aiming/adjust"].addTransition(_aiming_adjust_1)
  994. _aiming_adjust_2 = Transition(self, self.states["/aiming/adjust"], [self.states["/aiming/wait"]])
  995. _aiming_adjust_2.setAction(self._aiming_adjust_2_exec)
  996. _aiming_adjust_2.setTrigger(None)
  997. _aiming_adjust_2.setGuard(self._aiming_adjust_2_guard)
  998. self.states["/aiming/adjust"].addTransition(_aiming_adjust_2)
  999. # transition /aiming/wait
  1000. _aiming_wait_0 = Transition(self, self.states["/aiming/wait"], [self.states["/aiming/adjust"]])
  1001. _aiming_wait_0.setTrigger(Event("_0after"))
  1002. self.states["/aiming/wait"].addTransition(_aiming_wait_0)
  1003. # transition /aiming
  1004. _aiming_0 = Transition(self, self.states["/aiming"], [self.states["/idle"]])
  1005. _aiming_0.setAction(self._aiming_0_exec)
  1006. _aiming_0.setTrigger(Event("stop_aiming", None))
  1007. self.states["/aiming"].addTransition(_aiming_0)
  1008. _aiming_1 = Transition(self, self.states["/aiming"], [self.states["/aiming"]])
  1009. _aiming_1.setAction(self._aiming_1_exec)
  1010. _aiming_1.setTrigger(Event("aim_at", None))
  1011. self.states["/aiming"].addTransition(_aiming_1)
  1012. def _aiming_wait_enter(self):
  1013. self.addTimer(0, self.reaction_time)
  1014. def _aiming_wait_exit(self):
  1015. self.removeTimer(0)
  1016. def _aiming_0_exec(self, parameters):
  1017. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_control', Event("stop_turning", None, [])]))
  1018. def _aiming_1_exec(self, parameters):
  1019. target = parameters[0]
  1020. self.target = target
  1021. def _idle_0_exec(self, parameters):
  1022. target = parameters[0]
  1023. self.target = target
  1024. def _aiming_adjust_0_exec(self, parameters):
  1025. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_control', Event("turn_right", None, [])]))
  1026. def _aiming_adjust_0_guard(self, parameters):
  1027. return self.pointRight()
  1028. def _aiming_adjust_1_exec(self, parameters):
  1029. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_control', Event("turn_left", None, [])]))
  1030. def _aiming_adjust_1_guard(self, parameters):
  1031. return self.pointLeft()
  1032. def _aiming_adjust_2_exec(self, parameters):
  1033. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'turret_control', Event("stop_turning", None, [])]))
  1034. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'attack_planner', Event("ready_to_shoot", None, [])]))
  1035. def _aiming_adjust_2_guard(self, parameters):
  1036. return self.pointCorrect()
  1037. def initializeStatechart(self):
  1038. # enter default state
  1039. self.default_targets = self.states["/idle"].getEffectiveTargetStates()
  1040. RuntimeClassBase.initializeStatechart(self)
  1041. class MotorControl(RuntimeClassBase):
  1042. def __init__(self, controller, tank):
  1043. RuntimeClassBase.__init__(self, controller)
  1044. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  1045. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  1046. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  1047. self.semantics.priority = StatechartSemantics.SourceParent
  1048. self.semantics.concurrency = StatechartSemantics.Single
  1049. # build Statechart structure
  1050. self.build_statechart_structure()
  1051. # user defined attributes
  1052. self.tank = None
  1053. # call user defined constructor
  1054. MotorControl.user_defined_constructor(self, tank)
  1055. def user_defined_constructor(self, tank):
  1056. self.tank = tank
  1057. def user_defined_destructor(self):
  1058. pass
  1059. # builds Statechart structure
  1060. def build_statechart_structure(self):
  1061. # state <root>
  1062. self.states[""] = State(0, self)
  1063. # state /control
  1064. self.states["/control"] = ParallelState(1, self)
  1065. # state /control/left_right
  1066. self.states["/control/left_right"] = State(2, self)
  1067. # state /control/left_right/stop
  1068. self.states["/control/left_right/stop"] = State(3, self)
  1069. # state /control/left_right/going_forward
  1070. self.states["/control/left_right/going_forward"] = State(4, self)
  1071. # state /control/left_right/going_backward
  1072. self.states["/control/left_right/going_backward"] = State(5, self)
  1073. # state /control/forward_backward
  1074. self.states["/control/forward_backward"] = State(6, self)
  1075. # state /control/forward_backward/straight
  1076. self.states["/control/forward_backward/straight"] = State(7, self)
  1077. # state /control/forward_backward/turning_left
  1078. self.states["/control/forward_backward/turning_left"] = State(8, self)
  1079. # state /control/forward_backward/turning_right
  1080. self.states["/control/forward_backward/turning_right"] = State(9, self)
  1081. # add children
  1082. self.states[""].addChild(self.states["/control"])
  1083. self.states["/control"].addChild(self.states["/control/left_right"])
  1084. self.states["/control"].addChild(self.states["/control/forward_backward"])
  1085. self.states["/control/left_right"].addChild(self.states["/control/left_right/stop"])
  1086. self.states["/control/left_right"].addChild(self.states["/control/left_right/going_forward"])
  1087. self.states["/control/left_right"].addChild(self.states["/control/left_right/going_backward"])
  1088. self.states["/control/forward_backward"].addChild(self.states["/control/forward_backward/straight"])
  1089. self.states["/control/forward_backward"].addChild(self.states["/control/forward_backward/turning_left"])
  1090. self.states["/control/forward_backward"].addChild(self.states["/control/forward_backward/turning_right"])
  1091. self.states[""].fixTree()
  1092. self.states[""].default_state = self.states["/control"]
  1093. self.states["/control/left_right"].default_state = self.states["/control/left_right/stop"]
  1094. self.states["/control/forward_backward"].default_state = self.states["/control/forward_backward/straight"]
  1095. # transition /control/left_right/stop
  1096. _control_left_right_stop_0 = Transition(self, self.states["/control/left_right/stop"], [self.states["/control/left_right/going_forward"]])
  1097. _control_left_right_stop_0.setTrigger(Event("forward", None))
  1098. self.states["/control/left_right/stop"].addTransition(_control_left_right_stop_0)
  1099. _control_left_right_stop_1 = Transition(self, self.states["/control/left_right/stop"], [self.states["/control/left_right/going_backward"]])
  1100. _control_left_right_stop_1.setTrigger(Event("backward", None))
  1101. self.states["/control/left_right/stop"].addTransition(_control_left_right_stop_1)
  1102. # transition /control/left_right/going_forward
  1103. _control_left_right_going_forward_0 = Transition(self, self.states["/control/left_right/going_forward"], [self.states["/control/left_right/stop"]])
  1104. _control_left_right_going_forward_0.setTrigger(Event("stop", None))
  1105. self.states["/control/left_right/going_forward"].addTransition(_control_left_right_going_forward_0)
  1106. _control_left_right_going_forward_1 = Transition(self, self.states["/control/left_right/going_forward"], [self.states["/control/left_right/going_backward"]])
  1107. _control_left_right_going_forward_1.setTrigger(Event("backward", None))
  1108. self.states["/control/left_right/going_forward"].addTransition(_control_left_right_going_forward_1)
  1109. _control_left_right_going_forward_2 = Transition(self, self.states["/control/left_right/going_forward"], [self.states["/control/left_right/going_forward"]])
  1110. _control_left_right_going_forward_2.setAction(self._control_left_right_going_forward_2_exec)
  1111. _control_left_right_going_forward_2.setTrigger(Event("update", "engine"))
  1112. self.states["/control/left_right/going_forward"].addTransition(_control_left_right_going_forward_2)
  1113. # transition /control/left_right/going_backward
  1114. _control_left_right_going_backward_0 = Transition(self, self.states["/control/left_right/going_backward"], [self.states["/control/left_right/stop"]])
  1115. _control_left_right_going_backward_0.setTrigger(Event("stop", None))
  1116. self.states["/control/left_right/going_backward"].addTransition(_control_left_right_going_backward_0)
  1117. _control_left_right_going_backward_1 = Transition(self, self.states["/control/left_right/going_backward"], [self.states["/control/left_right/going_forward"]])
  1118. _control_left_right_going_backward_1.setTrigger(Event("forward", None))
  1119. self.states["/control/left_right/going_backward"].addTransition(_control_left_right_going_backward_1)
  1120. _control_left_right_going_backward_2 = Transition(self, self.states["/control/left_right/going_backward"], [self.states["/control/left_right/going_backward"]])
  1121. _control_left_right_going_backward_2.setAction(self._control_left_right_going_backward_2_exec)
  1122. _control_left_right_going_backward_2.setTrigger(Event("update", "engine"))
  1123. self.states["/control/left_right/going_backward"].addTransition(_control_left_right_going_backward_2)
  1124. # transition /control/forward_backward/straight
  1125. _control_forward_backward_straight_0 = Transition(self, self.states["/control/forward_backward/straight"], [self.states["/control/forward_backward/turning_right"]])
  1126. _control_forward_backward_straight_0.setTrigger(Event("turn_right", None))
  1127. self.states["/control/forward_backward/straight"].addTransition(_control_forward_backward_straight_0)
  1128. _control_forward_backward_straight_1 = Transition(self, self.states["/control/forward_backward/straight"], [self.states["/control/forward_backward/turning_left"]])
  1129. _control_forward_backward_straight_1.setTrigger(Event("turn_left", None))
  1130. self.states["/control/forward_backward/straight"].addTransition(_control_forward_backward_straight_1)
  1131. # transition /control/forward_backward/turning_left
  1132. _control_forward_backward_turning_left_0 = Transition(self, self.states["/control/forward_backward/turning_left"], [self.states["/control/forward_backward/straight"]])
  1133. _control_forward_backward_turning_left_0.setTrigger(Event("stop_turning", None))
  1134. self.states["/control/forward_backward/turning_left"].addTransition(_control_forward_backward_turning_left_0)
  1135. _control_forward_backward_turning_left_1 = Transition(self, self.states["/control/forward_backward/turning_left"], [self.states["/control/forward_backward/turning_right"]])
  1136. _control_forward_backward_turning_left_1.setTrigger(Event("turn_right", None))
  1137. self.states["/control/forward_backward/turning_left"].addTransition(_control_forward_backward_turning_left_1)
  1138. _control_forward_backward_turning_left_2 = Transition(self, self.states["/control/forward_backward/turning_left"], [self.states["/control/forward_backward/turning_left"]])
  1139. _control_forward_backward_turning_left_2.setAction(self._control_forward_backward_turning_left_2_exec)
  1140. _control_forward_backward_turning_left_2.setTrigger(Event("update", "engine"))
  1141. self.states["/control/forward_backward/turning_left"].addTransition(_control_forward_backward_turning_left_2)
  1142. # transition /control/forward_backward/turning_right
  1143. _control_forward_backward_turning_right_0 = Transition(self, self.states["/control/forward_backward/turning_right"], [self.states["/control/forward_backward/straight"]])
  1144. _control_forward_backward_turning_right_0.setTrigger(Event("stop_turning", None))
  1145. self.states["/control/forward_backward/turning_right"].addTransition(_control_forward_backward_turning_right_0)
  1146. _control_forward_backward_turning_right_1 = Transition(self, self.states["/control/forward_backward/turning_right"], [self.states["/control/forward_backward/turning_left"]])
  1147. _control_forward_backward_turning_right_1.setTrigger(Event("turn_left", None))
  1148. self.states["/control/forward_backward/turning_right"].addTransition(_control_forward_backward_turning_right_1)
  1149. _control_forward_backward_turning_right_2 = Transition(self, self.states["/control/forward_backward/turning_right"], [self.states["/control/forward_backward/turning_right"]])
  1150. _control_forward_backward_turning_right_2.setAction(self._control_forward_backward_turning_right_2_exec)
  1151. _control_forward_backward_turning_right_2.setTrigger(Event("update", "engine"))
  1152. self.states["/control/forward_backward/turning_right"].addTransition(_control_forward_backward_turning_right_2)
  1153. def _control_left_right_going_forward_2_exec(self, parameters):
  1154. self.tank.moveUp()
  1155. def _control_left_right_going_backward_2_exec(self, parameters):
  1156. self.tank.moveDown()
  1157. def _control_forward_backward_turning_left_2_exec(self, parameters):
  1158. self.tank.turnLeft()
  1159. def _control_forward_backward_turning_right_2_exec(self, parameters):
  1160. self.tank.turnRight()
  1161. def initializeStatechart(self):
  1162. # enter default state
  1163. self.default_targets = self.states["/control"].getEffectiveTargetStates()
  1164. RuntimeClassBase.initializeStatechart(self)
  1165. class TurretControl(RuntimeClassBase):
  1166. def __init__(self, controller, tank):
  1167. RuntimeClassBase.__init__(self, controller)
  1168. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  1169. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  1170. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  1171. self.semantics.priority = StatechartSemantics.SourceParent
  1172. self.semantics.concurrency = StatechartSemantics.Single
  1173. # build Statechart structure
  1174. self.build_statechart_structure()
  1175. # user defined attributes
  1176. self.tank = None
  1177. # call user defined constructor
  1178. TurretControl.user_defined_constructor(self, tank)
  1179. def user_defined_constructor(self, tank):
  1180. self.tank = tank
  1181. def user_defined_destructor(self):
  1182. pass
  1183. # builds Statechart structure
  1184. def build_statechart_structure(self):
  1185. # state <root>
  1186. self.states[""] = State(0, self)
  1187. # state /turret
  1188. self.states["/turret"] = ParallelState(1, self)
  1189. # state /turret/rotation
  1190. self.states["/turret/rotation"] = State(2, self)
  1191. # state /turret/rotation/none
  1192. self.states["/turret/rotation/none"] = State(3, self)
  1193. # state /turret/rotation/turning_left
  1194. self.states["/turret/rotation/turning_left"] = State(4, self)
  1195. # state /turret/rotation/turning_right
  1196. self.states["/turret/rotation/turning_right"] = State(5, self)
  1197. # state /turret/shooting
  1198. self.states["/turret/shooting"] = State(6, self)
  1199. # state /turret/shooting/polling
  1200. self.states["/turret/shooting/polling"] = State(7, self)
  1201. # add children
  1202. self.states[""].addChild(self.states["/turret"])
  1203. self.states["/turret"].addChild(self.states["/turret/rotation"])
  1204. self.states["/turret"].addChild(self.states["/turret/shooting"])
  1205. self.states["/turret/rotation"].addChild(self.states["/turret/rotation/none"])
  1206. self.states["/turret/rotation"].addChild(self.states["/turret/rotation/turning_left"])
  1207. self.states["/turret/rotation"].addChild(self.states["/turret/rotation/turning_right"])
  1208. self.states["/turret/shooting"].addChild(self.states["/turret/shooting/polling"])
  1209. self.states[""].fixTree()
  1210. self.states[""].default_state = self.states["/turret"]
  1211. self.states["/turret/rotation"].default_state = self.states["/turret/rotation/none"]
  1212. self.states["/turret/shooting"].default_state = self.states["/turret/shooting/polling"]
  1213. # transition /turret/rotation/none
  1214. _turret_rotation_none_0 = Transition(self, self.states["/turret/rotation/none"], [self.states["/turret/rotation/turning_right"]])
  1215. _turret_rotation_none_0.setTrigger(Event("turn_right", None))
  1216. self.states["/turret/rotation/none"].addTransition(_turret_rotation_none_0)
  1217. _turret_rotation_none_1 = Transition(self, self.states["/turret/rotation/none"], [self.states["/turret/rotation/turning_left"]])
  1218. _turret_rotation_none_1.setTrigger(Event("turn_left", None))
  1219. self.states["/turret/rotation/none"].addTransition(_turret_rotation_none_1)
  1220. # transition /turret/rotation/turning_left
  1221. _turret_rotation_turning_left_0 = Transition(self, self.states["/turret/rotation/turning_left"], [self.states["/turret/rotation/none"]])
  1222. _turret_rotation_turning_left_0.setTrigger(Event("stop_turning", None))
  1223. self.states["/turret/rotation/turning_left"].addTransition(_turret_rotation_turning_left_0)
  1224. _turret_rotation_turning_left_1 = Transition(self, self.states["/turret/rotation/turning_left"], [self.states["/turret/rotation/turning_right"]])
  1225. _turret_rotation_turning_left_1.setTrigger(Event("turn_right", None))
  1226. self.states["/turret/rotation/turning_left"].addTransition(_turret_rotation_turning_left_1)
  1227. _turret_rotation_turning_left_2 = Transition(self, self.states["/turret/rotation/turning_left"], [self.states["/turret/rotation/turning_left"]])
  1228. _turret_rotation_turning_left_2.setAction(self._turret_rotation_turning_left_2_exec)
  1229. _turret_rotation_turning_left_2.setTrigger(Event("update", "engine"))
  1230. self.states["/turret/rotation/turning_left"].addTransition(_turret_rotation_turning_left_2)
  1231. # transition /turret/rotation/turning_right
  1232. _turret_rotation_turning_right_0 = Transition(self, self.states["/turret/rotation/turning_right"], [self.states["/turret/rotation/none"]])
  1233. _turret_rotation_turning_right_0.setTrigger(Event("stop_turning", None))
  1234. self.states["/turret/rotation/turning_right"].addTransition(_turret_rotation_turning_right_0)
  1235. _turret_rotation_turning_right_1 = Transition(self, self.states["/turret/rotation/turning_right"], [self.states["/turret/rotation/turning_left"]])
  1236. _turret_rotation_turning_right_1.setTrigger(Event("turn_left", None))
  1237. self.states["/turret/rotation/turning_right"].addTransition(_turret_rotation_turning_right_1)
  1238. _turret_rotation_turning_right_2 = Transition(self, self.states["/turret/rotation/turning_right"], [self.states["/turret/rotation/turning_right"]])
  1239. _turret_rotation_turning_right_2.setAction(self._turret_rotation_turning_right_2_exec)
  1240. _turret_rotation_turning_right_2.setTrigger(Event("update", "engine"))
  1241. self.states["/turret/rotation/turning_right"].addTransition(_turret_rotation_turning_right_2)
  1242. # transition /turret/shooting/polling
  1243. _turret_shooting_polling_0 = Transition(self, self.states["/turret/shooting/polling"], [self.states["/turret/shooting/polling"]])
  1244. _turret_shooting_polling_0.setAction(self._turret_shooting_polling_0_exec)
  1245. _turret_shooting_polling_0.setTrigger(Event("shoot", None))
  1246. self.states["/turret/shooting/polling"].addTransition(_turret_shooting_polling_0)
  1247. def _turret_rotation_turning_left_2_exec(self, parameters):
  1248. self.tank.turnCannonLeft()
  1249. def _turret_rotation_turning_right_2_exec(self, parameters):
  1250. self.tank.turnCannonRight()
  1251. def _turret_shooting_polling_0_exec(self, parameters):
  1252. self.tank.shoot()
  1253. def initializeStatechart(self):
  1254. # enter default state
  1255. self.default_targets = self.states["/turret"].getEffectiveTargetStates()
  1256. RuntimeClassBase.initializeStatechart(self)
  1257. class ObjectManager(ObjectManagerBase):
  1258. def __init__(self, controller):
  1259. ObjectManagerBase.__init__(self, controller)
  1260. def instantiate(self, class_name, construct_params):
  1261. if class_name == "Main":
  1262. instance = Main(self.controller, construct_params[0])
  1263. instance.associations = {}
  1264. instance.associations["radar"] = Association("Radar", 1, 1)
  1265. instance.associations["enemy_tracker"] = Association("EnemyTracker", 1, 1)
  1266. instance.associations["pilot_strategy"] = Association("PilotStrategy", 1, 1)
  1267. instance.associations["explore_planner"] = Association("ExplorePlanner", 1, 1)
  1268. instance.associations["attack_planner"] = Association("AttackPlanner", 1, 1)
  1269. instance.associations["path_finder"] = Association("PathFinder", 1, 1)
  1270. instance.associations["steering"] = Association("Steering", 1, 1)
  1271. instance.associations["turret_steering"] = Association("TurretSteering", 1, 1)
  1272. instance.associations["motor_control"] = Association("MotorControl", 1, 1)
  1273. instance.associations["turret_control"] = Association("TurretControl", 1, 1)
  1274. elif class_name == "Radar":
  1275. instance = Radar(self.controller, construct_params[0])
  1276. instance.associations = {}
  1277. instance.associations["enemy_tracker"] = Association("EnemyTracker", 1, 1)
  1278. instance.associations["pilot_strategy"] = Association("PilotStrategy", 1, 1)
  1279. elif class_name == "EnemyTracker":
  1280. instance = EnemyTracker(self.controller)
  1281. instance.associations = {}
  1282. instance.associations["pilot_strategy"] = Association("PilotStrategy", 1, 1)
  1283. instance.associations["attack_planner"] = Association("AttackPlanner", 1, 1)
  1284. elif class_name == "PilotStrategy":
  1285. instance = PilotStrategy(self.controller)
  1286. instance.associations = {}
  1287. instance.associations["explore_planner"] = Association("ExplorePlanner", 1, 1)
  1288. instance.associations["attack_planner"] = Association("AttackPlanner", 1, 1)
  1289. elif class_name == "ExplorePlanner":
  1290. instance = ExplorePlanner(self.controller, construct_params[0], construct_params[1])
  1291. instance.associations = {}
  1292. instance.associations["path_finder"] = Association("PathFinder", 1, 1)
  1293. elif class_name == "AttackPlanner":
  1294. instance = AttackPlanner(self.controller)
  1295. instance.associations = {}
  1296. instance.associations["turret_steering"] = Association("TurretSteering", 1, 1)
  1297. instance.associations["path_finder"] = Association("PathFinder", 1, 1)
  1298. instance.associations["turret_control"] = Association("TurretControl", 1, 1)
  1299. elif class_name == "PathFinder":
  1300. instance = PathFinder(self.controller, construct_params[0], construct_params[1])
  1301. instance.associations = {}
  1302. instance.associations["steering"] = Association("Steering", 1, 1)
  1303. elif class_name == "Steering":
  1304. instance = Steering(self.controller, construct_params[0], construct_params[1])
  1305. instance.associations = {}
  1306. instance.associations["motor_control"] = Association("MotorControl", 1, 1)
  1307. instance.associations["path_finder"] = Association("PathFinder", 1, 1)
  1308. elif class_name == "TurretSteering":
  1309. instance = TurretSteering(self.controller, construct_params[0])
  1310. instance.associations = {}
  1311. instance.associations["turret_control"] = Association("TurretControl", 1, 1)
  1312. instance.associations["attack_planner"] = Association("AttackPlanner", 1, 1)
  1313. elif class_name == "MotorControl":
  1314. instance = MotorControl(self.controller, construct_params[0])
  1315. instance.associations = {}
  1316. elif class_name == "TurretControl":
  1317. instance = TurretControl(self.controller, construct_params[0])
  1318. instance.associations = {}
  1319. else:
  1320. raise Exception("Cannot instantiate class " + class_name)
  1321. return instance
  1322. class Controller(EventLoopControllerBase):
  1323. def __init__(self, tank, event_loop_callbacks, finished_callback = None, behind_schedule_callback = None):
  1324. if finished_callback == None: finished_callback = None
  1325. if behind_schedule_callback == None: behind_schedule_callback = None
  1326. EventLoopControllerBase.__init__(self, ObjectManager(self), event_loop_callbacks, finished_callback, behind_schedule_callback)
  1327. self.addInputPort("engine")
  1328. self.object_manager.createInstance("Main", [tank])