Files
2026-02-02 04:50:13 +01:00

461 lines
11 KiB
C++

/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/scummsys.h"
#include "common/config-manager.h"
#include "common/debug-channels.h"
#include "common/events.h"
#include "engines/util.h"
#include "mm/xeen/xeen.h"
#include "mm/xeen/files.h"
#include "mm/xeen/resources.h"
namespace MM {
namespace Xeen {
#ifdef USE_TTS
static const uint16 spanishEncodingTable[] = {
0x23, 0xc3a1, 0x24, 0xc3a9, 0x25, 0xc3bc, 0x26, 0xc3b3, 0x3d, 0xc3b1, 0x5b, 0xc2bf, // á, é, ü, ó, ñ, ¿
0x5c, 0xc39a, 0x5d, 0xc3ba, 0x5e, 0xc389, 0x5f, 0xc381, 0x7b, 0xc2a1, 0x7d, 0xc3ad, // Ú, ú, É, Á, ¡, í
0
};
#endif
XeenEngine *g_vm = nullptr;
XeenEngine::XeenEngine(OSystem *syst, const MightAndMagicGameDescription *gameDesc)
: MMEngine(syst, gameDesc) {
_combat = nullptr;
_debugger = nullptr;
_events = nullptr;
_files = nullptr;
_interface = nullptr;
_locations = nullptr;
_map = nullptr;
_party = nullptr;
_patcher = nullptr;
_resources = nullptr;
_saves = nullptr;
_screen = nullptr;
_scripts = nullptr;
_sound = nullptr;
_spells = nullptr;
_windows = nullptr;
_noDirectionSense = false;
_startupWindowActive = false;
_gameMode = GMODE_STARTUP;
_mode = MODE_STARTUP;
_endingScore = 0;
_loadSaveSlot = -1;
_gameWon[0] = _gameWon[1] = _gameWon[2] = false;
_finalScore = 0;
#ifdef USE_TTS
_mouseMoved = false;
#endif
g_vm = this;
}
XeenEngine::~XeenEngine() {
delete _combat;
//_debugger is deleted by Engine
delete _events;
delete _interface;
delete _locations;
delete _map;
delete _party;
delete _patcher;
delete _saves;
delete _screen;
delete _scripts;
delete _sound;
delete _spells;
delete _windows;
delete _resources;
delete _files;
g_vm = nullptr;
}
bool XeenEngine::initialize() {
// Create sub-objects of the engine
_files = new FileManager(this);
if (!_files->setup())
return false;
_resources = new Resources();
_combat = new Combat(this);
_debugger = new Debugger(this);
setDebugger(_debugger);
_events = new EventsManager(this);
_interface = new Interface(this);
_locations = new LocationManager();
_map = new Map(this);
_party = new Party(this);
_patcher = new Patcher();
_saves = new SavesManager(_targetName);
_screen = new Screen(this);
_scripts = new Scripts(this);
_sound = new Sound(_mixer);
_spells = new Spells(this);
_windows = new Windows();
// Set graphics mode
initGraphics(SCREEN_WIDTH, SCREEN_HEIGHT);
// Setup mixer
syncSoundSettings();
#ifdef USE_TTS
Common::TextToSpeechManager *ttsMan = g_system->getTextToSpeechManager();
if (ttsMan != nullptr) {
ttsMan->enable(ConfMan.getBool("tts_enabled"));
ttsMan->setLanguage(ConfMan.get("language"));
}
switch (getLanguage()) {
case Common::EN_ANY:
_ttsLanguage = kEnglish;
break;
case Common::DE_DEU:
_ttsLanguage = kGerman;
break;
case Common::FR_FRA:
_ttsLanguage = kFrench;
break;
case Common::ES_ESP:
_ttsLanguage = kSpanish;
break;
case Common::RU_RUS:
_ttsLanguage = kRussian;
break;
case Common::ZH_TWN:
_ttsLanguage = kChinese;
break;
default:
_ttsLanguage = kEnglish;
break;
}
if (_ttsLanguage == kRussian) {
_ttsTextEncoding = Common::CodePage::kDos866;
} else if (_ttsLanguage == kChinese) {
_ttsTextEncoding = Common::CodePage::kBig5;
} else {
_ttsTextEncoding = Common::CodePage::kDos850;
}
#endif
// Load settings
loadSettings();
return true;
}
void XeenEngine::loadSettings() {
_gameWon[0] = ConfMan.hasKey("game_won") && ConfMan.getBool("game_won");
_gameWon[1] = ConfMan.hasKey("game_won2") && ConfMan.getBool("game_won2");
_gameWon[2] = ConfMan.hasKey("game_won3") && ConfMan.getBool("game_won3");
_finalScore = ConfMan.hasKey("final_score") ? ConfMan.getInt("final_score") : 0;
_extOptions._showItemCosts = ConfMan.hasKey("ShowItemCosts") && ConfMan.getBool("ShowItemCosts");
_extOptions._durableArmor = ConfMan.hasKey("DurableArmor") && ConfMan.getBool("DurableArmor");
_extOptions._showHpSpBars = ConfMan.hasKey("ShowHpSpBars") && ConfMan.getBool("ShowHpSpBars");
// 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;
}
}
Common::Error XeenEngine::run() {
if (initialize())
outerGameLoop();
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;
}
}
}
int XeenEngine::getRandomNumber(int maxNumber) {
return _randomSource.getRandomNumber(maxNumber);
}
int XeenEngine::getRandomNumber(int minNumber, int maxNumber) {
return getRandomNumber(maxNumber - minNumber) + minNumber;
}
Common::Error XeenEngine::saveGameState(int slot, const Common::String &desc, bool isAutosave) {
return _saves->saveGameState(slot, desc);
}
Common::Error XeenEngine::loadGameState(int slot) {
_loadSaveSlot = slot;
return Common::kNoError;
}
bool XeenEngine::canLoadGameStateCurrently(Common::U32String *msg) {
return _mode != MODE_STARTUP;
}
bool XeenEngine::canSaveGameStateCurrently(Common::U32String *msg) {
return _mode != MODE_COMBAT && _mode != MODE_STARTUP && _mode != MODE_SCRIPT_IN_PROGRESS
&& (_map->mazeData()._mazeFlags & RESTRICTION_SAVE) == 0;
}
bool XeenEngine::canSaveAutosaveCurrently() {
return canSaveGameStateCurrently() &&
(_map && !(_map->mazeData()._mazeFlags & RESTRICTION_SAVE));
}
void XeenEngine::playGame() {
_files->setGameCc(0);
_sound->stopAllAudio();
SpriteResource::setClippedBottom(140);
play();
_sound->stopAllAudio();
}
void XeenEngine::play() {
_interface->setup();
_screen->loadBackground("back.raw");
_screen->loadPalette("mm4.pal");
if (getGameID() == GType_DarkSide && !_map->_loadCcNum) {
_map->_loadCcNum = 1;
_party->_mazeId = 29;
_party->_mazeDirection = DIR_NORTH;
_party->_mazePosition.x = 25;
_party->_mazePosition.y = 21;
}
_map->clearMaze();
if (_loadSaveSlot >= 0) {
_saves->newGame();
_saves->loadGameState(_loadSaveSlot);
_loadSaveSlot = -1;
} else {
_map->load(_party->_mazeId);
}
_interface->startup();
if (_mode == MODE_STARTUP) {
// _screen->fadeOut();
}
(*_windows)[0].update();
_interface->mainIconsPrint();
(*_windows)[0].update();
_events->setCursor(0);
_combat->_moveMonsters = true;
if (_mode == MODE_STARTUP) {
_mode = MODE_INTERACTIVE;
_screen->fadeIn();
}
_combat->_moveMonsters = true;
gameLoop();
if (_party->_dead)
death();
_mode = MODE_STARTUP;
_gameMode = GMODE_MENU;
}
void XeenEngine::gameLoop() {
// Main game loop
while (isLoadPending() || !shouldExit()) {
if (isLoadPending()) {
// Load any pending savegame
int saveSlot = _loadSaveSlot;
_loadSaveSlot = -1;
(void)_saves->loadGameState(saveSlot);
_interface->drawParty(true);
}
_map->cellFlagLookup(_party->_mazePosition);
if (_map->_currentIsEvent) {
_gameMode = (GameMode)_scripts->checkEvents();
if (isLoadPending())
continue;
if (shouldExit())
return;
}
_party->giveTreasure();
// Main user interface handler for waiting for and processing user input
_interface->perform();
if (_party->_dead)
break;
}
}
Common::String XeenEngine::printMil(uint value) {
return (value >= 1000000) ? Common::String::format("%u mil", value / 1000000) :
Common::String::format("%u", value);
}
Common::String XeenEngine::printK(uint value) {
return (value > 9999) ? Common::String::format("%uk", value / 1000) :
Common::String::format("%u", value);
}
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();
}
void XeenEngine::saveSettings() {
if (_gameWon[0])
ConfMan.setBool("game_won", true);
if (_gameWon[1])
ConfMan.setBool("game_won2", true);
if (_gameWon[2])
ConfMan.setBool("game_won3", true);
ConfMan.setInt("final_score", _finalScore);
ConfMan.flushToDisk();
}
void XeenEngine::GUIError(const Common::U32String &msg) {
GUIErrorMessage(msg);
}
#ifdef USE_TTS
void XeenEngine::sayText(const Common::String &text, Common::TextToSpeechManager::Action action) const {
if (text.empty()) {
return;
}
Common::TextToSpeechManager *ttsMan = g_system->getTextToSpeechManager();
if (ttsMan && ConfMan.getBool("tts_enabled")) {
if (getLanguage() == Common::ES_ESP) {
ttsMan->say(convertSpanishText(text), action);
} else {
ttsMan->say(text, action, _ttsTextEncoding);
}
}
}
void XeenEngine::stopTextToSpeech() const {
Common::TextToSpeechManager *ttsMan = g_system->getTextToSpeechManager();
if (ttsMan && ConfMan.getBool("tts_enabled") && ttsMan->isSpeaking()) {
ttsMan->stop();
}
}
Common::U32String XeenEngine::convertSpanishText(const Common::String &text) const {
const byte *bytes = (const byte *)text.c_str();
byte *convertedBytes = new byte[text.size() * 2 + 1];
int i = 0;
for (const byte *b = bytes; *b; ++b) {
if (*b == 0x60) { // ` to =
convertedBytes[i] = 0x3d;
i++;
continue;
}
bool inTable = false;
for (uint j = 0; spanishEncodingTable[j]; j += 2) {
if (*b == spanishEncodingTable[j]) {
convertedBytes[i] = (spanishEncodingTable[j + 1] >> 8) & 0xff;
convertedBytes[i + 1] = spanishEncodingTable[j + 1] & 0xff;
i += 2;
inTable = true;
break;
}
}
if (!inTable) {
convertedBytes[i] = *b;
i++;
}
}
convertedBytes[i] = 0;
Common::U32String result((char *)convertedBytes);
delete[] convertedBytes;
return result;
}
#endif
uint32 XeenEngine::getSpecificGameId() const {
uint gameId = g_vm->getGameID();
if (gameId == GType_WorldOfXeen)
gameId = _files->_ccNum ? GType_DarkSide : GType_Clouds;
return gameId;
}
uint32 XeenEngine::getGameFeatures() const {
return _gameDescription->features;
}
} // End of namespace Xeen
} // End of namespace MM