scummvm/engines/xeen/xeen.cpp

298 lines
6.8 KiB
C++
Raw Normal View History

2014-12-23 15:49:35 +11:00
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "common/scummsys.h"
#include "common/config-manager.h"
#include "common/debug-channels.h"
#include "common/events.h"
#include "engines/util.h"
#include "xeen/xeen.h"
#include "xeen/files.h"
#include "xeen/resources.h"
2014-12-23 15:49:35 +11:00
namespace Xeen {
XeenEngine *g_vm = nullptr;
2014-12-23 15:49:35 +11:00
XeenEngine::XeenEngine(OSystem *syst, const XeenGameDescription *gameDesc)
: Engine(syst), _gameDescription(gameDesc), _randomSource("Xeen") {
// Set up debug channels
DebugMan.addDebugChannel(kDebugPath, "Path", "Pathfinding debug level");
DebugMan.addDebugChannel(kDebugScripts, "scripts", "Game scripts");
DebugMan.addDebugChannel(kDebugGraphics, "graphics", "Graphics handling");
DebugMan.addDebugChannel(kDebugSound, "sound", "Sound processing");
_combat = nullptr;
2014-12-23 15:49:35 +11:00
_debugger = nullptr;
_events = nullptr;
_files = nullptr;
_interface = nullptr;
_locations = nullptr;
_map = nullptr;
_party = nullptr;
_resources = nullptr;
_saves = nullptr;
_screen = nullptr;
_scripts = nullptr;
_sound = nullptr;
2015-01-25 21:19:59 -05:00
_spells = nullptr;
_windows = nullptr;
_noDirectionSense = false;
_startupWindowActive = false;
_gameMode = GMODE_STARTUP;
_mode = MODE_0;
_endingScore = 0;
_loadSaveSlot = -1;
g_vm = this;
2014-12-23 15:49:35 +11:00
}
XeenEngine::~XeenEngine() {
delete _combat;
2014-12-23 15:49:35 +11:00
delete _debugger;
delete _events;
delete _interface;
delete _locations;
delete _map;
delete _party;
delete _saves;
delete _screen;
delete _scripts;
delete _sound;
2015-01-25 21:19:59 -05:00
delete _spells;
delete _windows;
delete _resources;
delete _files;
g_vm = nullptr;
2014-12-23 15:49:35 +11:00
}
2018-02-20 18:50:17 -05:00
bool XeenEngine::initialize() {
2014-12-23 15:49:35 +11:00
// Create sub-objects of the engine
_files = new FileManager(this);
2018-02-20 18:50:17 -05:00
if (!_files->setup())
return false;
_resources = new Resources();
_combat = new Combat(this);
2014-12-23 15:49:35 +11:00
_debugger = new Debugger(this);
_events = new EventsManager(this);
_interface = new Interface(this);
_locations = new LocationManager();
_map = new Map(this);
_party = new Party(this);
_saves = new SavesManager(_targetName);
_screen = new Screen(this);
_scripts = new Scripts(this);
_sound = new Sound(_mixer);
2015-01-25 21:19:59 -05:00
_spells = new Spells(this);
_windows = new Windows();
2014-12-23 15:49:35 +11:00
// Set graphics mode
2018-02-26 19:20:34 -05:00
initGraphics(SCREEN_WIDTH, SCREEN_HEIGHT);
2014-12-23 15:49:35 +11:00
// Setup mixer
syncSoundSettings();
2014-12-23 15:49:35 +11:00
// If requested, load a savegame instead of showing the intro
if (ConfMan.hasKey("save_slot")) {
int saveSlot = ConfMan.getInt("save_slot");
if (saveSlot >= 0 && saveSlot <= 999)
_loadSaveSlot = saveSlot;
}
2018-02-20 18:50:17 -05:00
return true;
2014-12-23 15:49:35 +11:00
}
Common::Error XeenEngine::run() {
2018-02-20 18:50:17 -05:00
if (initialize())
outerGameLoop();
2014-12-23 15:49:35 +11:00
return Common::kNoError;
}
void XeenEngine::outerGameLoop() {
if (_loadSaveSlot != -1)
// Loading savegame from launcher, so Skip menu and go straight to game
_gameMode = GMODE_PLAY_GAME;
while (!shouldQuit() && _gameMode != GMODE_QUIT) {
GameMode mode = _gameMode;
_gameMode = GMODE_NONE;
assert(mode != GMODE_NONE);
switch (mode) {
case GMODE_STARTUP:
showStartup();
break;
case GMODE_MENU:
showMainMenu();
break;
case GMODE_PLAY_GAME:
playGame();
break;
default:
break;
}
}
}
2014-12-23 15:49:35 +11:00
int XeenEngine::getRandomNumber(int maxNumber) {
return _randomSource.getRandomNumber(maxNumber);
}
int XeenEngine::getRandomNumber(int minNumber, int maxNumber) {
return getRandomNumber(maxNumber - minNumber) + minNumber;
}
2014-12-23 15:49:35 +11:00
Common::Error XeenEngine::saveGameState(int slot, const Common::String &desc) {
return _saves->saveGameState(slot, desc);
2014-12-23 15:49:35 +11:00
}
Common::Error XeenEngine::loadGameState(int slot) {
_loadSaveSlot = slot;
return Common::kNoError;
2014-12-23 15:49:35 +11:00
}
bool XeenEngine::canLoadGameStateCurrently() {
2018-01-27 21:39:40 -05:00
return _mode != MODE_COMBAT;
2014-12-23 15:49:35 +11:00
}
bool XeenEngine::canSaveGameStateCurrently() {
2018-01-27 21:39:40 -05:00
return _mode != MODE_COMBAT;
2014-12-23 15:49:35 +11:00
}
void XeenEngine::playGame() {
_files->setGameCc(0);
_sound->stopAllAudio();
play();
}
void XeenEngine::play() {
_interface->setup();
_screen->loadBackground("back.raw");
_screen->loadPalette("mm4.pal");
if (getGameID() == GType_DarkSide && !_map->_loadDarkSide) {
2015-01-23 21:12:35 -05:00
_map->_loadDarkSide = true;
_party->_mazeId = 29;
_party->_mazeDirection = DIR_NORTH;
_party->_mazePosition.x = 25;
_party->_mazePosition.y = 21;
}
if (_loadSaveSlot >= 0) {
_saves->newGame();
_saves->loadGameState(_loadSaveSlot);
_loadSaveSlot = -1;
} else {
_map->load(_party->_mazeId);
}
2015-01-05 15:10:42 -05:00
_interface->startup();
if (_mode == MODE_0) {
// _screen->fadeOut();
2015-01-05 15:10:42 -05:00
}
(*_windows)[0].update();
2015-01-05 15:10:42 -05:00
_interface->mainIconsPrint();
(*_windows)[0].update();
2015-01-05 15:10:42 -05:00
_events->setCursor(0);
_combat->_moveMonsters = true;
2015-01-05 15:10:42 -05:00
if (_mode == MODE_0) {
_mode = MODE_1;
_screen->fadeIn();
2015-01-05 15:10:42 -05:00
}
_combat->_moveMonsters = true;
gameLoop();
if (_party->_dead)
death();
}
void XeenEngine::gameLoop() {
2015-01-05 15:10:42 -05:00
// Main game loop
while (!shouldExit()) {
if (_loadSaveSlot >= 0) {
// Load any pending savegame
int saveSlot = _loadSaveSlot;
_loadSaveSlot = -1;
_saves->loadGameState(saveSlot);
}
_map->cellFlagLookup(_party->_mazePosition);
if (_map->_currentIsEvent) {
_gameMode = (GameMode)_scripts->checkEvents();
if (shouldExit() || _gameMode)
return;
}
2015-02-27 07:50:32 -05:00
_party->giveTreasure();
// Main user interface handler for waiting for and processing user input
_interface->perform();
if (_party->_dead)
break;
2015-01-05 15:10:42 -05:00
}
}
2015-01-26 21:35:50 -05:00
Common::String XeenEngine::printMil(uint value) {
2016-09-04 14:19:05 -04:00
return (value >= 1000000) ? Common::String::format("%u mil", value / 1000000) :
Common::String::format("%u", value);
2015-01-26 21:35:50 -05:00
}
Common::String XeenEngine::printK(uint value) {
return (value > 9999) ? Common::String::format("%uk", value / 1000) :
Common::String::format("%u", value);
}
2015-03-01 10:41:11 -05:00
Common::String XeenEngine::printK2(uint value) {
return (value > 999) ? Common::String::format("%uk", value / 1000) :
Common::String::format("%u", value);
}
void XeenEngine::syncSoundSettings() {
Engine::syncSoundSettings();
if (_sound)
_sound->updateSoundSettings();
}
2018-02-20 18:50:17 -05:00
void XeenEngine::GUIError(const char *msg, ...) {
char buffer[STRINGBUFLEN];
va_list va;
// Generate the full error message
va_start(va, msg);
vsnprintf(buffer, STRINGBUFLEN, msg, va);
va_end(va);
GUIErrorMessage(buffer);
}
2014-12-23 15:49:35 +11:00
} // End of namespace Xeen