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
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef KYRA_SCRIPT_TIM_H
24 #define KYRA_SCRIPT_TIM_H
25 
26 #include "kyra/kyra_v1.h"
27 
28 #include "common/array.h"
29 #include "common/func.h"
30 #include "common/str.h"
31 
32 namespace Kyra {
33 
34 class WSAMovie_v2;
35 class Screen_v2;
36 class Movie;
37 class LoLEngine;
38 
39 class TimAnimator {
40 public:
41 	struct AnimPart {
42 		uint16 firstFrame;
43 		uint16 lastFrame;
44 		uint16 cycles;
45 		int16 nextPart;
46 		int16 partDelay;
47 		uint16 field_A;
48 		int16 sfxIndex;
49 		uint16 sfxFrame;
50 	};
51 
52 	struct Animation {
53 		Movie *wsa;
54 		int16 x, y;
55 		uint32 nextFrame;
56 		uint8 enable;
57 		uint8 field_D;
58 		uint8 frameDelay;
59 		int8 curPart;
60 		uint8 curFrame;
61 		uint8 cyclesCompleted;
62 		uint16 wsaCopyParams;
63 		int8 lastPart;
64 		AnimPart *parts;
65 	};
66 
67 #ifdef ENABLE_LOL
68 	TimAnimator(LoLEngine *engine, Screen_v2 *screen_v2, OSystem *system, bool useParts);
69 #else
70 	TimAnimator(KyraEngine_v1 *engine, Screen_v2 *screen_v2, OSystem *system, bool useParts);
71 #endif
72 	~TimAnimator();
73 
74 	void init(int animIndex, Movie *wsa, int x, int y, int wsaCopyParams, int frameDelay);
75 	void reset(int animIndex, bool clearStruct);
76 
77 	void displayFrame(int animIndex, int page, int frame, int flags = -1);
78 
getWsaCPtr(int animIndex)79 	const Movie *getWsaCPtr(int animIndex) { return (animIndex >= 0 && animIndex < 6) ? _animations[animIndex].wsa : 0; }
getAnimX(int animIndex)80 	int getAnimX(int animIndex) { return (animIndex >= 0 && animIndex < 6) ? _animations[animIndex].x : 0; }
getAnimY(int animIndex)81 	int getAnimY(int animIndex) { return (animIndex >= 0 && animIndex < 6) ? _animations[animIndex].y : 0; }
82 
83 #ifdef ENABLE_LOL
84 	void setupPart(int animIndex, int part, int firstFrame, int lastFrame, int cycles, int nextPart, int partDelay, int f, int sfxIndex, int sfxFrame);
85 	void start(int animIndex, int part);
86 	void stop(int animIndex);
87 	void update(int animIndex);
88 	void playPart(int animIndex, int firstFrame, int lastFrame, int delay);
89 	int resetLastPart(int animIndex);
90 #endif
91 
92 private:
93 #ifdef ENABLE_LOL
94 	LoLEngine *_vm;
95 #else
96 	KyraEngine_v1 *_vm;
97 #endif
98 	Screen_v2 *_screen;
99 	OSystem *_system;
100 
101 	Animation *_animations;
102 
103 	const bool _useParts;
104 };
105 
106 struct TIM;
107 typedef Common::Functor2<const TIM *, const uint16 *, int> TIMOpcode;
108 
109 struct TIM {
110 	char filename[13];
111 
112 	uint16 clickedButton;
113 	int16 dlgFunc;
114 
115 	int16 procFunc;
116 	uint16 procParam;
117 
118 	enum {
119 		kCountFuncs = 10
120 	};
121 
122 	struct Function {
123 		uint16 *ip;
124 
125 		uint32 lastTime;
126 		uint32 nextTime;
127 
128 		uint16 *loopIp;
129 
130 		uint16 *avtl;
131 	} func[kCountFuncs];
132 
133 	enum {
134 		kWSASlots = 6,
135 		kAnimParts = 10
136 	};
137 
138 	struct WSASlot {
139 		int anim;
140 
141 		int16 x, y;
142 		uint16 wsaFlags;
143 		uint16 offscreen;
144 	} wsa[kWSASlots];
145 
146 	uint16 *avtl;
147 	uint8 *text;
148 
149 	const Common::Array<const TIMOpcode *> *opcodes;
150 
151 	// TODO: Get rid of this ugly HACK to allow the
152 	// Lands of Lore outro to be working properly.
153 	bool isLoLOutro;
154 	uint8 lolCharacter;
155 };
156 
157 class TIMInterpreter {
158 public:
159 	TIMInterpreter(KyraEngine_v1 *engine, Screen_v2 *screen_v2, OSystem *system);
160 	virtual ~TIMInterpreter();
161 
162 	TIM *load(const char *filename, const Common::Array<const TIMOpcode *> *opcodes);
163 	void unload(TIM *&tim) const;
164 
165 	bool callback(Common::IFFChunk &chunk);
166 
167 	virtual int initAnimStruct(int index, const char *filename, int x, int y, int, int offscreenBuffer, uint16 wsaFlags);
168 	virtual int freeAnimStruct(int index);
animator()169 	TimAnimator *animator() { return _animator; }
170 
171 	void setLangData(const char *filename);
clearLangData()172 	void clearLangData() { delete[] _langData; _langData = 0; }
173 
174 	const char *getCTableEntry(uint idx) const;
175 
resetFinishedFlag()176 	void resetFinishedFlag() { _finished = false; }
finished()177 	bool finished() const { return _finished; }
178 
179 	int exec(TIM *tim, bool loop);
stopCurFunc()180 	void stopCurFunc() { if (_currentTim) cmd_stopCurFunc(0); }
181 	void stopAllFuncs(TIM *tim);
182 
183 	void refreshTimersAfterPause(uint32 elapsedTime);
184 
185 	void displayText(uint16 textId, int16 flags);
186 	void displayText(uint16 textId, int16 flags, uint8 color);
187 	void setupTextPalette(uint index, int fadePalette);
188 
resetDialogueState(TIM * tim)189 	virtual void resetDialogueState(TIM *tim) {}
190 
191 	int _drawPage2;
192 
193 	int _palDelayInc, _palDiff, _palDelayAcc;
194 	int _abortFlag;
195 
196 protected:
197 	KyraEngine_v1 *_vm;
198 	Screen_v2 *_screen;
199 	OSystem *_system;
200 
201 	TIM *_currentTim;
202 	int _currentFunc;
203 
204 	TimAnimator *_animator;
205 
206 	bool _finished;
207 
208 	// used when loading
209 	int _avtlChunkSize;
210 	const char *_filename;
211 	TIM *_tim;
212 
213 	Common::String _vocFiles[120];
214 
update()215 	virtual void update() {}
checkSpeechProgress()216 	virtual void checkSpeechProgress() {}
217 
218 	char _audioFilename[32];
219 
220 	uint8 *_langData;
221 	char *getTableEntry(uint idx);
222 	bool _textDisplayed;
223 	uint8 *_textAreaBuffer;
224 
225 	virtual int execCommand(int cmd, const uint16 *param);
226 
227 	typedef int (TIMInterpreter::*CommandProc)(const uint16 *);
228 	struct CommandEntry {
229 		CommandProc proc;
230 		const char *desc;
231 	};
232 
233 	const CommandEntry *_commands;
234 	int _commandsSize;
235 
236 	int cmd_initFunc0(const uint16 *param);
237 	int cmd_stopCurFunc(const uint16 *param);
238 	int cmd_initWSA(const uint16 *param);
239 	int cmd_uninitWSA(const uint16 *param);
240 	int cmd_initFunc(const uint16 *param);
241 	int cmd_stopFunc(const uint16 *param);
242 	int cmd_wsaDisplayFrame(const uint16 *param);
243 	int cmd_displayText(const uint16 *param);
244 	int cmd_loadVocFile(const uint16 *param);
245 	int cmd_unloadVocFile(const uint16 *param);
246 	int cmd_playVocFile(const uint16 *param);
247 	int cmd_loadSoundFile(const uint16 *param);
248 	int cmd_playMusicTrack(const uint16 *param);
249 	virtual int cmd_setLoopIp(const uint16 *param);
250 	virtual int cmd_continueLoop(const uint16 *param);
251 	int cmd_resetLoopIp(const uint16 *param);
252 	int cmd_resetAllRuntimes(const uint16 *param);
253 	int cmd_execOpcode(const uint16 *param);
254 	int cmd_initFuncNow(const uint16 *param);
255 	int cmd_stopFuncNow(const uint16 *param);
256 #define cmd_return(n, v) \
257 	int cmd_return_##n(const uint16 *){ return v; }
258 	cmd_return( 1,  1)
259 	cmd_return(n1, -1)
260 #undef cmd_return
261 };
262 
263 #ifdef ENABLE_LOL
264 class LoLEngine;
265 class Screen_LoL;
266 class TIMInterpreter_LoL : public TIMInterpreter {
267 public:
268 	TIMInterpreter_LoL(LoLEngine *engine, Screen_v2 *screen_v2, OSystem *system);
269 
270 	int initAnimStruct(int index, const char *filename, int x, int y, int frameDelay, int, uint16 wsaCopyParams) override;
271 	int freeAnimStruct(int index) override;
272 
273 	void resetDialogueState(TIM *tim) override;
274 
275 private:
276 	void update() override;
277 	void checkSpeechProgress() override;
278 
279 	char *getTableString(int id);
280 	void advanceToOpcode(int opcode);
281 
282 	LoLEngine *_vm;
283 	Screen_LoL *_screen;
284 
285 	int execCommand(int cmd, const uint16 *param) override;
286 
287 	typedef int (TIMInterpreter_LoL::*CommandProc)(const uint16 *);
288 	struct CommandEntry {
289 		CommandProc proc;
290 		const char *desc;
291 	};
292 
293 	const CommandEntry *_commands;
294 	int _commandsSize;
295 
296 	int cmd_stopAllFuncs(const uint16 *param);
297 	int cmd_setLoopIp(const uint16 *param) override;
298 	int cmd_continueLoop(const uint16 *param) override;
299 	int cmd_processDialogue(const uint16 *param);
300 	int cmd_dialogueBox(const uint16 *param);
301 };
302 #endif // ENABLE_LOL
303 
304 } // End of namespace Kyra
305 
306 #endif
307