source: trunk/game/scripts/objects/action.py @ 568

Revision 568, 9.8 KB checked in by beliar, 10 years ago (diff)

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.
  • Property svn:eol-style set to native
Line 
1#!/usr/bin/python
2from winioctlcon import UNLOCK_ELEMENT
3
4#   This file is part of PARPG.
5
6#   PARPG is free software: you can redistribute it and/or modify
7#   it under the terms of the GNU General Public License as published by
8#   the Free Software Foundation, either version 3 of the License, or
9#   (at your option) any later version.
10
11#   PARPG is distributed in the hope that it will be useful,
12#   but WITHOUT ANY WARRANTY; without even the implied warranty of
13#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#   GNU General Public License for more details.
15
16#   You should have received a copy of the GNU General Public License
17#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
18
19#exceptions
20class NoSuchQuestException(Exception):
21    """NoQuestException is used when there is no active quest with the id"""
22    pass
23
24#classes
25
26class Action(object):
27    """Base Action class, to define the structure"""
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               
55class ChangeMapAction(Action):
56    """A change map scheduled"""
57    def __init__(self, model, view, target_map_name, target_pos, commands = None):
58        """Initiates a change of the position of the character
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)
72        self.model = model
73        self.view = view
74        self.target_pos = target_pos
75        self.target_map_name = target_map_name
76
77    def execute(self):
78        """Executes the map change."""
79        self.model.changeMap(self.target_map_name,
80                              self.target_pos)
81        Action.execute(self)
82       
83class OpenBoxAction(Action):
84    """Open a box. Needs to be more generic, but will do for now."""
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)
96        self.view = view
97        self.container = container
98       
99    def execute(self):
100        """Open the box."""
101        try:
102            self.container.open()
103            self.view.hud.createBoxGUI(self.container.name, \
104                                              self.container)
105            Action.execute(self)
106
107        except ValueError:
108            self.view.hud.createExamineBox(self.container.name, \
109                                                  "The container is locked")
110       
111class UnlockBoxAction(Action):
112    """Unlocks a box. Needs to be more generic, but will do for now."""
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)
122        self.container = container
123   
124    def execute(self):
125        """Open the box."""
126        self.container.unlock()
127        Action.execute(self)
128       
129class LockBoxAction(Action):
130    """Locks a box. Needs to be more generic, but will do for now."""
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)
140        self.container = container
141       
142    def execute(self):
143        """Lock the box."""
144        self.container.lock()
145        Action.execute(self)
146
147
148class ExamineBoxAction(Action):
149    """Examine a box. Needs to be more generic, but will do for now."""
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)
164        self.view = view
165        self.examine_name = examine_name
166        self.examine_desc = examine_desc
167       
168    def execute(self):
169        """Examine the box."""
170        self.view.hud.createExamineBox(self.examine_name, \
171                                              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)
201
202class TalkAction(Action):
203    """An action to represent starting a dialogue"""
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)
216        self.view = view
217        self.npc = npc
218       
219    def execute(self):
220        """Talk with the NPC when close enough, otherwise move closer.
221           @return: None"""
222     
223        player_char = self.model.game_state.PlayerCharacter
224        npc_coordinates = self.npc.getLocation().getLayerCoordinates()
225        pc_coordinates = player_char.behaviour.agent.\
226                            getLocation().getLayerCoordinates()
227       
228        distance_squared = (npc_coordinates.x - pc_coordinates.x) *\
229                           (npc_coordinates.x - pc_coordinates.x) +\
230                           (npc_coordinates.y - pc_coordinates.y) *\
231                           (npc_coordinates.y - pc_coordinates.y)
232       
233        # If we are too far away, we approach the NPC again
234        if distance_squared > 2:
235            player_char.approach([self.npc.getLocation().
236                         getLayerCoordinates().x,
237                         self.npc.getLocation().
238                         getLayerCoordinates().y], 
239                        TalkAction(self.model,
240                                   self.view,
241                                   self.npc))       
242        else:
243            player_char.behaviour.agent.act('stand', self.npc.getLocation())
244   
245            if self.npc.dialogue is not None:
246                self.npc.talk(player_char)
247                self.view.hud.showDialogue(self.npc)
248            else:
249                self.npc.behaviour.agent.say("Leave me alone!", 1000)
250               
251            self.model.game_state.PlayerCharacter.behaviour.idle()
252            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}
Note: See TracBrowser for help on using the repository browser.