def __init__(self): super(ObjectBase, self).__init__() self.pos = vectors.V(-100, -100, 0) # Assume we're offscreen self.velocity = vectors.V(0, 0, 0) self.facing = [0, 0] # XY, Z self.oid = 0
def _decelerate_ai(self): total_velocity = self.velocity.magnitude() if total_velocity <= self.deceleration: self.velocity = vectors.V(0, 0, 0) else: new_vel = total_velocity - self.deceleration div = total_velocity / new_vel self.velocity = vectors.V([v / div for v in self.velocity])
def apply_data(self, data): """Applies transitory data such as position and hp""" self.actor_type = data["type"] self.hp = data.get("hp", self.max_hp) self.pos = vectors.V(data.get("pos", self.pos)) self.velocity = vectors.V(data.get("velocity", self.velocity)) self.facing = data.get("facing", self.facing) self.team = data.get("team", self.team) self.completion = data.get("completion", self.completion)
def test_init(self): vals = ( ([0,1,2], [0,1,2]), (1, [1,0,0]), ([0,1], [0,1,0]), ([-5,-4,-6],[-5,-4,-6]), ) for args, expected in vals: if type(args) == list: self.assertEqual(vectors.V(*args).v, expected) self.assertEqual(vectors.V(args).v, expected) # We should get an error if we try to build a vector from a vector self.assertRaises(Exception, vectors.V, vectors.V(1,2,3))
def test_magnitude(self): vals = ( ([3,4,0], 5), ([4,4,4], 6.928), ) for v1, expected in vals: self.assertAlmostEqual(vectors.V(v1).magnitude(), expected, places=2)
def test_equals(self): vals = ( ([1,2,3], [1,2,3], True), ([-1,-2,-3],[-1,-2,-3], True), ([1,2,3], [0,2,3], False), ([1,2,3], [1,0,3], False), ([1,2,3], [1,2,0], False), ([1,1,1], [2,2,2], False), ) for v1, v2, expected in vals: if expected: self.assertEqual(vectors.V(v1), vectors.V(v2)) else: self.assertNotEqual(vectors.V(v1), vectors.V(v2))
def test_operators(self): # Addition add_vals = ( ([0,0,0], [1,1,1], [1,1,1]), ([2,2,2], [1,1,1], [3,3,3]), ([0,0,0], [0,0,0], [0,0,0]), ([3,5,7], [0,5,0], [3,10,7]), ) for v1, v2, expected in add_vals: self.assertEqual(vectors.V(v1) + vectors.V(v2), expected) self.assertEqual(vectors.V(v1) + v2, expected) # Subtraction subtract_vals = ( ([0,0,0], [1,1,1], [-1,-1,-1]), ([2,2,2], [1,1,1], [1,1,1]), ([0,0,0], [0,0,0], [0,0,0]), ([3,5,7], [0,5,0], [3,0,7]), ) for v1, v2, expected in subtract_vals: self.assertEqual(vectors.V(v1) - vectors.V(v2), expected) self.assertEqual(vectors.V(v1) - v2, expected) # Multiplication multiply_vals = ( ([0,0,0], [1,1,1], [0,0,0]), ([2,2,2], [1,2,1], [2,4,2]), ([0,0,0], [0,0,0], [0,0,0]), ([3,5,7], [2,5,0], [6,25,0]), ) for v1, v2, expected in multiply_vals: self.assertEqual(vectors.V(v1) * vectors.V(v2), expected) self.assertEqual(vectors.V(v1) * v2, expected) # Division divide_vals = ( ([0,0,0], [1,2,1], [0,0,0]), ([2,2,2], [1,2,1], [2,1,2]), ([0,0,0], [1,1,1], [0,0,0]), ([3,5,7], [2,2,1], [1.5,2.5,7]), ) for v1, v2, expected in divide_vals: self.assertEqual(vectors.V(v1) / vectors.V(v2), expected) self.assertEqual(vectors.V(v1) / v2, expected) # Absolute abs_vals = ( ([-1,0,0], [1,0,0]), ([2,-2,2], [2,2,2]), ([0,-1,0], [0,1,0]), ([3,5,-7], [3,5,7]), ) for v1, expected in abs_vals: self.assertEqual(abs(vectors.V(v1)), expected)
def test_angle(self): # First we test the function with an argument vals = ( # XY Tests ([0,0,0], [4,-4,0], [45, 0]),# Up and right ([0,0,0], [4,4,0], [135, 0]),# Down and right ([0,0,0], [-4,4,0], [225, 0]),# Down and left ([0,0,0], [-4,-4,0], [315, 0]),# Up and left # Same as before but scaled up in size ([0,0,0], [400,-400,0], [45, 0]),# Up and right ([0,0,0], [1000,1000,0], [135, 0]),# Down and right ([0,0,0], [-0.5,0.5,0], [225, 0]),# Down and left ([0,0,0], [-50000,-50000,0], [315, 0]),# Up and left ([0,0,0], [0,-4,0], [0, 0]),# Dead Up ([0,0,0], [4,0,0], [90, 0]),# Dead Right ([0,0,0], [0,4,0], [180, 0]),# Dead Down ([0,0,0], [-4,0,0], [270, 0]),# Dead Left # Specific XY tests ([1,1,0], [4,100,0], [178.26, 0]),# Down and a little bit right ([400, 400, 0], [100, 900, 0], [210.963,0]),# Down and left # Z Tests # ([0,0,0], [4,-4,0], [0, 45]),# Aim up # ([0,0,0], [4,4,0], [135, 0]),# Aim down # ([0,0,0], [-4,4,0], [225, 0]), # ([0,0,0], [-4,-4,0], [315, 0]), ) for v1, v2, expected in vals: r1, r2 = vectors.V(v1).angle(v2) self.assertAlmostEqual(r1, expected[0], places=2, msg="vectors.angle(%s, %s) should equal %s, instead got %s" % ( v1, v2, expected[0], r1 )) self.assertAlmostEqual(r2, expected[1], places=2) # Now we test the function with no argument vals = ( # XY Tests ([4,-4,0], [45, 0]),# Up and right ([4,4,0], [135, 0]),# Down and right ([-4,4,0], [225, 0]),# Down and left ([-4,-4,0], [315, 0]),# Up and left # Same as before but scaled up in size ([400,-400,0], [45, 0]),# Up and right ([1000,1000,0], [135, 0]),# Down and right ([-0.5,0.5,0], [225, 0]),# Down and left ([-50000,-50000,0], [315, 0]),# Up and left ([0,-4,0], [0, 0]),# Dead Up ([4,0,0], [90, 0]),# Dead Right ([0,4,0], [180, 0]),# Dead Down ([-4,0,0], [270, 0]),# Dead Left ) for a, expected in vals: r, r2 = vectors.V(a).angle() self.assertAlmostEqual(r, expected[0], places=2, msg="vectors.angle(%s) should equal %s, instead got %s" % ( a, expected[0], r )) self.assertAlmostEqual(r2, expected[1], places=2)
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 = vectors.V(0, 0, 0) if target == 0: self.next_order() elif cmd == "move": self._move_ai(pos) if vectors.distance(self.pos, pos) <= self.velocity.magnitude(): self.pos = vectors.V(pos) self.velocity = vectors.V(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