aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/engine
diff options
context:
space:
mode:
Diffstat (limited to 'engines/sci/engine')
-rw-r--r--engines/sci/engine/file.cpp84
-rw-r--r--engines/sci/engine/file.h24
-rw-r--r--engines/sci/engine/gc.cpp2
-rw-r--r--engines/sci/engine/kernel.cpp1
-rw-r--r--engines/sci/engine/kernel.h57
-rw-r--r--engines/sci/engine/kernel_tables.h188
-rw-r--r--engines/sci/engine/kevent.cpp28
-rw-r--r--engines/sci/engine/kfile.cpp633
-rw-r--r--engines/sci/engine/kgraphics.cpp6
-rw-r--r--engines/sci/engine/kgraphics32.cpp134
-rw-r--r--engines/sci/engine/klists.cpp248
-rw-r--r--engines/sci/engine/kmisc.cpp8
-rw-r--r--engines/sci/engine/kpathing.cpp4
-rw-r--r--engines/sci/engine/kscripts.cpp4
-rw-r--r--engines/sci/engine/kstring.cpp356
-rw-r--r--engines/sci/engine/kvideo.cpp23
-rw-r--r--engines/sci/engine/message.cpp17
-rw-r--r--engines/sci/engine/savegame.cpp145
-rw-r--r--engines/sci/engine/savegame.h10
-rw-r--r--engines/sci/engine/script_patches.cpp228
-rw-r--r--engines/sci/engine/seg_manager.cpp65
-rw-r--r--engines/sci/engine/seg_manager.h13
-rw-r--r--engines/sci/engine/segment.cpp75
-rw-r--r--engines/sci/engine/segment.h524
-rw-r--r--engines/sci/engine/selector.cpp25
-rw-r--r--engines/sci/engine/state.cpp54
-rw-r--r--engines/sci/engine/state.h24
-rw-r--r--engines/sci/engine/vm.cpp46
-rw-r--r--engines/sci/engine/vm.h16
-rw-r--r--engines/sci/engine/vm_types.cpp7
-rw-r--r--engines/sci/engine/workarounds.cpp88
-rw-r--r--engines/sci/engine/workarounds.h15
32 files changed, 2001 insertions, 1151 deletions
diff --git a/engines/sci/engine/file.cpp b/engines/sci/engine/file.cpp
index 8cecd8c82c..d46d68d9df 100644
--- a/engines/sci/engine/file.cpp
+++ b/engines/sci/engine/file.cpp
@@ -311,43 +311,65 @@ static bool _savegame_sort_byDate(const SavegameDesc &l, const SavegameDesc &r)
return (l.time > r.time);
}
-// Create a sorted array containing all found savedgames
-void listSavegames(Common::Array<SavegameDesc> &saves) {
+bool fillSavegameDesc(const Common::String &filename, SavegameDesc *desc) {
Common::SaveFileManager *saveFileMan = g_sci->getSaveFileManager();
+ Common::SeekableReadStream *in;
+ if ((in = saveFileMan->openForLoading(filename)) == nullptr) {
+ return false;
+ }
+
+ SavegameMetadata meta;
+ if (!get_savegame_metadata(in, &meta) || meta.name.empty()) {
+ // invalid
+ delete in;
+ return false;
+ }
+ delete in;
+
+ const int id = strtol(filename.end() - 3, NULL, 10);
+ desc->id = id;
+ desc->date = meta.saveDate;
+ // We need to fix date in here, because we save DDMMYYYY instead of
+ // YYYYMMDD, so sorting wouldn't work
+ desc->date = ((desc->date & 0xFFFF) << 16) | ((desc->date & 0xFF0000) >> 8) | ((desc->date & 0xFF000000) >> 24);
+ desc->time = meta.saveTime;
+ desc->version = meta.version;
+ desc->gameVersion = meta.gameVersion;
+#ifdef ENABLE_SCI32
+ if (g_sci->getGameId() == GID_SHIVERS) {
+ desc->lowScore = meta.lowScore;
+ desc->highScore = meta.highScore;
+ } else if (g_sci->getGameId() == GID_MOTHERGOOSEHIRES) {
+ desc->avatarId = meta.avatarId;
+ }
+#endif
+
+ if (meta.name.lastChar() == '\n')
+ meta.name.deleteLastChar();
+
+ Common::strlcpy(desc->name, meta.name.c_str(), SCI_MAX_SAVENAME_LENGTH);
- // Load all saves
+ return desc;
+}
+
+// Create an array containing all found savedgames, sorted by creation date
+void listSavegames(Common::Array<SavegameDesc> &saves) {
+ Common::SaveFileManager *saveFileMan = g_sci->getSaveFileManager();
Common::StringArray saveNames = saveFileMan->listSavefiles(g_sci->getSavegamePattern());
for (Common::StringArray::const_iterator iter = saveNames.begin(); iter != saveNames.end(); ++iter) {
- Common::String filename = *iter;
- Common::SeekableReadStream *in;
- if ((in = saveFileMan->openForLoading(filename))) {
- SavegameMetadata meta;
- if (!get_savegame_metadata(in, &meta) || meta.name.empty()) {
- // invalid
- delete in;
- continue;
- }
- delete in;
-
- SavegameDesc desc;
- desc.id = strtol(filename.end() - 3, NULL, 10);
- desc.date = meta.saveDate;
- // We need to fix date in here, because we save DDMMYYYY instead of
- // YYYYMMDD, so sorting wouldn't work
- desc.date = ((desc.date & 0xFFFF) << 16) | ((desc.date & 0xFF0000) >> 8) | ((desc.date & 0xFF000000) >> 24);
- desc.time = meta.saveTime;
- desc.version = meta.version;
-
- if (meta.name.lastChar() == '\n')
- meta.name.deleteLastChar();
-
- Common::strlcpy(desc.name, meta.name.c_str(), SCI_MAX_SAVENAME_LENGTH);
-
- debug(3, "Savegame in file %s ok, id %d", filename.c_str(), desc.id);
-
- saves.push_back(desc);
+ const Common::String &filename = *iter;
+
+#ifdef ENABLE_SCI32
+ const int id = strtol(filename.end() - 3, NULL, 10);
+ if (id == kNewGameId || id == kAutoSaveId) {
+ continue;
}
+#endif
+
+ SavegameDesc desc;
+ fillSavegameDesc(filename, &desc);
+ saves.push_back(desc);
}
// Sort the list by creation date of the saves
diff --git a/engines/sci/engine/file.h b/engines/sci/engine/file.h
index 982d7b7823..0154338f6c 100644
--- a/engines/sci/engine/file.h
+++ b/engines/sci/engine/file.h
@@ -34,12 +34,22 @@ enum {
_K_FILE_MODE_CREATE = 2
};
-/* Maximum length of a savegame name (including terminator character). */
-#define SCI_MAX_SAVENAME_LENGTH 0x24
+enum {
+ SCI_MAX_SAVENAME_LENGTH = 36, ///< Maximum length of a savegame name (including terminator character).
+ MAX_SAVEGAME_NR = 20 ///< Maximum number of savegames
+};
+#ifdef ENABLE_SCI32
enum {
- MAX_SAVEGAME_NR = 20 /**< Maximum number of savegames */
+ kAutoSaveId = 0, ///< The save game slot number for autosaves
+ kNewGameId = 999, ///< The save game slot number for a "new game" save
+
+ // SCI engine expects game IDs to start at 0, but slot 0 in ScummVM is
+ // reserved for autosave, so non-autosave games get their IDs shifted up
+ // when saving or restoring, and shifted down when enumerating save games
+ kSaveIdShift = 1
};
+#endif
#define VIRTUALFILE_HANDLE_START 32000
#define VIRTUALFILE_HANDLE_SCI32SAVE 32100
@@ -53,6 +63,14 @@ struct SavegameDesc {
int time;
int version;
char name[SCI_MAX_SAVENAME_LENGTH];
+ Common::String gameVersion;
+#ifdef ENABLE_SCI32
+ // Used by Shivers 1
+ uint16 lowScore;
+ uint16 highScore;
+ // Used by MGDX
+ uint8 avatarId;
+#endif
};
class FileHandle {
diff --git a/engines/sci/engine/gc.cpp b/engines/sci/engine/gc.cpp
index 6c1713bed9..50f7709baf 100644
--- a/engines/sci/engine/gc.cpp
+++ b/engines/sci/engine/gc.cpp
@@ -46,7 +46,7 @@ const char *segmentTypeNames[] = {
"dynmem", // 9
"obsolete", // 10: obsolete string fragments
"array", // 11: SCI32 arrays
- "string" // 12: SCI32 strings
+ "obsolete" // 12: obsolete SCI32 strings
};
#endif
diff --git a/engines/sci/engine/kernel.cpp b/engines/sci/engine/kernel.cpp
index c03504d277..85cad99226 100644
--- a/engines/sci/engine/kernel.cpp
+++ b/engines/sci/engine/kernel.cpp
@@ -410,7 +410,6 @@ uint16 Kernel::findRegType(reg_t reg) {
case SEG_TYPE_HUNK:
#ifdef ENABLE_SCI32
case SEG_TYPE_ARRAY:
- case SEG_TYPE_STRING:
case SEG_TYPE_BITMAP:
#endif
result |= SIG_TYPE_REFERENCE;
diff --git a/engines/sci/engine/kernel.h b/engines/sci/engine/kernel.h
index 45477e1153..cce9a223d3 100644
--- a/engines/sci/engine/kernel.h
+++ b/engines/sci/engine/kernel.h
@@ -483,30 +483,46 @@ reg_t kShowMovieWinGetDuration(EngineState *s, int argc, reg_t *argv);
reg_t kShowMovieWinPlayUntilEvent(EngineState *s, int argc, reg_t *argv);
reg_t kShowMovieWinInitDouble(EngineState *s, int argc, reg_t *argv);
+reg_t kSave(EngineState *s, int argc, reg_t *argv);
+reg_t kSaveGame32(EngineState *s, int argc, reg_t *argv);
+reg_t kRestoreGame32(EngineState *s, int argc, reg_t *argv);
+reg_t kGetSaveFiles32(EngineState *s, int argc, reg_t *argv);
+reg_t kCheckSaveGame32(EngineState *s, int argc, reg_t *argv);
+reg_t kMakeSaveCatName(EngineState *s, int argc, reg_t *argv);
+reg_t kMakeSaveFileName(EngineState *s, int argc, reg_t *argv);
+
+reg_t kSetHotRectangles(EngineState *s, int argc, reg_t *argv);
reg_t kIsHiRes(EngineState *s, int argc, reg_t *argv);
-reg_t kArray(EngineState *s, int argc, reg_t *argv);
reg_t kListAt(EngineState *s, int argc, reg_t *argv);
-reg_t kString(EngineState *s, int argc, reg_t *argv);
+reg_t kArray(EngineState *s, int argc, reg_t *argv);
+reg_t kArrayNew(EngineState *s, int argc, reg_t *argv);
+reg_t kArrayGetSize(EngineState *s, int argc, reg_t *argv);
+reg_t kArrayGetElement(EngineState *s, int argc, reg_t *argv);
+reg_t kArraySetElements(EngineState *s, int argc, reg_t *argv);
+reg_t kArrayFree(EngineState *s, int argc, reg_t *argv);
+reg_t kArrayFill(EngineState *s, int argc, reg_t *argv);
+reg_t kArrayCopy(EngineState *s, int argc, reg_t *argv);
+reg_t kArrayCompare(EngineState *s, int argc, reg_t *argv);
+reg_t kArrayDuplicate(EngineState *s, int argc, reg_t *argv);
+reg_t kArrayGetData(EngineState *s, int argc, reg_t *argv);
+reg_t kArrayByteCopy(EngineState *s, int argc, reg_t *argv);
+
+reg_t kString(EngineState *s, int argc, reg_t *argv);
reg_t kStringNew(EngineState *s, int argc, reg_t *argv);
-reg_t kStringSize(EngineState *s, int argc, reg_t *argv);
-reg_t kStringAt(EngineState *s, int argc, reg_t *argv);
-reg_t kStringPutAt(EngineState *s, int argc, reg_t *argv);
+reg_t kStringGetChar(EngineState *s, int argc, reg_t *argv);
reg_t kStringFree(EngineState *s, int argc, reg_t *argv);
-reg_t kStringFill(EngineState *s, int argc, reg_t *argv);
-reg_t kStringCopy(EngineState *s, int argc, reg_t *argv);
reg_t kStringCompare(EngineState *s, int argc, reg_t *argv);
-reg_t kStringDup(EngineState *s, int argc, reg_t *argv);
reg_t kStringGetData(EngineState *s, int argc, reg_t *argv);
-reg_t kStringLen(EngineState *s, int argc, reg_t *argv);
-reg_t kStringPrintf(EngineState *s, int argc, reg_t *argv);
-reg_t kStringPrintfBuf(EngineState *s, int argc, reg_t *argv);
-reg_t kStringAtoi(EngineState *s, int argc, reg_t *argv);
+reg_t kStringLength(EngineState *s, int argc, reg_t *argv);
+reg_t kStringFormat(EngineState *s, int argc, reg_t *argv);
+reg_t kStringFormatAt(EngineState *s, int argc, reg_t *argv);
+reg_t kStringToInteger(EngineState *s, int argc, reg_t *argv);
reg_t kStringTrim(EngineState *s, int argc, reg_t *argv);
-reg_t kStringUpper(EngineState *s, int argc, reg_t *argv);
-reg_t kStringLower(EngineState *s, int argc, reg_t *argv);
-reg_t kStringTrn(EngineState *s, int argc, reg_t *argv);
-reg_t kStringTrnExclude(EngineState *s, int argc, reg_t *argv);
+reg_t kStringToUpperCase(EngineState *s, int argc, reg_t *argv);
+reg_t kStringToLowerCase(EngineState *s, int argc, reg_t *argv);
+reg_t kStringReplaceSubstring(EngineState *s, int argc, reg_t *argv);
+reg_t kStringReplaceSubstringEx(EngineState *s, int argc, reg_t *argv);
reg_t kScrollWindowCreate(EngineState *s, int argc, reg_t *argv);
reg_t kScrollWindowAdd(EngineState *s, int argc, reg_t *argv);
@@ -547,7 +563,7 @@ reg_t kBitmapDrawText(EngineState *s, int argc, reg_t *argv);
reg_t kBitmapDrawColor(EngineState *s, int argc, reg_t *argv);
reg_t kBitmapDrawBitmap(EngineState *s, int argc, reg_t *argv);
reg_t kBitmapInvert(EngineState *s, int argc, reg_t *argv);
-reg_t kBitmapSetDisplace(EngineState *s, int argc, reg_t *argv);
+reg_t kBitmapSetOrigin(EngineState *s, int argc, reg_t *argv);
reg_t kBitmapCreateFromView(EngineState *s, int argc, reg_t *argv);
reg_t kBitmapCopyPixels(EngineState *s, int argc, reg_t *argv);
reg_t kBitmapClone(EngineState *s, int argc, reg_t *argv);
@@ -576,8 +592,6 @@ reg_t kListFirstTrue(EngineState *s, int argc, reg_t *argv);
reg_t kListAllTrue(EngineState *s, int argc, reg_t *argv);
reg_t kEditText(EngineState *s, int argc, reg_t *argv);
-reg_t kMakeSaveCatName(EngineState *s, int argc, reg_t *argv);
-reg_t kMakeSaveFileName(EngineState *s, int argc, reg_t *argv);
reg_t kSetScroll(EngineState *s, int argc, reg_t *argv);
reg_t kPaletteSetFromResource32(EngineState *s, int argc, reg_t *argv);
@@ -607,8 +621,6 @@ reg_t kMorphOn(EngineState *s, int argc, reg_t *argv);
reg_t kText(EngineState *s, int argc, reg_t *argv);
reg_t kTextSize32(EngineState *s, int argc, reg_t *argv);
reg_t kTextWidth(EngineState *s, int argc, reg_t *argv);
-reg_t kSave(EngineState *s, int argc, reg_t *argv);
-reg_t kAutoSave(EngineState *s, int argc, reg_t *argv);
reg_t kList(EngineState *s, int argc, reg_t *argv);
reg_t kCD(EngineState *s, int argc, reg_t *argv);
reg_t kCheckCD(EngineState *s, int argc, reg_t *argv);
@@ -622,6 +634,7 @@ reg_t kWinHelp(EngineState *s, int argc, reg_t *argv);
reg_t kMessageBox(EngineState *s, int argc, reg_t *argv);
reg_t kGetConfig(EngineState *s, int argc, reg_t *argv);
reg_t kGetSierraProfileInt(EngineState *s, int argc, reg_t *argv);
+reg_t kPrintDebug(EngineState *s, int argc, reg_t *argv);
reg_t kCelInfo(EngineState *s, int argc, reg_t *argv);
reg_t kSetLanguage(EngineState *s, int argc, reg_t *argv);
reg_t kScrollWindow(EngineState *s, int argc, reg_t *argv);
@@ -707,7 +720,7 @@ reg_t kFileIOReadByte(EngineState *s, int argc, reg_t *argv);
reg_t kFileIOWriteByte(EngineState *s, int argc, reg_t *argv);
reg_t kFileIOReadWord(EngineState *s, int argc, reg_t *argv);
reg_t kFileIOWriteWord(EngineState *s, int argc, reg_t *argv);
-reg_t kFileIOCreateSaveSlot(EngineState *s, int argc, reg_t *argv);
+reg_t kFileIOGetCWD(EngineState *s, int argc, reg_t *argv);
reg_t kFileIOIsValidDirectory(EngineState *s, int argc, reg_t *argv);
#endif
diff --git a/engines/sci/engine/kernel_tables.h b/engines/sci/engine/kernel_tables.h
index 6e141e7f3b..0f1210e0e1 100644
--- a/engines/sci/engine/kernel_tables.h
+++ b/engines/sci/engine/kernel_tables.h
@@ -67,8 +67,8 @@ struct SciKernelMapSubEntry {
#define SIG_SINCE_SCI11 SCI_VERSION_1_1, SCI_VERSION_NONE
#define SIG_SCI2 SCI_VERSION_2, SCI_VERSION_2
#define SIG_SCI21EARLY SCI_VERSION_2_1_EARLY, SCI_VERSION_2_1_EARLY
-#define SIG_UNTIL_SCI21EARLY SCI_VERSION_2, SCI_VERSION_2_1_EARLY
-#define SIG_UNTIL_SCI21MID SCI_VERSION_2, SCI_VERSION_2_1_MIDDLE
+#define SIG_THRU_SCI21EARLY SCI_VERSION_2, SCI_VERSION_2_1_EARLY
+#define SIG_THRU_SCI21MID SCI_VERSION_2, SCI_VERSION_2_1_MIDDLE
#define SIG_SINCE_SCI21 SCI_VERSION_2_1_EARLY, SCI_VERSION_3
#define SIG_SINCE_SCI21MID SCI_VERSION_2_1_MIDDLE, SCI_VERSION_3
#define SIG_SINCE_SCI21LATE SCI_VERSION_2_1_LATE, SCI_VERSION_3
@@ -248,7 +248,7 @@ static const SciKernelMapSubEntry kDoAudio_subops[] = {
// version, subId, function-mapping, signature, workarounds
static const SciKernelMapSubEntry kGraph_subops[] = {
- { SIG_SCI32, 1, MAP_CALL(StubNull), "", NULL }, // called by gk1 sci32 right at the start
+ // 1 - load bits
{ SIG_SCIALL, 2, MAP_CALL(GraphGetColorCount), "", NULL },
// 3 - set palette via resource
{ SIG_SCIALL, 4, MAP_CALL(GraphDrawLine), "iiiii(i)(i)", kGraphDrawLine_workarounds },
@@ -311,9 +311,9 @@ static const SciKernelMapSubEntry kPalette_subops[] = {
SCI_SUBOPENTRY_TERMINATOR
};
+// version, subId, function-mapping, signature, workarounds
static const SciKernelMapSubEntry kFileIO_subops[] = {
- { SIG_SCI32, 0, MAP_CALL(FileIOOpen), "r(i)", NULL },
- { SIG_SCIALL, 0, MAP_CALL(FileIOOpen), "ri", NULL },
+ { SIG_SCIALL, 0, MAP_CALL(FileIOOpen), "ri", kFileIOOpen_workarounds },
{ SIG_SCIALL, 1, MAP_CALL(FileIOClose), "i", NULL },
{ SIG_SCIALL, 2, MAP_CALL(FileIOReadRaw), "iri", NULL },
{ SIG_SCIALL, 3, MAP_CALL(FileIOWriteRaw), "iri", NULL },
@@ -326,13 +326,13 @@ static const SciKernelMapSubEntry kFileIO_subops[] = {
{ SIG_SCIALL, 10, MAP_CALL(FileIOExists), "r", NULL },
{ SIG_SINCE_SCI11, 11, MAP_CALL(FileIORename), "rr", NULL },
#ifdef ENABLE_SCI32
- { SIG_SCI32, 13, MAP_CALL(FileIOReadByte), "i", NULL },
- { SIG_SCI32, 14, MAP_CALL(FileIOWriteByte), "ii", NULL },
- { SIG_SCI32, 15, MAP_CALL(FileIOReadWord), "i", NULL },
- { SIG_SCI32, 16, MAP_CALL(FileIOWriteWord), "ii", NULL },
- { SIG_SCI32, 17, MAP_CALL(FileIOCreateSaveSlot), "ir", NULL },
- { SIG_SCI32, 18, MAP_EMPTY(FileIOChangeDirectory), "r", NULL }, // for SQ6, when changing the savegame directory in the save/load dialog
- { SIG_SCI32, 19, MAP_CALL(FileIOIsValidDirectory), "r", NULL }, // for Torin / Torin demo
+ { SIG_SINCE_SCI21MID, 13, MAP_CALL(FileIOReadByte), "i", NULL },
+ { SIG_SINCE_SCI21MID, 14, MAP_CALL(FileIOWriteByte), "ii", NULL },
+ { SIG_SINCE_SCI21MID, 15, MAP_CALL(FileIOReadWord), "i", NULL },
+ { SIG_SINCE_SCI21MID, 16, MAP_CALL(FileIOWriteWord), "ii", NULL },
+ { SIG_SINCE_SCI21MID, 17, "FileIOCheckFreeSpace", kCheckFreeSpace, "i(r)", NULL },
+ { SIG_SINCE_SCI21MID, 18, MAP_CALL(FileIOGetCWD), "r", NULL },
+ { SIG_SINCE_SCI21MID, 19, MAP_CALL(FileIOIsValidDirectory), "r", NULL },
#endif
SCI_SUBOPENTRY_TERMINATOR
};
@@ -351,15 +351,19 @@ static const SciKernelMapSubEntry kPalCycle_subops[] = {
// version, subId, function-mapping, signature, workarounds
static const SciKernelMapSubEntry kSave_subops[] = {
- { SIG_SCI32, 0, MAP_CALL(SaveGame), "[r0]i[r0](r0)", NULL },
- { SIG_SCI32, 1, MAP_CALL(RestoreGame), "[r0]i[r0]", NULL },
- { SIG_SCI32, 2, MAP_CALL(GetSaveDir), "(r*)", NULL },
- { SIG_SCI32, 3, MAP_CALL(CheckSaveGame), ".*", NULL },
+ { SIG_SCI32, 0, MAP_CALL(SaveGame32), "[r0]i[r0][r0]", NULL },
+ { SIG_SCI32, 1, MAP_CALL(RestoreGame32), "[r0]i[r0]", NULL },
+ // System script 64994 in several SCI2.1mid games (KQ7 2.00b, Phant1,
+ // PQ:SWAT, SQ6, Torin) calls GetSaveDir with an extra unused argument, and
+ // it is easier to just handle it here than to bother with creating
+ // workarounds
+ { SIG_SCI32, 2, MAP_CALL(GetSaveDir), "(r)", NULL },
+ { SIG_SCI32, 3, MAP_CALL(CheckSaveGame32), "ri[r0]", NULL },
// Subop 4 hasn't been encountered yet
- { SIG_SCI32, 5, MAP_CALL(GetSaveFiles), "rrr", NULL },
+ { SIG_SCI32, 5, MAP_CALL(GetSaveFiles32), "rrr", NULL },
{ SIG_SCI32, 6, MAP_CALL(MakeSaveCatName), "rr", NULL },
{ SIG_SCI32, 7, MAP_CALL(MakeSaveFileName), "rri", NULL },
- { SIG_SCI32, 8, MAP_CALL(AutoSave), "[o0]", NULL },
+ { SIG_SCI32, 8, MAP_EMPTY(GameIsRestarting), "(.*)", NULL },
SCI_SUBOPENTRY_TERMINATOR
};
@@ -387,12 +391,12 @@ static const SciKernelMapSubEntry kBitmap_subops[] = {
{ SIG_SINCE_SCI21, 5, MAP_CALL(BitmapDrawColor), "riiiii", NULL },
{ SIG_SINCE_SCI21, 6, MAP_CALL(BitmapDrawBitmap), "rr(i)(i)(i)", NULL },
{ SIG_SINCE_SCI21, 7, MAP_CALL(BitmapInvert), "riiiiii", NULL },
- { SIG_SINCE_SCI21MID, 8, MAP_CALL(BitmapSetDisplace), "rii", NULL },
+ { SIG_SINCE_SCI21MID, 8, MAP_CALL(BitmapSetOrigin), "rii", NULL },
{ SIG_SINCE_SCI21MID, 9, MAP_CALL(BitmapCreateFromView), "iii(i)(i)(i)([r0])", NULL },
{ SIG_SINCE_SCI21MID, 10, MAP_CALL(BitmapCopyPixels), "rr", NULL },
{ SIG_SINCE_SCI21MID, 11, MAP_CALL(BitmapClone), "r", NULL },
- { SIG_SINCE_SCI21LATE, 12, MAP_CALL(BitmapGetInfo), "r(i)(i)", NULL },
- { SIG_SINCE_SCI21LATE, 13, MAP_CALL(BitmapScale), "r...ii", NULL },
+ { SIG_SINCE_SCI21MID, 12, MAP_CALL(BitmapGetInfo), "r(i)(i)", NULL },
+ { SIG_SINCE_SCI21LATE,13, MAP_CALL(BitmapScale), "r...ii", NULL },
{ SIG_SCI3, 14, MAP_CALL(BitmapCreateFromUnknown), "......", NULL },
{ SIG_SCI3, 15, MAP_EMPTY(Bitmap), "(.*)", NULL },
{ SIG_SCI3, 16, MAP_EMPTY(Bitmap), "(.*)", NULL },
@@ -501,44 +505,56 @@ static const SciKernelMapSubEntry kRemapColors_subops[] = {
};
// version, subId, function-mapping, signature, workarounds
+static const SciKernelMapSubEntry kArray_subops[] = {
+ { SIG_SCI32, 0, MAP_CALL(ArrayNew), "ii", NULL },
+ { SIG_SCI32, 1, MAP_CALL(ArrayGetSize), "r", NULL },
+ { SIG_SCI32, 2, MAP_CALL(ArrayGetElement), "ri", NULL },
+ { SIG_SCI32, 3, MAP_CALL(ArraySetElements), "ri(.*)", kArraySetElements_workarounds },
+ { SIG_SCI32, 4, MAP_CALL(ArrayFree), "r", NULL },
+ { SIG_SCI32, 5, MAP_CALL(ArrayFill), "riii", NULL },
+ { SIG_SCI32, 6, MAP_CALL(ArrayCopy), "ririi", NULL },
+ // there is no subop 7
+ { SIG_SCI32, 8, MAP_CALL(ArrayDuplicate), "r", NULL },
+ { SIG_SCI32, 9, MAP_CALL(ArrayGetData), "[or]", NULL },
+ { SIG_SCI3, 10, MAP_CALL(ArrayByteCopy), "ririi", NULL },
+ SCI_SUBOPENTRY_TERMINATOR
+};
+
+// version, subId, function-mapping, signature, workarounds
static const SciKernelMapSubEntry kString_subops[] = {
- { SIG_SCI32, 0, MAP_CALL(StringNew), "i(i)", NULL },
- { SIG_SCI32, 1, MAP_CALL(StringSize), "[or]", NULL },
- { SIG_SCI32, 2, MAP_CALL(StringAt), "[or]i", NULL },
- { SIG_SCI32, 3, MAP_CALL(StringPutAt), "[or]i(i*)", kStringPutAt_workarounds },
- // StringFree accepts invalid references
- { SIG_SCI32, 4, MAP_CALL(StringFree), "[or0!]", NULL },
- { SIG_SCI32, 5, MAP_CALL(StringFill), "[or]ii", NULL },
- { SIG_SCI32, 6, MAP_CALL(StringCopy), "[or]i[or]ii", NULL },
- { SIG_SCI32, 7, MAP_CALL(StringCompare), "[or][or](i)", NULL },
-
- // =SCI2, SCI2.1 Early and SCI2.1 Middle=
- { SIG_UNTIL_SCI21MID, 8, MAP_CALL(StringDup), "[or]", NULL },
- // TODO: This gets called with null references in Torin. Check if this is correct, or it's
- // caused by missing functionality
- { SIG_UNTIL_SCI21MID, 9, MAP_CALL(StringGetData), "[or0]", NULL },
- { SIG_UNTIL_SCI21MID, 10, MAP_CALL(StringLen), "[or]", NULL },
- { SIG_UNTIL_SCI21MID, 11, MAP_CALL(StringPrintf), "[or](.*)", NULL },
- { SIG_UNTIL_SCI21MID, 12, MAP_CALL(StringPrintfBuf), "[or](.*)", NULL },
- { SIG_UNTIL_SCI21MID, 13, MAP_CALL(StringAtoi), "[or]", NULL },
- { SIG_UNTIL_SCI21MID, 14, MAP_CALL(StringTrim), "[or]i", NULL },
- { SIG_UNTIL_SCI21MID, 15, MAP_CALL(StringUpper), "[or]", NULL },
- { SIG_UNTIL_SCI21MID, 16, MAP_CALL(StringLower), "[or]", NULL },
- // the following 2 are unknown atm (happen in Phantasmagoria)
- // possibly translate?
- { SIG_UNTIL_SCI21MID, 17, MAP_CALL(StringTrn), "[or]", NULL },
- { SIG_UNTIL_SCI21MID, 18, MAP_CALL(StringTrnExclude), "[or]", NULL },
-
- // SCI2.1 Late + SCI3 - kStringDup + kStringGetData were removed
- { SIG_SINCE_SCI21LATE, 8, MAP_CALL(StringLen), "[or]", NULL },
- { SIG_SINCE_SCI21LATE, 9, MAP_CALL(StringPrintf), "[or](.*)", NULL },
- { SIG_SINCE_SCI21LATE,10, MAP_CALL(StringPrintfBuf), "[or](.*)", NULL },
- { SIG_SINCE_SCI21LATE,11, MAP_CALL(StringAtoi), "[or]", NULL },
- { SIG_SINCE_SCI21LATE,12, MAP_CALL(StringTrim), "[or]i", NULL },
- { SIG_SINCE_SCI21LATE,13, MAP_CALL(StringUpper), "[or]", NULL },
- { SIG_SINCE_SCI21LATE,14, MAP_CALL(StringLower), "[or]", NULL },
- { SIG_SINCE_SCI21LATE,15, MAP_CALL(StringTrn), "[or]", NULL },
- { SIG_SINCE_SCI21LATE,16, MAP_CALL(StringTrnExclude), "[or]", NULL },
+ // every single copy of script 64918 in SCI2 through 2.1mid calls StringNew
+ // with a second type argument which is unused (new strings are always type
+ // 3)
+ { SIG_THRU_SCI21MID, 0, MAP_CALL(StringNew), "i(i)", NULL },
+ { SIG_THRU_SCI21MID, 1, MAP_CALL(ArrayGetSize), "r", NULL },
+ { SIG_THRU_SCI21MID, 2, MAP_CALL(StringGetChar), "ri", NULL },
+ { SIG_THRU_SCI21MID, 3, MAP_CALL(ArraySetElements), "ri(i*)", kArraySetElements_workarounds },
+ { SIG_THRU_SCI21MID, 4, MAP_CALL(StringFree), "[0r]", NULL },
+ { SIG_THRU_SCI21MID, 5, MAP_CALL(ArrayFill), "rii", NULL },
+ { SIG_THRU_SCI21MID, 6, MAP_CALL(ArrayCopy), "ririi", NULL },
+ { SIG_SCI32, 7, MAP_CALL(StringCompare), "rr(i)", NULL },
+
+ { SIG_THRU_SCI21MID, 8, MAP_CALL(ArrayDuplicate), "r", NULL },
+ { SIG_THRU_SCI21MID, 9, MAP_CALL(StringGetData), "[0or]", NULL },
+ { SIG_THRU_SCI21MID, 10, MAP_CALL(StringLength), "r", NULL },
+ { SIG_THRU_SCI21MID, 11, MAP_CALL(StringFormat), "r(.*)", NULL },
+ { SIG_THRU_SCI21MID, 12, MAP_CALL(StringFormatAt), "r[ro](.*)", NULL },
+ { SIG_THRU_SCI21MID, 13, MAP_CALL(StringToInteger), "r", NULL },
+ { SIG_THRU_SCI21MID, 14, MAP_CALL(StringTrim), "ri(i)", NULL },
+ { SIG_THRU_SCI21MID, 15, MAP_CALL(StringToUpperCase), "r", NULL },
+ { SIG_THRU_SCI21MID, 16, MAP_CALL(StringToLowerCase), "r", NULL },
+ { SIG_THRU_SCI21MID, 17, MAP_CALL(StringReplaceSubstring), "rrrr", NULL },
+ { SIG_THRU_SCI21MID, 18, MAP_CALL(StringReplaceSubstringEx), "rrrr", NULL },
+
+ { SIG_SINCE_SCI21LATE, 8, MAP_CALL(StringLength), "r", NULL },
+ { SIG_SINCE_SCI21LATE, 9, MAP_CALL(StringFormat), "r(.*)", NULL },
+ { SIG_SINCE_SCI21LATE,10, MAP_CALL(StringFormatAt), "rr(.*)", NULL },
+ { SIG_SINCE_SCI21LATE,11, MAP_CALL(StringToInteger), "r", NULL },
+ { SIG_SINCE_SCI21LATE,12, MAP_CALL(StringTrim), "ri(i)", NULL },
+ { SIG_SINCE_SCI21LATE,13, MAP_CALL(StringToUpperCase), "r", NULL },
+ { SIG_SINCE_SCI21LATE,14, MAP_CALL(StringToLowerCase), "r", NULL },
+ { SIG_SINCE_SCI21LATE,15, MAP_CALL(StringReplaceSubstring), "rrrr", NULL },
+ { SIG_SINCE_SCI21LATE,16, MAP_CALL(StringReplaceSubstringEx), "rrrr", NULL },
SCI_SUBOPENTRY_TERMINATOR
};
@@ -613,10 +629,13 @@ static SciKernelMapEntry s_kernelMap[] = {
{ "CelHigh", kCelHigh32, SIG_SCI32, SIGFOR_ALL, "iii", NULL, NULL },
{ "CelWide", kCelWide32, SIG_SCI32, SIGFOR_ALL, "iii", NULL, kCelWide_workarounds },
#endif
- { MAP_CALL(CheckFreeSpace), SIG_SCI32, SIGFOR_ALL, "r.*", NULL, NULL },
+ { MAP_CALL(CheckFreeSpace), SIG_THRU_SCI21EARLY, SIGFOR_ALL, "r(i)", NULL, NULL },
{ MAP_CALL(CheckFreeSpace), SIG_SCI11, SIGFOR_ALL, "r(i)", NULL, NULL },
- { MAP_CALL(CheckFreeSpace), SIG_EVERYWHERE, "r", NULL, NULL },
- { MAP_CALL(CheckSaveGame), SIG_EVERYWHERE, ".*", NULL, NULL },
+ { MAP_CALL(CheckFreeSpace), SIG_SCI16, SIGFOR_ALL, "r", NULL, NULL },
+#ifdef ENABLE_SCI32
+ { "CheckSaveGame", kCheckSaveGame32, SIG_THRU_SCI21EARLY, SIGFOR_ALL, "ri[r0]", NULL, NULL },
+#endif
+ { MAP_CALL(CheckSaveGame), SIG_SCI16, SIGFOR_ALL, ".*", NULL, NULL },
{ MAP_CALL(Clone), SIG_EVERYWHERE, "o", NULL, NULL },
{ MAP_CALL(CoordPri), SIG_EVERYWHERE, "i(i)", NULL, NULL },
{ MAP_CALL(CosDiv), SIG_EVERYWHERE, "ii", NULL, NULL },
@@ -632,7 +651,7 @@ static SciKernelMapEntry s_kernelMap[] = {
{ MAP_CALL(DisposeWindow), SIG_EVERYWHERE, "i(i)", NULL, NULL },
{ MAP_CALL(DoAudio), SCI_VERSION_NONE, SCI_VERSION_2, SIGFOR_ALL, "i(.*)", NULL, NULL }, // subop
#ifdef ENABLE_SCI32
- { "DoAudio", kDoAudio32, SIG_SINCE_SCI21, SIGFOR_ALL, "(.*)", kDoAudio_subops, NULL },
+ { "DoAudio", kDoAudio32, SIG_SINCE_SCI21, SIGFOR_ALL, "(.*)", kDoAudio_subops, NULL },
#endif
{ MAP_CALL(DoAvoider), SIG_EVERYWHERE, "o(i)", NULL, NULL },
{ MAP_CALL(DoBresen), SIG_EVERYWHERE, "o", NULL, NULL },
@@ -666,15 +685,23 @@ static SciKernelMapEntry s_kernelMap[] = {
{ MAP_CALL(GetMenu), SIG_EVERYWHERE, "i.", NULL, NULL },
{ MAP_CALL(GetMessage), SIG_EVERYWHERE, "iiir", NULL, NULL },
{ MAP_CALL(GetPort), SIG_EVERYWHERE, "", NULL, NULL },
- { MAP_CALL(GetSaveDir), SIG_SCI32, SIGFOR_ALL, "(r*)", NULL, NULL },
- { MAP_CALL(GetSaveDir), SIG_EVERYWHERE, "", NULL, NULL },
+#ifdef ENABLE_SCI32
+ { MAP_CALL(GetSaveDir), SIG_THRU_SCI21EARLY, SIGFOR_ALL, "(r)", NULL, NULL },
+#endif
+ { MAP_CALL(GetSaveDir), SIG_SCI16, SIGFOR_ALL, "", NULL, NULL },
+#ifdef ENABLE_SCI32
+ { "GetSaveFiles", kGetSaveFiles32, SIG_THRU_SCI21EARLY, SIGFOR_ALL, "rrr", NULL, NULL },
+#endif
{ MAP_CALL(GetSaveFiles), SIG_EVERYWHERE, "rrr", NULL, NULL },
{ MAP_CALL(GetTime), SIG_EVERYWHERE, "(i)", NULL, NULL },
{ MAP_CALL(GlobalToLocal), SIG_SCI16, SIGFOR_ALL, "o", NULL, NULL },
#ifdef ENABLE_SCI32
{ "GlobalToLocal", kGlobalToLocal32, SIG_SCI32, SIGFOR_ALL, "oo", NULL, NULL },
#endif
- { MAP_CALL(Graph), SIG_EVERYWHERE, NULL, kGraph_subops, NULL },
+ { MAP_CALL(Graph), SIG_SCI16, SIGFOR_ALL, NULL, kGraph_subops, NULL },
+#ifdef ENABLE_SCI32
+ { MAP_EMPTY(Graph), SIG_SCI32, SIGFOR_ALL, "(.*)", NULL, NULL },
+#endif
{ MAP_CALL(HaveMouse), SIG_EVERYWHERE, "", NULL, NULL },
{ MAP_CALL(HiliteControl), SIG_EVERYWHERE, "o", NULL, NULL },
{ MAP_CALL(InitBresen), SIG_EVERYWHERE, "o(i)", NULL, NULL },
@@ -729,9 +756,15 @@ static SciKernelMapEntry s_kernelMap[] = {
{ MAP_CALL(ResCheck), SIG_EVERYWHERE, "ii(iiii)", NULL, NULL },
{ MAP_CALL(RespondsTo), SIG_EVERYWHERE, ".i", NULL, NULL },
{ MAP_CALL(RestartGame), SIG_EVERYWHERE, "", NULL, NULL },
+#ifdef ENABLE_SCI32
+ { "RestoreGame", kRestoreGame32, SIG_THRU_SCI21EARLY, SIGFOR_ALL, "[r0]i[r0]", NULL, NULL },
+#endif
{ MAP_CALL(RestoreGame), SIG_EVERYWHERE, "[r0]i[r0]", NULL, NULL },
{ MAP_CALL(Said), SIG_EVERYWHERE, "[r0]", NULL, NULL },
- { MAP_CALL(SaveGame), SIG_EVERYWHERE, "[r0]i[r0](r0)", NULL, NULL },
+#ifdef ENABLE_SCI32
+ { "SaveGame", kSaveGame32, SIG_THRU_SCI21EARLY, SIGFOR_ALL, "[r0]i[r0][r0]", NULL, NULL },
+#endif
+ { MAP_CALL(SaveGame), SIG_SCI16, SIGFOR_ALL, "[r0]i[r0](r0)", NULL, NULL },
{ MAP_CALL(ScriptID), SIG_EVERYWHERE, "[io](i)", NULL, NULL },
{ MAP_CALL(SetCursor), SIG_SCI11, SIGFOR_ALL, "i(i)(i)(i)(iiiiii)", NULL, NULL },
{ MAP_CALL(SetCursor), SIG_SCI16, SIGFOR_ALL, "i(i)(i)(i)(i)", NULL, kSetCursor_workarounds },
@@ -805,13 +838,13 @@ static SciKernelMapEntry s_kernelMap[] = {
#ifdef ENABLE_SCI32
// SCI2 Kernel Functions
// TODO: whoever knows his way through those calls, fix the signatures.
- { "TextSize", kTextSize32, SIG_UNTIL_SCI21EARLY, SIGFOR_ALL, "r[r0]i(i)", NULL, NULL },
- { MAP_DUMMY(TextColors), SIG_UNTIL_SCI21EARLY, SIGFOR_ALL, "(.*)", NULL, NULL },
- { MAP_DUMMY(TextFonts), SIG_UNTIL_SCI21EARLY, SIGFOR_ALL, "(.*)", NULL, NULL },
+ { "TextSize", kTextSize32, SIG_THRU_SCI21EARLY, SIGFOR_ALL, "r[r0]i(i)", NULL, NULL },
+ { MAP_DUMMY(TextColors), SIG_THRU_SCI21EARLY, SIGFOR_ALL, "(.*)", NULL, NULL },
+ { MAP_DUMMY(TextFonts), SIG_THRU_SCI21EARLY, SIGFOR_ALL, "(.*)", NULL, NULL },
{ MAP_CALL(AddPlane), SIG_EVERYWHERE, "o", NULL, NULL },
{ MAP_CALL(AddScreenItem), SIG_EVERYWHERE, "o", NULL, NULL },
- { MAP_CALL(Array), SIG_EVERYWHERE, "(.*)", NULL, NULL },
+ { MAP_CALL(Array), SIG_EVERYWHERE, "i(.*)", kArray_subops, NULL },
{ MAP_CALL(CreateTextBitmap), SIG_EVERYWHERE, "i(.*)", NULL, NULL },
{ MAP_CALL(DeletePlane), SIG_EVERYWHERE, "o", NULL, NULL },
{ MAP_CALL(DeleteScreenItem), SIG_EVERYWHERE, "o", NULL, NULL },
@@ -840,8 +873,8 @@ static SciKernelMapEntry s_kernelMap[] = {
{ MAP_CALL(UpdateScreenItem), SIG_EVERYWHERE, "o", NULL, NULL },
{ MAP_CALL(ObjectIntersect), SIG_EVERYWHERE, "oo", NULL, NULL },
{ MAP_CALL(EditText), SIG_EVERYWHERE, "o", NULL, NULL },
- { MAP_CALL(MakeSaveCatName), SIG_EVERYWHERE, "rr", NULL, NULL },
- { MAP_CALL(MakeSaveFileName), SIG_EVERYWHERE, "rri", NULL, NULL },
+ { MAP_CALL(MakeSaveCatName), SIG_THRU_SCI21EARLY, SIGFOR_ALL, "rr", NULL, NULL },
+ { MAP_CALL(MakeSaveFileName), SIG_THRU_SCI21EARLY, SIGFOR_ALL, "rri", NULL, NULL },
{ MAP_CALL(SetScroll), SIG_EVERYWHERE, "oiiii(i)(i)", NULL, NULL },
{ MAP_CALL(PalCycle), SIG_EVERYWHERE, "(.*)", kPalCycle_subops, NULL },
@@ -879,9 +912,9 @@ static SciKernelMapEntry s_kernelMap[] = {
{ MAP_DUMMY(MarkMemory), SIG_EVERYWHERE, "(.*)", NULL, NULL },
{ MAP_DUMMY(GetHighItemPri), SIG_EVERYWHERE, "(.*)", NULL, NULL },
{ MAP_DUMMY(ShowStylePercent), SIG_EVERYWHERE, "(.*)", NULL, NULL },
- { MAP_DUMMY(InvertRect), SIG_UNTIL_SCI21EARLY, SIGFOR_ALL, "(.*)", NULL, NULL },
+ { MAP_DUMMY(InvertRect), SIG_THRU_SCI21EARLY, SIGFOR_ALL, "(.*)", NULL, NULL },
{ MAP_DUMMY(InputText), SIG_EVERYWHERE, "(.*)", NULL, NULL },
- { MAP_CALL(TextWidth), SIG_UNTIL_SCI21EARLY, SIGFOR_ALL, "ri", NULL, NULL },
+ { MAP_CALL(TextWidth), SIG_THRU_SCI21EARLY, SIGFOR_ALL, "ri", NULL, NULL },
{ MAP_DUMMY(PointSize), SIG_EVERYWHERE, "(.*)", NULL, NULL },
// SCI2.1 Kernel Functions
@@ -917,14 +950,14 @@ static SciKernelMapEntry s_kernelMap[] = {
// stub in the original interpreters, but it gets called by the game scripts.
// Usually, it gets called with a string (which is the output format) and a
// variable number of parameters
- { MAP_EMPTY(PrintDebug), SIG_EVERYWHERE, "(.*)", NULL, NULL },
+ { MAP_CALL(PrintDebug), SIG_SCI32, SIGFOR_ALL, "r(.*)", NULL, NULL },
// SetWindowsOption is used to set Windows specific options, like for example the title bar visibility of
// the game window in Phantasmagoria 2. We ignore these settings completely.
- { MAP_EMPTY(SetWindowsOption), SIG_EVERYWHERE, "ii", NULL, NULL },
+ { MAP_EMPTY(SetWindowsOption), SIG_EVERYWHERE, "ii", NULL, NULL },
// Debug function called whenever the current room changes
- { MAP_EMPTY(NewRoom), SIG_EVERYWHERE, "(.*)", NULL, NULL },
+ { MAP_EMPTY(NewRoom), SIG_EVERYWHERE, "(.*)", NULL, NULL },
// Unused / debug SCI2.1 unused functions, always mapped to kDummy
@@ -957,6 +990,7 @@ static SciKernelMapEntry s_kernelMap[] = {
// SetHotRectangles - used by Phantasmagoria 1, script 64981 (used in the chase scene)
// <lskovlun> The idea, if I understand correctly, is that the engine generates events
// of a special HotRect type continuously when the mouse is on that rectangle
+ { MAP_CALL(SetHotRectangles), SIG_SINCE_SCI21MID, SIGFOR_ALL, "i(r)", NULL, NULL },
// Used by SQ6 to scroll through the inventory via the up/down buttons
{ MAP_CALL(MovePlaneItems), SIG_SINCE_SCI21, SIGFOR_ALL, "oii(i)", NULL, NULL },
diff --git a/engines/sci/engine/kevent.cpp b/engines/sci/engine/kevent.cpp
index 9250e0fc13..a00630622e 100644
--- a/engines/sci/engine/kevent.cpp
+++ b/engines/sci/engine/kevent.cpp
@@ -73,7 +73,7 @@ reg_t kGetEvent(EngineState *s, int argc, reg_t *argv) {
g_debug_simulated_key = 0;
return make_reg(0, 1);
}
-
+
curEvent = g_sci->getEventManager()->getSciEvent(mask);
if (s->_delayedRestoreGame) {
@@ -236,7 +236,7 @@ reg_t kGetEvent(EngineState *s, int argc, reg_t *argv) {
// check bugs #3058865 and #3127824
if (s->_gameIsBenchmarking) {
// Game is benchmarking, don't add a delay
- } else {
+ } else if (getSciVersion() < SCI_VERSION_2) {
g_system->delayMillis(10);
}
@@ -369,6 +369,30 @@ reg_t kLocalToGlobal32(EngineState *s, int argc, reg_t *argv) {
return make_reg(0, visible);
}
+
+reg_t kSetHotRectangles(EngineState *s, int argc, reg_t *argv) {
+ if (argc == 1) {
+ g_sci->getEventManager()->setHotRectanglesActive((bool)argv[0].toUint16());
+ return s->r_acc;
+ }
+
+ const int16 numRects = argv[0].toSint16();
+ SciArray &hotRects = *s->_segMan->lookupArray(argv[1]);
+
+ Common::Array<Common::Rect> rects;
+ rects.resize(numRects);
+
+ for (int16 i = 0; i < numRects; ++i) {
+ rects[i].left = hotRects.getAsInt16(i * 4);
+ rects[i].top = hotRects.getAsInt16(i * 4 + 1);
+ rects[i].right = hotRects.getAsInt16(i * 4 + 2) + 1;
+ rects[i].bottom = hotRects.getAsInt16(i * 4 + 3) + 1;
+ }
+
+ g_sci->getEventManager()->setHotRectanglesActive(true);
+ g_sci->getEventManager()->setHotRectangles(rects);
+ return s->r_acc;
+}
#endif
} // End of namespace Sci
diff --git a/engines/sci/engine/kfile.cpp b/engines/sci/engine/kfile.cpp
index e8b9d0461d..6aad256664 100644
--- a/engines/sci/engine/kfile.cpp
+++ b/engines/sci/engine/kfile.cpp
@@ -51,6 +51,7 @@ extern FileHandle *getFileFromHandle(EngineState *s, uint handle);
extern int fgets_wrapper(EngineState *s, char *dest, int maxsize, int handle);
extern void listSavegames(Common::Array<SavegameDesc> &saves);
extern int findSavegame(Common::Array<SavegameDesc> &saves, int16 savegameId);
+extern bool fillSavegameDesc(const Common::String &filename, SavegameDesc *desc);
/**
* Writes the cwd to the supplied address and returns the address in acc.
@@ -158,33 +159,37 @@ reg_t kDeviceInfo(EngineState *s, int argc, reg_t *argv) {
}
reg_t kCheckFreeSpace(EngineState *s, int argc, reg_t *argv) {
- if (argc > 1) {
- // SCI1.1/SCI32
- // TODO: don't know if those are right for SCI32 as well
- // Please note that sierra sci supported both calls either w/ or w/o opcode in SCI1.1
- switch (argv[1].toUint16()) {
- case 0: // return saved game size
- return make_reg(0, 0); // we return 0
-
- case 1: // return free harddisc space (shifted right somehow)
- return make_reg(0, 0x7fff); // we return maximum
-
- case 2: // same as call w/o opcode
- break;
- return make_reg(0, 1);
-
- default:
- error("kCheckFreeSpace: called with unknown sub-op %d", argv[1].toUint16());
- }
+ // A file path to test is also passed to this function as a separate
+ // argument, but we do not actually check anything, so it is unused
+
+ enum {
+ kSaveGameSize = 0,
+ kFreeDiskSpace = 1,
+ kEnoughSpaceToSave = 2
+ };
+
+ int16 subop;
+ // In SCI2.1mid, the call is moved into kFileIO and the arguments are
+ // flipped
+ if (getSciVersion() >= SCI_VERSION_2_1_MIDDLE) {
+ subop = argc > 0 ? argv[0].toSint16() : 2;
+ } else {
+ subop = argc > 1 ? argv[1].toSint16() : 2;
}
- Common::String path = s->_segMan->getString(argv[0]);
+ switch (subop) {
+ case kSaveGameSize:
+ return make_reg(0, 0);
- debug(3, "kCheckFreeSpace(%s)", path.c_str());
- // We simply always pretend that there is enough space. The alternative
- // would be to write a big test file, which is not nice on systems where
- // doing so is very slow.
- return make_reg(0, 1);
+ case kFreeDiskSpace: // in KiB; up to 32MiB maximum
+ return make_reg(0, 0x7fff);
+
+ case kEnoughSpaceToSave:
+ return make_reg(0, 1);
+
+ default:
+ error("kCheckFreeSpace: called with unknown sub-op %d", subop);
+ }
}
reg_t kValidPath(EngineState *s, int argc, reg_t *argv) {
@@ -224,6 +229,45 @@ reg_t kGetSavedCD(EngineState *s, int argc, reg_t *argv) {
// ---- FileIO operations -----------------------------------------------------
+#ifdef ENABLE_SCI32
+static bool isSaveCatalogue(const Common::String &name) {
+ return name == "autosave.cat" || name.hasSuffix("sg.cat");
+}
+
+// SCI32 save game scripts check for, and write directly to, the save game
+// catalogue. Since ScummVM does not use these catalogues, when looking for a
+// catalogue, we instead check for save games within ScummVM that are logically
+// equivalent to the behaviour of SSCI.
+static bool saveCatalogueExists(const Common::String &name) {
+ bool exists = false;
+ Common::SaveFileManager *saveFileMan = g_sci->getSaveFileManager();
+
+ // There will always be one save game in some games, the "new game"
+ // game, which should be ignored when deciding if there are any save
+ // games available
+ uint numPermanentSaves;
+ switch (g_sci->getGameId()) {
+ case GID_TORIN:
+ case GID_LSL7:
+ case GID_LIGHTHOUSE:
+ numPermanentSaves = 1;
+ break;
+ default:
+ numPermanentSaves = 0;
+ break;
+ }
+
+ // Torin uses autosave.cat; LSL7 uses autosvsg.cat
+ if (name == "autosave.cat" || name == "autosvsg.cat") {
+ exists = !saveFileMan->listSavefiles(g_sci->getSavegameName(0)).empty();
+ } else {
+ exists = saveFileMan->listSavefiles(g_sci->getSavegamePattern()).size() > numPermanentSaves;
+ }
+
+ return exists;
+}
+#endif
+
reg_t kFileIO(EngineState *s, int argc, reg_t *argv) {
if (!s)
return make_reg(0, getSciVersion());
@@ -233,9 +277,13 @@ reg_t kFileIO(EngineState *s, int argc, reg_t *argv) {
reg_t kFileIOOpen(EngineState *s, int argc, reg_t *argv) {
Common::String name = s->_segMan->getString(argv[0]);
- // SCI32 can call K_FILEIO_OPEN with only one argument. It seems to
- // just be checking if it exists.
- int mode = (argc < 2) ? (int)_K_FILE_MODE_OPEN_OR_FAIL : argv[1].toUint16();
+ if (name.empty()) {
+ // Happens many times during KQ1 (e.g. when typing something)
+ debugC(kDebugLevelFile, "Attempted to open a file with an empty filename");
+ return SIGNAL_REG;
+ }
+
+ int mode = argv[1].toUint16();
bool unwrapFilename = true;
// SQ4 floppy prepends /\ to the filenames
@@ -253,94 +301,158 @@ reg_t kFileIOOpen(EngineState *s, int argc, reg_t *argv) {
return SIGNAL_REG;
}
- // Torin's autosave system checks for the presence of autosave.cat
- // by opening it. Since we don't use .cat files, we instead check
- // for autosave.000 or autosave.001.
- //
- // The same logic is being followed for torinsg.cat - this shows
- // the "Open..." button when continuing a game.
- //
- // This has the added benefit of not detecting an SSCI autosave.cat
- // accompanying SSCI autosave files that we wouldn't be able to load.
- if (g_sci->getGameId() == GID_TORIN && (name == "autosave.cat" || name == "torinsg.cat")) {
- Common::SaveFileManager *saveFileMan = g_sci->getSaveFileManager();
- const Common::String pattern = (name == "autosave.cat") ? g_sci->wrapFilename("autosave.###") : g_sci->getSavegamePattern();
- bool exists = !saveFileMan->listSavefiles(pattern).empty();
- if (exists) {
- // Dummy handle. Torin only checks if this is SIGNAL_REG,
- // and calls kFileIOClose on it.
- return make_reg(0, VIRTUALFILE_HANDLE_SCI32SAVE);
- } else {
- return SIGNAL_REG;
+#ifdef ENABLE_SCI32
+ // GK1, GK2, KQ7, LSL6hires, Phant1, PQ4, PQ:SWAT, and SQ6 read in
+ // their game version from the VERSION file
+ if (name.compareToIgnoreCase("version") == 0) {
+ unwrapFilename = false;
+
+ // LSL6hires version is in a file with an empty extension
+ if (Common::File::exists(name + ".")) {
+ name += ".";
}
}
- if (name.empty()) {
- // Happens many times during KQ1 (e.g. when typing something)
- debugC(kDebugLevelFile, "Attempted to open a file with an empty filename");
- return SIGNAL_REG;
- }
- debugC(kDebugLevelFile, "kFileIO(open): %s, 0x%x", name.c_str(), mode);
+ if (g_sci->getGameId() == GID_SHIVERS && name.hasSuffix(".SG")) {
+ // Shivers stores the name and score of save games in separate %d.SG
+ // files, which are used by the save/load screen
+ if (mode == _K_FILE_MODE_OPEN_OR_CREATE || mode == _K_FILE_MODE_CREATE) {
+ // Suppress creation of the SG file, since it is not necessary
+ debugC(kDebugLevelFile, "Not creating unused file %s", name.c_str());
+ return SIGNAL_REG;
+ } else if (mode == _K_FILE_MODE_OPEN_OR_FAIL) {
+ // Create a virtual file containing the save game description
+ // and slot number, as the game scripts expect.
+ int saveNo;
+ sscanf(name.c_str(), "%d.SG", &saveNo);
+ saveNo += kSaveIdShift;
+
+ SavegameDesc save;
+ fillSavegameDesc(g_sci->getSavegameName(saveNo), &save);
+
+ Common::String score;
+ if (!save.highScore) {
+ score = Common::String::format("%u", save.lowScore);
+ } else {
+ score = Common::String::format("%u%03u", save.highScore, save.lowScore);
+ }
- if (name.hasPrefix("sciAudio\\")) {
- // fan-made sciAudio extension, don't create those files and instead return a virtual handle
- return make_reg(0, VIRTUALFILE_HANDLE_SCIAUDIO);
- }
+ const uint nameLength = strlen(save.name);
+ const uint size = nameLength + /* \r\n */ 2 + score.size();
+ char *buffer = (char *)malloc(size);
+ memcpy(buffer, save.name, nameLength);
+ buffer[nameLength] = '\r';
+ buffer[nameLength + 1] = '\n';
+ memcpy(buffer + nameLength + 2, score.c_str(), score.size());
-#ifdef ENABLE_SCI32
- // Shivers is trying to store savegame descriptions and current spots in
- // separate .SG files, which are hardcoded in the scripts.
- // Essentially, there is a normal save file, created by the executable
- // and an extra hardcoded save file, created by the game scripts, probably
- // because they didn't want to modify the save/load code to add the extra
- // information.
- // Each slot in the book then has two strings, the save description and a
- // description of the current spot that the player is at. Currently, the
- // spot strings are always empty (probably related to the unimplemented
- // kString subop 14, which gets called right before this call).
- // For now, we don't allow the creation of these files, which means that
- // all the spot descriptions next to each slot description will be empty
- // (they are empty anyway). Until a viable solution is found to handle these
- // extra files and until the spot description strings are initialized
- // correctly, we resort to virtual files in order to make the load screen
- // useable. Without this code it is unusable, as the extra information is
- // always saved to 0.SG for some reason, but on restore the correct file is
- // used. Perhaps the virtual ID is not taken into account when saving.
- //
- // Future TODO: maintain spot descriptions and show them too, ideally without
- // having to return to this logic of extra hardcoded files.
- if (g_sci->getGameId() == GID_SHIVERS && name.hasSuffix(".SG")) {
+ const uint handle = findFreeFileHandle(s);
+
+ s->_fileHandles[handle]._in = new Common::MemoryReadStream((byte *)buffer, size, DisposeAfterUse::YES);
+ s->_fileHandles[handle]._out = nullptr;
+ s->_fileHandles[handle]._name = "";
+
+ return make_reg(0, handle);
+ }
+ } else if (g_sci->getGameId() == GID_MOTHERGOOSEHIRES && name.hasSuffix(".DTA")) {
+ // MGDX stores the name and avatar ID in separate %d.DTA files, which
+ // are used by the save/load screen
if (mode == _K_FILE_MODE_OPEN_OR_CREATE || mode == _K_FILE_MODE_CREATE) {
- // Game scripts are trying to create a file with the save
- // description, stop them here
+ // Suppress creation of the DTA file, since it is not necessary
debugC(kDebugLevelFile, "Not creating unused file %s", name.c_str());
return SIGNAL_REG;
} else if (mode == _K_FILE_MODE_OPEN_OR_FAIL) {
// Create a virtual file containing the save game description
// and slot number, as the game scripts expect.
- int slotNumber;
- sscanf(name.c_str(), "%d.SG", &slotNumber);
+ int saveNo;
+ sscanf(name.c_str(), "%d.DTA", &saveNo);
+ saveNo += kSaveIdShift;
- Common::Array<SavegameDesc> saves;
- listSavegames(saves);
- int savegameNr = findSavegame(saves, slotNumber - SAVEGAMEID_OFFICIALRANGE_START);
+ SavegameDesc save;
+ fillSavegameDesc(g_sci->getSavegameName(saveNo), &save);
- int size = strlen(saves[savegameNr].name) + 2;
- char *buf = (char *)malloc(size);
- strcpy(buf, saves[savegameNr].name);
- buf[size - 1] = 0; // Spot description (empty)
+ const Common::String avatarId = Common::String::format("%02d", save.avatarId);
+ const uint nameLength = strlen(save.name);
+ const uint size = nameLength + /* \r\n */ 2 + avatarId.size() + 1;
+ char *buffer = (char *)malloc(size);
+ memcpy(buffer, save.name, nameLength);
+ buffer[nameLength] = '\r';
+ buffer[nameLength + 1] = '\n';
+ memcpy(buffer + nameLength + 2, avatarId.c_str(), avatarId.size() + 1);
- uint handle = findFreeFileHandle(s);
+ const uint handle = findFreeFileHandle(s);
- s->_fileHandles[handle]._in = new Common::MemoryReadStream((byte *)buf, size, DisposeAfterUse::YES);
+ s->_fileHandles[handle]._in = new Common::MemoryReadStream((byte *)buffer, size, DisposeAfterUse::YES);
s->_fileHandles[handle]._out = nullptr;
s->_fileHandles[handle]._name = "";
return make_reg(0, handle);
}
+ } else if (g_sci->getGameId() == GID_KQ7) {
+ // KQ7 creates a temp.tmp file to perform an atomic rewrite of the
+ // catalogue, but since we do not create catalogues for most SCI32
+ // games, ignore the write
+ if (name == "temp.tmp") {
+ return make_reg(0, VIRTUALFILE_HANDLE_SCI32SAVE);
+ }
+
+ // KQ7 tries to read out game information from catalogues directly
+ // instead of using the standard kSaveGetFiles function
+ if (name == "kq7cdsg.cat") {
+ if (mode == _K_FILE_MODE_OPEN_OR_CREATE || mode == _K_FILE_MODE_CREATE) {
+ // Suppress creation of the catalogue file, since it is not necessary
+ debugC(kDebugLevelFile, "Not creating unused file %s", name.c_str());
+ return SIGNAL_REG;
+ } else if (mode == _K_FILE_MODE_OPEN_OR_FAIL) {
+ Common::Array<SavegameDesc> saves;
+ listSavegames(saves);
+
+ const uint recordSize = sizeof(int16) + SCI_MAX_SAVENAME_LENGTH;
+ const uint numSaves = MIN<uint>(saves.size(), 10);
+ const uint size = numSaves * recordSize + /* terminator */ 2;
+ byte *const buffer = (byte *)malloc(size);
+
+ byte *out = buffer;
+ for (uint i = 0; i < numSaves; ++i) {
+ WRITE_UINT16(out, saves[i].id - kSaveIdShift);
+ Common::strlcpy((char *)out + sizeof(int16), saves[i].name, SCI_MAX_SAVENAME_LENGTH);
+ out += recordSize;
+ }
+ WRITE_UINT16(out, 0xFFFF);
+
+ const uint handle = findFreeFileHandle(s);
+ s->_fileHandles[handle]._in = new Common::MemoryReadStream(buffer, size, DisposeAfterUse::YES);
+ s->_fileHandles[handle]._out = nullptr;
+ s->_fileHandles[handle]._name = "";
+
+ return make_reg(0, handle);
+ }
+ }
+ }
+
+ // See kMakeSaveCatName
+ if (name == "fake.cat") {
+ return make_reg(0, VIRTUALFILE_HANDLE_SCI32SAVE);
+ }
+
+ if (isSaveCatalogue(name)) {
+ const bool exists = saveCatalogueExists(name);
+ if (exists) {
+ // Dummy handle is used to represent the catalogue and ignore any
+ // direct game script writes
+ return make_reg(0, VIRTUALFILE_HANDLE_SCI32SAVE);
+ } else {
+ return SIGNAL_REG;
+ }
}
#endif
+ debugC(kDebugLevelFile, "kFileIO(open): %s, 0x%x", name.c_str(), mode);
+
+ if (name.hasPrefix("sciAudio\\")) {
+ // fan-made sciAudio extension, don't create those files and instead return a virtual handle
+ return make_reg(0, VIRTUALFILE_HANDLE_SCIAUDIO);
+ }
+
// QFG import rooms get a virtual filelisting instead of an actual one
if (g_sci->inQfGImportRoom()) {
// We need to find out what the user actually selected, "savedHeroes" is
@@ -363,7 +475,7 @@ reg_t kFileIOClose(EngineState *s, int argc, reg_t *argv) {
if (handle >= VIRTUALFILE_HANDLE_START) {
// it's a virtual handle? ignore it
- return SIGNAL_REG;
+ return getSciVersion() >= SCI_VERSION_2 ? TRUE_REG : SIGNAL_REG;
}
FileHandle *f = getFileFromHandle(s, handle);
@@ -371,7 +483,7 @@ reg_t kFileIOClose(EngineState *s, int argc, reg_t *argv) {
f->close();
if (getSciVersion() <= SCI_VERSION_0_LATE)
return s->r_acc; // SCI0 semantics: no value returned
- return SIGNAL_REG;
+ return getSciVersion() >= SCI_VERSION_2 ? TRUE_REG : SIGNAL_REG;
}
if (getSciVersion() <= SCI_VERSION_0_LATE)
@@ -403,18 +515,37 @@ reg_t kFileIOReadRaw(EngineState *s, int argc, reg_t *argv) {
reg_t kFileIOWriteRaw(EngineState *s, int argc, reg_t *argv) {
uint16 handle = argv[0].toUint16();
uint16 size = argv[2].toUint16();
+
+#ifdef ENABLE_SCI32
+ if (handle == VIRTUALFILE_HANDLE_SCI32SAVE) {
+ return make_reg(0, size);
+ }
+#endif
+
char *buf = new char[size];
+ uint bytesWritten = 0;
bool success = false;
s->_segMan->memcpy((byte *)buf, argv[1], size);
debugC(kDebugLevelFile, "kFileIO(writeRaw): %d, %d", handle, size);
FileHandle *f = getFileFromHandle(s, handle);
if (f) {
- f->_out->write(buf, size);
- success = true;
+ bytesWritten = f->_out->write(buf, size);
+ success = !f->_out->err();
}
delete[] buf;
+
+#ifdef ENABLE_SCI32
+ if (getSciVersion() >= SCI_VERSION_2) {
+ if (!success) {
+ return SIGNAL_REG;
+ }
+
+ return make_reg(0, bytesWritten);
+ }
+#endif
+
if (success)
return NULL_REG;
return make_reg(0, 6); // DOS - invalid handle
@@ -444,19 +575,35 @@ reg_t kFileIOUnlink(EngineState *s, int argc, reg_t *argv) {
int savedir_nr = saves[slotNum].id;
name = g_sci->getSavegameName(savedir_nr);
result = saveFileMan->removeSavefile(name);
+#ifdef ENABLE_SCI32
} else if (getSciVersion() >= SCI_VERSION_2) {
+ // Special case for KQ7, basically identical to the SQ4 case above,
+ // where the game hardcodes its save game names
+ if (name.hasPrefix("kq7cdsg.")) {
+ int saveNo = atoi(name.c_str() + name.size() - 3);
+ name = g_sci->getSavegameName(saveNo + kSaveIdShift);
+ }
+
// The file name may be already wrapped, so check both cases
result = saveFileMan->removeSavefile(name);
if (!result) {
const Common::String wrappedName = g_sci->wrapFilename(name);
result = saveFileMan->removeSavefile(wrappedName);
}
+#endif
} else {
const Common::String wrappedName = g_sci->wrapFilename(name);
result = saveFileMan->removeSavefile(wrappedName);
}
debugC(kDebugLevelFile, "kFileIO(unlink): %s", name.c_str());
+
+#ifdef ENABLE_SCI32
+ if (getSciVersion() >= SCI_VERSION_2) {
+ return make_reg(0, result);
+ }
+#endif
+
if (result)
return NULL_REG;
return make_reg(0, 2); // DOS - file not found error code
@@ -567,6 +714,12 @@ reg_t kFileIOExists(EngineState *s, int argc, reg_t *argv) {
return NULL_REG;
}
+#ifdef ENABLE_SCI32
+ if (isSaveCatalogue(name)) {
+ return saveCatalogueExists(name) ? TRUE_REG : NULL_REG;
+ }
+#endif
+
// TODO: It may apparently be worth caching the existence of
// phantsg.dir, and possibly even keeping it open persistently
@@ -662,7 +815,7 @@ reg_t kFileIOWriteByte(EngineState *s, int argc, reg_t *argv) {
FileHandle *f = getFileFromHandle(s, argv[0].toUint16());
if (f)
f->_out->writeByte(argv[1].toUint16() & 0xff);
- return s->r_acc; // FIXME: does this really not return anything?
+ return s->r_acc;
}
reg_t kFileIOReadWord(EngineState *s, int argc, reg_t *argv) {
@@ -676,27 +829,13 @@ reg_t kFileIOWriteWord(EngineState *s, int argc, reg_t *argv) {
FileHandle *f = getFileFromHandle(s, argv[0].toUint16());
if (f)
f->_out->writeUint16LE(argv[1].toUint16());
- return s->r_acc; // FIXME: does this really not return anything?
+ return s->r_acc;
}
-reg_t kFileIOCreateSaveSlot(EngineState *s, int argc, reg_t *argv) {
- // Used in Shivers when the user enters his name on the guest book
- // in the beginning to start the game.
-
- // Creates a new save slot, and returns if the operation was successful
-
- // Argument 0 denotes the save slot as a negative integer, 2 means "0"
- // Argument 1 is a string, with the file name, obtained from kSave(5).
- // The interpreter checks if it can be written to (by checking for free
- // disk space and write permissions)
-
- // We don't really use or need any of this...
-
- uint16 saveSlot = argv[0].toUint16();
- char* fileName = s->_segMan->lookupString(argv[1])->getRawData();
- warning("kFileIOCreateSaveSlot(%d, '%s')", saveSlot, fileName);
-
- return TRUE_REG; // slot creation was successful
+reg_t kFileIOGetCWD(EngineState *s, int argc, reg_t *argv) {
+ SciArray &fileName = *s->_segMan->lookupArray(argv[0]);
+ fileName.fromString("C:\\SIERRA\\");
+ return argv[0];
}
reg_t kFileIOIsValidDirectory(EngineState *s, int argc, reg_t *argv) {
@@ -721,6 +860,13 @@ reg_t kSave(EngineState *s, int argc, reg_t *argv) {
#endif
reg_t kSaveGame(EngineState *s, int argc, reg_t *argv) {
+ // slot 0 is the ScummVM auto-save slot, which is not used by us, but is
+ // still reserved
+ enum {
+ SAVEGAMESLOT_FIRST = 1,
+ SAVEGAMESLOT_LAST = 99
+ };
+
Common::String game_id = !argv[0].isNull() ? s->_segMan->getString(argv[0]) : "";
int16 virtualId = argv[1].toSint16();
int16 savegameId = -1;
@@ -736,13 +882,6 @@ reg_t kSaveGame(EngineState *s, int argc, reg_t *argv) {
return NULL_REG;
}
- // Torin has two sets of saves: autosave.### and torinsg.###, both with
- // their own slots and .cat file.
- // The autosave system uses autosave.000 and autosave.001.
- // It also checks the presence of autosave.cat to determine if it should
- // show the chapter selection menu on startup. (See kFileIOOpen.)
- bool torinAutosave = g_sci->getGameId() == GID_TORIN && game_id == "Autosave";
-
if (argv[0].isNull()) {
// Direct call, from a patched Game::save
if ((argv[1] != SIGNAL_REG) || (!argv[2].isNull()))
@@ -761,14 +900,6 @@ reg_t kSaveGame(EngineState *s, int argc, reg_t *argv) {
g_sci->_soundCmd->pauseAll(false); // unpause music (we can't have it paused during save)
if (savegameId < 0)
return NULL_REG;
-
- } else if (torinAutosave) {
- if (argv[2].isNull())
- error("kSaveGame: called with description being NULL");
- game_description = s->_segMan->getString(argv[2]);
- savegameId = virtualId;
-
- debug(3, "kSaveGame(%s,%d,%s,%s) [Torin autosave]", game_id.c_str(), virtualId, game_description.c_str(), version.c_str());
} else {
// Real call from script
if (argv[2].isNull())
@@ -844,10 +975,6 @@ reg_t kSaveGame(EngineState *s, int argc, reg_t *argv) {
Common::SaveFileManager *saveFileMan = g_sci->getSaveFileManager();
Common::OutSaveFile *out;
- if (torinAutosave) {
- filename = g_sci->wrapFilename(Common::String::format("autosave.%03d", savegameId));
- }
-
out = saveFileMan->openForSaving(filename);
if (!out) {
warning("Error opening savegame \"%s\" for writing", filename.c_str());
@@ -876,10 +1003,6 @@ reg_t kRestoreGame(EngineState *s, int argc, reg_t *argv) {
debug(3, "kRestoreGame(%s,%d)", game_id.c_str(), savegameId);
-
- // See comment in kSaveGame
- bool torinAutosave = g_sci->getGameId() == GID_TORIN && game_id == "Autosave";
-
if (argv[0].isNull()) {
// Direct call, either from launcher or from a patched Game::restore
if (savegameId == -1) {
@@ -895,7 +1018,7 @@ reg_t kRestoreGame(EngineState *s, int argc, reg_t *argv) {
pausedMusic = true;
}
// don't adjust ID of the saved game, it's already correct
- } else if (!torinAutosave) {
+ } else {
if (g_sci->getGameId() == GID_JONES) {
// Jones has one save slot only
savegameId = 0;
@@ -912,9 +1035,8 @@ reg_t kRestoreGame(EngineState *s, int argc, reg_t *argv) {
s->r_acc = NULL_REG; // signals success
Common::Array<SavegameDesc> saves;
- if (!torinAutosave)
- listSavegames(saves);
- if (!torinAutosave && findSavegame(saves, savegameId) == -1) {
+ listSavegames(saves);
+ if (findSavegame(saves, savegameId) == -1) {
s->r_acc = TRUE_REG;
warning("Savegame ID %d not found", savegameId);
} else {
@@ -922,10 +1044,6 @@ reg_t kRestoreGame(EngineState *s, int argc, reg_t *argv) {
Common::String filename = g_sci->getSavegameName(savegameId);
Common::SeekableReadStream *in;
- if (torinAutosave) {
- filename = g_sci->wrapFilename(Common::String::format("autosave.%03d", savegameId));
- }
-
in = saveFileMan->openForLoading(filename);
if (in) {
// found a savegame file
@@ -998,10 +1116,6 @@ reg_t kCheckSaveGame(EngineState *s, int argc, reg_t *argv) {
}
reg_t kGetSaveFiles(EngineState *s, int argc, reg_t *argv) {
- Common::String game_id = s->_segMan->getString(argv[0]);
-
- debug(3, "kGetSaveFiles(%s)", game_id.c_str());
-
// Scripts ask for current save files, we can assume that if afterwards they ask us to create a new slot they really
// mean new slot instead of overwriting the old one
s->_lastSaveVirtualId = SAVEGAMEID_OFFICIALRANGE_START;
@@ -1010,6 +1124,10 @@ reg_t kGetSaveFiles(EngineState *s, int argc, reg_t *argv) {
listSavegames(saves);
uint totalSaves = MIN<uint>(saves.size(), MAX_SAVEGAME_NR);
+ Common::String game_id = s->_segMan->getString(argv[0]);
+
+ debug(3, "kGetSaveFiles(%s)", game_id.c_str());
+
reg_t *slot = s->_segMan->derefRegPtr(argv[2], totalSaves);
if (!slot) {
@@ -1037,46 +1155,205 @@ reg_t kGetSaveFiles(EngineState *s, int argc, reg_t *argv) {
#ifdef ENABLE_SCI32
-reg_t kMakeSaveCatName(EngineState *s, int argc, reg_t *argv) {
- // Normally, this creates the name of the save catalogue/directory to save into.
- // First parameter is the string to save the result into. Second is a string
- // with game parameters. We don't have a use for this at all, as we have our own
- // savegame directory management, thus we always return an empty string.
- return argv[0];
+reg_t kSaveGame32(EngineState *s, int argc, reg_t *argv) {
+ const bool isScummVMSave = argv[0].isNull();
+ Common::String gameName = "";
+ int16 saveNo;
+ Common::String saveDescription;
+ Common::String gameVersion = (argc <= 3 || argv[3].isNull()) ? "" : s->_segMan->getString(argv[3]);
+
+ if (isScummVMSave) {
+ // ScummVM call, from a patched Game::save
+ g_sci->_soundCmd->pauseAll(true);
+ GUI::SaveLoadChooser dialog(_("Save game:"), _("Save"), true);
+ saveNo = dialog.runModalWithCurrentTarget();
+ g_sci->_soundCmd->pauseAll(false);
+
+ if (saveNo < 0) {
+ // User cancelled save
+ return NULL_REG;
+ }
+
+ saveDescription = dialog.getResultString();
+ if (saveDescription.empty()) {
+ saveDescription = dialog.createDefaultSaveDescription(saveNo);
+ }
+ } else {
+ // Native script call
+ gameName = s->_segMan->getString(argv[0]);
+ saveNo = argv[1].toSint16();
+ saveDescription = argv[2].isNull() ? "" : s->_segMan->getString(argv[2]);
+ }
+
+ debugC(kDebugLevelFile, "Game name %s save %d desc %s ver %s", gameName.c_str(), saveNo, saveDescription.c_str(), gameVersion.c_str());
+
+ // Auto-save system used by Torin and LSL7
+ if (gameName == "Autosave" || gameName == "Autosv") {
+ if (saveNo == 0) {
+ // Autosave slot 0 is the autosave
+ } else {
+ // Autosave slot 1 is a "new game" save
+ saveNo = kNewGameId;
+ }
+ } else if (!isScummVMSave) {
+ // ScummVM save screen will give a pre-corrected save number, but native
+ // save-load will not
+ saveNo += kSaveIdShift;
+ }
+
+ Common::SaveFileManager *saveFileMan = g_sci->getSaveFileManager();
+ const Common::String filename = g_sci->getSavegameName(saveNo);
+ Common::OutSaveFile *saveStream = saveFileMan->openForSaving(filename);
+
+ if (saveStream == nullptr) {
+ warning("Error opening savegame \"%s\" for writing", filename.c_str());
+ return NULL_REG;
+ }
+
+ if (!gamestate_save(s, saveStream, saveDescription, gameVersion)) {
+ warning("Saving the game failed");
+ saveStream->finalize();
+ delete saveStream;
+ return NULL_REG;
+ }
+
+ saveStream->finalize();
+ if (saveStream->err()) {
+ warning("Writing the savegame failed");
+ delete saveStream;
+ return NULL_REG;
+ }
+
+ delete saveStream;
+ return TRUE_REG;
}
-reg_t kMakeSaveFileName(EngineState *s, int argc, reg_t *argv) {
- // Creates a savegame name from a slot number. Used when deleting saved games.
- // Param 0: the output buffer (same as in kMakeSaveCatName)
- // Param 1: a string with game parameters, ignored
- // Param 2: the selected slot
+reg_t kRestoreGame32(EngineState *s, int argc, reg_t *argv) {
+ const bool isScummVMRestore = argv[0].isNull();
+ Common::String gameName = "";
+ int16 saveNo = argv[1].toSint16();
+ const Common::String gameVersion = argv[2].isNull() ? "" : s->_segMan->getString(argv[2]);
+
+ if (isScummVMRestore && saveNo == -1) {
+ // ScummVM call, either from lancher or a patched Game::restore
+ g_sci->_soundCmd->pauseAll(true);
+ GUI::SaveLoadChooser dialog(_("Restore game:"), _("Restore"), false);
+ saveNo = dialog.runModalWithCurrentTarget();
+ g_sci->_soundCmd->pauseAll(false);
+
+ if (saveNo < 0) {
+ // User cancelled restore
+ return s->r_acc;
+ }
+ } else {
+ gameName = s->_segMan->getString(argv[0]);
+ }
- SciString *resultString = s->_segMan->lookupString(argv[0]);
- uint16 virtualId = argv[2].toUint16();
- if ((virtualId < SAVEGAMEID_OFFICIALRANGE_START) || (virtualId > SAVEGAMEID_OFFICIALRANGE_END))
- error("kMakeSaveFileName: invalid savegame ID specified");
- uint saveSlot = virtualId - SAVEGAMEID_OFFICIALRANGE_START;
+ if (gameName == "Autosave" || gameName == "Autosv") {
+ if (saveNo == 0) {
+ // Autosave slot 0 is the autosave
+ } else {
+ // Autosave slot 1 is a "new game" save
+ saveNo = kNewGameId;
+ }
+ } else if (!isScummVMRestore) {
+ // ScummVM save screen will give a pre-corrected save number, but native
+ // save-load will not
+ saveNo += kSaveIdShift;
+ }
+
+ Common::SaveFileManager *saveFileMan = g_sci->getSaveFileManager();
+ const Common::String filename = g_sci->getSavegameName(saveNo);
+ Common::SeekableReadStream *saveStream = saveFileMan->openForLoading(filename);
+
+ if (saveStream == nullptr) {
+ warning("Savegame #%d not found", saveNo);
+ return NULL_REG;
+ }
+
+ gamestate_restore(s, saveStream);
+ delete saveStream;
+
+ gamestate_afterRestoreFixUp(s, saveNo);
+ return TRUE_REG;
+}
+
+reg_t kCheckSaveGame32(EngineState *s, int argc, reg_t *argv) {
+ const Common::String gameName = s->_segMan->getString(argv[0]);
+ int16 saveNo = argv[1].toSint16();
+ const Common::String gameVersion = argv[2].isNull() ? "" : s->_segMan->getString(argv[2]);
Common::Array<SavegameDesc> saves;
listSavegames(saves);
- Common::String filename = g_sci->getSavegameName(saveSlot);
- resultString->fromString(filename);
+ if (gameName == "Autosave" || gameName == "Autosv") {
+ if (saveNo == 1) {
+ saveNo = kNewGameId;
+ }
+ } else {
+ saveNo += kSaveIdShift;
+ }
- return argv[0];
-}
+ SavegameDesc save;
+ if (!fillSavegameDesc(g_sci->getSavegameName(saveNo), &save)) {
+ return NULL_REG;
+ }
+
+ if (save.version < MINIMUM_SAVEGAME_VERSION ||
+ save.version > CURRENT_SAVEGAME_VERSION ||
+ save.gameVersion != gameVersion) {
-reg_t kAutoSave(EngineState *s, int argc, reg_t *argv) {
- // TODO
- // This is a timer callback, with 1 parameter: the timer object
- // (e.g. "timers").
- // It's used for auto-saving (i.e. save every X minutes, by checking
- // the elapsed time from the timer object)
+ return NULL_REG;
+ }
- // This function has to return something other than 0 to proceed
return TRUE_REG;
}
+reg_t kGetSaveFiles32(EngineState *s, int argc, reg_t *argv) {
+ // argv[0] is gameName, used in SSCI as the name of the save game catalogue
+ // but unused here since ScummVM does not support multiple catalogues
+ SciArray &descriptions = *s->_segMan->lookupArray(argv[1]);
+ SciArray &saveIds = *s->_segMan->lookupArray(argv[2]);
+
+ Common::Array<SavegameDesc> saves;
+ listSavegames(saves);
+
+ // Normally SSCI limits to 20 games per directory, but ScummVM allows more
+ // than that with games that use the standard save-load dialogue
+ descriptions.resize(SCI_MAX_SAVENAME_LENGTH * saves.size() + 1, true);
+ saveIds.resize(saves.size() + 1, true);
+
+ for (uint i = 0; i < saves.size(); ++i) {
+ const SavegameDesc &save = saves[i];
+ char *target = &descriptions.charAt(SCI_MAX_SAVENAME_LENGTH * i);
+ Common::strlcpy(target, save.name, SCI_MAX_SAVENAME_LENGTH);
+ saveIds.setFromInt16(i, save.id - kSaveIdShift);
+ }
+
+ descriptions.charAt(SCI_MAX_SAVENAME_LENGTH * saves.size()) = '\0';
+ saveIds.setFromInt16(saves.size(), 0);
+
+ return make_reg(0, saves.size());
+}
+
+reg_t kMakeSaveCatName(EngineState *s, int argc, reg_t *argv) {
+ // ScummVM does not use SCI catalogues for save games, but game scripts try
+ // to write out catalogues manually after a save game is deleted, so we need
+ // to be able to identify and ignore these IO operations by always giving
+ // back a fixed catalogue name and then ignoring it in kFileIO
+ SciArray &outCatName = *s->_segMan->lookupArray(argv[0]);
+ outCatName.fromString("fake.cat");
+ return argv[0];
+}
+
+reg_t kMakeSaveFileName(EngineState *s, int argc, reg_t *argv) {
+ SciArray &outFileName = *s->_segMan->lookupArray(argv[0]);
+ // argv[1] is the game name, which is not used by ScummVM
+ const int16 saveNo = argv[2].toSint16();
+ outFileName.fromString(g_sci->getSavegameName(saveNo + kSaveIdShift));
+ return argv[0];
+}
+
#endif
} // End of namespace Sci
diff --git a/engines/sci/engine/kgraphics.cpp b/engines/sci/engine/kgraphics.cpp
index d375a27954..c605ba29e1 100644
--- a/engines/sci/engine/kgraphics.cpp
+++ b/engines/sci/engine/kgraphics.cpp
@@ -356,7 +356,7 @@ reg_t kTextSize(EngineState *s, int argc, reg_t *argv) {
}
textWidth = dest[3].toUint16(); textHeight = dest[2].toUint16();
-
+
uint16 languageSplitter = 0;
Common::String splitText = g_sci->strSplitLanguage(text.c_str(), &languageSplitter, sep);
@@ -832,7 +832,7 @@ void _k_GenericDrawControl(EngineState *s, reg_t controlObject, bool hilite) {
uint16 languageSplitter = 0;
Common::String splitText;
-
+
switch (type) {
case SCI_CONTROLS_TYPE_BUTTON:
case SCI_CONTROLS_TYPE_TEXTEDIT:
@@ -1191,7 +1191,7 @@ reg_t kDisplay(EngineState *s, int argc, reg_t *argv) {
argc--; argc--; argv++; argv++;
text = g_sci->getKernel()->lookupText(textp, index);
}
-
+
uint16 languageSplitter = 0;
Common::String splitText = g_sci->strSplitLanguage(text.c_str(), &languageSplitter);
diff --git a/engines/sci/engine/kgraphics32.cpp b/engines/sci/engine/kgraphics32.cpp
index a33fcf3167..e5b8da4620 100644
--- a/engines/sci/engine/kgraphics32.cpp
+++ b/engines/sci/engine/kgraphics32.cpp
@@ -75,17 +75,14 @@ reg_t kBaseSetter32(EngineState *s, int argc, reg_t *argv) {
CelObjView celObj(viewId, loopNo, celNo);
const int16 scriptWidth = g_sci->_gfxFrameout->getCurrentBuffer().scriptWidth;
- const int16 scriptHeight = g_sci->_gfxFrameout->getCurrentBuffer().scriptHeight;
-
- const Ratio scaleX(scriptWidth, celObj._scaledWidth);
- const Ratio scaleY(scriptHeight, celObj._scaledHeight);
+ const Ratio scaleX(scriptWidth, celObj._xResolution);
int16 brLeft;
if (celObj._mirrorX) {
- brLeft = x - ((celObj._width - celObj._displace.x) * scaleX).toInt();
+ brLeft = x - ((celObj._width - celObj._origin.x) * scaleX).toInt();
} else {
- brLeft = x - (celObj._displace.x * scaleX).toInt();
+ brLeft = x - (celObj._origin.x * scaleX).toInt();
}
const int16 brRight = brLeft + (celObj._width * scaleX).toInt() - 1;
@@ -317,7 +314,7 @@ reg_t kText(EngineState *s, int argc, reg_t *argv) {
reg_t kTextSize32(EngineState *s, int argc, reg_t *argv) {
g_sci->_gfxText32->setFont(argv[2].toUint16());
- reg_t *rect = s->_segMan->derefRegPtr(argv[0], 4);
+ SciArray *rect = s->_segMan->lookupArray(argv[0]);
if (rect == nullptr) {
error("kTextSize: %04x:%04x cannot be dereferenced", PRINT_REG(argv[0]));
}
@@ -327,10 +324,14 @@ reg_t kTextSize32(EngineState *s, int argc, reg_t *argv) {
bool doScaling = argc > 4 ? argv[4].toSint16() : true;
Common::Rect textRect = g_sci->_gfxText32->getTextSize(text, maxWidth, doScaling);
- rect[0] = make_reg(0, textRect.left);
- rect[1] = make_reg(0, textRect.top);
- rect[2] = make_reg(0, textRect.right - 1);
- rect[3] = make_reg(0, textRect.bottom - 1);
+
+ reg_t value[4] = {
+ make_reg(0, textRect.left),
+ make_reg(0, textRect.top),
+ make_reg(0, textRect.right - 1),
+ make_reg(0, textRect.bottom - 1) };
+
+ rect->setElements(0, 4, value);
return s->r_acc;
}
@@ -426,7 +427,7 @@ reg_t kCelHigh32(EngineState *s, int argc, reg_t *argv) {
int16 loopNo = argv[1].toSint16();
int16 celNo = argv[2].toSint16();
CelObjView celObj(resourceId, loopNo, celNo);
- return make_reg(0, mulru(celObj._height, Ratio(g_sci->_gfxFrameout->getCurrentBuffer().scriptHeight, celObj._scaledHeight)));
+ return make_reg(0, mulru(celObj._height, Ratio(g_sci->_gfxFrameout->getCurrentBuffer().scriptHeight, celObj._yResolution)));
}
reg_t kCelWide32(EngineState *s, int argc, reg_t *argv) {
@@ -434,7 +435,7 @@ reg_t kCelWide32(EngineState *s, int argc, reg_t *argv) {
int16 loopNo = argv[1].toSint16();
int16 celNo = argv[2].toSint16();
CelObjView celObj(resourceId, loopNo, celNo);
- return make_reg(0, mulru(celObj._width, Ratio(g_sci->_gfxFrameout->getCurrentBuffer().scriptWidth, celObj._scaledWidth)));
+ return make_reg(0, mulru(celObj._width, Ratio(g_sci->_gfxFrameout->getCurrentBuffer().scriptWidth, celObj._xResolution)));
}
reg_t kCelInfo(EngineState *s, int argc, reg_t *argv) {
@@ -447,10 +448,10 @@ reg_t kCelInfo(EngineState *s, int argc, reg_t *argv) {
switch (argv[0].toUint16()) {
case 0:
- result = view._displace.x;
+ result = view._origin.x;
break;
case 1:
- result = view._displace.y;
+ result = view._origin.y;
break;
case 2:
case 3:
@@ -615,13 +616,13 @@ reg_t kSetFontHeight(EngineState *s, int argc, reg_t *argv) {
// of setting the fontHeight on the font manager, in
// which case we could just get the font directly ourselves.
g_sci->_gfxText32->setFont(argv[0].toUint16());
- g_sci->_gfxText32->_scaledHeight = (g_sci->_gfxText32->_font->getHeight() * g_sci->_gfxFrameout->getCurrentBuffer().scriptHeight + g_sci->_gfxText32->_scaledHeight - 1) / g_sci->_gfxText32->_scaledHeight;
- return make_reg(0, g_sci->_gfxText32->_scaledHeight);
+ g_sci->_gfxText32->_yResolution = (g_sci->_gfxText32->_font->getHeight() * g_sci->_gfxFrameout->getCurrentBuffer().scriptHeight + g_sci->_gfxText32->_yResolution - 1) / g_sci->_gfxText32->_yResolution;
+ return make_reg(0, g_sci->_gfxText32->_yResolution);
}
reg_t kSetFontRes(EngineState *s, int argc, reg_t *argv) {
- g_sci->_gfxText32->_scaledWidth = argv[0].toUint16();
- g_sci->_gfxText32->_scaledHeight = argv[1].toUint16();
+ g_sci->_gfxText32->_xResolution = argv[0].toUint16();
+ g_sci->_gfxText32->_yResolution = argv[1].toUint16();
return s->r_acc;
}
@@ -636,12 +637,12 @@ reg_t kBitmapCreate(EngineState *s, int argc, reg_t *argv) {
int16 height = argv[1].toSint16();
int16 skipColor = argv[2].toSint16();
int16 backColor = argv[3].toSint16();
- int16 scaledWidth = argc > 4 ? argv[4].toSint16() : g_sci->_gfxText32->_scaledWidth;
- int16 scaledHeight = argc > 5 ? argv[5].toSint16() : g_sci->_gfxText32->_scaledHeight;
+ int16 xResolution = argc > 4 ? argv[4].toSint16() : g_sci->_gfxText32->_xResolution;
+ int16 yResolution = argc > 5 ? argv[5].toSint16() : g_sci->_gfxText32->_yResolution;
bool useRemap = argc > 6 ? argv[6].toSint16() : false;
reg_t bitmapId;
- SciBitmap &bitmap = *s->_segMan->allocateBitmap(&bitmapId, width, height, skipColor, 0, 0, scaledWidth, scaledHeight, 0, useRemap, true);
+ SciBitmap &bitmap = *s->_segMan->allocateBitmap(&bitmapId, width, height, skipColor, 0, 0, xResolution, yResolution, 0, useRemap, true);
memset(bitmap.getPixels(), backColor, width * height);
return bitmapId;
}
@@ -675,12 +676,12 @@ reg_t kBitmapDrawView(EngineState *s, int argc, reg_t *argv) {
const int16 alignY = argc > 8 ? argv[8].toSint16() : -1;
Common::Point position(
- x == -1 ? bitmap.getDisplace().x : x,
- y == -1 ? bitmap.getDisplace().y : y
+ x == -1 ? bitmap.getOrigin().x : x,
+ y == -1 ? bitmap.getOrigin().y : y
);
- position.x -= alignX == -1 ? view._displace.x : alignX;
- position.y -= alignY == -1 ? view._displace.y : alignY;
+ position.x -= alignX == -1 ? view._origin.x : alignX;
+ position.y -= alignY == -1 ? view._origin.y : alignY;
Common::Rect drawRect(
position.x,
@@ -755,16 +756,37 @@ reg_t kBitmapInvert(EngineState *s, int argc, reg_t *argv) {
return kStubNull(s, argc + 1, argv - 1);
}
-reg_t kBitmapSetDisplace(EngineState *s, int argc, reg_t *argv) {
+reg_t kBitmapSetOrigin(EngineState *s, int argc, reg_t *argv) {
SciBitmap &bitmap = *s->_segMan->lookupBitmap(argv[0]);
- bitmap.setDisplace(Common::Point(argv[1].toSint16(), argv[2].toSint16()));
+ bitmap.setOrigin(Common::Point(argv[1].toSint16(), argv[2].toSint16()));
return s->r_acc;
}
reg_t kBitmapCreateFromView(EngineState *s, int argc, reg_t *argv) {
- // viewId, loopNo, celNo, skipColor, backColor, useRemap, source overlay bitmap
+ CelObjView view(argv[0].toUint16(), argv[1].toSint16(), argv[2].toSint16());
+ const uint8 skipColor = argc > 3 && argv[3].toSint16() != -1 ? argv[3].toSint16() : view._skipColor;
+ const uint8 backColor = argc > 4 && argv[4].toSint16() != -1 ? argv[4].toSint16() : view._skipColor;
+ const bool useRemap = argc > 5 ? (bool)argv[5].toSint16() : false;
- return kStub(s, argc + 1, argv - 1);
+ reg_t bitmapId;
+ SciBitmap &bitmap = *s->_segMan->allocateBitmap(&bitmapId, view._width, view._height, skipColor, 0, 0, view._xResolution, view._yResolution, 0, useRemap, true);
+ Buffer &buffer = bitmap.getBuffer();
+
+ const Common::Rect viewRect(view._width, view._height);
+ buffer.fillRect(viewRect, backColor);
+ view.draw(buffer, viewRect, Common::Point(0, 0), view._mirrorX);
+
+ if (argc > 6 && !argv[6].isNull()) {
+ reg_t clutHandle = argv[6];
+ if (s->_segMan->isObject(clutHandle)) {
+ clutHandle = readSelector(s->_segMan, clutHandle, SELECTOR(data));
+ }
+
+ SciArray &clut = *s->_segMan->lookupArray(clutHandle);
+ bitmap.applyRemap(clut);
+ }
+
+ return bitmapId;
}
reg_t kBitmapCopyPixels(EngineState *s, int argc, reg_t *argv) {
@@ -780,12 +802,24 @@ reg_t kBitmapClone(EngineState *s, int argc, reg_t *argv) {
}
reg_t kBitmapGetInfo(EngineState *s, int argc, reg_t *argv) {
- // bitmap
+ SciBitmap &bitmap = *s->_segMan->lookupBitmap(argv[0]);
- // argc 1 = get width
- // argc 2 = pixel at row 0 col n
- // argc 3 = pixel at row n col n
- return kStub(s, argc + 1, argv - 1);
+ if (argc == 1) {
+ return make_reg(0, bitmap.getWidth());
+ }
+
+ int32 offset;
+ if (argc == 2) {
+ offset = argv[1].toUint16();
+ } else {
+ const int16 x = argv[1].toSint16();
+ const int16 y = argv[2].toSint16();
+ offset = y * bitmap.getWidth() + x;
+ }
+
+ assert(offset >= 0 && offset < bitmap.getWidth() * bitmap.getHeight());
+ const uint8 color = bitmap.getPixels()[offset];
+ return make_reg(0, color);
}
reg_t kBitmapScale(EngineState *s, int argc, reg_t *argv) {
@@ -924,17 +958,17 @@ reg_t kPaletteSetGamma(EngineState *s, int argc, reg_t *argv) {
}
reg_t kPaletteSetFade(EngineState *s, int argc, reg_t *argv) {
- uint16 fromColor = argv[0].toUint16();
- uint16 toColor = argv[1].toUint16();
- uint16 percent = argv[2].toUint16();
+ const uint16 fromColor = argv[0].toUint16();
+ const uint16 toColor = argv[1].toUint16();
+ const uint16 percent = argv[2].toUint16();
g_sci->_gfxPalette32->setFade(percent, fromColor, toColor);
return s->r_acc;
}
reg_t kPalVarySetVary(EngineState *s, int argc, reg_t *argv) {
- GuiResourceId paletteId = argv[0].toUint16();
- int time = argc > 1 ? argv[1].toSint16() * 60 : 0;
- int16 percent = argc > 2 ? argv[2].toSint16() : 100;
+ const GuiResourceId paletteId = argv[0].toUint16();
+ const int32 time = argc > 1 ? argv[1].toSint16() * 60 : 0;
+ const int16 percent = argc > 2 ? argv[2].toSint16() : 100;
int16 fromColor;
int16 toColor;
@@ -950,9 +984,9 @@ reg_t kPalVarySetVary(EngineState *s, int argc, reg_t *argv) {
}
reg_t kPalVarySetPercent(EngineState *s, int argc, reg_t *argv) {
- int time = argc > 0 ? argv[0].toSint16() * 60 : 0;
- int16 percent = argc > 1 ? argv[1].toSint16() : 0;
- g_sci->_gfxPalette32->setVaryPercent(percent, time, -1, -1);
+ const int32 time = argc > 0 ? argv[0].toSint16() * 60 : 0;
+ const int16 percent = argc > 1 ? argv[1].toSint16() : 0;
+ g_sci->_gfxPalette32->setVaryPercent(percent, time);
return s->r_acc;
}
@@ -966,31 +1000,31 @@ reg_t kPalVaryOff(EngineState *s, int argc, reg_t *argv) {
}
reg_t kPalVaryMergeTarget(EngineState *s, int argc, reg_t *argv) {
- GuiResourceId paletteId = argv[0].toUint16();
+ const GuiResourceId paletteId = argv[0].toUint16();
g_sci->_gfxPalette32->kernelPalVaryMergeTarget(paletteId);
return make_reg(0, g_sci->_gfxPalette32->getVaryPercent());
}
reg_t kPalVarySetTime(EngineState *s, int argc, reg_t *argv) {
- int time = argv[0].toSint16() * 60;
+ const int32 time = argv[0].toSint16() * 60;
g_sci->_gfxPalette32->setVaryTime(time);
return s->r_acc;
}
reg_t kPalVarySetTarget(EngineState *s, int argc, reg_t *argv) {
- GuiResourceId paletteId = argv[0].toUint16();
+ const GuiResourceId paletteId = argv[0].toUint16();
g_sci->_gfxPalette32->kernelPalVarySetTarget(paletteId);
return make_reg(0, g_sci->_gfxPalette32->getVaryPercent());
}
reg_t kPalVarySetStart(EngineState *s, int argc, reg_t *argv) {
- GuiResourceId paletteId = argv[0].toUint16();
+ const GuiResourceId paletteId = argv[0].toUint16();
g_sci->_gfxPalette32->kernelPalVarySetStart(paletteId);
return make_reg(0, g_sci->_gfxPalette32->getVaryPercent());
}
reg_t kPalVaryMergeStart(EngineState *s, int argc, reg_t *argv) {
- GuiResourceId paletteId = argv[0].toUint16();
+ const GuiResourceId paletteId = argv[0].toUint16();
g_sci->_gfxPalette32->kernelPalVaryMergeStart(paletteId);
return make_reg(0, g_sci->_gfxPalette32->getVaryPercent());
}
@@ -1006,7 +1040,6 @@ reg_t kPalCycleSetCycle(EngineState *s, int argc, reg_t *argv) {
const uint16 toColor = argv[1].toUint16();
const int16 direction = argv[2].toSint16();
const uint16 delay = argc > 3 ? argv[3].toUint16() : 0;
-
g_sci->_gfxPalette32->setCycle(fromColor, toColor, direction, delay);
return s->r_acc;
}
@@ -1014,7 +1047,6 @@ reg_t kPalCycleSetCycle(EngineState *s, int argc, reg_t *argv) {
reg_t kPalCycleDoCycle(EngineState *s, int argc, reg_t *argv) {
const uint16 fromColor = argv[0].toUint16();
const int16 speed = argc > 1 ? argv[1].toSint16() : 1;
-
g_sci->_gfxPalette32->doCycle(fromColor, speed);
return s->r_acc;
}
diff --git a/engines/sci/engine/klists.cpp b/engines/sci/engine/klists.cpp
index e780d3cdc3..5cd9c3623d 100644
--- a/engines/sci/engine/klists.cpp
+++ b/engines/sci/engine/klists.cpp
@@ -554,7 +554,7 @@ reg_t kListEachElementDo(EngineState *s, int argc, reg_t *argv) {
++list->numRecursions;
- if (list->numRecursions > ARRAYSIZE(list->nextNodes)) {
+ if (list->numRecursions >= ARRAYSIZE(list->nextNodes)) {
error("Too much recursion in kListEachElementDo");
}
@@ -683,197 +683,105 @@ reg_t kMoveToEnd(EngineState *s, int argc, reg_t *argv) {
}
reg_t kArray(EngineState *s, int argc, reg_t *argv) {
- uint16 op = argv[0].toUint16();
-
- // Use kString when accessing strings
- // This is possible, as strings inherit from arrays
- // and in this case (type 3) arrays are of type char *.
- // kString is almost exactly the same as kArray, so
- // this call is possible
- // TODO: we need to either merge SCI2 strings and
- // arrays together, and in the future merge them with
- // the SCI1 strings and arrays in the segment manager
- bool callStringFunc = false;
- if (op == 0) {
- // New, check if the target type is 3 (string)
- if (argv[2].toUint16() == 3)
- callStringFunc = true;
- } else {
- if (s->_segMan->getSegmentType(argv[1].getSegment()) == SEG_TYPE_STRING ||
- s->_segMan->getSegmentType(argv[1].getSegment()) == SEG_TYPE_SCRIPT) {
- callStringFunc = true;
- }
-
-#if 0
- if (op == 6) {
- if (s->_segMan->getSegmentType(argv[3].getSegment()) == SEG_TYPE_STRING ||
- s->_segMan->getSegmentType(argv[3].getSegment()) == SEG_TYPE_SCRIPT) {
- callStringFunc = true;
- }
- }
-#endif
- }
+ if (!s)
+ return make_reg(0, getSciVersion());
+ error("not supposed to call this");
+}
- if (callStringFunc) {
- Kernel *kernel = g_sci->getKernel();
- uint16 kernelStringFuncId = kernel->_kernelFunc_StringId;
- if (kernelStringFuncId) {
- const KernelFunction *kernelStringFunc = &kernel->_kernelFuncs[kernelStringFuncId];
-
- if (op < kernelStringFunc->subFunctionCount) {
- // subfunction-id is valid
- const KernelSubFunction *kernelStringSubCall = &kernelStringFunc->subFunctions[op];
- argc--;
- argv++; // remove subfunction-id from arguments
- // and call the kString subfunction
- return kernelStringSubCall->function(s, argc, argv);
- }
- }
- }
+reg_t kArrayNew(EngineState *s, int argc, reg_t *argv) {
+ uint16 size = argv[0].toUint16();
+ const SciArrayType type = (SciArrayType)argv[1].toUint16();
- switch (op) {
- case 0: { // New
- reg_t arrayHandle;
- SciArray<reg_t> *array = s->_segMan->allocateArray(&arrayHandle);
- array->setType(argv[2].toUint16());
- array->setSize(argv[1].toUint16());
- return arrayHandle;
- }
- case 1: { // Size
- SciArray<reg_t> *array = s->_segMan->lookupArray(argv[1]);
- return make_reg(0, array->getSize());
+ if (type == kArrayTypeString) {
+ ++size;
}
- case 2: { // At (return value at an index)
- SciArray<reg_t> *array = s->_segMan->lookupArray(argv[1]);
- if (g_sci->getGameId() == GID_PHANTASMAGORIA2) {
- // HACK: Phantasmagoria 2 keeps trying to access past the end of an
- // array when it starts. I'm assuming it's trying to see where the
- // array ends, or tries to resize it. Adjust the array size
- // accordingly, and return NULL for now.
- if (array->getSize() == argv[2].toUint16()) {
- array->setSize(argv[2].toUint16());
- return NULL_REG;
- }
- }
- return array->getValue(argv[2].toUint16());
- }
- case 3: { // Atput (put value at an index)
- SciArray<reg_t> *array = s->_segMan->lookupArray(argv[1]);
-
- uint32 index = argv[2].toUint16();
- uint32 count = argc - 3;
- if (index + count > 65535)
- break;
+ reg_t arrayHandle;
+ s->_segMan->allocateArray(type, size, &arrayHandle);
+ return arrayHandle;
+}
- if (array->getSize() < index + count)
- array->setSize(index + count);
+reg_t kArrayGetSize(EngineState *s, int argc, reg_t *argv) {
+ const SciArray &array = *s->_segMan->lookupArray(argv[0]);
+ return make_reg(0, array.size());
+}
- for (uint16 i = 0; i < count; i++)
- array->setValue(i + index, argv[i + 3]);
+reg_t kArrayGetElement(EngineState *s, int argc, reg_t *argv) {
+ SciArray &array = *s->_segMan->lookupArray(argv[0]);
+ return array.getAsID(argv[1].toUint16());
+}
- return argv[1]; // We also have to return the handle
- }
- case 4: // Free
- // Freeing of arrays is handled by the garbage collector
- return s->r_acc;
- case 5: { // Fill
- SciArray<reg_t> *array = s->_segMan->lookupArray(argv[1]);
- uint16 index = argv[2].toUint16();
+reg_t kArraySetElements(EngineState *s, int argc, reg_t *argv) {
+ SciArray &array = *s->_segMan->lookupArray(argv[0]);
+ array.setElements(argv[1].toUint16(), argc - 2, argv + 2);
+ return argv[0];
+}
- // A count of -1 means fill the rest of the array
- uint16 count = argv[3].toSint16() == -1 ? array->getSize() - index : argv[3].toUint16();
- uint16 arraySize = array->getSize();
+reg_t kArrayFree(EngineState *s, int argc, reg_t *argv) {
+ s->_segMan->freeArray(argv[0]);
+ return s->r_acc;
+}
- if (arraySize < index + count)
- array->setSize(index + count);
+reg_t kArrayFill(EngineState *s, int argc, reg_t *argv) {
+ SciArray &array = *s->_segMan->lookupArray(argv[0]);
+ array.fill(argv[1].toUint16(), argv[2].toUint16(), argv[3]);
+ return argv[0];
+}
- for (uint16 i = 0; i < count; i++)
- array->setValue(i + index, argv[4]);
+reg_t kArrayCopy(EngineState *s, int argc, reg_t *argv) {
+ SciArray &target = *s->_segMan->lookupArray(argv[0]);
+ const uint16 targetIndex = argv[1].toUint16();
- return argv[1];
+ SciArray source;
+ // String copies may be made from static script data
+ if (!s->_segMan->isArray(argv[2])) {
+ source.setType(kArrayTypeString);
+ source.fromString(s->_segMan->getString(argv[2]));
+ } else {
+ source = *s->_segMan->lookupArray(argv[2]);
}
- case 6: { // Cpy
- if (argv[1].isNull() || argv[3].isNull()) {
- if (getSciVersion() == SCI_VERSION_3) {
- // FIXME: Happens in SCI3, probably because of a missing kernel function.
- warning("kArray(Cpy): Request to copy from or to a null pointer");
- return NULL_REG;
- } else {
- // SCI2-2.1: error out
- error("kArray(Cpy): Request to copy from or to a null pointer");
- }
- }
+ const uint16 sourceIndex = argv[3].toUint16();
+ const uint16 count = argv[4].toUint16();
- reg_t arrayHandle = argv[1];
- SciArray<reg_t> *array1 = s->_segMan->lookupArray(argv[1]);
- SciArray<reg_t> *array2 = s->_segMan->lookupArray(argv[3]);
- uint32 index1 = argv[2].toUint16();
- uint32 index2 = argv[4].toUint16();
-
- // The original engine ignores bad copies too
- if (index2 > array2->getSize())
- break;
-
- // A count of -1 means fill the rest of the array
- uint32 count = argv[5].toSint16() == -1 ? array2->getSize() - index2 : argv[5].toUint16();
-
- if (array1->getSize() < index1 + count)
- array1->setSize(index1 + count);
+ target.copy(source, sourceIndex, targetIndex, count);
+ return argv[0];
+}
- for (uint16 i = 0; i < count; i++)
- array1->setValue(i + index1, array2->getValue(i + index2));
+reg_t kArrayDuplicate(EngineState *s, int argc, reg_t *argv) {
+ reg_t targetHandle;
- return arrayHandle;
+ // String duplicates may be made from static script data
+ if (!s->_segMan->isArray(argv[0])) {
+ const Common::String source = s->_segMan->getString(argv[0]);
+ SciArray &target = *s->_segMan->allocateArray(kArrayTypeString, source.size(), &targetHandle);
+ target.fromString(source);
+ } else {
+ SciArray &source = *s->_segMan->lookupArray(argv[0]);
+ SciArray &target = *s->_segMan->allocateArray(source.getType(), source.size(), &targetHandle);
+ target = source;
}
- case 7: // Cmp
- // Not implemented in SSCI
- warning("kArray(Cmp) called");
- return s->r_acc;
- case 8: { // Dup
- if (argv[1].isNull()) {
- warning("kArray(Dup): Request to duplicate a null pointer");
-#if 0
- // Allocate an array anyway
- reg_t arrayHandle;
- SciArray<reg_t> *dupArray = s->_segMan->allocateArray(&arrayHandle);
- dupArray->setType(3);
- dupArray->setSize(0);
- return arrayHandle;
-#endif
- return NULL_REG;
- }
- SegmentObj *sobj = s->_segMan->getSegmentObj(argv[1].getSegment());
- if (!sobj || sobj->getType() != SEG_TYPE_ARRAY)
- error("kArray(Dup): Request to duplicate a segment which isn't an array");
-
- reg_t arrayHandle;
- SciArray<reg_t> *dupArray = s->_segMan->allocateArray(&arrayHandle);
- // This must occur after allocateArray, as inserting a new object
- // in the heap object list might invalidate this pointer. Also refer
- // to the same issue in kClone()
- SciArray<reg_t> *array = s->_segMan->lookupArray(argv[1]);
-
- dupArray->setType(array->getType());
- dupArray->setSize(array->getSize());
- for (uint32 i = 0; i < array->getSize(); i++)
- dupArray->setValue(i, array->getValue(i));
-
- return arrayHandle;
- }
- case 9: // Getdata
- if (!s->_segMan->isHeapObject(argv[1]))
- return argv[1];
+ return targetHandle;
+}
- return readSelector(s->_segMan, argv[1], SELECTOR(data));
- default:
- error("Unknown kArray subop %d", op);
+reg_t kArrayGetData(EngineState *s, int argc, reg_t *argv) {
+ if (s->_segMan->isObject(argv[0])) {
+ return readSelector(s->_segMan, argv[0], SELECTOR(data));
}
- return NULL_REG;
+ return argv[0];
}
+reg_t kArrayByteCopy(EngineState *s, int argc, reg_t *argv) {
+ SciArray &target = *s->_segMan->lookupArray(argv[0]);
+ const uint16 targetOffset = argv[1].toUint16();
+ const SciArray &source = *s->_segMan->lookupArray(argv[2]);
+ const uint16 sourceOffset = argv[3].toUint16();
+ const uint16 count = argv[4].toUint16();
+
+ target.byteCopy(source, sourceOffset, targetOffset, count);
+ return argv[0];
+}
#endif
} // End of namespace Sci
diff --git a/engines/sci/engine/kmisc.cpp b/engines/sci/engine/kmisc.cpp
index f2a3c6b0f7..9aa03a4760 100644
--- a/engines/sci/engine/kmisc.cpp
+++ b/engines/sci/engine/kmisc.cpp
@@ -459,6 +459,14 @@ reg_t kGetWindowsOption(EngineState *s, int argc, reg_t *argv) {
}
}
+extern Common::String format(const Common::String &source, int argc, const reg_t *argv);
+
+reg_t kPrintDebug(EngineState *s, int argc, reg_t *argv) {
+ const Common::String debugString = s->_segMan->getString(argv[0]);
+ debugC(kDebugLevelGame, "%s", format(debugString, argc - 1, argv + 1).c_str());
+ return s->r_acc;
+}
+
#endif
// kIconBar is really a subop of kMacPlatform for SCI1.1 Mac
diff --git a/engines/sci/engine/kpathing.cpp b/engines/sci/engine/kpathing.cpp
index 06f16299aa..937b1cfc2f 100644
--- a/engines/sci/engine/kpathing.cpp
+++ b/engines/sci/engine/kpathing.cpp
@@ -1397,10 +1397,8 @@ static reg_t allocateOutputArray(SegManager *segMan, int size) {
#ifdef ENABLE_SCI32
if (getSciVersion() >= SCI_VERSION_2) {
- SciArray<reg_t> *array = segMan->allocateArray(&addr);
+ SciArray *array = segMan->allocateArray(kArrayTypeInt16, size * 2, &addr);
assert(array);
- array->setType(0);
- array->setSize(size * 2);
return addr;
}
#endif
diff --git a/engines/sci/engine/kscripts.cpp b/engines/sci/engine/kscripts.cpp
index 6fd130bceb..0e29ccf783 100644
--- a/engines/sci/engine/kscripts.cpp
+++ b/engines/sci/engine/kscripts.cpp
@@ -238,8 +238,8 @@ reg_t kScriptID(EngineState *s, int argc, reg_t *argv) {
// initialized to 0, whereas it's 6 in other versions. Thus, we assign it
// to 6 here, fixing the speed of the introduction. Refer to bug #3102071.
if (g_sci->getGameId() == GID_PQ2 && script == 200 &&
- s->variables[VAR_GLOBAL][3].isNull()) {
- s->variables[VAR_GLOBAL][3] = make_reg(0, 6);
+ s->variables[VAR_GLOBAL][kGlobalVarSpeed].isNull()) {
+ s->variables[VAR_GLOBAL][kGlobalVarSpeed] = make_reg(0, 6);
}
return make_reg(scriptSeg, address);
diff --git a/engines/sci/engine/kstring.cpp b/engines/sci/engine/kstring.cpp
index 1c08bf597c..6ec61343b0 100644
--- a/engines/sci/engine/kstring.cpp
+++ b/engines/sci/engine/kstring.cpp
@@ -202,11 +202,6 @@ reg_t kReadNumber(EngineState *s, int argc, reg_t *argv) {
}
-#define ALIGN_NONE 0
-#define ALIGN_RIGHT 1
-#define ALIGN_LEFT -1
-#define ALIGN_CENTER 2
-
/* Format(targ_address, textresnr, index_inside_res, ...)
** or
** Format(targ_address, heap_text_addr, ...)
@@ -214,6 +209,13 @@ reg_t kReadNumber(EngineState *s, int argc, reg_t *argv) {
** the supplied parameters and writes it to the targ_address.
*/
reg_t kFormat(EngineState *s, int argc, reg_t *argv) {
+ enum {
+ ALIGN_NONE = 0,
+ ALIGN_RIGHT = 1,
+ ALIGN_LEFT = -1,
+ ALIGN_CENTER = 2
+ };
+
uint16 *arguments;
reg_t dest = argv[0];
int maxsize = 4096; /* Arbitrary... */
@@ -301,12 +303,6 @@ reg_t kFormat(EngineState *s, int argc, reg_t *argv) {
case 's': { /* Copy string */
reg_t reg = argv[startarg + paramindex];
-#ifdef ENABLE_SCI32
- // If the string is a string object, get to the actual string in the data selector
- if (s->_segMan->isObject(reg))
- reg = readSelector(s->_segMan, reg, SELECTOR(data));
-#endif
-
Common::String tempsource = g_sci->getKernel()->lookupText(reg,
arguments[paramindex + 1]);
int slen = strlen(tempsource.c_str());
@@ -379,12 +375,6 @@ reg_t kFormat(EngineState *s, int argc, reg_t *argv) {
case 'u':
unsignedVar = true;
case 'd': { /* Copy decimal */
- // In the new SCI2 kString function, %d is used for unsigned
- // integers. An example is script 962 in Shivers - it uses %d
- // to create file names.
- if (getSciVersion() >= SCI_VERSION_2)
- unsignedVar = true;
-
/* int templen; -- unused atm */
const char *format_string = "%d";
@@ -437,14 +427,6 @@ reg_t kFormat(EngineState *s, int argc, reg_t *argv) {
*target = 0; /* Terminate string */
-#ifdef ENABLE_SCI32
- // Resize SCI32 strings if necessary
- if (getSciVersion() >= SCI_VERSION_2) {
- SciString *string = s->_segMan->lookupString(dest);
- string->setSize(strlen(targetbuf) + 1);
- }
-#endif
-
s->_segMan->strcpy(dest, targetbuf);
return dest; /* Return target addr */
@@ -661,238 +643,236 @@ reg_t kStrSplit(EngineState *s, int argc, reg_t *argv) {
#ifdef ENABLE_SCI32
-// TODO: there is an unused second argument, happens at least in LSL6 right during the intro
+reg_t kString(EngineState *s, int argc, reg_t *argv) {
+ if (!s)
+ return make_reg(0, getSciVersion());
+ error("not supposed to call this");
+}
+
reg_t kStringNew(EngineState *s, int argc, reg_t *argv) {
reg_t stringHandle;
- SciString *string = s->_segMan->allocateString(&stringHandle);
- string->setSize(argv[0].toUint16());
-
- // Make sure the first character is a null character
- if (string->getSize() > 0)
- string->setValue(0, 0);
-
+ const uint16 size = argv[0].toUint16();
+ s->_segMan->allocateArray(kArrayTypeString, size, &stringHandle);
return stringHandle;
}
-reg_t kStringSize(EngineState *s, int argc, reg_t *argv) {
- return make_reg(0, s->_segMan->getString(argv[0]).size());
-}
-
-// At (return value at an index)
-reg_t kStringAt(EngineState *s, int argc, reg_t *argv) {
- // Note that values are put in bytes to avoid sign extension
- if (argv[0].getSegment() == s->_segMan->getStringSegmentId()) {
- SciString *string = s->_segMan->lookupString(argv[0]);
- byte val = string->getRawData()[argv[1].toUint16()];
- return make_reg(0, val);
- } else {
- Common::String string = s->_segMan->getString(argv[0]);
- byte val = string[argv[1].toUint16()];
- return make_reg(0, val);
- }
-}
-
-// Atput (put value at an index)
-reg_t kStringPutAt(EngineState *s, int argc, reg_t *argv) {
- SciString *string = s->_segMan->lookupString(argv[0]);
+reg_t kStringGetChar(EngineState *s, int argc, reg_t *argv) {
+ const uint16 index = argv[1].toUint16();
- uint32 index = argv[1].toUint16();
- uint32 count = argc - 2;
+ // Game scripts may contain static raw string data
+ if (!s->_segMan->isArray(argv[0])) {
+ const Common::String string = s->_segMan->getString(argv[0]);
+ if (index >= string.size()) {
+ return make_reg(0, 0);
+ }
- if (index + count > 65535)
- return NULL_REG;
+ return make_reg(0, (byte)string[index]);
+ }
- if (string->getSize() < index + count)
- string->setSize(index + count);
+ SciArray &array = *s->_segMan->lookupArray(argv[0]);
- for (uint16 i = 0; i < count; i++)
- string->setValue(i + index, argv[i + 2].toUint16());
+ if (index >= array.size()) {
+ return make_reg(0, 0);
+ }
- return argv[0]; // We also have to return the handle
+ return array.getAsID(index);
}
reg_t kStringFree(EngineState *s, int argc, reg_t *argv) {
- // Freeing of strings is handled by the garbage collector
+ if (!argv[0].isNull()) {
+ s->_segMan->freeArray(argv[0]);
+ }
return s->r_acc;
}
-reg_t kStringFill(EngineState *s, int argc, reg_t *argv) {
- SciString *string = s->_segMan->lookupString(argv[0]);
- uint16 index = argv[1].toUint16();
+reg_t kStringCompare(EngineState *s, int argc, reg_t *argv) {
+ const Common::String string1 = s->_segMan->getString(argv[0]);
+ const Common::String string2 = s->_segMan->getString(argv[1]);
- // A count of -1 means fill the rest of the array
- uint16 count = argv[2].toSint16() == -1 ? string->getSize() - index : argv[2].toUint16();
- uint16 stringSize = string->getSize();
+ int result;
+ if (argc == 3) {
+ result = strncmp(string1.c_str(), string2.c_str(), argv[2].toUint16());
+ } else {
+ result = strcmp(string1.c_str(), string2.c_str());
+ }
- if (stringSize < index + count)
- string->setSize(index + count);
+ return make_reg(0, (result > 0) - (result < 0));
+}
- for (uint16 i = 0; i < count; i++)
- string->setValue(i + index, argv[3].toUint16());
+reg_t kStringGetData(EngineState *s, int argc, reg_t *argv) {
+ if (s->_segMan->isObject(argv[0])) {
+ return readSelector(s->_segMan, argv[0], SELECTOR(data));
+ }
return argv[0];
}
-reg_t kStringCopy(EngineState *s, int argc, reg_t *argv) {
- const char *string2 = 0;
- uint32 string2Size = 0;
- Common::String string;
+reg_t kStringLength(EngineState *s, int argc, reg_t *argv) {
+ return make_reg(0, s->_segMan->getString(argv[0]).size());
+}
- if (argv[2].getSegment() == s->_segMan->getStringSegmentId()) {
- SciString *sstr;
- sstr = s->_segMan->lookupString(argv[2]);
- string2 = sstr->getRawData();
- string2Size = sstr->getSize();
- } else {
- string = s->_segMan->getString(argv[2]);
- string2 = string.c_str();
- string2Size = string.size() + 1;
+namespace {
+ bool isFlag(const char c) {
+ return strchr("-+ 0#", c);
}
- uint32 index1 = argv[1].toUint16();
- uint32 index2 = argv[3].toUint16();
-
- if (argv[0] == argv[2]) {
- // source and destination string are one and the same
- if (index1 == index2) {
- // even same index? ignore this call
- // Happens in KQ7, when starting a chapter
- return argv[0];
- }
- // TODO: this will crash, when setSize() is triggered later
- // we need to exactly replicate original interpreter behavior
- warning("kString(Copy): source is the same as destination string");
+ bool isPrecision(const char c) {
+ return strchr(".0123456789*", c);
}
- // The original engine ignores bad copies too
- if (index2 >= string2Size)
- return NULL_REG;
-
- // A count of -1 means fill the rest of the array
- uint32 count = string2Size - index2;
- if (argv[4].toSint16() != -1) {
- count = MIN(count, (uint32)argv[4].toUint16());
+ bool isWidth(const char c) {
+ return strchr("0123456789*", c);
}
-// reg_t strAddress = argv[0];
- SciString *string1 = s->_segMan->lookupString(argv[0]);
- //SciString *string1 = !argv[1].isNull() ? s->_segMan->lookupString(argv[1]) : s->_segMan->allocateString(&strAddress);
+ bool isLength(const char c) {
+ return strchr("hjlLtz", c);
+ }
- if (string1->getSize() < index1 + count)
- string1->setSize(index1 + count);
+ bool isType(const char c) {
+ return strchr("dsuxXaAceEfFgGinop", c);
+ }
- // Note: We're accessing from c_str() here because the
- // string's size ignores the trailing 0 and therefore
- // triggers an assert when doing string2[i + index2].
- for (uint16 i = 0; i < count; i++)
- string1->setValue(i + index1, string2[i + index2]);
+ bool isSignedType(const char type) {
+ return type == 'd' || type == 'i';
+ }
- return argv[0];
-}
+ bool isUnsignedType(const char type) {
+ return strchr("uxXoc", type);
+ }
-reg_t kStringCompare(EngineState *s, int argc, reg_t *argv) {
- Common::String string1 = argv[0].isNull() ? "" : s->_segMan->getString(argv[0]);
- Common::String string2 = argv[1].isNull() ? "" : s->_segMan->getString(argv[1]);
+ bool isStringType(const char type) {
+ return type == 's';
+ }
- if (argc == 3) // Strncmp
- return make_reg(0, strncmp(string1.c_str(), string2.c_str(), argv[2].toUint16()));
- else // Strcmp
- return make_reg(0, strcmp(string1.c_str(), string2.c_str()));
-}
+ Common::String readPlaceholder(const char *&in, reg_t arg) {
+ const char *const start = in;
-// was removed for SCI2.1 Late+
-reg_t kStringDup(EngineState *s, int argc, reg_t *argv) {
- reg_t stringHandle;
+ assert(*in == '%');
+ ++in;
- SciString *dupString = s->_segMan->allocateString(&stringHandle);
+ while (isFlag(*in)) {
+ ++in;
+ }
+ while (isWidth(*in)) {
+ ++in;
+ }
+ while (isPrecision(*in)) {
+ ++in;
+ }
+ while (isLength(*in)) {
+ ++in;
+ }
- if (argv[0].getSegment() == s->_segMan->getStringSegmentId()) {
- *dupString = *s->_segMan->lookupString(argv[0]);
- } else {
- dupString->fromString(s->_segMan->getString(argv[0]));
+ char format[64];
+ format[0] = '\0';
+ const char type = *in++;
+ Common::strlcpy(format, start, MIN<size_t>(64, in - start + 1));
+
+ if (isType(type)) {
+ if (isSignedType(type)) {
+ const int value = arg.toSint16();
+ return Common::String::format(format, value);
+ } else if (isUnsignedType(type)) {
+ const uint value = arg.toUint16();
+ return Common::String::format(format, value);
+ } else if (isStringType(type)) {
+ Common::String value;
+ SegManager *segMan = g_sci->getEngineState()->_segMan;
+ if (segMan->isObject(arg)) {
+ value = segMan->getString(readSelector(segMan, arg, SELECTOR(data)));
+ } else {
+ value = segMan->getString(arg);
+ }
+ return Common::String::format(format, value.c_str());
+ } else {
+ error("Unsupported format type %c", type);
+ }
+ } else {
+ return Common::String::format("%s", format);
+ }
}
-
- return stringHandle;
}
-// was removed for SCI2.1 Late+
-reg_t kStringGetData(EngineState *s, int argc, reg_t *argv) {
- if (!s->_segMan->isHeapObject(argv[0]))
- return argv[0];
+Common::String format(const Common::String &source, int argc, const reg_t *argv) {
+ Common::String out;
+ const char *in = source.c_str();
+ int argIndex = 0;
+ while (*in != '\0') {
+ if (*in == '%') {
+ if (in[1] == '%') {
+ in += 2;
+ out += "%";
+ continue;
+ }
- return readSelector(s->_segMan, argv[0], SELECTOR(data));
-}
+ assert(argIndex < argc);
+ out += readPlaceholder(in, argv[argIndex++]);
+ } else {
+ out += *in++;
+ }
+ }
-reg_t kStringLen(EngineState *s, int argc, reg_t *argv) {
- return make_reg(0, s->_segMan->strlen(argv[0]));
+ return out;
}
-reg_t kStringPrintf(EngineState *s, int argc, reg_t *argv) {
+reg_t kStringFormat(EngineState *s, int argc, reg_t *argv) {
reg_t stringHandle;
- s->_segMan->allocateString(&stringHandle);
-
- reg_t *adjustedArgs = new reg_t[argc + 1];
- adjustedArgs[0] = stringHandle;
- memcpy(&adjustedArgs[1], argv, argc * sizeof(reg_t));
-
- kFormat(s, argc + 1, adjustedArgs);
- delete[] adjustedArgs;
+ SciArray &target = *s->_segMan->allocateArray(kArrayTypeString, 0, &stringHandle);
+ reg_t source = argv[0];
+ // Str objects may be passed in place of direct references to string data
+ if (s->_segMan->isObject(argv[0])) {
+ source = readSelector(s->_segMan, argv[0], SELECTOR(data));
+ }
+ target.fromString(format(s->_segMan->getString(source), argc - 1, argv + 1));
return stringHandle;
}
-reg_t kStringPrintfBuf(EngineState *s, int argc, reg_t *argv) {
- return kFormat(s, argc, argv);
+reg_t kStringFormatAt(EngineState *s, int argc, reg_t *argv) {
+ SciArray &target = *s->_segMan->lookupArray(argv[0]);
+ reg_t source = argv[1];
+ // Str objects may be passed in place of direct references to string data
+ if (s->_segMan->isObject(argv[1])) {
+ source = readSelector(s->_segMan, argv[1], SELECTOR(data));
+ }
+ target.fromString(format(s->_segMan->getString(source), argc - 2, argv + 2));
+ return argv[0];
}
-reg_t kStringAtoi(EngineState *s, int argc, reg_t *argv) {
- Common::String string = s->_segMan->getString(argv[0]);
- return make_reg(0, (uint16)atoi(string.c_str()));
+reg_t kStringToInteger(EngineState *s, int argc, reg_t *argv) {
+ return make_reg(0, (uint16)s->_segMan->getString(argv[0]).asUint64());
}
reg_t kStringTrim(EngineState *s, int argc, reg_t *argv) {
- Common::String string = s->_segMan->getString(argv[0]);
-
- string.trim();
- // TODO: Second parameter (bitfield, trim from left, right, center)
- warning("kStringTrim (%d)", argv[1].getOffset());
- s->_segMan->strcpy(argv[0], string.c_str());
- return NULL_REG;
+ SciArray &array = *s->_segMan->lookupArray(argv[0]);
+ const int8 flags = argv[1].toSint16();
+ const char showChar = argc > 2 ? argv[2].toSint16() : '\0';
+ array.trim(flags, showChar);
+ return s->r_acc;
}
-reg_t kStringUpper(EngineState *s, int argc, reg_t *argv) {
+reg_t kStringToUpperCase(EngineState *s, int argc, reg_t *argv) {
Common::String string = s->_segMan->getString(argv[0]);
-
string.toUppercase();
s->_segMan->strcpy(argv[0], string.c_str());
- return NULL_REG;
+ return argv[0];
}
-reg_t kStringLower(EngineState *s, int argc, reg_t *argv) {
+reg_t kStringToLowerCase(EngineState *s, int argc, reg_t *argv) {
Common::String string = s->_segMan->getString(argv[0]);
-
string.toLowercase();
s->_segMan->strcpy(argv[0], string.c_str());
- return NULL_REG;
-}
-
-// Possibly kStringTranslate?
-reg_t kStringTrn(EngineState *s, int argc, reg_t *argv) {
- warning("kStringTrn (argc = %d)", argc);
- return NULL_REG;
+ return argv[0];
}
-// Possibly kStringTranslateExclude?
-reg_t kStringTrnExclude(EngineState *s, int argc, reg_t *argv) {
- warning("kStringTrnExclude (argc = %d)", argc);
- return NULL_REG;
+reg_t kStringReplaceSubstring(EngineState *s, int argc, reg_t *argv) {
+ error("TODO: kStringReplaceSubstring not implemented");
+ return argv[3];
}
-reg_t kString(EngineState *s, int argc, reg_t *argv) {
- if (!s)
- return make_reg(0, getSciVersion());
- error("not supposed to call this");
+reg_t kStringReplaceSubstringEx(EngineState *s, int argc, reg_t *argv) {
+ error("TODO: kStringReplaceSubstringEx not implemented");
+ return argv[3];
}
-
#endif
} // End of namespace Sci
diff --git a/engines/sci/engine/kvideo.cpp b/engines/sci/engine/kvideo.cpp
index b539c84f5d..83a02883af 100644
--- a/engines/sci/engine/kvideo.cpp
+++ b/engines/sci/engine/kvideo.cpp
@@ -165,7 +165,6 @@ reg_t kShowMovie(EngineState *s, int argc, reg_t *argv) {
switch (argv[0].toUint16()) {
case 0: {
Common::String filename = s->_segMan->getString(argv[1]);
- videoDecoder = new Video::AVIDecoder();
if (filename.equalsIgnoreCase("gk2a.avi")) {
// HACK: Switch to 16bpp graphics for Indeo3.
@@ -180,6 +179,8 @@ reg_t kShowMovie(EngineState *s, int argc, reg_t *argv) {
}
}
+ videoDecoder = new Video::AVIDecoder();
+
if (!videoDecoder->loadFile(filename.c_str())) {
warning("Failed to open movie file %s", filename.c_str());
delete videoDecoder;
@@ -253,11 +254,13 @@ reg_t kRobotGetFrameSize(EngineState *s, int argc, reg_t *argv) {
Common::Rect frameRect;
const uint16 numFramesTotal = g_sci->_video32->getRobotPlayer().getFrameSize(frameRect);
- reg_t *outRect = s->_segMan->derefRegPtr(argv[0], 4);
- outRect[0] = make_reg(0, frameRect.left);
- outRect[1] = make_reg(0, frameRect.top);
- outRect[2] = make_reg(0, frameRect.right - 1);
- outRect[3] = make_reg(0, frameRect.bottom - 1);
+ SciArray *outRect = s->_segMan->lookupArray(argv[0]);
+ reg_t values[4] = {
+ make_reg(0, frameRect.left),
+ make_reg(0, frameRect.top),
+ make_reg(0, frameRect.right - 1),
+ make_reg(0, frameRect.bottom - 1) };
+ outRect->setElements(0, 4, values);
return make_reg(0, numFramesTotal);
}
@@ -446,10 +449,10 @@ reg_t kPlayVMDSetBlackoutArea(EngineState *s, int argc, reg_t *argv) {
const int16 scriptHeight = g_sci->_gfxFrameout->getCurrentBuffer().scriptHeight;
Common::Rect blackoutArea;
- blackoutArea.left = MAX((int16)0, argv[0].toSint16());
- blackoutArea.top = MAX((int16)0, argv[1].toSint16());
- blackoutArea.right = MIN(scriptWidth, (int16)(argv[2].toSint16() + 1));
- blackoutArea.bottom = MIN(scriptHeight, (int16)(argv[3].toSint16() + 1));
+ blackoutArea.left = MAX<int16>(0, argv[0].toSint16());
+ blackoutArea.top = MAX<int16>(0, argv[1].toSint16());
+ blackoutArea.right = MIN<int16>(scriptWidth, argv[2].toSint16() + 1);
+ blackoutArea.bottom = MIN<int16>(scriptHeight, argv[3].toSint16() + 1);
g_sci->_video32->getVMDPlayer().setBlackoutArea(blackoutArea);
return s->r_acc;
}
diff --git a/engines/sci/engine/message.cpp b/engines/sci/engine/message.cpp
index 5300b72b71..b0615b4213 100644
--- a/engines/sci/engine/message.cpp
+++ b/engines/sci/engine/message.cpp
@@ -439,21 +439,8 @@ Common::String MessageState::processString(const char *s) {
void MessageState::outputString(reg_t buf, const Common::String &str) {
#ifdef ENABLE_SCI32
if (getSciVersion() >= SCI_VERSION_2) {
- if (_segMan->getSegmentType(buf.getSegment()) == SEG_TYPE_STRING) {
- SciString *sciString = _segMan->lookupString(buf);
- sciString->setSize(str.size() + 1);
- for (uint32 i = 0; i < str.size(); i++)
- sciString->setValue(i, str.c_str()[i]);
- sciString->setValue(str.size(), 0);
- } else if (_segMan->getSegmentType(buf.getSegment()) == SEG_TYPE_ARRAY) {
- // Happens in the intro of LSL6, we are asked to write the string
- // into an array
- SciArray<reg_t> *sciString = _segMan->lookupArray(buf);
- sciString->setSize(str.size() + 1);
- for (uint32 i = 0; i < str.size(); i++)
- sciString->setValue(i, make_reg(0, str.c_str()[i]));
- sciString->setValue(str.size(), NULL_REG);
- }
+ SciArray *sciString = _segMan->lookupArray(buf);
+ sciString->fromString(str);
} else {
#endif
SegmentRef buffer_r = _segMan->dereference(buf);
diff --git a/engines/sci/engine/savegame.cpp b/engines/sci/engine/savegame.cpp
index be2d7660cb..2e4da46b70 100644
--- a/engines/sci/engine/savegame.cpp
+++ b/engines/sci/engine/savegame.cpp
@@ -102,66 +102,6 @@ void syncWithSerializer(Common::Serializer &s, Node &obj) {
syncWithSerializer(s, obj.value);
}
-#ifdef ENABLE_SCI32
-void syncWithSerializer(Common::Serializer &s, SciArray<reg_t> &obj) {
- byte type = 0;
- uint32 size = 0;
-
- if (s.isSaving()) {
- type = (byte)obj.getType();
- size = obj.getSize();
- }
- s.syncAsByte(type);
- s.syncAsUint32LE(size);
- if (s.isLoading()) {
- obj.setType((int8)type);
-
- // HACK: Skip arrays that have a negative type
- if ((int8)type < 0)
- return;
-
- obj.setSize(size);
- }
-
- for (uint32 i = 0; i < size; i++) {
- reg_t value;
-
- if (s.isSaving())
- value = obj.getValue(i);
-
- syncWithSerializer(s, value);
-
- if (s.isLoading())
- obj.setValue(i, value);
- }
-}
-
-void syncWithSerializer(Common::Serializer &s, SciString &obj) {
- uint32 size = 0;
-
- if (s.isSaving()) {
- size = obj.getSize();
- s.syncAsUint32LE(size);
- } else {
- s.syncAsUint32LE(size);
- obj.setSize(size);
- }
-
- for (uint32 i = 0; i < size; i++) {
- char value = 0;
-
- if (s.isSaving())
- value = obj.getValue(i);
-
- s.syncAsByte(value);
-
- if (s.isLoading())
- obj.setValue(i, value);
- }
-}
-
-#endif
-
#pragma mark -
// By default, sync using syncWithSerializer, which in turn can easily be overloaded.
@@ -292,9 +232,6 @@ void SegManager::saveLoadWithSerializer(Common::Serializer &s) {
} else if (type == SEG_TYPE_ARRAY) {
// Set the correct segment for SCI32 arrays
_arraysSegId = i;
- } else if (type == SEG_TYPE_STRING) {
- // Set the correct segment for SCI32 strings
- _stringSegId = i;
} else if (s.getVersion() >= 36 && type == SEG_TYPE_BITMAP) {
_bitmapSegId = i;
#endif
@@ -393,6 +330,28 @@ static void sync_SavegameMetadata(Common::Serializer &s, SavegameMetadata &obj)
}
s.syncAsUint32LE(obj.playTime);
}
+
+ // Some games require additional metadata to display their restore screens
+ // correctly
+ if (s.getVersion() >= 39) {
+ if (s.isSaving()) {
+ const reg_t *globals = g_sci->getEngineState()->variables[VAR_GLOBAL];
+ if (g_sci->getGameId() == GID_SHIVERS) {
+ obj.lowScore = globals[kGlobalVarScore].toUint16();
+ obj.highScore = globals[kGlobalVarShivers1Score].toUint16();
+ obj.avatarId = 0;
+ } else if (g_sci->getGameId() == GID_MOTHERGOOSEHIRES) {
+ obj.lowScore = obj.highScore = 0;
+ obj.avatarId = readSelectorValue(g_sci->getEngineState()->_segMan, globals[kGlobalVarEgo], SELECTOR(view));
+ } else {
+ obj.lowScore = obj.highScore = obj.avatarId = 0;
+ }
+ }
+
+ s.syncAsUint16LE(obj.lowScore);
+ s.syncAsUint16LE(obj.highScore);
+ s.syncAsByte(obj.avatarId);
+ }
}
void EngineState::saveLoadWithSerializer(Common::Serializer &s) {
@@ -673,13 +632,16 @@ void SoundCommandParser::syncPlayList(Common::Serializer &s) {
}
void SoundCommandParser::reconstructPlayList() {
- Common::StackLock lock(_music->_mutex);
+ _music->_mutex.lock();
// We store all songs here because starting songs may re-shuffle their order
MusicList songs;
for (MusicList::iterator i = _music->getPlayListStart(); i != _music->getPlayListEnd(); ++i)
songs.push_back(*i);
+ // Done with main playlist, so release lock
+ _music->_mutex.unlock();
+
for (MusicList::iterator i = songs.begin(); i != songs.end(); ++i) {
initSoundResource(*i);
@@ -707,11 +669,35 @@ void ArrayTable::saveLoadWithSerializer(Common::Serializer &ser) {
sync_Table<ArrayTable>(ser, *this);
}
-void StringTable::saveLoadWithSerializer(Common::Serializer &ser) {
- if (ser.getVersion() < 18)
- return;
+void SciArray::saveLoadWithSerializer(Common::Serializer &s) {
+ uint16 savedSize;
+
+ if (s.isSaving()) {
+ savedSize = _size;
+ }
+
+ s.syncAsByte(_type);
+ s.syncAsByte(_elementSize);
+ s.syncAsUint16LE(savedSize);
+
+ if (s.isLoading()) {
+ resize(savedSize);
+ }
- sync_Table<StringTable>(ser, *this);
+ switch (_type) {
+ case kArrayTypeInt16:
+ case kArrayTypeID:
+ for (int i = 0; i < savedSize; ++i) {
+ syncWithSerializer(s, ((reg_t *)_data)[i]);
+ }
+ break;
+ case kArrayTypeByte:
+ case kArrayTypeString:
+ s.syncBytes((byte *)_data, savedSize);
+ break;
+ default:
+ error("Attempt to sync invalid SciArray type %d", _type);
+ }
}
void BitmapTable::saveLoadWithSerializer(Common::Serializer &ser) {
@@ -784,7 +770,7 @@ void GfxPalette::saveLoadWithSerializer(Common::Serializer &s) {
}
#ifdef ENABLE_SCI32
-void saveLoadPalette32(Common::Serializer &s, Palette *const palette) {
+static void saveLoadPalette32(Common::Serializer &s, Palette *const palette) {
s.syncAsUint32LE(palette->timestamp);
for (int i = 0; i < ARRAYSIZE(palette->colors); ++i) {
s.syncAsByte(palette->colors[i].used);
@@ -794,7 +780,7 @@ void saveLoadPalette32(Common::Serializer &s, Palette *const palette) {
}
}
-void saveLoadOptionalPalette32(Common::Serializer &s, Palette **const palette) {
+static void saveLoadOptionalPalette32(Common::Serializer &s, Palette **const palette) {
bool hasPalette;
if (s.isSaving()) {
hasPalette = (*palette != nullptr);
@@ -815,6 +801,16 @@ void GfxPalette32::saveLoadWithSerializer(Common::Serializer &s) {
if (s.isLoading()) {
++_version;
+
+ for (int i = 0; i < kNumCyclers; ++i) {
+ delete _cyclers[i];
+ _cyclers[i] = nullptr;
+ }
+
+ delete _varyTargetPalette;
+ _varyTargetPalette = nullptr;
+ delete _varyStartPalette;
+ _varyStartPalette = nullptr;
}
s.syncAsSint16LE(_varyDirection);
@@ -1044,10 +1040,11 @@ bool gamestate_save(EngineState *s, Common::WriteStream *fh, const Common::Strin
meta.gameObjectOffset = g_sci->getGameObject().getOffset();
// Checking here again
- if (s->executionStackBase) {
- warning("Cannot save from below kernel function");
- return false;
- }
+// TODO: This breaks Torin autosave, is there actually any reason for it?
+// if (s->executionStackBase) {
+// warning("Cannot save from below kernel function");
+// return false;
+// }
Common::Serializer ser(0, fh);
sync_SavegameMetadata(ser, meta);
diff --git a/engines/sci/engine/savegame.h b/engines/sci/engine/savegame.h
index 6616081a20..873394aebb 100644
--- a/engines/sci/engine/savegame.h
+++ b/engines/sci/engine/savegame.h
@@ -37,6 +37,7 @@ struct EngineState;
*
* Version - new/changed feature
* =============================
+ * 39 - Accurate SCI32 arrays/strings, score metadata, avatar metadata
* 38 - SCI32 cursor
* 37 - Segment entry data changed to pointers
* 36 - SCI32 bitmap segment
@@ -63,7 +64,7 @@ struct EngineState;
*/
enum {
- CURRENT_SAVEGAME_VERSION = 38,
+ CURRENT_SAVEGAME_VERSION = 39,
MINIMUM_SAVEGAME_VERSION = 14
};
@@ -77,6 +78,13 @@ struct SavegameMetadata {
uint32 playTime;
uint16 gameObjectOffset;
uint16 script0Size;
+
+ // Used by Shivers 1
+ uint16 lowScore;
+ uint16 highScore;
+
+ // Used by MGDX
+ uint8 avatarId;
};
/**
diff --git a/engines/sci/engine/script_patches.cpp b/engines/sci/engine/script_patches.cpp
index 664e7fa0c4..f2f290fa9e 100644
--- a/engines/sci/engine/script_patches.cpp
+++ b/engines/sci/engine/script_patches.cpp
@@ -103,6 +103,9 @@ static const char *const selectorNameTable[] = {
"modNum", // King's Quest 6 CD / Laura Bow 2 CD for audio+text support
"cycler", // Space Quest 4 / system selector
"setLoop", // Laura Bow 1 Colonel's Bequest
+#ifdef ENABLE_SCI32
+ "newWith", // SCI2 array script
+#endif
NULL
};
@@ -132,8 +135,86 @@ enum ScriptPatcherSelectors {
SELECTOR_modNum,
SELECTOR_cycler,
SELECTOR_setLoop
+#ifdef ENABLE_SCI32
+ ,
+ SELECTOR_newWith
+#endif
+};
+
+#ifdef ENABLE_SCI32
+// It is not possible to change the directory for ScummVM save games, so disable
+// the "change directory" button in the standard save dialogue
+static const uint16 sci2ChangeDirSignature[] = {
+ 0x72, SIG_ADDTOOFFSET(+2), // lofsa changeDirI
+ 0x4a, SIG_UINT16(0x04), // send 4
+ SIG_MAGICDWORD,
+ 0x36, // push
+ 0x35, 0xF7, // ldi $f7
+ 0x12, // and
+ 0x36, // push
+ SIG_END
+};
+
+static const uint16 sci2ChangeDirPatch[] = {
+ PATCH_ADDTOOFFSET(+3), // lofsa changeDirI
+ PATCH_ADDTOOFFSET(+3), // send 4
+ PATCH_ADDTOOFFSET(+1), // push
+ 0x35, 0x00, // ldi 0
+ PATCH_END
+};
+
+// Save game script hardcodes the maximum number of save games to 20, but
+// this is an artificial constraint that does not apply to ScummVM
+static const uint16 sci2NumSavesSignature1[] = {
+ SIG_MAGICDWORD,
+ 0x8b, 0x02, // lsl local[2]
+ 0x35, 0x14, // ldi 20
+ 0x22, // lt?
+ SIG_END
+};
+
+static const uint16 sci2NumSavesPatch1[] = {
+ PATCH_ADDTOOFFSET(+2), // lsl local[2]
+ 0x35, 0x63, // ldi 99
+ PATCH_END
+};
+
+static const uint16 sci2NumSavesSignature2[] = {
+ SIG_MAGICDWORD,
+ 0x8b, 0x02, // lsl local[2]
+ 0x35, 0x14, // ldi 20
+ 0x1a, // eq?
+ SIG_END
+};
+
+static const uint16 sci2NumSavesPatch2[] = {
+ PATCH_ADDTOOFFSET(+2), // lsl local[2]
+ 0x35, 0x63, // ldi 99
+ PATCH_END
};
+// Phantasmagoria & SQ6 try to initialize the first entry of an int16 array
+// using an empty string, which is not valid (it should be a number)
+static const uint16 sci21IntArraySignature[] = {
+ 0x38, SIG_SELECTOR16(newWith), // pushi newWith
+ 0x7a, // push2
+ 0x39, 0x04, // pushi $4
+ 0x72, SIG_ADDTOOFFSET(+2), // lofsa string ""
+ SIG_MAGICDWORD,
+ 0x36, // push
+ 0x51, 0x0b, // class IntArray
+ 0x4a, 0x8, // send $8
+ SIG_END
+};
+
+static const uint16 sci21IntArrayPatch[] = {
+ PATCH_ADDTOOFFSET(+6), // push $b9; push2; pushi $4
+ 0x76, // push0
+ 0x34, PATCH_UINT16(0x0001), // ldi 0001 (waste bytes)
+ PATCH_END
+};
+#endif
+
// ===========================================================================
// Conquests of Camelot
// At the bazaar in Jerusalem, it's possible to see a girl taking a shower.
@@ -614,6 +695,10 @@ static const SciScriptPatcherEntry freddypharkasSignatures[] = {
SCI_SIGNATUREENTRY_TERMINATOR
};
+#ifdef ENABLE_SCI32
+#pragma mark -
+#pragma mark Gabriel Knight 1
+
// ===========================================================================
// daySixBeignet::changeState (4) is called when the cop goes out and sets cycles to 220.
// this is not enough time to get to the door, so we patch that to 23 seconds
@@ -918,9 +1003,25 @@ static const SciScriptPatcherEntry gk1Signatures[] = {
{ true, 230, "day 6 police beignet timer issue", 1, gk1SignatureDay6PoliceBeignet, gk1PatchDay6PoliceBeignet },
{ true, 230, "day 6 police sleep timer issue", 1, gk1SignatureDay6PoliceSleep, gk1PatchDay6PoliceSleep },
{ true, 808, "day 10 gabriel dress up infinite turning", 1, gk1SignatureDay10GabrielDressUp, gk1PatchDay10GabrielDressUp },
+ { true, 64990, "increase number of save games", 1, sci2NumSavesSignature1, sci2NumSavesPatch1 },
+ { true, 64990, "increase number of save games", 1, sci2NumSavesSignature2, sci2NumSavesPatch2 },
+ { true, 64990, "disable change directory button", 1, sci2ChangeDirSignature, sci2ChangeDirPatch },
+ SCI_SIGNATUREENTRY_TERMINATOR
+};
+
+#pragma mark -
+#pragma mark Gabriel Knight 2
+
+// script, description, signature patch
+static const SciScriptPatcherEntry gk2Signatures[] = {
+ { true, 64990, "increase number of save games", 1, sci2NumSavesSignature1, sci2NumSavesPatch1 },
+ { true, 64990, "increase number of save games", 1, sci2NumSavesSignature2, sci2NumSavesPatch2 },
+ { true, 64990, "disable change directory button", 1, sci2ChangeDirSignature, sci2ChangeDirPatch },
SCI_SIGNATUREENTRY_TERMINATOR
};
+#endif
+
// ===========================================================================
// at least during harpy scene export 29 of script 0 is called in kq5cd and
// has an issue for those calls, where temp 3 won't get inititialized, but
@@ -1618,6 +1719,10 @@ static const SciScriptPatcherEntry kq6Signatures[] = {
SCI_SIGNATUREENTRY_TERMINATOR
};
+#ifdef ENABLE_SCI32
+#pragma mark -
+#pragma mark Kings Quest 7
+
// ===========================================================================
// King's Quest 7 has really weird subtitles. It seems as if the subtitles were
@@ -1753,7 +1858,7 @@ static const uint16 kq7SignatureSubtitleFix3[] = {
static const uint16 kq7PatchSubtitleFix3[] = {
PATCH_ADDTOOFFSET(+2), // skip over "pToa initialized code"
0x2f, 0x0c, // bt [skip init code] - saved 1 byte
- 0x38,
+ 0x38,
PATCH_GETORIGINALBYTE(+6),
PATCH_GETORIGINALBYTE(+7), // pushi (init)
0x76, // push0
@@ -1779,6 +1884,8 @@ static const SciScriptPatcherEntry kq7Signatures[] = {
SCI_SIGNATUREENTRY_TERMINATOR
};
+#endif
+
// ===========================================================================
// Script 210 in the German version of Longbow handles the case where Robin
// hands out the scroll to Marion and then types his name using the hand code.
@@ -2079,6 +2186,20 @@ static const SciScriptPatcherEntry larry6Signatures[] = {
SCI_SIGNATUREENTRY_TERMINATOR
};
+#ifdef ENABLE_SCI32
+#pragma mark -
+#pragma mark Leisure Suit Larry 6 Hires
+
+// script, description, signature patch
+static const SciScriptPatcherEntry larry6HiresSignatures[] = {
+ { true, 64990, "increase number of save games", 1, sci2NumSavesSignature1, sci2NumSavesPatch1 },
+ { true, 64990, "increase number of save games", 1, sci2NumSavesSignature2, sci2NumSavesPatch2 },
+ { true, 64990, "disable change directory button", 1, sci2ChangeDirSignature, sci2ChangeDirPatch },
+ SCI_SIGNATUREENTRY_TERMINATOR
+};
+
+#endif
+
// ===========================================================================
// Laura Bow 1 - Colonel's Bequest
//
@@ -2700,6 +2821,18 @@ static const SciScriptPatcherEntry mothergoose256Signatures[] = {
SCI_SIGNATUREENTRY_TERMINATOR
};
+#ifdef ENABLE_SCI32
+#pragma mark -
+#pragma mark Phantasmagoria
+
+// script, description, signature patch
+static const SciScriptPatcherEntry phantasmagoriaSignatures[] = {
+ { true, 901, "invalid array construction", 1, sci21IntArraySignature, sci21IntArrayPatch },
+ SCI_SIGNATUREENTRY_TERMINATOR
+};
+
+#endif
+
// ===========================================================================
// Police Quest 1 VGA
@@ -2841,6 +2974,20 @@ static const SciScriptPatcherEntry pq1vgaSignatures[] = {
SCI_SIGNATUREENTRY_TERMINATOR
};
+#ifdef ENABLE_SCI32
+#pragma mark -
+#pragma mark Police Quest 4
+
+// script, description, signature patch
+static const SciScriptPatcherEntry pq4Signatures[] = {
+ { true, 64990, "increase number of save games", 1, sci2NumSavesSignature1, sci2NumSavesPatch1 },
+ { true, 64990, "increase number of save games", 1, sci2NumSavesSignature2, sci2NumSavesPatch2 },
+ { true, 64990, "disable change directory button", 1, sci2ChangeDirSignature, sci2ChangeDirPatch },
+ SCI_SIGNATUREENTRY_TERMINATOR
+};
+
+#endif
+
// ===========================================================================
// At the healer's house there is a bird's nest up on the tree.
// The player can throw rocks at it until it falls to the ground.
@@ -3745,7 +3892,7 @@ static const uint16 qfg3PatchRoom750Bounds1[] = {
static const uint16 qfg3SignatureRoom750Bounds2[] = {
// (ego x: 294 y: 39)
0x78, // push1 ("x")
- 0x78, // push1
+ 0x78, // push1
0x38, SIG_UINT16(294), // pushi 294
0x76, // push0 ("y")
0x78, // push1
@@ -3808,6 +3955,20 @@ static const SciScriptPatcherEntry qfg3Signatures[] = {
SCI_SIGNATUREENTRY_TERMINATOR
};
+#ifdef ENABLE_SCI32
+#pragma mark -
+#pragma mark Quest for Glory 4
+
+// script, description, signature patch
+static const SciScriptPatcherEntry qfg4Signatures[] = {
+ { true, 64990, "increase number of save games", 1, sci2NumSavesSignature1, sci2NumSavesPatch1 },
+ { true, 64990, "increase number of save games", 1, sci2NumSavesSignature2, sci2NumSavesPatch2 },
+ { true, 64990, "disable change directory button", 1, sci2ChangeDirSignature, sci2ChangeDirPatch },
+ SCI_SIGNATUREENTRY_TERMINATOR
+};
+
+#endif
+
// ===========================================================================
// script 298 of sq4/floppy has an issue. object "nest" uses another property
// which isn't included in property count. We return 0 in that case, the game
@@ -4371,6 +4532,34 @@ static const SciScriptPatcherEntry sq5Signatures[] = {
SCI_SIGNATUREENTRY_TERMINATOR
};
+#ifdef ENABLE_SCI32
+#pragma mark -
+#pragma mark Space Quest 6
+
+// script, description, signature patch
+static const SciScriptPatcherEntry sq6Signatures[] = {
+ { true, 15, "invalid array construction", 1, sci21IntArraySignature, sci21IntArrayPatch },
+ { true, 22, "invalid array construction", 1, sci21IntArraySignature, sci21IntArrayPatch },
+ { true, 460, "invalid array construction", 1, sci21IntArraySignature, sci21IntArrayPatch },
+ { true, 510, "invalid array construction", 1, sci21IntArraySignature, sci21IntArrayPatch },
+ { true, 64990, "increase number of save games", 1, sci2NumSavesSignature1, sci2NumSavesPatch1 },
+ { true, 64990, "increase number of save games", 1, sci2NumSavesSignature2, sci2NumSavesPatch2 },
+ { true, 64990, "disable change directory button", 1, sci2ChangeDirSignature, sci2ChangeDirPatch },
+ SCI_SIGNATUREENTRY_TERMINATOR
+};
+
+#pragma mark -
+#pragma mark Torins Passage
+
+// script, description, signature patch
+static const SciScriptPatcherEntry torinSignatures[] = {
+ { true, 64990, "increase number of save games", 1, sci2NumSavesSignature1, sci2NumSavesPatch1 },
+ { true, 64990, "increase number of save games", 1, sci2NumSavesSignature2, sci2NumSavesPatch2 },
+ SCI_SIGNATUREENTRY_TERMINATOR
+};
+
+#endif
+
// =================================================================================
ScriptPatcher::ScriptPatcher() {
@@ -4802,18 +4991,25 @@ void ScriptPatcher::processScript(uint16 scriptNr, byte *scriptData, const uint3
case GID_FREDDYPHARKAS:
signatureTable = freddypharkasSignatures;
break;
+#ifdef ENABLE_SCI32
case GID_GK1:
signatureTable = gk1Signatures;
break;
+ case GID_GK2:
+ signatureTable = gk2Signatures;
+ break;
+#endif
case GID_KQ5:
signatureTable = kq5Signatures;
break;
case GID_KQ6:
signatureTable = kq6Signatures;
break;
+#ifdef ENABLE_SCI32
case GID_KQ7:
signatureTable = kq7Signatures;
break;
+#endif
case GID_LAURABOW:
signatureTable = laurabow1Signatures;
break;
@@ -4832,12 +5028,27 @@ void ScriptPatcher::processScript(uint16 scriptNr, byte *scriptData, const uint3
case GID_LSL6:
signatureTable = larry6Signatures;
break;
+#ifdef ENABLE_SCI32
+ case GID_LSL6HIRES:
+ signatureTable = larry6HiresSignatures;
+ break;
+#endif
case GID_MOTHERGOOSE256:
signatureTable = mothergoose256Signatures;
break;
+#ifdef ENABLE_SCI32
+ case GID_PHANTASMAGORIA:
+ signatureTable = phantasmagoriaSignatures;
+ break;
+#endif
case GID_PQ1:
signatureTable = pq1vgaSignatures;
break;
+#ifdef ENABLE_SCI32
+ case GID_PQ4:
+ signatureTable = pq4Signatures;
+ break;
+#endif
case GID_QFG1:
signatureTable = qfg1egaSignatures;
break;
@@ -4850,6 +5061,11 @@ void ScriptPatcher::processScript(uint16 scriptNr, byte *scriptData, const uint3
case GID_QFG3:
signatureTable = qfg3Signatures;
break;
+#ifdef ENABLE_SCI32
+ case GID_QFG4:
+ signatureTable = qfg4Signatures;
+ break;
+#endif
case GID_SQ1:
signatureTable = sq1vgaSignatures;
break;
@@ -4859,6 +5075,14 @@ void ScriptPatcher::processScript(uint16 scriptNr, byte *scriptData, const uint3
case GID_SQ5:
signatureTable = sq5Signatures;
break;
+#ifdef ENABLE_SCI32
+ case GID_SQ6:
+ signatureTable = sq6Signatures;
+ break;
+ case GID_TORIN:
+ signatureTable = torinSignatures;
+ break;
+#endif
default:
break;
}
diff --git a/engines/sci/engine/seg_manager.cpp b/engines/sci/engine/seg_manager.cpp
index 5cf8d6162d..83e7495b3c 100644
--- a/engines/sci/engine/seg_manager.cpp
+++ b/engines/sci/engine/seg_manager.cpp
@@ -42,7 +42,6 @@ SegManager::SegManager(ResourceManager *resMan, ScriptPatcher *scriptPatcher)
#ifdef ENABLE_SCI32
_arraysSegId = 0;
- _stringSegId = 0;
_bitmapSegId = 0;
#endif
@@ -72,7 +71,6 @@ void SegManager::resetSegMan() {
#ifdef ENABLE_SCI32
_arraysSegId = 0;
- _stringSegId = 0;
_bitmapSegId = 0;
#endif
@@ -88,9 +86,8 @@ void SegManager::initSysStrings() {
_parserPtr = make_reg(_saveDirPtr.getSegment(), _saveDirPtr.getOffset() + 256);
#ifdef ENABLE_SCI32
} else {
- SciString *saveDirString = allocateString(&_saveDirPtr);
- saveDirString->setSize(256);
- saveDirString->setValue(0, 0);
+ SciArray *saveDirString = allocateArray(kArrayTypeString, 256, &_saveDirPtr);
+ saveDirString->byteAt(0) = '\0';
_parserPtr = NULL_REG; // no SCI2 game had a parser
#endif
@@ -863,7 +860,10 @@ bool SegManager::freeDynmem(reg_t addr) {
}
#ifdef ENABLE_SCI32
-SciArray<reg_t> *SegManager::allocateArray(reg_t *addr) {
+#pragma mark -
+#pragma mark Arrays
+
+SciArray *SegManager::allocateArray(SciArrayType type, uint16 size, reg_t *addr) {
ArrayTable *table;
int offset;
@@ -875,10 +875,14 @@ SciArray<reg_t> *SegManager::allocateArray(reg_t *addr) {
offset = table->allocEntry();
*addr = make_reg(_arraysSegId, offset);
- return &table->at(offset);
+
+ SciArray *array = &table->at(offset);
+ array->setType(type);
+ array->resize(size);
+ return array;
}
-SciArray<reg_t> *SegManager::lookupArray(reg_t addr) {
+SciArray *SegManager::lookupArray(reg_t addr) {
if (_heap[addr.getSegment()]->getType() != SEG_TYPE_ARRAY)
error("Attempt to use non-array %04x:%04x as array", PRINT_REG(addr));
@@ -899,54 +903,17 @@ void SegManager::freeArray(reg_t addr) {
if (!arrayTable.isValidEntry(addr.getOffset()))
error("Attempt to use non-array %04x:%04x as array", PRINT_REG(addr));
- arrayTable[addr.getOffset()].destroy();
arrayTable.freeEntry(addr.getOffset());
}
-SciString *SegManager::allocateString(reg_t *addr) {
- StringTable *table;
- int offset;
-
- if (!_stringSegId) {
- table = (StringTable *)allocSegment(new StringTable(), &(_stringSegId));
- } else
- table = (StringTable *)_heap[_stringSegId];
-
- offset = table->allocEntry();
-
- *addr = make_reg(_stringSegId, offset);
- return &table->at(offset);
-}
-
-SciString *SegManager::lookupString(reg_t addr) {
- if (_heap[addr.getSegment()]->getType() != SEG_TYPE_STRING)
- error("lookupString: Attempt to use non-string %04x:%04x as string", PRINT_REG(addr));
-
- StringTable &stringTable = *(StringTable *)_heap[addr.getSegment()];
-
- if (!stringTable.isValidEntry(addr.getOffset()))
- error("lookupString: Attempt to use non-string %04x:%04x as string", PRINT_REG(addr));
-
- return &(stringTable[addr.getOffset()]);
-}
-
-void SegManager::freeString(reg_t addr) {
- if (_heap[addr.getSegment()]->getType() != SEG_TYPE_STRING)
- error("freeString: Attempt to use non-string %04x:%04x as string", PRINT_REG(addr));
-
- StringTable &stringTable = *(StringTable *)_heap[addr.getSegment()];
-
- if (!stringTable.isValidEntry(addr.getOffset()))
- error("freeString: Attempt to use non-string %04x:%04x as string", PRINT_REG(addr));
-
- stringTable[addr.getOffset()].destroy();
- stringTable.freeEntry(addr.getOffset());
+bool SegManager::isArray(reg_t addr) const {
+ return addr.getSegment() == _arraysSegId;
}
#pragma mark -
#pragma mark Bitmaps
-SciBitmap *SegManager::allocateBitmap(reg_t *addr, const int16 width, const int16 height, const uint8 skipColor, const int16 displaceX, const int16 displaceY, const int16 scaledWidth, const int16 scaledHeight, const uint32 paletteSize, const bool remap, const bool gc) {
+SciBitmap *SegManager::allocateBitmap(reg_t *addr, const int16 width, const int16 height, const uint8 skipColor, const int16 originX, const int16 originY, const int16 xResolution, const int16 yResolution, const uint32 paletteSize, const bool remap, const bool gc) {
BitmapTable *table;
int offset;
@@ -961,7 +928,7 @@ SciBitmap *SegManager::allocateBitmap(reg_t *addr, const int16 width, const int1
*addr = make_reg(_bitmapSegId, offset);
SciBitmap &bitmap = table->at(offset);
- bitmap.create(width, height, skipColor, displaceX, displaceY, scaledWidth, scaledHeight, paletteSize, remap, gc);
+ bitmap.create(width, height, skipColor, originX, originY, xResolution, yResolution, paletteSize, remap, gc);
return &bitmap;
}
diff --git a/engines/sci/engine/seg_manager.h b/engines/sci/engine/seg_manager.h
index 8ed1c3a143..c409744711 100644
--- a/engines/sci/engine/seg_manager.h
+++ b/engines/sci/engine/seg_manager.h
@@ -433,16 +433,12 @@ public:
reg_t getParserPtr() const { return _parserPtr; }
#ifdef ENABLE_SCI32
- SciArray<reg_t> *allocateArray(reg_t *addr);
- SciArray<reg_t> *lookupArray(reg_t addr);
+ SciArray *allocateArray(SciArrayType type, uint16 size, reg_t *addr);
+ SciArray *lookupArray(reg_t addr);
void freeArray(reg_t addr);
+ bool isArray(reg_t addr) const;
- SciString *allocateString(reg_t *addr);
- SciString *lookupString(reg_t addr);
- void freeString(reg_t addr);
- SegmentId getStringSegmentId() { return _stringSegId; }
-
- SciBitmap *allocateBitmap(reg_t *addr, const int16 width, const int16 height, const uint8 skipColor = kDefaultSkipColor, const int16 displaceX = 0, const int16 displaceY = 0, const int16 scaledWidth = kLowResX, const int16 scaledHeight = kLowResY, const uint32 paletteSize = 0, const bool remap = false, const bool gc = true);
+ SciBitmap *allocateBitmap(reg_t *addr, const int16 width, const int16 height, const uint8 skipColor = kDefaultSkipColor, const int16 originX = 0, const int16 originY = 0, const int16 xResolution = kLowResX, const int16 yResolution = kLowResY, const uint32 paletteSize = 0, const bool remap = false, const bool gc = true);
SciBitmap *lookupBitmap(reg_t addr);
void freeBitmap(reg_t addr);
#endif
@@ -469,7 +465,6 @@ private:
#ifdef ENABLE_SCI32
SegmentId _arraysSegId;
- SegmentId _stringSegId;
SegmentId _bitmapSegId;
#endif
diff --git a/engines/sci/engine/segment.cpp b/engines/sci/engine/segment.cpp
index 7943946ee4..fffa7f4d7e 100644
--- a/engines/sci/engine/segment.cpp
+++ b/engines/sci/engine/segment.cpp
@@ -67,9 +67,6 @@ SegmentObj *SegmentObj::createSegmentObj(SegmentType type) {
case SEG_TYPE_ARRAY:
mem = new ArrayTable();
break;
- case SEG_TYPE_STRING:
- mem = new StringTable();
- break;
case SEG_TYPE_BITMAP:
mem = new BitmapTable();
break;
@@ -254,63 +251,39 @@ SegmentRef DynMem::dereference(reg_t pointer) {
SegmentRef ArrayTable::dereference(reg_t pointer) {
SegmentRef ret;
- ret.isRaw = false;
- ret.maxSize = at(pointer.getOffset()).getSize() * 2;
- ret.reg = at(pointer.getOffset()).getRawData();
- return ret;
-}
-void ArrayTable::freeAtAddress(SegManager *segMan, reg_t sub_addr) {
- at(sub_addr.getOffset()).destroy();
- freeEntry(sub_addr.getOffset());
+ SciArray &array = at(pointer.getOffset());
+ const bool isRaw = array.getType() == kArrayTypeByte || array.getType() == kArrayTypeString;
+
+ ret.isRaw = isRaw;
+ ret.maxSize = array.byteSize();
+ if (isRaw) {
+ ret.raw = (byte *)array.getRawData();
+ } else {
+ ret.reg = (reg_t *)array.getRawData();
+ }
+ return ret;
}
Common::Array<reg_t> ArrayTable::listAllOutgoingReferences(reg_t addr) const {
- Common::Array<reg_t> tmp;
+ Common::Array<reg_t> refs;
if (!isValidEntry(addr.getOffset())) {
- error("Invalid array referenced for outgoing references: %04x:%04x", PRINT_REG(addr));
+ // Scripts may still hold references to array memory that has been
+ // explicitly freed; ignore these references
+ return refs;
}
- const SciArray<reg_t> *array = &at(addr.getOffset());
-
- for (uint32 i = 0; i < array->getSize(); i++) {
- reg_t value = array->getValue(i);
- if (value.getSegment() != 0)
- tmp.push_back(value);
+ SciArray &array = const_cast<SciArray &>(at(addr.getOffset()));
+ if (array.getType() == kArrayTypeID || array.getType() == kArrayTypeInt16) {
+ for (uint16 i = 0; i < array.size(); ++i) {
+ const reg_t value = array.getAsID(i);
+ if (value.isPointer()) {
+ refs.push_back(value);
+ }
+ }
}
- return tmp;
-}
-
-Common::String SciString::toString() const {
- if (_type != 3)
- error("SciString::toString(): Array is not a string");
-
- Common::String string;
- for (uint32 i = 0; i < _size && _data[i] != 0; i++)
- string += _data[i];
-
- return string;
-}
-
-void SciString::fromString(const Common::String &string) {
- if (_type != 3)
- error("SciString::fromString(): Array is not a string");
-
- setSize(string.size() + 1);
-
- for (uint32 i = 0; i < string.size(); i++)
- _data[i] = string[i];
-
- _data[string.size()] = 0;
-}
-
-SegmentRef StringTable::dereference(reg_t pointer) {
- SegmentRef ret;
- ret.isRaw = true;
- ret.maxSize = at(pointer.getOffset()).getSize();
- ret.raw = (byte *)at(pointer.getOffset()).getRawData();
- return ret;
+ return refs;
}
#endif
diff --git a/engines/sci/engine/segment.h b/engines/sci/engine/segment.h
index add5f4c57c..361c1cb895 100644
--- a/engines/sci/engine/segment.h
+++ b/engines/sci/engine/segment.h
@@ -24,7 +24,7 @@
#define SCI_ENGINE_SEGMENT_H
#include "common/serializer.h"
-
+#include "common/str.h"
#include "sci/engine/object.h"
#include "sci/engine/vm.h"
#include "sci/engine/vm_types.h" // for reg_t
@@ -70,7 +70,7 @@ enum SegmentType {
#ifdef ENABLE_SCI32
SEG_TYPE_ARRAY = 11,
- SEG_TYPE_STRING = 12,
+ // 12 used to be string, now obsolete
SEG_TYPE_BITMAP = 13,
#endif
@@ -408,142 +408,456 @@ public:
#ifdef ENABLE_SCI32
-template<typename T>
-class SciArray {
+#pragma mark -
+#pragma mark Arrays
+
+enum SciArrayType {
+ kArrayTypeInt16 = 0,
+ kArrayTypeID = 1,
+ kArrayTypeByte = 2,
+ kArrayTypeString = 3,
+ // Type 4 was for 32-bit integers; never used
+ kArrayTypeInvalid = 5
+};
+
+enum SciArrayTrim {
+ kArrayTrimRight = 1, ///< Trim whitespace after the last non-whitespace character
+ kArrayTrimCenter = 2, ///< Trim whitespace between non-whitespace characters
+ kArrayTrimLeft = 4 ///< Trim whitespace before the first non-whitespace character
+};
+
+class SciArray : public Common::Serializable {
public:
- SciArray() : _type(-1), _data(NULL), _size(0), _actualSize(0) { }
+ SciArray() :
+ _type(kArrayTypeInvalid),
+ _size(0),
+ _data(nullptr) {}
- SciArray(const SciArray<T> &array) {
+ SciArray(const SciArray &array) {
_type = array._type;
_size = array._size;
- _actualSize = array._actualSize;
- _data = new T[_actualSize];
+ _elementSize = array._elementSize;
+ _data = malloc(_elementSize * _size);
assert(_data);
- memcpy(_data, array._data, _size * sizeof(T));
+ memcpy(_data, array._data, _elementSize * _size);
}
- SciArray<T>& operator=(const SciArray<T> &array) {
+ SciArray &operator=(const SciArray &array) {
if (this == &array)
return *this;
- delete[] _data;
+ free(_data);
_type = array._type;
_size = array._size;
- _actualSize = array._actualSize;
- _data = new T[_actualSize];
+ _elementSize = array._elementSize;
+ _data = malloc(_elementSize * _size);
assert(_data);
- memcpy(_data, array._data, _size * sizeof(T));
+ memcpy(_data, array._data, _elementSize * _size);
return *this;
}
virtual ~SciArray() {
- destroy();
+ free(_data);
+ _size = 0;
+ _type = kArrayTypeInvalid;
}
- virtual void destroy() {
- delete[] _data;
- _data = NULL;
- _type = -1;
- _size = _actualSize = 0;
- }
+ void saveLoadWithSerializer(Common::Serializer &s);
- void setType(byte type) {
- if (_type >= 0)
- error("SciArray::setType(): Type already set");
+ /**
+ * Returns the type of this array.
+ */
+ SciArrayType getType() const {
+ return _type;
+ }
+ /**
+ * Sets the type of this array. The type of the array may only be set once.
+ */
+ void setType(const SciArrayType type) {
+ assert(_type == kArrayTypeInvalid);
+ switch(type) {
+ case kArrayTypeInt16:
+ case kArrayTypeID:
+ _elementSize = sizeof(reg_t);
+ break;
+ case kArrayTypeString:
+ _elementSize = sizeof(char);
+ break;
+ case kArrayTypeByte:
+ _elementSize = sizeof(byte);
+ break;
+ default:
+ error("Invalid array type %d", type);
+ }
_type = type;
}
- void setSize(uint32 size) {
- if (_type < 0)
- error("SciArray::setSize(): No type set");
+ /**
+ * Returns the size of the array, in elements.
+ */
+ uint16 size() const {
+ return _size;
+ }
- // Check if we don't have to do anything
- if (_size == size)
- return;
+ /**
+ * Returns the size of the array, in bytes.
+ */
+ uint16 byteSize() const {
+ return _size * _elementSize;
+ }
- // Check if we don't have to expand the array
- if (size <= _actualSize) {
- _size = size;
- return;
+ /**
+ * Ensures the array is large enough to store at least the given number of
+ * values given in `newSize`. If `force` is true, the array will be resized
+ * to store exactly `newSize` values. New values are initialized to zero.
+ */
+ void resize(uint16 newSize, const bool force = false) {
+ if (force || newSize > _size) {
+ _data = realloc(_data, _elementSize * newSize);
+ if (newSize > _size) {
+ memset((byte *)_data + _elementSize * _size, 0, (newSize - _size) * _elementSize);
+ }
+ _size = newSize;
}
+ }
- // So, we're going to have to create an array of some sort
- T *newArray = new T[size];
- memset(newArray, 0, size * sizeof(T));
+ /**
+ * Shrinks a string array to its optimal size.
+ */
+ void snug() {
+ assert(_type == kArrayTypeString || _type == kArrayTypeByte);
+ resize(strlen((char *)_data) + 1, true);
+ }
- // Check if we never created an array before
- if (!_data) {
- _size = _actualSize = size;
- _data = newArray;
- return;
+ /**
+ * Returns a pointer to the array's raw data storage.
+ */
+ void *getRawData() { return _data; }
+ const void *getRawData() const { return _data; }
+
+ /**
+ * Gets the value at the given index as a reg_t.
+ */
+ reg_t getAsID(const uint16 index) {
+ if (getSciVersion() >= SCI_VERSION_3) {
+ resize(index);
+ } else {
+ assert(index < _size);
}
- // Copy data from the old array to the new
- memcpy(newArray, _data, _size * sizeof(T));
+ switch(_type) {
+ case kArrayTypeInt16:
+ case kArrayTypeID:
+ return ((reg_t *)_data)[index];
+ case kArrayTypeByte:
+ case kArrayTypeString:
+ return make_reg(0, ((byte *)_data)[index]);
+ default:
+ error("Invalid array type %d", _type);
+ }
+ }
+
+ /**
+ * Sets the value at the given index from a reg_t.
+ */
+ void setFromID(const uint16 index, const reg_t value) {
+ if (getSciVersion() >= SCI_VERSION_3) {
+ resize(index);
+ } else {
+ assert(index < _size);
+ }
- // Now set the new array to the old and set the sizes
- delete[] _data;
- _data = newArray;
- _size = _actualSize = size;
+ switch(_type) {
+ case kArrayTypeInt16:
+ case kArrayTypeID:
+ ((reg_t *)_data)[index] = value;
+ break;
+ case kArrayTypeByte:
+ case kArrayTypeString:
+ ((byte *)_data)[index] = value.toSint16();
+ break;
+ default:
+ error("Invalid array type %d", _type);
+ }
}
- T getValue(uint16 index) const {
- if (index >= _size)
- error("SciArray::getValue(): %d is out of bounds (%d)", index, _size);
+ /**
+ * Gets the value at the given index as an int16.
+ */
+ int16 getAsInt16(const uint16 index) {
+ assert(_type == kArrayTypeInt16);
- return _data[index];
+ if (getSciVersion() >= SCI_VERSION_3) {
+ resize(index);
+ } else {
+ assert(index < _size);
+ }
+
+ const reg_t value = ((reg_t *)_data)[index];
+ assert(value.isNumber());
+ return value.toSint16();
}
- void setValue(uint16 index, T value) {
- if (index >= _size)
- error("SciArray::setValue(): %d is out of bounds (%d)", index, _size);
+ /**
+ * Sets the value at the given index from an int16.
+ */
+ void setFromInt16(const uint16 index, const int16 value) {
+ assert(_type == kArrayTypeInt16);
+
+ if (getSciVersion() >= SCI_VERSION_3) {
+ resize(index + 1);
+ } else {
+ assert(index < _size);
+ }
- _data[index] = value;
+ ((reg_t *)_data)[index] = make_reg(0, value);
}
- byte getType() const { return _type; }
- uint32 getSize() const { return _size; }
- T *getRawData() { return _data; }
- const T *getRawData() const { return _data; }
+ /**
+ * Returns a reference to the byte at the given index. Only valid for
+ * string and byte arrays.
+ */
+ byte &byteAt(const uint16 index) {
+ assert(_type == kArrayTypeString || _type == kArrayTypeByte);
-protected:
- int8 _type;
- T *_data;
- uint32 _size; // _size holds the number of entries that the scripts have requested
- uint32 _actualSize; // _actualSize is the actual numbers of entries allocated
-};
+ if (getSciVersion() >= SCI_VERSION_3) {
+ resize(index);
+ } else {
+ assert(index < _size);
+ }
-class SciString : public SciArray<char> {
-public:
- SciString() : SciArray<char>() { setType(3); }
+ return ((byte *)_data)[index];
+ }
- // We overload destroy to ensure the string type is 3 after destroying
- void destroy() { SciArray<char>::destroy(); _type = 3; }
+ /**
+ * Returns a reference to the char at the given index. Only valid for
+ * string and byte arrays.
+ */
+ char &charAt(const uint16 index) {
+ assert(_type == kArrayTypeString || _type == kArrayTypeByte);
- Common::String toString() const;
- void fromString(const Common::String &string);
-};
+ if (getSciVersion() >= SCI_VERSION_3) {
+ resize(index);
+ } else {
+ assert(index < _size);
+ }
-struct ArrayTable : public SegmentObjTable<SciArray<reg_t> > {
- ArrayTable() : SegmentObjTable<SciArray<reg_t> >(SEG_TYPE_ARRAY) {}
+ return ((char *)_data)[index];
+ }
- virtual void freeAtAddress(SegManager *segMan, reg_t sub_addr);
- virtual Common::Array<reg_t> listAllOutgoingReferences(reg_t object) const;
+ /**
+ * Returns a reference to the reg_t at the given index. Only valid for ID
+ * and int16 arrays.
+ */
+ reg_t &IDAt(const uint16 index) {
+ assert(_type == kArrayTypeID || _type == kArrayTypeInt16);
- void saveLoadWithSerializer(Common::Serializer &ser);
- SegmentRef dereference(reg_t pointer);
-};
+ if (getSciVersion() >= SCI_VERSION_3) {
+ resize(index);
+ } else {
+ assert(index < _size);
+ }
-struct StringTable : public SegmentObjTable<SciString> {
- StringTable() : SegmentObjTable<SciString>(SEG_TYPE_STRING) {}
+ return ((reg_t *)_data)[index];
+ }
- virtual void freeAtAddress(SegManager *segMan, reg_t sub_addr) {
- at(sub_addr.getOffset()).destroy();
- freeEntry(sub_addr.getOffset());
+ /**
+ * Reads values from the given reg_t pointer and sets them in the array,
+ * growing the array if needed to store all values.
+ */
+ void setElements(const uint16 index, uint16 count, const reg_t *values) {
+ resize(index + count);
+
+ switch (_type) {
+ case kArrayTypeInt16:
+ case kArrayTypeID: {
+ const reg_t *source = values;
+ reg_t *target = (reg_t *)_data + index;
+ while (count--) {
+ *target++ = *source++;
+ }
+ break;
+ }
+ case kArrayTypeByte:
+ case kArrayTypeString: {
+ const reg_t *source = values;
+ byte *target = (byte *)_data + index;
+ while (count--) {
+ if (!source->isNumber()) {
+ error("Non-number %04x:%04x sent to byte or string array", PRINT_REG(*source));
+ }
+ *target++ = source->getOffset();
+ ++source;
+ }
+ break;
+ }
+ default:
+ error("Attempted write to SciArray with invalid type %d", _type);
+ }
+ }
+
+ /**
+ * Fills the array with the given value. Existing values will be
+ * overwritten. The array will be grown if needed to store all values.
+ */
+ void fill(const uint16 index, uint16 count, const reg_t value) {
+ if (count == 65535 /* -1 */) {
+ count = size() - index;
+ }
+
+ if (!count) {
+ return;
+ }
+
+ resize(index + count);
+
+ switch (_type) {
+ case kArrayTypeInt16:
+ case kArrayTypeID: {
+ reg_t *target = (reg_t *)_data + index;
+ while (count--) {
+ *target = value;
+ }
+ break;
+ }
+ case kArrayTypeByte:
+ case kArrayTypeString: {
+ byte *target = (byte *)_data + index;
+ const byte fillValue = value.getOffset();
+ while (count--) {
+ *target = fillValue;
+ }
+ break;
+ }
+ case kArrayTypeInvalid:
+ error("Attempted write to uninitialized SciArray");
+ }
+ }
+
+ /**
+ * Copies values from the source array. Both arrays will be grown if needed
+ * to prevent out-of-bounds reads/writes.
+ */
+ void copy(SciArray &source, const uint16 sourceIndex, const uint16 targetIndex, uint16 count) {
+ if (count == 65535 /* -1 */) {
+ count = source.size() - sourceIndex;
+ }
+
+ if (!count) {
+ return;
+ }
+
+ resize(targetIndex + count);
+ source.resize(sourceIndex + count);
+
+ assert(source._elementSize == _elementSize);
+
+ const byte *sourceData = (byte *)source._data + sourceIndex * source._elementSize;
+ byte *targetData = (byte *)_data + targetIndex * _elementSize;
+ memmove(targetData, sourceData, count * _elementSize);
+ }
+
+ void byteCopy(const SciArray &source, const uint16 sourceOffset, const uint16 targetOffset, const uint16 count) {
+ error("SciArray::byteCopy not implemented");
}
+ /**
+ * Removes whitespace from string data held in this array.
+ */
+ void trim(const int8 flags, const char showChar) {
+ enum {
+ kWhitespaceBoundary = 32,
+ kAsciiBoundary = 128
+ };
+
+ byte *data = (byte *)_data;
+ byte *source;
+ byte *target;
+
+ if (flags & kArrayTrimLeft) {
+ target = data;
+ source = data;
+ while (*source != '\0' && *source != showChar && *source <= kWhitespaceBoundary) {
+ ++source;
+ }
+ strcpy((char *)target, (char *)source);
+ }
+
+ if (flags & kArrayTrimRight) {
+ source = data + strlen((char *)data) - 1;
+ while (source > data && *source != showChar && *source <= kWhitespaceBoundary) {
+ --source;
+ }
+ *source = '\0';
+ }
+
+ if (flags & kArrayTrimCenter) {
+ target = data;
+ while (*target && *target <= kWhitespaceBoundary && *target != showChar) {
+ ++target;
+ }
+
+ if (*target) {
+ while (*target && (*target > kWhitespaceBoundary || *target == showChar)) {
+ ++target;
+ }
+
+ if (*target) {
+ source = target;
+ while (*source) {
+ while (*source && *source <= kWhitespaceBoundary && *source != showChar) {
+ ++source;
+ }
+
+ while (*source && (*source > kWhitespaceBoundary || *source == showChar)) {
+ *target++ = *source++;
+ }
+ }
+
+ --source;
+ while (source > target && (*source <= kWhitespaceBoundary || *source >= kAsciiBoundary) && *source != showChar) {
+ --source;
+ }
+ ++source;
+
+ memmove(target, source, strlen((char *)source) + 1);
+ }
+ }
+ }
+ }
+
+ /**
+ * Copies the string data held by this array into a new Common::String.
+ */
+ Common::String toString() const {
+ assert(_type == kArrayTypeString);
+ return Common::String((char *)_data);
+ }
+
+ /**
+ * Copies the string from the given Common::String into this array.
+ */
+ void fromString(const Common::String &string) {
+ // At least LSL6hires uses a byte-type array to hold string data
+ assert(_type == kArrayTypeString || _type == kArrayTypeByte);
+ resize(string.size() + 1, true);
+ Common::strlcpy((char *)_data, string.c_str(), string.size() + 1);
+ }
+
+protected:
+ void *_data;
+ SciArrayType _type;
+ uint16 _size;
+ uint8 _elementSize;
+};
+
+struct ArrayTable : public SegmentObjTable<SciArray> {
+ ArrayTable() : SegmentObjTable<SciArray>(SEG_TYPE_ARRAY) {}
+
+ virtual Common::Array<reg_t> listAllOutgoingReferences(reg_t object) const;
+
void saveLoadWithSerializer(Common::Serializer &ser);
SegmentRef dereference(reg_t pointer);
};
@@ -649,7 +963,7 @@ public:
/**
* Allocates and initialises a new bitmap.
*/
- inline void create(const int16 width, const int16 height, const uint8 skipColor, const int16 displaceX, const int16 displaceY, const int16 scaledWidth, const int16 scaledHeight, const uint32 paletteSize, const bool remap, const bool gc) {
+ inline void create(const int16 width, const int16 height, const uint8 skipColor, const int16 originX, const int16 originY, const int16 xResolution, const int16 yResolution, const uint32 paletteSize, const bool remap, const bool gc) {
_dataSize = getBitmapSize(width, height) + paletteSize;
_data = (byte *)realloc(_data, _dataSize);
@@ -659,7 +973,7 @@ public:
setWidth(width);
setHeight(height);
- setDisplace(Common::Point(displaceX, displaceY));
+ setOrigin(Common::Point(originX, originY));
setSkipColor(skipColor);
_data[9] = 0;
WRITE_SCI11ENDIAN_UINT16(_data + 10, 0);
@@ -670,8 +984,8 @@ public:
setDataOffset(bitmapHeaderSize);
setUncompressedDataOffset(bitmapHeaderSize);
setControlOffset(0);
- setScaledWidth(scaledWidth);
- setScaledHeight(scaledHeight);
+ setXResolution(xResolution);
+ setYResolution(yResolution);
_buffer = Buffer(getWidth(), getHeight(), getPixels());
}
@@ -703,16 +1017,16 @@ public:
BITMAP_PROPERTY(16, Width, 0);
BITMAP_PROPERTY(16, Height, 2);
- inline Common::Point getDisplace() const {
+ inline Common::Point getOrigin() const {
return Common::Point(
(int16)READ_SCI11ENDIAN_UINT16(_data + 4),
(int16)READ_SCI11ENDIAN_UINT16(_data + 6)
);
}
- inline void setDisplace(const Common::Point &displace) {
- WRITE_SCI11ENDIAN_UINT16(_data + 4, (uint16)displace.x);
- WRITE_SCI11ENDIAN_UINT16(_data + 6, (uint16)displace.y);
+ inline void setOrigin(const Common::Point &origin) {
+ WRITE_SCI11ENDIAN_UINT16(_data + 4, (uint16)origin.x);
+ WRITE_SCI11ENDIAN_UINT16(_data + 6, (uint16)origin.y);
}
inline uint8 getSkipColor() const {
@@ -761,7 +1075,7 @@ public:
// NOTE: This property always seems to be zero
BITMAP_PROPERTY(32, ControlOffset, 32);
- inline uint16 getScaledWidth() const {
+ inline uint16 getXResolution() const {
if (getDataOffset() >= 40) {
return READ_SCI11ENDIAN_UINT16(_data + 36);
}
@@ -770,13 +1084,13 @@ public:
return 320;
}
- inline void setScaledWidth(uint16 scaledWidth) {
+ inline void setXResolution(uint16 xResolution) {
if (getDataOffset() >= 40) {
- WRITE_SCI11ENDIAN_UINT16(_data + 36, scaledWidth);
+ WRITE_SCI11ENDIAN_UINT16(_data + 36, xResolution);
}
}
- inline uint16 getScaledHeight() const {
+ inline uint16 getYResolution() const {
if (getDataOffset() >= 40) {
return READ_SCI11ENDIAN_UINT16(_data + 38);
}
@@ -785,9 +1099,9 @@ public:
return 200;
}
- inline void setScaledHeight(uint16 scaledHeight) {
+ inline void setYResolution(uint16 yResolution) {
if (getDataOffset() >= 40) {
- WRITE_SCI11ENDIAN_UINT16(_data + 38, scaledHeight);
+ WRITE_SCI11ENDIAN_UINT16(_data + 38, yResolution);
}
}
@@ -803,6 +1117,16 @@ public:
}
virtual void saveLoadWithSerializer(Common::Serializer &ser);
+
+ void applyRemap(SciArray &clut) {
+ const int length = getWidth() * getHeight();
+ uint8 *pixel = getPixels();
+ for (int i = 0; i < length; ++i) {
+ const int16 color = clut.getAsInt16(*pixel);
+ assert(color >= 0 && color <= 255);
+ *pixel++ = (uint8)color;
+ }
+ }
};
struct BitmapTable : public SegmentObjTable<SciBitmap> {
diff --git a/engines/sci/engine/selector.cpp b/engines/sci/engine/selector.cpp
index ac621f58ae..3ae902215c 100644
--- a/engines/sci/engine/selector.cpp
+++ b/engines/sci/engine/selector.cpp
@@ -226,15 +226,14 @@ void writeSelector(SegManager *segMan, reg_t object, Selector selectorId, reg_t
ObjVarRef address;
if ((selectorId < 0) || (selectorId > (int)g_sci->getKernel()->getSelectorNamesSize())) {
- error("Attempt to write to invalid selector %d of"
- " object at %04x:%04x.", selectorId, PRINT_REG(object));
- return;
+ const SciCallOrigin origin = g_sci->getEngineState()->getCurrentCallOrigin();
+ error("Attempt to write to invalid selector %d. Address %04x:%04x, %s", selectorId, PRINT_REG(object), origin.toString().c_str());
}
- if (lookupSelector(segMan, object, selectorId, &address, NULL) != kSelectorVariable)
- error("Selector '%s' of object at %04x:%04x could not be"
- " written to", g_sci->getKernel()->getSelectorName(selectorId).c_str(), PRINT_REG(object));
- else {
+ if (lookupSelector(segMan, object, selectorId, &address, NULL) != kSelectorVariable) {
+ const SciCallOrigin origin = g_sci->getEngineState()->getCurrentCallOrigin();
+ error("Selector '%s' of object could not be written to. Address %04x:%04x, %s", g_sci->getKernel()->getSelectorName(selectorId).c_str(), PRINT_REG(object), origin.toString().c_str());
+ } else {
*address.getPointer(segMan) = value;
#ifdef ENABLE_SCI32
updateInfoFlagViewVisible(segMan->getObject(object), selectorId);
@@ -255,12 +254,12 @@ void invokeSelector(EngineState *s, reg_t object, int selectorId,
slc_type = lookupSelector(s->_segMan, object, selectorId, NULL, NULL);
if (slc_type == kSelectorNone) {
- error("Selector '%s' of object at %04x:%04x could not be invoked",
- g_sci->getKernel()->getSelectorName(selectorId).c_str(), PRINT_REG(object));
+ const SciCallOrigin origin = g_sci->getEngineState()->getCurrentCallOrigin();
+ error("invokeSelector: Selector '%s' could not be invoked. Address %04x:%04x, %s", g_sci->getKernel()->getSelectorName(selectorId).c_str(), PRINT_REG(object), origin.toString().c_str());
}
if (slc_type == kSelectorVariable) {
- error("Attempting to invoke variable selector %s of object %04x:%04x",
- g_sci->getKernel()->getSelectorName(selectorId).c_str(), PRINT_REG(object));
+ const SciCallOrigin origin = g_sci->getEngineState()->getCurrentCallOrigin();
+ error("invokeSelector: Attempting to invoke variable selector %s. Address %04x:%04x, %s", g_sci->getKernel()->getSelectorName(selectorId).c_str(), PRINT_REG(object), origin.toString().c_str());
}
for (i = 0; i < argc; i++)
@@ -288,8 +287,8 @@ SelectorType lookupSelector(SegManager *segMan, reg_t obj_location, Selector sel
selectorId &= ~1;
if (!obj) {
- error("lookupSelector(): Attempt to send to non-object or invalid script. Address was %04x:%04x",
- PRINT_REG(obj_location));
+ const SciCallOrigin origin = g_sci->getEngineState()->getCurrentCallOrigin();
+ error("lookupSelector: Attempt to send to non-object or invalid script. Address %04x:%04x, %s", PRINT_REG(obj_location), origin.toString().c_str());
}
index = obj->locateVarSelector(segMan, selectorId);
diff --git a/engines/sci/engine/state.cpp b/engines/sci/engine/state.cpp
index a338beffc9..c23add1211 100644
--- a/engines/sci/engine/state.cpp
+++ b/engines/sci/engine/state.cpp
@@ -159,11 +159,11 @@ void EngineState::initGlobals() {
}
uint16 EngineState::currentRoomNumber() const {
- return variables[VAR_GLOBAL][13].toUint16();
+ return variables[VAR_GLOBAL][kGlobalVarNewRoomNo].toUint16();
}
void EngineState::setRoomNumber(uint16 roomNumber) {
- variables[VAR_GLOBAL][13] = make_reg(0, roomNumber);
+ variables[VAR_GLOBAL][kGlobalVarNewRoomNo] = make_reg(0, roomNumber);
}
void EngineState::shrinkStackToBase() {
@@ -202,12 +202,12 @@ Common::String SciEngine::getSciLanguageString(const Common::String &str, kLangu
const byte *textPtr = (const byte *)str.c_str();
byte curChar = 0;
byte curChar2 = 0;
-
+
while (1) {
curChar = *textPtr;
if (!curChar)
break;
-
+
if ((curChar == '%') || (curChar == '#')) {
curChar2 = *(textPtr + 1);
foundLanguage = charToLanguage(curChar2);
@@ -236,7 +236,7 @@ Common::String SciEngine::getSciLanguageString(const Common::String &str, kLangu
while (1) {
curChar = *textPtr;
-
+
switch (curChar) {
case 0: // Terminator NUL
return fullWidth;
@@ -257,7 +257,7 @@ Common::String SciEngine::getSciLanguageString(const Common::String &str, kLangu
continue;
}
}
-
+
textPtr++;
mappedChar = s_halfWidthSJISMap[curChar];
@@ -380,4 +380,46 @@ void SciEngine::checkVocabularySwitch() {
}
}
+SciCallOrigin EngineState::getCurrentCallOrigin() const {
+ // IMPORTANT: This method must always return values that match *exactly* the
+ // values in the workaround tables in workarounds.cpp, or workarounds will
+ // be broken
+
+ Common::String curObjectName = _segMan->getObjectName(xs->sendp);
+ Common::String curMethodName;
+ const Script *localScript = _segMan->getScriptIfLoaded(xs->local_segment);
+ int curScriptNr = localScript->getScriptNumber();
+
+ if (xs->debugLocalCallOffset != -1) {
+ // if lastcall was actually a local call search back for a real call
+ Common::List<ExecStack>::const_iterator callIterator = _executionStack.end();
+ while (callIterator != _executionStack.begin()) {
+ callIterator--;
+ const ExecStack &loopCall = *callIterator;
+ if ((loopCall.debugSelector != -1) || (loopCall.debugExportId != -1)) {
+ xs->debugSelector = loopCall.debugSelector;
+ xs->debugExportId = loopCall.debugExportId;
+ break;
+ }
+ }
+ }
+
+ if (xs->type == EXEC_STACK_TYPE_CALL) {
+ if (xs->debugSelector != -1) {
+ curMethodName = g_sci->getKernel()->getSelectorName(xs->debugSelector);
+ } else if (xs->debugExportId != -1) {
+ curObjectName = "";
+ curMethodName = Common::String::format("export %d", xs->debugExportId);
+ }
+ }
+
+ SciCallOrigin reply;
+ reply.objectName = curObjectName;
+ reply.methodName = curMethodName;
+ reply.scriptNr = curScriptNr;
+ reply.localCallOffset = xs->debugLocalCallOffset;
+ reply.roomNr = currentRoomNumber();
+ return reply;
+}
+
} // End of namespace Sci
diff --git a/engines/sci/engine/state.h b/engines/sci/engine/state.h
index baa912b60e..5297a176d3 100644
--- a/engines/sci/engine/state.h
+++ b/engines/sci/engine/state.h
@@ -57,10 +57,6 @@ enum AbortGameState {
kAbortQuitGame = 3
};
-// slot 0 is the ScummVM auto-save slot, which is not used by us, but is still reserved
-#define SAVEGAMESLOT_FIRST 1
-#define SAVEGAMESLOT_LAST 99
-
// We assume that scripts give us savegameId 0->99 for creating a new save slot
// and savegameId 100->199 for existing save slots. Refer to kfile.cpp
enum {
@@ -99,6 +95,21 @@ struct VideoState {
}
};
+/**
+ * Trace information about a VM function call.
+ */
+struct SciCallOrigin {
+ int scriptNr; //< The source script of the function
+ Common::String objectName; //< The name of the object being called
+ Common::String methodName; //< The name of the method being called
+ int localCallOffset; //< The byte offset of a local script subroutine called by the origin method. -1 if not in a local subroutine.
+ int roomNr; //< The room that was loaded at the time of the call
+
+ Common::String toString() const {
+ return Common::String::format("method %s::%s (room %d, script %d, localCall %x)", objectName.c_str(), methodName.c_str(), roomNr, scriptNr, localCallOffset);
+ }
+};
+
struct EngineState : public Common::Serializable {
public:
EngineState(SegManager *segMan);
@@ -209,6 +220,11 @@ public:
* Resets the engine state.
*/
void reset(bool isRestoring);
+
+ /**
+ * Finds and returns the origin of the current call.
+ */
+ SciCallOrigin getCurrentCallOrigin() const;
};
} // End of namespace Sci
diff --git a/engines/sci/engine/vm.cpp b/engines/sci/engine/vm.cpp
index 548fd477bf..82de957387 100644
--- a/engines/sci/engine/vm.cpp
+++ b/engines/sci/engine/vm.cpp
@@ -20,6 +20,7 @@
*
*/
+#include "common/config-manager.h"
#include "common/debug.h"
#include "common/debug-channels.h"
@@ -124,21 +125,17 @@ static reg_t read_var(EngineState *s, int type, int index) {
case VAR_TEMP: {
// Uninitialized read on a temp
// We need to find correct replacements for each situation manually
- SciTrackOriginReply originReply;
+ SciCallOrigin originReply;
SciWorkaroundSolution solution = trackOriginAndFindWorkaround(index, uninitializedReadWorkarounds, &originReply);
if (solution.type == WORKAROUND_NONE) {
#ifdef RELEASE_BUILD
// If we are running an official ScummVM release -> fake 0 in unknown cases
- warning("Uninitialized read for temp %d from method %s::%s (room %d, script %d, localCall %x)",
- index, originReply.objectName.c_str(), originReply.methodName.c_str(), s->currentRoomNumber(),
- originReply.scriptNr, originReply.localCallOffset);
+ warning("Uninitialized read for temp %d from %s", index, originReply.toString().c_str());
s->variables[type][index] = NULL_REG;
break;
#else
- error("Uninitialized read for temp %d from method %s::%s (room %d, script %d, localCall %x)",
- index, originReply.objectName.c_str(), originReply.methodName.c_str(), s->currentRoomNumber(),
- originReply.scriptNr, originReply.localCallOffset);
+ error("Uninitialized read for temp %d from %s", index, originReply.toString().c_str());
#endif
}
assert(solution.type == WORKAROUND_FAKE);
@@ -179,7 +176,7 @@ static void write_var(EngineState *s, int type, int index, reg_t value) {
// stopGroop object, which points to ego, to the new ego object. If this is not
// done, ego's movement will not be updated properly, so the result is
// unpredictable (for example in LSL5, Patti spins around instead of walking).
- if (index == 0 && type == VAR_GLOBAL && getSciVersion() > SCI_VERSION_0_EARLY) { // global 0 is ego
+ if (index == kGlobalVarEgo && type == VAR_GLOBAL && getSciVersion() > SCI_VERSION_0_EARLY) {
reg_t stopGroopPos = s->_segMan->findObjectByName("stopGroop");
if (!stopGroopPos.isNull()) { // does the game have a stopGroop object?
// Find the "client" member variable of the stopGroop object, and update it
@@ -200,9 +197,9 @@ static void write_var(EngineState *s, int type, int index, reg_t value) {
s->variables[type][index] = value;
- if (type == VAR_GLOBAL && index == 90) {
+ if (type == VAR_GLOBAL && index == kGlobalVarMessageType) {
// The game is trying to change its speech/subtitle settings
- if (!g_sci->getEngineState()->_syncedAudioOptions || s->variables[VAR_GLOBAL][4] == TRUE_REG) {
+ if (!g_sci->getEngineState()->_syncedAudioOptions || s->variables[VAR_GLOBAL][kGlobalVarQuit] == TRUE_REG) {
// ScummVM audio options haven't been applied yet, so apply them.
// We also force the ScummVM audio options when loading a game from
// the launcher.
@@ -213,6 +210,12 @@ static void write_var(EngineState *s, int type, int index, reg_t value) {
g_sci->updateScummVMAudioOptions();
}
}
+
+#ifdef ENABLE_SCI32
+ if (type == VAR_GLOBAL && index == kGlobalVarTextSpeed && getSciVersion() >= SCI_VERSION_2) {
+ ConfMan.setInt("talkspeed", (8 - value.toSint16()) * 255 / 8);
+ }
+#endif
}
}
@@ -361,16 +364,13 @@ static void callKernelFunc(EngineState *s, int kernelCallNr, int argc) {
if (kernelCall.signature
&& !kernel->signatureMatch(kernelCall.signature, argc, argv)) {
// signature mismatch, check if a workaround is available
- SciTrackOriginReply originReply;
+ SciCallOrigin originReply;
SciWorkaroundSolution solution = trackOriginAndFindWorkaround(0, kernelCall.workarounds, &originReply);
switch (solution.type) {
case WORKAROUND_NONE: {
Common::String signatureDetailsStr;
kernel->signatureDebug(signatureDetailsStr, kernelCall.signature, argc, argv);
- error("\n%s[VM] k%s[%x]: signature mismatch in method %s::%s (room %d, script %d, localCall 0x%x)",
- signatureDetailsStr.c_str(),
- kernelCall.name, kernelCallNr, originReply.objectName.c_str(), originReply.methodName.c_str(),
- s->currentRoomNumber(), originReply.scriptNr, originReply.localCallOffset);
+ error("\n%s[VM] k%s[%x]: signature mismatch in %s", signatureDetailsStr.c_str(), kernelCall.name, kernelCallNr, originReply.toString().c_str());
break;
}
case WORKAROUND_IGNORE: // don't do kernel call, leave acc alone
@@ -429,7 +429,7 @@ static void callKernelFunc(EngineState *s, int kernelCallNr, int argc) {
const KernelSubFunction &kernelSubCall = kernelCall.subFunctions[subId];
if (kernelSubCall.signature && !kernel->signatureMatch(kernelSubCall.signature, argc, argv)) {
// Signature mismatch
- SciTrackOriginReply originReply;
+ SciCallOrigin originReply;
SciWorkaroundSolution solution = trackOriginAndFindWorkaround(0, kernelSubCall.workarounds, &originReply);
switch (solution.type) {
case WORKAROUND_NONE: {
@@ -438,15 +438,13 @@ static void callKernelFunc(EngineState *s, int kernelCallNr, int argc) {
int callNameLen = strlen(kernelCall.name);
if (strncmp(kernelCall.name, kernelSubCall.name, callNameLen) == 0) {
const char *subCallName = kernelSubCall.name + callNameLen;
- error("\n%s[VM] k%s(%s): signature mismatch in method %s::%s (room %d, script %d, localCall %x)",
- signatureDetailsStr.c_str(),
- kernelCall.name, subCallName, originReply.objectName.c_str(), originReply.methodName.c_str(),
- s->currentRoomNumber(), originReply.scriptNr, originReply.localCallOffset);
+ error("\n%s[VM] k%s(%s): signature mismatch in %s",
+ signatureDetailsStr.c_str(), kernelCall.name, subCallName,
+ originReply.toString().c_str());
}
- error("\n%s[VM] k%s: signature mismatch in method %s::%s (room %d, script %d, localCall %x)",
- signatureDetailsStr.c_str(),
- kernelSubCall.name, originReply.objectName.c_str(), originReply.methodName.c_str(),
- s->currentRoomNumber(), originReply.scriptNr, originReply.localCallOffset);
+ error("\n%s[VM] k%s: signature mismatch in %s",
+ signatureDetailsStr.c_str(), kernelSubCall.name,
+ originReply.toString().c_str());
break;
}
case WORKAROUND_IGNORE: // don't do kernel call, leave acc alone
diff --git a/engines/sci/engine/vm.h b/engines/sci/engine/vm.h
index c41060dc32..dd66d9d52c 100644
--- a/engines/sci/engine/vm.h
+++ b/engines/sci/engine/vm.h
@@ -136,6 +136,22 @@ enum {
VAR_PARAM = 3
};
+enum GlobalVar {
+ kGlobalVarEgo = 0,
+ kGlobalVarCurrentRoom = 2,
+ kGlobalVarSpeed = 3, // SCI16
+ kGlobalVarQuit = 4,
+ kGlobalVarPlanes = 10, // SCI32
+ kGlobalVarCurrentRoomNo = 11,
+ kGlobalVarPreviousRoomNo = 12,
+ kGlobalVarNewRoomNo = 13,
+ kGlobalVarScore = 15,
+ kGlobalVarFastCast = 84, // SCI16
+ kGlobalVarMessageType = 90,
+ kGlobalVarTextSpeed = 94, // SCI32; 0 is fastest, 8 is slowest
+ kGlobalVarShivers1Score = 349
+};
+
/** Number of kernel calls in between gcs; should be < 50000 */
enum {
GC_INTERVAL = 0x8000
diff --git a/engines/sci/engine/vm_types.cpp b/engines/sci/engine/vm_types.cpp
index d74e2b194c..b2e250ab8b 100644
--- a/engines/sci/engine/vm_types.cpp
+++ b/engines/sci/engine/vm_types.cpp
@@ -66,13 +66,10 @@ void reg_t::setOffset(uint32 offset) {
}
reg_t reg_t::lookForWorkaround(const reg_t right, const char *operation) const {
- SciTrackOriginReply originReply;
+ SciCallOrigin originReply;
SciWorkaroundSolution solution = trackOriginAndFindWorkaround(0, arithmeticWorkarounds, &originReply);
if (solution.type == WORKAROUND_NONE)
- error("Invalid arithmetic operation (%s - params: %04x:%04x and %04x:%04x) from method %s::%s (room %d, script %d, localCall %x)",
- operation, PRINT_REG(*this), PRINT_REG(right), originReply.objectName.c_str(),
- originReply.methodName.c_str(), g_sci->getEngineState()->currentRoomNumber(), originReply.scriptNr,
- originReply.localCallOffset);
+ error("Invalid arithmetic operation (%s - params: %04x:%04x and %04x:%04x) from %s", operation, PRINT_REG(*this), PRINT_REG(right), originReply.toString().c_str());
assert(solution.type == WORKAROUND_FAKE);
return make_reg(0, solution.value);
}
diff --git a/engines/sci/engine/workarounds.cpp b/engines/sci/engine/workarounds.cpp
index 7aaea0902a..4c0fee49c5 100644
--- a/engines/sci/engine/workarounds.cpp
+++ b/engines/sci/engine/workarounds.cpp
@@ -318,15 +318,15 @@ const SciWorkaroundEntry uninitializedReadWorkarounds[] = {
{ GID_LSL6, 820, 82, 0, "", "export 0", NULL, -1, { WORKAROUND_FAKE, 0 } }, // when touching the electric fence - bug #5103
{ GID_LSL6, -1, 85, 0, "washcloth", "doVerb", NULL, 0, { WORKAROUND_FAKE, 0 } }, // washcloth in inventory
{ GID_LSL6, -1, 928, -1, "Narrator", "startText", NULL, 0, { WORKAROUND_FAKE, 0 } }, // used by various objects that are even translated in foreign versions, that's why we use the base-class
- { GID_LSL6HIRES, 0, 85, 0, "LL6Inv", "init", NULL, 0, { WORKAROUND_FAKE, 0 } }, // on startup
+ { GID_LSL6HIRES, -1, 85, 0, "LL6Inv", "init", NULL, 0, { WORKAROUND_FAKE, 0 } }, // when creating a new game
{ GID_LSL6HIRES, -1, 64950, 1, "Feature", "handleEvent", NULL, 0, { WORKAROUND_FAKE, 0 } }, // at least when entering swimming pool area
{ GID_LSL6HIRES, -1, 64964, 0, "DPath", "init", NULL, 1, { WORKAROUND_FAKE, 0 } }, // during the game
{ GID_MOTHERGOOSE256, -1, 0, 0, "MG", "doit", NULL, 5, { WORKAROUND_FAKE, 0 } }, // SCI1.1: When moving the cursor all the way to the left during the game - bug #5224
{ GID_MOTHERGOOSE256, -1, 992, 0, "AIPath", "init", NULL, 0, { WORKAROUND_FAKE, 0 } }, // Happens in the demo and full version. In the demo, it happens when walking two screens from mother goose's house to the north. In the full version, it happens in rooms 7 and 23 - bug #5269
{ GID_MOTHERGOOSE256, 90, 90, 0, "introScript", "changeState", NULL, 65, { WORKAROUND_FAKE, 0 } }, // SCI1(CD): At the very end, after the game is completed and restarted - bug #5626
{ GID_MOTHERGOOSE256, 94, 94, 0, "sunrise", "changeState", NULL, 367, { WORKAROUND_FAKE, 0 } }, // At the very end, after the game is completed - bug #5294
- { GID_MOTHERGOOSEHIRES,-1,64950, 1, "Feature", "handleEvent", NULL, 0, { WORKAROUND_FAKE, 0 } }, // right when clicking on a child at the start and probably also later
- { GID_MOTHERGOOSEHIRES,-1,64950, 1, "View", "handleEvent", NULL, 0, { WORKAROUND_FAKE, 0 } }, // see above
+ { GID_MOTHERGOOSEHIRES,-1,64950, -1, "Feature", "handleEvent", NULL, 0, { WORKAROUND_FAKE, 0 } }, // right when clicking on a child at the start and probably also later
+ { GID_MOTHERGOOSEHIRES,-1,64950, -1, "View", "handleEvent", NULL, 0, { WORKAROUND_FAKE, 0 } }, // see above
{ GID_PEPPER, -1, 894, 0, "Package", "doVerb", NULL, 3, { WORKAROUND_FAKE, 0 } }, // using the hand on the book in the inventory - bug #5154
{ GID_PEPPER, 150, 928, 0, "Narrator", "startText", NULL, 0, { WORKAROUND_FAKE, 0 } }, // happens during the non-interactive demo of Pepper
{ GID_PQ4, -1, 25, 0, "iconToggle", "select", NULL, 1, { WORKAROUND_FAKE, 0 } }, // when toggling the icon bar to auto-hide or not
@@ -347,6 +347,7 @@ const SciWorkaroundEntry uninitializedReadWorkarounds[] = {
{ GID_QFG3, 330, 330, -1, "Teller", "doChild", NULL, -1, { WORKAROUND_FAKE, 0 } }, // when talking to King Rajah about "Rajah" (bug #5033, temp 1) or "Tarna" (temp 0), or when clicking on yourself and saying "Greet" (bug #5148, temp 1)
{ GID_QFG3, 700, 700, -1, "monsterIsDead", "changeState", NULL, 0, { WORKAROUND_FAKE, 0 } }, // in the jungle, after winning any fight, bug #5169
{ GID_QFG3, 470, 470, -1, "rm470", "notify", NULL, 0, { WORKAROUND_FAKE, 0 } }, // closing the character screen in the Simbani village in the room with the bridge, bug #5165
+ { GID_QFG3, 470, 470, -1, "<invalid name>", "notify", NULL, 0, { WORKAROUND_FAKE, 0 } }, // same as previous, with rm470::name used for temp storage by fan patches added by GOG
{ GID_QFG3, 490, 490, -1, "computersMove", "changeState", NULL, 0, { WORKAROUND_FAKE, 0 } }, // when finishing awari game, bug #5167
{ GID_QFG3, 490, 490, -1, "computersMove", "changeState", sig_uninitread_qfg3_2, 4, { WORKAROUND_FAKE, 0 } }, // also when finishing awari game
{ GID_QFG3, 851, 32, -1, "ProjObj", "doit", NULL, 1, { WORKAROUND_FAKE, 0 } }, // near the end, when throwing the spear of death, bug #5282
@@ -379,6 +380,7 @@ const SciWorkaroundEntry uninitializedReadWorkarounds[] = {
{ GID_SQ6, -1, 0, 0, "SQ6", "init", NULL, 2, { WORKAROUND_FAKE, 0 } }, // Demo and full version: called when the game starts (demo: room 0, full: room 100)
{ GID_SQ6, -1, 64950, -1, "Feature", "handleEvent", NULL, 0, { WORKAROUND_FAKE, 0 } }, // called when pressing "Start game" in the main menu, when entering the Orion's Belt bar (room 300), and perhaps other places
{ GID_SQ6, -1, 64964, 0, "DPath", "init", NULL, 1, { WORKAROUND_FAKE, 0 } }, // during the game
+ { GID_SQ6, 210, 210, 0, "buttonSecret", "doVerb", NULL, 0, { WORKAROUND_FAKE, 0 } }, // after winning the first round of stooge fighter 3
{ GID_TORIN, -1, 64017, 0, "oFlags", "clear", NULL, 0, { WORKAROUND_FAKE, 0 } }, // entering Torin's home in the French version
{ GID_TORIN, 10000, 64029, 0, "oMessager", "nextMsg", NULL, 3, { WORKAROUND_FAKE, 0 } }, // start of chapter one
{ GID_TORIN, 20100, 64964, 0, "DPath", "init", NULL, 1, { WORKAROUND_FAKE, 0 } }, // going down the cliff at the first screen of chapter 2 (washing area)
@@ -396,6 +398,12 @@ const SciWorkaroundEntry kAbs_workarounds[] = {
};
// gameID, room,script,lvl, object-name, method-name, local-call-signature, index, workaround
+const SciWorkaroundEntry kArraySetElements_workarounds[] = {
+ { GID_PHANTASMAGORIA,902, 64918, 0, "Str", "callKernel", NULL, 0, { WORKAROUND_FAKE, 0 } }, // tries to set an element of a string array to the ego object when starting a new game and selecting a chapter above 1
+ SCI_WORKAROUNDENTRY_TERMINATOR
+};
+
+// gameID, room,script,lvl, object-name, method-name, local-call-signature, index, workaround
const SciWorkaroundEntry kCelHigh_workarounds[] = {
{ GID_KQ5, -1, 255, 0, "deathIcon", "setSize", NULL, 0, { WORKAROUND_STILLCALL, 0 } }, // english floppy: when getting beaten up in the inn and probably more, called with 2nd parameter as object - bug #5049
{ GID_PQ2, -1, 255, 0, "DIcon", "setSize", NULL, 0, { WORKAROUND_STILLCALL, 0 } }, // when showing picture within windows, called with 2nd/3rd parameters as objects
@@ -544,6 +552,12 @@ const SciWorkaroundEntry kGetAngle_workarounds[] = {
};
// gameID, room,script,lvl, object-name, method-name, local-call-signature, index, workaround
+const SciWorkaroundEntry kFileIOOpen_workarounds[] = {
+ { GID_TORIN, 61000, 61000, 0, "roSierraLogo", "init", NULL, 0, { WORKAROUND_STILLCALL, 0 } }, // Missing second argument when the game checks for autosave.cat after the Sierra logo
+ SCI_WORKAROUNDENTRY_TERMINATOR
+};
+
+// gameID, room,script,lvl, object-name, method-name, local-call-signature, index, workaround
const SciWorkaroundEntry kFindKey_workarounds[] = {
{ GID_ECOQUEST2, 100, 999, 0, "myList", "contains", NULL, 0, { WORKAROUND_FAKE, 0 } }, // When Noah Greene gives Adam the Ecorder, and just before the game gives a demonstration, a null reference to a list is passed - bug #4987
{ GID_HOYLE4, 300, 999, 0, "Piles", "contains", NULL, 0, { WORKAROUND_FAKE, 0 } }, // When passing the three cards in Hearts, a null reference to a list is passed - bug #5664
@@ -670,6 +684,20 @@ const SciWorkaroundEntry kNewWindow_workarounds[] = {
};
// gameID, room,script,lvl, object-name, method-name, local-call-signature, index, workaround
+const SciWorkaroundEntry kNumCels_workarounds[] = {
+ { GID_GK1, 460, 64998, -1, "GKEgo", "lastCel", NULL, -1, { WORKAROUND_FAKE, 5 } }, // when Gabriel clicks "ask" on the bartender from a distance
+ { GID_GK1, 808, 64998, -1, "sDJEnters", "changeState", NULL, -1, { WORKAROUND_FAKE, 6 } }, //
+ { GID_GK2, 470, 64998, -1, "pLookieLoos", "lastCel", NULL, -1, { WORKAROUND_FAKE, 50 } }, // random background movement in the crime scene in Munich city centre
+ { GID_GK2, 470, 64998, -1, "pNewsCrew", "lastCel", NULL, -1, { WORKAROUND_FAKE, 18 } }, // random background movement in the crime scene in Munich city centre
+ { GID_GK2, 470, 64998, -1, "pLeberGroup", "lastCel", NULL, -1, { WORKAROUND_FAKE, 22 } }, // random background movement in the crime scene in Munich city centre
+ { GID_SQ6, 270, 64998, -1, "offWorld", "lastCel", NULL, -1, { WORKAROUND_FAKE, 3 } }, // when exiting the kidnapping room
+ { GID_SQ6, 320, 64998, -1, "wandererB", "lastCel", NULL, -1, { WORKAROUND_FAKE, 8 } }, // random background movement on Polysorbate LX street 3
+ { GID_SQ6, 340, 64998, -1, "wandererB", "lastCel", NULL, -1, { WORKAROUND_FAKE, 8 } }, // random background movement on Polysorbate LX street 1
+ { GID_SQ6, 530, 64998, -1, "monitors", "lastCel", NULL, -1, { WORKAROUND_FAKE, 5 } }, // random background movement during cutscene
+ SCI_WORKAROUNDENTRY_TERMINATOR
+};
+
+// gameID, room,script,lvl, object-name, method-name, local-call-signature, index, workaround
const SciWorkaroundEntry kPalVarySetPercent_workarounds[] = {
{ GID_GK1, 370, 370, 0, "graceComeOut", "changeState", NULL, 0, { WORKAROUND_STILLCALL, 0 } }, // there's an extra parameter in GK1, when changing chapters. This extra parameter seems to be a bug or just unimplemented functionality, as there's no visible change from the original in the chapter change room
SCI_WORKAROUNDENTRY_TERMINATOR
@@ -699,7 +727,7 @@ const SciWorkaroundEntry kPaletteUnsetFlag_workarounds[] = {
// gameID, room,script,lvl, object-name, method-name, local-call-signature, index, workaround
const SciWorkaroundEntry kSetCursor_workarounds[] = {
{ GID_KQ5, -1, 768, 0, "KQCursor", "init", NULL, 0, { WORKAROUND_STILLCALL, 0 } }, // CD: gets called with 4 additional "900d" parameters
- { GID_MOTHERGOOSEHIRES,0, 0, -1, "MG", "setCursor", NULL, 0, { WORKAROUND_STILLCALL, 0 } }, // At the start of the game, an object is passed as the cel number
+ { GID_MOTHERGOOSEHIRES,-1, 0, -1, "MG", "setCursor", NULL, 0, { WORKAROUND_STILLCALL, 0 } }, // At the start of the game, an object is passed as the cel number
SCI_WORKAROUNDENTRY_TERMINATOR
};
@@ -767,17 +795,12 @@ const SciWorkaroundEntry kUnLoad_workarounds[] = {
};
// gameID, room,script,lvl, object-name, method-name, local-call-signature, index, workaround
-const SciWorkaroundEntry kStringPutAt_workarounds[] = {
- { GID_PHANTASMAGORIA,902, 64918, 0, "Str", "callKernel", NULL, 0, { WORKAROUND_IGNORE, 0 } }, // When starting a new game from after chapter 1, the game tries to save ego's object in a string
-};
-
-// gameID, room,script,lvl, object-name, method-name, local-call-signature, index, workaround
const SciWorkaroundEntry kScrollWindowAdd_workarounds[] = {
{ GID_PHANTASMAGORIA, 45, 64907, 0, "ScrollableWindow", "addString", NULL, 0, { WORKAROUND_STILLCALL, 0 } }, // ScrollWindow interface passes the last two parameters twice
+ SCI_WORKAROUNDENTRY_TERMINATOR
};
-
-SciWorkaroundSolution trackOriginAndFindWorkaround(int index, const SciWorkaroundEntry *workaroundList, SciTrackOriginReply *trackOrigin) {
+SciWorkaroundSolution trackOriginAndFindWorkaround(int index, const SciWorkaroundEntry *workaroundList, SciCallOrigin *trackOrigin) {
// HACK for SCI3: Temporarily ignore this
if (getSciVersion() == SCI_VERSION_3) {
warning("SCI3 HACK: trackOriginAndFindWorkaround() called, ignoring");
@@ -789,37 +812,14 @@ SciWorkaroundSolution trackOriginAndFindWorkaround(int index, const SciWorkaroun
const EngineState *state = g_sci->getEngineState();
ExecStack *lastCall = state->xs;
- const Script *localScript = state->_segMan->getScriptIfLoaded(lastCall->local_segment);
- int curScriptNr = localScript->getScriptNumber();
- int curLocalCallOffset = lastCall->debugLocalCallOffset;
-
- if (curLocalCallOffset != -1) {
- // if lastcall was actually a local call search back for a real call
- Common::List<ExecStack>::const_iterator callIterator = state->_executionStack.end();
- while (callIterator != state->_executionStack.begin()) {
- callIterator--;
- const ExecStack &loopCall = *callIterator;
- if ((loopCall.debugSelector != -1) || (loopCall.debugExportId != -1)) {
- lastCall->debugSelector = loopCall.debugSelector;
- lastCall->debugExportId = loopCall.debugExportId;
- break;
- }
- }
- }
-
- Common::String curObjectName = state->_segMan->getObjectName(lastCall->sendp);
- Common::String curMethodName;
const SciGameId gameId = g_sci->getGameId();
- const int curRoomNumber = state->currentRoomNumber();
-
- if (lastCall->type == EXEC_STACK_TYPE_CALL) {
- if (lastCall->debugSelector != -1) {
- curMethodName = g_sci->getKernel()->getSelectorName(lastCall->debugSelector);
- } else if (lastCall->debugExportId != -1) {
- curObjectName = "";
- curMethodName = Common::String::format("export %d", lastCall->debugExportId);
- }
- }
+
+ *trackOrigin = state->getCurrentCallOrigin();
+ const Common::String &curObjectName = trackOrigin->objectName;
+ const Common::String &curMethodName = trackOrigin->methodName;
+ const int &curRoomNumber = trackOrigin->roomNr;
+ const int &curScriptNr = trackOrigin->scriptNr;
+ const int &curLocalCallOffset = trackOrigin->localCallOffset;
if (workaroundList) {
// Search if there is a workaround for this one
@@ -895,12 +895,6 @@ SciWorkaroundSolution trackOriginAndFindWorkaround(int index, const SciWorkaroun
} while (!searchObject.isNull()); // no parent left?
}
- // give caller origin data
- trackOrigin->objectName = curObjectName;
- trackOrigin->methodName = curMethodName;
- trackOrigin->scriptNr = curScriptNr;
- trackOrigin->localCallOffset = lastCall->debugLocalCallOffset;
-
SciWorkaroundSolution noneFound;
noneFound.type = WORKAROUND_NONE;
noneFound.value = 0;
diff --git a/engines/sci/engine/workarounds.h b/engines/sci/engine/workarounds.h
index 2cccd05475..d1e985dcf1 100644
--- a/engines/sci/engine/workarounds.h
+++ b/engines/sci/engine/workarounds.h
@@ -24,6 +24,7 @@
#define SCI_ENGINE_WORKAROUNDS_H
#include "sci/engine/vm_types.h"
+#include "sci/engine/state.h"
#include "sci/sci.h"
namespace Sci {
@@ -35,13 +36,6 @@ enum SciWorkaroundType {
WORKAROUND_FAKE // fake kernel call / replace temp value / fake opcode
};
-struct SciTrackOriginReply {
- int scriptNr;
- Common::String objectName;
- Common::String methodName;
- int localCallOffset;
-};
-
struct SciWorkaroundSolution {
SciWorkaroundType type;
uint16 value;
@@ -76,6 +70,7 @@ extern const SciWorkaroundEntry kDirLoop_workarounds[];
extern const SciWorkaroundEntry kDisposeScript_workarounds[];
extern const SciWorkaroundEntry kDoSoundPlay_workarounds[];
extern const SciWorkaroundEntry kDoSoundFade_workarounds[];
+extern const SciWorkaroundEntry kFileIOOpen_workarounds[];
extern const SciWorkaroundEntry kFindKey_workarounds[];
extern const SciWorkaroundEntry kDeleteKey_workarounds[];
extern const SciWorkaroundEntry kGetAngle_workarounds[];
@@ -90,20 +85,22 @@ extern const SciWorkaroundEntry kIsObject_workarounds[];
extern const SciWorkaroundEntry kMemory_workarounds[];
extern const SciWorkaroundEntry kMoveCursor_workarounds[];
extern const SciWorkaroundEntry kNewWindow_workarounds[];
+extern const SciWorkaroundEntry kNumCels_workarounds[];
extern const SciWorkaroundEntry kPalVarySetPercent_workarounds[];
extern const SciWorkaroundEntry kRandom_workarounds[];
extern const SciWorkaroundEntry kReadNumber_workarounds[];
extern const SciWorkaroundEntry kPaletteUnsetFlag_workarounds[];
extern const SciWorkaroundEntry kSetCursor_workarounds[];
+extern const SciWorkaroundEntry kArraySetElements_workarounds[];
extern const SciWorkaroundEntry kSetPort_workarounds[];
extern const SciWorkaroundEntry kStrAt_workarounds[];
extern const SciWorkaroundEntry kStrCpy_workarounds[];
extern const SciWorkaroundEntry kStrLen_workarounds[];
extern const SciWorkaroundEntry kUnLoad_workarounds[];
-extern const SciWorkaroundEntry kStringPutAt_workarounds[];
+extern const SciWorkaroundEntry kStringNew_workarounds[];
extern const SciWorkaroundEntry kScrollWindowAdd_workarounds[];
-extern SciWorkaroundSolution trackOriginAndFindWorkaround(int index, const SciWorkaroundEntry *workaroundList, SciTrackOriginReply *trackOrigin);
+extern SciWorkaroundSolution trackOriginAndFindWorkaround(int index, const SciWorkaroundEntry *workaroundList, SciCallOrigin *trackOrigin);
} // End of namespace Sci