def move_to_target_unit(self, game): unit = game.unit_by_name(self.target_unit) if not unit: self.target_unit = None self.reset(game) return False self.point_at(unit) if obstacle(game, (self.x + self.dx, self.y + self.dy)): if (unit.x, unit.y) == (self.x + self.dx, self.y + self.dy): if unit.hostile != self.hostile: self.refresh_activity(game, 'attacking') elif obstacle_unit(game, (self.x + self.dx, self.y + self.dy)): unit_2 = obstacle_unit(game, (self.x + self.dx, self.y + self.dy)) if unit_2.hostile != self.hostile: self.target_unit = unit_2.name self.refresh_activity(game, 'attacking') else: self.sidestep(game) else: self.sidestep(game) else: if random.random() < self.move_frequency_2: if random.random() < self.lurch_frequency and distance( (self.x, self.y), (unit.x, unit.y)) >= 3: self.refresh_activity(game, 'lurching') else: self.refresh_activity(game, 'walking') self.reset_ticks()
def get_action(self, game): self.target_unit = None for unit in game.units: if unit.hostile != self.hostile: if self.target_unit: target_unit = game.unit_by_name(self.target_unit) if distance((self.x, self.y), (unit.x, unit.y)) < distance( (self.x, self.y), (target_unit.x, target_unit.y)): self.target_unit = unit.name else: if distance((self.x, self.y), (unit.x, unit.y)) <= 5: self.target_unit = unit.name if self.target_unit: target_unit = game.unit_by_name(self.target_unit) if not unit: self.target_unit = None self.reset(game) return False self.point_at(target_unit) (self.dx, self.dy) = (-self.dx, -self.dy) if obstacle(game, (self.x + self.dx, self.y + self.dy)): self.sidestep(game) else: if (self.x - self.dx, self.y - self.dy) == (target_unit.x, target_unit.y): (self.dx, self.dy) = (-self.dx, -self.dy) self.refresh_activity(game, 'attacking') else: if random.random() > 0.50: self.refresh_activity(game, 'walking') self.reset_ticks() else: #face nearest player unit min_distance = 100 self.target_unit = None for unit in game.units: if distance((self.x, self.y), (unit.x, unit.y)) < min_distance: self.target_unit = unit.name self.point_at(unit) min_distance = distance((self.x, self.y), (unit.x, unit.y)) if self.target_unit: if obstacle(game, (self.x + self.dx, self.y + self.dy)): pass else: pass #self.refresh_activity(game, 'walking') self.reset_ticks()
def do_events(self, game): self.next_frame(game) for unit in game.units: if unit.name != self.name: if (unit.x, unit.y) == (self.x, self.y): self.sidestep(game) return if self.current_activity == 'walking': if self.ticks == 2: for unit in game.units: if (unit.x, unit.y) == (self.x + self.dx, self.y + self.dy): if unit.hostile == self.hostile: if unit.current_activity == 'standing': if unit.name != self.target_unit: unit.knockback(game, (self.dx, self.dy)) if obstacle(game, (self.x + self.dx, self.y + self.dy)): self.sidestep(game) else: self.move((self.dx, self.dy)) elif self.ticks == 4: self.reset(game) elif self.current_activity == 'standing': if self.ticks == 4: self.reset(game) elif self.current_activity == 'falling': if self.ticks == 8: self.refresh_activity(game, 'dead') game.corpses.append(Corpse(game, self)) self.selected = False for unit in game.units: if unit.target_unit == self.name: unit.target_unit = None game.units.remove(self) return elif self.current_activity == 'stunned': if self.ticks == 16: self.reset(game) if self.ticks >= 16: print "WaypointedCivilian failsafe" self.reset(game) if self.ticks == 0: self.get_action(game)
def wander_to_target_unit(self, game): #For units in smell range, aka far away. unit = game.unit_by_name(self.target_unit) if not unit: self.target_unit = None self.reset(game) return False else: self.point_at(unit) rand_seed = random.random() # Small chance to move in random direction if rand_seed < 0.1: self.dx = random.randint(-1, 1) self.dy = random.randint(-1, 1) if rand_seed < self.move_frequency_1: if obstacle(game, (self.x + self.dx, self.y + self.dy)): self.sidestep(game) else: self.refresh_activity(game, 'walking') self.reset_ticks() self.target_unit = None if random.random() < 0.05: self.play_smell_sound()
def get_action(self, game): self.reset_ticks() self.target_unit = None nearest_friendly_healer = None nearest_friendly_ranged = None nearest_friendly_melee = None for unit in game.units: if distance((self.x, self.y), (unit.x, unit.y)) <= 15: if unit.hostile == self.hostile and unit != self: # before and after joining the party if (self.ally and unit.playable) or ((not self.ally) and (not unit.playable) and (not unit.ally)): if isinstance(unit, PlayerHealer): if nearest_friendly_healer == None: nearest_friendly_healer = unit elif distance((self.x, self.y), (unit.x, unit.y)) < distance( (self.x, self.y), (nearest_friendly_healer.x, nearest_friendly_healer.y)): nearest_friendly_healer = unit elif isinstance(unit, PlayerFemale) or isinstance( unit, PlayerArcher): if nearest_friendly_ranged == None: nearest_friendly_ranged = unit elif distance((self.x, self.y), (unit.x, unit.y)) < distance( (self.x, self.y), (nearest_friendly_ranged.x, nearest_friendly_ranged.y)): nearest_friendly_ranged = unit else: if nearest_friendly_melee == None: nearest_friendly_melee = unit elif distance((self.x, self.y), (unit.x, unit.y)) < distance( (self.x, self.y), (nearest_friendly_melee.x, nearest_friendly_melee.y)): nearest_friendly_melee = unit if nearest_friendly_healer: friendly_target = nearest_friendly_healer elif nearest_friendly_ranged: friendly_target = nearest_friendly_ranged else: friendly_target = None #move closer to friendlies, if necessary if friendly_target: if distance((self.x, self.y), (friendly_target.x, friendly_target.y)) > 10: self.point_at(friendly_target) if obstacle(game, (self.x + self.dx, self.y + self.dy)): self.sidestep(game) return True else: self.refresh_activity(game, "walking") return True #target an enemy for unit in game.units: if unit.hostile != self.hostile: if distance((self.x, self.y), (unit.x, unit.y)) <= 10: if self.target_unit: current_target = game.unit_by_name(self.target_unit) if distance((self.x, self.y), (unit.x, unit.y)) < distance( (self.x, self.y), (current_target.x, current_target.y)): if friendly_target: if distance( (friendly_target.x, friendly_target.y), (unit.x, unit.y)) > 10: continue self.target_unit = unit.name else: self.target_unit = unit.name if self.target_unit: target_unit = game.unit_by_name(self.target_unit) if (self.current_hp / self.max_hp) > 0.25: self.point_at(target_unit) if obstacle(game, (self.x + self.dx, self.y + self.dy)): if (target_unit.x, target_unit.y) == (self.x + self.dx, self.y + self.dy): self.refresh_activity(game, 'attacking') elif obstacle_unit(game, (self.x + self.dx, self.y + self.dy)): unit_2 = obstacle_unit( game, (self.x + self.dx, self.y + self.dy)) if unit_2.hostile != self.hostile: self.target_unit = unit_2.name self.refresh_activity(game, 'attacking') else: self.sidestep(game) else: self.sidestep(game) else: self.refresh_activity(game, 'walking') self.reset_ticks() else: #cowardly behavior if nearest_friendly_healer: if distance( (self.x, self.y), (nearest_friendly_healer.x, nearest_friendly_healer.y )) > 10: #check range of heals self.point_at(nearest_friendly_healer) if obstacle(game, (self.x + self.dx, self.y + self.dy)): self.sidestep(game) return else: self.refresh_activity(game, "walking") return if distance((self.x, self.y), (target_unit.x, target_unit.y)) <= 4: self.point_at(target_unit) (self.dx, self.dy) = (-self.dx, -self.dy) if obstacle(game, (self.x + self.dx, self.y + self.dy)): self.sidestep(game) return else: self.refresh_activity(game, "walking") return else: if nearest_friendly_melee: if distance( (self.x, self.y), (nearest_friendly_melee.x, nearest_friendly_melee.y)) > 5: self.point_at(nearest_friendly_melee) if obstacle(game, (self.x + self.dx, self.y + self.dy)): self.sidestep(game) return else: self.refresh_activity(game, "walking") return
def do_events(self, game): self.next_frame(game) for unit in game.units: if unit.name != self.name: if (unit.x, unit.y) == (self.x, self.y): self.sidestep(game) return if self.current_activity == 'walking': if self.ticks == 2: for unit in game.units: if (unit.x, unit.y) == (self.x + self.dx, self.y + self.dy): if unit.hostile == self.hostile: if unit.current_activity == 'standing': if unit.name != self.target_unit: unit.knockback(game, (self.dx, self.dy)) if obstacle(game, (self.x + self.dx, self.y + self.dy)): self.sidestep(game) else: self.move((self.dx, self.dy)) elif self.ticks == 4: self.reset(game) if (self.target_x, self.target_y) != (None, None): if (self.x, self.y) == (self.target_x, self.target_y): (self.target_x, self.target_y) = (None, None) enemy_units = [ u for u in game.units if u.hostile != self.hostile ] closest_enemy = None max_distance = 10 for u in enemy_units: if distance((self.x, self.y), (u.x, u.y)) < max_distance: if game.LOS((self.x, self.y), (u.x, u.y)): if closest_enemy == None: closest_enemy = u else: if distance((self.x, self.y), (u.x, u.y)) < distance( (self.x, self.y), (closest_enemy.x, closest_enemy.y)): closest_enemy = u if closest_enemy: self.target_unit = closest_enemy.name self.point_at(closest_enemy) self.refresh_activity(game, 'throwing') for rocks in game.rock_piles: if (rocks.x, rocks.y) == (self.x, self.y): rocks.remove_one(game) elif self.current_activity == 'standing': if self.ticks == 4: self.reset(game) elif self.current_activity == 'falling': if self.ticks == 8: self.refresh_activity(game, 'dead') game.corpses.append(Corpse(game, self)) self.selected = False for unit in game.units: if unit.target_unit == self.name: unit.target_unit = None game.units.remove(self) return elif self.current_activity == 'stunned': if self.ticks == 16: self.reset(game) elif self.current_activity == 'throwing': if self.ticks == 4: game.darts.append( ThrowingRock(game, 8, self.name, self.target_unit)) elif self.ticks == 16: self.reset(game) if self.ticks >= 16: print "WaypointedCivilian failsafe" self.reset(game) if self.ticks == 0: if self.current_activity != 'throwing': self.get_action(game)
def get_action(self, game): #EnemyZombie if self.target_unit: old_target = game.unit_by_name(self.target_unit) else: old_target = None #check current target target_unit = game.unit_by_name(self.target_unit) if not target_unit: self.target_unit = None elif distance((self.x, self.y), (target_unit.x, target_unit.y)) > self.smell_radius: self.target_unit = None #target an enemy for unit in game.units: if unit.hostile != self.hostile: if game.LOS((self.x, self.y), (unit.x, unit.y)): if old_target: if distance((self.x, self.y), (unit.x, unit.y)) < distance( (self.x, self.y), (old_target.x, old_target.y)): self.target_unit = unit.name else: if distance((self.x, self.y), (unit.x, unit.y)) <= self.vision_radius: self.target_unit = unit.name if self.target_unit: # i.e. enemy target unit if not old_target: self.play_target_sound() self.move_to_target_unit(game) else: #Find a unit by smell for unit in game.units: if unit.hostile != self.hostile: if self.target_unit: current_target = game.unit_by_name(self.target_unit) if current_target.hostile == self.hostile: if distance((self.x, self.y), (unit.x, unit.y)) <= self.smell_radius: self.target_unit = unit.name if self.name in current_target.zombies: #if target is a wizard current_target.zombies.remove(self.name) elif distance( (self.x, self.y), (unit.x, unit.y)) < distance( (self.x, self.y), (current_target.x, current_target.y)): self.target_unit = unit.name else: if distance((self.x, self.y), (unit.x, unit.y)) <= self.smell_radius: self.target_unit = unit.name if self.target_unit: # i.e. enemy target unit self.wander_to_target_unit(game) else: # wander around at random! if random.random() < 0.25: dx = random.randint(-1, 1) dy = random.randint(-1, 1) if obstacle(game, (self.x + dx, self.y + dy)): self.sidestep(game) else: (self.dx, self.dy) = (dx, dy) self.refresh_activity(game, 'walking') self.reset_ticks()
def do_events(self, game): if self.current_activity in ["falling", "decapitated"]: if self.current_animation.findex < len( self.current_animation.frames) - 1: self.next_frame(game) else: self.next_frame(game) #Emergency maneuvers for obj in game.units + game.trees: if obj != self: if (obj.x, obj.y) == (self.x, self.y): self.sidestep(game) self.move((self.dx, self.dy)) if self.current_activity == 'walking': if self.ticks == 3: game.pointers.append(Pointer((self.x, self.y), self.name)) self.move((self.dx, self.dy)) elif self.ticks == 6: self.reset(game) if self.current_activity == 'lurching': if self.ticks < 3: unit = game.unit_by_name(self.target_unit) self.point_at(unit) if distance((self.x, self.y), (unit.x, unit.y)) > 1: if obstacle(game, (self.x + self.dx, self.y + self.dy)): pass elif not obstacle_unit( game, (self.x + self.dx, self.y + self.dy)): game.pointers.append( Pointer((self.x, self.y), self.name)) self.move((self.dx, self.dy)) else: pass elif self.ticks == 3: self.reset(game) elif self.current_activity == 'rising': if self.ticks == 8: self.reset(game) self.play_target_sound() elif self.current_activity == 'attacking': if self.ticks == 8: self.end_attack(game) elif self.ticks == 12: self.reset(game) elif self.current_activity == 'standing': if self.ticks == 4: self.reset(game) elif self.current_activity == 'falling': if self.ticks == 8: self.reset_ticks() self.refresh_activity(game, 'dead') game.corpses.append(Corpse(game, self)) powerup_chance = 0.20 if random.random() < powerup_chance: for powerup in game.powerups: if (powerup.x, powerup.y) == (self.x, self.y): break else: game.powerups.append( HealthPowerup((self.x, self.y), 30)) for unit in game.units: if unit.target_unit == self.name: unit.target_unit = None game.units.remove(self) return True elif self.current_activity == 'decapitated': if self.ticks == 8: self.reset_ticks() self.refresh_activity(game, 'dead_decapitated') game.corpses.append(Corpse(game, self)) powerup_chance = 0.20 if random.random() < powerup_chance: for powerup in game.powerups: if (powerup.x, powerup.y) == (self.x, self.y): break else: game.powerups.append( HealthPowerup((self.x, self.y), 30)) for unit in game.units: if unit.target_unit == self.name: unit.target_unit = None game.units.remove(self) return True elif self.current_activity == 'stunned': if self.ticks == 16: self.reset(game) if self.ticks >= 16: self.reset(game) print "Zombie failsafe" if self.ticks == 0: if self.current_activity in [ 'falling', 'dead', 'decapitated', 'dead_decapitated', 'stunned' ]: #print 'bad zombie!' pass else: self.get_action(game)