forked from TheGLander/tworld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tworld.c
2169 lines (1973 loc) · 55.4 KB
/
tworld.c
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
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* tworld.c: The top-level module.
*
* Copyright (C) 2001-2017 by Brian Raiter, Madhav Shanbhag, and Eric Schmidt,
* under the GNU General Public License. No warranty. See COPYING for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "defs.h"
#include "err.h"
#include "series.h"
#include "res.h"
#include "play.h"
#include "score.h"
#include "settings.h"
#include "solution.h"
#include "unslist.h"
#include "help.h"
#include "oshw.h"
#include "cmdline.h"
#include "ver.h"
/* Bell-ringing macro.
*/
#define bell() (silence ? (void)0 : ding())
enum { Play_None, Play_Normal, Play_Back, Play_Verify };
/* The data needed to identify what level is being played.
*/
typedef struct gamespec {
gameseries series; /* the complete set of levels */
int currentgame; /* which level is currently selected */
int playmode; /* which mode to play */
int usepasswds; /* FALSE if passwords are to be ignored */
int status; /* final status of last game played */
int enddisplay; /* TRUE if the final level was completed */
int melindacount; /* count for Melinda's free pass */
} gamespec;
/* Structure used to hold data collected by initoptionswithcmdline().
*/
typedef struct startupdata {
char *filename; /* which data file to use */
char *savefilename; /* an alternate solution file */
int levelnum; /* a selected initial level */
int listdirs; /* TRUE if directories should be listed */
int listseries; /* TRUE if the files should be listed */
int listscores; /* TRUE if the scores should be listed */
int listtimes; /* TRUE if the times should be listed */
int batchverify; /* TRUE to enter batch verification */
int lynxmode; /* TRUE to use Lynx for the initial level */
} startupdata;
/* History of levelsets in order of last used date/time.
*/
static history *historylist = NULL;
static int historycount = 0;
static int noLevelMove = 0;
/* Structure used to hold the complete list of available series.
*/
typedef struct seriesdata {
gameseries *list; /* the array of available series */
int count; /* size of array */
mapfileinfo *mflist; /* List of all levelset files */
int mfcount; /* Number of levelset files */
tablespec table; /* table for displaying the array */
} seriesdata;
/* TRUE suppresses sound and the console bell.
*/
static int silence = FALSE;
/* TRUE means the program should attempt to run in fullscreen mode.
*/
static int fullscreen = FALSE;
/* FALSE suppresses all password checking.
*/
static int usepasswds = TRUE;
/* TRUE if the user requested an idle-time histogram.
*/
static int showhistogram = FALSE;
/* Slowdown factor, used for debugging.
*/
static int mudsucking = 1;
/* Frame-skipping disable flag.
*/
static int noframeskip = FALSE;
/* The sound buffer scaling factor.
*/
static int soundbufsize = -1;
/* The initial volume level.
*/
static int volumelevel = -1;
/* The top of the stack of subtitles.
*/
static void **subtitlestack = NULL;
/*
* Text-mode output functions.
*/
/* Find a position to break a string inbetween words. The integer at
* breakpos receives the length of the string prefix less than or
* equal to len. The string pointer *str is advanced to the first
* non-whitespace after the break. The original string pointer is
* returned.
*/
static char *findstrbreak(char const **str, int maxlen, int *breakpos)
{
char const *start;
int n;
retry:
start = *str;
n = strlen(start);
if (n <= maxlen) {
*str += n;
*breakpos = n;
} else {
n = maxlen;
if (isspace(start[n])) {
*str += n;
while (isspace(**str))
++*str;
while (n > 0 && isspace(start[n - 1]))
--n;
if (n == 0)
goto retry;
*breakpos = n;
} else {
while (n > 0 && !isspace(start[n - 1]))
--n;
if (n == 0) {
*str += maxlen;
*breakpos = maxlen;
} else {
*str = start + n;
while (n > 0 && isspace(start[n - 1]))
--n;
if (n == 0)
goto retry;
*breakpos = n;
}
}
}
return (char*)start;
}
/* Render a table to the given file. This function encapsulates both
* the process of determining the necessary widths for each column of
* the table, and then sequentially rendering the table's contents to
* a stream. On the first pass through the data, single-cell
* non-word-wrapped entries are measured and each column sized to fit.
* If the resulting table is too large for the given area, then the
* collapsible column is reduced as necessary. If there is still
* space, however, then the entries that span multiple cells are
* measured in a second pass, and columns are grown to fit them as
* well where needed. If there is still space after this, the column
* containing word-wrapped entries may be expanded as well.
*/
void printtable(FILE *out, tablespec const *table)
{
int const maxwidth = 79;
char const *mlstr;
char const *p;
int *colsizes;
int mlindex, mlwidth, mlpos;
int diff, pos;
int i, j, n, i0, c, w, z;
if (!(colsizes = malloc(table->cols * sizeof *colsizes)))
return;
for (i = 0 ; i < table->cols ; ++i)
colsizes[i] = 0;
mlindex = -1;
mlwidth = 0;
n = 0;
for (j = 0 ; j < table->rows ; ++j) {
for (i = 0 ; i < table->cols ; ++n) {
c = table->items[n][0] - '0';
if (c == 1) {
w = strlen(table->items[n] + 2);
if (table->items[n][1] == '!') {
if (w > mlwidth || mlindex != i)
mlwidth = w;
mlindex = i;
} else {
if (w > colsizes[i])
colsizes[i] = w;
}
}
i += c;
}
}
w = -table->sep;
for (i = 0 ; i < table->cols ; ++i)
w += colsizes[i] + table->sep;
diff = maxwidth - w;
if (diff < 0 && table->collapse >= 0) {
w = -diff;
if (colsizes[table->collapse] < w)
w = colsizes[table->collapse] - 1;
colsizes[table->collapse] -= w;
diff += w;
}
if (diff > 0) {
n = 0;
for (j = 0 ; j < table->rows && diff > 0 ; ++j) {
for (i = 0 ; i < table->cols ; ++n) {
c = table->items[n][0] - '0';
if (c > 1 && table->items[n][1] != '!') {
w = table->sep + strlen(table->items[n] + 2);
for (i0 = i ; i0 < i + c ; ++i0)
w -= colsizes[i0] + table->sep;
if (w > 0) {
if (table->collapse >= i && table->collapse < i + c)
i0 = table->collapse;
else if (mlindex >= i && mlindex < i + c)
i0 = mlindex;
else
i0 = i + c - 1;
if (w > diff)
w = diff;
colsizes[i0] += w;
diff -= w;
if (diff == 0)
break;
}
}
i += c;
}
}
}
if (diff > 0 && mlindex >= 0 && colsizes[mlindex] < mlwidth) {
mlwidth -= colsizes[mlindex];
w = mlwidth < diff ? mlwidth : diff;
colsizes[mlindex] += w;
diff -= w;
}
n = 0;
for (j = 0 ; j < table->rows ; ++j) {
mlstr = NULL;
mlwidth = mlpos = 0;
pos = 0;
for (i = 0 ; i < table->cols ; ++n) {
if (i)
pos += fprintf(out, "%*s", table->sep, "");
c = table->items[n][0] - '0';
w = -table->sep;
while (c--)
w += colsizes[i++] + table->sep;
if (table->items[n][1] == '-')
fprintf(out, "%-*.*s", w, w, table->items[n] + 2);
else if (table->items[n][1] == '+')
fprintf(out, "%*.*s", w, w, table->items[n] + 2);
else if (table->items[n][1] == '.') {
z = (w - strlen(table->items[n] + 2)) / 2;
if (z < 0)
z = w;
fprintf(out, "%*.*s%*s",
w - z, w - z, table->items[n] + 2, z, "");
} else if (table->items[n][1] == '!') {
mlwidth = w;
mlpos = pos;
mlstr = table->items[n] + 2;
p = findstrbreak(&mlstr, w, &z);
fprintf(out, "%.*s%*s", z, p, w - z, "");
}
pos += w;
}
fputc('\n', out);
while (mlstr && *mlstr) {
p = findstrbreak(&mlstr, mlwidth, &w);
fprintf(out, "%*s%.*s\n", mlpos, "", w, p);
}
}
free(colsizes);
}
/* Display directory settings.
*/
static void printdirectories(void)
{
printf("Resource files read from: %s\n", resdir);
printf("Level sets read from: %s\n", seriesdir);
printf("Configured data files read from: %s\n", seriesdatdir);
printf("Solution files saved in: %s\n", savedir);
}
/*
* Callback functions for oshw.
*/
/* An input callback that only accepts the characters Y and N.
*/
static int yninputcallback(void)
{
switch (input(TRUE)) {
case 'Y': case 'y': return 'Y';
case 'N': case 'n': return 'N';
case CmdWest: return '\b';
case CmdProceed: return '\n';
case CmdQuitLevel: return -1;
case CmdQuit: exit(0);
}
return 0;
}
/* An input callback that accepts only alphabetic characters.
*/
static int keyinputcallback(void)
{
int ch;
ch = input(TRUE);
switch (ch) {
case CmdWest: return '\b';
case CmdProceed: return '\n';
case CmdQuitLevel: return -1;
case CmdQuit: exit(0);
default:
if (isalpha(ch))
return toupper(ch);
}
return 0;
}
/* An input callback used while displaying a scrolling list.
*/
static int scrollinputcallback(int *move)
{
int cmd;
switch ((cmd = input(TRUE))) {
case CmdPrev10: *move = SCROLL_HALFPAGE_UP; break;
case CmdNorth: *move = SCROLL_UP; break;
case CmdPrev: *move = SCROLL_UP; break;
case CmdPrevLevel: *move = SCROLL_UP; break;
case CmdSouth: *move = SCROLL_DN; break;
case CmdNext: *move = SCROLL_DN; break;
case CmdNextLevel: *move = SCROLL_DN; break;
case CmdNext10: *move = SCROLL_HALFPAGE_DN; break;
case CmdProceed: *move = CmdProceed; return FALSE;
case CmdQuitLevel: *move = CmdQuitLevel; return FALSE;
case CmdHelp: *move = CmdHelp; return FALSE;
case CmdQuit: exit(0);
}
return TRUE;
}
/* An input callback used while displaying the scrolling list of scores.
*/
static int scorescrollinputcallback(int *move)
{
int cmd;
switch ((cmd = input(TRUE))) {
case CmdPrev10: *move = SCROLL_HALFPAGE_UP; break;
case CmdNorth: *move = SCROLL_UP; break;
case CmdPrev: *move = SCROLL_UP; break;
case CmdPrevLevel: *move = SCROLL_UP; break;
case CmdSouth: *move = SCROLL_DN; break;
case CmdNext: *move = SCROLL_DN; break;
case CmdNextLevel: *move = SCROLL_DN; break;
case CmdNext10: *move = SCROLL_HALFPAGE_DN; break;
case CmdProceed: *move = CmdProceed; return FALSE;
case CmdSeeSolutionFiles: *move = CmdSeeSolutionFiles; return FALSE;
case CmdQuitLevel: *move = CmdQuitLevel; return FALSE;
case CmdHelp: *move = CmdHelp; return FALSE;
case CmdQuit: exit(0);
}
return TRUE;
}
/* An input callback used while displaying the scrolling list of solutions.
*/
static int solutionscrollinputcallback(int *move)
{
int cmd;
switch ((cmd = input(TRUE))) {
case CmdPrev10: *move = SCROLL_HALFPAGE_UP; break;
case CmdNorth: *move = SCROLL_UP; break;
case CmdPrev: *move = SCROLL_UP; break;
case CmdPrevLevel: *move = SCROLL_UP; break;
case CmdSouth: *move = SCROLL_DN; break;
case CmdNext: *move = SCROLL_DN; break;
case CmdNextLevel: *move = SCROLL_DN; break;
case CmdNext10: *move = SCROLL_HALFPAGE_DN; break;
case CmdProceed: *move = CmdProceed; return FALSE;
case CmdSeeScores: *move = CmdSeeScores; return FALSE;
case CmdQuitLevel: *move = CmdQuitLevel; return FALSE;
case CmdHelp: *move = CmdHelp; return FALSE;
case CmdQuit: exit(0);
}
return TRUE;
}
/*
* Basic game activities.
*/
/* Return TRUE if the given level is a final level.
*/
static int islastinseries(gamespec const *gs, int index)
{
return index == gs->series.count - 1
|| gs->series.games[index].number == gs->series.final;
}
/* Return TRUE if the current level has a solution.
*/
static int issolved(gamespec const *gs, int index)
{
return hassolution(gs->series.games + index);
}
/* Mark the current level's solution as replaceable.
*/
static void replaceablesolution(gamespec *gs, int change)
{
if (change < 0)
gs->series.games[gs->currentgame].sgflags ^= SGF_REPLACEABLE;
else if (change > 0)
gs->series.games[gs->currentgame].sgflags |= SGF_REPLACEABLE;
else
gs->series.games[gs->currentgame].sgflags &= ~SGF_REPLACEABLE;
}
/* Mark the current level's password as known to the user.
*/
static void passwordseen(gamespec *gs, int number)
{
if (!(gs->series.games[number].sgflags & SGF_HASPASSWD)) {
gs->series.games[number].sgflags |= SGF_HASPASSWD;
savesolutions(&gs->series);
}
}
/* Change the current level, ensuring that the user is not granted
* access to a forbidden level. FALSE is returned if the specified
* level is not available to the user.
*/
static int setcurrentgame(gamespec *gs, int n)
{
if (n == gs->currentgame)
return TRUE;
if (n < 0 || n >= gs->series.count)
return FALSE;
if (gs->usepasswds)
if (n > 0 && !(gs->series.games[n].sgflags & SGF_HASPASSWD)
&& !issolved(gs, n -1))
return FALSE;
gs->currentgame = n;
gs->melindacount = 0;
return TRUE;
}
/* Change the current level by a delta value. If the user cannot go to
* that level, the "nearest" level in that direction is chosen
* instead. FALSE is returned if the current level remained unchanged.
*/
static int changecurrentgame(gamespec *gs, int offset)
{
int sign, m, n;
if (offset == 0 || noLevelMove)
return FALSE;
m = gs->currentgame;
n = m + offset;
if (n < 0)
n = 0;
else if (n >= gs->series.count)
n = gs->series.count - 1;
if (gs->usepasswds && n > 0) {
sign = offset < 0 ? -1 : +1;
for ( ; n >= 0 && n < gs->series.count ; n += sign) {
if (!n || (gs->series.games[n].sgflags & SGF_HASPASSWD)
|| issolved(gs, n - 1)) {
m = n;
break;
}
}
n = m;
if (n == gs->currentgame && offset != sign) {
n = gs->currentgame + offset - sign;
for ( ; n != gs->currentgame ; n -= sign) {
if (n < 0 || n >= gs->series.count)
continue;
if (!n || (gs->series.games[n].sgflags & SGF_HASPASSWD)
|| issolved(gs, n - 1))
break;
}
}
}
if (n == gs->currentgame)
return FALSE;
gs->currentgame = n;
gs->melindacount = 0;
return TRUE;
}
/* Return TRUE if Melinda is watching Chip's progress on this level --
* i.e., if it is possible to earn a pass to the next level.
*/
static int melindawatching(gamespec const *gs)
{
if (!gs->usepasswds)
return FALSE;
if (islastinseries(gs, gs->currentgame))
return FALSE;
if (gs->series.games[gs->currentgame + 1].sgflags & SGF_HASPASSWD)
return FALSE;
if (issolved(gs, gs->currentgame))
return FALSE;
return TRUE;
}
/*
* The subtitle stack
*/
static void pushsubtitle(char const *subtitle)
{
void **stk;
int n;
if (!subtitle)
subtitle = "";
n = strlen(subtitle) + 1;
stk = NULL;
x_alloc(stk, sizeof(void**) + n);
*stk = subtitlestack;
subtitlestack = stk;
memcpy(stk + 1, subtitle, n);
setsubtitle(subtitle);
}
static void popsubtitle(void)
{
void **stk;
if (subtitlestack) {
stk = *subtitlestack;
free(subtitlestack);
subtitlestack = stk;
}
setsubtitle(subtitlestack ? (char*)(subtitlestack + 1) : NULL);
}
static void changesubtitle(char const *subtitle)
{
int n;
if (!subtitle)
subtitle = "";
n = strlen(subtitle) + 1;
x_alloc(subtitlestack, sizeof(void**) + n);
memcpy(subtitlestack + 1, subtitle, n);
setsubtitle(subtitle);
}
/*
*
*/
static void dohelp(int topic)
{
pushsubtitle("Help");
switch (topic) {
case Help_First:
case Help_FileListKeys:
case Help_ScoreListKeys:
onlinecontexthelp(topic);
break;
default:
onlinemainhelp(topic);
break;
}
popsubtitle();
}
/* Display a scrolling list of the available solution files, and allow
* the user to select one. Return TRUE if the user selected a solution
* file different from the current one. Do nothing if there is only
* one solution file available. (If for some reason the new solution
* file cannot be read, TRUE will still be returned, as the list of
* solved levels will still need to be updated.)
*/
static int showsolutionfiles(gamespec *gs)
{
tablespec table;
char const **filelist;
int readonly = FALSE;
int count, current, f, n;
if (haspathname(gs->series.name) || (gs->series.savefilename
&& haspathname(gs->series.savefilename))) {
bell();
return FALSE;
} else if (!createsolutionfilelist(&gs->series, FALSE, &filelist,
&count, &table)) {
bell();
return FALSE;
}
current = -1;
n = 0;
if (gs->series.savefilename) {
for (n = 0 ; n < count ; ++n)
if (!strcmp(filelist[n], gs->series.savefilename))
break;
if (n == count)
n = 0;
else
current = n;
}
pushsubtitle(gs->series.name);
for (;;) {
f = displaylist("SOLUTION FILES", &table, &n,
LIST_SOLUTIONFILES, solutionscrollinputcallback);
if (f == CmdProceed) {
readonly = FALSE;
break;
} else if (f == CmdSeeScores) {
readonly = TRUE;
break;
} else if (f == CmdQuitLevel) {
n = -1;
break;
} else if (f == CmdHelp) {
dohelp(Help_FileListKeys);
}
}
popsubtitle();
f = n >= 0 && n != current;
if (f) {
clearsolutions(&gs->series);
if (!gs->series.savefilename)
gs->series.savefilename = getpathbuffer();
sprintf(gs->series.savefilename, "%.*s", getpathbufferlen(),
filelist[n]);
if (readsolutions(&gs->series)) {
if (readonly)
gs->series.gsflags |= GSF_NOSAVING;
} else {
bell();
}
n = gs->currentgame;
gs->currentgame = 0;
passwordseen(gs, 0);
changecurrentgame(gs, n);
}
freesolutionfilelist(filelist, &table);
return f;
}
/* Display the scrolling list of the user's current scores, and allow
* the user to select a current level.
*/
static int showscores(gamespec *gs)
{
tablespec table;
int *levellist;
int ret = FALSE;
int count, f, n;
restart:
if (!createscorelist(&gs->series, gs->usepasswds, CHAR_MZERO,
&levellist, &count, &table)) {
bell();
return ret;
}
for (n = 0 ; n < count ; ++n)
if (levellist[n] == gs->currentgame)
break;
pushsubtitle(gs->series.name);
for (;;) {
f = displaylist(gs->series.filebase, &table, &n,
LIST_SCORES, scorescrollinputcallback);
if (f == CmdProceed) {
n = levellist[n];
break;
} else if (f == CmdSeeSolutionFiles) {
if (!(gs->series.gsflags & GSF_NODEFAULTSAVE)) {
n = levellist[n];
break;
}
} else if (f == CmdQuitLevel) {
n = -1;
break;
} else if (f == CmdHelp) {
dohelp(Help_ScoreListKeys);
}
}
popsubtitle();
freescorelist(levellist, &table);
if (f == CmdSeeSolutionFiles) {
setcurrentgame(gs, n);
ret = showsolutionfiles(gs);
goto restart;
}
if (n < 0)
return ret;
return setcurrentgame(gs, n) || ret;
}
/* Obtain a password from the user and move to the requested level.
*/
static int selectlevelbypassword(gamespec *gs)
{
char passwd[5] = "";
int n;
setkeyboardinputmode(TRUE);
n = displayinputprompt("Enter Password", passwd, 4,
INPUT_ALPHA, keyinputcallback);
setkeyboardinputmode(FALSE);
if (!n)
return FALSE;
n = findlevelinseries(&gs->series, 0, passwd);
if (n < 0) {
bell();
return FALSE;
}
passwordseen(gs, n);
return setcurrentgame(gs, n);
}
/*
* The levelset history functions.
*/
/* Load the levelset history.
*/
static int loadhistory(void)
{
fileinfo file;
char buf[256];
int n;
char *hdate, *htime, *hpasswd, *hnumber, *hname;
int hyear, hmon, hmday, hhour, hmin, hsec;
history *h;
historycount = 0;
free(historylist);
clearfileinfo(&file);
if (!openfileindir(&file, savedir, "history", "r", NULL))
return FALSE;
for (;;) {
n = sizeof buf - 1;
if (!filegetline(&file, buf, &n, NULL))
break;
if (buf[0] == '#')
continue;
hdate = strtok(buf , " \t");
htime = strtok(NULL, " \t");
hpasswd = strtok(NULL, " \t");
hnumber = strtok(NULL, " \t");
hname = strtok(NULL, "\r\n");
if ( ! (hdate && htime && hpasswd && hnumber && hname &&
sscanf(hdate, "%d-%d-%d", &hyear, &hmon, &hmday) == 3 &&
sscanf(htime, "%d:%d:%d", &hhour, &hmin, &hsec) == 3 &&
*hpasswd && *hnumber && *hname) )
continue;
++historycount;
x_alloc(historylist, historycount * sizeof *historylist);
h = historylist + historycount - 1;
sprintf(h->name, "%.*s", (int)(sizeof h->name - 1), hname);
sprintf(h->passwd, "%.*s", (int)(sizeof h->passwd - 1), hpasswd);
h->levelnumber = (int)strtol(hnumber, NULL, 0);
h->dt.tm_year = hyear - 1900;
h->dt.tm_mon = hmon - 1;
h->dt.tm_mday = hmday;
h->dt.tm_hour = hhour;
h->dt.tm_min = hmin;
h->dt.tm_sec = hsec;
h->dt.tm_isdst = -1;
}
fileclose(&file, NULL);
return TRUE;
}
/* Update the levelset history for the set and level being played.
*/
static void updatehistory(char const *name, char const *passwd, int number)
{
time_t t = time(NULL);
int i, j;
history *h;
h = historylist;
for (i = 0; i < historycount; ++i, ++h) {
if (stricmp(h->name, name) == 0)
break;
}
if (i == historycount) {
++historycount;
x_alloc(historylist, historycount * sizeof *historylist);
}
for (j = i; j > 0; --j) {
historylist[j] = historylist[j-1];
}
h = historylist;
sprintf(h->name, "%.*s", (int)(sizeof h->name - 1), name);
sprintf(h->passwd, "%.*s", (int)(sizeof h->passwd - 1), passwd);
h->levelnumber = number;
h->dt = *localtime(&t);
}
/* Save the levelset history.
*/
static void savehistory(void)
{
fileinfo file;
history *h;
int i;
clearfileinfo(&file);
if (!openfileindir(&file, savedir, "history", "w", NULL))
return;
h = historylist;
for (i = 0; i < historycount; ++i, ++h) {
fprintf(file.fp, "%04d-%02d-%02d %02d:%02d:%02d\t%s\t%d\t%s\n",
1900 + h->dt.tm_year, 1 + h->dt.tm_mon, h->dt.tm_mday,
h->dt.tm_hour, h->dt.tm_min, h->dt.tm_sec,
h->passwd, h->levelnumber, h->name);
}
fileclose(&file, NULL);
}
/*
* The game-playing functions.
*/
#define leveldelta(n) if (!changecurrentgame(gs, (n))) { bell(); continue; }
/* Get a key command from the user at the start of the current level.
*/
static int startinput(gamespec *gs)
{
static int lastlevel = -1;
char yn[2];
int cmd, n;
if (gs->currentgame != lastlevel) {
lastlevel = gs->currentgame;
setstepping(0, FALSE);
}
drawscreen(TRUE);
gs->playmode = Play_None;
for (;;) {
cmd = input(TRUE);
if (cmd >= CmdMoveFirst && cmd <= CmdMoveLast) {
gs->playmode = Play_Normal;
return cmd;
}
switch (cmd) {
case CmdProceed: gs->playmode = Play_Normal; return cmd;
case CmdQuitLevel: return cmd;
case CmdPrev10: leveldelta(-10); return CmdNone;
case CmdPrev: leveldelta(-1); return CmdNone;
case CmdPrevLevel: leveldelta(-1); return CmdNone;
case CmdNextLevel: leveldelta(+1); return CmdNone;
case CmdNext: leveldelta(+1); return CmdNone;
case CmdNext10: leveldelta(+10); return CmdNone;
case CmdStepping: changestepping(4, TRUE); break;
case CmdSubStepping: changestepping(1, TRUE); break;
case CmdRandomFF: advanceinitrandomff(TRUE); break;
case CmdVolumeUp: changevolume(+2, TRUE); break;
case CmdVolumeDown: changevolume(-2, TRUE); break;
case CmdHelp: dohelp(Help_KeysBetweenGames); break;
case CmdQuit: exit(0);
case CmdPlayback:
case CmdAdvanceGame:
case CmdAdvanceMoveGame:
if (prepareplayback()) {
gs->playmode = Play_Back;
return cmd;
}
bell();
break;
case CmdSeek:
if (getreplaysecondstoskip() > 0) {
gs->playmode = Play_Back;
return CmdProceed;
}
break;
case CmdCheckSolution:
if (prepareplayback()) {
gs->playmode = Play_Verify;
return CmdProceed;
}
bell();
break;
case CmdReplSolution:
if (issolved(gs, gs->currentgame))
replaceablesolution(gs, -1);
else
bell();
break;
case CmdKillSolution:
if (!issolved(gs, gs->currentgame)) {
bell();
break;
}
yn[0] = '\0';
setkeyboardinputmode(TRUE);
n = displayinputprompt("Really delete solution?",
yn, 1, INPUT_YESNO, yninputcallback);
setkeyboardinputmode(FALSE);
if (n && *yn == 'Y')
if (deletesolution())
savesolutions(&gs->series);
break;
case CmdSeeScores:
if (showscores(gs))
return CmdNone;
break;
case CmdSeeSolutionFiles:
if (showsolutionfiles(gs))
return CmdNone;
break;
case CmdTimesClipboard:
copytoclipboard(leveltimes(&gs->series));
break;
case CmdGotoLevel:
if (selectlevelbypassword(gs))
return CmdNone;
break;
case CmdKeys:
n = 0;
displaylist("", keyboardhelp(KEYHELP_TWPLUSPLUS), &n, LIST_HELP, NULL);
return CmdNone;
default:
continue;
}
drawscreen(TRUE);
}
}
/* Get a key command from the user at the completion of the current
* level.
*/
static int endinput(gamespec *gs)
{
char yn[2];
int bscore = 0, tscore = 0;
long gscore = 0;
int n;
int cmd = CmdNone;
if (gs->status < 0) {
if (melindawatching(gs) && secondsplayed() >= 10) {
++gs->melindacount;
if (gs->melindacount >= 10) {
yn[0] = '\0';
setkeyboardinputmode(TRUE);
n = displayinputprompt("Skip level?", yn, 1,
INPUT_YESNO, yninputcallback);
setkeyboardinputmode(FALSE);