Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.49 KB | None | 0 0
  1. from __future__ import generators
  2. import plus
  3. import AI
  4. from AI import vector3
  5. import Arenas
  6. import Gooey
  7. import math
  8. import operator
  9. import Tactics
  10. import Tactics_2
  11.  
  12. class SpinupOmni2(AI.SuperAI):
  13. "Waits for weapon to spin up before moving. Also reverses weapon if it gets jammed."
  14. name = "SpinupOmni3"
  15. # Update 7/24/12: Fixed AI not moving to avoid immobility when the weapon is jammed.
  16. # ***CUSTOMIZABLE SETTINGS*** #
  17. # 'ticks' sets how many times per tick the AI checks its RPM (Revolutions Per Minute) (a tick is 1/8 second). Needs to be high for accurate measurements on fast spinners, but too high causes lag. Default is 3.75 (=30 times/second).
  18. # ***NOTE*** RA2 can't measure times less than 1/30 second. Therefore the RPM calculator is only accurate for speeds of 1500, 750, 500, 375, 300, and <250 RPM. Speeds over 1500 RPM can't be measured, and accuracy decreases with increasing RPM.
  19. # 'MotorID' tells the AI which motor to measure RPM on.
  20. # 'Motor2ID' (Optional) Set ID for a second motor to measure RPM on.
  21. # 'TargetRPM' is the RPM the weapon needs to be spinning at before the AI will move. (With two motors, only one needs to exceed the target RPM.) Default is 100.
  22. # 'DisplayRPM' Set this to 1 to display current RPM, average RPM, and maximum RPM during battle. Use this to determine ticks and TargetRPM values.
  23. # 'JamTime' Sets the amount of time (in seconds) the weapon should be jammed for, before reversing the direction in an attempt to unjam it. For unidirectional weapons, just set to 999. Default is 1.5.
  24.  
  25. def __init__(self, **args):
  26. AI.SuperAI.__init__(self, **args)
  27.  
  28. self.zone = "weapon"
  29. self.triggers = ["Fire"]
  30. self.trigger2 = ["Srimech"]
  31. self.reloadTime = 0
  32. self.reloadDelay = 3
  33. self.tickFactor = 3.75
  34. self.spin_range = 3.0
  35. self.motorID = 2
  36. self.motorID2 = -1
  37. self.RPM = 0
  38. self.RPM2 = 0
  39. self.revolution = 0
  40. self.revolution2 = 0
  41. self.measuring = 0
  42. self.measuring2 = 0
  43. self.max_RPM = 0
  44. self.RPMhist = []
  45. self.avg_RPM = 0
  46. self.display = 0
  47. self.targetRPM = 100
  48. self.spin_reverse = 0
  49. self.jamTimer = 0
  50. self.raspberry = 1.5
  51. self.stopFunction = self.Stop
  52.  
  53. if 'range' in args:
  54. self.spin_range = args.get('range')
  55.  
  56. if 'triggers' in args: self.triggers = args['triggers']
  57. if 'reload' in args: self.reloadDelay = args['reload']
  58. if 'ticks' in args: self.tickFactor = args['ticks']
  59. if 'MotorID' in args: self.motorID = args['MotorID']
  60. if 'Motor2ID' in args: self.motorID2 = args['Motor2ID']
  61. if 'DisplayRPM' in args: self.display = args['DisplayRPM']
  62. if 'TargetRPM' in args: self.targetRPM = args['TargetRPM']
  63. if 'JamTime' in args: self.raspberry = args['JamTime']
  64.  
  65. self.triggerIterator = iter(self.triggers)
  66.  
  67. self.tactics.append(Tactics_2.Tactic_Evade(self))
  68.  
  69. def Activate(self, active):
  70. plus.AI.__setattr__tickInterval__(self, 0.125/self.tickFactor)
  71. if active:
  72. if AI.SuperAI.debugging:
  73. self.debug = Gooey.Plain("watch", 0, 75, 100, 75)
  74. tbox = self.debug.addText("line0", 0, 0, 100, 15)
  75. tbox.setText("Throttle")
  76. tbox = self.debug.addText("line1", 0, 15, 100, 15)
  77. tbox.setText("Turning")
  78. tbox = self.debug.addText("line2", 0, 30, 100, 15)
  79. tbox.setText("")
  80. tbox = self.debug.addText("line3", 0, 45, 100, 15)
  81. tbox.setText("")
  82. if self.display != 0:
  83. self.tauntbox = Gooey.Plain("taunt", 10, 175, 640, 175)
  84. tbox = self.tauntbox.addText("taunt1", 10, 0, 640, 15)
  85. tbox.setText("")
  86. tbox2 = self.tauntbox.addText("taunt2", 10, 20, 640, 15)
  87. tbox2.setText("")
  88. tbox3 = self.tauntbox.addText("taunt3", 10, 40, 640, 15)
  89. tbox3.setText("")
  90. tbox4 = self.tauntbox.addText("taunt4", 10, 60, 640, 15)
  91. tbox4.setText("")
  92.  
  93. self.RegisterSmartZone(self.zone, 1)
  94. else:
  95. # get rid of reference to self
  96. self.stopFunction = None
  97.  
  98. return AI.SuperAI.Activate(self, active)
  99.  
  100. def Tick(self):
  101. if self.display != 0:
  102. self.tauntbox.get("taunt1").setText(str(self.RPM) + " RPM")
  103. if self.motorID2 > 0:
  104. self.tauntbox.get("taunt2").setText(str(self.RPM2) + " RPM (Motor 2)")
  105. else:
  106. self.tauntbox.get("taunt2").setText("No motor 2.")
  107. self.tauntbox.get("taunt3").setText("Average RPM: "+str(self.avg_RPM))
  108. self.tauntbox.get("taunt4").setText("Maximum RPM: "+str(self.max_RPM))
  109.  
  110. #Measure RPM
  111. if (self.GetMotorAngle(self.motorID) > math.pi/3 or self.GetMotorAngle(self.motorID) < 0) and self.measuring == 0:
  112. self.revolution = plus.getTimeElapsed()
  113. self.measuring = 1
  114.  
  115. if 0 < self.GetMotorAngle(self.motorID) < math.pi/3 and self.measuring == 1:
  116. self.RPM = 50/(plus.getTimeElapsed() - self.revolution)
  117. if self.display != 0:
  118. self.RPMhist.append(self.RPM)
  119. if self.RPM > self.max_RPM:
  120. self.max_RPM = self.RPM
  121. self.measuring = 0
  122.  
  123. if self.motorID2 > 0:
  124. #Measure RPM of second motor
  125. if (self.GetMotorAngle(self.motorID2) > math.pi/3 or self.GetMotorAngle(self.motorID2) < 0) and self.measuring2 == 0:
  126. self.revolution2 = plus.getTimeElapsed()
  127. self.measuring2 = 1
  128.  
  129. if 0 < self.GetMotorAngle(self.motorID2) < math.pi/3 and self.measuring2 == 1:
  130. self.RPM2 = 50/(plus.getTimeElapsed() - self.revolution2)
  131. if self.display != 0:
  132. self.RPMhist.append(self.RPM2)
  133. if self.RPM2 > self.max_RPM:
  134. self.max_RPM = self.RPM2
  135. self.measuring2 = 0
  136.  
  137. if len(self.RPMhist) > 0:
  138. self.avg_RPM = reduce(operator.add, self.RPMhist)/len(self.RPMhist)
  139.  
  140. # fire weapon
  141.  
  142. # spin up depending on enemy's range
  143. enemy, range = self.GetNearestEnemy()
  144.  
  145. if enemy is not None and range < self.spin_range:
  146. if self.spin_reverse == 0:
  147. self.Input("Spin", 0, 100)
  148. if self.spin_reverse == 1:
  149. self.Input("Spin", 0, -100)
  150. elif self.GetInputStatus("Spin", 0) != 0:
  151. self.Input("Spin", 0, 0)
  152.  
  153. # Optional turning control to aid in self righting
  154. if self.IsUpsideDown() and not self.bInvertible:
  155. self.Input("Sridrive", 0, 100)
  156. else:
  157. self.Input("Sridrive", 0, 0)
  158.  
  159. # Try reversing weapons if they get jammed
  160. if plus.getTimeElapsed() - self.revolution > self.raspberry or (self.motorID2 > 0 and plus.getTimeElapsed() - self.revolution2 > self.raspberry):
  161. self.jamTimer += 1
  162. else:
  163. self.jamTimer = 0
  164.  
  165. if self.jamTimer >= 24*self.tickFactor and self.spin_reverse == 0:
  166. self.spin_reverse = 1
  167. self.jamTimer = 0
  168.  
  169. if self.jamTimer >= 24*self.tickFactor and self.spin_reverse == 1:
  170. self.spin_reverse = 0
  171. self.jamTimer = 0
  172.  
  173. targets = [x for x in self.sensors.itervalues() if x.contacts > 0 \
  174. and not plus.isDefeated(x.robot)]
  175.  
  176. if self.weapons:
  177. # slight delay between firing
  178. if self.reloadTime > 0: self.reloadTime -= 1
  179.  
  180. if len(targets) > 0 and self.reloadTime <= 0:
  181. try:
  182. trigger = self.triggerIterator.next()
  183. except StopIteration:
  184. self.triggerIterator = iter(self.triggers)
  185. trigger = self.triggerIterator.next()
  186.  
  187. self.Input(trigger, 0, 1)
  188. self.reloadTime = self.reloadDelay
  189.  
  190. bReturn = AI.SuperAI.Tick(self)
  191.  
  192. # call this now so it takes place after other driving commands
  193. if self.stopFunction: self.stopFunction(len(targets) > 0)
  194.  
  195. return bReturn
  196.  
  197. def Stop(self, bTarget):
  198. # Avoid the enemy if weapon isn't spinning fast
  199. if self.weapons and not self.bImmobile and (self.RPM > self.targetRPM or (self.motorID2 > 0 and self.RPM2 > self.targetRPM)):
  200. self.tactics.append(Tactics.Engage(self))
  201. if self.weapons and not self.bImmobile and (self.RPM < self.targetRPM or (self.motorID2 > 0 and self.RPM2 < self.targetRPM)):
  202. self.tactics.append((Tactics_2.Tactic_Evade(self))
  203. # stay put if the weapon is jammed, to give it a chance to unjam
  204. if (plus.getTimeElapsed() - self.revolution > self.raspberry or (self.motorID2 > 0 and plus.getTimeElapsed() - self.revolution2 > self.raspberry)) and not self.bImmobile:
  205. self.Throttle(0)
  206.  
  207. def StuckHandler(self):
  208. "This default generator is called when the bot is almost immobile."
  209. while 1:
  210. # back up for 2 seconds (will stop once we're not immobile)
  211. for i in range(0, 16*self.tickFactor):
  212. pos = vector3(self.GetLocation())
  213. dir = vector3(self.GetDirection())
  214. self.DriveToLocation((pos - dir * 3).asTuple(), True)
  215. yield 0
  216. # go forward for 2 seconds
  217. for i in range(0, 16*self.tickFactor):
  218. pos = vector3(self.GetLocation())
  219. dir = vector3(self.GetDirection())
  220. self.DriveToLocation((pos + dir * 3).asTuple())
  221. yield 0
  222.  
  223. def Think(self):
  224. self.Evaluate()
  225. self.countdownToEvaluation = 8*self.tickFactor
  226. # shut down motors while we think
  227. self.Throttle(0)
  228. self.Turn(0)
  229.  
  230. def InvertHandler(self):
  231. # fire all weapons once per second (until we're upright!)
  232. while 1:
  233. for trigger in self.trigger2:
  234. self.Input(trigger, 0, 1)
  235.  
  236. for i in range(0, 8*self.tickFactor):
  237. yield 0
  238.  
  239. def LostComponent(self, id):
  240. # if we lose all our weapons, stop using the Engage tactic and switch to Shove
  241. if id in self.weapons: self.weapons.remove(id)
  242.  
  243. if not self.weapons:
  244. tactic = [x for x in self.tactics if x.name == "Engage"]
  245. if len(tactic) > 0:
  246. self.tactics.remove(tactic[0])
  247.  
  248. self.tactics.append(Tactics.Shove(self))
  249. self.tactics.append(Tactics.Charge(self))
  250.  
  251. return AI.SuperAI.LostComponent(self, id)
  252.  
  253. def DebugString(self, id, string):
  254. if self.debug:
  255. if id == 0: self.debug.get("line0").setText(string)
  256. elif id == 1: self.debug.get("line1").setText(string)
  257. elif id == 2: self.debug.get("line2").setText(string)
  258. elif id == 3: self.debug.get("line3").setText(string)
  259.  
  260. AI.register(SpinupOmni2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement