ScummVM API documentation
dm.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 /*
23  * Based on the Reverse Engineering work of Christophe Fontanel,
24  * maintainer of the Dungeon Master Encyclopaedia (http://dmweb.free.fr/)
25  */
26 
27 #ifndef DM_DM_H
28 #define DM_DM_H
29 
30 #include "engines/engine.h"
31 #include "engines/savestate.h"
32 
33 #include "common/random.h"
34 #include "common/savefile.h"
35 #include "common/str.h"
36 #include "common/memstream.h"
37 
38 #include "advancedDetector.h"
39 
40 #include "dm/console.h"
41 #include "dm/detection.h"
42 
43 namespace DM {
44 
45 class DisplayMan;
46 class DungeonMan;
47 class EventManager;
48 class MenuMan;
49 class ChampionMan;
50 class ObjectMan;
51 class InventoryMan;
52 class TextMan;
53 class MovesensMan;
54 class GroupMan;
55 class Timeline;
56 class ProjExpl;
57 class DialogMan;
58 class SoundMan;
59 
60 enum DMActions {
61  kActionNone,
62  kActionToggleInventoryChampion0,
63  kActionToggleInventoryChampion1,
64  kActionToggleInventoryChampion2,
65  kActionToggleInventoryChampion3,
66  kActionSave,
67  kActionFreezeGame,
68  kActionTurnLeft,
69  kActionMoveForward,
70  kActionTurnRight,
71  kActionMoveLeft,
72  kActionMoveBackward,
73  kActionMoveRight,
74  kActionWakeUp,
75  kActionSelectChoice,
76 };
77 
78 enum Direction {
79  kDMDirNorth = 0,
80  kDMDirEast = 1,
81  kDMDirSouth = 2,
82  kDMDirWest = 3
83 };
84 
85 enum ThingType {
86  kDMThingTypeParty = -1, // @ CM1_THING_TYPE_PARTY
87  kDMThingTypeDoor = 0, // @ C00_THING_TYPE_DOOR
88  kDMThingTypeTeleporter = 1, // @ C01_THING_TYPE_TELEPORTER
89  kDMstringTypeText = 2, // @ C02_THING_TYPE_TEXTSTRING
90  kDMThingTypeSensor = 3, // @ C03_THING_TYPE_SENSOR
91  kDMThingTypeGroup = 4, // @ C04_THING_TYPE_GROUP
92  kDMThingTypeWeapon = 5, // @ C05_THING_TYPE_WEAPON
93  kDMThingTypeArmour = 6, // @ C06_THING_TYPE_ARMOUR
94  kDMThingTypeScroll = 7, // @ C07_THING_TYPE_SCROLL
95  kDMThingTypePotion = 8, // @ C08_THING_TYPE_POTION
96  kDMThingTypeContainer = 9, // @ C09_THING_TYPE_CONTAINER
97  kDMThingTypeJunk = 10, // @ C10_THING_TYPE_JUNK
98  kDMThingTypeProjectile = 14, // @ C14_THING_TYPE_PROJECTILE
99  kDMThingTypeExplosion = 15, // @ C15_THING_TYPE_EXPLOSION
100  kDMThingTypeTotal = 16 // +1 than the last (explosionThingType)
101 }; // @ C[00..15]_THING_TYPE_...
102 
103 enum Cell {
104  kDMCellAny = -1, // @ CM1_CELL_ANY
105  kDMCellNorthWest = 0, // @ C00_CELL_NORTHWEST
106  kDMCellNorthEast = 1, // @ C01_CELL_NORTHEAST
107  kDMCellSouthEast = 2, // @ C02_CELL_SOUTHEAST
108  kDMCellSouthWest = 3 // @ C03_CELL_SOUTHWEST
109 };
110 
111 enum GameMode {
112  kDMModeLoadSavedGame = 0, // @ C000_MODE_LOAD_SAVED_GAME
113  kDMModeLoadDungeon = 1, // @ C001_MODE_LOAD_DUNGEON
114  kDMModeWaitingOnEntrance = 99, // @ C099_MODE_WAITING_ON_ENTRANCE
115  kDMModeEntranceDrawCredits = 202 // @ C202_MODE_ENTRANCE_DRAW_CREDITS
116 };
117 
118 enum LoadgameResult {
119  kDMLoadgameFailure = -1, // @ CM1_LOAD_GAME_FAILURE
120  kDMLoadgameSuccess = 1// @ C01_LOAD_GAME_SUCCESS
121 };
122 
123 enum MapIndice {
124  kDMMapIndexNone = -1, // @ CM1_MAP_INDEX_NONE
125  kDMMapIndexEntrance = 255 // @ C255_MAP_INDEX_ENTRANCE
126 };
127 
128 #define kDMMaskDecodeEvenIfInvisible 0x8000 // @ MASK0x8000_DECODE_EVEN_IF_INVISIBLE
129 #define kDMMaskMergeCycles 0x8000 // @ MASK0x8000_MERGE_CYCLES
130 
131 #define kDMSlotBoxInventoryFirstSlot 8 // @ C08_SLOT_BOX_INVENTORY_FIRST_SLOT
132 #define kDMSlotBoxInventoryActionHand 9 // @ C09_SLOT_BOX_INVENTORY_ACTION_HAND
133 #define kDMSlotBoxChestFirstSlot 38 // @ C38_SLOT_BOX_CHEST_FIRST_SLOT
134 
135 class Thing {
136 public:
137  uint16 _data;
138 
139  Thing() : _data(0) {}
140  explicit Thing(uint16 d) { set(d); }
141 
142  void set(uint16 d) {
143  _data = d;
144  }
145 
146  byte getCell() const { return _data >> 14; }
147  ThingType getType() const { return (ThingType)((_data >> 10) & 0xF); }
148  uint16 getIndex() const { return _data & 0x3FF; }
149 
150  void setCell(uint16 cell) { _data = (_data & ~(0x3 << 14)) | ((cell & 0x3) << 14); }
151  void setType(uint16 type) { _data = (_data & ~(0xF << 10)) | ((type & 0xF) << 10); }
152  void setIndex(uint16 index) { _data = (_data & ~0x3FF) | (index & 0x3FF); }
153 
154  uint16 getTypeAndIndex() { return _data & 0x3FFF; }
155  uint16 toUint16() const { return _data; } // I don't like 'em cast operators
156  bool operator==(const Thing &rhs) const { return _data == rhs._data; }
157  bool operator!=(const Thing &rhs) const { return _data != rhs._data; }
158 }; // @ THING
159 
160 #define setFlag(val, mask) ((val) |= (mask))
161 #define getFlag(val, mask) ((val) & (mask))
162 #define clearFlag(val, mask) ((val) &= (~(mask))) // @ M09_CLEAR
163 
164 // Note: F0026_MAIN_GetBoundedValue<T> has been replaced by CLIP<T>
165 
166 #define CALL_MEMBER_FN(object, ptrToMember) ((object).*(ptrToMember))
167 
169  byte _version;
170  SaveStateDescriptor _descr;
171 };
172 
173 class DMEngine : public Engine {
174 private:
175  void startGame(); // @ F0462_START_StartGame_CPSF
176  void processNewPartyMap(uint16 mapIndex); // @ F0003_MAIN_ProcessNewPartyMap_CPSE
177  void initializeGame(); // @ F0463_START_InitializeGame_CPSADEF
178  void initMemoryManager(); // @ F0448_STARTUP1_InitializeMemoryManager_CPSADEF
179  void gameloop(); // @ F0002_MAIN_GameLoop_CPSDF
180  void initConstants();
181  Common::String getSavefileName(uint16 slot);
182  void writeSaveGameHeader(Common::OutSaveFile *out, const Common::String &saveName);
183  bool writeCompleteSaveFile(int16 slot, Common::String &desc, int16 saveAndPlayChoice);
184  void drawEntrance(); // @ F0439_STARTEND_DrawEntrance
185  void fuseSequenceUpdate(); // @ F0445_STARTEND_FuseSequenceUpdate
186  void processEntrance(); // @ F0441_STARTEND_ProcessEntrance
187  void openEntranceDoors(); // @ F0438_STARTEND_OpenEntranceDoors
188  void drawTittle(); // @ F0437_STARTEND_DrawTitle
189 
190 public:
191  explicit DMEngine(OSystem *syst, const DMADGameDescription *gameDesc);
192  ~DMEngine() override;
193  bool hasFeature(EngineFeature f) const override;
194 
195  Common::Error loadGameState(int slot) override;
196  bool canLoadGameStateCurrently(Common::U32String *msg = nullptr) override;
197 
198  bool isDemo() const;
199 
200  void delay(uint16 verticalBlank); // @ F0022_MAIN_Delay
201  uint16 getScaledProduct(uint16 val, uint16 scale, uint16 vale2); // @ F0030_MAIN_GetScaledProduct
202  uint16 getRandomNumber(uint32 max) { return _rnd->getRandomNumber(max - 1); }
203  int16 ordinalToIndex(int16 val); // @ M01_ORDINAL_TO_INDEX
204  int16 indexToOrdinal(int16 val); // @ M00_INDEX_TO_ORDINAL
205  Common::Error run() override; // @ main
206  void saveGame(); // @ F0433_STARTEND_ProcessCommand140_SaveGame_CPSCDF
207  LoadgameResult loadgame(int16 slot); // @ F0435_STARTEND_LoadGame_CPSF
208  void endGame(bool doNotDrawCreditsOnly); // @ F0444_STARTEND_Endgame
209 
210  void entranceDrawCredits();
211  void fuseSequence(); // @ F0446_STARTEND_FuseSequence
212  Common::Language getGameLanguage();
213 
214  Direction turnDirRight(int16 dir); // @ M17_NEXT
215  Direction turnDirLeft(int16 dir); // @ M19_PREVIOUS
216  Direction returnOppositeDir(int16 dir); // @ M18_OPPOSITE
217  bool isOrientedWestEast(int16 dir); // @ M16_IS_ORIENTED_WEST_EAST
218  uint16 normalizeModulo4(int16 dir); // @ M21_NORMALIZE
219 
220  int32 filterTime(int32 mapTime); // @ M30_TIME
221  int32 setMapAndTime(uint32 map, uint32 time); // @ M33_SET_MAP_AND_TIME
222  uint16 getMap(int32 mapTime); // @ M29_MAP
223  Thing thingWithNewCell(Thing thing, int16 cell); // @ M15_THING_WITH_NEW_CELL
224  int16 getDistance(int16 mapx1, int16 mapy1, int16 mapx2, int16 mapy2); // @ M38_DISTANCE
225  int32 setMap(int32 mapTime, uint32 map); // @ M31_setMap
226 
227 
228 private:
229  uint16 _dungeonId; // @ G0526_ui_DungeonID
230  byte *_entranceDoorAnimSteps[10]; // @ G0562_apuc_Bitmap_EntranceDoorAnimationSteps
231  byte *_interfaceCredits; // @ G0564_puc_Graphic5_InterfaceCredits
232  Common::RandomSource *_rnd;
233 
234  byte *_savedScreenForOpenEntranceDoors; // ad-hoc HACK
235  const DMADGameDescription *_gameVersion;
236  bool _canLoadFromGMM;
237 public:
238  Console *_console;
239  DisplayMan *_displayMan;
240  DungeonMan *_dungeonMan;
241  EventManager *_eventMan;
242  MenuMan *_menuMan;
243  ChampionMan *_championMan;
244  ObjectMan *_objectMan;
245  InventoryMan *_inventoryMan;
246  TextMan *_textMan;
247  MovesensMan *_moveSens;
248  GroupMan *_groupMan;
249  Timeline *_timeline;
250  ProjExpl *_projexpl;
251  DialogMan *_dialog;
252  SoundMan *_sound;
253 
254  Common::MemoryWriteStreamDynamic *_saveThumbnail;
255 
256  bool _engineShouldQuit;
257  int _loadSaveSlotAtRuntime;
258 
259  GameMode _gameMode; // @ G0298_B_NewGame
260  bool _restartGameRequest; // @ G0523_B_RestartGameRequested
261 
262  bool _stopWaitingForPlayerInput; // @ G0321_B_StopWaitingForPlayerInput
263  bool _gameTimeTicking; // @ G0301_B_GameTimeTicking
264  bool _restartGameAllowed; // @ G0524_B_RestartGameAllowed
265  bool _pressingEye; // @ G0331_B_PressingEye
266  bool _stopPressingEye; // @ G0332_B_StopPressingEye
267  bool _pressingMouth; // @ G0333_B_PressingMouth
268  bool _stopPressingMouth; // @ G0334_B_StopPressingMouth
269  bool _highlightBoxInversionRequested; // @ G0340_B_HighlightBoxInversionRequested
270  int16 _projectileDisableMovementTicks; // @ G0311_i_ProjectileDisabledMovementTicks
271  int16 _lastProjectileDisabledMovementDirection; // @ G0312_i_LastProjectileDisabledMovementDirection
272  bool _gameWon; // @ G0302_B_GameWon
273  int16 _newPartyMapIndex; // @ G0327_i_NewPartyMapIndex
274  bool _setMousePointerToObjectInMainLoop; // @ G0325_B_SetMousePointerToObjectInMainLoop
275  int16 _disabledMovementTicks; // @ G0310_i_DisabledMovementTicks
276 
277  int8 _dirIntoStepCountEast[4]; // @ G0233_ai_Graphic559_DirectionToStepEastCount
278  int8 _dirIntoStepCountNorth[4]; // @ G0234_ai_Graphic559_DirectionToStepNorthCount
279  int32 _gameTime; // @ G0313_ul_GameTime
280  char _stringBuildBuffer[128]; // @ G0353_ac_StringBuildBuffer
281  int16 _waitForInputMaxVerticalBlankCount; // @ G0318_i_WaitForInputMaximumVerticalBlankCount
282 
283  Thing _thingNone; // @ C0xFFFF_THING_NONE
284  Thing _thingEndOfList; // @ C0xFFFE_THING_ENDOFLIST
285  Thing _thingFirstExplosion; // @ C0xFF80_THING_FIRST_EXPLOSION
286  Thing _thingExplFireBall; // @ C0xFF80_THING_EXPLOSION_FIREBALL
287  Thing _thingExplSlime; // @ C0xFF81_THING_EXPLOSION_SLIME
288  Thing _thingExplLightningBolt; // @ C0xFF82_THING_EXPLOSION_LIGHTNING_BOLT
289  Thing _thingExplHarmNonMaterial; // @ C0xFF83_THING_EXPLOSION_HARM_NON_MATERIAL
290  Thing _thingExplOpenDoor; // @ C0xFF84_THING_EXPLOSION_OPEN_DOOR
291  Thing _thingExplPoisonBolt; // @ C0xFF86_THING_EXPLOSION_POISON_BOLT
292  Thing _thingExplPoisonCloud; // @ C0xFF87_THING_EXPLOSION_POISON_CLOUD
293  Thing _thingExplSmoke; // @ C0xFFA8_THING_EXPLOSION_SMOKE
294  Thing _thingExplFluxcage; // @ C0xFFB2_THING_EXPLOSION_FLUXCAGE
295  Thing _thingExplRebirthStep1; // @ C0xFFE4_THING_EXPLOSION_REBIRTH_STEP1
296  Thing _thingExplRebirthStep2; // @ C0xFFE5_THING_EXPLOSION_REBIRTH_STEP2
297  Thing _thingParty; // @ C0xFFFF_THING_PARTY
298 };
299 
300 WARN_UNUSED_RESULT bool readSaveGameHeader(Common::InSaveFile *in, SaveGameHeader *header, bool skipThumbnail = true);
301 
302 } // End of namespace DM
303 
304 #endif
Definition: projexpl.h:43
Definition: str.h:59
EngineFeature
Definition: engine.h:260
Definition: savefile.h:54
Definition: error.h:81
Definition: dm.h:135
Definition: random.h:44
Definition: movesens.h:45
Definition: memstream.h:194
Definition: eventman.h:199
Definition: sounds.h:109
Definition: stream.h:745
Definition: savestate.h:56
Definition: gfx.h:568
Definition: menus.h:77
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
Definition: ustr.h:57
bool skipThumbnail(Common::SeekableReadStream &in)
Definition: dm.h:168
Definition: text.h:42
Definition: console.h:36
Definition: detection.h:70
Definition: dungeonman.h:607
Definition: dialog.h:48
Definition: champion.h:35
Definition: group.h:156
Definition: dm.h:173
Definition: champion.h:479
Definition: system.h:163
Definition: inventory.h:53
Definition: objectman.h:48
Definition: engine.h:146
Definition: timeline.h:151
Language
Definition: language.h:45