def do_fight_action(self, action_name): if self.opponent and mapReader.l1_dist(self.state, self.opponent.state) > 2: self.disengage_in_combat(self.opponent) return if self.opponent.killed: self.disengage_in_combat(self.opponent) return self.goals[3].modify_value(-1) if self.opponent.hidden: print str(self), ' cannot find ', str(self.opponent) self.last_action = action_name self.printy_action = action_name damage = 0 # with weapon 1d6 damage + 1d(str/2) + 1 if self.has_weapon: damage = self.weapon.damage + random.randrange(1, (self.attributes['strength'] / 4) + 2) + random.randint(0, self.attributes['weapon_skill']/2 + 1)+ 1 else: # without, 1d2 damage + 1d(str) damage = random.randrange(1, 3) + random.randrange(1, self.attributes['strength'] + 1) draw = random.random() chance_mult = 1 if self.attributes['fighting_skill'] > 3: chance_mult += 0.1 if self.attributes['fighting_skill'] > 7: chance_mult += 0.15 if action_name == 'attack_head': if draw < 0.5 * chance_mult: self.opponent.hurt(damage + 2, 'head') self.goals[7].value = max(self.goals[7].value - (damage + 2) / 2, 0) elif action_name == 'attack_chest': if draw < 0.8 * chance_mult: self.opponent.hurt(damage + 1, 'chest') self.goals[7].value = max(self.goals[7].value - (damage + 1) / 2, 0) elif action_name == 'attack_gut': if draw < 0.8 * chance_mult: self.opponent.hurt(damage, 'gut') self.goals[7].value = max(self.goals[7].value - (damage) / 2, 0) elif action_name == 'attack_legs': if draw < 0.9 * chance_mult: self.opponent.hurt(damage - 1, 'legs') self.goals[7].value = max(self.goals[7].value - (damage - 1) / 2, 0) elif action_name == 'flee': self.opponent.disengage_in_combat(self) for goal in self.goals: if goal.name == "kill": goal.value = 0 self.fighting_state = FIGHT_STATE['fleeing']
def do_action(self, action, game_map): ##IF you can Craft a weapon, do it ##if(not self.has_weapon): ## for weapon in self.weaponInfo.weaponList: ## if self.weaponInfo.canCraft(weapon, self.craftPouch): ## action.index = 7 ## self.wepCanCraft = weapon self.hidden = False self.last_action = action self.printy_action = action.description rand = (random.randint(1, 10)) / 10 loc = game_map[self.state[0]][self.state[1]] if action.index >= 0 and action.index <= 3: # moving so don't know what gonna do here loc.setTribute(None) self.old_state = self.state (game_map[self.state[0]][self.state[1]]).setTribute(None) self.state = ((self.state[0] + action.delta_state[0]) % engine.GameEngine.map_dims[0], (self.state[1] + action.delta_state[1]) % engine.GameEngine.map_dims[1]) (game_map[self.state[0]][self.state[1]]).setTribute(self) elif action.index == 4: # find food food_prob = loc.getFoodChance() for goal in self.goals: if goal.name == "hunger": if rand <= food_prob: goal.value -= action.values[0]*3 elif action.index == 5: # fight self.sighted.engage_in_combat(self) self.goals[3].value = max(self.goals[3].value - action.values[0], 0) elif action.index == 6: # scavenge wep_prob = loc.getWeaponChance() for goal in self.goals: if goal.name == "getweapon": if wep_prob > 0.9: if rand <= wep_prob: self.getWeapon() goal.value -= action.values[0] else: #doCraftScavenge will return zero if you fail to find something, and one if you succeed num = self.checkCraftScavenge(game_map) goal.value -= self.bestScavPoints * self.doCraftScavenge(game_map, self.bestScavChoice) elif action.index == 7: # craft ##Crafting Probability is factored into doCraftWeapon self.checkCraftWeapon() if self.wepCanCraft != '': for goal in self.goals: if goal.name == "getweapon": ## Returns boolean if you did it or not crafted = self.doCraftWeapon(game_map, self.wepCanCraft) if crafted: goal.value = 0 else: goal.value -= (self.attributes['crafting_skill']) elif action.index == 8: # hide ub = self.attributes['camouflage_skill'] if random.randrange(0, 11) < ub: self.hidden = True elif action.index == 9: # get water water_prob = loc.getWaterChance() for goal in self.goals: if goal.name == "thirst": if rand <= water_prob: goal.value -= action.values[0] elif action.index == 10: # rest for goal in self.goals: if goal.name == "rest": goal.value -= action.values[0] elif action.index == 11: # talk ally f1 = self.attributes['friendliness'] x = self.state[0] y = self.state[1] w = engine.GameEngine.map_dims[0] h = engine.GameEngine.map_dims[1] targ = game_map[(x + 1) % w][y].tribute or \ game_map[(x - 1) % w][y].tribute or \ game_map[x][(y + 1) % h].tribute or \ game_map[x][(y - 1) % h].tribute if not targ: print 'No target for ally!' elif targ not in self.allies and targ.id != self.id: f2 = targ.attributes['friendliness'] a1 = self.attributes['district_prejudices'][targ.district] a2 = targ.attributes['district_prejudices'][self.district] v = (f1 + f2 + a1 + a2) / 224.0 if random.random() < v: ##print str(self), ' and ', str(targ), ' have gotten allied!' self.allies.append(targ) targ.allies.append(self) self.goals[6].value = 0 elif action.index == 12: # explore directions = mapReader.get_neighbors(game_map, self.state) evals = [] for i, direction in enumerate(directions): if game_map[direction[0]][direction[1]].tribute is None: evals.append((mapReader.l1_dist(direction, self.explore_point), direction, i)) if len(evals) > 0: direction = min(evals, key=lambda x: x[0] + random.random() / 1000) # rand is for breaking ties if mapReader.l1_dist(self.explore_point, direction[1]) < 3: if self.goals[3].value > FIGHT_EMERGENCY_CUTOFF: self.last_sighted_location = (self.explore_point[0] + U(0, 16), self.explore_point[1] + U(0, 16)) else: self.explore_point_index = (self.explore_point_index + 1) % len(NAVIGATION_POINTS) self.explore_point = NAVIGATION_POINTS[self.explore_point_index] ##print 'exploring!!' self.state = direction[1]
def do_action(self, action, game_map): self.hidden = False self.last_action = action self.printy_action = action.description rand = (random.randint(1, 10)) / 10 loc = game_map[self.state[0]][self.state[1]] if 3 >= action.index >= 0: # moving so don't know what gonna do here loc.set_tribute(None) self.old_state = self.state (game_map[self.state[0]][self.state[1]]).set_tribute(None) self.state = ((self.state[0] + action.delta_state[0]) % engine.GameEngine.map_dims[0], (self.state[1] + action.delta_state[1]) % engine.GameEngine.map_dims[1]) game_map[self.state[0]][self.state[1]].set_tribute(self) elif action.index == 4: # find food food_prob = loc.get_food_chance() if rand <= food_prob: self.goals['hunger'].modify_value(-action.values[0] * 3) elif action.index == 5: # fight self.sighted.engage_in_combat(self) self.goals['kill'].value = max( self.goals['kill'].value - action.values[0], 0) elif action.index == 6: # scavenge wep_prob = loc.get_weapon_chance() goal = self.goals['get_weapon'] if wep_prob > 0.9: if rand <= wep_prob: self.get_weapon() goal.modify_value(-action.values[0]) else: # doCraftScavenge will return zero if you fail to find something, and one if you succeed #num = self.check_craft_scavenge(game_map) goal.modify_value(-self.best_scavenge_points * self.do_craft_scavenge(game_map, self.best_scavenge_choice)) elif action.index == 7: # craft # Crafting Probability is factored into doCraftWeapon self.check_craft_weapon() if self.wep_can_craft != '': # Returns boolean if you did it or not crafted = self.do_craft_weapon(self.wep_can_craft) if crafted: self.goals['get_weapon'].value = 0 else: self.goals[ 'get_weapon'].modify_value(-self.attributes['crafting_skill']) elif action.index == 8: # hide ub = self.attributes['camouflage_skill'] if random.randrange(0, 11) < ub: self.hidden = True elif action.index == 9: # get water water_prob = loc.get_water_chance() if rand <= water_prob: self.goals['thirst'].modify_value(-action.values[0]) elif action.index == 10: # rest self.goals['rest'].modify_value(-action.values[0]) elif action.index == 11: # talk ally f1 = self.attributes['friendliness'] x = self.state[0] y = self.state[1] w = engine.GameEngine.map_dims[0] h = engine.GameEngine.map_dims[1] target = game_map[(x + 1) % w][y].tribute or \ game_map[(x - 1) % w][y].tribute or \ game_map[x][(y + 1) % h].tribute or \ game_map[x][(y - 1) % h].tribute if not target: print 'No target for ally!' elif target not in self.allies and target.id != self.id: f2 = target.attributes['friendliness'] a1 = self.attributes['district_prejudices'][target.district] a2 = target.attributes['district_prejudices'][self.district] v = (f1 + f2 + a1 + a2) / 224.0 if random.random() < v: if engine.GameEngine.FIGHT_MESSAGES: print str(self), ' and ', str(target), ' have gotten allied!' self.allies.append(target) target.allies.append(self) self.goals['ally'].value = 0 elif action.index == 12: # explore directions = mapReader.get_neighbors(game_map, self.state) evals = [] for i, direction in enumerate(directions): if game_map[direction[0]][direction[1]].tribute is None: evals.append( (mapReader.l1_dist( direction, self.explore_point), direction, i)) if len(evals) > 0: direction = min( evals, key=lambda x1: x1[0] + random.random() / 1000) # rand is for breaking ties if mapReader.l1_dist(self.explore_point, direction[1]) < 3: if self.goals['kill'].value > FIGHT_EMERGENCY_CUTOFF: self.last_sighted_location = (self.explore_point[0] + u(0, 16), self.explore_point[1] + u(0, 16)) else: self.explore_point_index = ( self.explore_point_index + 1) % len(NAVIGATION_POINTS) self.explore_point = NAVIGATION_POINTS[ self.explore_point_index] self.state = direction[1]