aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/frotz/frotz.cpp
blob: 95cdb22ee92210818958a03f7bb5142b4a978c5a (plain)
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
/* 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/frotz/frotz.h"
#include "glk/frotz/frotz_types.h"
#include "common/config-manager.h"

namespace Glk {
namespace Frotz {

Frotz *g_vm;

Frotz::Frotz(OSystem *syst, const GlkGameDescription &gameDesc) :
		Processor(syst, gameDesc) {
	g_vm = this;
}

Frotz::~Frotz() {
	reset_memory();
}

void Frotz::runGame(Common::SeekableReadStream *gameFile) {
	story_fp = gameFile;
	initialize();

	// Game loop
	interpret();
}

void Frotz::initialize() {
	// Call process initialization
	Processor::initialize();

	// Restart the game
	z_restart();
}

Common::Error Frotz::saveGameData(strid_t file) {
#ifdef TODO
	long pc;
	zword addr;
	zword nsp, nfp;
	int skip;
	int i;

	// Open game file

	if ((gfp = frotzopenprompt(FILE_SAVE)) == nullptr)
		goto finished;

	if (_quetzal) {
		success = save_quetzal(gfp, story_fp, blorb_ofs);
	}
	else {
		// Write game file

		fputc((int)hi(h_release), gfp);
		fputc((int)lo(h_release), gfp);
		fputc((int)hi(h_checksum), gfp);
		fputc((int)lo(h_checksum), gfp);

		GET_PC(pc)

			fputc((int)(pc >> 16) & 0xff, gfp);
		fputc((int)(pc >> 8) & 0xff, gfp);
		fputc((int)(pc)& 0xff, gfp);

		nsp = (int)(_sp - _stack);
		nfp = (int)(_fp - _stack);

		fputc((int)hi(nsp), gfp);
		fputc((int)lo(nsp), gfp);
		fputc((int)hi(nfp), gfp);
		fputc((int)lo(nfp), gfp);

		for (i = nsp; i < STACK_SIZE; i++) {
			fputc((int)hi(_stack[i]), gfp);
			fputc((int)lo(_stack[i]), gfp);
		}

		fseek(story_fp, blorb_ofs, SEEK_SET);

		for (addr = 0, skip = 0; addr < h_dynamic_size; addr++)
			if (zmp[addr] != fgetc(story_fp) || skip == 255 || addr + 1 == h_dynamic_size) {
				fputc(skip, gfp);
				fputc(zmp[addr], gfp);
				skip = 0;
			}
			else skip++;
	}

	// Close game file and check for errors

	if (fclose(gfp) == EOF || ferror(story_fp)) {
		print_string("Error writing save file\n");
		goto finished;
	}

	// Success
	success = 1;
#endif
	return Common::kNoError;
}

Common::Error Frotz::loadGameData(strid_t file) {
#ifdef TODO
	long pc;
	zword release;
	zword addr;
	int i;

	// Open game file
	if ((gfp = frotzopenprompt(FILE_RESTORE)) == nullptr)
		goto finished;

	if (_quetzal) {
		success = restore_quetzal (gfp, story_fp, blorb_ofs);

	} else {
		// Load game file

		release = (unsigned) fgetc (gfp) << 8;
		release |= fgetc (gfp);

		() fgetc (gfp);
		() fgetc (gfp);

		// Check the release number
		if (release == h_release) {

			pc = (long) fgetc (gfp) << 16;
			pc |= (unsigned) fgetc (gfp) << 8;
			pc |= fgetc (gfp);

			SET_PC (pc);

			_sp = _stack + (fgetc (gfp) << 8);
			_sp += fgetc (gfp);
			_fp = _stack + (fgetc (gfp) << 8);
			_fp += fgetc (gfp);

			for (i = (int) (_sp - _stack); i < STACK_SIZE; i++) {
				_stack[i] = (unsigned) fgetc (gfp) << 8;
				_stack[i] |= fgetc (gfp);
			}

			fseek (story_fp, blorb_ofs, SEEK_SET);

			for (addr = 0; addr < h_dynamic_size; addr++) {
				int skip = fgetc (gfp);
				for (i = 0; i < skip; i++)
					zmp[addr++] = fgetc (story_fp);
				zmp[addr] = fgetc (gfp);
				() fgetc (story_fp);
			}

			// Check for errors
			if (ferror (gfp) || ferror (story_fp) || addr != h_dynamic_size)
				success = -1;
			else

				// Success
				success = 2;

		} else print_string ("Invalid save file\n");
	}

	if ((short) success >= 0) {

		// Close game file
		fclose (gfp);

		if ((short) success > 0) {
			zbyte old_screen_rows;
			zbyte old_screen_cols;

			// In V3, reset the upper window.
			if (h_version == V3)
				split_window (0);

			LOW_BYTE (H_SCREEN_ROWS, old_screen_rows);
			LOW_BYTE (H_SCREEN_COLS, old_screen_cols);

			/* Reload cached header fields. */
			restart_header ();

			/*
				* Since QUETZAL files may be saved on many different machines,
				* the screen sizes may vary a lot. Erasing the status window
				* seems to cover up most of the resulting badness.
				*/
			if (h_version > V3 && h_version != V6
					&& (h_screen_rows != old_screen_rows
						|| h_screen_cols != old_screen_cols))
				erase_window (1);
		}
	} else
		os_fatal ("Error reading save file");
#endif
	return Common::kNoError;
}

} // End of namespace Scott
} // End of namespace Glk