ai_controller.py 81 KB

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