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
|
/* ScummVM - Scumm Interpreter
* Copyright (C) 2001-2005 The ScummVM project
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Header$
*
*/
#include "common/stdafx.h"
#include "gui/about.h"
#include "gui/message.h"
#include "simon/simon.h"
#include "simon/intern.h"
#include "common/savefile.h"
#include "common/system.h"
namespace Simon {
void SimonEngine::o_save_game() {
save_or_load_dialog(false);
}
void SimonEngine::o_load_game() {
save_or_load_dialog(true);
}
int SimonEngine::count_savegames() {
Common::InSaveFile *f;
uint i = 1;
bool marks[256];
char *prefix = gen_savename(999);
prefix[strlen(prefix)-3] = '\0';
_saveFileMan->listSavefiles(prefix, marks, 256);
while (i < 256) {
if (marks[i] &&
(f = _saveFileMan->openForLoading(gen_savename(i)))) {
i++;
delete f;
} else
break;
}
return i;
}
int SimonEngine::display_savegame_list(int curpos, bool load, char *dst) {
int slot, last_slot;
Common::InSaveFile *in;
showMessageFormat("\xC");
memset(dst, 0, 18 * 6);
slot = curpos;
while (curpos + 6 > slot) {
if (!(in = _saveFileMan->openForLoading(gen_savename(slot))))
break;
in->read(dst, 18);
delete in;
last_slot = slot;
if (slot < 10)
showMessageFormat(" ");
showMessageFormat("%d", slot);
showMessageFormat(".%s\n", dst);
dst += 18;
slot++;
}
// while_break
if (!load) {
if (curpos + 6 == slot)
slot++;
else {
if (slot < 10)
showMessageFormat(" ");
showMessageFormat("%d.\n", slot);
}
} else {
if (curpos + 6 == slot) {
if ((in = _saveFileMan->openForLoading(gen_savename(slot)))) {
slot++;
delete in;
}
}
}
return slot - curpos;
}
void SimonEngine::quick_load_or_save() {
// simon1demo subroutines are missing too many segments
// original demo didn't allow load or save either.
if (_game == GAME_SIMON1DEMO)
return;
bool success;
char buf[50];
char *filename = gen_savename(_saveLoadSlot);
if (_saveLoadType == 2) {
Subroutine *sub;
success = load_game(_saveLoadSlot);
if (!success) {
sprintf(buf, "Failed to load game state to file:\n\n%s", filename);
} else {
// Redraw Inventory
lock();
fcs_unk_proc_1(2, getItem1Ptr(), 0, 0);
unlock();
// Reset engine?
vc_set_bit_to(97, true);
sub = getSubroutineByID(100);
startSubroutine(sub);
}
} else {
success = save_game(_saveLoadSlot, _saveLoadName);
if (!success)
sprintf(buf, "Failed to save game state to file:\n\n%s", filename);
}
if (!success) {
GUI::MessageDialog dialog(buf, "OK");
dialog.runModal();
} else if (_saveLoadType == 1) {
sprintf(buf, "Successfully saved game state in file:\n\n%s", filename);
GUI::TimedMessageDialog dialog(buf, 1500);
dialog.runModal();
}
_saveLoadType = 0;
}
void SimonEngine::savegame_dialog(char *buf) {
int i;
o_unk_132_helper_3();
i = display_savegame_list(_saveLoadRowCurPos, _saveOrLoad, buf);
_saveDialogFlag = true;
if (i != 7) {
i++;
if (!_saveOrLoad)
i++;
_saveDialogFlag = false;
}
if (!--i)
return;
do {
clear_hitarea_bit_0x40(0xd0 + i - 1);
} while (--i);
}
void SimonEngine::save_or_load_dialog(bool load) {
time_t save_time;
int number_of_savegames;
int i;
int unk132_result;
FillOrCopyStruct *fcs;
char *name;
int name_len;
bool b;
char buf[108];
_saveOrLoad = load;
save_time = time(NULL);
_copyPartialMode = 1;
number_of_savegames = count_savegames();
if (!load)
number_of_savegames++;
number_of_savegames -= 6;
if (number_of_savegames < 0)
number_of_savegames = 0;
number_of_savegames++;
_numSaveGameRows = number_of_savegames;
_saveLoadRowCurPos = 1;
if (!load)
_saveLoadRowCurPos = number_of_savegames;
_saveLoadFlag = false;
restart:;
do {
i = o_unk_132_helper(&b, buf);
} while (!b);
if (i == 205)
goto get_out;
if (!load) {
// if_1
if_1:;
unk132_result = i;
set_hitarea_bit_0x40(0xd0 + i);
leaveHitAreaById(0xd0 + i);
// some code here
fcs = _fcsPtrArray3[5];
fcs->textRow = unk132_result;
if (_language == 20) { //Hebrew
// init x offset with a 2 character savegame number + a period (18 pix)
fcs->textColumn = 3;
fcs->textColumnOffset = 6;
fcs->textLength = 3;
} else {
// init x offset with a 2 character savegame number + a period (18 pix)
fcs->textColumn = 2;
fcs->textColumnOffset = 2;
fcs->textLength = 3;
}
name = buf + i * 18;
// now process entire savegame name to get correct x offset for cursor
name_len = 0;
while (name[name_len]) {
if (_language == 20) { //Hebrew
byte width = 6;
if (name[name_len] >= 64 && name[name_len] < 91)
width = _hebrew_char_widths [name[name_len] - 64];
fcs->textLength++;
fcs->textColumnOffset -= width;
if (fcs->textColumnOffset < width) {
fcs->textColumnOffset += 8;
fcs->textColumn++;
}
} else {
fcs->textLength++;
fcs->textColumnOffset += 6;
if (name[name_len] == 'i' || name[name_len] == 'l')
fcs->textColumnOffset -= 2;
if (fcs->textColumnOffset >= 8) {
fcs->textColumnOffset -= 8;
fcs->textColumn++;
}
}
name_len++;
}
// while_1_end
// do_3_start
for (;;) {
video_putchar(fcs, 0x7f);
_saveLoadFlag = true;
// do_2
do {
i = o_unk_132_helper(&b, buf);
if (b) {
if (i == 205)
goto get_out;
clear_hitarea_bit_0x40(0xd0 + unk132_result);
if (_saveLoadFlag) {
o_clear_character(_fcsPtrArray3[5], 8);
// move code
}
goto if_1;
}
// is_not_b
if (!_saveLoadFlag) {
clear_hitarea_bit_0x40(0xd0 + unk132_result);
goto restart;
}
} while (i >= 0x80 || i == 0);
// after_do_2
o_clear_character(_fcsPtrArray3[5], 8);
if (i == 10 || i == 13)
break;
if (i == 8) {
// do_backspace
if (name_len != 0) {
int x;
byte m;
name_len--;
m = name[name_len];
if (_language == 20) //Hebrew
x = 8;
else
x = (name[name_len] == 'i' || name[name_len] == 'l') ? 1 : 8;
name[name_len] = 0;
o_clear_character(_fcsPtrArray3[5], x, m);
}
} else if (i >= 32 && name_len != 17) {
name[name_len++] = i;
video_putchar(_fcsPtrArray3[5], i);
}
}
// do_save
if (!save_game(_saveLoadRowCurPos + unk132_result, buf + unk132_result * 18))
o_file_error(_fcsPtrArray3[5], true);
} else {
if (!load_game(_saveLoadRowCurPos + i))
o_file_error(_fcsPtrArray3[5], false);
}
get_out:;
o_unk_132_helper_3();
_base_time = time(NULL) - save_time + _base_time;
_copyPartialMode = 0;
dx_copy_rgn_from_3_to_2(94, 208, 46, 80);
i = _timer4;
do {
delay(10);
} while (i == _timer4);
g_system->setFeatureState(OSystem::kFeatureVirtualKeyboard, false);
}
void SimonEngine::o_file_error(FillOrCopyStruct *fcs, bool save_error) {
HitArea *ha;
const char *string, *string2;
if (save_error) {
string = "\r Save failed.";
string2 = "\r Disk error.";
} else {
string = "\r Load failed.";
string2 = "\r File not found.";
}
video_putchar(fcs, 0xC);
for (; *string; string++)
video_putchar(fcs, *string);
for (; *string2; string2++)
video_putchar(fcs, *string2);
fcs->textColumn = (fcs->width >> 1) - 3;
fcs->textRow = fcs->height - 1;
fcs->textLength = 0;
string = "[ OK ]";
for (; *string; string++)
video_putchar(fcs, *string);
ha = findEmptyHitArea();
ha->x = ((fcs->width >> 1) + (fcs->x - 3)) << 3;
ha->y = (fcs->height << 3) + fcs->y - 8;
ha->width = 48;
ha->height = 8;
ha->flags = 0x20;
ha->id = 0x7FFF;
ha->layer = 0x3EF;
loop:;
_lastHitArea = _lastHitArea3 = 0;
do {
delay(1);
} while (_lastHitArea3 == 0);
ha = _lastHitArea;
if (ha == NULL || ha->id != 0x7FFF)
goto loop;
// Return
delete_hitarea(0x7FFF);
}
bool SimonEngine::save_game(uint slot, char *caption) {
Common::OutSaveFile *f;
uint item_index, num_item, i, j;
TimeEvent *te;
_lockWord |= 0x100;
f = _saveFileMan->openForSaving(gen_savename(slot));
if (f == NULL) {
_lockWord &= ~0x100;
return false;
}
f->write(caption, 0x12);
f->writeUint32BE(_itemArrayInited - 1);
f->writeUint32BE(0xFFFFFFFF);
f->writeUint32BE(0);
f->writeUint32BE(0);
i = 0;
for (te = _firstTimeStruct; te; te = te->next)
i++;
f->writeUint32BE(i);
for (te = _firstTimeStruct; te; te = te->next) {
f->writeUint32BE(te->time + _base_time);
f->writeUint16BE(te->subroutine_id);
}
item_index = 1;
for (num_item = _itemArrayInited - 1; num_item; num_item--) {
Item *item = _itemArrayPtr[item_index++];
f->writeUint16BE(item->parent);
f->writeUint16BE(item->sibling);
f->writeUint16BE(item->state);
f->writeUint16BE(item->classFlags);
Child1 *child1 = (Child1 *)findChildOfType(item, 1);
if (child1) {
f->writeUint16BE(child1->fr2);
}
Child2 *child2 = (Child2 *)findChildOfType(item, 2);
if (child2) {
f->writeUint32BE(child2->avail_props);
i = child2->avail_props & 1;
for (j = 1; j < 16; j++) {
if ((1 << j) & child2->avail_props) {
f->writeUint16BE(child2->array[i++]);
}
}
}
Child9 *child9 = (Child9 *) findChildOfType(item, 9);
if (child9) {
for (i = 0; i != 4; i++) {
f->writeUint16BE(child9->array[i]);
}
}
}
// write the 255 variables
for (i = 0; i != 255; i++) {
f->writeUint16BE(readVariable(i));
}
// write the items in array 6
for (i = 0; i != 10; i++) {
f->writeUint16BE(itemPtrToID(_itemArray6[i]));
}
// Write the bits in array 1 & 2
for (i = 0; i != 32; i++)
f->writeUint16BE(_bitArray[i]);
delete f;
_lockWord &= ~0x100;
return true;
}
char *SimonEngine::gen_savename(int slot) {
static char buf[15];
if (_game & GF_SIMON2) {
sprintf(buf, "simon2.%.3d", slot);
} else {
sprintf(buf, "simon1.%.3d", slot);
}
return buf;
}
bool SimonEngine::load_game(uint slot) {
char ident[18];
Common::InSaveFile *f;
uint num, item_index, i, j;
_lockWord |= 0x100;
f = _saveFileMan->openForLoading(gen_savename(slot));
if (f == NULL) {
_lockWord &= ~0x100;
return false;
}
f->read(ident, 18);
num = f->readUint32BE();
if (f->readUint32BE() != 0xFFFFFFFF || num != _itemArrayInited - 1) {
delete f;
_lockWord &= ~0x100;
return false;
}
f->readUint32BE();
f->readUint32BE();
_noParentNotify = true;
// add all timers
killAllTimers();
for (num = f->readUint32BE(); num; num--) {
uint32 timeout = f->readUint32BE();
uint16 func_to_call = f->readUint16BE();
addTimeEvent(timeout, func_to_call);
}
item_index = 1;
for (num = _itemArrayInited - 1; num; num--) {
Item *item = _itemArrayPtr[item_index++], *parent_item;
uint parent = f->readUint16BE();
uint sibling = f->readUint16BE();
parent_item = derefItem(parent);
setItemParent(item, parent_item);
if (parent_item == NULL) {
item->parent = parent;
item->sibling = sibling;
}
item->state = f->readUint16BE();
item->classFlags = f->readUint16BE();
Child1 *child1 = (Child1 *)findChildOfType(item, 1);
if (child1 != NULL) {
child1->fr2 = f->readUint16BE();
}
Child2 *child2 = (Child2 *)findChildOfType(item, 2);
if (child2 != NULL) {
child2->avail_props = f->readUint32BE();
i = child2->avail_props & 1;
for (j = 1; j < 16; j++) {
if ((1 << j) & child2->avail_props) {
child2->array[i++] = f->readUint16BE();
}
}
}
Child9 *child9 = (Child9 *) findChildOfType(item, 9);
if (child9) {
for (i = 0; i != 4; i++) {
child9->array[i] = f->readUint16BE();
}
}
}
// read the 255 variables
for (i = 0; i != 255; i++) {
writeVariable(i, f->readUint16BE());
}
// write the items in array 6
for (i = 0; i != 10; i++) {
_itemArray6[i] = derefItem(f->readUint16BE());
}
// Write the bits in array 1 & 2
for (i = 0; i != 32; i++)
_bitArray[i] = f->readUint16BE();
if (f->ioFailed()) {
error("load failed");
}
delete f;
_noParentNotify = false;
_lockWord &= ~0x100;
return true;
}
} // End of namespace Simon
|