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