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 BLADERUNNER_BLADERUNNER_H
24 #define BLADERUNNER_BLADERUNNER_H
25 
26 #include "bladerunner/archive.h"
27 
28 #include "common/array.h"
29 #include "common/cosinetables.h"
30 #include "common/random.h"
31 #include "common/sinetables.h"
32 #include "common/stream.h"
33 #include "common/keyboard.h"
34 
35 #include "engines/engine.h"
36 
37 #include "graphics/surface.h"
38 
39 //TODO: change this to debugflag
40 #define BLADERUNNER_DEBUG_CONSOLE     0
41 #define BLADERUNNER_ORIGINAL_SETTINGS 0
42 #define BLADERUNNER_ORIGINAL_BUGS     0
43 
44 namespace Common {
45 struct Event;
46 }
47 
48 namespace GUI {
49 class Debugger;
50 }
51 
52 struct ADGameDescription;
53 
54 namespace BladeRunner {
55 
56 enum DebugLevels {
57 	kDebugScript = 1 << 0
58 };
59 
60 class Actor;
61 class ActorDialogueQueue;
62 class ScreenEffects;
63 class AIScripts;
64 class AmbientSounds;
65 class AudioCache;
66 class AudioMixer;
67 class AudioPlayer;
68 class AudioSpeech;
69 class Chapters;
70 class CrimesDatabase;
71 class Combat;
72 class Debugger;
73 class DialogueMenu;
74 class Elevator;
75 class EndCredits;
76 class ESPER;
77 class Framelimiter;
78 class Font;
79 class GameFlags;
80 class GameInfo;
81 class ItemPickup;
82 class Items;
83 class KIA;
84 class Lights;
85 class Mouse;
86 class Music;
87 class Obstacles;
88 class Overlays;
89 class PoliceMaze;
90 class Scene;
91 class SceneObjects;
92 class SceneScript;
93 class Scores;
94 class Settings;
95 class Shapes;
96 class SliceAnimations;
97 class SliceRenderer;
98 class Spinner;
99 class Subtitles;
100 class SuspectsDatabase;
101 class TextResource;
102 class Time;
103 class Vector3;
104 class View;
105 class VK;
106 class Waypoints;
107 class ZBuffer;
108 
109 class BladeRunnerEngine : public Engine {
110 public:
111 	static const int kArchiveCount = 12; // +2 to original value (10) to accommodate for SUBTITLES.MIX and one extra resource file, to allow for capability of loading all VQAx.MIX and the MODE.MIX file (debug purposes)
112 	static const int kActorCount =  100;
113 	static const int kActorVoiceOver = kActorCount - 1;
114 
115 	// Incremental number to keep track of significant revisions of the ScummVM bladerunner engine
116 	// that could potentially introduce incompatibilities with old save files or require special actions to restore compatibility
117 	// This is stored in game global variable "kVariableGameVersion"
118 	// Original (classic) save game files will have version number of 0
119 	// Values:
120 	// 1: alpha testing (from May 15, 2019 to July 17, 2019)
121 	// 2: all time code uses uint32 (since July 17 2019),
122 	static const int kBladeRunnerScummVMVersion = 2;
123 
124 	bool _gameIsRunning;
125 	bool _windowIsActive;
126 	int  _playerLosesControlCounter;
127 
128 	Common::String   _languageCode;
129 	Common::Language _language;
130 	bool             _russianCP1251;
131 	bool             _noMusicDriver; // If "Music Device" is set to "No Music" from Audio tab
132 
133 	ActorDialogueQueue *_actorDialogueQueue;
134 	ScreenEffects      *_screenEffects;
135 	AIScripts          *_aiScripts;
136 	AmbientSounds      *_ambientSounds;
137 	AudioCache         *_audioCache;
138 	AudioMixer         *_audioMixer;
139 	AudioPlayer        *_audioPlayer;
140 	AudioSpeech        *_audioSpeech;
141 	Chapters           *_chapters;
142 	CrimesDatabase     *_crimesDatabase;
143 	Combat             *_combat;
144 	DialogueMenu       *_dialogueMenu;
145 	Elevator           *_elevator;
146 	EndCredits         *_endCredits;
147 	ESPER              *_esper;
148 	GameFlags          *_gameFlags;
149 	GameInfo           *_gameInfo;
150 	ItemPickup         *_itemPickup;
151 	Items              *_items;
152 	KIA                *_kia;
153 	Lights             *_lights;
154 	Font               *_mainFont;
155 	Subtitles          *_subtitles;
156 	Mouse              *_mouse;
157 	Music              *_music;
158 	Obstacles          *_obstacles;
159 	Overlays           *_overlays;
160 	PoliceMaze         *_policeMaze;
161 	Scene              *_scene;
162 	SceneObjects       *_sceneObjects;
163 	SceneScript        *_sceneScript;
164 	Scores             *_scores;
165 	Settings           *_settings;
166 	SliceAnimations    *_sliceAnimations;
167 	SliceRenderer      *_sliceRenderer;
168 	Spinner            *_spinner;
169 	SuspectsDatabase   *_suspectsDatabase;
170 	Time               *_time;
171 	View               *_view;
172 	Framelimiter       *_framelimiter;
173 	VK                 *_vk;
174 	Waypoints          *_waypoints;
175 	int                *_gameVars;
176 
177 	TextResource       *_textActorNames;
178 	TextResource       *_textCrimes;
179 	TextResource       *_textClueTypes;
180 	TextResource       *_textKIA;
181 	TextResource       *_textSpinnerDestinations;
182 	TextResource       *_textVK;
183 	TextResource       *_textOptions;
184 
185 	Shapes *_shapes;
186 
187 	Actor *_actors[kActorCount];
188 	Actor *_playerActor;
189 
190 	Graphics::PixelFormat _screenPixelFormat;
191 
192 	Graphics::Surface  _surfaceFront;
193 	Graphics::Surface  _surfaceBack;
194 	bool               _surfaceFrontCreated;
195 	bool               _surfaceBackCreated;
196 
197 	ZBuffer           *_zbuffer;
198 
199 	Common::RandomSource _rnd;
200 
201 	Debugger *_debugger;
202 
203 	Common::CosineTable *_cosTable1024;
204 	Common::SineTable   *_sinTable1024;
205 
206 	bool _isWalkingInterruptible;
207 	bool _interruptWalking;
208 	bool _playerActorIdle;
209 	bool _playerDead;
210 	bool _actorIsSpeaking;
211 	bool _actorSpeakStopIsRequested;
212 	bool _gameOver;
213 	bool _gameJustLaunched;
214 	int  _gameAutoSaveTextId;
215 	bool _gameIsAutoSaving;
216 	bool _gameIsLoading;
217 	bool _sceneIsLoading;
218 	bool _vqaIsPlaying;
219 	bool _vqaStopIsRequested;
220 	bool _subtitlesEnabled;  // tracks the state of whether subtitles are enabled or disabled from ScummVM GUI option or KIA checkbox (the states are synched)
221 	bool _sitcomMode;
222 	bool _shortyMode;
223 	bool _noDelayMillisFramelimiter;
224 	bool _framesPerSecondMax;
225 	bool _disableStaminaDrain;
226 	bool _cutContent;
227 	bool _validBootParam;
228 
229 	int _walkSoundId;
230 	int _walkSoundVolume;
231 	int _walkSoundPan;
232 	int _runningActorId;
233 
234 	uint32 _mouseClickTimeLast;
235 	uint32 _mouseClickTimeDiff;
236 
237 	int  _walkingToExitId;
238 	bool _isInsideScriptExit;
239 	int  _walkingToRegionId;
240 	bool _isInsideScriptRegion;
241 	int  _walkingToObjectId;
242 	bool _isInsideScriptObject;
243 	int  _walkingToItemId;
244 	bool _isInsideScriptItem;
245 	bool _walkingToEmpty;
246 	int  _walkingToEmptyX;
247 	int  _walkingToEmptyY;
248 	bool _isInsideScriptEmpty;
249 	int  _walkingToActorId;
250 	bool _isInsideScriptActor;
251 
252 	int    _actorUpdateCounter;
253 	uint32 _actorUpdateTimeLast;
254 
255 	uint32 _timeOfMainGameLoopTickPrevious;
256 
257 	// This addon is to emulate keeping a keyboard key pressed (continuous / repeated firing of the event)
258 	// -- code is pretty much identical from our common\events.cpp (KeyboardRepeatEventSourceWrapper)
259 	// for continuous events (keyDown)
260 	enum {
261 		kKeyRepeatInitialDelay = 400,
262 		kKeyRepeatSustainDelay = 100
263 	};
264 
265 	Common::KeyState _currentKeyDown;
266 	uint32 _keyRepeatTimeLast;
267 	uint32 _keyRepeatTimeDelay;
268 
269 private:
270 	MIXArchive _archives[kArchiveCount];
271 
272 public:
273 	BladeRunnerEngine(OSystem *syst, const ADGameDescription *desc);
274 	~BladeRunnerEngine() override;
275 
276 	bool hasFeature(EngineFeature f) const override;
277 	bool canLoadGameStateCurrently() override;
278 	Common::Error loadGameState(int slot) override;
279 	bool canSaveGameStateCurrently() override;
280 	Common::Error saveGameState(int slot, const Common::String &desc, bool isAutosave = false) override;
281 	void pauseEngineIntern(bool pause) override;
282 
283 	Common::Error run() override;
284 
285 	bool checkFiles(Common::Array<Common::String> &missingFiles);
286 
287 	bool startup(bool hasSavegames = false);
288 	void initChapterAndScene();
289 	void shutdown();
290 
291 	bool loadSplash();
292 
293 	Common::Point getMousePos() const;
294 	bool isMouseButtonDown() const;
295 
296 	void gameLoop();
297 	void gameTick();
298 
299 	void actorsUpdate();
300 
301 	void walkingReset();
302 
303 	void handleEvents();
304 	void handleKeyUp(Common::Event &event);
305 	void handleKeyDown(Common::Event &event);
306 	void handleMouseAction(int x, int y, bool mainButton, bool buttonDown, int scrollDirection = 0);
307 	void handleMouseClickExit(int exitId, int x, int y, bool buttonDown);
308 	void handleMouseClickRegion(int regionId, int x, int y, bool buttonDown);
309 	void handleMouseClickItem(int itemId, bool buttonDown);
310 	void handleMouseClickActor(int actorId, bool mainButton, bool buttonDown, Vector3 &scenePosition, int x, int y);
311 	void handleMouseClick3DObject(int objectId, bool buttonDown, bool isClickable, bool isTarget);
312 	void handleMouseClickEmpty(int x, int y, Vector3 &scenePosition, bool buttonDown);
313 
314 	void gameWaitForActive();
315 	void loopActorSpeaking();
316 	void loopQueuedDialogueStillPlaying();
317 
318 	void outtakePlay(int id, bool no_localization, int container = -1);
319 
320 	bool openArchive(const Common::String &name);
321 	bool closeArchive(const Common::String &name);
322 	bool isArchiveOpen(const Common::String &name) const;
323 
324 	void syncSoundSettings() override;
325 	bool isSubtitlesEnabled();
326 	void setSubtitlesEnabled(bool newVal);
327 
328 	Common::SeekableReadStream *getResourceStream(const Common::String &name);
329 
330 	bool playerHasControl();
331 	void playerLosesControl();
332 	void playerGainsControl(bool force = false);
333 	void playerDied();
334 
335 	bool saveGame(Common::WriteStream &stream, Graphics::Surface *thumb = NULL, bool origformat = false);
336 	bool loadGame(Common::SeekableReadStream &stream, int version);
337 	void newGame(int difficulty);
338 	void autoSaveGame(int textId, bool endgame);
339 
340 	void ISez(const Common::String &str);
341 
342 	void blitToScreen(const Graphics::Surface &src) const;
343 	Graphics::Surface generateThumbnail() const;
344 
345 	Common::String getTargetName() const;
346 };
347 
gameDataPixelFormat()348 static inline const Graphics::PixelFormat gameDataPixelFormat() {
349 	return Graphics::PixelFormat(2, 5, 5, 5, 1, 10, 5, 0, 15);
350 }
351 
getGameDataColor(uint16 color,uint8 & a,uint8 & r,uint8 & g,uint8 & b)352 static inline void getGameDataColor(uint16 color, uint8 &a, uint8 &r, uint8 &g, uint8 &b) {
353 	// gameDataPixelFormat().colorToARGB(vqaColor, a, r, g, b);
354 	// using pixel format functions is too slow on some ports because of runtime checks
355 	uint8 r5 = (color >> 10) & 0x1F;
356 	uint8 g5 = (color >>  5) & 0x1F;
357 	uint8 b5 = (color      ) & 0x1F;
358 	a = color >> 15;
359 	r = (r5 << 3) | (r5 >> 2);
360 	g = (g5 << 3) | (g5 >> 2);
361 	b = (b5 << 3) | (b5 >> 2);
362 }
363 
screenPixelFormat()364 static inline const Graphics::PixelFormat screenPixelFormat() {
365 	return ((BladeRunnerEngine*)g_engine)->_screenPixelFormat;
366 }
367 
drawPixel(Graphics::Surface & surface,void * dst,uint32 value)368 static inline void drawPixel(Graphics::Surface &surface, void* dst, uint32 value) {
369 	switch (surface.format.bytesPerPixel) {
370 	case 1:
371 		*(uint8*)dst = (uint8)value;
372 		break;
373 	case 2:
374 		*(uint16*)dst = (uint16)value;
375 		break;
376 	case 4:
377 		*(uint32*)dst = (uint32)value;
378 		break;
379 	default:
380 		break;
381 	}
382 }
383 
384 void blit(const Graphics::Surface &src, Graphics::Surface &dst);
385 
386 } // End of namespace BladeRunner
387 
388 #endif
389