def _bounce_one(a1, a2): # Bounces a1 dir_angle = vectors.angle(a2.pos, a1.pos) vel_angle = vectors.angle(a1.velocity) # If they are head on then we want to swivel them a little if vectors.bound_angle(dir_angle[0]+180) == vel_angle[0]: dir_angle[0] = vectors.bound_angle(dir_angle[0] + 40) # Keep trying distances further and further apart until they're # not going to be overlapping any more overlapping = True dist = vectors.total_velocity(a1.velocity) a2_rect = (a2.pos[0], a2.pos[1], a2.size[0], a2.size[1]) while overlapping: new_pos = vectors.add_vectors(a1.pos, vectors.move_to_vector( dir_angle, dist )) new_rect = (new_pos[0], new_pos[1], a1.size[0], a1.size[1]) if not geometry.rect_collision(new_rect, a2_rect, True): overlapping = False dist += 1 # Add a bit to be safe new_pos = vectors.add_vectors(a1.pos, vectors.move_to_vector( dir_angle, dist + vectors.total_velocity(a1.velocity) )) a1.pos = new_pos
def _bounce_one(a1, a2): # Bounces a1 dir_angle = vectors.angle(a2.pos, a1.pos) vel_angle = vectors.angle(a1.velocity) # If they are head on then we want to swivel them a little if vectors.bound_angle(dir_angle[0] + 180) == vel_angle[0]: dir_angle[0] = vectors.bound_angle(dir_angle[0] + 40) # Keep trying distances further and further apart until they're # not going to be overlapping any more overlapping = True dist = vectors.total_velocity(a1.velocity) a2_rect = (a2.pos[0], a2.pos[1], a2.size[0], a2.size[1]) while overlapping: new_pos = vectors.add_vectors(a1.pos, vectors.move_to_vector(dir_angle, dist)) new_rect = (new_pos[0], new_pos[1], a1.size[0], a1.size[1]) if not geometry.rect_collision(new_rect, a2_rect, True): overlapping = False dist += 1 # Add a bit to be safe new_pos = vectors.add_vectors( a1.pos, vectors.move_to_vector(dir_angle, dist + vectors.total_velocity(a1.velocity))) a1.pos = new_pos
def run_ai(self): if self.micro_orders == []: cmd, pos, target = self.current_order else: cmd, pos, target = self.micro_orders[0] self._attack_ai() if cmd == "stop" or cmd == "hold position": self.velocity = [0,0,0] if target == 0: self.next_order() elif cmd == "move" or cmd == "reverse": dist = vectors.distance(self.pos, pos) self.velocity = vectors.move_to_vector(vectors.angle(self.pos, pos), self.max_velocity) if dist <= vectors.total_velocity(self.velocity): self.pos = pos self.velocity = [0,0,0] self.next_order() else: raise Exception("No handler for cmd %s (pos: %s, target: %s)" % (cmd, pos, target))
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 _decelerate_ai(self): total_velocity = vectors.total_velocity(self.velocity) if total_velocity <= self.deceleration: self.velocity = [0, 0, 0] else: new_vel = total_velocity - self.deceleration div = total_velocity / new_vel self.velocity = [v / div for v in self.velocity]
def _move_ai(self, target): # We can drift to the side (like a spaceship) if self.drifts: # First we turn if self._turn_ai(target): # And if facing the right way we accelerate self._accelerate_ai(target) else: if vectors.total_velocity(self.velocity) > 0: self._decelerate_ai() else: # We cannot drift (like a tank) origional_facing = list(self.facing) if self._turn_ai(target): # We are facing the right way, lets accelerate self._accelerate_ai(target) else: # Wrong way, if we are moving we need to stop before turning if vectors.total_velocity(self.velocity) > 0: self.facing = origional_facing self._decelerate_ai()
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