-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditbrd.c
More file actions
3815 lines (3675 loc) · 136 KB
/
editbrd.c
File metadata and controls
3815 lines (3675 loc) · 136 KB
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 -c -Wno-unused-result -std=gnu99 editbrd.c `sdl-config --cflags`
exit
#endif
#define USING_RW_DATA
#include "common.h"
#include <math.h>
static Uint16 brd_id;
static Uint16 xcur,ycur,xcur2,ycur2;
static Uint8 status_on=255;
static Tile clip,overclip;
static Tile*clipq;
static Tile*overclipq;
static Uint8*apparent_clipq;
static Uint8 nclipq,autocirc;
static Uint8 apparent_clip;
static Uint16 numprefix;
static Uint8 emode,vmode;
static Uint8*markgrid;
static Uint16 markwidth,markheight,markskip;
static Uint8*markgrid2;
static Uint16 markwidth2,markheight2,markskip2;
static Uint8 zcur,zvmode;
static Uint32 zvis;
static Tile*areabuf;
static Uint16 area_width,area_height;
static Sint32 area_xoffset,area_yoffset;
static Uint8 arealayer; // bit0=under bit1=main bit2=over
static StatXY*find_stat(Uint16 x,Uint16 y,Uint8 n,Uint8 lay,Uint8 nlay);
static void stat_list_callback(Uint16 n,int y,void*uz);
static void stat_xy_edit(Stat*s,Uint16 n);
static Uint8 parameter_edit(Uint16 addr,Uint8 par,Uint8 sta,StatXY*sxy);
void set_board_name(Uint16 id,const char*name) {
if(maxboard<id) {
boardnames=realloc(boardnames,(id+1)*sizeof(Uint8*));
if(!boardnames) err(1,"Allocation failed");
while(maxboard<id) boardnames[++maxboard]=0;
} else if(!boardnames) {
boardnames=calloc(maxboard+1,sizeof(Uint8*));
if(!boardnames) err(1,"Allocation failed");
}
free(boardnames[id]);
boardnames[id]=strdup(name);
if(!boardnames[id]) err(1,"Allocation failed");
}
static Uint8 set_mark(Uint16 x,Uint16 y,Uint8 mask) {
Uint8*g0;
Uint16 w0,h0,s0;
Uint32 i;
if(x>=board_info.width || y>=board_info.height) return 0;
if(!markgrid || x>=markwidth || y>=markheight) {
if(g0=markgrid) {
w0=markwidth;
h0=markheight;
s0=markskip;
}
markwidth=board_info.width;
markheight=board_info.height;
markskip=(markwidth+7)>>3;
markgrid=calloc(markskip,markheight);
if(!markgrid) err(1,"Allocation failed");
if(g0) {
for(i=0;i<h0;i++) memcpy(markgrid+i*markskip,g0+i*s0,s0);
free(g0);
}
}
g0=markgrid+(x>>3)+y*markskip;
if(*g0&(1<<(x&7))) {
if(!(mask&2)) *g0&=~(1<<(x&7));
return 1;
} else {
if(mask&1) *g0|=1<<(x&7);
return 0;
}
}
static void exchange_mark_grid(void) {
Uint8*g;
Uint16 w,h,s;
g=markgrid2; w=markwidth2; h=markheight2; s=markskip2;
markgrid2=markgrid; markwidth2=markwidth; markheight2=markheight; markskip2=markskip;
markgrid=g; markwidth=w; markheight=h; markskip=s;
}
static void goto_board(Uint16 id) {
FILE*fp=open_lump_by_number(id,"BRD","r");
const char*e;
char b=0;
int i;
Uint16 m;
if(!fp) {
if(maxboard<id) {
alert_text("Board not found");
return;
}
b=1;
if(!config.editor_auto || !memory[0x241]) fp=open_lump_by_number(config.template_board,"BRD","r");
}
if(fp) {
if(e=load_board(fp)) alert_text(e);
fclose(fp);
if(b) {
memset(b_under,0,board_info.width*board_info.height*sizeof(Tile));
memset(b_main,0,board_info.width*board_info.height*sizeof(Tile));
memset(b_over,0,board_info.width*board_info.height*sizeof(Tile));
for(i=0;i<maxstat;i++) {
free(stats[i].text);
free(stats[i].xy);
stats[i].text=0;
stats[i].xy=0;
stats[i].length=0;
stats[i].count=0;
}
maxstat=1;
}
} else if(b && config.editor_auto && (m=memory[0x241])) {
if(m>0xFFF0 || memory[m]!=2 || (memory[m+3]&~127)) {
alert_text("The ED1 65 command is used improperly");
goto blank;
}
free(b_under);
if(!board_info.width || !(memory[m+4]&1)) board_info.width=memory[m+1]?:80;
if(!board_info.height || !(memory[m+4]&1)) board_info.height=memory[m+2]?:25;
b_under=calloc(board_info.height*board_info.width,3*sizeof(Tile));
if(!b_under) err(1,"Allocation failed");
b_main=b_under+board_info.height*board_info.width;
b_over=b_main+board_info.height*board_info.width;
board_info.exits[0]=board_info.exits[1]=0;
board_info.exits[2]=board_info.exits[3]=0;
for(i=0;i<16;i++) {
free(ozone[i]); ozone[i]=0;
free(uzone[i]); uzone[i]=0;
}
if(!stats || maxstat!=memory[m+3]) {
for(i=0;i<maxstat;i++) {
free(stats[i].text);
free(stats[i].xy);
stats[i].text=0;
stats[i].length=0;
stats[i].xy=0;
stats[i].frame=0;
stats[i].count=0;
}
stats=realloc(stats,(memory[m+3]?:1)*sizeof(Stat));
if(!stats) err(1,"Allocation failed");
while(maxstat<memory[m+3]) memset(stats+maxstat++,0,sizeof(Stat));
maxstat=memory[m+3]?:1;
}
parameter_edit(m+5,0,0,0);
} else {
blank:
free(b_under);
if(!board_info.width) board_info.width=60;
if(!board_info.height) board_info.height=25;
b_under=calloc(board_info.height*board_info.width,3*sizeof(Tile));
if(!b_under) err(1,"Allocation failed");
b_main=b_under+board_info.height*board_info.width;
b_over=b_main+board_info.height*board_info.width;
board_info.screen=0;
board_info.exits[0]=board_info.exits[1]=0;
board_info.exits[2]=board_info.exits[3]=0;
board_info.userdata=board_info.flag=0;
for(i=0;i<16;i++) {
free(ozone[i]); ozone[i]=0;
free(uzone[i]); uzone[i]=0;
}
for(i=0;i<maxstat;i++) {
free(stats[i].text);
free(stats[i].xy);
}
free(stats);
stats=calloc(1,sizeof(Stat));
if(!stats) err(1,"Allocation failed");
maxstat=1;
}
if(xcur>=board_info.width || ycur>=board_info.height) xcur=ycur=0;
brd_id=id;
work_varproperties(&board_info.varprop);
}
static void edit_board_info(void) {
int i;
Uint16 n;
char nam[61]="";
if(brd_id<=maxboard && boardnames && boardnames[brd_id]) strncpy(nam,boardnames[brd_id],60);
nam[60]=0;
win_form("Board info") {
win_help("binfo",0);
win_text('m',"Board name: ",nam);
win_picture(1) {
char buf[40];
draw_text(1,0,buf,7,snprintf(buf,40,"Dimensions: %dx%d",board_info.width,board_info.height));
}
win_numeric('U',(config.editor_custom_labels && memory[0x224])?(char*)gtext[memory[0x224]]:"User data: ",board_info.userdata,0,65535);
win_numeric('c',"Screen: ",board_info.screen,0,65535);
win_boolean('0',(config.editor_custom_labels && memory[0x220])?(char*)gtext[memory[0x220]]:"User flag 0",board_info.flag,BF_USER0);
win_boolean('1',(config.editor_custom_labels && memory[0x221])?(char*)gtext[memory[0x221]]:"User flag 1",board_info.flag,BF_USER1);
win_boolean('2',(config.editor_custom_labels && memory[0x222])?(char*)gtext[memory[0x222]]:"User flag 2",board_info.flag,BF_USER2);
win_boolean('3',(config.editor_custom_labels && memory[0x223])?(char*)gtext[memory[0x223]]:"User flag 3",board_info.flag,BF_USER3);
win_boolean('P',"Persist",board_info.flag,BF_PERSIST);
win_boolean('g',"Suppress global scripts",board_info.flag,BF_NO_GLOBAL);
win_boolean('V',"Visible overlay",board_info.flag,BF_OVERLAY);
win_boolean('o',"Always allow saving on sensors",board_info.flag,BF_SAVE_ON_SENSOR);
win_boolean('y',"Always allow saving not on sensors",board_info.flag,BF_SAVE_NOT_SENSOR);
win_boolean('A',"Alternate mode for board colors",board_info.flag,BF_MAIN_ALT_MODE);
win_boolean('r',"Alternate mode for overlay colors",board_info.flag,BF_OVER_ALT_MODE);
if(config.editor_custom_labels && memory[0x225] && maxstat) win_numeric('5',gtext[memory[0x225]],stats->misc1,0,65535);
if(config.editor_custom_labels && memory[0x226] && maxstat) win_numeric('6',gtext[memory[0x226]],stats->misc2,0,65535);
if(config.editor_custom_labels && memory[0x227] && maxstat) win_numeric('7',gtext[memory[0x227]],stats->misc3,0,65535);
win_blank();
win_numeric('E',"East exit: ",board_info.exits[DIR_E],0,65535) win_refresh();
win_numeric('N',"North exit: ",board_info.exits[DIR_N],0,65535) win_refresh();
win_numeric('W',"West exit: ",board_info.exits[DIR_W],0,65535) win_refresh();
win_numeric('S',"South exit: ",board_info.exits[DIR_S],0,65535) win_refresh();
win_blank();
win_picture(4) {
for(i=0;i<4;i++) {
n=board_info.exits[i];
v_char[i*80]="\x1A\x18\x1B\x19"[i];
v_color[i*80]=(n?14:8);
if(n && boardnames && n<=maxboard && boardnames[n]) draw_text(2,i,boardnames[n],7,60);
}
}
win_blank();
win_command_esc(0,"Done") break;
}
if(*nam) set_board_name(brd_id,nam);
}
static void fix_zones(void) {
UnordZone*u;
OrdZone*o;
Uint32 a,y;
int z;
for(z=0;z<16;z++) {
if(u=uzone[z]) {
for(a=((u->maxy+1)*(Uint32)board_info.width)/8+1;a && !u->data[a-1];a--);
if(a) y=(8ULL*a+board_info.width-1)/board_info.width-1; else y=0;
if(y<u->maxy) u->maxy=y;
if(!u->flag && !a && !u->data[0] && !u->extra) free(uzone[z]),uzone[z]=0;
}
if(o=ozone[z]) {
if(!o->flag && !o->ncells && !o->extra) free(ozone[z]),ozone[z]=0;
}
}
}
static void edit_zone_cells(void) {
char buf[80];
OrdZone*o=ozone[zcur-16];
Uint32 id=0;
Uint32 s=0;
int i;
show0:
memset(v_font,VF_SYSTEM|VF_FRONT,80*25);
memset(v_color,0x07,80*25);
memset(v_char,0x20,80*25);
show1:
draw_text(0,0,buf,0x2F,snprintf(buf,80," Zone#%d (%d cells) ",zcur,o->ncells));
if(id>o->ncells) id=o->ncells;
s=24*(id/24);
for(i=0;i<24;i++) {
if(i+s<o->ncells) {
draw_text(1,i+1,buf,i+s==id?14:7,snprintf(buf,80,"%c %5d: (%5d,%5d)",i+s==id?16:32,i+s,o->xy[i+s].x,o->xy[i+s].y));
} else {
draw_text(1,i+1,buf,i+s==id?6:8,snprintf(buf,80,"%c : (-----,-----)",i+s==id?16:32));
}
}
redisplay();
key:
do { if(!next_event()) return; } while(event.type!=SDL_KEYDOWN);
switch(event.key.keysym.sym) {
case SDLK_ESCAPE: case SDLK_q: return;
case SDLK_DELETE: case SDLK_d:
if(id<o->ncells) {
memmove(o->xy+id,o->xy+id+1,(o->ncells-id-1)*sizeof(OrdZoneXY));
o->ncells--;
}
goto show1;
case SDLK_INSERT: case SDLK_i:
if(o->ncells==0xFFFF) goto key;
o->ncells++;
o=ozone[zcur-16]=realloc(o,o->ncells*sizeof(OrdZoneXY)+sizeof(OrdZone));
memmove(o->xy+id+1,o->xy+id,(o->ncells-id-1)*sizeof(OrdZoneXY));
o->xy[id].x=o->xy[id].y=0;
// fall through
case SDLK_SPACE: case SDLK_e:
if(id>=o->ncells) goto key;
win_form("Edit zone cell") {
win_numeric('X',"X: ",o->xy[id].x,0,board_info.width-1);
win_numeric('Y',"Y: ",o->xy[id].y,0,board_info.height-1);
win_command('U',"Use cursor position") {
o->xy[id].x=xcur; o->xy[id].y=ycur;
win_refresh();
}
win_blank();
win_command_esc(0,"Done") break;
}
goto show0;
case SDLK_UP: case SDLK_KP8: case SDLK_k: if(id) id--; goto show1;
case SDLK_DOWN: case SDLK_KP2: case SDLK_j: if(id<o->ncells) id++; goto show1;
case SDLK_PAGEUP: case SDLK_KP4: case SDLK_KP9: case SDLK_LEFT: case SDLK_h: id=(id>24?id-24:0); goto show1;
case SDLK_PAGEDOWN: case SDLK_KP3: case SDLK_KP6: case SDLK_RIGHT: case SDLK_l: id=(id>24?id-24:0); goto show1;
case SDLK_HOME: case SDLK_KP7: id=0; goto show1;
case SDLK_END: case SDLK_KP1: id=o->ncells; goto show1;
default: goto key;
}
}
static void edit_zone_info(void) {
char buf[80];
UnordZone*u;
OrdZone*o;
int i;
Uint32 x,y;
win_form("Zone info") {
win_help("zone",zcur&16?"o":"u");
win_numeric(':',"Zone: ",zcur,0,31) win_refresh();
win_blank();
if(zcur&16) {
Uint8 m;
if(!(o=ozone[zcur-16]) && !(o=ozone[zcur-16]=calloc(1,sizeof(OrdZone)))) err(1,"Allocation failed");
win_picture(1) draw_text(2,0,buf,7,snprintf(buf,40,"(%d cells)",o->ncells));
win_boolean('0',"User0",o->flag,ZF_USER0);
win_boolean('1',"User1",o->flag,ZF_USER1);
win_boolean('2',"User2",o->flag,ZF_USER2);
win_boolean('3',"User3",o->flag,ZF_USER3);
win_boolean('R',"Reverse",o->flag,ZF_REVERSE);
win_numeric('x',"Extra value: ",o->extra,0,0xFFFF);
win_blank();
win_heading("Rotation:");
m=o->flag>>14;
win_option('C',"Cyclic",m,0);
win_option('F',"Full",m,1);
win_option('N',"Normal",m,2);
win_option('P',"Pushable",m,3);
o->flag=(o->flag&0x3FFF)+(m<<14);
win_boolean('u',"Affect under layer",o->flag,ZF_AFFECT_UNDER);
win_boolean('a',"Affect main layer",o->flag,ZF_AFFECT_MAIN);
win_boolean('o',"Affect over layer",o->flag,ZF_AFFECT_OVER);
win_blank();
win_command('v',"Remove all cells") {
o->ncells=0;
win_refresh();
}
if(o->ncells) win_command('M',"Move cells...") {
int d=DIR_E;
Uint16 n=1;
win_form("Move zone cells") {
win_numeric('A',"Amount: ",n,0,65535);
win_option('E',"East",d,DIR_E);
win_option('N',"North",d,DIR_N);
win_option('W',"West",d,DIR_W);
win_option('S',"South",d,DIR_S);
win_blank();
win_command('x',"Execute") {
for(i=0;i<o->ncells;i++) {
x=o->xy[i].x+(d==DIR_E?n:d==DIR_W?-n:0); y=o->xy[i].y+(d==DIR_S?n:d==DIR_N?-n:0);
if(x<board_info.width && y<board_info.height) {
o->xy[i].x=x; o->xy[i].y=y;
} else {
o->ncells--;
memmove(o->xy+i,o->xy+i+1,(o->ncells-i)*sizeof(OrdZoneXY));
i--;
}
}
break;
}
win_command_esc(0,"Cancel") break;
}
}
win_command('E',"Edit cells...") {
edit_zone_cells();
win_refresh();
}
} else {
if(!(u=uzone[zcur]) && !(u=uzone[zcur]=calloc(1,board_info.width/8+1+sizeof(UnordZone)))) err(1,"Allocation failed");
win_picture(1) draw_text(2,0,buf,7,snprintf(buf,40,"(Max Y = %d)",u->maxy));
win_boolean('0',"User0",u->flag,ZF_USER0);
win_boolean('1',"User1",u->flag,ZF_USER1);
win_boolean('2',"User2",u->flag,ZF_USER2);
win_boolean('3',"User3",u->flag,ZF_USER3);
win_numeric('x',"Extra value: ",u->extra,0,0xFFFF);
win_blank();
win_command('v',"Remove all cells") {
u->maxy=0;
memset(u->data,0,board_info.width/8+1);
win_refresh();
}
}
win_blank();
win_command_esc(0,"Done") break;
}
fix_zones();
}
static void clear_extra_stats(void) {
int i,j;
for(i=0;i<maxstat;i++) for(j=0;j<stats[i].count;) {
if(stats[i].xy[j].x>=board_info.width || stats[i].xy[j].y>=board_info.height) {
memmove(stats[i].xy+j,stats[i].xy+j+1,(stats[i].count-j-1)*sizeof(StatXY));
--stats[i].count;
} else {
j++;
}
}
}
static Uint8 new_stat(void) {
if(maxstat==255) return 0;
stats=realloc(stats,(maxstat+1)*sizeof(Stat));
if(!stats) err(1,"Allocation failed");
stats[maxstat].misc1=stats[maxstat].misc2=stats[maxstat].misc3=0;
stats[maxstat].speed=1;
stats[maxstat].length=0;
stats[maxstat].count=0;
stats[maxstat].text=0;
stats[maxstat].xy=0;
stats[maxstat].frame=0;
stats[maxstat].mode=0;
return ++maxstat;
}
static void edit_tile(char lay) {
Tile*p;
win_form("Tile") {
win_numeric('X',"X: ",xcur,0,board_info.width-1) win_refresh();
win_numeric('Y',"Y: ",ycur,0,board_info.height-1) win_refresh();
win_option('U',"Under layer",lay,0) win_refresh();
win_option('M',"Main layer",lay,1) win_refresh();
win_option('O',"Over layer",lay,2) win_refresh();
win_blank();
p=(lay==0?b_under:lay==1?b_main:b_over)+xcur+ycur*board_info.width;
if(lay==2) {
win_boolean('S',"Solid",p->kind,OVER_SOLID);
win_boolean('R',"Reserved",p->kind,OVER_RESERVED);
win_boolean('B',"BG Thru",p->kind,OVER_BG_THRU);
win_boolean('V',"Visible",p->kind,OVER_VISIBLE);
win_boolean('1',"User defined (1)",p->kind,0x01);
win_boolean('2',"User defined (2)",p->kind,0x02);
win_boolean('4',"User defined (4)",p->kind,0x04);
win_boolean('8',"User defined (8)",p->kind,0x08);
} else {
win_numeric('K',"Kind: ",p->kind,0,255);
}
win_color('C',"Color: ",p->color);
win_numeric('P',"Parameter: ",p->param,0,255);
win_char('h',"Character: ",p->param) win_refresh();
win_picture(1) {
char buf[40];
draw_text(0,0,buf,7,snprintf(buf,40,"Stat: %3d",p->stat));
if(!p->stat) v_color[8]=8;
}
win_command('t',"Select stat...") {
int n;
win_form("Select stat") {
win_cursor(p->stat);
win_list(maxstat+1,0,stat_list_callback,n) {
if(n!=p->stat) {
find_stat(xcur,ycur,p->stat,lay+1,0);
find_stat(xcur,ycur,p->stat=n,0,lay+1);
}
break;
}
win_blank();
win_command_esc(0,"Cancel") break;
}
}
if(p->stat && p->stat<=maxstat) {
win_command('E',"Edit stat item...") {
StatXY*r=find_stat(xcur,ycur,p->stat,lay+1,lay+1);
if(r) stat_xy_edit(stats+p->stat-1,r-stats[p->stat-1].xy);
}
}
// win_command('n',"Parameter menu...");
win_blank();
win_command_esc(0,"Done") break;
}
}
static void stat_list_callback(Uint16 n,int y,void*uz) {
char buf[81];
Stat*s=n?stats+n-1:0;
if(!n) {
draw_text(1,y," 0 (N/A)",0x08,-1);
v_color[y*80+3]=0x0E;
return;
}
draw_text(1,y,buf,0x0E,snprintf(buf,4,"%3u",n));
if(s->text && *s->text=='@') {
for(n=1;s->text[n] && s->text[n]!='\r' && s->text[n]!='\n' && n<28;n++);
draw_text(5,y,s->text+1,0x06,n-1);
}
draw_text(35,y,buf,0x07,snprintf(buf,48,"(%05u,%05u,%05u) L=%05u C=%05u S=%03u",s->misc1,s->misc2,s->misc3,s->length,s->count,s->speed));
}
static void stat_xy_list_callback(Uint16 n,int y,void*uz) {
char buf[81];
StatXY*o=((Stat*)uz)->xy+n;
draw_text(1,y,buf,0x0E,snprintf(buf,40,"%5u",n));
draw_text(7,y,buf,0x07,snprintf(buf,40,"(%05d,%05d,%c)",o->x,o->y,"-umo"[o->layer&3]));
v_char[80*y+23]=(o->layer&0x80?'L':250); v_color[80*y+23]=(o->layer&0x80?12:8);
v_char[80*y+24]=(o->layer&0x40?'U':250); v_color[80*y+24]=(o->layer&0x40?13:8);
draw_text(26,y,buf,0x07,snprintf(buf,40,"D=%3u",o->delay));
if(o->instptr==65535) draw_text(32,y,"(STOP)",7,-1); else draw_text(32,y,buf,7,snprintf(buf,40,"IP=%5u",o->instptr));
}
static Uint16 exchange_statxy(Stat*s,Uint16 m,Uint16 n) {
StatXY*o=s->xy+n;
StatXY*p=s->xy+m;
StatXY x=*p;
*p=*o;
*o=x;
return m;
}
static void copy_statxy(Stat*s,Uint16 m,Uint16 n) {
StatXY*o=s->xy+n;
StatXY*p=s->xy+m;
p->instptr=o->instptr;
p->layer=(p->layer&3)|(o->layer&~3);
p->delay=o->delay;
p->frame=o->frame;
p->extra=o->extra;
}
static void stat_xy_edit(Stat*s,Uint16 n) {
char r;
StatXY*o=s->xy+n;
char f=(o->layer>>2)&3;
char buf[81];
static const char*const lay[4]={"N/A","Under","Main","Over"};
win_form("Stat XY Edit") {
win_picture(4) {
draw_text(1,0,buf,7,snprintf(buf,80,"Index: %5u/%5u",n,s->count));
draw_text(1,1,buf,7,snprintf(buf,80,"X: %5u",o->x));
draw_text(1,2,buf,7,snprintf(buf,80,"Y: %5u",o->y));
draw_text(1,3,buf,7,snprintf(buf,80,"Layer: %s",lay[o->layer&3]));
}
win_boolean('U',"User",o->layer,0x40);
win_boolean('L',"Lock",o->layer,0x80);
win_numeric('y',"Delay: ",o->delay,0,255);
win_numeric('I',"Instruction: ",o->instptr,0,65535);
win_numeric('v',"Extra value: ",o->extra,0,65535);
win_blank();
win_command('R',"Restart script") o->instptr=0,r=1;
win_command('o',"Stop script") o->instptr=0xFFFF,r=1;
win_blank();
win_heading("Facing:");
win_option('E',"East",f,0);
win_option('N',"North",f,1);
win_option('W',"West",f,2);
win_option('S',"South",f,3);
win_boolean('k',"Walking",o->layer,0x10);
win_blank();
win_command('t',"Move to start") o=s->xy+(n=exchange_statxy(s,0,n)),r=1;
if(n) win_command('p',"Move to previous") o=s->xy+(n=exchange_statxy(s,n-1,n)),r=1;
if(n<s->count-1) win_command('x',"Move to next") o=s->xy+(n=exchange_statxy(s,n+1,n)),r=1;
win_command('d',"Move to end") o=s->xy+(n=exchange_statxy(s,s->count-1,n)),r=1;
win_blank();
win_command('C',"Copy instance data...") {
Uint16 m; Tile*b; Tile*t0; Tile*t1;
o->layer=(o->layer&0xF3)|(f<<2);
win_form("Copy instance data") {
win_command('A',"All") {
for(m=0;m<s->count;m++) copy_statxy(s,m,n);
break;
}
win_command('B',"Backward") {
for(m=0;m<n;m++) copy_statxy(s,m,n);
break;
}
win_command('F',"Forward") {
for(m=n+1;m<s->count;m++) copy_statxy(s,m,n);
break;
}
if(o->x<board_info.width && o->y<board_info.height && (o->layer&3)) win_command('M',"Matching tiles") {
b=b_under+((o->layer&3)-1)*(board_info.width*board_info.height); t0=b+o->y*board_info.width+o->x;
for(m=0;m<s->count;m++) if(m!=n && !((o->layer^s->xy[m].layer)&3) && s->xy[m].x<board_info.width && s->xy[m].y<board_info.height) {
t1=b+s->xy[m].y*board_info.width+s->xy[m].x;
if(t0->kind==t1->kind && t0->color==t1->color && t0->param==t1->param && t0->stat==t1->stat) copy_statxy(s,m,n);
}
break;
}
win_command_esc(0,"Cancel") break;
}
r=1;
}
if(r) {
r=0;
win_refresh();
}
win_blank();
win_command_esc(0,"Done") break;
}
o->layer=(o->layer&0xF3)|(f<<2);
}
static void stat_xy_edit_at(Uint16 x,Uint16 y,Uint8 lay) {
StatXY*r;
Uint8 n=(lay==1?b_under:lay==2?b_main:b_over)[y*board_info.width+x].stat;
if(!n || !lay) return;
if(r=find_stat(x,y,n,lay,lay)) stat_xy_edit(stats+n-1,r-stats[n-1].xy);
}
static int statxy_sorter_callback(const void*aa,const void*bb) {
const StatXY*a=aa;
const StatXY*b=bb;
int q=numprefix>>4;
int t=numprefix&15;
int z=(a->layer-b->layer)&3;
if(t&8) {
if(a->layer&0x80&~b->layer) return -1;
if(b->layer&0x80&~a->layer) return 1;
}
if(q<2 && z) return z*(t&4?-1:1);
if((q&1) && a->y!=b->y) return a->y<b->y?(t&2?1:-1):(t&2?-1:1);
if(a->x!=b->x) return a->x<b->x?(t&1?1:-1):(t&1?-1:1);
if(a->y!=b->y) return a->y<b->y?(t&2?1:-1):(t&2?-1:1);
return z*(t&4?-1:1);
}
static void stat_edit(int n) {
char title[40];
char buf[80];
char nam[60];
Stat*s=stats+n-1;
int i,j;
Uint32 at;
Stat q;
snprintf(title,40,"Stat #%d",n);
name:
nam[1]=0;
if(s->text && *s->text=='@') {
for(i=0;s->text[i] && s->text[i]!='\r' && s->text[i]!='\n' && i<48;i++) nam[i]=s->text[i];
nam[i]=0;
}
win_form(title) {
win_numeric('1',"Misc1: ",s->misc1,0,0xFFFF);
win_numeric('2',"Misc2: ",s->misc2,0,0xFFFF);
win_numeric('3',"Misc3: ",s->misc3,0,0xFFFF);
win_numeric('S',"Speed: ",s->speed,0,255);
win_boolean('m',"Restrict movement to zone",s->mode,STAT_ZONERESTRICT);
win_numeric('Z',"Zone: ",s->zone,0,255);
win_picture(3) {
draw_text(1,0,buf,7,snprintf(buf,80,"Length: %5d",s->length));
draw_text(1,1,buf,7,snprintf(buf,80,"Count: %5d",s->count));
draw_text(1,2,"Name: ",7,-1);
draw_text(7,2,nam+1,6,-1);
}
win_command('T',"Text") {
s->text=text_editor(s->text);
s->length=(s->text?strlen(s->text):0);
goto name;
}
win_command('L',"XY List") {
win_form(title) {
win_list(s->count,s,stat_xy_list_callback,i) stat_xy_edit(s,i);
win_blank();
win_command('S',"Sort...") {
i=j=0;
win_form("Sort stat XY list") {
win_option('X',"Layer,X,Y",i,0);
win_option('Y',"Layer,Y,X",i,1);
win_option('L',"X,Y,Layer",i,2);
win_option('a',"Y,X,Layer",i,3);
win_blank();
win_boolean('R',"Reverse X",j,1);
win_boolean('v',"Reverse Y",j,2);
win_boolean('s',"Reverse Layer",j,4);
win_boolean('u',"First if user bits set",j,8);
win_blank();
win_command('E',"Execute") {
numprefix=(i<<4)+j;
qsort(s->xy,s->count,sizeof(StatXY),statxy_sorter_callback);
numprefix=0;
break;
}
win_command_esc(0,"Cancel") break;
}
}
win_command('R',"Reverse") {
if(s->count) for(i=0;i<=s->count/2;i++) exchange_statxy(s,i,s->count-i-1);
}
win_command_esc(0,"Done") break;
}
}
win_command('x',"Exchange") {
*buf=0;
ask_text("Exchange with:",buf,8);
if(*buf && (i=strtol(buf,0,10)) && i<=maxstat && i!=n) {
for(at=0;at<board_info.width*board_info.height*3;at++) {
if(b_under[at].stat==n) b_under[at].stat=i; else if(b_under[at].stat==i) b_under[at].stat=n;
}
q=stats[i-1];
stats[i-1]=stats[n-1];
stats[n-1]=q;
s=stats+(n=i)-1;
}
}
if(maxstat>1) win_command('D',"Delete") {
if(ask_yn("Delete?",0)) {
for(at=0;at<board_info.width*board_info.height*3;at++) {
if(b_under[at].stat==n) b_under[at].stat=0; else if(b_under[at].stat>n) --b_under[at].stat;
}
if(n<maxstat) memmove(stats+n-1,stats+n,(maxstat-n)*sizeof(Stat));
--maxstat;
return;
}
}
win_command_esc(0,"Done") break;
}
}
static void stat_list(int pc) {
int n;
restart:
win_form("Stat list") {
win_cursor(pc?:1);
win_list(maxstat+1,0,stat_list_callback,n) if(n) stat_edit(n);
win_blank();
if(maxstat<255) win_command('A',"Add new stat") {
pc=new_stat();
goto restart;
}
win_command_esc(0,"Done") break;
}
}
static void resize_board(void) {
static Uint8 m=0;
static Uint8 b=0;
Uint16 w=board_info.width;
Uint16 h=board_info.height;
Tile*ku=b_under;
Tile*km=b_main;
Tile*ko=b_over;
Uint32 x,y,z;
Sint32 ox,oy;
win_form("Resize board") {
win_help("editbrd","resize");
win_picture(1) {
char buf[40];
draw_text(1,0,buf,7,snprintf(buf,40,"Current: %dx%d",board_info.width,board_info.height));
}
win_numeric('W',"Width: ",w,1,9999);
win_numeric('H',"Height: ",h,1,9999);
win_heading("Do with existing grid:");
win_option('N',"Northwest",m,0);
win_option('o',"Northeast",m,1);
win_option('S',"Southwest",m,2);
win_option('u',"Southeast",m,3);
win_option('C',"Center",m,4);
win_option('T',"Tile",m,5);
win_option('d',"Tile with duplicate stats",m,6);
win_boolean('F',"Fill main with current",b,1);
win_boolean('v',"Fill overlay with NW tile",b,2);
win_blank();
win_command('p',"Use cursor position") {
w=xcur+1;
h=ycur+1;
win_refresh();
}
win_command('x',"Execute") break;
win_command_esc(0,"Cancel") return;
}
if(w==board_info.width && h==board_info.height) return;
if(w!=board_info.width) {
for(z=0;z<16;z++) if(uzone[z]) {
if(ask_yn("If you proceed, unordered zones will be erased. Are you sure?",0)) break; else return;
}
} else if(h<board_info.height) {
for(z=0;z<16;z++) if(uzone[z]->maxy>=h) {
if(ask_yn("If you proceed, unordered zones may be truncated. Are you sure?",0)) break; else return;
}
}
if(w<board_info.width || h<board_info.height) {
for(z=0;z<16;z++) if(ozone[z] && ozone[z]->ncells) {
if(ask_yn("If you proceed, ordered zones may be truncated. Are you sure?",0)) break; else return;
}
}
b_under=calloc(w*h,3*sizeof(Tile));
if(!b_under) err(1,"Allocation failed");
b_main=b_under+w*h;
b_over=b_main+w*h;
if(m>4) {
for(x=0;x<w;x++) for(y=0;y<h;y++) {
z=x%board_info.width+(y%board_info.height)*board_info.width;
b_under[x+y*w]=ku[z];
b_main[x+y*w]=km[z];
b_over[x+y*w]=ko[z];
if(m==5 && x>=board_info.width || y>=board_info.height) b_under[z].stat=b_main[z].stat=b_over[z].stat=0;
}
if(m==6) {
ox=(w/board_info.width)+(w%board_info.width?1:0);
oy=(h/board_info.height)+(h%board_info.height?1:0);
for(x=0;x<maxstat;x++) if(stats[x].count) {
z=stats[x].count;
stats[x].xy=realloc(stats[x].xy,(stats[x].count*=ox*oy)*sizeof(StatXY));
if(!stats[x].xy) err(1,"Allocation failed");
for(y=z;y<stats[x].count;y++) {
stats[x].xy[y]=stats[x].xy[y%z];
stats[x].xy[y].x+=board_info.width*((y/z)%ox);
stats[x].xy[y].y+=board_info.height*((y/z)/ox);
}
}
}
} else {
if(b&1) for(x=0;x<w*h;x++) b_main[x]=clip,b_main[x].stat=0;
if(b&2) for(x=0;x<w*h;x++) b_over[x]=*ko,b_over[x].stat=0;
switch(m) {
case 0: ox=oy=0; break;
case 1: ox=w-board_info.width; oy=0; break;
case 2: ox=0; oy=h-board_info.height; break;
case 3: ox=w-board_info.width; oy=h-board_info.height; break;
case 4: ox=(w-board_info.width)/2; oy=(h-board_info.height)/2; break;
}
for(x=0;x<maxstat;x++) for(y=0;y<stats[x].count;y++) {
stats[x].xy[y].x+=ox;
stats[x].xy[y].y+=oy;
}
xcur+=ox; ycur+=oy;
if(xcur>=w) xcur=0;
if(ycur>=h) ycur=0;
ox=-ox; oy=-oy;
for(x=0;x<board_info.width;x++) for(y=0;y<board_info.height;y++) {
if(x+ox<0 || x+ox>=w || y+oy<0 || y+oy>=h) continue;
z=x+ox+(y+oy)*board_info.width;
b_under[x+y*w]=ku[z];
b_main[x+y*w]=km[z];
b_over[x+y*w]=ko[z];
}
}
if(w!=board_info.width) {
for(z=0;z<16;z++) if(uzone[z]) {
uzone[z]->maxy=0;
memset(uzone[z]->data,0,board_info.width/8+1);
}
} else if(h<board_info.height) {
for(z=0;z<16;z++) if(uzone[z]->maxy>=h) uzone[z]->maxy=h;
}
if(w<board_info.width || h<board_info.height) {
for(z=0;z<16;z++) if(ozone[z] && ozone[z]->ncells) {
for(x=0;x<ozone[z]->ncells;x++) if(ozone[z]->xy[x].x>=w || ozone[z]->xy[x].y>=h) {
ozone[z]->ncells--;
memmove(ozone[z]->xy+x,ozone[z]->xy+x+1,(ozone[z]->ncells-x)*sizeof(OrdZoneXY));
x--;
}
}
}
board_info.width=w;
board_info.height=h;
free(ku);
clear_extra_stats();
}
static void set_board_editor_screen(void) {
memset(cur_screen.command,SC_BOARD+5,80*25);
memset(cur_screen.color,0x01,80*25);
memset(cur_screen.parameter,177,80*25);
cur_screen.view_x=39;
cur_screen.view_y=12;
cur_screen.message_x=cur_screen.message_y=180;
cur_screen.flag=0;
cur_screen.soft_edge[DIR_W]=cur_screen.hard_edge[DIR_W]=0;
cur_screen.soft_edge[DIR_N]=cur_screen.hard_edge[DIR_N]=0;
cur_screen.soft_edge[DIR_E]=cur_screen.hard_edge[DIR_E]=79;
cur_screen.soft_edge[DIR_S]=cur_screen.hard_edge[DIR_S]=24;
}
static void esave(void) {
FILE*fp=open_lump_by_number(brd_id,"BRD","w");
if(fp) {
save_board(fp,0);
fclose(fp);
}
}
static inline void switch_to_board(Sint32 id) {
FILE*fp;
if(id<0) return;
esave();
fp=open_lump_by_number(id,"BRD","r");
if(!fp) {
if(id<=maxboard) goto_board(id);
return;
}
load_board(fp);
fclose(fp);
if(xcur>=board_info.width || ycur>=board_info.height) xcur=ycur=0;
brd_id=id;
}
static Sint32 add_board(void) {
char buf[61]="";
ask_text("Add new board:",buf,60);
if(*buf) {
set_board_name(maxboard+1,buf);
return maxboard;
} else {
return -1;
}
}
static void escroll(void) {
if(xcur>scroll_x+79) {
scroll_x+=config.editor_scroll_x;
if(xcur>scroll_x+79) scroll_x=xcur-79;
} else if(xcur<scroll_x) {
scroll_x-=config.editor_scroll_x;
if(xcur<scroll_x) scroll_x=xcur;
}
if(ycur>scroll_y+24) {
scroll_y+=config.editor_scroll_y;
if(ycur>scroll_y+24) scroll_y=ycur-24;
} else if(ycur<scroll_y) {
scroll_y-=config.editor_scroll_y;
if(ycur<scroll_y) scroll_y=ycur;
}
if(scroll_x+79>board_info.width) scroll_x=board_info.width-79;
if(scroll_y+24>board_info.height) scroll_y=board_info.height-24;
if(scroll_x<0) scroll_x=0;
if(scroll_y<0) scroll_y=0;
}
static void set_apparent_clipq(void) {
Uint8 n,c,d;
for(n=0;n<nclipq;n++) {
c=elem_def[clipq[n].kind].app[0];
d=elem_def[clipq[n].kind].app[1];
if(c&0x20) {
apparent_clipq[n]=appearance_mapping[((d&0x7E)+((clipq[n].param>>(c&7))&"\x01\x03\x07\x0F"[(c>>3)&3]))&0x7F];
} else switch(c&0x1F) {
case AP_FIXED: case AP_UNDER: apparent_clipq[n]=d; break;
case AP_PARAM: apparent_clipq[n]=d+clipq[n].param; break;
case AP_LINES: apparent_clipq[n]=appearance_mapping[(d&0x70)|0x0F]; break;
case AP_ANIMATE: apparent_clipq[n]=appearance_mapping[(animation[d&3].step[0]+(d&0x7C))&0x7F]; break;
case AP_MISC1: if(clipq[n].stat && clipq[n].stat<=maxstat) apparent_clipq[n]=stats[clipq[n].stat-1].misc1; else apparent_clipq[n]=d; break;
case AP_MISC2: if(clipq[n].stat && clipq[n].stat<=maxstat) apparent_clipq[n]=stats[clipq[n].stat-1].misc2; else apparent_clipq[n]=d; break;
case AP_MISC3: if(clipq[n].stat && clipq[n].stat<=maxstat) apparent_clipq[n]=stats[clipq[n].stat-1].misc3; else apparent_clipq[n]=d; break;
default: apparent_clipq[n]='?';
}
}
}
static void set_apparent_clip(void) {
Uint8 c=elem_def[clip.kind].app[0];
Uint8 d=elem_def[clip.kind].app[1];
if(c&0x20) {
apparent_clip=appearance_mapping[((d&0x7E)+((clip.param>>(c&7))&"\x01\x03\x07\x0F"[(c>>3)&3]))&0x7F];
} else switch(c&0x1F) {
case AP_FIXED: case AP_UNDER: apparent_clip=d; break;
case AP_PARAM: apparent_clip=d+clip.param; break;
case AP_LINES: apparent_clip=appearance_mapping[(d&0x70)|0x0F]; break;
case AP_ANIMATE: apparent_clip=appearance_mapping[(animation[d&3].step[0]+(d&0x7C))&0x7F]; break;
case AP_MISC1: if(clip.stat && clip.stat<=maxstat) apparent_clip=stats[clip.stat-1].misc1; else apparent_clip=d; break;
case AP_MISC2: if(clip.stat && clip.stat<=maxstat) apparent_clip=stats[clip.stat-1].misc2; else apparent_clip=d; break;
case AP_MISC3: if(clip.stat && clip.stat<=maxstat) apparent_clip=stats[clip.stat-1].misc3; else apparent_clip=d; break;
default: apparent_clip='?';
}
}
static void estatus(void) {
// 00000000001111111111222222222233333333334444444444555555555566666666667777777777
// 01234567890123456789012345678901234567890123456789012345678901234567890123456789
// _____ee<c>_______________<pp>s*umo_____ VE/qqqqqqqqqqqqq +____+____(____,____)
char buf[80];
int y=24;
int x;
if(board_info.height>24 && v_ycur>12) y=0;
memset(v_color+y*80,0x11,80);
memset(v_font+y*80,VF_FRONT|VF_SYSTEM,80);
draw_text(0,y,buf,0x1B,snprintf(buf,80,"%5d",brd_id));
v_color[y*80+5]=v_color[y*80+6]=0x12;
v_char[y*80+5]="\xFA\x18\x19\x12"[(board_info.exits[DIR_N]?1:0)+(board_info.exits[DIR_S]?2:0)];
v_char[y*80+6]="\xFA\x1A\x1B\x1D"[(board_info.exits[DIR_E]?1:0)+(board_info.exits[DIR_W]?2:0)];
draw_text(7,y,"<\xFE>",0x17,3);
v_color[y*80+8]=clip.color;
v_font[y*80+8]=VF_FRONT;
draw_text(10,y,elem_def[clip.kind].name,0x1B,-1);
draw_text(25,y,buf,0x17,snprintf(buf,80,"<%02X>",clip.param));
v_char[y*80+29]=(clip.stat?'s':' ');
v_color[y*80+29]=0x1A;
v_char[y*80+30]=apparent_clip;
v_color[y*80+30]=clip.color;
v_font[y*80+30]=VF_FRONT;
if(b_under[ycur*board_info.width+xcur].kind) v_char[y*80+31]='u',v_color[y*80+31]=0x13;
if(b_main[ycur*board_info.width+xcur].kind) v_char[y*80+32]='m',v_color[y*80+32]=0x13;
if(b_over[ycur*board_info.width+xcur].kind) v_char[y*80+33]='o',v_color[y*80+33]=0x13;
if(numprefix) draw_text(34,y,buf,0x1E,snprintf(buf,80,"%5d",numprefix));
if(markgrid && xcur<markwidth && ycur<markheight) {
if(markgrid[(xcur>>3)+ycur*markskip]&(1<<(xcur&7))) v_char[y*80+39]=7,v_color[y*80+39]=0x1A;
}
if(vmode) v_char[y*80+41]='V',v_color[y*80+41]=0x1D;