Esempio n. 1
0
def place_actor(actor_list, building_rect, distance=100, boundries=None):
    if type(distance) == list:
        for d in distance:
            r = place_actor(actor_list, building_rect, d, boundries)
            if r != None:
                return r
        return None
    
    """Tests several positions for the building by nudging it around. This is
    because an AI may not always have the most recent positions for actors."""
    
    ox, oy = building_rect.left, building_rect.right
    
    nudges = (
        (0,0),
        
        (-1,-1),
        (-1,0),
        (-1,1),
        (0,-1),
        (0,1),
        (1,-1),
        (1,0),
        (1,1),
    )
    
    for x,y in nudges:
        building_rect.left = ox + x * distance
        building_rect.top = oy + y * distance
        
        if not sim_lib.test_possible_collision(actor_list, building_rect, True):
            
            if boundries != None:
                if 0 < building_rect.left and building_rect.right < boundries[0]:
                    if 0 < building_rect.top and building_rect.bottom < boundries[1]:
                        return building_rect
            else:
                return building_rect
    
    return None
Esempio n. 2
0
 def logic_cycle(self):
     """The core function of the sim, this is where the 'magic happens'"""
     if int(time.time()) != self.cycle_count[1]:
         self.cycle_count = [0, int(time.time())]
     
     self.next_ai_update -= 1
     if self.next_ai_update <= 0:
         self.update_ai_queues()
         self.next_ai_update = 30
     
     self.read_ai_queues()
     
     self.tick += 1
     
     self.orders[self.tick + self.tick_jump] = []
     self.q_orders[self.tick + self.tick_jump] = []
     self.issue_orders()
     
     # This will warn us if the sim is lagging behind how fast it's meant to be
     time_over = time.time() - self.next_cycle
     if time_over > 0.25:
         print("Logic lag of %ss" % time_over)
     
     # Update the AIs
     for t, a in self.autotargeters.items():
         a.update()
     
     # Update the actors themselves
     to_remove = []
     to_add = []
     for i, a in enumerate(self.actors):
         # First we need to check to see if it's got a build order
         # it'd have one because the AI can't directly tell us
         # to build something and instantly be told what the building
         # item is, thus we place it here and automatically tell
         # the actor to go build it
         if a.current_order[0] == "build" and a.completion >= 100:
             cmd, pos, type_name = a.current_order
             
             # Should this be done via a build queue instead?
             if self.actor_types[a.actor_type]['uses_build_queue']:
                 a.build_queue.append(type_name)
                 
             else:
                 a_type = self.actor_types[type_name]
         
                 new_rect = pygame.Rect(pos[0], pos[1], a_type['size'][0], a_type['size'][1])
                 building_rect = ai_lib.place_actor(self.actors, new_rect, [50, 100, 200], self.battlefield['size'])
                 
                 if building_rect != None:
                     posx = building_rect.left + building_rect.width/2
                     posy = building_rect.top + building_rect.height/2
             
                     to_add.append((a, {
                         "type": type_name,
                         "pos":  [posx, posy, 0],
                         "team": a.team,
                         "order_queue": list(a.rally_orders),
                     }))
             
             a.next_order()
         
         a.update()
         
         # Is the actor trying to place a new unit?
         # We only check as often as we check for collisions, this gives a cycle
         # for an already started actor to be given a position as it defaults to 0,0
         # and this has an impact on it's rect
         if self._collision_inverval_count < 2:
             if a.build_queue != [] and a.completion >= 100:
                 a_type = self.actor_types[a.build_queue[0]]
                 
                 new_rect_pos = vectors.add_vectors(a.pos, a.build_offset)
                 new_rect = pygame.Rect(new_rect_pos[0], new_rect_pos[1], a_type['size'][0], a_type['size'][1])
             
                 if not sim_lib.test_possible_collision(self.actors, new_rect):
                     to_add.append((a, {
                         "type": a.build_queue[0],
                         "pos":  new_rect_pos,
                         "team": a.team,
                         "order_queue": list(a.rally_orders),
                     }))
                     self.signal_menu_rebuild = True
                     del(a.build_queue[0])
         
         if a.hp <= 0: to_remove.insert(0, i)
     for i in to_remove: del(self.actors[i])
     for builder, new_actor in to_add:
         new_target = self.place_actor(new_actor)
         builder.issue_command("aid", target=new_target)
     
     # Bullets too
     to_delete = []
     for i, b in enumerate(self.bullets):
         b.update()
         
         if b.dead:
             new_effect = b.explode(self.actors)
             if new_effect != None:
                 self.effects.append(new_effect)
             to_delete.insert(0, i)
     for i in to_delete:
         del(self.bullets[i])
     
     # And lastly effects
     to_delete = []
     for i, e in enumerate(self.effects):
         e.update()
         if e.dead:
             to_delete.insert(0, i)
             continue
     
     # Delete any uneeded effects
     for i in to_delete:
         del(self.effects[i])
     
     # Check for collisions
     self._collision_inverval_count -= 1
     
     if self._collision_inverval_count < 1:
         self._collision_inverval_count = self._collision_interval
         collisions = sim_lib.get_collisions(self.actors)
         
         # We now have a list of all the collisions
         for obj1, obj2 in collisions:
             # We order them based on aid, this way we always deal with the same
             # actor in the same way and the order the collison was found is
             # irrelevant
             actor_lib.handle_pathing_collision(min(obj1, obj2), max(obj1, obj2))
     
     # Set next cycle time
     self.next_cycle = time.time() + self._cycle_delay
     self.cycle_count[0] += 1