-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
1934 lines (1888 loc) · 58.7 KB
/
game.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
#if 0
gcc ${CFLAGS:--s -O2} -c -Wno-unused-result -Wno-multichar -fwrapv game.c `sdl-config --cflags`
exit
#endif
/*
This program is part of Free Hero Mesh and is public domain.
*/
#include "SDL.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "sqlite3.h"
#include "smallxrm.h"
#include "heromesh.h"
#include "quarks.h"
#include "cursorshapes.h"
#include "names.h"
MoveItem*replay_list;
size_t replay_size;
Uint16 replay_count,replay_pos,replay_mark;
Uint8 solution_replay=255;
char*best_list;
Sint32 best_score=NO_SCORE;
static volatile Uint8 timerflag;
static int exam_scroll;
static MoveItem*inputs;
static size_t inputs_size;
static int inputs_count;
static Uint8 side_mode=255;
static Uint8 should_record_solution;
static Uint8 should_record_private_solution;
static Uint8 replay_speed;
static Uint8 replay_time;
static Uint8 solved;
static Uint8 inserting,saved_inserting;
static sqlite3_stmt*autowin;
static size_t dum_size; // not used by Free Hero Mesh, but needed by some C library functions.
typedef struct {
char*data;
size_t size;
Uint16 mark,pos;
} SaveState;
static SaveState savestates[8];
static Uint8 has_savestates;
int encode_move(FILE*fp,MoveItem v) {
// Encodes a single move and writes the encoded move to the file.
// Returns the number of bytes of the encoded move.
if(v>=8 && v<256) {
fputc(v,fp);
return 1;
} else if(v>=0x8000 && v<=0x8FFF) {
fputc(KEY_XY,fp);
fputc(((v>>6)&63)+1,fp);
fputc((v&63)+1,fp);
return 3;
} else {
fatal("Unencodable move (%u)\n",(int)v);
}
}
int encode_move_list(FILE*fp) {
// Encodes the current replay list into the file; returns the number of bytes.
// Does not write a null terminator.
int i;
int c=0;
for(i=0;i<replay_count;i++) c+=encode_move(fp,replay_list[i]);
return c;
}
MoveItem decode_move(FILE*fp) {
// Decodes a single move from the file, and returns the move.
// Returns zero if there is no more moves.
int v=fgetc(fp);
if(v>=8 && v<256) {
return v;
} else if(v==KEY_XY) {
v=0x8000|((fgetc(fp)-1)<<6);
return v|(fgetc(fp)-1);
} else if(v==EOF || !v) {
return 0;
} else {
fatal("Undecodable move (%u)\n",v);
}
}
int decode_move_list(FILE*fp) {
// Decodes a move list from the file, and stores it in replay_list and replay_count.
// Returns the number of moves (replay_count).
MoveItem v;
FILE*o;
free(replay_list);
replay_list=0;
replay_size=0;
replay_count=0;
o=open_memstream((char**)&replay_list,&replay_size);
if(!o) fatal("Allocation failed\n");
while(replay_count<0xFFFE && (v=decode_move(fp))) {
fwrite(&v,1,sizeof(MoveItem),o);
replay_count++;
}
fclose(o);
if(replay_count && !replay_list) fatal("Allocation failed\n");
return replay_count;
}
#define MSIZ (sizeof(MoveItem))
#define memcpyM(a_,b_,c_) memcpy(a_,b_,(c_)*MSIZ);
#define memmoveM(a_,b_,c_) memmove(a_,b_,(c_)*MSIZ);
static void record_solution(void);
static void setup_game(void) {
const char*v;
optionquery[1]=Q_showInventory;
v=xrm_get_resource(resourcedb,optionquery,optionquery,2)?:"";
side_mode=boolxrm(v,1);
optionquery[1]=Q_replaySpeed;
v=xrm_get_resource(resourcedb,optionquery,optionquery,2)?:"";
replay_speed=strtol(v,0,10)?:16;
optionquery[1]=Q_saveSolutions;
optionquery[2]=Q_private;
v=xrm_get_resource(resourcedb,optionquery,optionquery,3)?:"";
should_record_private_solution=boolxrm(v,0);
if(main_options['r']) {
should_record_solution=0;
} else {
v=xrm_get_resource(resourcedb,optionquery,optionquery,2)?:"";
should_record_solution=boolxrm(v,0);
}
solution_replay=0;
optionquery[1]=Q_autoWin;
v=xrm_get_resource(resourcedb,optionquery,optionquery,2);
if(v && *v) sqlite3_prepare_v3(userdb,v,-1,SQLITE_PREPARE_PERSISTENT,&autowin,0);
}
static void redraw_game(void) {
char buf[32];
SDL_Rect r;
int x,y;
r.x=r.y=0;
r.h=screen->h;
r.w=left_margin;
SDL_FillRect(screen,&r,0xF0);
r.x=left_margin-1;
r.w=1;
SDL_FillRect(screen,&r,0xF7);
r.x=left_margin;
r.w=screen->w-r.x;
SDL_FillRect(screen,&r,back_color);
for(x=1;x<=pfwidth;x++) for(y=1;y<=pfheight;y++) draw_cell(x,y);
x=y=0;
SDL_GetMouseState(&x,&y);
SDL_LockSurface(screen);
if(left_margin>=88) {
snprintf(buf,32,"%5d/%5d",level_ord,level_nindex);
draw_text(0,0,buf,0xF0,solution_replay?0xFE:solved?0xFA:0xFC);
snprintf(buf,32,"%5d",level_id);
draw_text(0,8,"ID",0xF0,0xF7);
draw_text(48,8,buf,0xF0,0xFF);
snprintf(buf,32,"%5d",level_version);
draw_text(0,16,"VER",0xF0,0xF7);
draw_text(48,16,buf,0xF0,0xFF);
snprintf(buf,32,"%5d",level_code);
draw_text(0,24,"CODE",0xF0,0xF7);
draw_text(48,24,buf,0xF0,0xFF);
} else {
snprintf(buf,32,"%5d",level_ord);
draw_text(16,0,buf,0xF0,solution_replay?0xFE:solved?0xFA:0xFC);
snprintf(buf,32,"%5d",level_id);
draw_text(0,8,"I",0xF0,0xF7);
draw_text(16,8,buf,0xF0,0xFF);
snprintf(buf,32,"%5d",level_version);
draw_text(0,16,"V",0xF0,0xF7);
draw_text(16,16,buf,0xF0,0xFF);
snprintf(buf,32,"%5d",level_code);
draw_text(0,24,"C",0xF0,0xF7);
draw_text(16,24,buf,0xF0,0xFF);
}
if(!gameover) {
snprintf(buf,8,"%2dx%2d",pfwidth,pfheight);
draw_text(8,32,buf,0xF0,0xFD);
draw_text(24,32,"x",0xF0,0xF5);
} else if(gameover<0) {
draw_text(4,32,"*LOSE*",0xF4,0xFC);
} else {
draw_text(4,32,"*WIN*",0xF2,0xFA);
}
x=x>=left_margin?(x-left_margin)/picture_size+1:0;
y=y/picture_size+1;
if(x>0 && y>0 && x<=pfwidth && y<=pfheight) snprintf(buf,8,"(%2d,%2d)",x,y);
else strcpy(buf," ");
draw_text(0,40,buf,0xF0,0xF2);
if(side_mode) {
// Inventory
x=20-(left_margin-picture_size)/8;
if(x>19) x=19;
if(x<0) x=0;
for(y=0;y<ninventory;y++) {
if(y*picture_size+60>=screen->h) break;
snprintf(buf,22,"%20d",inventory[y].value);
draw_text(picture_size,y*picture_size+52,buf+x,0xF8,0xFE);
}
SDL_UnlockSurface(screen);
r.x=0; r.y=52; r.w=picture_size; r.h=screen->h-52;
SDL_FillRect(screen,&r,inv_back_color);
for(y=0;y<ninventory;y++) {
if(y*picture_size+60>=screen->h) break;
if(classes[inventory[y].class]->nimages<inventory[y].image) continue;
draw_picture(0,y*picture_size+52,classes[inventory[y].class]->images[inventory[y].image]&0x7FFF);
}
} else {
// Move list
snprintf(buf,8,"%5d",replay_pos);
draw_text(8,52,buf,0xF0,0xF9);
snprintf(buf,8,"%5d",replay_count);
draw_text(8,screen->h-8,buf,0xF0,solution_replay?0xFA:0xFC);
for(y=44,x=replay_pos-(screen->h-68)/32;;x++) {
y+=16;
if(y+24>screen->h) break;
if(x>=0 && x<replay_count) {
if(replay_list[x]<256) {
draw_key(16,y,replay_list[x],0xF8,0xFB);
} else if((replay_list[x]&0xF000)==0x8000) {
sprintf(buf,"%02u",((replay_list[x]>>6)&63)+1);
draw_text(16,y,buf,0xF8,0x47);
sprintf(buf,"%02u",(replay_list[x]&63)+1);
draw_text(16,y+8,buf,0xF8,0x45);
}
}
if(x==replay_count) draw_key(16,y,1,0xF0,0xF8);
if(x==replay_pos) draw_text(0,y,inserting?"I~":"~~",0xF0,0xFE);
if(x==replay_mark) draw_text(32,y,"~~",0xF0,0xFD);
}
SDL_UnlockSurface(screen);
}
if(quiz_text) draw_popup(quiz_text);
SDL_Flip(screen);
set_cursor(XC_arrow);
}
static void continue_animation(void) {
Uint32 n=firstobj;
Object*o;
Animation*a;
DeadAnimation*d;
int i;
for(i=0;i<8;i++) if(anim_slot[i].length && ++anim_slot[i].vtime==anim_slot[i].speed && (anim_slot[i].vtime=0,++anim_slot[i].frame==anim_slot[i].length)) anim_slot[i].frame=0;
while(n!=VOIDLINK) {
o=objects[n];
if((a=o->anim) && (a->status&ANISTAT_VISUAL)) {
i=a->vstep;
if(a->step[i].flag&ANI_SYNC) {
i=anim_slot[a->step[i].slot].frame+a->step[i].start;
if(i!=a->vimage) {
a->vimage=i;
draw_cell(o->x,o->y);
}
} else if(++a->vtime>=a->step[i].speed) {
a->vtime=0;
if(a->vimage==a->step[i].end) {
if(a->step[i].flag&ANI_ONCE) {
if(a->vstep==a->lstep) {
a->status&=~ANISTAT_VISUAL;
} else {
if(++a->vstep==max_animation) a->vstep=0;
a->vimage=a->step[a->vstep].start;
}
} else if(a->step[i].flag&ANI_OSC) {
a->step[i].end=a->step[i].start;
a->step[i].start=a->vimage;
goto advance;
} else {
a->vimage=a->step[i].start;
}
} else {
advance:
if(a->step[i].end>=a->step[i].start) ++a->vimage; else --a->vimage;
}
draw_cell(o->x,o->y);
}
}
n=o->next;
}
if(ndeadanim) {
for(i=0;i<ndeadanim;i++) {
d=deadanim+i;
draw_cell(d->x,d->y);
if(!d->s.flag) continue;
if(d->delay) {
--d->delay;
continue;
}
if(d->vimage<classes[d->class]->nimages)
draw_picture((d->x-1)*picture_size+left_margin,(d->y-1)*picture_size,classes[d->class]->images[d->vimage]&0x7FFF);
if(++d->vtime>=d->s.speed) {
if(d->vimage==d->s.end) d->s.flag=0;
if(d->s.end>=d->s.start) ++d->vimage; else --d->vimage;
d->vtime=0;
}
}
for(i=0;i<ndeadanim;i++) while(i<ndeadanim && !deadanim[i].s.flag) {
draw_cell(deadanim[i].x,deadanim[i].y);
if(i<ndeadanim-1) deadanim[i]=deadanim[ndeadanim-1];
--ndeadanim;
}
}
SDL_Flip(screen);
}
static void show_mouse_xy(SDL_Event*ev) {
char buf[32];
int x,y;
x=(ev->motion.x-left_margin)/picture_size+1;
y=ev->motion.y/picture_size+1;
if(ev->motion.x<left_margin) {
if(ev->button.y<48) {
strcpy(buf," ");
} else if(side_mode) {
// Inventory
x=(ev->button.y-52)/picture_size;
if(x<0 || x>=ninventory) strcpy(buf," "); else snprintf(buf,8,"%7d",inventory[x].value);
} else {
// Move list
x=replay_pos+(ev->button.y+4)/16-(screen->h-68)/32-4;
if(x<0 || x>replay_count) strcpy(buf," "); else snprintf(buf,8,"%c%6d",x<replay_pos?0xAE:x>replay_pos?0xAF:0xFE,x);
}
} else {
if(x>0 && y>0 && x<=pfwidth && y<=pfheight) snprintf(buf,8,"(%2d,%2d)",x,y);
else strcpy(buf," ");
}
SDL_LockSurface(screen);
draw_text(0,40,buf,0xF0,0xF2);
SDL_UnlockSurface(screen);
SDL_Flip(screen);
}
static void save_replay(void) {
int i;
unsigned char*buf=0;
size_t sz=0;
FILE*fp;
if(solution_replay || !replay_list || !replay_count) return;
if(gameover==1) solved=1;
fp=open_memstream((char**)&buf,&sz);
if(!fp) fatal("Allocation failed\n");
fputc(0x00,fp);
fputc(0x01,fp);
encode_move_list(fp);
fputc(0x00,fp);
if(solved) {
fputc(0x41,fp);
fputc(level_version,fp);
fputc(level_version>>8,fp);
if(best_list) {
fputc(0x02,fp);
fwrite(best_list,1,strlen(best_list)+1,fp);
fputc(0x81,fp);
fputc(best_score,fp);
fputc(best_score>>8,fp);
fputc(best_score>>16,fp);
fputc(best_score>>24,fp);
}
}
if(replay_mark) {
fputc(0x42,fp);
fputc(replay_mark,fp);
fputc(replay_mark>>8,fp);
}
if(has_savestates) for(i=0;i<8;i++) if(savestates[i].data) {
fputc(i+0x30,fp);
fputs(savestates[i].data,fp);
fputc(0,fp);
if(savestates[i].mark || savestates[i].pos) {
fputc(i+0xB0,fp);
fputc(savestates[i].mark,fp);
fputc(savestates[i].mark>>8,fp);
fputc(savestates[i].pos,fp);
fputc(savestates[i].pos>>8,fp);
}
}
fclose(fp);
if(!buf) fatal("Allocation failed\n");
write_userstate(FIL_LEVEL,level_id,sz,buf);
free(buf);
}
static void load_replay(void) {
FILE*fp=0;
unsigned char*buf=0;
long sz;
int i,j;
free(replay_list);
replay_list=0;
replay_count=replay_mark=replay_size=0;
free(best_list);
best_list=0;
if(has_savestates) {
for(i=0;i<8;i++) {
free(savestates[i].data);
savestates[i].data=0;
savestates[i].size=0;
savestates[i].mark=savestates[i].pos=0;
}
has_savestates=0;
}
if(solution_replay) {
gameover_score=NO_SCORE;
if(buf=read_lump(FIL_SOLUTION,level_id,&sz)) {
fp=fmemopen(buf,sz,"r");
if(!fp) fatal("Allocation failed\n");
// Solution format: version (16-bits), flag (8-bits), score (32-bits), user name (null-terminated), timestamp (64-bits), move list
if(sz>3) {
i=fgetc(fp); i|=fgetc(fp)<<8;
if(i==level_version) {
j=fgetc(fp);
if(j&128) {
gameover_score=fgetc(fp);
gameover_score|=fgetc(fp)<<8;
gameover_score|=fgetc(fp)<<16;
gameover_score|=fgetc(fp)<<24;
}
if(j&1) while(fgetc(fp)>0);
if(j&2) for(i=0;i<8;i++) fgetc(fp);
decode_move_list(fp);
}
}
}
} else if(buf=read_userstate(FIL_LEVEL,level_id,&sz)) {
fp=fmemopen(buf,sz,"r");
if(!fp) fatal("Allocation failed\n");
best_score=NO_SCORE;
if(sz>2 && *buf) {
// Old format
replay_count=(buf[sz-2]<<8)|buf[sz-1];
if(sz-replay_count>=4) replay_mark=(buf[replay_count]<<8)|buf[replay_count+1]; else replay_mark=0;
if(sz-replay_count>=6) {
i=(buf[replay_count+2]<<8)|buf[replay_count+3];
if(i==level_version) solved=1;
}
replay_list=malloc(replay_size=sizeof(MoveItem)*(replay_count+1));
if(!replay_list) fatal("Allocation failed\n");
for(i=0;i<replay_count;i++) replay_list[i]=buf[i];
} else {
// New format
fgetc(fp); // skip first null byte
while((i=fgetc(fp))!=EOF) switch(i) {
case 0x01: // Replay list
if(replay_list) goto skip;
decode_move_list(fp);
break;
case 0x02: // Best list
if(best_list) goto skip;
dum_size=0;
getdelim(&best_list,&dum_size,0,fp);
break;
case 0x30 ... 0x37: // Save states
if(savestates[i&7].data) goto skip;
getdelim(&savestates[i&7].data,&savestates[i&7].size,0,fp);
has_savestates=1;
break;
case 0x41: // Solved version
i=fgetc(fp); i|=fgetc(fp)<<8;
if(i==level_version) solved=1;
break;
case 0x42: // Mark
replay_mark=fgetc(fp);
replay_mark|=fgetc(fp)<<8;
break;
case 0x81: // Best score
best_score=fgetc(fp);
best_score|=fgetc(fp)<<8;
best_score|=fgetc(fp)<<16;
best_score|=fgetc(fp)<<24;
break;
case 0xB0 ... 0xB7: // Save states
savestates[i&7].mark=fgetc(fp);
savestates[i&7].mark|=fgetc(fp)<<8;
savestates[i&7].pos=fgetc(fp);
savestates[i&7].pos|=fgetc(fp)<<8;
break;
default: skip:
if(i<0x40) {
while(fgetc(fp)>0);
} else if(i<0x80) {
fgetc(fp); fgetc(fp);
} else if(i<0xC0) {
for(i=0;i<4;i++) fgetc(fp);
} else {
for(i=0;i<8;i++) fgetc(fp);
}
}
if(best_list && !solved) {
free(best_list);
best_list=0;
best_score=NO_SCORE;
}
}
}
if(fp) fclose(fp);
free(buf);
}
static int exchange_state(int slot,int how) {
// Return value is replay position of save state (-1 if error)
// slot = 0 to 7
// how = 'l' (load), 's' (save), 'x' (exchange)
SaveState*ss=savestates+slot;
SaveState v=*ss;
FILE*fp;
if(how!='s' && !v.data) {
screen_message("Nonexisting save state");
return -1;
}
if(how!='l') {
if(ss->data) {
if(how=='s') free(ss->data);
ss->data=0;
ss->size=0;
}
if(replay_count) {
has_savestates=1;
fp=open_memstream(&ss->data,&ss->size);
if(!fp) fatal("Allocation failed\n");
encode_move_list(fp);
fputc(0,fp);
fclose(fp);
}
ss->mark=replay_mark;
ss->pos=replay_pos;
}
if(how!='s') {
fp=fmemopen(v.data,v.size,"r");
if(!fp) fatal("Allocation failed\n");
decode_move_list(fp);
fclose(fp);
if(how=='x') free(v.data);
replay_mark=v.mark;
}
return v.pos;
}
static void begin_level(int id) {
const char*t;
replay_time=0;
if(replay_count) save_replay();
inputs_count=0;
replay_pos=0;
solved=0;
inserting=0;
t=load_level(id)?:init_level();
load_replay();
if(t) {
gameover=-1;
screen_message(t);
} else {
gameover=0;
}
timerflag=0;
}
static inline void exam_value(const char*t,int y,Value v) {
char buf[256];
int i;
y=(y-exam_scroll)*8;
if(y<0 || y>screen->h-8) return;
draw_text(0,y,t,0xF0,0xF7);
switch(v.t) {
case TY_NUMBER:
snprintf(buf,255,"%12lu 0x%08lX %ld",(long)v.u,(long)v.u,(long)v.s);
draw_text(200,y,buf,0xF0,0xFE);
break;
case TY_CLASS:
draw_text(200,y,"$",0xF0,0xFB);
draw_text(208,y,classes[v.u]->name,0xF0,0xFB);
break;
case TY_MESSAGE:
snprintf(buf,255,"%s%s",v.u<256?"":"#",v.u<256?standard_message_names[v.u]:messages[v.u-256]);
draw_text(200,y,buf,0xF0,0xFD);
break;
case TY_LEVELSTRING: case TY_STRING:
draw_text(200,y,"<String>",0xF0,0xF9);
break;
case TY_SOUND: case TY_USOUND:
draw_text(200,y,"<Sound>",0xF0,0xF6);
break;
case TY_MARK:
draw_text(200,y,"<Mark>",0xF0,0xF3);
break;
case TY_ARRAY:
draw_text(200,y,"<Array>",0xF0,0xF9);
snprintf(buf,255,"0x%08lX",(long)v.u);
draw_text(264,y,buf,0xF0,0xFE);
break;
default:
snprintf(buf,80,"<%lu:%lu>",(long)v.u,(long)v.t);
draw_text(200,y,buf,0xF0,0xFA);
i=strlen(buf)*8+208;
if(v.u<nobjects && objects[v.u] && objects[v.u]->generation==v.t) {
snprintf(buf,80,"@ (%d,%d)",objects[v.u]->x,objects[v.u]->y);
draw_text(i,y,buf,0xF0,0xF2);
} else {
draw_text(i,y,"(dead)",0xF0,0xF4);
}
break;
}
}
static inline void exam_flags(int y,Uint16 v) {
y=(y-exam_scroll)*8;
if(y<0 || y>screen->h-8) return;
draw_text(0,y,"Flags:",0xF0,0xF7);
draw_text(200,y,"--- --- --- --- --- --- --- --- --- --- --- --- --- ---",0xF0,0xF8);
if(v&OF_INVISIBLE) draw_text(200,y,"Inv",0xF0,0xFF);
if(v&OF_VISUALONLY) draw_text(232,y,"Vis",0xF0,0xFF);
if(v&OF_STEALTHY) draw_text(264,y,"Stl",0xF0,0xFF);
if(v&OF_BUSY) draw_text(296,y,"Bus",0xF0,0xFF);
if(v&OF_USERSTATE) draw_text(328,y,"Ust",0xF0,0xFF);
if(v&OF_USERSIGNAL) draw_text(360,y,"Usg",0xF0,0xFF);
if(v&OF_MOVED) draw_text(392,y,"Mov",0xF0,0xFF);
if(v&OF_DONE) draw_text(424,y,"Don",0xF0,0xFF);
if(v&OF_KEYCLEARED) draw_text(456,y,"Key",0xF0,0xFF);
if(v&OF_DESTROYED) draw_text(488,y,"Des",0xF0,0xFF);
if(v&OF_BIZARRO) draw_text(520,y,"Biz",0xF0,0xFF);
if(v&OF_CONNECTION) draw_text(552,y,"Con",0xF0,0xFF);
if(v&OF_MOVING) draw_text(584,y,"Mvi",0xF0,0xFF);
if(v&OF_ORDERED) draw_text(616,y,"Ord",0xF0,0xFF);
}
static inline void exam_hardsharp(const char*t,int y,Uint16*v) {
int i;
char buf[16];
y=(y-exam_scroll)*8;
if(y<0 || y>screen->h-8) return;
draw_text(0,y,t,0xF0,0xF7);
for(i=0;i<4;i++) {
snprintf(buf,8,"%5u",v[i]);
draw_text(200+i*56,y,buf,0xF0,v[i]?0xFF:0xF8);
}
}
static void draw_back_line(int y,int c) {
unsigned char*p=screen->pixels;
int i;
p+=screen->pitch*y;
for(i=0;i<screen->w;i++) if(p[i]==0xF0 || p[i]==0xF1) p[i]=i&1?c:0xF0;
}
static void examine(Uint32 n) {
sqlite3_stmt*st;
SDL_Event ev;
SDL_Rect r;
Object*o;
int i,y;
y=0;
i=sqlite3_prepare_v2(userdb,"SELECT '%'||`NAME`,`ID`&0xFFFF FROM `VARIABLES` WHERE `ID` BETWEEN ?1 AND (?1|0xFFFF) ORDER BY `ID`",-1,&st,0);
if(i) fatal("SQL error (%d): %s",i,sqlite3_errmsg(userdb));
object:
if(n==VOIDLINK) return;
o=objects[n];
if(!o) return;
sqlite3_bind_int(st,1,o->class<<16);
exam_scroll=0;
redraw:
set_cursor(XC_arrow);
r.x=r.y=0;
r.w=screen->w;
r.h=screen->h;
SDL_FillRect(screen,&r,0xF0);
SDL_LockSurface(screen);
exam_value("Self:",0,OVALUE(n));
exam_value("Class:",1,CVALUE(o->class));
exam_value("Image:",2,NVALUE(o->image));
if(classes[o->class]->cflags&CF_QUIZ) goto quiz;
exam_value("Dir:",3,NVALUE(o->dir));
exam_value("Misc1:",4,o->misc1);
exam_value("Misc2:",5,o->misc2);
exam_value("Misc3:",6,o->misc3);
exam_value("Misc4:",7,o->misc4);
exam_value("Misc5:",8,o->misc5);
exam_value("Misc6:",9,o->misc6);
exam_value("Misc7:",10,o->misc7);
exam_value("Temperature:",11,NVALUE(o->temperature));
exam_flags(12,o->oflags);
exam_value("Density:",13,NVALUE(o->density));
exam_value("Volume:",14,NVALUE(o->volume));
exam_value("Strength:",15,NVALUE(o->strength));
exam_value("Weight:",16,NVALUE(o->weight));
exam_value("Climb:",17,NVALUE(o->climb));
exam_value("Height:",18,NVALUE(o->height));
exam_value("Arrivals:",19,NVALUE(o->arrivals));
exam_value("Departures:",20,NVALUE(o->departures));
exam_value("Shape:",21,NVALUE(o->shape));
exam_value("Shovable:",22,NVALUE(o->shovable));
exam_value("Distance:",23,NVALUE(o->distance));
exam_value("Inertia:",24,NVALUE(o->inertia));
exam_hardsharp("Hardness:",25,o->hard);
exam_hardsharp("Sharpness:",26,o->sharp);
exam_value("NextR:",27,o->replacement);
while(sqlite3_step(st)==SQLITE_ROW) {
i=sqlite3_column_int(st,1);
exam_value(sqlite3_column_text(st,0),i+29,o->uservars[i]);
}
quiz:
sqlite3_reset(st);
SDL_UnlockSurface(screen);
SDL_Flip(screen);
while(SDL_WaitEvent(&ev)) switch(ev.type) {
case SDL_KEYDOWN:
switch(ev.key.keysym.sym) {
case SDLK_ESCAPE: case SDLK_RETURN: case SDLK_KP_ENTER: sqlite3_finalize(st); return;
case SDLK_UP: if(exam_scroll) exam_scroll--; break;
case SDLK_DOWN: exam_scroll++; break;
case SDLK_HOME: exam_scroll=0; break;
case SDLK_PAGEUP: exam_scroll-=screen->h/8; if(exam_scroll<0) exam_scroll=0; break;
case SDLK_PAGEDOWN: exam_scroll+=screen->h/8; break;
case SDLK_F1: case SDLK_g: if(classes[o->class]->gamehelp) modal_draw_popup(classes[o->class]->gamehelp); break;
case SDLK_F2: case SDLK_h: if(classes[o->class]->edithelp) modal_draw_popup(classes[o->class]->edithelp); break;
case SDLK_1: case SDLK_4: if(o->misc1.t==TY_LEVELSTRING) modal_draw_popup(value_string_ptr(o->misc1)); break;
case SDLK_2: case SDLK_5: if(o->misc2.t==TY_LEVELSTRING) modal_draw_popup(value_string_ptr(o->misc2)); break;
case SDLK_3: case SDLK_6: if(o->misc3.t==TY_LEVELSTRING) modal_draw_popup(value_string_ptr(o->misc3)); break;
}
goto redraw;
case SDL_MOUSEMOTION:
if(ev.motion.y!=y && ev.motion.y<screen->h) {
SDL_LockSurface(screen);
draw_back_line(y,0xF0);
draw_back_line(y=ev.motion.y,0xF1);
SDL_UnlockSurface(screen);
SDL_Flip(screen);
}
break;
case SDL_VIDEOEXPOSE:
goto redraw;
case SDL_QUIT:
exit(0);
break;
}
}
static void global_examine(void) {
sqlite3_stmt*st;
SDL_Event ev;
SDL_Rect r;
int i,y;
y=0;
i=sqlite3_prepare_v2(userdb,"SELECT '@'||`NAME`,`ID` FROM `VARIABLES` WHERE `ID` BETWEEN 0x0000 AND 0xFFFF ORDER BY `ID`",-1,&st,0);
if(i) fatal("SQL error (%d): %s",i,sqlite3_errmsg(userdb));
exam_scroll=0;
redraw:
set_cursor(XC_arrow);
r.x=r.y=0;
r.w=screen->w;
r.h=screen->h;
SDL_FillRect(screen,&r,0xF0);
SDL_LockSurface(screen);
exam_value("MoveNumber:",0,NVALUE(move_number));
exam_value("LevelStrings:",1,NVALUE(nlevelstrings));
exam_value("Generation:",2,NVALUE(generation_number));
while(sqlite3_step(st)==SQLITE_ROW) {
i=sqlite3_column_int(st,1);
exam_value(sqlite3_column_text(st,0),i+4,globals[i]);
}
sqlite3_reset(st);
SDL_UnlockSurface(screen);
SDL_Flip(screen);
while(SDL_WaitEvent(&ev)) switch(ev.type) {
case SDL_KEYDOWN:
switch(ev.key.keysym.sym) {
case SDLK_ESCAPE: case SDLK_RETURN: case SDLK_KP_ENTER: sqlite3_finalize(st); return;
case SDLK_UP: if(exam_scroll) exam_scroll--; break;
case SDLK_DOWN: exam_scroll++; break;
case SDLK_HOME: exam_scroll=0; break;
case SDLK_PAGEUP: exam_scroll-=screen->h/8; if(exam_scroll<0) exam_scroll=0; break;
case SDLK_PAGEDOWN: exam_scroll+=screen->h/8; break;
}
goto redraw;
case SDL_MOUSEMOTION:
if(ev.motion.y!=y && ev.motion.y<screen->h) {
SDL_LockSurface(screen);
draw_back_line(y,0xF0);
draw_back_line(y=ev.motion.y,0xF1);
SDL_UnlockSurface(screen);
SDL_Flip(screen);
}
break;
case SDL_VIDEOEXPOSE:
goto redraw;
case SDL_QUIT:
exit(0);
break;
}
}
static void list_objects_at(int xy,Uint32*pf,const char*s) {
static const char*const dirs[8]={"E ","NE","N ","NW","W ","SW","S ","SE"};
SDL_Event ev;
SDL_Rect r;
char buf[256];
int scroll=0;
int count=0;
Uint32 n,t;
Object*o;
int i,j;
if(xy<0 || xy>=64*64) return;
n=pf[xy];
if(n==VOIDLINK) return;
while(n!=VOIDLINK) t=n,count++,n=objects[n]->up;
redraw:
r.x=r.y=0;
r.w=screen->w;
r.h=screen->h;
SDL_FillRect(screen,&r,0xF1);
r.y=8;
r.h-=8;
scrollbar(&scroll,r.h/8,count,0,&r);
snprintf(buf,255," %d %sobjects at (%d,%d): ",count,s,(xy&63)+1,(xy/64)+1);
SDL_LockSurface(screen);
draw_text(0,0,buf,0xF7,0xF0);
n=t;
for(i=0;i<scroll && n!=VOIDLINK;i++) n=objects[n]->down;
for(i=0;i<screen->h/8 && n!=VOIDLINK;i++) {
o=objects[n];
snprintf(buf,255," %8d: %-14.14s %3d %s",n,classes[o->class]->name,o->image,classes[o->class]->cflags&CF_QUIZ?"":dirs[o->dir&7]);
draw_text(24,r.y,buf,0xF1,o->generation?(classes[o->class]->cflags&CF_PLAYER?0xFE:0xFF):0xF8);
n=o->down;
r.y+=8;
}
SDL_UnlockSurface(screen);
SDL_Flip(screen);
while(SDL_WaitEvent(&ev)) {
if(ev.type!=SDL_VIDEOEXPOSE) {
r.h=screen->h-8;
r.x=0;
r.y=8;
if(scrollbar(&scroll,r.h/8,count,&ev,&r)) goto redraw;
}
switch(ev.type) {
case SDL_MOUSEBUTTONDOWN:
if(ev.button.button!=1 || ev.button.y<8) break;
j=ev.button.y/8-scroll-1;
if(j>=count) break;
n=t;
for(i=0;i<j;i++) n=objects[n]->down;
examine(n);
set_cursor(XC_draft_small);
goto redraw;
case SDL_MOUSEMOTION:
set_cursor(XC_draft_small);
break;
case SDL_KEYDOWN:
switch(ev.key.keysym.sym) {
case SDLK_ESCAPE: case SDLK_RETURN: case SDLK_KP_ENTER: return;
}
goto redraw;
case SDL_VIDEOEXPOSE:
goto redraw;
case SDL_QUIT:
exit(0);
break;
}
}
}
static void describe_at(int xy) {
unsigned char*s;
Uint32 n;
if(xy<0 || xy>=64*64) return;
n=playfield[xy];
if(n==VOIDLINK) return;
while(n!=VOIDLINK && objects[n]->up!=VOIDLINK) n=objects[n]->up;
while(n!=VOIDLINK && !classes[objects[n]->class]->gamehelp) n=objects[n]->down;
if(n==VOIDLINK) return;
if(classes[objects[n]->class]->gamehelp[0]==16 && !classes[objects[n]->class]->gamehelp[1]) {
if(objects[n]->misc1.t!=TY_LEVELSTRING || objects[n]->misc1.u>=nlevelstrings) return;
modal_draw_popup(levelstrings[objects[n]->misc1.u]);
return;
}
s=sqlite3_mprintf("\x0C\x0E%s:%d\\ %s\x0B\x0F%s",classes[objects[n]->class]->name,objects[n]->image,classes[objects[n]->class]->name,classes[objects[n]->class]->gamehelp);
if(!s) fatal("Allocation failed\n");
modal_draw_popup(s);
sqlite3_free(s);
}
static void describe_inventory(int n) {
unsigned char*s;
if(n<0 || n>=ninventory) return;
if(!classes[inventory[n].class]->gamehelp) return;
if(classes[inventory[n].class]->gamehelp[0]==16) return;
s=sqlite3_mprintf("\x0C\x0E%s:%d\\ %s\x0B\x0F%s",classes[inventory[n].class]->name,inventory[n].image,classes[inventory[n].class]->name,classes[inventory[n].class]->gamehelp);
if(!s) fatal("Allocation failed\n");
modal_draw_popup(s);
sqlite3_free(s);
}
static void do_import_moves(const char*arg) {
FILE*fp;
int i;
if(!arg || !arg[strspn(arg," \t")]) return;
fp=popen(arg,"r");
if(!fp) {
screen_message("Unable to open pipe for reading");
return;
}
replay_mark=0;
decode_move_list(fp);
pclose(fp);
}
static void do_export_moves(const char*arg) {
FILE*fp;
int i;
if(!arg || !arg[strspn(arg," \t")]) return;
fp=popen(arg,"w");
if(!fp) {
screen_message("Unable to open pipe for writing");
return;
}
encode_move_list(fp);
pclose(fp);
}
static void do_load_moves(sqlite3_stmt*st) {
FILE*fp;
int i=sqlite3_column_bytes(st,1);
fp=fmemopen((char*)sqlite3_column_blob(st,1)?:"",i,"r");
if(!fp) fatal("Allocation failed\n");
decode_move_list(fp);
fclose(fp);
}
static int copy_text_to_plain(unsigned char*out,int maxlen,const unsigned char*in) {
int at=0;
if(!in) {
*out=0;
return 0;
}
while(*in && at<maxlen) switch(*in) {
case 10: if(at && out[at-1]!=32) out[at++]=32; in++; break;
case 14: case 30: in=strchrnul(in,'\\'); if(*in) in++; break;
case 31: in++; if(*in) out[at++]=*in++; break;
case 32 ... 255: out[at++]=*in++; break;
default: in++;
}
out[at]=0;
return at;
}
static inline void levels_column(int x,int y,int n,int bg,sqlite3_stmt*st,char*buf) {
const DisplayColumn*dc=ll_disp+n;
Uint8 co=dc->color;
int w=dc->width;
int nc=dc->data+(dc->flag&4?0:8);
int t=sqlite3_column_type(st,nc);
int a=0;
int i;
const char*p;
sqlite3_int64 v;
if(t==SQLITE_NULL) return;
if(dc->flag&1) w=255;
if(t==SQLITE_BLOB || t==SQLITE_TEXT) {
blob:
if(p=sqlite3_column_text(st,nc)) i=snprintf(buf,w,"%s",p); else *buf=i=0;
if(dc->form[0]=='R' && i<w) a=w-i;
if(dc->flag&2) co=0xFF;
} else {
// This implementation does not check that the format is necessarily valid.
// You should not rely on the use of any undocumented format.
v=sqlite3_column_int64(st,nc);
if(dc->flag&2) {
co=0xFF;
for(i=0;i<dc->color;i++) if(ll_code[dc->ptr+i]>=0xFF00 || v+128<=(ll_code[dc->ptr+i]>>8)) {
co=ll_code[dc->ptr+i]&0xFF;
break;
}
}
switch(dc->form[0]) {
case 'L': case 'R':
if(dc->form[1]) {
if(v<0 || v>w) v=w;
memset(buf,dc->form[1],v);
buf[v]=0;
if(dc->form[0]=='R') a=w-v;
} else {
goto blob;
}
break;
case 'd':
snprintf(buf,w+1,"%*lld",w,(long long)(dc->form[1] && v<0?-v:v));
if(dc->form[1]=='-') *buf=v<0?'-':' ';
if(dc->form[1]=='+') *buf=v<0?'-':'+';
break;
case 'o':