ScummVM API documentation
script.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  * Based on the original sources
22  * Faery Tale II -- The Halls of the Dead
23  * (c) 1993-1996 The Wyrmkeep Entertainment Co.
24  */
25 
26 #ifndef SAGA2_SCRIPT_H
27 #define SAGA2_SCRIPT_H
28 
29 #include "saga2/objects.h"
30 #include "saga2/calendar.h"
31 
32 namespace Saga2 {
33 
34 typedef int16 ThreadID;
35 
36 // Various result codes returned from runScript
37 
38 enum scriptResult {
39 
40  // Code returned when attempt to run a non-existent script
41  kScriptResultNoScript = 0,
42 
43  // Code returned when script was aborted before completion
44  kScriptResultAborted,
45 
46  // Code returned when script finished
47  kScriptResultFinished,
48 
49  // Script spun off as async thread; no answer available.
50  kScriptResultAsync
51 };
52 
53 // Variables specific to a thread
54 
56 
57  // ID of object who's method is being called (which can be the same
58  // as one of the other objects below).
59  ObjectID invokedObject = Nothing;
60  ActiveItemID invokedTAI;
61 
62  // ID of the objects in the interaction.
63  ObjectID enactor = Nothing, // actor who caused interaction
64  directObject = Nothing, // the object being acted on
65  indirectObject = Nothing; // the object being used
66  // with the other one
67  ActiveItemID directTAI, // the tile activity instance
68  // being used
69  indirectTAI; // the tile activity instance
70  // upon which the object is being
71  // used
72 
73  int16 responseType = -1; // used with knowledge package
74 
75  int16 methodNum = -1; // which method being invoked
76 
77  // Misc fields used in passing parameters to scripts.
78  int16 idNum = 0; // a misc. id number
79  int16 value = 0; // a misc. parameter value
80  TilePoint coords; // a misc. tilepoint
81 
82  int16 returnVal = 0; // return value of script
83 };
84 
85 // Standard return codes from scripts in the "returnVal" field
86 
87 enum {
88  // Code returned by script when script decides requested
89  // action is not possible, and the calling C-code should
90  // take action to inform user
91  kActionResultFailure = 0,
92 
93  // Code returned by script when script completes the action
94  // successfully and C-code should not complete the action
95  kActionResultSuccess,
96 
97  // Code returned by script when requested action should complete
98  // the action
99  kActionResultNotDone
100 };
101 
102 // Method used to refer to a SAGA object
103 
104 struct SegmentRef {
105  uint16 segment; // segment / resource number
106  uint16 offset; // offset within segment
107 };
108 
109 // Segment numbers of "builtin" SAGA data structures,
110 // such as actors and TAGS
111 
112 enum builtinTypes {
113  kBuiltinTypeObject = -1,
114  kBuiltinTypeTAG = -2,
115  kBuiltinAbstract = -3,
116  kBuiltinTypeMission = -4
117 };
118 
119 /* ===================================================================== *
120  SAGA management functions
121  * ===================================================================== */
122 
123 // Load the SAGA data segment from the resource file
124 void initSAGADataSeg();
125 
126 void saveSAGADataSeg(Common::OutSaveFile *outS);
127 void loadSAGADataSeg(Common::InSaveFile *in);
128 
129 // Dispose of the SAGA data segment -- do nothing
130 inline void cleanupSAGADataSeg() {}
131 
132 /* ===================================================================== *
133  Thread management functions
134  * ===================================================================== */
135 
136 class Thread;
137 
138 // Initialize the SAGA thread list
139 void initSAGAThreads();
140 
141 void saveSAGAThreads(Common::OutSaveFile *outS);
142 void loadSAGAThreads(Common::InSaveFile *in, int32 chunkSize);
143 
144 // Dispose of the active SAGA threads
145 void cleanupSAGAThreads();
146 
147 // Dispose of an active SAGA thread
148 void deleteThread(Thread *p);
149 
150 void newThread(Thread *p, ThreadID id);
151 
152 void newThread(Thread *p);
153 
154 // Return the ID of the specified SAGA thread
155 ThreadID getThreadID(Thread *thread);
156 
157 // Return a pointer to a SAGA thread, given a thread ID
158 Thread *getThreadAddress(ThreadID id);
159 
160 /* ===================================================================== *
161  Class Thread: An execution context of a script
162  * ===================================================================== */
163 
164 // A script task is called a thread
165 
166 scriptResult runMethod(
167  uint16 scriptClassID, // which script class
168  int16 bType, // builtin type
169  uint16 index, // object index
170  uint16 methodNum, // method number to call
171  scriptCallFrame &args);
172 
173 class Thread {
174 
175  friend char *STRING(int strNum);
176 
177  friend scriptResult runScript(uint16 exportEntryNum, scriptCallFrame &args);
178 
179  friend void wakeUpThread(ThreadID, int16);
180 
181 public:
182  SegmentRef _programCounter; // current PC location
183 
184  uint8 *_stackPtr; // current stack location
185  byte *_codeSeg; // base of current data segment
186 // *_stringBase; // base of string resource
187 
188  uint8 *_stackBase; // base of module stack
189 
190  enum threadFlags {
191  kTFWaiting = (1 << 0), // thread waiting for event
192  kTFFinished = (1 << 1), // thread finished normally
193  kTFAborted = (1 << 2), // thread is aborted
194  kTFExtended = (1 << 3), // this is an extended sequence
195  kTFExpectResult = (1 << 4), // script is expecting result on stack
196  kTFSynchronous = (1 << 5), // when this bit is set this thread will
197  // run until it is finished or this bit
198  // is cleared
199 
200  kTFAsleep = (kTFWaiting | kTFFinished | kTFAborted)
201  };
202 
203  int16 _stackSize, // allocated size of stack
204  _flags, // execution flags
205  _framePtr, // pointer to call frame
206  _returnVal; // return value from ccalls
207 
208  bool _valid;
209 
210  // Various signals that a script can wait upon
211  enum WaitTypes {
212  kWaitNone = 0, // waiting for nothing
213  kWaitDelay, // waiting for a timer
214  kWaitFrameDelay, // waiting for frame count
215  kWaitOther, // waiting for to be awoken
216  kWaitTagSemaphore // waiting for a tag semaphore
217 
218 // kWaitSpeech, // waiting for speech to finish
219 // kWaitDialogEnd, // waiting for my dialog to finish
220 // kWaitDialogBegin, // waiting for other dialog to finish
221 // kWaitWalk, // waiting to finish walking
222 // kWaitRequest, // a request is up
223  };
224 
225  WaitTypes _waitType; // what we're waiting for
226  union {
227  Alarm _waitAlarm; // for time-delay
228  FrameAlarm _waitFrameAlarm; // for frame count delay
229  ActiveItem *_waitParam; // for other waiting
230  };
231 
232  scriptCallFrame _threadArgs; // arguments from C to thread
233 
234  // For 'cfunc' member functions, the address of the object who's
235  // member function is being invoked.
236  void *_thisObject;
237  uint16 _argCount; // number of args to cfunc
238 
239  // Constructor
240  Thread(uint16 segNum, uint16 segOff, scriptCallFrame &args);
241 
242  // Constructor -- reconstruct from archive buffer
243  Thread(void **buf);
244 
245  Thread(Common::SeekableReadStream *stream, ThreadID id);
246 
247  // Destructor
248  ~Thread();
249 
250  // Return the number of bytes need to archive this thread in an
251  // arhive buffer
252  int32 archiveSize();
253 
254  // Create an archive of this thread in an archive buffer
255  void *archive(void *buf);
256 
257  void write(Common::MemoryWriteStreamDynamic *out);
258 
259  // Dispatch all asynchronous threads
260  static void dispatch();
261 
262  // Intepret a single thread
263  scriptResult run();
264 
265  // Tells thread to wait for an event
266  void waitForEvent(WaitTypes wt, ActiveItem *param) {
267  _flags |= kTFWaiting;
268  _waitType = wt;
269  _waitParam = param;
270  }
271 
272  // Convert to extended script, and back to synchonous script
273  void setExtended();
274  void clearExtended();
275 
276  bool interpret();
277 private:
278  uint8 *strAddress(int strNum);
279 
280 };
281 
282 const int maxTimeSlice = 16, // max instructions per call
283  kStackSize = 512; // thread stack size
284 
285 /* ============================================================================ *
286  C-Function dispatch table
287  * ============================================================================ */
288 
289 typedef int16 C_Call(int16 *);
290 
291 struct CallTable {
292  C_Call **table;
293  uint16 numEntries;
294  uint16 classID;
295 };
296 
297 extern CallTable globalCFuncs,
298  actorCFuncs,
299  tagCFuncs,
300  missionCFuncs;
301 
302 //extern C_Call *ccall_table[];
303 //extern int16 ccall_count;
304 
305 /* ===================================================================== *
306  Externals
307  * ===================================================================== */
308 
309 extern Thread *thisThread; // task queue
310 
311 // Thread control
312 //void killThread( Thread *th );
313 
314 /*
315 void wakeUpActorThread(WaitTypes wakeupType, void *obj);
316 void wakeUpThreads(WaitTypes wakeupType);
317 void wakeUpThreadsDelayed(WaitTypes wakeupType, int newdelay);
318 void abortObjectThreads(Thread *keep, uint16 id);
319 bool abortAllThreads(void);
320 */
321 
322 // Run a script function
323 scriptResult runScript(uint16 exportEntryNum, scriptCallFrame &args);
324 
325 // Run a script class method.
326 scriptResult runObjectMethod(
327  ObjectID id, uint16 methodNum, scriptCallFrame &args);
328 
329 scriptResult runTagMethod(
330  uint16 tagNum, uint16 methodNum, scriptCallFrame &args);
331 
332 
334  int16 deadActorProto,
335  reserved[2];
336  int16 EXP_spellEffect_CreateFireWisp,
337  EXP_spellEffect_CreateWindWisp,
338  EXP_spellEffect_CreateWraith,
339  EXP_spellEffect_TeleportToShrine,
340  EXP_spellEffect_Rejoin,
341  EXP_spellEffect_Timequake,
342  EXP_spellEffect_CreateFood;
343 };
344 
345 extern ResImportTable *resImports;
346 
347 } // end of namespace Saga2
348 
349 #endif
Definition: tile.h:388
Definition: savefile.h:54
Definition: script.h:173
Definition: actor.h:32
Definition: idtypes.h:124
Definition: script.h:55
Definition: memstream.h:194
Definition: tcoords.h:127
Definition: stream.h:745
Definition: script.h:291
Definition: script.h:333
Definition: script.h:104
Definition: calendar.h:86
Definition: fta.h:132