Advertisement
Guest User

CodeTanks

a guest
Nov 12th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 18.57 KB | None | 0 0
  1. *** m3d.py ***
  2. ---
  3. import math as math
  4.  
  5. class Vector:
  6.     def __init__(self,x,y):
  7.         self.x = x
  8.         self.y = y
  9.  
  10. def VectorAdd(v1, v2):
  11.     return Vector( v1.x + v2.x, v1.y + v2.y )
  12.  
  13. def VectorSub(v1, v2):
  14.     return Vector( v1.x - v2.x, v1.y - v2.y )
  15.  
  16. def VectorNeg(v):
  17.     return Vector( -v.x, -v.y )
  18.  
  19. def VectorLength(v):
  20.     return math.hypot(v.x,v.y)
  21.  
  22. def VectorNormalize(v):
  23.     len = VectorLength(v)
  24.     return Vector( v.x / len, v.y / len ) if len != 0 else Vector(0,0)
  25.  
  26. def VectorSetLength(v, len):
  27.     norm = VectorNormalize(v)
  28.     return Vector( norm.x*len, norm.y*len)
  29.  
  30. def VectorRotate(v,angle,origin):
  31.     vec = VectorSub(v,origin)
  32.     rotated = Vector( vec.x * math.cos(angle) - vec.y * math.sin(angle),  vec.y * math.cos(angle) + vec.x * math.sin(angle) )
  33.     return VectorAdd( rotated, origin )
  34.  
  35. class BBox:
  36.     def __init__(self,mid,ext):
  37.         self.mid = mid
  38.         self.ext = Vector(math.fabs(ext.x),math.fabs(ext.y))
  39.  
  40.     def Lines(self):
  41.         v1 = Vector( self.mid.x - self.ext.x, self.mid.y - self.ext.y)
  42.         v2 = Vector( self.mid.x + self.ext.x, self.mid.y - self.ext.y)
  43.         v3 = Vector( self.mid.x - self.ext.x, self.mid.y + self.ext.y)
  44.         v4 = Vector( self.mid.x + self.ext.x, self.mid.y + self.ext.y)
  45.         return ( Line(v1,v2), Line(v2,v3), Line(v3,v4), Line(v4,v1) )
  46.  
  47.     def Mid(self):
  48.         return self.mid
  49.  
  50. class OBBox:
  51.     def __init__(self,box,angle):
  52.         self.box = box
  53.         self.angle = angle
  54.  
  55.     def Lines(self):
  56.         v1 = Vector( self.box.mid.x - self.box.ext.x, self.box.mid.y - self.box.ext.y)
  57.         v2 = Vector( self.box.mid.x + self.box.ext.x, self.box.mid.y - self.box.ext.y)
  58.         v3 = Vector( self.box.mid.x - self.box.ext.x, self.box.mid.y + self.box.ext.y)
  59.         v4 = Vector( self.box.mid.x + self.box.ext.x, self.box.mid.y + self.box.ext.y)
  60.         v1 = VectorRotate( v1, self.angle, self.box.mid )
  61.         v2 = VectorRotate( v2, self.angle, self.box.mid )
  62.         v3 = VectorRotate( v3, self.angle, self.box.mid )
  63.         v4 = VectorRotate( v4, self.angle, self.box.mid )
  64.         return ( Line(v1,v2), Line(v2,v3), Line(v3,v4), Line(v4,v1) )
  65.  
  66.     def Mid(self):
  67.         return self.box.Mid()
  68.  
  69. class Ray:
  70.     def __init__(self,pos,dir):
  71.         self.pos = pos
  72.         self.dir = dir
  73.  
  74. def makeRay( pos, angle):
  75.     dir = Vector( math.cos(angle), math.sin(angle))
  76.     return Ray(pos,dir)
  77.  
  78. def makeLineFromRay( ray, len ):
  79.     end = VectorAdd( VectorSetLength( ray.dir, len ), ray.pos )
  80.     return Line( ray.pos, end )
  81.  
  82. class Line:
  83.     def __init__(self,beg,end):
  84.         self.beg = beg
  85.         self.end = end
  86.  
  87. def intersectsLineRay(ray,line):
  88.     x1_,y1_ = ray.pos.x, ray.pos.y
  89.     x2_,y2_ = ray.pos.x + ray.dir.x, ray.pos.y + ray.dir.y
  90.     x3_,y3_ = line.beg.x, line.beg.y
  91.     x4_,y4_ = line.end.x, line.end.y
  92.     ##Make sure the lines aren't parallel
  93.     if ((y2_ - y1_) / (x2_ - x1_) != (y4_ - y3_) / (x4_ - x3_)):
  94.         d = (((x2_ - x1_) * (y4_ - y3_)) - (y2_ - y1_) * (x4_ - x3_))
  95.         if (d != 0):
  96.             r = (((y1_ - y3_) * (x4_ - x3_)) - (x1_ - x3_) * (y4_ - y3_)) / d
  97.             s = (((y1_ - y3_) * (x2_ - x1_)) - (x1_ - x3_) * (y2_ - y1_)) / d
  98.             if (r >= 0):
  99.                 if (s >= 0 and s <= 1):
  100.                     return True
  101.     return False
  102.  
  103. def intersectsLineLine(l1,l2):
  104.     x1, y1 = l1.beg.x, l1.beg.y
  105.     x2, y2 = l1.end.x, l1.end.y
  106.     x3, y3 = l2.beg.x, l2.beg.y
  107.     x4, y4 = l2.end.x, l2.end.y
  108.  
  109.     bx = x2 - x1;
  110.     by = y2 - y1;
  111.     dx = x4 - x3;
  112.     dy = y4 - y3;
  113.     b_dot_d_perp = bx * dy - by * dx;
  114.     if b_dot_d_perp == 0:
  115.         return None
  116.     cx = x3 - x1
  117.     cy = y3 - y1
  118.     t = (cx * dy - cy * dx) / b_dot_d_perp
  119.     if t < 0 or t > 1:
  120.         return None
  121.  
  122.     u = (cx * by - cy * bx) / b_dot_d_perp;
  123.     if(u < 0 or u > 1):
  124.         return None
  125.     return Vector(x1+t*bx, y1+t*by)
  126.  
  127. def intersectsBoxRay(ray,box):
  128.     lines = box.Lines()
  129.     for l in lines:
  130.         if intersectsLineRay(ray,l):
  131.             return True
  132.     return False
  133.  
  134. def intersectsBoxLine(line,box):
  135.     lines = box.Lines()
  136.     for l in lines:
  137.         if intersectsLineLine(line,l):
  138.             return True
  139.     return False
  140. ---
  141.  
  142. *** const.py ***
  143. ---
  144. from model.TankType import TankType
  145.  
  146. class K:
  147.     RegularShellSpeed = 16
  148.     PremiumShellSpeed = 13
  149.     ShellSize = 8
  150.     CellSize = 80
  151.  
  152.     RotationAcceleration = { #angular acceleration coefficient
  153.                         TankType.MEDIUM         : 0.000837,
  154.                         TankType.HEAVY          : 0.000296,
  155.                         TankType.TANK_DESTROYER : 0.000254
  156.                       }
  157.     #maxRotAccOnSpot = self.rotAcc * me.engine_rear_power_factor
  158.     RotationResistance = { #tank rotation resistance coefficient
  159.                         TankType.MEDIUM         : 0.0205,
  160.                         TankType.HEAVY          : 0.0144,
  161.                         TankType.TANK_DESTROYER : 0.0141
  162.                       }
  163.     #self.rotMomentum = 1 - self.rotRes
  164. ---
  165.  
  166. *** Grid.py ***
  167. ---
  168. import math as math
  169. from m3d import *
  170. from const import K
  171.  
  172. class Grid:
  173.     def __init__(self,width,height):
  174.         self.width = math.ceil(width/K.CellSize)
  175.         self.height = math.ceil(height/K.CellSize)
  176.         self.busy = bytearray([0 for i in range(0, self.width*self.height)])
  177.         self.danger = bytearray([0 for i in range(0, self.width*self.height)])
  178.  
  179.     def FillObtacles(self,list,width):
  180.         def distanceFactor(item,pos):
  181.             distance = item.get_distance_to(pos.x, pos.y)
  182.             return 1 if distance < item.width/2 + width/2 else 0
  183.  
  184.         for i in range(0,self.height*self.width):
  185.             pos = self.CellToVector(i)
  186.             val = 0
  187.             for item in list:
  188.                 val = max( val, distanceFactor(item,pos) )
  189.             self.busy[i] = val
  190.  
  191.     def CalculateDanger(self,cell,tanks,shels):
  192.         width = 300
  193.         def distanceFactor(item,pos):
  194.             distance = item.get_distance_to(pos.x, pos.y)
  195.             val = 100 if distance < item.width/2 else (  50 + 50 * (item.width/2 + width/2 - distance) / (item.width/2 + width/2) if distance < (item.width/2 + width/2) else 0 )
  196.             #box = BBox(pos, Vector(K.CellSize/2,K.CellSize/2))
  197.             #ray = makeRay(item,item.angle + item.turret_relative_angle)
  198.             #val = val + (100 if intersectsBoxRay(ray,box) else 0)
  199.             return val
  200.  
  201.         def shellFactor(item,pos):
  202.             box = BBox(pos, Vector(K.CellSize/2,K.CellSize/2))
  203.             ray = makeRay(item,item.angle)
  204.             return 255 if intersectsBoxRay(ray,box) else 0
  205.  
  206.         pos = self.CellToVector(i)
  207.         val = 0 if i > self.width and i < (self.height-1)*self.width and i % self.width not in [0, self.width-1] else 100
  208.         for item in tanks:
  209.             val = val + distanceFactor(item,pos)
  210.         for item in shells:
  211.             val = val + shellFactor(item,pos)
  212.         return val
  213.  
  214.  
  215.     def FillDanger(self,tanks,shells):
  216.         for i in range(0,self.height*self.width):
  217.             val = self.CalculateDanger(i,tanks,shell)
  218.             self.danger[i] = int(val) if val < 255 else 255
  219.  
  220.     def GetDanger(self,pos):
  221.         start = self.VectorToCell(pos)
  222.         return self.danger[start]
  223.  
  224.     def VectorToCell(self,v):
  225.         return math.floor(v.y/K.CellSize)*self.width + math.floor(v.x/K.CellSize)
  226.  
  227.     def CellToVector(self,c):
  228.         return Vector( (c%self.width)*K.CellSize + K.CellSize/2, math.floor(c/self.width)*K.CellSize + K.CellSize/2 )
  229.  
  230.     def naighbors(self,e,full):
  231.         def check(e,width,height,busy):
  232.             if e >= 0 and e < width*height:
  233.                 if busy[e] == 0:
  234.                     return e
  235.             return None
  236.  
  237.         ret = []
  238.         c = e - self.width
  239.         if check(c,self.width,self.height,self.busy) != None:
  240.             ret.append(c)
  241.         c = e - 1
  242.         if check(c,self.width,self.height,self.busy) != None:
  243.             if e % self.width > 0:
  244.                 ret.append(c)
  245.         c = e + 1
  246.         if check(c,self.width,self.height,self.busy) != None:
  247.             if e % self.width < self.width - 1:
  248.                 ret.append(c)
  249.         c = e + self.width
  250.         if check(c,self.width,self.height,self.busy) != None:
  251.             ret.append(c)
  252.  
  253.         if full == True:
  254.             if e % self.width > 0:
  255.                 c = e - self.width - 1
  256.                 if check(c,self.width,self.height,self.busy) != None:
  257.                     ret.append(c)
  258.                 c = e + self.width - 1
  259.                 if check(c,self.width,self.height,self.busy) != None:
  260.                     ret.append(c)
  261.  
  262.             if e % self.width < self.width - 1:
  263.                 c = e - self.width + 1
  264.                 if check(c,self.width,self.height,self.busy) != None:
  265.                     ret.append(c)
  266.                 c = e + self.width + 1
  267.                 if check(c,self.width,self.height,self.busy) != None:
  268.                     ret.append(c)
  269.  
  270.         return ret
  271.  
  272.     def NewWave(self,waveGrid,num,oldList):
  273.         newList = []
  274.         for e in oldList:
  275.             n = self.naighbors(e,False)
  276.             for c in n:
  277.                 if waveGrid[c] > num:
  278.                     newList.append(c)
  279.                     waveGrid[c] = num
  280.  
  281.         return newList
  282.  
  283.     def MakePath(self,waveGrid,targetCell):
  284.         pos = self.CellToVector(targetCell)
  285.         num = waveGrid[targetCell]
  286.         movingScenario = []
  287.            
  288.         while num > 1:
  289.             prevWave = self.naighbors(targetCell,True)
  290.             bestDistance = math.pow(K.CellSize*2,2) ## bigger value
  291.             bestPos = None
  292.             bestCell = None
  293.             bestNum = num
  294.             for c in prevWave:
  295.                 if waveGrid[c] < num:
  296.                     newPos = self.CellToVector(c)
  297.                     newLen = math.hypot(newPos.x - pos.x,newPos.y - pos.y)
  298.                     if (newLen < bestDistance and waveGrid[c] == bestNum) or waveGrid[c] < bestNum:
  299.                         bestNum = waveGrid[c]
  300.                         bestDistance = newLen
  301.                         bestPos = newPos
  302.                         bestCell = c
  303.  
  304.             num = bestNum
  305.             targetCell = bestCell
  306.             pos = self.CellToVector(targetCell)
  307.             movingScenario.append(pos)
  308.                
  309.         return movingScenario
  310.  
  311.     def FindPath(self, pos, items, maxPath):
  312.         if len(items) == 0:
  313.             return None,None
  314.  
  315.         itemsCells = [ self.VectorToCell(item) for item in items ]
  316.  
  317.         waveGrid = bytearray([255 for j in range(0, self.width*self.height)])
  318.         start = self.VectorToCell(pos)
  319.         waveGrid[start] = 0
  320.         num = 0
  321.         itemsInWave = []
  322.         cellOfItems = []
  323.         wave = [start]
  324.         while num < maxPath/K.CellSize and len(itemsInWave) == 0 and len(wave) > 0:
  325.             num = num + 1
  326.             wave = self.NewWave(waveGrid, num, wave)
  327.            
  328.             for cell in wave:
  329.                 if cell in itemsCells:
  330.                     item = items[itemsCells.index(cell)]
  331.                     itemsInWave.append(item)
  332.                     cellOfItems.append(cell)
  333.                     break
  334.  
  335.         if len(itemsInWave) != 0:
  336.             targetItem = itemsInWave[0]
  337.             targetCell = cellOfItems[0]
  338.             return [Vector(targetItem.x,targetItem.y)] + self.MakePath(waveGrid,targetCell), targetItem
  339.  
  340.         return None,None
  341.  
  342.     def FindCover(self, pos):
  343.         waveGrid = bytearray([255 for j in range(0, self.width*self.height)])
  344.         start = self.VectorToCell(pos)
  345.         waveGrid[start] = 0
  346.         num = 0
  347.         coverVal = self.danger[start]
  348.         coverCell = start
  349.         wave = [start]
  350.         while num < self.width/4 and len(wave) > 0:
  351.             num = num + 1
  352.             wave = self.NewWave(waveGrid, num, wave)
  353.    
  354.             for cell in wave:
  355.                 if self.danger[cell] < coverVal:
  356.                     coverVal = self.danger[cell]
  357.                     coverCell = cell
  358.  
  359.         return [self.CellToVector(coverCell)] + self.MakePath(waveGrid,coverCell)
  360. ---
  361.  
  362. *** MyStrategy.py ***
  363. ---
  364. import math as math
  365. from m3d import *
  366. from Grid import *
  367. from const import K
  368. from model.FireType import FireType
  369. from model.TankType import TankType
  370. from model.BonusType import BonusType
  371. from model.Tank import Tank
  372. import time
  373.  
  374. class BonusMission:
  375.     def __init__(self, unit):
  376.         self.unit = unit
  377.  
  378.     def check(self, world):
  379.         id = self.unit.id
  380.         for unit in world.bonuses:
  381.             if unit.id == id:
  382.                 return True
  383.         return False
  384.  
  385. def removeUnitFromList(list,units):
  386.     ret = []
  387.     for item in list:
  388.         for unit in units:
  389.             if item.id == unit.id:
  390.                 break
  391.         else:
  392.             ret.append(item)
  393.     return ret
  394.  
  395. class MyStrategy:
  396.     def __init__(self):
  397.         self.prediction = {}
  398.         self.mission = None
  399.         self.target = None
  400.         self.busyGrid = None
  401.         self.dangerGrid = None
  402.         self.scenario = None
  403.         self.bonusesFrom = []
  404.         self.targetId = None
  405.  
  406.     def select_tank(self, tank_index, team_size):
  407.         return TankType.MEDIUM  
  408.  
  409.     def move(self, me, world, move):
  410.         if self.busyGrid == None:
  411.             self.busyGrid = Grid(world.width,world.height)
  412.             self.dangerGrid = Grid(world.width,world.height)
  413.  
  414.         self.prediction = self.calculatePrediction(me,world.tanks)
  415.  
  416.         #move
  417.         if world.tick%2 == 0:
  418.             self.busyGrid.FillObtacles(removeUnitFromList(world.tanks,[me]), me.height)
  419.             self.busyGrid.FillDanger(self.selectEnemies(world.tanks),world.shells)
  420.        
  421.             if self.mission == None and self.scenario != None:
  422.                 if self.busyGrid.GetDanger(me) < 50:
  423.                     self.scenario = None
  424.  
  425.             if self.busyGrid.GetDanger(me) > 150:  
  426.                 self.scenario = self.busyGrid.FindCover(me)
  427.                 self.mission = None
  428.  
  429.             if self.mission != None:
  430.                 if self.mission.check(world) == False:
  431.                     self.mission = None
  432.  
  433.             if self.scenario == None:
  434.                 needList = self.selectNeedList(me) 
  435.                 if self.mission == None:
  436.                     needBonuses = self.selectNeedBonuses(world.bonuses,needList)
  437.                 else:
  438.                     needBonuses = [self.mission.unit] + self.selectNeedBonuses(self.selectNewBonuses(world.bonuses),needList)
  439.                 if len(needBonuses) > 0:
  440.                     self.scenario,bonusItem = self.busyGrid.FindPath(me, needBonuses, world.width)
  441.                     if self.scenario != None:
  442.                         self.mission = BonusMission(bonusItem)
  443.        
  444.             if self.scenario == None:
  445.                 if self.busyGrid.GetDanger(me) > 50:   
  446.                     self.scenario = self.busyGrid.FindCover(me)
  447.  
  448.             if self.scenario == None and world.tick % 50 == 0:
  449.                 nearestBonuses = self.selectNearestBonuses(me,world.bonuses,300)
  450.                 if len(nearestBonuses) > 0:
  451.                     self.scenario,bonusItem = self.busyGrid.FindPath(me, nearestBonuses,300)
  452.                     if self.scenario != None:
  453.                         self.mission = BonusMission(bonusItem)
  454.  
  455.         if self.scenario != None:
  456.             if len(self.scenario) > 0:
  457.                 i = 0
  458.                 while i < len(self.scenario) and self.checkObtacles( Line( me, self.scenario[i]), me.height, removeUnitFromList(world.tanks,[me]) ) == True:
  459.                     i = i + 1
  460.            
  461.                 self.scenario[i+1:] = []
  462.                 bonus = self.scenario[-1]
  463.                 if me.get_distance_to_unit(bonus) > me.height:
  464.                     self.moveTo(me, bonus, move)
  465.                 else:
  466.                     self.scenario.pop()
  467.             else:
  468.                 self.scenario = None
  469.  
  470.         # save bonuses
  471.         self.bonusesFrom = world.bonuses
  472.         # fire
  473.         if self.targetId != None:
  474.             target = self.findItemWithId(world.tanks,self.targetId)
  475.             if target != None and (target.crew_health <= 0 or target.hull_durability <= 0):
  476.                 self.targetId = None
  477.                 target = None
  478.                        
  479.         if self.targetId == None or world.tick % 50 == 0:
  480.             enemies = self.selectEnemies(world.tanks)
  481.             target = self.selectBestTarget(me, enemies,world)
  482.             if target != None:
  483.                 self.targetId = target.id
  484.  
  485.         if target != None:
  486.             leftTracPower = (move.left_track_power if move.left_track_power>=0 else move.left_track_power * me.engine_rear_power_factor)
  487.             rightTracPower =(move.right_track_power if move.right_track_power>=0 else move.right_track_power * me.engine_rear_power_factor)
  488.             rotationSpeed = me.angular_speed * (1 - K.RotationResistance[me.type]) + K.RotationAcceleration[me.type] *  (me.crew_health/me.crew_max_health) * (leftTracPower - rightTracPower) / 2
  489.  
  490.             predictedCoord = self.prediction[self.targetId]
  491.             angle = me.get_turret_angle_to(predictedCoord.x, predictedCoord.y)
  492.             angle = angle - rotationSpeed
  493.             if angle > 0:
  494.                 move.turret_turn = me.turret_turn_speed
  495.             elif angle < 0:
  496.                 move.turret_turn = -me.turret_turn_speed
  497.            
  498.             if me.remaining_reloading_time == 0 and world.tick != 0:
  499.                 predicted = self.prediction[target.id]
  500.                 if  intersectsBoxRay(makeRay(me, me.angle+me.turret_relative_angle), OBBox( BBox(predicted, Vector(target.width/4,target.height/4)), target.angle)):
  501.                     obtacles = removeUnitFromList(world.tanks,[me,target]) + world.bonuses
  502.                     if self.checkObtacles(Line(me,predicted),K.ShellSize, obtacles) == False:
  503.                         if me.get_distance_to_unit(predicted) / K.PremiumShellSpeed < 20 or me.premium_shell_count > 2:
  504.                             move.fire_type = FireType.PREMIUM_PREFERRED
  505.                         else:
  506.                             move.fire_type = FireType.REGULAR
  507.  
  508.             if world.tick == 0:
  509.                 move.fire_type = 0
  510.  
  511. ###########################################################################3
  512.     def selectNeedList(self,me):
  513.         ret = []
  514.         if me.crew_health/me.crew_max_health < 0.6:
  515.             ret.append(BonusType.MEDIKIT)
  516.         if me.hull_durability / me.hull_max_durability < 0.6:
  517.             ret.append(BonusType.REPAIR_KIT)
  518.         return ret
  519.  
  520.     def selectNeedBonuses(self,bonuses,needList):
  521.         ret = []
  522.         for bonus in bonuses:
  523.             if bonus.type in needList:
  524.                 ret.append(bonus)
  525.         return ret
  526.  
  527.     def selectEnemies(self, tanks):
  528.         result = [tank for tank in tanks if (tank.crew_health > 0 and tank.hull_durability > 0 and tank.teammate == False)]
  529.         return result
  530.  
  531.     def selectTanksWhoCanShootMe(self, me, tanks):
  532.         result = []
  533.         for tank in tanks:
  534.             angle = tank.get_turret_angle_to_unit(me)
  535.             if math.fabs(angle) < math.pi/18:
  536.                 result.append(tank)
  537.         return result
  538.  
  539.     def selectBestTarget(self, me, targets,world):    
  540.         bestTarget = None
  541.         maxRating = 0
  542.         for tank in targets:
  543.             angle = me.get_turret_angle_to(self.prediction[tank.id].x, self.prediction[tank.id].y)
  544.             obtacles = removeUnitFromList(world.tanks,[me,tank]) + world.bonuses
  545.             if self.checkObtacles(Line(me,self.prediction[tank.id]),K.ShellSize, obtacles) == False:
  546.                 angle = angle if math.fabs(angle) > 0.1 else 0.1
  547.                 distance = me.get_distance_to_unit( tank )
  548.                 rating = 100 / ( math.fabs(angle)/math.pi * distance)
  549.                 if rating > maxRating:
  550.                     maxRating = rating
  551.                     bestTarget = tank
  552.         return bestTarget
  553.  
  554.     def sheckShells(self,me,shells,obtacles):
  555.         pass
  556.  
  557.     def checkObtacles(self,line,width,units):
  558.         for unit in units:
  559.             if intersectsBoxLine(line,BBox( unit, Vector( unit.width/2 + width/2, unit.width/2 + width/2 ) ) ):
  560.                 return True
  561.         return False
  562.  
  563.     def selectNearestBonuses(self, me, bonuses, maxDistance):
  564.         ret = []
  565.         for bonus in bonuses:
  566.             distance = me.get_distance_to_unit(bonus)
  567.             if distance < maxDistance:
  568.                 ret.append(bonus)
  569.         return ret
  570.  
  571.     def selectNewBonuses(self,bonuses):
  572.         ret = []
  573.         for bonus in bonuses:
  574.             if self.findItemWithId(self.bonusesFrom, bonus.id) == None:
  575.                 ret.append(bonus)
  576.         return ret
  577.  
  578.     def findItemWithId(self,list,id):
  579.         for item in list:
  580.             if item.id == id:
  581.                 return item
  582.         return None
  583.  
  584.     def calculatePrediction(self, me, list):
  585.         predicted = {}
  586.         for tank in list:
  587.             distance = me.get_distance_to_unit(tank)
  588.             timeToImpact = distance / (K.PremiumShellSpeed if me.premium_shell_count > 0 else K.RegularShellSpeed)
  589.             predicted[tank.id] = VectorAdd( tank, VectorSetLength( Vector(tank.speedX, tank.speedY), timeToImpact ) )
  590.         return predicted
  591.  
  592.     def moveTo(self, me, pos, move):
  593.         angle = me.get_angle_to(pos.x, pos.y)
  594.  
  595.         if math.fabs(angle) < 3*math.pi/4:
  596.             if angle > 0:
  597.                 move.left_track_power = 1.0
  598.                 move.right_track_power = 2.0*math.pow(math.cos(math.fabs(angle)),2) - 1.0
  599.             elif angle < 0:
  600.                 move.left_track_power = 2.0*math.pow(math.cos(math.fabs(angle)),2) - 1.0
  601.                 move.right_track_power = 1.0
  602.             else:
  603.                 move.left_track_power = 1.0
  604.                 move.right_track_power =1.0
  605.         else:
  606.             if angle < math.pi:
  607.                 move.left_track_power = -1.0
  608.                 move.right_track_power = 1.0 - 2.0 * math.pow(math.cos(math.fabs(math.pi-angle)),2)
  609.             elif angle > -pi:
  610.                 move.left_track_power = 1.0 - 2.0 * math.pow(math.cos(math.fabs(math.pi-angle)),2)
  611.                 move.right_track_power = -1.0
  612.             else:
  613.                 move.left_track_power = -1.0
  614.                 move.right_track_power = -1.0
  615. ---
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement