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 SCUMM_HE_INTERN_HE_H
24 #define SCUMM_HE_INTERN_HE_H
25 
26 #include "scumm/scumm_v6.h"
27 #ifdef ENABLE_HE
28 #include "scumm/he/floodfill_he.h"
29 #include "scumm/he/wiz_he.h"
30 #endif
31 #include "scumm/actor_he.h"	// For AuxBlock & AuxEntry
32 
33 namespace Common {
34 class SeekableReadStream;
35 class WriteStream;
36 }
37 
38 namespace Scumm {
39 
40 class ActorHE;
41 class ResExtractor;
42 #ifdef ENABLE_HE
43 class LogicHE;
44 class MoviePlayer;
45 class Sprite;
46 class CUP_Player;
47 #endif
48 
49 class ScummEngine_v60he : public ScummEngine_v6 {
50 protected:
51 
52 public:
53 	Common::SeekableReadStream *_hInFileTable[17];
54 	Common::WriteStream *_hOutFileTable[17];
55 
56 	Common::Rect _actorClipOverride;	// HE specific
57 
58 	int _heTimers[16];
59 	uint32 _pauseStartTime;
60 
61 	int getHETimer(int timer);
62 	void setHETimer(int timer);
63 	void pauseHETimers(bool pause);
64 
65 public:
66 	ScummEngine_v60he(OSystem *syst, const DetectorResult &dr);
67 	~ScummEngine_v60he();
68 
69 	virtual Common::String generateFilename(const int room) const;
70 
71 	virtual void resetScumm();
72 
73 protected:
74 	virtual void setupOpcodes();
75 
76 	virtual void saveLoadWithSerializer(Common::Serializer &s);
77 
78 	void localizeArray(int slot, byte scriptSlot);
79 	void redimArray(int arrayId, int newX, int newY, int d);
80 	int readFileToArray(int slot, int32 size);
81 	void writeFileFromArray(int slot, int resID);
82 
83 	int virtScreenSave(byte *dst, int x1, int y1, int x2, int y2);
84 	void virtScreenLoad(int resIdx, int x1, int y1, int x2, int y2);
85 
86 	virtual void decodeParseString(int a, int b);
87 	void swapObjects(int object1, int object2);
88 
89 	Common::String convertFilePath(const byte *src);
90 	Common::String convertSavePath(const byte *src);
91 	Common::String convertSavePathOld(const byte *src);
92 
93 	Common::SeekableReadStream *openFileForReading(const byte *fileName);
94 	Common::SeekableReadStream *openSaveFileForReading(const byte *fileName);
95 	Common::WriteStream *openSaveFileForWriting(const byte *fileName);
96 	Common::WriteStream *openSaveFileForAppending(const byte *fileName);
97 	void deleteSaveFile(const byte *fileName);
98 	void renameSaveFile(const byte *from, const byte *to);
99 	void pauseEngineIntern(bool pause);
100 
101 	Common::SeekableReadStream *openSaveFileForReading(int slot, bool compat, Common::String &fileName);
102 	Common::WriteStream *openSaveFileForWriting(int slot, bool compat, Common::String &fileName);
103 
104 	/* HE version 60 script opcodes */
105 	void o60_setState();
106 	void o60_roomOps();
107 	void o60_actorOps();
108 	void o60_kernelSetFunctions();
109 	void o60_kernelGetFunctions();
110 	void o60_openFile();
111 	void o60_closeFile();
112 	void o60_deleteFile();
113 	void o60_readFile();
114 	void o60_rename();
115 	void o60_writeFile();
116 	void o60_soundOps();
117 	void o60_seekFilePos();
118 	void o60_localizeArrayToScript();
119 	void o60_redimArray();
120 	void o60_readFilePos();
121 };
122 
123 class ScummEngine_v70he : public ScummEngine_v60he {
124 	friend class ResExtractor;
125 
126 protected:
127 	enum HESndFlags {
128 		HE_SND_LOOP = 1,
129 		HE_SND_APPEND = 2,
130 		HE_SND_SOFT_SOUND = 4,
131 		HE_SND_QUICK_START = 8,
132 		HE_SND_OFFSET = 16,
133 		HE_SND_VOL = 32,
134 		HE_SND_FREQUENCY = 64,
135 		HE_SND_PAN = 128
136 	};
137 
138 	ResExtractor *_resExtractor;
139 
140 	byte *_heV7DiskOffsets;
141 	byte *_heV7RoomOffsets;
142 	uint32 *_heV7RoomIntOffsets;
143 
144 	int32 _heSndSoundId, _heSndOffset, _heSndChannel, _heSndFlags, _heSndSoundFreq, _heSndPan, _heSndVol;
145 
146 	int _numStoredFlObjects;
147 	ObjectData *_storedFlObjects;
148 
149 public:
150 	ScummEngine_v70he(OSystem *syst, const DetectorResult &dr);
151 	~ScummEngine_v70he();
152 
153 	virtual Common::String generateFilename(const int room) const;
154 
155 	void restoreBackgroundHE(Common::Rect rect, int dirtybit = 0);
156 
157 protected:
158 	virtual void allocateArrays();
159 	virtual int readResTypeList(ResType type);
160 	virtual uint32 getResourceRoomOffset(ResType type, ResId idx);
161 	virtual void setupOpcodes();
162 
163 	virtual void setupScummVars();
164 	virtual void resetScummVars();
165 
166 	virtual void saveLoadWithSerializer(Common::Serializer &s);
167 
168 	virtual void readRoomsOffsets();
169 	virtual void readGlobalObjects();
170 	virtual void readIndexBlock(uint32 blocktype, uint32 itemsize);
171 
172 	virtual void clearRoomObjects();
173 	virtual void resetRoomObjects();
174 
175 	virtual int getActorFromPos(int x, int y);
176 
177 	virtual void loadFlObject(uint object, uint room);
178 	void storeFlObject(int slot);
179 	void restoreFlObjects();
180 
181 	virtual void setCursorFromImg(uint img, uint room, uint imgindex);
182 	virtual void setDefaultCursor();
183 
184 	/* HE version 70 script opcodes */
185 	void o70_soundOps();
186 	void o70_pickupObject();
187 	void o70_getActorRoom();
188 	void o70_resourceRoutines();
189 	void o70_systemOps();
190 	void o70_getStringLen();
191 	void o70_isResourceLoaded();
192 	void o70_readINI();
193 	void o70_writeINI();
194 	void o70_createDirectory();
195 	void o70_setSystemMessage();
196 
197 	byte VAR_NUM_SOUND_CHANNELS;
198 };
199 
200 #ifdef ENABLE_HE
201 class Moonbase;
202 
203 class ScummEngine_v71he : public ScummEngine_v70he {
204 	friend class Wiz;
205 	friend class Moonbase;
206 
207 protected:
208 	bool _skipProcessActors;
209 
210 public:
211 	ScummEngine_v71he(OSystem *syst, const DetectorResult &dr);
212 	~ScummEngine_v71he();
213 
214 	byte *heFindResourceData(uint32 tag, byte *ptr);
215 	byte *heFindResource(uint32 tag, byte *ptr);
216 	byte *findWrappedBlock(uint32 tag, byte *ptr, int state, bool flagError);
217 
218 	Wiz *_wiz;
219 
220 	virtual int setupStringArray(int size);
221 
222 protected:
223 	virtual void setupOpcodes();
224 
225 	virtual void saveLoadWithSerializer(Common::Serializer &s);
226 
227 	virtual void redrawBGAreas();
228 
229 	virtual void processActors();
230 	void preProcessAuxQueue();
231 	void postProcessAuxQueue();
232 
233 	virtual void clearDrawQueues();
234 
235 	int getStringCharWidth(byte chr);
236 	void appendSubstring(int dst, int src, int len2, int len);
237 	void adjustRect(Common::Rect &rect);
238 
239 	/* HE version 71 script opcodes */
240 	void o71_kernelSetFunctions();
241 	void o71_copyString();
242 	void o71_getStringWidth();
243 	void o71_appendString();
244 	void o71_concatString();
245 	void o71_compareString();
246 	void o71_getStringLenForWidth();
247 	void o71_getCharIndexInString();
248 	void o71_findBox();
249 	void o71_polygonOps();
250 	void o71_polygonHit();
251 
252 	byte VAR_WIZ_TCOLOR;
253 public:
254 	/* Actor AuxQueue stuff (HE) */
255 	AuxBlock _auxBlocks[16];
256 	uint16 _auxBlocksNum;
257 	AuxEntry _auxEntries[16];
258 	uint16 _auxEntriesNum;
259 
260 	void queueAuxBlock(ActorHE *a);
261 	void queueAuxEntry(int actorNum, int subIndex);
262 
263 	void remapHEPalette(const uint8 *src, uint8 *dst);
264 };
265 
266 class ScummEngine_v72he : public ScummEngine_v71he {
267 protected:
268 
269 #include "common/pack-start.h"	// START STRUCT PACKING
270 
271 	struct ArrayHeader {
272 		int32 type;      //0
273 		int32 dim1start; //4
274 		int32 dim1end;   //8
275 		int32 dim2start; //0C
276 		int32 dim2end;   //10
277 		byte data[1];    //14
278 	} PACKED_STRUCT;
279 
280 #include "common/pack-end.h"	// END STRUCT PACKING
281 
282 	int _stringLength;
283 	byte _stringBuffer[4096];
284 
285 	WizParameters _wizParams;
286 
287 public:
288 	ScummEngine_v72he(OSystem *syst, const DetectorResult &dr);
289 
290 	virtual void resetScumm();
291 
292 	virtual byte *getStringAddress(ResId idx);
293 	virtual int setupStringArray(int size);
294 	virtual int setupStringArrayFromString(const char *cStr);
295 	virtual void getStringFromArray(int arrayNumber, char *buffer, int maxLength);
296 
297 protected:
298 	virtual void setupOpcodes();
299 
300 	virtual void setupScummVars();
301 	virtual void resetScummVars();
302 	virtual void readArrayFromIndexFile();
303 
304 	virtual void readMAXS(int blockSize);
305 
306 	virtual void redrawBGAreas();
307 	virtual void checkExecVerbs();
308 
309 	byte *defineArray(int array, int type, int dim2start, int dim2end, int dim1start, int dim1end);
310 	virtual int readArray(int array, int idx2, int idx1);
311 	virtual void writeArray(int array, int idx2, int idx1, int value);
312 	void redimArray(int arrayId, int newDim2start, int newDim2end,
313 					int newDim1start, int newDim1end, int type);
314 	void checkArrayLimits(int array, int dim2start, int dim2end, int dim1start, int dim1end);
315 	void copyArray(int array1, int a1_dim2start, int a1_dim2end, int a1_dim1start, int a1_dim1end,
316 					int array2, int a2_dim2start, int a2_dim2end, int a2_dim1start, int a2_dim1end);
317 	void copyArrayHelper(ArrayHeader *ah, int idx2, int idx1, int len1, byte **data, int *size, int *num);
318 	int readFileToArray(int slot, int32 size);
319 	void writeFileFromArray(int slot, int32 resID);
320 
321 	virtual void decodeParseString(int a, int b);
322 	void decodeScriptString(byte *dst, bool scriptString = false);
323 	void copyScriptString(byte *dst, int dstSize);
324 
325 	int findObject(int x, int y, int num, int *args);
326 	int getSoundResourceSize(ResId idx);
327 
328 	virtual bool handleNextCharsetCode(Actor *a, int *c);
329 	virtual int convertMessageToString(const byte *msg, byte *dst, int dstSize);
330 
331 	void debugInput(byte *string);
332 
333 	/* HE version 72 script opcodes */
334 	void o72_pushDWord();
335 	void o72_getScriptString();
336 	void o72_isAnyOf();
337 	void o72_resetCutscene();
338 	void o72_findObjectWithClassOf();
339 	void o72_getObjectImageX();
340 	void o72_getObjectImageY();
341 	void o72_captureWizImage();
342 	void o72_getTimer();
343 	void o72_setTimer();
344 	void o72_getSoundPosition();
345 	void o72_startScript();
346 	void o72_startObject();
347 	void o72_drawObject();
348 	void o72_printWizImage();
349 	void o72_getArrayDimSize();
350 	void o72_getNumFreeArrays();
351 	void o72_roomOps();
352 	void o72_actorOps();
353 	void o72_verbOps();
354 	void o72_findObject();
355 	void o72_arrayOps();
356 	void o72_systemOps();
357 	void o72_talkActor();
358 	void o72_talkEgo();
359 	void o72_dimArray();
360 	void o72_dim2dimArray();
361 	void o72_traceStatus();
362 	void o72_debugInput();
363 	void o72_drawWizImage();
364 	void o72_kernelGetFunctions();
365 	void o72_jumpToScript();
366 	void o72_openFile();
367 	void o72_readFile();
368 	void o72_writeFile();
369 	void o72_findAllObjects();
370 	void o72_deleteFile();
371 	void o72_rename();
372 	void o72_getPixel();
373 	void o72_pickVarRandom();
374 	void o72_redimArray();
375 	void o72_readINI();
376 	void o72_writeINI();
377 	void o72_getResourceSize();
378 	void o72_createDirectory();
379 	void o72_setSystemMessage();
380 
381 	byte VAR_NUM_ROOMS;
382 	byte VAR_NUM_SCRIPTS;
383 	byte VAR_NUM_SOUNDS;
384 	byte VAR_NUM_COSTUMES;
385 	byte VAR_NUM_IMAGES;
386 	byte VAR_NUM_CHARSETS;
387 
388 	byte VAR_SOUND_ENABLED;
389 
390 	byte VAR_POLYGONS_ONLY;
391 
392 	byte VAR_MOUSE_STATE;			// Used in checkExecVerbs();
393 	byte VAR_PLATFORM;
394 };
395 
396 class ScummEngine_v80he : public ScummEngine_v72he {
397 protected:
398 	int32 _heSndResId, _curSndId, _sndPtrOffs, _sndTmrOffs, _sndDataSize;
399 
400 public:
401 	ScummEngine_v80he(OSystem *syst, const DetectorResult &dr);
402 
403 protected:
404 	virtual void setupOpcodes();
405 
406 	virtual void setupScummVars();
407 	virtual void resetScummVars();
408 
409 	virtual void parseEvent(Common::Event event);
410 
411 	virtual void initCharset(int charset);
412 
413 	virtual void clearDrawQueues();
414 
415 	void createSound(int snd1id, int snd2id);
416 
417 	void drawLine(int x1, int y1, int x, int unk1, int unk2, int type, int id);
418 	void drawPixel(int x, int y, int flags);
419 
420 	virtual void setDefaultCursor();
421 
422 	/* HE version 80 script opcodes */
423 	void o80_createSound();
424 	void o80_getFileSize();
425 	void o80_stringToInt();
426 	void o80_getSoundVar();
427 	void o80_localizeArrayToRoom();
428 	void o80_sourceDebug();
429 	void o80_readConfigFile();
430 	void o80_writeConfigFile();
431 	void o80_cursorCommand();
432 	void o80_setState();
433 	void o80_drawWizPolygon();
434 	void o80_drawLine();
435 	void o80_pickVarRandom();
436 
437 	byte VAR_PLATFORM_VERSION;
438 	byte VAR_CURRENT_CHARSET;
439 	byte VAR_KEY_STATE;
440 	byte VAR_COLOR_DEPTH;
441 };
442 
443 class ScummEngine_v90he : public ScummEngine_v80he {
444 	friend class LogicHE;
445 	friend class Moonbase;
446 	friend class MoviePlayer;
447 	friend class Sprite;
448 
449 protected:
450 	FloodFillParameters _floodFillParams;
451 
452 	struct VideoParameters {
453 		byte filename[260];
454 		int32 status;
455 		int32 flags;
456 		int32 number;
457 		int32 wizResNum;
458 	};
459 
460 	VideoParameters _videoParams;
461 
462 	int32 _heObject, _heObjectNum;
463 	int32 _hePaletteNum;
464 
465 	int32 _curMaxSpriteId;
466 	int32 _curSpriteId;
467 	int32 _curSpriteGroupId;
468 
469 	LogicHE *_logicHE;
470 	MoviePlayer *_moviePlay;
471 	Sprite *_sprite;
472 
473 public:
474 	ScummEngine_v90he(OSystem *syst, const DetectorResult &dr);
475 	~ScummEngine_v90he();
476 
477 protected:
478 	virtual void allocateArrays();
479 	virtual void setupOpcodes();
480 
481 	virtual void resetScumm();
482 
483 	virtual void setupScummVars();
484 	virtual void resetScummVars();
485 
486 	virtual void scummLoop(int delta);
487 	virtual void scummLoop_handleDrawing();
488 	virtual void runBootscript();
489 
490 	virtual void processInput();
491 	virtual void clearClickedStatus();
492 
493 	virtual void saveLoadWithSerializer(Common::Serializer &s);
494 
495 	virtual void readMAXS(int blockSize);
496 	void setResourceOffHeap(int typeId, int resId, int val);
497 
498 	virtual void processActors();
499 
500 	int computeWizHistogram(int resnum, int state, int x, int y, int w, int h);
501 	void getArrayDim(int array, int *dim2start, int *dim2end, int *dim1start, int *dim1end);
502 	void sortArray(int array, int dim2start, int dim2end, int dim1start, int dim1end, int sortOrder);
503 
504 public:
505 	int getGroupSpriteArray(int spriteGroupId);
506 
507 protected:
508 	uint8 *getHEPaletteIndex(int palSlot);
509 	int getHEPaletteColor(int palSlot, int color);
510 	int getHEPaletteSimilarColor(int palSlot, int red, int green, int start, int end);
511 	int getHEPalette16BitColorComponent(int component, int type);
512 	int getHEPaletteColorComponent(int palSlot, int color, int component);
513 	void setHEPaletteColor(int palSlot, uint8 color, uint8 r, uint8 g, uint8 b);
514 	void setHEPaletteFromPtr(int palSlot, const uint8 *palData);
515 	void setHEPaletteFromCostume(int palSlot, int resId);
516 	void setHEPaletteFromImage(int palSlot, int resId, int state);
517 	void setHEPaletteFromRoom(int palSlot, int resId, int state);
518 	void restoreHEPalette(int palSlot);
519 	void copyHEPalette(int dstPalSlot, int srcPalSlot);
520 	void copyHEPaletteColor(int palSlot, uint8 dstColor, uint16 srcColor);
521 
522 protected:
523 	/* HE version 90 script opcodes */
524 	void o90_dup_n();
525 	void o90_min();
526 	void o90_max();
527 	void o90_sin();
528 	void o90_cos();
529 	void o90_sqrt();
530 	void o90_atan2();
531 	void o90_getSegmentAngle();
532 	void o90_getActorData();
533 	void o90_startScriptUnk();
534 	void o90_jumpToScriptUnk();
535 	void o90_videoOps();
536 	void o90_getVideoData();
537 	void o90_wizImageOps();
538 	void o90_getDistanceBetweenPoints();
539 	void o90_getSpriteInfo();
540 	void o90_setSpriteInfo();
541 	void o90_getSpriteGroupInfo();
542 	void o90_setSpriteGroupInfo();
543 	void o90_getWizData();
544 	void o90_floodFill();
545 	void o90_mod();
546 	void o90_shl();
547 	void o90_shr();
548 	void o90_xor();
549 	void o90_findAllObjectsWithClassOf();
550 	void o90_getPolygonOverlap();
551 	void o90_cond();
552 	void o90_dim2dim2Array();
553 	void o90_redim2dimArray();
554 	void o90_getLinesIntersectionPoint();
555 	void o90_sortArray();
556 	void o90_getObjectData();
557 	void o90_getPaletteData();
558 	void o90_paletteOps();
559 	void o90_fontUnk();
560 	void o90_getActorAnimProgress();
561 	void o90_kernelGetFunctions();
562 	void o90_kernelSetFunctions();
563 
564 	byte VAR_NUM_SPRITE_GROUPS;
565 	byte VAR_NUM_SPRITES;
566 	byte VAR_NUM_PALETTES;
567 	byte VAR_NUM_UNK;
568 
569 	byte VAR_U32_VERSION;
570 	byte VAR_U32_ARRAY_UNK;
571 };
572 
573 class ScummEngine_v99he : public ScummEngine_v90he {
574 public:
ScummEngine_v99he(OSystem * syst,const DetectorResult & dr)575 	ScummEngine_v99he(OSystem *syst, const DetectorResult &dr) : ScummEngine_v90he(syst, dr) {}
576 
577 	virtual void resetScumm();
578 
579 protected:
580 	virtual void resetScummVars();
581 
582 	virtual void readMAXS(int blockSize);
583 
584 	virtual void saveLoadWithSerializer(Common::Serializer &s);
585 
586 	virtual void copyPalColor(int dst, int src);
587 	virtual void darkenPalette(int redScale, int greenScale, int blueScale, int startColor, int endColor);
588 	virtual void setPaletteFromPtr(const byte *ptr, int numcolor = -1);
589 	virtual void setPalColor(int index, int r, int g, int b);
590 	virtual void updatePalette();
591 };
592 
593 class ScummEngine_v100he : public ScummEngine_v99he {
594 friend class AI;
595 
596 protected:
597 	ResType _heResType;
598 	int32 _heResId;
599 
600 	byte _debugInputBuffer[256];
601 
602 public:
603 	Moonbase *_moonbase;
604 
605 public:
606 	ScummEngine_v100he(OSystem *syst, const DetectorResult &dr);
607 	~ScummEngine_v100he();
608 
609 	virtual void resetScumm();
610 
611 	virtual void setupScummVars();
612 
613 protected:
614 	virtual void setupOpcodes();
615 
616 	virtual void saveLoadWithSerializer(Common::Serializer &s);
617 
618 	virtual void decodeParseString(int a, int b);
619 
620 	/* HE version 100 script opcodes */
621 	void o100_actorOps();
622 	void o100_arrayOps();
623 	void o100_dim2dimArray();
624 	void o100_redim2dimArray();
625 	void o100_dimArray();
626 	void o100_drawLine();
627 	void o100_drawObject();
628 	void o100_floodFill();
629 	void o100_setSpriteGroupInfo();
630 	void o100_resourceRoutines();
631 	void o100_wizImageOps();
632 	void o100_jumpToScript();
633 	void o100_createSound();
634 	void o100_dim2dim2Array();
635 	void o100_paletteOps();
636 	void o100_jumpToScriptUnk();
637 	void o100_startScriptUnk();
638 	void o100_redimArray();
639 	void o100_roomOps();
640 	void o100_setSystemMessage();
641 	void o100_soundOps();
642 	void o100_setSpriteInfo();
643 	void o100_startScript();
644 	void o100_systemOps();
645 	void o100_cursorCommand();
646 	void o100_videoOps();
647 	void o100_wait();
648 	void o100_writeFile();
649 	void o100_debugInput();
650 	void o100_isResourceLoaded();
651 	void o100_getResourceSize();
652 	void o100_getSpriteGroupInfo();
653 	void o100_getPaletteData();
654 	void o100_readFile();
655 	void o100_getSpriteInfo();
656 	void o100_getWizData();
657 	void o100_getVideoData();
658 
659 protected:
660 	byte VAR_U32_USER_VAR_A;
661 	byte VAR_U32_USER_VAR_B;
662 	byte VAR_U32_USER_VAR_C;
663 	byte VAR_U32_USER_VAR_D;
664 	byte VAR_U32_USER_VAR_E;
665 	byte VAR_U32_USER_VAR_F;
666 };
667 
668 class ScummEngine_vCUPhe : public Engine {
669 protected:
670 	CUP_Player *_cupPlayer;
671 	FilenamePattern _filenamePattern;
672 
673 public:
674 	ScummEngine_vCUPhe(OSystem *syst, const DetectorResult &dr);
675 	~ScummEngine_vCUPhe();
676 
677 	virtual Common::Error run();
678 
679 	void parseEvents();
680 
681 	OSystem *_syst;
682 
683 	GameSettings _game;
684 };
685 
686 #endif
687 
688 
689 } // End of namespace Scumm
690 
691 #endif
692