GLK: COMPREHEND: Merged string lookups into Game class

This commit is contained in:
Paul Gilbert 2020-05-31 09:40:33 -07:00
parent 0a2478984f
commit 6447f64709
7 changed files with 71 additions and 149 deletions

View File

@ -23,7 +23,6 @@
#include "glk/comprehend/debugger_dumper.h"
#include "glk/comprehend/dictionary.h"
#include "glk/comprehend/game.h"
#include "glk/comprehend/strings.h"
#include "glk/comprehend/util.h"
namespace Glk {
@ -120,7 +119,7 @@ Common::String DebuggerDumper::dumpInstruction(ComprehendGame *game,
opcode_map = game->_opcodeMap;
opcode = opcode_map[instr->opcode];
line += " [%.2x] ", instr->opcode;
line += Common::String::format(" [%.2x] ", instr->opcode);
if (_opcodes.contains(opcode))
line += _opcodes[opcode];
else
@ -129,7 +128,7 @@ Common::String DebuggerDumper::dumpInstruction(ComprehendGame *game,
if (instr->nr_operands) {
line += "(";
for (i = 0; i < instr->nr_operands; i++)
line += "%.2x%s", instr->operand[i],
line += Common::String::format("%.2x%s", instr->operand[i]),
i == instr->nr_operands - 1 ? ")" : ", ";
}
@ -147,11 +146,11 @@ Common::String DebuggerDumper::dumpInstruction(ComprehendGame *game,
str_table = instr->operand[2];
}
line += " %s", instr_lookup_string(game, str_index, str_table);
line += Common::String::format(" %s", game->instrStringLookup(str_index, str_table).c_str());
break;
case OPCODE_SET_STRING_REPLACEMENT:
line += " %s", game->_replaceWords[instr->operand[0] - 1];
line += Common::String::format(" %s", game->_replaceWords[instr->operand[0] - 1]);
break;
}
@ -298,7 +297,7 @@ void DebuggerDumper::dumpRooms() {
print(" [%.2x] flags=%.2x, graphic=%.2x\n",
i, room->flags, room->graphic);
print(" %s\n", string_lookup(_game, room->string_desc));
print(" %s\n", _game->stringLookup(room->string_desc).c_str());
print(" n: %.2x s: %.2x e: %.2x w: %.2x\n",
room->direction[DIRECTION_NORTH],
room->direction[DIRECTION_SOUTH],
@ -322,10 +321,10 @@ void DebuggerDumper::dumpItems() {
item = &_game->_items[i];
print(" [%.2x] %s\n", i + 1,
item->string_desc ? string_lookup(_game, item->string_desc) : "");
item->string_desc ? _game->stringLookup(item->string_desc).c_str() : "");
if (_game->_comprehendVersion == 2)
print(" long desc: %s\n",
string_lookup(_game, item->long_string));
_game->stringLookup(item->long_string).c_str());
print(" words: ");
for (j = 0; j < _game->_nr_words; j++)

View File

@ -29,7 +29,6 @@
#include "glk/comprehend/draw_surface.h"
#include "glk/comprehend/game_data.h"
#include "glk/comprehend/opcode_map.h"
#include "glk/comprehend/strings.h"
#include "glk/comprehend/util.h"
namespace Glk {
@ -86,6 +85,51 @@ void ComprehendGame::synchronizeSave(Common::Serializer &s) {
_items[i].synchronize(s);
}
Common::String ComprehendGame::stringLookup(uint16 index) {
uint16 string;
uint8 table;
/*
* There are two tables of strings. The first is stored in the main
* game data file, and the second is stored in multiple string files.
*
* In instructions string indexes are split into a table and index
* value. In other places such as the save files strings from the
* main table are occasionally just a straight 16-bit index. We
* convert all string indexes to the former case so that we can handle
* them the same everywhere.
*/
table = (index >> 8) & 0xff;
string = index & 0xff;
switch (table) {
case 0x81:
case 0x01:
string += 0x100;
/* Fall-through */
case 0x00:
case 0x80:
if (string < _strings.nr_strings)
return _strings.strings[string];
break;
case 0x83:
string += 0x100;
/* Fall-through */
case 0x02:
case 0x82:
if (string < _strings2.nr_strings)
return _strings2.strings[string];
break;
}
return Common::String::format("BAD_STRING(%.4x)", index);
}
Common::String ComprehendGame::instrStringLookup(uint8 index, uint8 table) {
return stringLookup(table << 8 | index);
}
/*-------------------------------------------------------*/
static void console_init(void) {
@ -237,7 +281,7 @@ void game_restore(ComprehendGame *game) {
}
void game_restart(ComprehendGame *game) {
console_println(game, string_lookup(game, game->_gameStrings->game_restart));
console_println(game, game->stringLookup(game->_gameStrings->game_restart).c_str());
console_get_key();
comprehend_load_game(game);
@ -340,14 +384,14 @@ static void describe_objects_in_current_room(ComprehendGame *game) {
}
if (count > 0) {
console_println(game, string_lookup(game, STRING_YOU_SEE));
console_println(game, game->stringLookup(STRING_YOU_SEE).c_str());
for (i = 0; i < game->_header.nr_items; i++) {
item = &game->_items[i];
if (item->room == game->_currentRoom &&
item->string_desc != 0)
console_println(game, string_lookup(game, item->string_desc));
console_println(game, game->stringLookup(item->string_desc).c_str());
}
}
}
@ -364,7 +408,7 @@ static void update(ComprehendGame *game) {
&room_desc_string);
if (game->_updateFlags & UPDATE_ROOM_DESC)
console_println(game, string_lookup(game, room_desc_string));
console_println(game, game->stringLookup(room_desc_string).c_str());
if ((game->_updateFlags & UPDATE_ITEM_LIST) &&
room_type == ROOM_IS_NORMAL)
@ -528,9 +572,8 @@ static void eval_instruction(ComprehendGame *game,
break;
case OPCODE_PRINT:
console_println(game, instr_lookup_string(game,
instr->operand[0],
instr->operand[1]));
console_println(game, game->instrStringLookup(
instr->operand[0], instr->operand[1]).c_str());
break;
case OPCODE_TEST_NOT_ROOM_FLAG:
@ -576,14 +619,14 @@ static void eval_instruction(ComprehendGame *game,
if (room->direction[verb->_index - 1])
move_to(game, room->direction[verb->_index - 1]);
else
console_println(game, string_lookup(game, STRING_CANT_GO));
console_println(game, game->stringLookup(STRING_CANT_GO).c_str());
break;
case OPCODE_MOVE_DIRECTION:
if (room->direction[instr->operand[0] - 1])
move_to(game, room->direction[instr->operand[0] - 1]);
else
console_println(game, string_lookup(game, STRING_CANT_GO));
console_println(game, game->stringLookup(STRING_CANT_GO).c_str());
break;
case OPCODE_ELSE:
@ -626,7 +669,7 @@ static void eval_instruction(ComprehendGame *game,
* FIXME - unsure what the single operand is for.
*/
item = get_item_by_noun(game, noun);
printf("%s\n", string_lookup(game, item->long_string));
printf("%s\n", game->stringLookup(item->long_string).c_str());
break;
case OPCODE_CURRENT_OBJECT_IN_ROOM:
@ -771,32 +814,32 @@ static void eval_instruction(ComprehendGame *game,
case OPCODE_INVENTORY:
count = num_objects_in_room(game, ROOM_INVENTORY);
if (count == 0) {
console_println(game, string_lookup(game, STRING_INVENTORY_EMPTY));
console_println(game, game->stringLookup(STRING_INVENTORY_EMPTY).c_str());
break;
}
console_println(game, string_lookup(game, STRING_INVENTORY));
console_println(game, game->stringLookup(STRING_INVENTORY).c_str());
for (i = 0; i < game->_header.nr_items; i++) {
item = &game->_items[i];
if (item->room == ROOM_INVENTORY)
printf("%s\n",
string_lookup(game, item->string_desc));
game->stringLookup(item->string_desc).c_str());
}
break;
case OPCODE_INVENTORY_ROOM:
count = num_objects_in_room(game, instr->operand[0]);
if (count == 0) {
console_println(game, string_lookup(game, instr->operand[1] + 1));
console_println(game, game->stringLookup(instr->operand[1] + 1).c_str());
break;
}
console_println(game, string_lookup(game, instr->operand[1]));
console_println(game, game->stringLookup(instr->operand[1]).c_str());
for (i = 0; i < game->_header.nr_items; i++) {
item = &game->_items[i];
if (item->room == instr->operand[0])
printf("%s\n",
string_lookup(game, item->string_desc));
game->stringLookup(item->string_desc).c_str());
}
break;
@ -1074,7 +1117,7 @@ static bool handle_sentence(ComprehendGame *game,
}
/* No matching action */
console_println(game, string_lookup(game, STRING_DONT_UNDERSTAND));
console_println(game, game->stringLookup(STRING_DONT_UNDERSTAND).c_str());
return false;
}

View File

@ -67,6 +67,9 @@ public:
virtual void handle_special_opcode(uint8 operand) {}
void synchronizeSave(Common::Serializer &s);
Common::String stringLookup(uint16 index);
Common::String instrStringLookup(uint8 index, uint8 table);
};
void console_println(ComprehendGame *game, const char *text);

View File

@ -26,7 +26,6 @@
#include "glk/comprehend/draw_surface.h"
#include "glk/comprehend/file_buf.h"
#include "glk/comprehend/game.h"
#include "glk/comprehend/strings.h"
#include "glk/comprehend/util.h"
namespace Glk {

View File

@ -1,83 +0,0 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "glk/comprehend/comprehend.h"
#include "glk/comprehend/game.h"
#include "glk/comprehend/game_data.h"
#include "glk/comprehend/strings.h"
namespace Glk {
namespace Comprehend {
static char bad_string[128];
const char *string_lookup(ComprehendGame *game, uint16 index)
{
uint16 string;
uint8 table;
/*
* There are two tables of strings. The first is stored in the main
* game data file, and the second is stored in multiple string files.
*
* In instructions string indexes are split into a table and index
* value. In other places such as the save files strings from the
* main table are occasionally just a straight 16-bit index. We
* convert all string indexes to the former case so that we can handle
* them the same everywhere.
*/
table = (index >> 8) & 0xff;
string = index & 0xff;
switch (table) {
case 0x81:
case 0x01:
string += 0x100;
/* Fall-through */
case 0x00:
case 0x80:
if (string < game->_strings.nr_strings)
return game->_strings.strings[string];
break;
case 0x83:
string += 0x100;
/* Fall-through */
case 0x02:
case 0x82:
if (string < game->_strings2.nr_strings)
return game->_strings2.strings[string];
break;
}
snprintf(bad_string, sizeof(bad_string), "BAD_STRING(%.4x)", index);
return bad_string;
}
const char *instr_lookup_string(ComprehendGame *game, uint8 index,
uint8 table)
{
return string_lookup(game, table << 8 | index);
}
} // namespace Comprehend
} // namespace Glk

View File

@ -1,38 +0,0 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef GLK_COMPREHEND_STRINGS_H
#define GLK_COMPREHEND_STRINGS_H
namespace Glk {
namespace Comprehend {
class ComprehendGame;
const char *string_lookup(ComprehendGame *game, uint16 index);
const char *instr_lookup_string(ComprehendGame *game, uint8 index,
uint8 table);
} // namespace Comprehend
} // namespace Glk
#endif

View File

@ -181,7 +181,6 @@ MODULE_OBJS := \
comprehend/game_tr.o \
comprehend/image_data.o \
comprehend/opcode_map.o \
comprehend/strings.o \
comprehend/util.o \
frotz/bitmap_font.o \
frotz/config.o \