ScummVM API documentation
encounters.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef ASYLUM_RESOURCES_ENCOUNTERS_H
23 #define ASYLUM_RESOURCES_ENCOUNTERS_H
24 
25 #include "common/array.h"
26 #include "common/serializer.h"
27 
28 #include "asylum/eventhandler.h"
29 #include "asylum/shared.h"
30 
31 namespace Asylum {
32 
33 class AsylumEngine;
34 
36  uint32 speechResourceId;
37  ResourceId scriptResourceId;
38  int16 keywords[50];
39  byte variable2;
40 
41  EncounterItem() {
42  speechResourceId = 0;
43  scriptResourceId = kResourceNone;
44  memset(&keywords, 0, sizeof(keywords));
45  variable2 = 0;
46  }
47 
48  virtual ~EncounterItem() {}
49 
50  // Serializable
51  void saveLoadWithSerializer(Common::Serializer &s) {
52  s.syncAsSint32LE(speechResourceId);
53  s.syncAsSint32LE(scriptResourceId);
54 
55  for (int32 i = 0; i < ARRAYSIZE(keywords); i++)
56  s.syncAsSint16LE(keywords[i]);
57 
58  s.syncAsByte(variable2);
59  }
60 };
61 
62 class EncounterVariables : public Common::Array<int16>, public Common::Serializable {
63 public:
64  virtual ~EncounterVariables() {}
65 
66  // Serializable
67  void saveLoadWithSerializer(Common::Serializer &s) {
68  for (uint i = 0; i < _size; i++)
69  s.syncAsSint16LE(_storage[i]);
70  }
71 };
72 
73 class EncounterItems : public Common::Array<EncounterItem>, public Common::Serializable {
74 public:
75  virtual ~EncounterItems() {}
76 
77  // Serializable
78  void saveLoadWithSerializer(Common::Serializer &s) {
79  for (uint i = 0; i < _size; i++)
80  _storage[i].saveLoadWithSerializer(s);
81  }
82 };
83 
84 class Encounter : public EventHandler {
85 public:
86  Encounter(AsylumEngine *engine);
87  virtual ~Encounter() {};
88 
89  void run(int32 encounterIndex, ObjectId objectId1, ObjectId objectId2, ActorIndex actorIndex);
90 
91  bool handleEvent(const AsylumEvent &evt);
92 
93  void drawScreen();
94 
95  void setShouldEnablePlayer(bool state) { _shouldEnablePlayer = state; }
96  bool shouldEnablePlayer() { return _shouldEnablePlayer; }
97 
98  // Accessors (for saving game)
99  EncounterItems *items() { return &_items; }
100  EncounterVariables *variables() { return &_variables; }
101 
102 private:
103  AsylumEngine *_vm;
104 
106  // Data
107  enum KeywordOptions {
108  kKeywordOptionsDisabled = 0x20,
109  kKeywordOptionsUnknown = 0x40,
110  kKeywordOptionsVisible = 0x80
111  };
112 
113  struct EncounterGraphic {
114  uint32 frameIndex;
115  uint32 frameCount;
116  Common::Rect rect;
117  ResourceId resourceId;
118  int32 transTableNum;
119  int32 transTableMax;
120  int32 speech0;
121  int32 speech1;
122  int32 speech2;
123  int32 speech3;
124 
125  EncounterGraphic() {
126  frameIndex = 0;
127  frameCount = 0;
128  resourceId = kResourceNone;
129  transTableNum = 0;
130  transTableMax = 0;
131  speech0 = 0;
132  speech1 = 0;
133  speech2 = 0;
134  speech3 = 0;
135  }
136  };
137 
138  struct EncounterDrawingStruct {
139  Common::Point point1;
140  Common::Point point2;
141  uint32 frameIndex;
142  int32 transTableNum;
143  int32 status;
144  ResourceId resourceId;
145 
146  EncounterDrawingStruct() {
147  frameIndex = 0;
148  transTableNum = -1;
149  status = 0;
150  resourceId = kResourceNone;
151  }
152  };
153 
154  EncounterVariables _variables;
155  EncounterItems _items;
156  EncounterDrawingStruct _drawingStructs[2];
157  int32 _keywordIndexes[50];
158 
159  // Background & portrait
160  EncounterGraphic _background;
161  EncounterGraphic _portrait1;
162  EncounterGraphic _portrait2;
163  Common::Point _point;
164 
165  int32 _rectIndex;
166 
167  // Running encounter data
168  int32 _index;
169  int32 _speechResourceId;
170  ResourceId _soundResourceId;
171  EncounterItem *_item;
172  ObjectId _objectId1;
173  ObjectId _objectId2;
174  ObjectId _objectId3;
175  ActorIndex _actorIndex;
176 
177  int16 _value1;
178  uint32 _tick;
179 
180  // Internal data
181  int32 _data_455B14;
182  int16 _data_455B3C;
183  int16 _data_455B70;
184  bool _data_455BCC;
185  bool _isDialogOpen;
186  bool _shouldCloseDialog;
187  bool _data_455BD8;
188  bool _data_455BDC;
189  bool _data_455BE0;
190  bool _shouldCloseBackground;
191  bool _data_455BE8;
192  int16 _data_455BF0;
193  uint32 _data_455BF4;
194  uint32 _keywordStartIndex;
195  uint32 _keywordsOffset;
196 
197  // Internal flags
198  bool _shouldEnablePlayer;
199  bool _wasPlayerDisabled;
200  bool _isClosing;
201  bool _isScriptRunning;
202 
204  // Data
205  void load();
206  void initData();
207  void initBackground();
208  void initPortraits();
209  void initDrawStructs();
210 
212  // Message handling
213  bool init();
214  bool update();
215  bool mouse(const AsylumEvent &evt);
216  bool cancel(const AsylumEvent &evt);
217 
219  // Variables
220  void setVariable(uint32 index, int16 val);
221  int16 getVariable(uint32 index);
222  int16 getVariableInv(int16 index);
223 
225  // Actions
226  uint32 findKeyword(EncounterItem *item, int16 keyword) const;
227  int32 getKeywordIndex();
228  void choose(int32 keywordIndex);
229  bool checkKeywords() const;
230  bool checkKeywords2() const;
231  void updateFromRect(int32 rectIndex);
232 
234  // Speech
235  void resetSpeech(int16 a1, int16 a2);
236  void setupPortraits();
237  void setupSpeechText();
238  void setupSpeechData(char val, EncounterGraphic *encounterGraphic) const;
239  void setupSpeech(ResourceId textResourceId, ResourceId fontResourceId);
240  bool setupSpeechTest(ResourceId id);
241  bool isSpeaking();
242 
244  // Drawing
245  bool drawBackground();
246  bool drawPortraits();
247  void drawStructs();
248  void drawDialogOptions();
249  void drawSubtitle(char *text, ResourceId font, int16 y);
250 
252  // Misc
253  void exitEncounter();
254  void setupEntities(bool type4);
255  int32 findRect();
256  void updateDrawingStatus();
257  void updateDrawingStatus1(int32 rectIndex);
258  void updateDrawingStatus2(int32 rectIndex);
259  bool updateScreen();
260  void updatePalette1();
261  void updatePalette2();
262 
263  bool isKeywordVisible(int16 keyword) const { return (bool)(BYTE1(keyword) & kKeywordOptionsVisible); }
264  bool isKeywordDisabled(int16 keyword) const { return (bool)(BYTE1(keyword) & kKeywordOptionsDisabled); }
265 
267  // Scripts
268  enum EncounterOpcode {
269  kOpcodeEncounterReturn = 0,
270  kOpcodeSetScriptVariable = 1,
271  kOpcodeSetCounterFromVars = 2,
272  kOpcodeSetOffset = 3,
273  kOpcodeSetOffsetIfCounterNegative = 4,
274  kOpcodeSetOffsetIfCounterNegativeOrNull = 5,
275  kOpcodeSetOffsetIfCounterIsNull = 6,
276  kOpcodeSetOffsetIfCounterIsNotNull = 7,
277  kOpcodeSetOffsetIfCounterPositiveOrNull = 8,
278  kOpcodeSetOffsetIfCounterPositive = 9,
279  kOpcodeSetCurrentItemOptions = 10,
280  kOpcodeClearCurrentItemOptions = 11,
281  kOpcodeSetItemOptions = 12,
282  kOpcodeCloseDialog = 13,
283  kOpcodeResetSpeech = 14,
284  kOpcodeSetVariable = 15,
285  kOpcodeIncrementScriptVariable = 16,
286  kOpcodeProcessVariable3 = 17,
287  kOpcodeAddRemoveInventoryItem = 18,
288  kOpcodeSetCounterIfInventoryOmits = 21,
289  kOpcodePrepareMovie = 23,
290  kOpcodeSetClearGameFlag = 24,
291  kOpcodeSetCounterFromGameFlag = 25
292  };
293 
294  struct ScriptEntry {
295  byte opcode;
296  byte param1;
297  uint16 param2;
298 
299  ScriptEntry(byte *data) {
300  opcode = *data;
301  param1 = *(data + 1);
302  param2 = READ_LE_UINT16(data + 2);
303  }
304 
305  Common::String toString();
306  };
307 
308  struct ScriptData {
309  int32 vars[40];
310  uint32 offset;
311  int32 counter;
312  ResourceId resourceId;
313 
314  ScriptData() {
315  reset(kResourceNone);
316  }
317 
318  void reset(ResourceId id) {
319  memset(&vars, 0, sizeof(vars));
320  offset = 0;
321  counter = 0;
322  resourceId = id;
323  }
324  };
325 
326  ScriptData _scriptData;
327 
328  void initScript(ResourceId resourceId);
329  ScriptEntry getScriptEntry(ResourceId resourceId, uint32 offset);
330  void runScript();
331 
332  friend class Console;
333 };
334 
335 } // end of namespace Asylum
336 
337 #endif // ASYLUM_RESOURCES_ENCOUNTERS_H
#define ARRAYSIZE(x)
Definition: util.h:91
Definition: encounters.h:35
Definition: encounters.h:84
Definition: str.h:59
Definition: array.h:52
Definition: asylum.h:53
Definition: rect.h:144
Definition: encounters.h:73
Definition: serializer.h:79
Definition: eventhandler.h:43
Definition: eventhandler.h:61
Definition: rect.h:45
Definition: asylum.h:73
Definition: console.h:38
Definition: serializer.h:308
Definition: encounters.h:62