def describe(self): io.out(self.name, "\n\tDescription: ",self.desc, "\n\tValue: ",self.value, "\n\tWeight: ",self.weight,) if self.attributes: for attr in self.attributes: io.out("\t", attr, ": ", self.attributes[attr])
def parse_help(self, words): if words[0] != 'help': return False io.out( 'move/go, inventory/in, look/inspect, pep/stats/skills, pickup/drop, use, attack' ) return True
def use(self, user): if not user.equipped[self.attributes["part"]]: user.equipped[self.attributes["part"]] = self if user.local_player: io.out('You have put on your {}'.format(self.name)) return True else: if user.local_player: io.out('You already have something placed there.') return False
def parse_move(self, words): if words[0] != 'go' and words[0] != 'move': return None if len(words) < 2: io.out('USAGE: [move/go] [direction]') return False direction = " ".join(words[1:]).lower() try: next_room = self.game.p.at.get_room_at(direction) except KeyError: io.out('That is not a valid direction.') return False if not next_room: io.out('There is nothing in that direction.') return False if next_room.locked: io.out('The room is locked and you cannot go in.') return False self.game.p.move_to(next_room) next_room.describe(self.game.p) return True
def parse_attack(self, words): if words[0] != "attack": return None if len(words) != 2: io.out( 'USAGE: attack [character in room]. Use `look` to get the ID of the character.' ) try: entity_id = int(words[1]) except: io.out('Invalid character specified.') return False entities_except_current = self.game.p.at.entities[:] entities_except_current.remove(self.game.p) entity_to_attack = None if not entities_except_current: io.out('There is no one else in the room, you cannot attack.') return False for i, entity in enumerate(entities_except_current): if i == entity_id: entity_to_attack = entity break if not entity_to_attack: io.out('There is no such character to attack.') return False self.game.p.attack_send(entity) return True
def run(self): self.show_intro() self.p = self.create_character() c = self.get_item("Kevlar vest") d = self.get_item("Baseball bat") self.p.add_item(c) self.p.add_item(d) io.out('') # Game loop self.p.at.describe(who_for=self.p) while True: words = io.send_in() if not self.parser.parse(words): break if self.p.get_hp() <= 0: break
def get_entities(self, who_for, initial=False): """ If verbose is true, we will print information for non-existent stuff. Should be set only for detailed looking. """ entities_except_current = self.entities[:] entities_except_current.remove(who_for) if not entities_except_current: if not initial: io.out('There is no one else in the room.') return False else: output = ["There is a(n)"] for i, thing in enumerate(entities_except_current): output.append(str(thing) + " (" + str(i) + ")") output.append("in this room") io.out(" ".join(output)) return True
def describe(self, who_for, initial=True): if initial: io.out(self.name.upper()) io.out(self.description) io.out("") self.get_entities(initial=initial, who_for=who_for) self.get_items(initial) self.get_directions()
def attack_receive(self, attacker, body_part, damage): if self.hp <= 0: return if self.local_player: io.out('') io.out('{} has dealt {} damage to you on your {}.'.format( attacker, damage, body_part)) clothes = self.equipped[body_part] # Reduce damage by DR percentage. if isinstance(clothes, Clothing): damage = ceil(damage * (100 - clothes.attributes["dr"]) * 0.01) if self.local_player: io.out( 'Thanks to your {}, this has been reduced to {}.'.format( damage)) self.part_damage[body_part] += damage if damage > self.get_hp() and (body_part == 'left hand' or body_part == 'right hand'): self.equipped[body_part] = None if self.local_player: io.out( 'Your hand is crippled. Anything that you held in there has been unequipped.' ) self.hp -= damage self.hp = max(self.hp, 0) if self.hp == 0 and self.local_player: io.out('You have died.') return
def get_directions(self): directions = [ direction for (direction, room) in self.paths.items() if room ] if self.path_text: io.out(self.path_text) elif not self.paths: io.out('It appears there is no way out of this room.') else: io.out("You can go {}.".format(', '.join(directions)))
def use(self,user): if not user.equipped["right hand"] and user.part_damage["right hand"] <= user.get_hp(): user.equipped["right hand"] = self if user.local_player: io.out('You are now wielding a {} in your right hand'.format(self.name)) return True elif not user.equipped["left hand"] and user.part_damage["left hand"] <= user.get_hp(): user.equipped["left hand"] = self if user.local_player: io.out('You are now wielding a {} in your left hand'.format(self.name)) return True else: io.out('You do not have more (intact) hands to hold the {}'.format(self.name)) return False
def parse_use_item(self, words): if len(words) == 0 or words[0] != 'use': return None if len(words) == 1: io.out('USAGE: use [item id]. Get item id from `look`.') return False try: index = int(words[1]) except: io.out('Invalid index specified.') return False try: item = self.game.p.inventory[index] except IndexError: io.out('This is not a valid item.') return False item[0].use(self.game.p) return True
def parse_observe_item(self, words): if len(words) != 3 or (words[0] != 'look' and words[0] != 'inspect' ) or (words[1] != "room" and words[1] != "inventory"): return None try: index = int(words[2]) except: io.out('USAGE: [look/inspect] [room/inventory] [index]') return False target = words[1] if target == "room": try: item = self.game.p.at.items[index] item_obj = item[0] except IndexError: io.out('That item does not exist.') return None item_obj.describe() elif target == "inventory": try: item = self.game.p.inventory[index] item_obj = item[0] except IndexError: io.out('That item does not exist.') return None item_obj.describe() else: return False return True
def get_items(self, initial): if initial: return True if not self.items: io.out('There are no items in the room.') return False else: output = ["You can see"] for i, thing in enumerate(self.items): thing_object = thing[0] thing_amount = thing[1] output.append( str(thing_amount) + " " + thing_object.name + "(" + str(i) + ")") output.append("in this room.") io.out(", ".join(output)) io.out('Use inspect room [id] to inspect an item in this room.') return True
def level_up(self): self.level += 1 if self.local_player: io.out("You are now level {}!".format(self.level))
def show_pep(self): io.out('') io.out('PEP-BOY Mk. 8 Copyright 2018 mpm, inc. All rights reserved.') io.out('') io.out('Name:\t{}\t\tSex:\t{}'.format(self.name, self.sex)) io.out('') io.out('STATISTICS') io.out('LVL:\t{}\tHP:\t{}\tAP:\t{}'.format(self.level, self.get_hp(), self.get_ap())) io.out('STR:\t{}\tPER:\t{}\tEND:\t{}'.format( self.get_stat("strength"), self.get_stat("perception"), self.get_stat("endurance"))) io.out('CHA:\t{}\tINT:\t{}\tAGL:\t{}'.format( self.get_stat("charisma"), self.get_stat("intelligence"), self.get_stat("agility"))) io.out('LCK:\t{}'.format(self.get_stat("luck"))) io.out('') io.out('SKILLS') for skill in self.skills.keys(): io.out('{:<16}{}'.format(skill.capitalize(), self.get_skill(skill))) io.out('') io.out('BODY PART DAMAGE') for bodypart, part_damage in self.part_damage.items(): io.out('{:<16}{:<8}'.format(bodypart.capitalize(), part_damage)) io.out('')
def attack_send(self, victim): if not self.local_player: return if victim.get_hp() <= 0: io.out("You can't fight {}, they are dead.".format(victim)) return False curr_ap = self.get_ap() io.out("YOU ARE NOW FIGHTING {}".format(str(victim).upper())) while True: if self.get_hp() <= 0: return io.out('') io.out('ROUND') io.out( 'Opponent has {} HP. You have {} HP. You have {} action points to use in total.' .format(victim.get_hp(), self.get_hp(), curr_ap)) weapons = self.get_equipped_weapon() unarmed_damage = damage = ceil( self.get_skill("unarmed") / 20 + 0.5) damage = None for i, weapon in enumerate(weapons): if not weapon: continue io.out("[{}] {} (DPA: {}, CRIT damage: {}, AP cost: {}".format( i, weapon, weapon.attributes["dpa"], weapon.attributes["crit_damage"], weapon.attributes["ap_cost"])) io.out( 'Choose a weapon to attack with or type in u to attack unarmed.' ) while True: attack_weapon = io.send_in('! ') if attack_weapon == "u": damage = unarmed_damage io.out( "Fair enough, use your fists. You will be dealing {} damage." .format(damage)) break try: attack_weapon = weapons[int(attack_weapon)] except: io.out('Invalid weapon. Try again.') continue if attack_weapon.attributes["ap_cost"] > curr_ap: io.out( 'You do not have enough action points for this action.' ) continue curr_ap -= attack_weapon.attributes["ap_cost"] damage = attack_weapon.attributes["dpa"] io.out('You are attacking with {}, base damage of {} dealt.'. format(attack_weapon, damage)) crit_chance = ((self.get_stat("luck") * 0.01) * attack_weapon.attributes["crit_chance"]) + 0.15 if random.random() < crit_chance: damage += attack_weapon.attributes["crit_damage"] io.out( "This is a critical hit! You are now dishing out {} damage points." .format(damage)) break body_part = random.choice(victim.body_parts) io.out('You have struck the {} of the {}.'.format( body_part, victim)) victim.attack_receive(self, body_part, damage) if victim.get_hp() <= 0: io.out('You killed {}.'.format(victim)) return victim.attack_send(self)
def parse_player_room_item_interaction(self, words): """ false: pickup true: drop none: invalid """ action = None player = self.game.p room = self.game.p.at if words[0] == "pickup": action = False elif words[0] == "drop": action = True else: return False if len(words) != 2: io.out('USAGE: [pickup/drop] [item id]. Get item id from `look`.') return False try: index = int(words[1]) except: io.out('Invalid index specified.') return False if action: item = player.remove_item(index) if not item: io.out('That is not a valid item.') return False room.add_item(item) io.out('You have successfully dropped {} in {}.'.format( item, room)) if not action: item = room.remove_item(index) if not item: io.out('That is not a valid item.') return False player.add_item(item) io.out('You have successfully picked up {} from {}.'.format( item, room)) return True
def create_character(self): while True: name = io.send_in('By what name should I call you? > ').strip() if name: break while True: sex = io.send_in("Okay, " + name + ", what is your sex? (male, female) > ").strip() if sex == "male" or sex == "female": break p = Human(name, sex, local_player=True) io.out( "Good job, {}. To create your character, assign some skills to your character.\n" .format(name)) """ Here be dragons. """ while True: assignable_points = Entity.MAX_SPECIAL_POINTS total_points = 0 i = 1 out_of = len(p.stats) - 1 for stat in p.stats.keys(): if stat == "level": continue while True: try: points = io.send_in( "[{}/{}] Assign points for {} (default: 5, minimum: 1, maximum: 10 {}/{} assigned). > " .format(i, out_of, stat.upper(), total_points, assignable_points)) points = int(points) except ValueError: io.out("I'll take that as 5.") points = 5 if points < 1 or points > 10: io.out('Points must be between 1 and 10.') continue if total_points == assignable_points and i <= out_of: break if total_points + points > assignable_points: io.out('You cannot assign this many points.') continue p.stats[stat] = points total_points += points i += 1 break if total_points == assignable_points and i <= out_of: io.out( 'You used up your points a bit too fast there. Let us try again....\n' ) break if i > out_of: break # Character stats created, let us put the character into the first available room. p.move_to(self.rooms[0]) return p
def show_inventory(self): if not self.local_player: return io.out('') io.out('YOU ARE CARRYING...') io.out( 'Use `inspect inventory [id]` to inspect an item in your inventory.' ) io.out('Use `use [id]` to use an item in your inventory.') if not self.inventory: io.out('You do not seem to be carrying anything.') for i, item in enumerate(self.inventory): if item: item_obj = item[0] item_amount = item[1] io.out('[{}]\t{}\t\tAMT: {}\t\tWGH: {}'.format( i, item_obj, item_amount, item_obj.weight)) io.out('') io.out('YOU ARE WEARING...') for bodypart, item in self.equipped.items(): if not item: item = "Nothing" io.out('{:<16}{}'.format(bodypart.upper(), item)) io.out('')
def use(self,user): if user.local_player: io.out('You marvel at the magnificence of this item.') return True