forked from NagyD/SDLPoP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.c
2477 lines (2210 loc) · 75.5 KB
/
editor.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
/*
SDLPoP editor module
Copyright (C) 2013-2016 Dávid Nagy, Enrique P. Calot
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
The authors of this program may be contacted at http://forum.princed.org
*/
#include "common.h"
#ifdef USE_EDITOR
/********************************************\
* Headers of room API *
\********************************************/
#define MAP_SIDE (NUMBER_OF_ROOMS*2+3)
#define MAP_CENTER (NUMBER_OF_ROOMS+1)
#define POS_UP (-MAP_SIDE)
#define POS_DOWN MAP_SIDE
#define POS_LEFT (-1)
#define POS_RIGHT 1
#define MAP_POS(x,y) ((y)*MAP_SIDE+(x))
#define PALETTE_MODE (drawn_room>NUMBER_OF_ROOMS)
/********************************************\
* Randomization sublayer headers *
\********************************************/
/* E_x_y = e^(-(x^2+y^2)) -> gaussian blur */
#define E_0_1 .36787944117144232159
#define E_0_2 .01831563888873418029
#define E_1_1 .13533528323661269189
#define E_1_2 .00673794699908546709
typedef struct probability_info {
int count;
float value;
} tProbability;
float room_api_measure_entropy(const tMap* map, tile_global_location_type t, byte* level_mask);
tile_packed_type room_api_suggest_tile(const tMap* map, tile_global_location_type tilepos, byte* level_mask);
/********************************************\
* DUR: Do, undo, redo layer implementation *
\********************************************/
/* DUR: stack sublayer */
#define true 1
#define false 0
int stack_size=0;
int stack_cursor=0;
long* stack_pointer=NULL;
int stack_top=0;
#define stack_reset() stack_top=0;
/* private type */
typedef uint64_t flag_type; /* note that uint64_t/unsigned long long is supported even in 32 bits machines and virtually by all linux, 32 bit windows and OSX. The variable is used as:
RRRRRRRR RRRRRRRR RRRRRRRR RRRRRRRR RRRRRRRR Rgpxmres BBBBBBBB AAAAAAAA
where R are an offset/reserved bit
r,m,x,p,g are redraw,remap,reserved,guard_presence,guard_repaint flags
e,s are startm end marks
B is the 8 bits code before editing
A is the 8 bits code after editing
*/
void stack_push(flag_type data) {
if (!stack_size) {
stack_size=100;
stack_pointer=malloc(sizeof(flag_type)*stack_size);
}
stack_pointer[stack_cursor]=data;
stack_cursor++;
/*if (stack_top<stack_cursor)*/ stack_top=stack_cursor;
if (stack_size<=stack_top) {
stack_size*=2;
stack_pointer=realloc(stack_pointer,sizeof(flag_type)*stack_size);
}
}
int stack_pop(flag_type* data) {
if (!stack_cursor) return false;
*data=stack_pointer[--stack_cursor];
return true;
}
int stack_unpop(flag_type* data) {
if (stack_cursor==stack_top) return false;
*data=stack_pointer[stack_cursor++];
return true;
}
void stack_or(flag_type data) {
if (stack_cursor) {
stack_pointer[stack_cursor-1]|=data;
}
}
#undef true
#undef false
/* DUR: Do actions sublayer */
#define MARK_BITS 7
typedef enum {
mark_middle=0,
mark_start=1,
mark_end=2,
mark_all=3,
flag_redraw=4,
flag_remap=8,
flag_reserved=16, /* not used yet */
flag_guard_presence =32,
flag_guard_repaint =64,
flag_mask=124,
mark_flag_mask=127
}tUndoQueueMark;
tUndoQueueMark prevMark=mark_middle;
#define editor__do_mark_start(m) prevMark=mark_start|m
void editor__do_mark_end(tUndoQueueMark m) {
if (prevMark) /* if writing is open */
stack_or((mark_end|m)<<16);
prevMark=mark_middle;
}
/* editor level layer used by do/undo/redo */
typedef enum {extra_none,extra_up='A',extra_down='B',extra_left='L',extra_right='R'} movement_type;
typedef struct {
tile_packed_type main;
movement_type extratype;
tile_packed_type extra;
} copied_type;
void editor__load_level();
copied_type copied={NO_TILE_,extra_none,NO_TILE_};
tile_packed_type clipboard[30]={NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_,NO_TILE_};
byte selected_mask[30]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
#define clean_selected_mask(room) memset(selected_mask,0,sizeof(byte)*30);selected_mask_room=room
int selected_mask_room=-1;
enum {chNothing=-1,chFullRoom=1,chTiles=0} clipboard_has=chNothing;
int clipboard_shift=0;
level_type edited;
int ambiguous_mode=0;
int remember_room=0; /* when switching to palette room mode */
int map_selected_room=0;
int goto_next_room=0;
tMap edited_map={NULL,NULL};
int edited_doorlinks=0;
int edition_level=-1;
#define offset_(field) ((long)(&(((level_type*)NULL)->field)))
#define editor__do(field,c,mark) editor__do_( offset_(field) ,c,mark)
void editor__do_(long offset, byte c, tUndoQueueMark mark) {
byte before;
mark=mark|prevMark;
prevMark=mark_middle;
if (edition_level!=current_level)
editor__load_level();
before=offset[(char*)(&edited)];
if (mark==mark_middle && before==c) return; /* optimization: do nothing if the tile is the same and no marks are needed */
offset[(char*)(&edited)]=c;
offset[(char*)(&level)]=c;
stack_push(offset<<(16+MARK_BITS)|mark<<16|before<<8|c);
}
void editor_change_tile(tile_global_location_type l, tile_packed_type t) {
if (door_api_is_related(t.concept.fg&TILE_MASK)==cButton)
t.concept.bg=255; /* don't mess with room linking */
editor__do(fg[l],t.concept.fg,mark_middle);
editor__do(bg[l],t.concept.bg,mark_middle);
}
tUndoQueueMark editor__undo() {
flag_type aux;
int offset;
byte before;
tUndoQueueMark mark=0;
while (stack_pop(&aux)) {
/* after= aux & 0xff; */
before= (aux>>8) & 0xff;
mark= (aux>>16)& mark_flag_mask;
offset= (aux>>(16+MARK_BITS));
offset[(char*)(&level)]=before;
offset[(char*)(&edited)]=before;
if (mark & mark_start) break;
}
return mark&flag_mask;
}
tUndoQueueMark editor__redo() {
flag_type aux;
int offset;
byte after;
tUndoQueueMark mark=0;
while (stack_unpop(&aux)) {
after= aux & 0xff;
/* before= (aux>>8) & 0xff; */
mark= (aux>>16)& mark_flag_mask;
offset= (aux>>(16+MARK_BITS));
offset[(char*)(&level)]=after;
offset[(char*)(&edited)]=after;
if (mark & mark_end) break;
}
return mark&flag_mask;
}
/********************************************\
* Room refreshing functions *
\********************************************/
void ed_select_room(int room) {
get_room_address(room);
room_L = level.roomlinks[room-1].left;
room_R = level.roomlinks[room-1].right;
}
void ed_redraw_tile(int tilepos) { //of the current room
level.fg[T(loaded_room,tilepos)]=edited.fg[T(loaded_room,tilepos)];
level.bg[T(loaded_room,tilepos)]=edited.bg[T(loaded_room,tilepos)];
load_alter_mod(tilepos);
}
void ed_redraw_room() { //all the current room
for (int i=0;i<30;i++) ed_redraw_tile(i);
}
/********************************************\
* Door linking API *
\********************************************/
tTileDoorType door_api_is_related(byte tile) {
switch(tile) {
case tiles_6_closer:
case tiles_15_opener:
return cButton;
case tiles_4_gate:
case tiles_16_level_door_left:
case tiles_17_level_door_right:
return cDoor;
}
return cOther;
}
void door_api_init_iterator(tIterator* it, tile_global_location_type tp) {
byte tile=edited.fg[tp]&TILE_MASK;
switch(tile) {
case tiles_6_closer:
case tiles_15_opener:
it->data.index=edited.bg[tp];
it->type=it->data.index==255?noneIterator:buttonIterator; /* 255 is reserved as no link */
return;
case tiles_4_gate:
case tiles_16_level_door_left:
it->data.info.tile=tp;
it->data.info.i=0;
it->type=doorIterator;
return;
case tiles_17_level_door_right:
if (P(tp)%10 &&
(edited.fg[tp-1]&TILE_MASK)==tiles_16_level_door_left) {
tp--;
door_api_init_iterator(it,tp);
return;
}
}
it->type=noneIterator;
return;
}
int door_api_get(tIterator* it, tile_global_location_type *tile) {
switch (it->type) {
case buttonIterator:
if (it->data.index>=NUMBER_OF_DOORLINKS) return 0;
short next;
get_doorlink((edited.doorlinks2[it->data.index]<<8)|edited.doorlinks1[it->data.index],tile,&next);
it->data.index++;
if (!next) it->type=noneIterator;
return 1;
case doorIterator:
for (short* i=&it->data.info.i;(*i)<NUMBER_OF_ROOMS*30;(*i)++) { /* first loop: check all tiles to find buttons */
byte fg=edited.fg[*i]&TILE_MASK;
if (door_api_is_related(fg)==cButton) {
tIterator it2;
tile_global_location_type linked_tile;
if (!edited_map.list[R_(*i)]) continue; /* skip unused rooms */
door_api_init_iterator(&it2,*i);
while(door_api_get(&it2,&linked_tile)) { /* second loop: find door opened by those buttons */
if (linked_tile==it->data.info.tile) {
*tile=*i;
(*i)++;
return 1;
}
}
}
}
it->type=noneIterator;
case noneIterator:
break;
}
return 0;
}
void door_api_free(int* max_doorlinks) {}
void door_api_init(int* max_doorlinks) {
tile_global_location_type i;
*max_doorlinks=0;
for (i=0;i<NUMBER_OF_ROOMS*30;i++) {
if (door_api_is_related(edited.fg[i]&TILE_MASK)==cButton) {
byte aux=edited.bg[i];
if (*max_doorlinks<aux) *max_doorlinks=aux;
}
}
/* itarate over the last value */
short next=1;
tile_global_location_type junk;
do {
get_doorlink((edited.doorlinks2[*max_doorlinks]<<8)|edited.doorlinks1[*max_doorlinks],&junk,&next);
(*max_doorlinks)++;
} while ((*max_doorlinks)<256 && next);
(*max_doorlinks)--;
}
void door_api_swap(const int* max_doorlinks, int r1,int r2) {
for (int i=0;i<=*max_doorlinks;i++) {
tile_global_location_type door;
short next;
get_doorlink((edited.doorlinks2[i]<<8)|edited.doorlinks1[i],&door,&next);
int room=R(door);
if (R(door)==r1) room=r2;
else if (R(door)==r2) room=r1;
Uint16 doorlink;
set_doorlink(&doorlink,T(room,P(door)),next);
editor__do(doorlinks1[i],doorlink&0xff,mark_middle);
editor__do(doorlinks2[i],(doorlink>>8)&0xff,mark_middle);
}
}
void door_api_refresh(int* max_doorlinks, tMap* map, int* selected_door_tile) {
if (*selected_door_tile!=-1 && door_api_is_related(edited.fg[*selected_door_tile]&TILE_MASK)==cOther)
*selected_door_tile=-1;
//check the door handling system for inconsistencies
tile_global_location_type doors[255];
tile_global_location_type buttons[256];
Uint8 door_counts[256];
Uint8 door_count=0;
Uint8 door_count_absolute=0;
Uint8 button_count=0;
for (tile_global_location_type button=0;button<30*NUMBER_OF_ROOMS;button++)
if (map->list[R_(button)])
if (door_api_is_related(edited.fg[button]&TILE_MASK)==cButton) {
/* start iteration */
tIterator it;
tile_global_location_type door;
door_api_init_iterator(&it,button);
while(door_api_get(&it,&door)) /* check if existent to unlink */
if (door_api_is_related(edited.fg[door]&TILE_MASK)==cDoor) {
if (door_count_absolute>255) break;
doors[door_count_absolute]=door;
door_count++;
door_count_absolute++;
}
buttons[button_count]=button;
door_counts[button_count]=door_count;
door_count=0;
button_count++;
if (door_count_absolute>255) break;
}
*max_doorlinks=door_count_absolute-1;
/* rewrite all door handling areas in the level */
//printf("Door api debug:\n");
for (int k=0,l2=0;k<button_count;k++) {
//printf("button=(%2d,%2d):\n",R(buttons[k]),P(buttons[k]));
if (!door_counts[k]) { /* empty button */
editor__do(bg[buttons[k]],255,mark_middle);
} else {
editor__do(bg[buttons[k]],l2,mark_middle);
for (int l=0;l<door_counts[k];l++,l2++) {
//printf("d=(%2d,%2d)\n",R(doors[l2]),P(doors[l2]));
Uint16 doorlink;
set_doorlink(&doorlink,doors[l2],/* next?*/ l+1!=door_counts[k]);
editor__do(doorlinks1[l2],doorlink&0xff,mark_middle);
editor__do(doorlinks2[l2],(doorlink>>8)&0xff,mark_middle);
}
}
}
}
int door_api_link(int* max_doorlinks, tile_global_location_type door,tile_global_location_type button) { /* Assumption: door is a door (or left exitdoor) and button is a button */
if (*max_doorlinks==255) return 0; /* no more space available */
int pivot=edited.bg[button];
Uint16 doorlink;
if (pivot==255) { /* I'm defining 255 as "no links" */
/* append link on the top of the list */
(*max_doorlinks)++;
editor__do(bg[button],(*max_doorlinks),mark_middle);
/* 3) insert the link */
set_doorlink(&doorlink,door,0);
editor__do(doorlinks1[*max_doorlinks],doorlink&0xff,mark_middle);
editor__do(doorlinks2[*max_doorlinks],(doorlink>>8)&0xff,mark_middle);
} else {
/* three steps to insert a new link: */
/* 1) make space in the table */
int i;
for (i=*max_doorlinks;i>=pivot;i--) {
editor__do(doorlinks1[i+1],edited.doorlinks1[i],mark_middle);
editor__do(doorlinks2[i+1],edited.doorlinks2[i],mark_middle);
}
/* 2) update button references */
for (i=0;i<NUMBER_OF_ROOMS*30;i++)
if (door_api_is_related(edited.fg[i]&TILE_MASK)==cButton)
if (edited.bg[i]>pivot && edited.bg[i]!=255)
editor__do(bg[i],edited.bg[i]+1,mark_middle);
/* 3) insert the link */
(*max_doorlinks)++;
set_doorlink(&doorlink,door,1);
editor__do(doorlinks1[pivot],doorlink&0xff,mark_middle);
editor__do(doorlinks2[pivot],(doorlink>>8)&0xff,mark_middle);
}
return 1;
}
void door_api_unlink(int* max_doorlinks, tile_global_location_type door,tile_global_location_type button) {
/* read link */
tile_global_location_type tile;
short next;
int i=edited.bg[button];
if (i==255) return; /* error, button has no links */
do {
get_doorlink((edited.doorlinks2[i]<<8)|edited.doorlinks1[i],&tile,&next);
i++;
} while(next && door!=tile);
i--;
if (door!=tile) return; /* error, link not found */
if (!next) { /* this is the last link */
if (i==edited.bg[button]) { /* if the last link is the first, empty the list */
editor__do(bg[button],255,mark_middle); /* remove link */
} else { /* if there are more links before this one, set the !next bit in the previous one */
get_doorlink((edited.doorlinks2[i-1]<<8)|edited.doorlinks1[i-1],&tile,&next);
next=0;
Uint16 aux;
set_doorlink(&aux,tile,next);
editor__do(doorlinks1[i-1],aux&0xff,mark_middle);
editor__do(doorlinks2[i-1],(aux>>8)&0xff,mark_middle);
}
}
/* update references */
for (int j=0;j<NUMBER_OF_ROOMS*30;j++)
if (door_api_is_related(edited.fg[j]&TILE_MASK)==cButton)
if (edited.bg[j]>i && edited.bg[j]!=255) /* 255 is no link */
editor__do(bg[j],edited.bg[j]-1,mark_middle);
/* just shift the array one position */
for (;i<*max_doorlinks;i++) {
editor__do(doorlinks1[i],edited.doorlinks1[i+1],mark_middle);
editor__do(doorlinks2[i],edited.doorlinks2[i+1],mark_middle);
}
(*max_doorlinks)--;
}
int door_api_fix_pos(tile_global_location_type* door,tile_global_location_type* button) {
if (*door==-1 || *button==-1) return 0;
byte tile_door=edited.fg[*door]&TILE_MASK;
byte tile_button=edited.fg[*button]&TILE_MASK;
tTileDoorType door_type=door_api_is_related(tile_door);
tTileDoorType button_type=door_api_is_related(tile_button);
if (door_type==button_type || !door_type || !button_type) return 0;
if (door_type==cButton) { /* Swap */
tile_global_location_type aux=*door;
*door=*button;
*button=aux;
tile_door=edited.fg[*door]&TILE_MASK; /* refresh tile_door after swapping */
}
if (tile_door==tiles_17_level_door_right) { /* door right case */
if (
(*door)%10 &&
(edited.fg[(*door)-1]&TILE_MASK)==tiles_16_level_door_left
) {
(*door)--;
}
}
return 1;
}
/* editor functions related to this api */
int selected_door_tile=-1;
void editor__save_door_tile(tile_global_location_type tile) { /* if the same tile was selected "unselect" if not "select this tile".*/
if ((edited.fg[tile]&TILE_MASK)==tiles_17_level_door_right) {
if (tile%10 &&
(edited.fg[tile-1]&TILE_MASK)==tiles_16_level_door_left) {
tile--;
} else {
return;
}
}
selected_door_tile=(selected_door_tile==tile)?-1:tile;
}
/* debug function
void printl() {
int i;
for (i=0;i<=edited_doorlinks;i++) {
tile_global_location_type tile;
short next;
get_doorlink((edited.doorlinks2[i]<<8)|edited.doorlinks1[i],&tile,&next);
printf("i=%d r=%d,t=%d next=%d\n",i,R(tile),P(tile),next);
}
}*/
const char* editor__toggle_door_tile(short room,short tilepos) {
tile_global_location_type door=T(room,tilepos);
tile_global_location_type button=selected_door_tile;
if (!door_api_fix_pos(&door,&button)) return "Select a button and a door"; /* Error */
tIterator it;
tile_global_location_type check_tile;
door_api_init_iterator(&it,door);
while(door_api_get(&it,&check_tile)) /* check if existent to unlink */
if (check_tile==button) {
door_api_unlink(&edited_doorlinks,door,button);
return "Door unlinked";
}
if (door_api_link(&edited_doorlinks,door,button))
return "Door linked";
return "Link error";
}
/********************************************\
* INI functions *
\********************************************/
int ini_load(const char *filename,
int (*report)(const char *section, const char *name, const char *value));
typedef enum {
cMain=1, /* White+grey: cursors cross+tile. */
cRight=3, /* Red: cursor tile when ctrl+alt is pressed and the tile is a door/button. */
cWrong=5, /* Blue: cursor tile when ctrl+alt is pressed and the tile is NOT a door/button. No actions possible. */
cLinked=7, /* Green: When a door/button is selected, the linked tiles will have this color */
cSelected=9,/* Yellow: The selected door/button. */
cRandom=11, /* Cyan: Before randomization. */
cTileSel=13 /* Orange: Selected tile. */
} tCursorColors; /* The palettes are taken from the res201 bitmap, ignoring the res200 palette. */
typedef enum {
cCross=0,
cSingleTile=1,
cExitdoor=4,
cBigPillar=7,
cSmallTiles=10,
cSmallCharacters=11,
aFeather=12,
aFlip=13,
aLoose=14,
aLife=15,
aPlaneVertical=16,
aPlaneHorizontal=19
} tEditorImageOffset;
typedef struct {
tile_packed_type tile,mask;
tEditorImageOffset res;
} ambi_type;
typedef union {
struct {
tile_packed_type tile,mask;
} by_mask;
Uint32 by_flags;
} match_type;
typedef struct {
char type,by,level;
match_type match;
tile_packed_type new_tile;
tile_packed_type new_mask;
} sani_c_tile_type;
typedef struct {
char type,by,x,y,null_is_true;
match_type match;
} sani_tile_type;
typedef union {
char type;
sani_c_tile_type center;
sani_tile_type adjacent;
} sani_type;
struct {
int ambi_count;
ambi_type ambi[50];
int sani_count;
int sani_alloc;
sani_type* sani;
} editor_tables;
void add_sani(sani_type s) {
if (!editor_tables.sani_alloc) {
editor_tables.sani_alloc=100;
editor_tables.sani=malloc(editor_tables.sani_alloc*sizeof(sani_type));
} else if (editor_tables.sani_count==editor_tables.sani_alloc) {
editor_tables.sani_alloc<<=1;
editor_tables.sani=realloc(editor_tables.sani,editor_tables.sani_alloc*sizeof(sani_type));
}
editor_tables.sani[editor_tables.sani_count++]=s;
}
int parse_match(const char* str,match_type* mt, char* by) {
int t1,t2,m1,m2;
if (sscanf(str,"%d.%d/%d.%d",&t1,&t2,&m1,&m2)) {
mt->by_mask.tile=TP_(t1,t2);
mt->by_mask.mask=TP_(m1,m2);
*by=0;
} else {
char str2[100];
if (sscanf(str,"(%[^)])",str2)) {
char* token;
char* init=str2;
mt->by_flags=0;
while((token = strtok(init,","))) {
mt->by_flags|=1<<atoi(token);
init=NULL;
}
*by=1;
} else {
printf("editor.ini matching error '%s'\n",str);
return 0;
}
}
return 1;
}
int ini_editor_callback(const char *section, const char *name, const char *value) {
if (!strcmp(section,"ambiguous")) {
int fg,bg,fgm,bgm;
int res,c;
c=editor_tables.ambi_count;
if (sscanf(value,"%d.%d/%d.%d %d",&fg,&bg,&fgm,&bgm,&res)) {
editor_tables.ambi[c].tile=TP_(fg,bg);
editor_tables.ambi[c].mask=TP_(fgm,bgm);
editor_tables.ambi[c].res=res-1;
editor_tables.ambi_count++;
} else {
printf("editor.ini: parsing error for %s: '%s'\n",name,value);
quit(0);
}
} else if (!strcmp(section,"sanitation")) {
if (!strcmp(name,"change")) { //change=<?match>([0-9]*.[0-9]*/[0-9]*.[0-9]*|\([0-9]*(,[0-9]*)*\)) <?replacement>([0-9]*.[0-9]*/[0-9]*.[0-9]*) <?level>[0-9]
char str[100];
int nt1,nt2,nm1,nm2,level;
if (sscanf(value,"%[^ ] %d.%d/%d.%d %d",str,&nt1,&nt2,&nm1,&nm2,&level)) {
sani_type aux;
aux.type=0; //central tile
if (!parse_match(str,&aux.center.match,&aux.center.by)) quit(0);
aux.center.level=level;
aux.center.new_tile=TP_(nt1,nt2);
aux.center.new_mask=TP_(nm1,nm2);
add_sani(aux);
} else {
printf("editor.ini: parsing error for %s: '%s'\n",name,value);
quit(0);
}
} else if (!strcmp(name,"match")) { //match=<?match>([0-9]*.[0-9]*/[0-9]*.[0-9]*|\([0-9]*(,[0-9]*)*\)) <?where>([udlr]*) <?does_null_match>[yn]
char str[100];
char str2[20];
char str3[4];
if (sscanf(value,"%[^ ] %[udlr] %[yn]",str,str2,str3)) {
sani_type aux;
aux.type=1; //adjacent tile
if (!parse_match(str,&aux.adjacent.match,&aux.adjacent.by)) quit(0);
Uint8 x=0,y=0;
const char* i=str2;
while (*i) switch(*(i++)){
case 'u':y--;break;
case 'd':y++;break;
case 'l':x--;break;
case 'r':x++;break;
default:printf("editor.ini: the code '%c' from '%s' is expected to be a,b,l or r\n",*(i-1),str2);quit(0);
}
aux.adjacent.x=x;
aux.adjacent.y=y;
aux.adjacent.null_is_true=str3[0]=='y';
add_sani(aux);
} else {
printf("editor.ini: parsing error for %s: '%s'\n",name,value);
quit(0);
}
}
}
return 1;
}
/********************************************\
* Editor functions *
\********************************************/
void dump_room(int room,byte* aux) {
int i;
int j=0;
#define dump_multiple_block(field,off,size) for (i=0;i<size;i++) aux[j++]=((byte*)&edited)[offset_(field)+(off)+i];
dump_multiple_block(fg,30*(room-1),30);
dump_multiple_block(bg,30*(room-1),30);
dump_multiple_block(roomlinks,sizeof(link_type)*(room-1),sizeof(link_type));
/*
dump_multiple_block(guards_tile,(room-1),1);
dump_multiple_block(guards_dir,(room-1),1);
dump_multiple_block(guards_x,(room-1),1);
dump_multiple_block(guards_skill,(room-1),1);
dump_multiple_block(guards_seq_hi,(room-1),1);
dump_multiple_block(guards_color,(room-1),1);
*/
}
void load_room(int room,const byte* aux) {
int i;
int j=0;
#define load_multiple_block(field,off,size) for (i=0;i<size;i++) editor__do_(offset_(field)+(off)+i,aux[j++],mark_middle);
load_multiple_block(fg,30*(room-1),30);
load_multiple_block(bg,30*(room-1),30);
load_multiple_block(roomlinks,sizeof(link_type)*(room-1),sizeof(link_type));
/* load_multiple_block(guards_tile,(room-1),1);
load_multiple_block(guards_dir,(room-1),1);
load_multiple_block(guards_x,(room-1),1);
load_multiple_block(guards_skill,(room-1),1);
load_multiple_block(guards_seq_hi,(room-1),1);
load_multiple_block(guards_color,(room-1),1);
*/
}
void printf_links() {
printf("debug\n");
for (int i=0;i<NUMBER_OF_ROOMS;i++) {
printf("S%d: L%d R%d A%d B%d\n",i+1,edited.roomlinks[i].left,edited.roomlinks[i].right,edited.roomlinks[i].up,edited.roomlinks[i].down);
}
}
void editor_swap_room_id(int r1, int r2) {
printf("SWAP: S%d WITH S%d %ld\n",r1,r2,sizeof(link_type));
editor__do_mark_start(flag_redraw);
byte dump_r1[70];
byte dump_r2[70];
dump_room(r1,dump_r1);
dump_room(r2,dump_r2);
load_room(r2,dump_r1);
load_room(r1,dump_r2);
//update room links
for (int i=0;i<NUMBER_OF_ROOMS*4;i++) {
byte l=i[(byte*)&edited.roomlinks];
if (l==r1) editor__do_(offset_(roomlinks)+i,r2,mark_middle);
if (l==r2) editor__do_(offset_(roomlinks)+i,r1,mark_middle);
}
//update start room
byte s=edited.start_room;
if (s==r1) editor__do(start_room,r2,mark_middle);
if (s==r2) editor__do(start_room,r1,mark_middle);
door_api_swap(&edited_doorlinks,r1,r2);
editor__do_mark_end(flag_redraw);
ed_select_room(r1);
ed_redraw_room();
ed_select_room(r2);
ed_redraw_room();
need_full_redraw=1;
//TODO: update doorlinks
}
chtab_type* chtab_editor_sprites=NULL;
void editor__load_level() {
dat_type* dathandle;
level=edited;
edition_level=current_level;
stack_reset();
selected_door_tile=-1;
remember_room=0;
dathandle = open_dat("editor", 0);
if (chtab_editor_sprites) { //Initialization
free_chtab(chtab_editor_sprites);
} else {
memset(&editor_tables,0,sizeof(editor_tables));
ini_load("data/editor/editor.ini", ini_editor_callback);
}
chtab_editor_sprites = load_sprites_from_file(200, 1<<11, 1);
close_dat(dathandle);
/* since chtab_editor_sprites is useful until quit(), I'll let the OS free_chtab(chtab_editor_sprites); */
}
#define copy_block(field,size,type) memcpy(&dst->field,&src->field,((size)*sizeof(type)))
#define level_copy_function(name,dst_type,src_type) \
void name(dst_type* dst, src_type* src) { \
copy_block(fg,NUMBER_OF_ROOMS * 30,byte); \
copy_block(bg,NUMBER_OF_ROOMS * 30,byte); \
copy_block(doorlinks1,256,byte); \
copy_block(doorlinks2,256,byte); \
copy_block(roomlinks,NUMBER_OF_ROOMS,link_type); \
copy_block(used_rooms,1,byte); \
copy_block(roomxs,NUMBER_OF_ROOMS,byte); \
copy_block(roomys,NUMBER_OF_ROOMS,byte); \
copy_block(fill_1,15,byte); \
copy_block(start_room,1,byte); \
copy_block(start_pos,1,byte); \
copy_block(start_dir,1,sbyte); \
copy_block(fill_2,4,byte); \
copy_block(guards_tile,NUMBER_OF_ROOMS,byte); \
copy_block(guards_dir,NUMBER_OF_ROOMS,byte); \
copy_block(guards_x,NUMBER_OF_ROOMS,byte); \
copy_block(guards_seq_lo,NUMBER_OF_ROOMS,byte); \
copy_block(guards_skill,NUMBER_OF_ROOMS,byte); \
copy_block(guards_seq_hi,NUMBER_OF_ROOMS,byte); \
copy_block(guards_color,NUMBER_OF_ROOMS,byte); \
copy_block(fill_3,18,byte); \
}
level_copy_function(editor__extend_level,level_type,level_real_type);
level_copy_function(editor__simplify_level,level_real_type,level_type);
level_copy_function(editor__extend_level_to_new_rooms,level_extended_type,level_type);
int load_resource(const char* file, int res, void* data, int size, const char* ext);
void save_resource(const char* file, int res, const void* data, int size, const char* ext);
void load_edit_palettes(level_type* level_to_load) {
load_resource("editor",100,&(level_to_load->fg[NUMBER_OF_ROOMS*30]),8*30, "bin");
load_resource("editor",101,&(level_to_load->bg[NUMBER_OF_ROOMS*30]),8*30, "bin");
if (!load_resource("editor",102,&(level_to_load->roomlinks[NUMBER_OF_ROOMS]),8*sizeof(link_type), "bin"))
for (int a=NUMBER_OF_ROOMS;a<NUMBER_OF_ROOMS+8;a++) {
level_to_load->roomlinks[a].left=(a==NUMBER_OF_ROOMS)?0:a-1+1;
level_to_load->roomlinks[a].right=(a==NUMBER_OF_ROOMS+7)?0:a+1+1;
level_to_load->roomlinks[a].up=(a<NUMBER_OF_ROOMS+4)?0:a-4+1;
level_to_load->roomlinks[a].down=(a>=NUMBER_OF_ROOMS+4)?0:a+4+1;
}
for (int i=NUMBER_OF_ROOMS;i<NUMBER_OF_ROOMS+8;i++)
level_to_load->guards_tile[i]=30;
}
void save_edit_palettes() {
save_resource("editor",100,&(edited.fg[NUMBER_OF_ROOMS*30]),8*30, "bin");
save_resource("editor",101,&(edited.bg[NUMBER_OF_ROOMS*30]),8*30, "bin");
save_resource("editor",102,&(edited.roomlinks[NUMBER_OF_ROOMS]),8*sizeof(link_type), "bin");
}
void editor__load_dat_file(level_type* l) {
level_real_type aux;
dat_type* dathandle;
dathandle = open_dat("LEVELS.DAT", 0);
load_from_opendats_to_area(current_level + 2000, &aux, sizeof(aux), "bin");
close_dat(dathandle);
/* set up extended level */
memset(l,0,sizeof(level_type));
editor__extend_level(l,&aux);
load_edit_palettes(l);
}
void editor__loading_dat() {
remember_room=0;
if (edition_level!=current_level) {
editor__load_dat_file(&edited);
editor__load_level();
room_api_free(&edited_map);
door_api_free(&edited_doorlinks);
room_api_init(&edited_map);
door_api_init(&edited_doorlinks);
} else {
level=edited;
}
}
void editor_revert_level() {
editor__load_dat_file(&edited);
editor__load_level();
level=edited;
room_api_free(&edited_map);
door_api_free(&edited_doorlinks);
room_api_init(&edited_map);
door_api_init(&edited_doorlinks);
}
void editor__position(char_type* character,int col,int row,int room,int x,word seq) {
character->curr_col=col;
character->curr_row=row;
character->room=room;
character->x=x;
character->y=55+63*row;
character->curr_seq=seq;
if (character->direction == dir_56_none) character->direction = dir_0_right;
}
void editor__paste_room(int room) {
int i;
editor__do_mark_start(flag_redraw);
for (i=0;i<30;i++)
if (clipboard[i].number!=NO_TILE.number)
editor_change_tile(T(drawn_room,i+((clipboard_has==chTiles)?clipboard_shift:0)),clipboard[i]);
door_api_refresh(&edited_doorlinks,&edited_map,&selected_door_tile);
editor__do_mark_end(flag_redraw);
}
void save_resource(const char* file, int res, const void* data, int size, const char* ext) {
char aux[255];
FILE* fp;
snprintf(aux,255,"data/%s/res%d.%s",file,res,ext);
fp=fopen(aux,"wb");
if (fp) {
fwrite(data,size,1,fp);
fclose(fp);
} else {
printf("error opening '%s'\n",aux);
}
}
int load_resource(const char* file, int res, void* data, int size, const char* ext) {
char aux[255];
FILE* fp;
snprintf(aux,255,"data/%s/res%d.%s",file,res,ext);
fp=fopen(aux,"rb");
if (fp) {
fread(data,size,1,fp);
fclose(fp);
return 1;
} else {
printf("error opening '%s'\n",aux);
return 0;
}
}
void save_level() {
level_real_type aux;
editor__simplify_level(&aux,&edited);
save_resource("LEVELS.DAT",current_level + 2000, &aux, sizeof(aux), "bin");
}
void save_extended_level() {
level_extended_type aux;
memset(&aux,0,sizeof(aux));
for (int i=NUMBER_OF_ROOMS;i<NEW_NUMBER_OF_ROOMS;i++)
aux.guards_tile[i]=30;
editor__extend_level_to_new_rooms(&aux,&edited);
save_resource("LEVELS.DAT",current_level + 3000, &aux, sizeof(aux), "bin");
}
void editor__set_guard(byte tilepos,byte x) {
if (level.guards_tile[loaded_room-1]>=30) { //if not present: create
editor__do(guards_tile[loaded_room-1],tilepos,mark_start|flag_guard_presence);
editor__do(guards_color[loaded_room-1],1,mark_middle);
editor__do(guards_x[loaded_room-1],x,mark_middle);
editor__do(guards_dir[loaded_room-1],0,mark_middle);
editor__do(guards_seq_hi[loaded_room-1],0,mark_middle);
editor__do(guards_skill[loaded_room-1],0,mark_end|flag_guard_presence);
} else { //if present move
editor__do(guards_tile[loaded_room-1],tilepos,mark_start|flag_guard_presence);
editor__do(guards_x[loaded_room-1],x,mark_end|flag_guard_presence);
}
enter_guard(); // load color, HP
}