def make_item(*args): item_to_make = args[0] component_search_dict = game_state.terminal_search_dict recipes={ "gun":(), "hat":("Cloth","Feather"), "pants":("Leather","Zipper"), "first_aid_kit":(), } results={ "gun":("11mm_handgun",), "hat":("colorful_hat",), "pants":("cargo_pants",), "first_aid_kit":("first_aid_kit",) } if item_to_make in recipes: necessary_components = recipes[item_to_make] missing_components = list(necessary_components[:]) consumed_components = [] for component in component_search_dict.values(): if isinstance(component.object, Item) and component.object.name in missing_components: missing_components.remove(component.object.name) consumed_components.append(component) if len(missing_components) > 0: message_center.add_message(channel="fail",message="Not enough components to make item {0}, missing: {1}".format(item_to_make, ", ".join(missing_components))) else: message_center.add_message(channel="default",message="Making item...".format(item_to_make)) for component in consumed_components: game_state.current_state.destroy_marker(component.identifier) for product in results[item_to_make]: create_object("container", {"identifier":str(get_unique_id()), "template":product}, (), game_state.current_state.get_focus_identifier())
def generate_description(self): message_center.add_message(channel="heading",message="Your Inventory") message_center.add_message(channel="default", message="These are the items you have found in your mission so far.") message_center.add_message(channel="subheading",message="You are carrying...") id_list = game_state.current_state.get_children_of(self.identifier) if len(id_list) > 0: message_center.add_message(channel="item", message="\n".join([game_state.current_state.get_object(ident).name for ident in id_list])) else: message_center.add_message(channel="item", message="Nothing at all.")
def generate_description(self): message_center.add_message(channel="heading",message=self.name) if self.description != None: message_center.add_message(channel="default", message=" \n" + self.description + "\n") else: message_center.add_message(channel="default", message=" \n") sub_markers = game_state.current_state.get_children_of(self.identifier) if len(sub_markers)> 0: message_center.add_message(channel="subheading",message="Looking inside, you find...") message_center.add_message(channel="item", message="\n".join(sub_markers))
def release_held_item(self): if self.held_item == None: message_center.add_message(channel="fail", message="{0} is not holding anything.".format(self.name)) return None released_item = self.held_item game_state.current_state.move_marker(released_item.identifier, None) self.held_item = None message_center.add_message(channel="default", message="{0} is now empty.".format(self.name, self.held_item.name)) return released_item
def generate_description(self): lines = [] message_center.add_message(channel="heading",message=self.name) if self.description != None: message_center.add_message(channel="default", message=" \n" + self.description + "\n") else: message_center.add_message(channel="default", message=" \n") message_center.add_message(channel="subheading",message="Looking around, you find...") message_center.add_message(channel="item", message="\n".join([o.name for o in [game_state.current_state.get_object(identifier) for identifier in game_state.current_state.get_children_of(self.identifier)]])) return lines
def load_from_file(self, filename): save_file = open(filename,'r') GameState.__init__(self) self.key_set = pickle.load(save_file) self.marker_dict = pickle.load(save_file) self.template_dict = pickle.load(save_file) self.focus_stack = pickle.load(save_file) save_file.close() message_center.add_message("="*10 + "\nGame loaded!\n") if len(self.focus_stack) > 0: self.focus_stack[-1].on_focus() self.__refresh_focus_info()
def save_to_file(self, filename): """Saves the entire GameState instance to a file using the standard pickle module. It is possible that a game could attach things to the state in such a way that they would not be saved correctly. It is also possible that attaching certain things to the state will cause saving to crash the program. This behaviour is poorly tested at the time of this writing.""" save_file = open(filename,'w') pickle.dump(self.key_set, save_file) pickle.dump(self.marker_dict, save_file) pickle.dump(self.template_dict, save_file) pickle.dump(self.focus_stack, save_file) save_file.close() message_center.add_message("\nGame saved!\n")
def take_damage(self, amount, verbed="hit"): global damage_message_template sub_dict = {"name":self.name,"verbed":verbed} self.cur_hp -= amount if self.cur_hp >= 0: sub_dict["number"] = amount sub_dict["hp_string"] = self.generate_hp_string() else: overkill = 0 - self.cur_hp real_damage = amount-overkill self.cur_hp = 0 sub_dict["number"] = real_damage sub_dict["hp_string"] = self.generate_hp_string() # message_center.add_message(channel="default",message="%s was #{f57900}#overkilled#{XXXXXX}#, wasting #{f57900}#%i#{XXXXXX}# damage!" % (self.colored_name, overkill)) message_center.add_message(channel="default",message=damage_message_template.substitute(sub_dict))
def be_healed(self, amount, verbed="hit"): global healing_message_template sub_dict = {"name":self.name,"verbed":verbed} self.cur_hp += amount if self.cur_hp <= self.max_hp: sub_dict["number"] = amount sub_dict["hp_string"] = self.generate_hp_string() else: overheal = self.cur_hp - self.max_hp real_heal = amount-overheal self.cur_hp = self.max_hp sub_dict["number"] = real_heal sub_dict["hp_string"] = self.generate_hp_string() # message_center.add_message(channel="default",message="%s was #{f57900}#overhealed#{XXXXXX}#, wasting #{FFFF99}#%i#{XXXXXX}# healing!" % (self.colored_name, overheal)) message_center.add_message(channel="default",message=healing_message_template.substitute(sub_dict))
def try_to_hold_item(self, item): if not isinstance(item, Item): item = game_state.current_state.get_object(item) if self.held_item != None: message_center.add_message(channel="fail", message="{0} is already holding {1}.".format(self.name, self.held_item.name)) return False if hasattr(item, "size") and item.size > self.max_held_size: message_center.add_message(channel="fail", message="{0} is too large for {1} to hold.".format(item.name, self.name)) return False if hasattr(item, "weight") and item.weight > self.max_held_weight: message_center.add_message(channel="fail", message="{0} is too heavy for {1} to hold.".format(item.name, self.name)) return False self.held_item = item game_state.current_state.move_marker(item.identifier, self.identifier) message_center.add_message(channel="default", message="{0} is now holding {1}.".format(self.name, self.held_item.name)) return True
def on_text_update(self): command_dict = self.command_dict current_line = self.get_current_line() cmd_strings = self.parse_command_strings_from_line(current_line) self.helper_text = "" if self.current_line_is_complete(): self.clear_current_line() message_center.add_message(" \n>>> " + current_line + "\n") if len(cmd_strings) == 0: # User has entered blank command return command_string = cmd_strings.pop(0) try: if command_string not in command_dict: raise StandardCommandError("Unrecognized Command: " + command_string) command_object = command_dict[command_string] if len(cmd_strings) > 0 and cmd_strings[-1] == "": cmd_strings.pop() if len(command_object.arg_handlers) != len(cmd_strings): raise StandardCommandError( "Wrong number of arguments for %s. Try 'help %s' for more information." % (command_string, command_string) ) arg_objects = [] for arg_string, arg_handler in zip(cmd_strings, command_object.arg_handlers): try: arg_objects.append(arg_handler.get_target_for_arg_string(arg_string)) except ArgumentError, e: message_center.add_message("Invalid argument: %s " % str(e), "terminal_error") arg_objects.append(None) if None in arg_objects: raise StandardCommandError( "Could not parse arguments; see previous errors. Try 'help %s' for command help." % command_string ) ### If we made it this far, we have a valid command object which accepts the argument objects we have retreived. command_object.execute(*arg_objects) except StandardCommandError as e: message_center.add_message(str(e), "terminal_error")
def get_that_item(*args): target_item_marker = args[0] hand = game_state.current_state.get_object("right_hand") success = hand.try_to_hold_item(target_item_marker.object) if not success: message_center.add_message(channel="default", message="You leave {0} where it is for now.".format(target_item_marker.object.name))
def get_target_for_arg_string(self, arg_string): if arg_string in self.choices: return arg_string else: message_center.add_message(channel="error", message="'%s' is not a valid choice here." % arg_string) return None
def current_markers(*args): for key in game_state.terminal_search_dict.keys(): message_center.add_message(str(key))
def current_keys(*args): for key in game_state.current_state.key_set.dict.keys(): message_center.add_message(str(key))
def display_player_status(*args): the_player_marker = game_state.current_state.get_marker_for_identifier("the_player") po = the_player_marker.object message_center.add_message(channel="default",message="Current HP: %s" % po.generate_hp_string())
def describe(*args): for x in args: if hasattr(x,'generate_description'): x.generate_description() else: message_center.add_message(channel="failure", message="You can't actually see %s." % x.name)
def generate_description(self): if self.description == None: message_center.add_message(str(self.name).capitalize() + " is rather difficult to describe.") else: message_center.add_message(channel="heading",message=self.name) message_center.add_message(" \n" + self.description)