1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "glk/comprehend/game.h"
#include "common/debug-channels.h"
#include "common/translation.h"
#include "glk/comprehend/comprehend.h"
#include "glk/comprehend/debugger.h"
#include "glk/comprehend/dictionary.h"
#include "glk/comprehend/draw_surface.h"
#include "glk/comprehend/game_data.h"
namespace Glk {
namespace Comprehend {
void Sentence::clear() {
for (uint idx = 0; idx < 4; ++idx)
_words[idx].clear();
for (uint idx = 0; idx < 6; ++idx)
_formattedWords[idx] = 0;
_nr_words = 0;
_specialOpcodeVal2 = 0;
}
void Sentence::copyFrom(const Sentence &src, bool copyNoun) {
for (uint idx = (copyNoun ? 0 : 1); idx < 6; ++idx)
_formattedWords[idx] = src._formattedWords[idx];
}
void Sentence::format() {
for (uint idx = 0; idx < 6; ++idx)
_formattedWords[idx] = 0;
byte wordTypes[5] = { 0, 0, 0, 0, 0 };
for (uint idx = 0; idx < _nr_words; ++idx) {
const Word &w = _words[idx];
if (w._type & 8) {
if (w._type < 24) {
int index, type;
if (w._type & 0xf0 & wordTypes[2]) {
index = _formattedWords[2];
type = wordTypes[2];
} else if (w._type & 0xf0 & wordTypes[3]) {
index = _formattedWords[3];
type = wordTypes[3];
} else {
continue;
}
if (!_formattedWords[2]) {
_formattedWords[2] = index;
wordTypes[2] = type;
} else if (!_formattedWords[3]) {
_formattedWords[3] = index;
wordTypes[3] = type;
}
} else {
if (w._type == 8)
_specialOpcodeVal2 = 1;
else if (w._type == 9)
_specialOpcodeVal2 = 2;
}
} else {
int val = w._type & 0xf0;
if (val) {
if ((w._type & 1) && !_formattedWords[0]) {
_formattedWords[0] = w._index;
} else if (!_formattedWords[2]) {
_formattedWords[2] = w._index;
wordTypes[2] = val;
} else if (!_formattedWords[3]) {
_formattedWords[3] = w._index;
wordTypes[3] = val;
}
} else if (w._type & 1) {
if (!_formattedWords[0]) {
_formattedWords[0] = w._index;
} else if (!_formattedWords[1]) {
_formattedWords[1] = w._index;
}
} else if (w._type == 2) {
if (!_formattedWords[4])
_formattedWords[4] = w._index;
} else if (w._type == 4) {
if (!_formattedWords[5])
_formattedWords[5] = w._index;
}
}
}
}
/*-------------------------------------------------------*/
ComprehendGame::ComprehendGame() : _gameStrings(nullptr), _ended(false),
_functionNum(0), _specialOpcode(0), _nounState(NOUNSTATE_INITIAL),
_inputLineIndex(0), _currentRoomCopy(-1), _redoLine(REDO_NONE) {
Common::fill(&_inputLine[0], &_inputLine[INPUT_LINE_SIZE], 0);
}
ComprehendGame::~ComprehendGame() {
}
void ComprehendGame::synchronizeSave(Common::Serializer &s) {
uint dir, i;
size_t nr_rooms, nr_items;
s.syncAsUint16LE(_currentRoom);
// Variables
for (i = 0; i < ARRAY_SIZE(_variables); i++)
s.syncAsUint16LE(_variables[i]);
// Flags
for (i = 0; i < ARRAY_SIZE(_flags); i++)
s.syncAsByte(_flags[i]);
// Rooms. Note that index 0 is the player's inventory
nr_rooms = _rooms.size();
s.syncAsByte(nr_rooms);
assert(nr_rooms == _rooms.size());
for (i = 1; i < _rooms.size(); ++i) {
s.syncAsUint16LE(_rooms[i]._stringDesc);
for (dir = 0; dir < NR_DIRECTIONS; dir++)
s.syncAsByte(_rooms[i]._direction[dir]);
s.syncAsByte(_rooms[i]._flags);
s.syncAsByte(_rooms[i]._graphic);
}
// Objects
nr_items = _items.size();
s.syncAsByte(nr_items);
assert(nr_items == _items.size());
for (i = 0; i < _items.size(); ++i)
_items[i].synchronize(s);
_redoLine = REDO_NONE;
}
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.size())
return _strings[string];
break;
case 0x83:
string += 0x100;
/* Fall-through */
case 0x02:
case 0x82:
if (string < _strings2.size())
return _strings2[string];
break;
}
return Common::String::format("BAD_STRING(%.4x)", index);
}
Common::String ComprehendGame::instrStringLookup(uint8 index, uint8 table) {
return stringLookup(table << 8 | index);
}
int ComprehendGame::console_get_key() {
return g_comprehend->readChar();
}
void ComprehendGame::console_println(const char *text) {
const char *replace, *word = nullptr, *p = text;
char bad_word[64];
int word_len = 0;
if (!text) {
g_comprehend->print("\n");
return;
}
while (*p) {
switch (*p) {
case '\n':
word = nullptr;
word_len = 0;
g_comprehend->print("\n");
p++;
break;
case '@':
/* Replace word */
if (_currentReplaceWord >= _replaceWords.size()) {
snprintf(bad_word, sizeof(bad_word),
"[BAD_REPLACE_WORD(%.2x)]",
_currentReplaceWord);
word = bad_word;
} else {
word = _replaceWords[_currentReplaceWord].c_str();
}
word_len = strlen(word);
p++;
break;
default:
/* Find next space */
word_len = strcspn(p, " \n");
if (word_len == 0)
break;
/*
* If this word contains a replacement symbol, then
* print everything before the symbol.
*/
replace = strchr(p, '@');
if (replace)
word_len = replace - p;
word = p;
p += word_len;
break;
}
if (!word || !word_len)
continue;
Common::String wordStr(word, word_len);
g_comprehend->print("%s", wordStr.c_str());
if (*p == ' ') {
g_comprehend->print(" ");
p++;
/* Skip any double spaces */
while (*p == ' ')
p++;
}
}
g_comprehend->print("\n");
}
Room *ComprehendGame::get_room(uint16 index) {
/* Room zero is reserved for the players inventory */
if (index == 0)
error("Room index 0 (player inventory) is invalid");
if (index >= (int)_rooms.size())
error("Room index %d is invalid", index);
return &_rooms[index];
}
Item *ComprehendGame::get_item(uint16 index) {
if (index >= _items.size())
error("Bad item %d\n", index);
return &_items[index];
}
void ComprehendGame::game_save() {
int c;
console_println(_strings[STRING_SAVE_GAME].c_str());
c = console_get_key();
if (g_comprehend->shouldQuit())
return;
if (c < '1' || c > '3') {
/*
* The original Comprehend games just silently ignore any
* invalid selection.
*/
console_println("Invalid save game number");
return;
}
g_comprehend->saveGameState(c - '0', _("Savegame"));
}
void ComprehendGame::game_restore() {
int c;
console_println(_strings[STRING_RESTORE_GAME].c_str());
c = console_get_key();
if (g_comprehend->shouldQuit())
return;
if (c < '1' || c > '3') {
/*
* The original Comprehend games just silently ignore any
* invalid selection.
*/
console_println("Invalid save game number");
return;
}
(void)g_comprehend->loadGameState(c - '0');
}
bool ComprehendGame::handle_restart() {
console_println(stringLookup(_gameStrings->game_restart).c_str());
_ended = false;
if (tolower(console_get_key()) == 'r') {
loadGame();
_updateFlags = UPDATE_ALL;
return true;
} else {
g_comprehend->quitGame();
return false;
}
}
Item *ComprehendGame::get_item_by_noun(byte noun) {
uint i;
if (!noun)
return nullptr;
/*
* FIXME - in oo-topos the word 'box' matches more than one object
* (the box and the snarl-in-a-box). The player is unable
* to drop the latter because this will match the former.
*/
for (i = 0; i < _items.size(); i++)
if (_items[i]._word == noun)
return &_items[i];
return nullptr;
}
int ComprehendGame::get_item_id(byte noun) {
for (int i = 0; i < (int)_items.size(); i++)
if (_items[i]._word == noun)
return i;
return -1;
}
void ComprehendGame::update_graphics() {
Item *item;
Room *room;
int type;
uint i;
if (!g_comprehend->isGraphicsEnabled())
return;
type = roomIsSpecial(_currentRoomCopy, nullptr);
switch (type) {
case ROOM_IS_DARK:
if (_updateFlags & UPDATE_GRAPHICS)
g_comprehend->clearScreen(false);
break;
case ROOM_IS_TOO_BRIGHT:
if (_updateFlags & UPDATE_GRAPHICS)
g_comprehend->clearScreen(true);
break;
default:
if (_updateFlags & UPDATE_GRAPHICS) {
room = get_room(_currentRoom);
g_comprehend->drawLocationPicture(room->_graphic - 1);
}
if ((_updateFlags & UPDATE_GRAPHICS) ||
(_updateFlags & UPDATE_GRAPHICS_ITEMS)) {
for (i = 0; i < _items.size(); i++) {
item = &_items[i];
if (item->_room == _currentRoom &&
item->_graphic != 0)
g_comprehend->drawItemPicture(item->_graphic - 1);
}
}
break;
}
}
void ComprehendGame::describe_objects_in_current_room() {
Item *item;
size_t count = 0;
uint i;
for (i = 0; i < _items.size(); i++) {
item = &_items[i];
if (item->_room == _currentRoom && item->_stringDesc != 0
&& !(item->_flags & ITEMF_INVISIBLE))
count++;
}
if (count > 0) {
console_println(stringLookup(STRING_YOU_SEE).c_str());
for (i = 0; i < _items.size(); i++) {
item = &_items[i];
if (item->_room == _currentRoom && item->_stringDesc != 0
&& !(item->_flags & ITEMF_INVISIBLE))
console_println(stringLookup(item->_stringDesc).c_str());
}
}
}
void ComprehendGame::updateRoomDesc() {
Room *room = get_room(_currentRoom);
uint room_desc_string = room->_stringDesc;
roomIsSpecial(_currentRoom, &room_desc_string);
Common::String desc = stringLookup(room_desc_string);
g_comprehend->printRoomDesc(desc);
}
void ComprehendGame::update() {
Room *room = get_room(_currentRoom);
uint room_type, room_desc_string;
update_graphics();
/* Check if the room is special (dark, too bright, etc) */
room_desc_string = room->_stringDesc;
room_type = roomIsSpecial(_currentRoom, &room_desc_string);
if (_updateFlags & UPDATE_ROOM_DESC) {
Common::String desc = stringLookup(room_desc_string);
console_println(desc.c_str());
g_comprehend->printRoomDesc(desc.c_str());
}
if ((_updateFlags & UPDATE_ITEM_LIST) && room_type == ROOM_IS_NORMAL)
describe_objects_in_current_room();
_updateFlags = 0;
}
void ComprehendGame::move_to(uint8 room) {
if (room >= (int)_rooms.size())
error("Attempted to move to invalid room %.2x\n", room);
_currentRoom = _currentRoomCopy = room;
_updateFlags = (UPDATE_GRAPHICS | UPDATE_ROOM_DESC |
UPDATE_ITEM_LIST);
}
size_t ComprehendGame::num_objects_in_room(int room) {
size_t count = 0, i;
for (i = 0; i < _items.size(); i++)
if (_items[i]._room == room)
count++;
return count;
}
void ComprehendGame::move_object(Item *item, int new_room) {
unsigned obj_weight = item->_flags & ITEMF_WEIGHT_MASK;
if (item->_room == new_room)
return;
if (item->_room == ROOM_INVENTORY) {
/* Removed from player's inventory */
_variables[VAR_INVENTORY_WEIGHT] -= obj_weight;
}
if (new_room == ROOM_INVENTORY) {
/* Moving to the player's inventory */
_variables[VAR_INVENTORY_WEIGHT] += obj_weight;
}
if (item->_room == _currentRoom) {
/* Item moved away from the current room */
_updateFlags |= UPDATE_GRAPHICS;
} else if (new_room == _currentRoom) {
/*
* Item moved into the current room. Only the item needs a
* redraw, not the whole room.
*/
_updateFlags |= (UPDATE_GRAPHICS_ITEMS |
UPDATE_ITEM_LIST);
}
item->_room = new_room;
}
void ComprehendGame::eval_instruction(FunctionState *func_state,
const Function &func, uint functionOffset, const Sentence *sentence) {
const Instruction *instr = &func[functionOffset];
if (DebugMan.isDebugChannelEnabled(kDebugScripts)) {
Common::String line;
if (!instr->_isCommand) {
line += "? ";
} else {
if (func_state->_testResult)
line += "+ ";
else
line += "- ";
}
line += Common::String::format("%.2x ", functionOffset);
line += g_debugger->dumpInstruction(this, func_state, instr);
debugC(kDebugScripts, "%s", line.c_str());
}
if (func_state->_orCount)
func_state->_orCount--;
if (instr->_isCommand) {
bool do_command;
func_state->_inCommand = true;
do_command = func_state->_testResult;
if (func_state->_orCount != 0)
g_comprehend->print("Warning: or_count == %d\n",
func_state->_orCount);
func_state->_orCount = 0;
if (!do_command)
return;
func_state->_elseResult = false;
func_state->_executed = true;
} else {
if (func_state->_inCommand) {
/* Finished command sequence - clear test result */
func_state->_inCommand = false;
func_state->_testResult = false;
func_state->_and = false;
}
}
execute_opcode(instr, sentence, func_state);
}
void ComprehendGame::eval_function(uint functionNum, const Sentence *sentence) {
FunctionState func_state;
uint i;
const Function &func = _functions[functionNum];
func_state._elseResult = true;
func_state._executed = false;
debugC(kDebugScripts, "Start of function %.4x", functionNum);
for (i = 0; i < func.size(); i++) {
if (func_state._executed && !func[i]._isCommand) {
/*
* At least one command has been executed and the
* current instruction is a test. Exit the function.
*/
break;
}
eval_instruction(&func_state, func, i, sentence);
}
debugC(kDebugScripts, "End of function %.4x\n", functionNum);
}
void ComprehendGame::skip_whitespace(const char **p) {
while (**p && Common::isSpace(**p))
(*p)++;
}
void ComprehendGame::skip_non_whitespace(const char **p) {
while (**p && !Common::isSpace(**p) && **p != ',' && **p != '\n')
(*p)++;
}
bool ComprehendGame::handle_sentence(Sentence *sentence) {
if (sentence->_nr_words == 1 && !strcmp(sentence->_words[0]._word, "quit")) {
g_comprehend->quitGame();
return true;
}
// Set up default sentence
Common::Array<byte> words;
const byte *src = &sentence->_formattedWords[0];
if (src[1] && src[3]) {
words.clear();
for (int idx = 0; idx < 4; ++idx)
words.push_back(src[idx]);
if (handle_sentence(0, sentence, words))
return true;
}
if (src[1]) {
words.clear();
for (int idx = 0; idx < 3; ++idx)
words.push_back(src[idx]);
if (handle_sentence(1, sentence, words))
return true;
}
if (src[3] && src[4]) {
words.clear();
words.push_back(src[4]);
words.push_back(src[0]);
words.push_back(src[2]);
words.push_back(src[3]);
if (handle_sentence(2, sentence, words))
return true;
}
if (src[4]) {
words.clear();
words.push_back(src[4]);
words.push_back(src[0]);
words.push_back(src[2]);
if (handle_sentence(3, sentence, words))
return true;
}
if (src[3]) {
words.clear();
words.push_back(src[0]);
words.push_back(src[2]);
words.push_back(src[3]);
if (handle_sentence(4, sentence, words))
return true;
}
if (src[2]) {
words.clear();
words.push_back(src[0]);
words.push_back(src[2]);
if (handle_sentence(5, sentence, words))
return true;
}
if (src[0]) {
words.clear();
words.push_back(src[0]);
if (handle_sentence(6, sentence, words))
return true;
}
return false;
}
bool ComprehendGame::handle_sentence(uint tableNum, Sentence *sentence, Common::Array<byte> &words) {
const ActionTable &table = _actions[tableNum];
for (uint i = 0; i < table.size(); i++) {
const Action &action = table[i];
// Check for a match on the words of the action
bool isMatch = true;
for (uint idx = 0; idx < action._nr_words && isMatch; ++idx)
isMatch = action._words[idx] == words[idx];
if (isMatch) {
// Match
_functionNum = action._function;
return true;
}
}
// No matching action
return false;
}
void ComprehendGame::handleAction(Sentence *sentence) {
_specialOpcode = 0;
if (_functionNum == 0) {
console_println(stringLookup(STRING_DONT_UNDERSTAND).c_str());
} else {
eval_function(_functionNum, sentence);
_functionNum = 0;
eval_function(0, nullptr);
}
handleSpecialOpcode();
}
void ComprehendGame::read_sentence(Sentence *sentence) {
bool sentence_end = false;
const char *word_string, *p = &_inputLine[_inputLineIndex];
Word *word;
sentence->clear();
for (;;) {
// Get the next word
skip_whitespace(&p);
word_string = p;
skip_non_whitespace(&p);
Common::String wordStr(word_string, p);
// Check for end of sentence
// FIXME: The below is a hacked simplified version of how the
// original handles cases like "get item1, item2"
if (*p == ',' || *p == '\n' || wordStr.equalsIgnoreCase("and")) {
// Sentence separator
++p;
sentence_end = true;
} else if (*p == '\0') {
sentence_end = true;
}
/* Find the dictionary word for this */
word = dict_find_word_by_string(this, wordStr.c_str());
if (!word)
sentence->_words[sentence->_nr_words].clear();
else
sentence->_words[sentence->_nr_words] = *word;
sentence->_nr_words++;
if (sentence->_nr_words >= ARRAY_SIZE(sentence->_words) ||
sentence_end)
break;
}
parse_sentence_word_pairs(sentence);
sentence->format();
_inputLineIndex = p - _inputLine;
}
void ComprehendGame::parse_sentence_word_pairs(Sentence *sentence) {
if (sentence->_nr_words < 2)
return;
// Iterate through the pairs
for (uint idx = 0; idx < _wordMaps.size(); ++idx) {
for (int firstWord = 0; firstWord < (int)sentence->_nr_words - 1; ++firstWord) {
for (int secondWord = firstWord + 1; secondWord < (int)sentence->_nr_words; ) {
if (sentence->_words[firstWord] == _wordMaps[idx]._word[0] &&
sentence->_words[secondWord] == _wordMaps[idx]._word[1]) {
// Found a word pair match
// Delete the second word
for (; secondWord < (int)sentence->_nr_words - 1; ++secondWord)
sentence->_words[secondWord] = sentence->_words[secondWord + 1];
sentence->_words[sentence->_nr_words - 1].clear();
sentence->_nr_words--;
// Replace the first word with the target
sentence->_words[firstWord] = _wordMaps[idx]._word[2];
} else {
// Move to next word
++secondWord;
}
}
}
}
}
void ComprehendGame::doBeforeTurn() {
// Make a copy of the current room
_currentRoomCopy = _currentRoom;
beforeTurn();
if (!_ended)
update();
}
void ComprehendGame::beforeTurn() {
// Run the each turn functions
eval_function(0, nullptr);
}
void ComprehendGame::read_input() {
Sentence tempSentence;
bool handled;
turn:
doBeforeTurn();
if (_ended)
return;
// If we're in full screen text, we can afford a blank row between
// any game response and the next line of text
if (!g_comprehend->isGraphicsEnabled())
g_comprehend->print("\n");
beforePrompt();
for (;;) {
_redoLine = REDO_NONE;
g_comprehend->print("> ");
g_comprehend->readLine(_inputLine, INPUT_LINE_SIZE);
if (g_comprehend->shouldQuit())
return;
_inputLineIndex = 0;
if (strlen(_inputLine) == 0) {
// Empty line, so toggle picture window visibility
if (!g_comprehend->toggleGraphics())
updateRoomDesc();
g_comprehend->print(_("Picture window toggled\n"));
_updateFlags |= UPDATE_GRAPHICS;
update_graphics();
continue;
}
afterPrompt();
if (_redoLine == REDO_NONE)
break;
else if (_redoLine == REDO_TURN)
goto turn;
}
for (;;) {
NounState prevNounState = _nounState;
_nounState = NOUNSTATE_STANDARD;
read_sentence(&tempSentence);
_sentence.copyFrom(tempSentence, tempSentence._formattedWords[0] || prevNounState != NOUNSTATE_STANDARD);
handled = handle_sentence(&_sentence);
handleAction(&_sentence);
if (!handled)
return;
/* FIXME - handle the 'before you can continue' case */
if (_inputLine[_inputLineIndex] == '\0')
break;
}
afterTurn();
}
void ComprehendGame::playGame() {
if (!g_comprehend->loadLauncherSavegameIfNeeded())
beforeGame();
_updateFlags = (uint)UPDATE_ALL;
while (!g_comprehend->shouldQuit()) {
read_input();
if (_ended && !handle_restart())
break;
}
}
uint ComprehendGame::getRandomNumber(uint max) const {
return g_comprehend->getRandomNumber(max);
}
void ComprehendGame::doMovementVerb(uint verbNum) {
assert(verbNum >= 1 && verbNum <= NR_DIRECTIONS);
Room *room = get_room(_currentRoom);
byte newRoom = room->_direction[verbNum - 1];
if (newRoom)
move_to(newRoom);
else
console_println(_strings[0].c_str());
}
void ComprehendGame::weighInventory() {
_totalInventoryWeight = 0;
if (!g_debugger->_invLimit)
// Allow for an unlimited number of items in inventory
return;
for (int idx = _itemCount - 1; idx > 0; --idx) {
Item *item = get_item(idx);
if (item->_room == ROOM_INVENTORY)
_totalInventoryWeight += item->_flags & ITEMF_WEIGHT_MASK;
}
}
} // namespace Comprehend
} // namespace Glk
|