Ignore:
Timestamp:
06/27/10 13:52:27 (9 years ago)
Author:
beliar
Message:

Patch by Beliar.

  • The Basic Action now has a commands property. The execute method of the class now loops through this dictionary, parses the values and executes special commands based on these values.
  • Implemented "SetQuestVariable?" command in the Action class. This sets a variable of a quest.
  • Added action property to Usable
  • Added ReadAction? class. For now this prints a name and a text to the console.
  • When right clicking on an item in a container or the pc's inventory the item will be checked for available actions and create a menu from these.
  • Implemented "Read" action to container/inventory items
  • Moved Pamphlet from pc inventory to "DirtyOldCrate?"
  • Added "Grain" object to ObjectDatabase?
  • Gave "Grain" object with ID "bushel_of_grain" to "Farmer Manslow" NPC
  • With these changes it is now possible to advance a bit into the Beer-Quest without using the console. It is still not finishable though.
Location:
trunk/game/scripts/objects
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/game/scripts/objects/action.py

    r563 r568  
    11#!/usr/bin/python 
     2from winioctlcon import UNLOCK_ELEMENT 
    23 
    34#   This file is part of PARPG. 
     
    1617#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>. 
    1718 
     19#exceptions 
     20class NoSuchQuestException(Exception): 
     21    """NoQuestException is used when there is no active quest with the id""" 
     22    pass 
     23 
     24#classes 
     25 
    1826class Action(object): 
    1927    """Base Action class, to define the structure""" 
    20     def execute(self): 
    21         """To be overwritten""" 
    22         pass 
    23  
     28 
     29 
     30    def __init__(self, model, commands = None): 
     31        """Basic action constructor 
     32        @param model: A reference to the game model. 
     33        @type model: scripts.GameModel 
     34        @param commands: Special commands that are executed 
     35        @type commands: Dictionary  
     36        """ 
     37        self.commands = commands or {} 
     38        self.model = model 
     39     
     40    def execute(self): 
     41        """To be overwritten"""         
     42        #Check if there are special commands and execute them 
     43        for command_data in self.commands: 
     44            command = command_data["Command"] 
     45            if command == "SetQuestVariable": 
     46                quest_id = command_data["ID"] 
     47                variable = command_data["Variable"] 
     48                value = command_data["Value"] 
     49                quest_engine = self.model.game_state.quest_engine  
     50                if quest_engine.hasQuest(quest_id): 
     51                    quest_engine[quest_id].setValue(variable, value) 
     52                else: 
     53                    raise NoSuchQuestException 
     54                 
    2455class ChangeMapAction(Action): 
    2556    """A change map scheduled""" 
    26     def __init__(self, model, view, target_map_name, target_pos): 
     57    def __init__(self, model, view, target_map_name, target_pos, commands = None): 
    2758        """Initiates a change of the position of the character 
    28            possibly flagging a new map to be loaded. 
    29            @type model: scripts.GameModel 
    30            @param model: A reference to the game model. 
    31            @type view: class derived from scripts.ViewBase 
    32            @param view: The view 
    33            @type target_map_name: String 
    34            @param target_map_name: Target map id  
    35            @type target_pos: Tuple 
    36            @param target_pos: (X, Y) coordinates on the target map. 
    37            @return: None""" 
    38         Action.__init__(self) 
     59        possibly flagging a new map to be loaded. 
     60        @param model: A reference to the game model. 
     61        @type model: scripts.GameModel 
     62        @param commands: Special commands that are executed 
     63        @type commands: Dictionary  
     64        @type view: class derived from scripts.ViewBase 
     65        @param view: The view 
     66        @type target_map_name: String 
     67        @param target_map_name: Target map id  
     68        @type target_pos: Tuple 
     69        @param target_pos: (X, Y) coordinates on the target map. 
     70        @return: None""" 
     71        Action.__init__(self, model, commands) 
    3972        self.model = model 
    4073        self.view = view 
     
    4679        self.model.changeMap(self.target_map_name, 
    4780                              self.target_pos) 
     81        Action.execute(self) 
    4882        
    4983class OpenBoxAction(Action): 
    5084    """Open a box. Needs to be more generic, but will do for now.""" 
    51     def __init__(self, model, view, container): 
    52         """@type model: Engine reference 
    53            @param model: A reference to the model. 
    54            @type view: class derived from scripts.ViewBase 
    55            @param view: The view 
    56            @param container: A reference to the container""" 
    57         Action.__init__(self) 
    58         self.model = model 
     85    def __init__(self, model, view, container, commands = None): 
     86        """ 
     87        @param model: A reference to the game model. 
     88        @type model: scripts.GameModel 
     89        @param commands: Special commands that are executed 
     90        @type commands: Dictionary  
     91        @type view: class derived from scripts.ViewBase 
     92        @param view: The view 
     93        @param container: A reference to the container 
     94        """ 
     95        Action.__init__(self, model, commands) 
    5996        self.view = view 
    6097        self.container = container 
     
    66103            self.view.hud.createBoxGUI(self.container.name, \ 
    67104                                              self.container) 
     105            Action.execute(self) 
    68106 
    69107        except ValueError: 
     
    73111class UnlockBoxAction(Action): 
    74112    """Unlocks a box. Needs to be more generic, but will do for now.""" 
    75     def __init__(self,  container): 
    76         """@param container: A reference to the container""" 
    77         Action.__init__(self) 
     113    def __init__(self, model, container, commands = None): 
     114        """ 
     115        @param model: A reference to the game model. 
     116        @type model: scripts.GameModel 
     117        @param commands: Special commands that are executed 
     118        @type commands: Dictionary  
     119        @param container: A reference to the container 
     120        """ 
     121        Action.__init__(self, model, commands) 
    78122        self.container = container 
    79123     
     
    81125        """Open the box.""" 
    82126        self.container.unlock() 
     127        Action.execute(self) 
    83128         
    84129class LockBoxAction(Action): 
    85130    """Locks a box. Needs to be more generic, but will do for now.""" 
    86     def __init__(self, container): 
    87         """@param container: A reference to the container""" 
    88         Action.__init__(self) 
     131    def __init__(self, model, container, commands = None): 
     132        """ 
     133        @param model: A reference to the game model. 
     134        @type model: scripts.GameModel 
     135        @param commands: Special commands that are executed 
     136        @type commands: Dictionary  
     137        @param container: A reference to the container 
     138        """ 
     139        Action.__init__(self, model, commands) 
    89140        self.container = container 
    90141         
     
    92143        """Lock the box.""" 
    93144        self.container.lock() 
     145        Action.execute(self) 
    94146 
    95147 
    96148class ExamineBoxAction(Action): 
    97149    """Examine a box. Needs to be more generic, but will do for now.""" 
    98     def __init__(self, model, view, examine_name, examine_desc): 
    99         """@type model: Engine reference 
    100            @param model: A reference to the model. 
    101            @type view: class derived from scripts.ViewBase 
    102            @param view: The view 
    103            @type examine_name: String 
    104            @param examine_name: Name of the object to be examined. 
    105            @type examine_name: String 
    106            @param examine_name: Description of the object to be examined.""" 
    107         Action.__init__(self) 
    108         self.model = model 
     150    def __init__(self, model, view, examine_name, examine_desc, commands = None): 
     151        """ 
     152        @param model: A reference to the game model. 
     153        @type model: scripts.GameModel 
     154        @param commands: Special commands that are executed 
     155        @type commands: Dictionary  
     156        @type view: class derived from scripts.ViewBase 
     157        @param view: The view 
     158        @type examine_name: String 
     159        @param examine_name: Name of the object to be examined. 
     160        @type examine_name: String 
     161        @param examine_name: Description of the object to be examined. 
     162        """ 
     163        Action.__init__(self, model, commands) 
    109164        self.view = view 
    110165        self.examine_name = examine_name 
     
    115170        self.view.hud.createExamineBox(self.examine_name, \ 
    116171                                              self.examine_desc) 
     172        Action.execute(self) 
     173 
     174class ReadAction(Action): 
     175    """Read a text.""" 
     176    def __init__(self, model, view, text_name, text, commands = None): 
     177        """ 
     178        @param model: A reference to the game model. 
     179        @type model: scripts.GameModel 
     180        @param commands: Special commands that are executed 
     181        @type commands: Dictionary  
     182        @param view: The view 
     183        @type view: class derived from scripts.ViewBase 
     184        @param text_name: Name of the object containing the text 
     185        @type text_name: String 
     186        @param text: Text to be displayied 
     187        @type text: String 
     188        """ 
     189        Action.__init__(self, model, commands) 
     190        self.view = view 
     191        self.text_name = text_name 
     192        self.text = text 
     193         
     194    def execute(self): 
     195        """Examine the box.""" 
     196        print "You read " + self.text_name 
     197        print self.text 
     198        #self.view.hud.createExamineBox(self.text_name, \ 
     199        #                                      self.text) 
     200        Action.execute(self) 
    117201 
    118202class TalkAction(Action): 
    119203    """An action to represent starting a dialogue""" 
    120     def __init__(self, model, view, npc): 
    121         """@type model: Engine reference 
    122            @param model: A reference to the model. 
    123            @type view: class derived from scripts.ViewBase 
    124            @param view: The view 
    125            @type npc: NonPlayerCharacter 
    126            @param npc: NPC to interact with.""" 
    127         Action.__init__(self) 
    128         self.model = model 
     204    def __init__(self, model, view, npc, commands = None): 
     205        """ 
     206        @param model: A reference to the game model. 
     207        @type model: scripts.GameModel 
     208        @param commands: Special commands that are executed 
     209        @type commands: Dictionary  
     210        @type view: class derived from scripts.ViewBase 
     211        @param view: The view 
     212        @type npc: NonPlayerCharacter 
     213        @param npc: NPC to interact with. 
     214        """ 
     215        Action.__init__(self, model, commands) 
    129216        self.view = view 
    130217        self.npc = npc 
     
    134221           @return: None""" 
    135222       
    136         pc = self.model.game_state.PlayerCharacter 
     223        player_char = self.model.game_state.PlayerCharacter 
    137224        npc_coordinates = self.npc.getLocation().getLayerCoordinates() 
    138         pc_coordinates = pc.behaviour.agent.getLocation().getLayerCoordinates() 
     225        pc_coordinates = player_char.behaviour.agent.\ 
     226                            getLocation().getLayerCoordinates() 
    139227         
    140228        distance_squared = (npc_coordinates.x - pc_coordinates.x) *\ 
     
    145233        # If we are too far away, we approach the NPC again 
    146234        if distance_squared > 2: 
    147             pc.approach([self.npc.getLocation(). 
     235            player_char.approach([self.npc.getLocation(). 
    148236                         getLayerCoordinates().x, 
    149237                         self.npc.getLocation(). 
     
    153241                                   self.npc))         
    154242        else: 
    155             pc.behaviour.agent.act('stand', self.npc.getLocation()) 
     243            player_char.behaviour.agent.act('stand', self.npc.getLocation()) 
    156244     
    157245            if self.npc.dialogue is not None: 
    158                 self.npc.talk(pc) 
     246                self.npc.talk(player_char) 
    159247                self.view.hud.showDialogue(self.npc) 
    160248            else: 
     
    163251            self.model.game_state.PlayerCharacter.behaviour.idle() 
    164252            self.model.game_state.PlayerCharacter.nextAction = None 
     253            Action.execute(self) 
     254 
     255ACTIONS = {"ChangeMap":ChangeMapAction,  
     256           "OpenBox":OpenBoxAction,  
     257           "Unlock":UnlockBoxAction, 
     258           "Lock":LockBoxAction, 
     259           "Examine":ExamineBoxAction, 
     260           "Read":ReadAction, 
     261           "Talk":TalkAction} 
  • trunk/game/scripts/objects/actors.py

    r563 r568  
    323323        if inventory == None: 
    324324            self.inventory = Inventory() 
    325             self.inventory.placeItem(CarryableItem(ID = 632, 
    326                                                    name = "box", 
    327                                                    image = "/gui/inv_images/inv_giftbox.png")) 
    328  
    329325        else: 
    330326            self.inventory = inventory 
     
    346342        ret_dict = super(NonPlayerCharacter, self).getStateForSaving() 
    347343        ret_dict["Lives"] = self.is_living 
     344        ret_dict["Inventory"] = self.inventory.serializeInventory() 
    348345        ret_dict["State"] = self.behaviour.state 
    349346        return ret_dict 
  • trunk/game/scripts/objects/base.py

    r563 r568  
    358358            if "name" in kwargs and self.items[i].name != kwargs["name"]: 
    359359                continue 
     360            if "ID" in kwargs and self.items[i].ID != kwargs["ID"]: 
     361                continue 
    360362            if "kind" in kwargs and not self.items[i].trueAttr(kwargs["kind"]): 
    361363                continue 
     
    407409    """Allows the object to be used in some way (e.g. a Zippo lighter  
    408410       to make a fire)""" 
    409     def __init__ (self, **kwargs): 
     411    def __init__ (self, actions = None, **kwargs): 
    410412        self.is_usable = True 
     413        self.actions = actions or {}    
    411414         
    412415class Weapon (object): 
  • trunk/game/scripts/objects/composed.py

    r563 r568  
    1919 
    2020from base import GameObject, Container, Lockable, \ 
    21                 Scriptable, Trappable, Destructable, Carryable 
     21                Scriptable, Trappable, Destructable, Carryable, \ 
     22                Usable 
    2223 
    2324class ImmovableContainer(GameObject, Container, Lockable, Scriptable,  
     
    7879        CarryableContainer.placeItem(self, item) 
    7980 
    80 class CarryableItem (GameObject, Carryable): 
     81class CarryableItem (GameObject, Carryable, Usable): 
    8182    """Composite class that will be used for all carryable items""" 
    8283    def __init__(self, **kwargs): 
    8384        GameObject.__init__(self, **kwargs) 
    8485        Carryable.__init__(self, **kwargs) 
     86        Usable.__init__(self, **kwargs) 
    8587 
    8688    def prepareStateForSaving(self, state): 
Note: See TracChangeset for help on using the changeset viewer.