def run_ai(self): if self.micro_orders == []: cmd, pos, target = self.current_order else: cmd, pos, target = self.micro_orders[0] self._passive_ai() self._attack_ai() self._help_ai() if cmd == "stop" or cmd == "hold position": self.velocity = [0, 0, 0] if target == 0: self.next_order() elif cmd == "move": self._move_ai(pos) if vectors.distance(self.pos, pos) <= vectors.total_velocity(self.velocity): self.pos = pos self.velocity = [0, 0, 0] self.next_order() elif cmd == "attack": target = self.get_first_target() # If we have a target, lets move closer to it if target != None: # First, are we within optimum range of our target? # If not then we need to get closer target_distance = vectors.distance(self.pos, target.pos) if target_distance > self.optimum_attack_range: attack_pos = vectors.get_midpoint(self.pos, target.pos, self.optimum_attack_range) self._move_ai(attack_pos) else: # If we are close enough then we can slow down self._decelerate_ai() elif cmd == "aid": if target == None: target = self.get_first_ally() # If we have a target, lets move closer to it if target != None: dist = vectors.distance(self.pos, target.pos) if dist > self.optimum_heal_range: target_pos = vectors.get_midpoint(self.pos, target.pos, self.optimum_heal_range) self._move_ai(target_pos) else: self._decelerate_ai() else: raise Exception("No handler for cmd %s (pos: %s, target: %s)" % (cmd, pos, target)) # Do we have something to build? if self.build_queue != []: pass
def test_midpoint(self): vals = ( ([10, 10, 0], [15, 5, 0], 1, [10.707, 9.292, 0]), ([10, 10, 0], [15, 5, 0], 5, [13.535, 6.464, 0]), ([0, 0, 0], [100, 100, 0], 20, [14.14, 14.14, 0]), # Now 3D ([10, 10, 10], [15, 5, 0], 5, [13.535, 6.464, 14.082]), ) for pos1, pos2, distance, expected in vals: x, y, z = vectors.get_midpoint(pos1, pos2, distance) try: self.assertAlmostEqual(expected[0], x, places=2) self.assertAlmostEqual(expected[1], y, places=2) self.assertAlmostEqual(expected[2], z, places=2) except Exception as e: print("\n\nTrying to midpoint({}, {}, {})\n\n".format( pos1, pos2, distance)) raise
def test_midpoint(self): vals = ( ([10,10,0], [15,5,0], 1, [10.707, 9.292, 0]), ([10,10,0], [15,5,0], 5, [13.535, 6.464, 0]), ([0,0,0], [100,100,0], 20, [14.14, 14.14, 0]), # Now 3D ([10,10,10], [15,5,0], 5, [13.535, 6.464, 14.082]), ) for pos1, pos2, distance, expected in vals: x,y,z = vectors.get_midpoint(pos1, pos2, distance) try: self.assertAlmostEqual(expected[0], x, places=2) self.assertAlmostEqual(expected[1], y, places=2) self.assertAlmostEqual(expected[2], z, places=2) except Exception as e: print("\n\nTrying to midpoint({}, {}, {})\n\n".format(pos1, pos2, distance)) raise
def run_ai(self): if self.micro_orders == []: cmd, pos, target = self.current_order else: cmd, pos, target = self.micro_orders[0] self._passive_ai() self._attack_ai() self._help_ai() if cmd == "stop" or cmd == "hold position": self.velocity = [0, 0, 0] if target == 0: self.next_order() elif cmd == "move": self._move_ai(pos) if vectors.distance(self.pos, pos) <= vectors.total_velocity( self.velocity): self.pos = pos self.velocity = [0, 0, 0] self.next_order() elif cmd == "attack": target = self.get_first_target() # If we have a target, lets move closer to it if target != None: # First, are we within optimum range of our target? # If not then we need to get closer target_distance = vectors.distance(self.pos, target.pos) if target_distance > self.optimum_attack_range: attack_pos = vectors.get_midpoint( self.pos, target.pos, self.optimum_attack_range) self._move_ai(attack_pos) else: # If we are close enough then we can slow down self._decelerate_ai() elif cmd == "aid": if target == None: target = self.get_first_ally() # If we have a target, lets move closer to it if target != None: dist = vectors.distance(self.pos, target.pos) if dist > self.optimum_heal_range: target_pos = vectors.get_midpoint(self.pos, target.pos, self.optimum_heal_range) self._move_ai(target_pos) else: self._decelerate_ai() else: raise Exception("No handler for cmd %s (pos: %s, target: %s)" % (cmd, pos, target)) # Do we have something to build? if self.build_queue != []: pass