Пример #1
0
 def think(self, things):
     indices = self.rect_of_awareness.collidelistall(things)
     beast = grid.obstruance("beast")
     dangers = self.find_nearest(things[i] for i in indices if things[i].obstruance & beast)
     if dangers:
         danger = dangers[0]
         self.walking = True
         direction = (self.orientation_towards(danger.rect.center) + 4) % 8
         self.orient(direction)
         self.navigate(self.step_position())
         fear = 2
     else:
         flock = self.find_nearest(
             [things[i] for i in indices if isinstance(things[i], Sheep) and things[i] != self]
         )
         if flock:
             choice = random.choice(flock)
             x, y = self.rect.center
             ox, oy = choice.rect.center
             dx, dy = (ox - x, oy - y)
             dist2 = dx * dx + dy * dy
             if 1024 < dist2 <= 4096:
                 self.walking = True
                 self.orient(self.orientation_towards((ox, oy)))
                 self.navigate(self.step_position())
             else:
                 self.walking = False
         fear = 1
     if random.random() < 0.01 * fear:
         phonographs.play(self.attack_phonograph)
 def __init__(self, location):
     self.damage = 0
     self.animation_frame = 0
     self.temporal_accumulator = 0
     self.condition = 0
     horizontal = (location.width/location.height) > 1
     direction = "h" if horizontal else "v"
     self.animated_chromograph_name = "facilities/%s.png"%(direction+self.chromograph_suffix)
     self.footprint = ((grid.LOT_WIDTH, 2 * grid.FENCE_MARGIN_NORTH)
                  if horizontal else
                  (2 * grid.FENCE_MARGIN_WEST, grid.LOT_DEPTH))
     self.obstruance = grid.obstruance(direction+"fence")
     self.exclusion = grid.obstruance(direction+"fence","beast","unit","facility")
     self.rect = Rect(0,0, *self.footprint)
     self.rect.center = location.center
     self.image = self.obtain_frame()
     self.flash = False
 def random_malice(self, things):
     """ Vent spleen on all things within reach """
     indices = self.rect_of_attack.collidelistall(things)
     targets = [things[i] for i in indices
                if things[i].obstruance & grid.obstruance("fence", "facility", "unit")
                and not things[i].destroyed()]
     if targets:
         if self.attack():
             for target in targets:
                 target.harm(self.destructiveness, "trampled")
             return targets
Пример #4
0
 def think(self, things):
     """ Determine the tactics of the unit.
     If it acted upon any thing else, return a list of such
     things. Otherwise return a non-true value.
     """
     # Fire when any beast is attackable
     indices = self.rect_of_awareness.collidelistall(things)
     category = grid.obstruance("beast")
     beasts = self.find_nearest([things[i] for i in indices if things[i].obstruance & category])
     if beasts and not self.attacking:
         target = beasts[0]
         self.orient(self.orientation_towards(target.rect.center))
         if self.rect_of_attack.colliderect(target.rect):
             if self.attack():
                 target.harm(self.firepower, "gunfire")
                 return [target]
 def deal_with_obstacles(self, obstacles):
     """ attack whatever is in the way """
     indices = self.rect_of_attack.collidelistall(obstacles)
     if indices:
         categories = grid.obstruance("unit","facility","fence")
         targets = [obstacles[i] for i in indices 
                    if obstacles[i].obstruance & categories
                    and not obstacles[i].destroyed()]
         if targets and not self.attacking:
             target = targets[0]
             if self.bite_target(target, self.destructiveness, "trampled"):
                 self.maybe_explode()
                 return [target]
         elif not targets:
             # yet somehow obstructed? become dangerously impatient
             self.maybe_explode(0.1)
     directions = (0,1,2,3,4,5,6,7) + ((4,5,6) if self.bored else (1,2,3))
     self.orient(random.choice(directions))
 def on_mousebuttondown(self, e):
     if self.build_menu.is_open:
         choice = self.build_menu.mouse_event(e)
         if choice:
             if choice == "REPAIR":
                 self.repair_facility(self.build_menu.repairable)
             else:
                 self.build_a_thing(choice)
         self.close_build_menu()
     else:
         edge = self.current_edge
         lot = self.current_lot
         if e.button == 1:
             thing = self.situation.last_build
             built = False
             if thing:
                 fence = bool(thing.obstruance & grid.obstruance("fence"))
                 if (edge and fence) or (lot and not fence):
                     self.build_a_thing(thing)
                     built = True
             if not built:
                 self.open_build_menu(e.pos)
         elif e.button == 3:
             self.open_build_menu(e.pos)
 def get_facilities(self):
     return [f for f in self.installations
             if f.obstruance & grid.obstruance("land","facility","fence")]
 def get_beasts(self):
     return [b for b in self.installations
             if b.obstruance & grid.obstruance("beast")]
 def get_units(self):
     return [u for u in self.installations
             if u.obstruance & grid.obstruance("unit")]
Пример #10
0
 def think(self, things):
     indices = self.rect_of_awareness.collidelistall(things)
     beasts = self.find_nearest([things[i] for i in indices if things[i].obstruance & grid.obstruance("beast")])
     if self.destination:
         self.walking = True
         self.orient(self.orientation_towards(self.destination))
         self.navigate(self.step_position())
         if self.rect.collidepoint(self.destination):
             self.destination = None
             self.walking = False
     if beasts and not self.attacking:
         target = beasts[0]
         self.orient(self.orientation_towards(target.rect.center))
         if self.rect_of_attack.colliderect(target.rect):
             if self.attack():
                 target.harm(self.firepower, "gunfire")
                 return [target]