1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "glk/advsys/advsys.h"
24 #include "common/translation.h"
25 #include "common/config-manager.h"
26
27 namespace Glk {
28 namespace AdvSys {
29
runGame()30 void AdvSys::runGame() {
31 // Check for savegame
32 _saveSlot = ConfMan.hasKey("save_slot") ? ConfMan.getInt("save_slot") : -1;
33
34 if (!initialize()) {
35 GUIErrorMessage(_("Could not start AdvSys game"));
36 return;
37 }
38
39 // Outer play loop - this loop re-iterates if a game is restarted
40 while (!shouldQuit()) {
41 // Run game startup
42 execute(_initCodeOffset);
43
44 if (_saveSlot != -1) {
45 Common::ErrorCode err = loadGameState(_saveSlot).getCode();
46 _saveSlot = -1;
47 if (err != Common::kNoError)
48 print(_("Sorry, the savegame couldn't be restored"));
49 else
50 _pendingLine = "look"; // Do a look action after loading the savegame
51 }
52
53 // Gameplay loop
54 while (!shouldQuit() && !shouldRestart()) {
55 // Run update code
56 execute(_updateCodeOffset);
57
58 // Get and parse a single line
59 if (getInput()) {
60 if (singleAction()) {
61 while (!shouldQuit() && nextCommand() && singleAction()) {}
62 }
63 }
64 }
65 }
66
67 deinitialize();
68 }
69
initialize()70 bool AdvSys::initialize() {
71 // Create a Glk window for the game
72 if (!GlkInterface::initialize())
73 return false;
74
75 // Load the game's header
76 if (!Game::init(&_gameFile))
77 return false;
78
79 return true;
80 }
81
deinitialize()82 void AdvSys::deinitialize() {
83 }
84
singleAction()85 bool AdvSys::singleAction() {
86 // Do the before code
87 switch (execute(_beforeOffset)) {
88 case ABORT:
89 // Script aborted
90 return false;
91 case CHAIN:
92 // Execute the action handler
93 if (execute(getActionField(getVariable(V_ACTION), A_CODE)) == ABORT)
94 return false;
95
96 // fall through
97 case FINISH:
98 // Do the after code
99 if (execute(_afterOffset) == ABORT)
100 return false;
101 break;
102
103 default:
104 break;
105 }
106
107 return true;
108 }
109
readSaveData(Common::SeekableReadStream * rs)110 Common::Error AdvSys::readSaveData(Common::SeekableReadStream *rs) {
111 if (rs->size() != (int)_saveSize)
112 return Common::kReadingFailed;
113
114 rs->read(_saveArea, rs->size());
115 return Common::kNoError;
116 }
117
writeGameData(Common::WriteStream * ws)118 Common::Error AdvSys::writeGameData(Common::WriteStream *ws) {
119 ws->write(_saveArea, _saveSize);
120 return Common::kNoError;
121 }
122
123 } // End of namespace AdvSys
124 } // End of namespace Glk
125