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
|
/* 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/archetype/sys_object.h"
#include "glk/archetype/archetype.h"
#include "glk/archetype/game_stat.h"
#include "glk/archetype/heap_sort.h"
#include "glk/archetype/parser.h"
#include "glk/archetype/wrap.h"
#include "common/algorithm.h"
#include "common/debug-channels.h"
#include "common/savefile.h"
namespace Glk {
namespace Archetype {
enum SysStateType {
IDLING, INIT_SORTER, OPEN_SORTER, CLOSE_SORTER, NEXT_SORTED, PLAYER_CMD,
NORMALIZE, ABBR, OPEN_PARSER, VERB_LIST, NOUN_LIST, CLOSE_PARSER, INIT_PARSER,
WHICH_OBJECT, ROLL_CALL, PRESENT, PARSE, NEXT_OBJECT, DEBUG_MESSAGES,
DEBUG_EXPRESSIONS, DEBUG_STATEMENTS, DEBUG_MEMORY, FREE_MEMORY, SAVE_STATE, LOAD_STATE
};
const char *const StateLookup[LOAD_STATE + 1] = {
"IDLING", "INIT SORTER", "OPEN SORTER", "CLOSE SORTER", "NEXT SORTED", "PLAYER CMD",
"NORMALIZE", "ABBR", "OPEN PARSER", "VERB LIST", "NOUN LIST", "CLOSE PARSER", "INIT PARSER",
"WHICH OBJECT", "ROLL CALL", "PRESENT", "PARSE", "NEXT OBJECT", "DEBUG MESSAGES",
"DEBUG EXPRESSIONS", "DEBUG STATEMENTS", "DEBUG MEMORY", "FREE MEMORY", "SAVE STATE", "LOAD STATE"
};
// Global variables which retain the state of the system object between calls
SysStateType sys_state;
TargetListType target_list;
void sys_object_init() {
sys_state = IDLING;
target_list = PARSER_VERBLIST;
}
static bool figure_state(const String &s) {
for (int st = IDLING; st <= LOAD_STATE; ++st) {
if (StateLookup[st] == s) {
sys_state = (SysStateType)st;
return true;
}
}
return false;
}
void send_to_system(int transport, String &strmsg, ResultType &result, ContextType &context) {
int the_caller;
int obj_index;
String nomatch;
NodePtr np;
void *p;
if (transport == OP_SEND)
the_caller = context.self;
else
the_caller = context.sender;
do {
cleanup(result);
switch (sys_state) {
case IDLING:
if (figure_state(strmsg)) {
switch (sys_state) {
case PLAYER_CMD:
case ABBR:
case SAVE_STATE:
case LOAD_STATE:
case OPEN_PARSER:
case OPEN_SORTER:
case WHICH_OBJECT:
return; // come back again!
case INIT_SORTER:
reinit_heap();
sys_state = OPEN_SORTER;
return;
case INIT_PARSER:
new_parse_list();
sys_state = OPEN_PARSER;
return;
default:
break;
}
}
case PLAYER_CMD:
normalize_string(strmsg, g_vm->Command);
sys_state = IDLING;
break;
case NORMALIZE:
// last normalized command
result._kind = STR_PTR;
result._data._str.acl_str = NewDynStr(g_vm->Command);
sys_state = IDLING;
case ABBR:
result._kind = STR_PTR;
result._data._str.acl_str = NewDynStr(strmsg);
if (convert_to(NUMERIC, result)) {
g_vm->Abbreviate = result._data._numeric.acl_int;
}
else {
wraperr("Warning: non-numeric abbreviation message sent to system");
cleanup(result);
}
sys_state = IDLING;
break;
case OPEN_PARSER:
if (figure_state(strmsg)) {
switch (sys_state) {
case CLOSE_PARSER:
sys_state = IDLING;
break;
case VERB_LIST:
target_list = PARSER_VERBLIST;
sys_state = OPEN_PARSER;
break;
case NOUN_LIST:
target_list = PARSER_NOUNLIST;
sys_state = OPEN_PARSER;
break;
default:
break;
}
}
else {
add_parse_word(target_list, strmsg, the_caller);
}
return;
case OPEN_SORTER:
if (figure_state(strmsg)) {
switch (sys_state) {
case CLOSE_SORTER:
sys_state = IDLING;
break;
default:
break;
}
} else {
drop_str_on_heap(strmsg);
}
return;
case NEXT_SORTED:
if (!pop_heap(p)) {
cleanup(result);
} else {
result._kind = STR_PTR;
result._data._str.acl_str = (StringPtr)p;
sys_state = IDLING;
}
break;
case WHICH_OBJECT:
obj_index = find_object(strmsg);
if (obj_index != 0) {
result._kind = IDENT;
result._data._ident.ident_kind = OBJECT_ID;
result._data._ident.ident_int = obj_index;
}
sys_state = IDLING;
break;
case ROLL_CALL:
dispose_list(g_vm->Proximate);
new_list(g_vm->Proximate);
sys_state = IDLING;
break;
case PRESENT:
np = new NodeType();
np->data = nullptr;
np->key = the_caller;
insert_item(g_vm->Proximate, np);
sys_state = IDLING;
break;
case PARSE:
parse_sentence();
sys_state = IDLING;
break;
case NEXT_OBJECT:
if (!pop_object(obj_index, nomatch)) {
cleanup(result);
} else if (obj_index < 0) {
result._kind = STR_PTR;
result._data._str.acl_str = NewDynStr(nomatch);
} else {
result._kind = IDENT;
result._data._ident.ident_kind = OBJECT_ID;
result._data._ident.ident_int = obj_index;
}
sys_state = IDLING;
break;
case DEBUG_MESSAGES:
if (DebugMan.isDebugChannelEnabled(DEBUG_MSGS))
DebugMan.disableDebugChannel(DEBUG_MSGS);
else
DebugMan.enableDebugChannel(DEBUG_MSGS);
sys_state = IDLING;
break;
case DEBUG_EXPRESSIONS:
if (DebugMan.isDebugChannelEnabled(DEBUG_EXPRESSIONS))
DebugMan.disableDebugChannel(DEBUG_EXPRESSIONS);
else
DebugMan.enableDebugChannel(DEBUG_EXPRESSIONS);
sys_state = IDLING;
break;
case DEBUG_STATEMENTS:
if (DebugMan.isDebugChannelEnabled(DEBUG_STATEMENTS))
DebugMan.disableDebugChannel(DEBUG_STATEMENTS);
else
DebugMan.enableDebugChannel(DEBUG_STATEMENTS);
sys_state = IDLING;
break;
case DEBUG_MEMORY:
wrapout("", true); // get to beginning of line
//g_vm->writeln("Maximum memory request: %d bytes", MaxAvail);
//g_vm->writeln("Actual free memory: %d bytes", MemAvail);
sys_state = IDLING;
break;
case FREE_MEMORY:
result._kind = NUMERIC;
result._data._numeric.acl_int = 0xffff; // MemAvail;
sys_state = IDLING;
break;
case SAVE_STATE: {
Common::OutSaveFile *stfile = g_system->getSavefileManager()->openForSaving(strmsg);
if (stfile == nullptr) {
g_vm->writeln("Error opening %s", strmsg.c_str());
cleanup(result);
}
else {
save_game_state(stfile, g_vm->Object_List);
result._kind = RESERVED;
result._data._reserved.keyword = RW_TRUE;
stfile->finalize();
delete stfile;
}
sys_state = IDLING;
break;
}
case LOAD_STATE: {
Common::InSaveFile *stfile = g_system->getSavefileManager()->openForLoading(strmsg);
if (stfile == nullptr) {
g_vm->writeln("Error opening %s", strmsg.c_str());
cleanup(result);
} else {
result._kind = RESERVED;
result._data._reserved.keyword = load_game_state(stfile, g_vm->Object_List) ? RW_TRUE : RW_FALSE;
delete stfile;
}
sys_state = IDLING;
break;
}
default:
break;
}
if (g_vm->shouldQuit())
sys_state = IDLING;
} while (sys_state != IDLING);
}
} // End of namespace Archetype
} // End of namespace Glk
|