# HG changeset patch # User KarstenBock@gmx.net # Date 1318163942 -7200 # Node ID 371b17bc911371126debeeddcffe1bcfa6a521a1 # Parent 87073af1d2d2da7820fb433222e173e33a5ed2ec Added ReplaceItemAction, to dialogueactions, which replaces a single item in the players inventory. diff -r 87073af1d2d2 -r 371b17bc9113 src/parpg/dialogueactions.py --- a/src/parpg/dialogueactions.py Sun Oct 09 14:38:54 2011 +0200 +++ b/src/parpg/dialogueactions.py Sun Oct 09 14:39:02 2011 +0200 @@ -153,7 +153,8 @@ item = container.take_item(game_state['npc'].container, item_type) if (item): container.put_item(game_state['pc'].container, item) - print("{0} gave you the {1}".format(game_state['npc'].description.view_name, + print("{0} gave you the {1}".format(game_state['npc']. + description.view_name, item_type)) else: print("{0} doesn't have the {1}".format(game_state['npc']. @@ -190,6 +191,39 @@ DialogueAction.registerAction(GiveStuffAction) +class ReplaceItemAction(InventoryAction): + """ + L{InventoryAction} used to replace an item with another in the player's + inventory. + """ + + keyword = 'replace_item' + + def __call__(self, game_state): + """ + Take an item from the player and place another at its place. + + @param game_state: variables and functions that make up the current + game state. + @type game_state: dict of objects + """ + old_type = self.item_types[0] + new_type = self.item_types[1] + item = container.take_item(game_state['pc'].container, old_type) + if item: + model = game_state['model'] + new_item = model.createItemByType(new_type, new_type, + item.entity.world) + container.put_item(game_state['pc'].container, + new_item.containable, item.slot) + model.deleteObject(item.entity.general.identifier) + item.delete() + print("{0} took the {1} and gave you the {2}".format( + game_state['npc'].description.view_name, old_type, item_type)) + else: + print("You don't have the {0}".format(old_type)) +DialogueAction.registerAction(ReplaceItemAction) + class QuestAction(DialogueAction): """ Abstract base class for quest-related L{DialogueActions}.