/
run_robot.c
6050 lines (5207 loc) · 167 KB
/
run_robot.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
/* MegaZeux
*
* Copyright (C) 1996 Greg Janson
* Copyright (C) 2004 Gilead Kutnick <exophase@adelphia.net>
* Copyright (C) 2017 Alice Rowan <petrifiedrowan@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "block.h"
#include "board.h"
#include "const.h"
#include "core.h"
#include "counter.h"
#include "data.h"
#include "error.h"
#include "event.h"
#include "expr.h"
#include "extmem.h"
#include "game.h"
#include "game_ops.h"
#include "game_player.h"
#include "graphics.h"
#include "idarray.h"
#include "idput.h"
#include "intake.h"
#include "mzm.h"
#include "robot.h"
#include "scrdisp.h"
#include "sprite.h"
#include "str.h"
#include "util.h"
#include "window.h"
#include "world.h"
#include "io/fsafeopen.h"
#include "audio/audio.h"
#include "audio/sfx.h"
#define parsedir(a, b, c, d) \
parsedir(mzx_world, a, b, c, d, _bl[0], _bl[1], _bl[2], _bl[3])
static const char *const item_to_counter[9] =
{
"GEMS",
"AMMO",
"TIME",
"SCORE",
"HEALTH",
"LIVES",
"LOBOMBS",
"HIBOMBS",
"COINS"
};
// Default parameters (-2 = 0 and no edit, -3 = character, -4 = board)
__editor_maybe_static const int def_params[128] =
{
-2, -2, -2, -2, -2, -3, -2, -3, -2, -2, -3, -2, -3, -2, -2, -2, // 0x00 - 0x0F
-2, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, 0, -2, -2, 10, 0, // 0x10 - 0x1F
0, -2, -2, 5, 0,128, 32, -2, -2, 0, -2, -4, -4, -2, -2, 0, // 0x20 - 0x2F
-2, 0, -2, -3, -3, -3, -3, 17, 0, -2, -2, -2, 0, 4, 0, -2, // 0x30 - 0x3F
-2, -2, -2, -4, -4, -4, -4, -2, 0, -2, 48, 0, -3, -3, 0,127, // 0x40 - 0x4F
32, 26, 2, 1, 0, 2, 65, 2, 66, 2,129, 34, 66, 66, 12, 10, // 0x50 - 0x5F
-2,194, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, // 0x60 - 0x6F
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 0, 0, 0, 0, 0, -2 // 0x70 - 0x7F
};
static void magic_load_mod(struct world *mzx_world, char *filename)
{
struct board *src_board = mzx_world->current_board;
size_t mod_name_size;
if(load_game_module(mzx_world, filename, false))
strcpy(src_board->mod_playing, filename);
// If a * was provided, set the current board mod to *.
mod_name_size = strlen(filename);
if(mod_name_size && filename[mod_name_size - 1] == '*')
{
strcpy(src_board->mod_playing, "*");
}
}
static void save_player_position(struct world *mzx_world, int pos)
{
mzx_world->pl_saved_x[pos] = mzx_world->player_x;
mzx_world->pl_saved_y[pos] = mzx_world->player_y;
mzx_world->pl_saved_board[pos] = mzx_world->current_board_id;
}
static void restore_player_position(struct world *mzx_world, int pos)
{
mzx_world->target_x = mzx_world->pl_saved_x[pos];
mzx_world->target_y = mzx_world->pl_saved_y[pos];
mzx_world->target_board = mzx_world->pl_saved_board[pos];
mzx_world->target_where = TARGET_POSITION;
}
static void calculate_blocked(struct world *mzx_world, int x, int y, int id,
int bl[4])
{
struct board *src_board = mzx_world->current_board;
int board_width = src_board->board_width;
char *level_id = src_board->level_id;
if(id)
{
int i, offset, new_x, new_y;
for(i = 0; i < 4; i++)
{
new_x = x;
new_y = y;
if(!move_dir(src_board, &new_x, &new_y, int_to_dir(i)))
{
offset = new_x + (new_y * board_width);
// Not edge... blocked?
if((flags[(int)level_id[offset]] & A_UNDER) != A_UNDER)
bl[i] = 1;
else
bl[i] = 0;
}
else
{
bl[i] = 1; // Edge is considered blocked
}
}
}
}
// Turns a color (including those w/??) to a real color (0-255)
static int fix_color(int color, int def)
{
if(color < 256)
return color;
if(color < 272)
return (color & 0x0F) + (def & 0xF0);
if(color < 288)
return ((color - 272) << 4) + (def & 0x0F);
return def;
}
int place_at_xy(struct world *mzx_world, enum thing id, int color, int param,
int x, int y)
{
struct board *src_board = mzx_world->current_board;
int board_width = src_board->board_width;
int offset = x + (y * board_width);
enum thing old_id = (enum thing)src_board->level_id[offset];
// Okay, I'm really stabbing at behavior here.
// Wildcard param, use source but only if id matches and isn't
// greater than or equal to 122..
if(param == 256)
{
// The param must represent the char for this to happen,
// nothing else will give.
if((id_chars[old_id] != 255) || (id_chars[id] != 255))
{
if(def_params[id] > 0)
param = def_params[id];
else
param = 0;
}
else
{
param = src_board->level_param[offset];
}
}
if(old_id == SENSOR)
{
// Okay to put player on sensor; it won't destroy the sensor
if(id != PLAYER)
{
int old_param = src_board->level_param[offset];
clear_sensor_id(src_board, old_param);
}
}
else
if(is_signscroll(old_id))
{
int old_param = src_board->level_param[offset];
clear_scroll_id(src_board, old_param);
}
else
if(is_robot(old_id))
{
int old_param = src_board->level_param[offset];
clear_robot_id(src_board, old_param);
}
else
if(old_id == PLAYER)
{
return 0; // Don't allow the player to be overwritten
}
if(param == 256)
{
param = src_board->level_param[offset];
}
id_place(mzx_world, x, y, id,
fix_color(color, src_board->level_color[offset]), param);
return 1;
}
static int place_under_xy(struct board *src_board, enum thing id, int color,
int param, int x, int y)
{
int board_width = src_board->board_width;
char *level_under_id = src_board->level_under_id;
char *level_under_color = src_board->level_under_color;
char *level_under_param = src_board->level_under_param;
int offset = x + (y * board_width);
if(param == 256)
param = level_under_param[offset];
color = fix_color(color, level_under_color[offset]);
if(level_under_id[offset] == SENSOR)
clear_sensor_id(src_board, level_under_param[offset]);
level_under_id[offset] = (char)id;
level_under_color[offset] = color;
level_under_param[offset] = param;
return 1;
}
static int place_dir_xy(struct world *mzx_world, enum thing id, int color,
int param, int x, int y, enum dir direction, struct robot *cur_robot,
int *_bl)
{
struct board *src_board = mzx_world->current_board;
// Check under
if(direction == BENEATH)
{
return place_under_xy(src_board, id, color, param, x, y);
}
else
{
int new_x = x;
int new_y = y;
direction = parsedir(direction, x, y, cur_robot->walk_dir);
if(is_cardinal_dir(direction))
{
if(!move_dir(src_board, &new_x, &new_y, direction))
{
return place_at_xy(mzx_world, id, color, param,
new_x, new_y);
}
}
return 0;
}
}
int place_player_xy(struct world *mzx_world, int x, int y)
{
if((mzx_world->player_x != x) || (mzx_world->player_y != y))
{
struct board *src_board = mzx_world->current_board;
int offset = x + (y * src_board->board_width);
int did = src_board->level_id[offset];
int dparam = src_board->level_param[offset];
if(is_robot(did))
{
clear_robot_id(src_board, dparam);
}
else
if(is_signscroll(did))
{
clear_scroll_id(src_board, dparam);
}
else
if(did == SENSOR)
{
step_sensor(mzx_world, dparam);
}
id_remove_top(mzx_world, mzx_world->player_x, mzx_world->player_y);
id_place(mzx_world, x, y, PLAYER, 0, 0);
mzx_world->player_x = x;
mzx_world->player_y = y;
return 1;
}
return 0;
}
static void send_at_xy(struct world *mzx_world, int id, int x, int y,
char *label)
{
struct board *src_board = mzx_world->current_board;
int offset = x + (y * src_board->board_width);
enum thing d_id = (enum thing)src_board->level_id[offset];
if(is_robot(d_id))
{
char label_buffer[ROBOT_MAX_TR];
int d_param = src_board->level_param[offset];
tr_msg(mzx_world, label, id, label_buffer);
if(d_param == id)
{
send_robot_self(mzx_world,
mzx_world->current_board->robot_list[id], label_buffer, 0);
}
else
{
send_robot_id(mzx_world, src_board->level_param[offset],
label_buffer, 0);
}
}
}
static int get_random_range(int min_value, int max_value)
{
int result;
unsigned int difference;
if(min_value == max_value)
{
result = min_value;
}
else
{
if(max_value > min_value)
{
difference = max_value - min_value;
}
else
{
difference = min_value - max_value;
min_value = max_value;
}
result = Random((unsigned long long)difference + 1) + min_value;
}
return result;
}
static int send_self_label_tr(struct world *mzx_world, char *param, int id)
{
char label_buffer[ROBOT_MAX_TR];
tr_msg(mzx_world, param, id, label_buffer);
if(send_robot_self(mzx_world,
mzx_world->current_board->robot_list[id], label_buffer, 1))
{
return 0;
}
else
{
return 1;
}
}
static void split_colors(int color, int *fg, int *bg)
{
if(color & 256)
{
color ^= 256;
if(color == 32)
{
*fg = 16;
*bg = 16;
}
else
if(color < 16)
{
*bg = 16;
*fg = color;
}
else
{
*bg = color - 16;
*fg = 16;
}
}
else
{
*bg = color >> 4;
*fg = color & 0x0F;
}
}
static int check_at_xy(struct board *src_board, enum thing id, int fg, int bg,
int param, int offset)
{
enum thing d_id = (enum thing)src_board->level_id[offset];
int dcolor = src_board->level_color[offset];
int dparam = src_board->level_param[offset];
// Whirlpool matches down
if(is_whirlpool(d_id))
d_id = WHIRLPOOL_1;
if((d_id == id) && (((dcolor & 0x0F) == fg) || (fg == 16)) &&
(((dcolor >> 4) == bg) || (bg == 16)) &&
((dparam == param) || (param == 256)))
{
return 1;
}
return 0;
}
static int check_under_xy(struct board *src_board, enum thing id,
int fg, int bg, int param, int offset)
{
enum thing did = (enum thing)src_board->level_under_id[offset];
int dcolor = src_board->level_under_color[offset];
int dparam = src_board->level_under_param[offset];
if((did == id) && (((dcolor & 0x0F) == fg) || (fg == 16)) &&
(((dcolor >> 4) == bg) || (bg == 16)) &&
((dparam == param) || (param == 256)))
return 1;
return 0;
}
static int check_dir_xy(struct world *mzx_world, enum thing id, int color,
int param, int x, int y, enum dir direction, struct robot *cur_robot,
int *_bl)
{
struct board *src_board = mzx_world->current_board;
int board_width = src_board->board_width;
int fg, bg;
int offset;
split_colors(color, &fg, &bg);
// Check under
if(direction == BENEATH)
{
offset = x + (y * board_width);
if(check_under_xy(src_board, id, fg, bg, param, offset))
return 1;
return 0;
}
else
{
int new_x = x;
int new_y = y;
if((direction & 0x1F) == SEEK)
{
get_robot_position(cur_robot, &x, &y);
}
direction = parsedir(direction, x, y, cur_robot->walk_dir);
if(!move_dir(src_board, &new_x, &new_y, direction))
{
offset = new_x + (new_y * board_width);
if(check_at_xy(src_board, id, fg, bg, param, offset))
return 1;
}
return 0;
}
}
static void copy_xy_to_xy(struct world *mzx_world, int src_x, int src_y,
int dest_x, int dest_y)
{
struct board *src_board = mzx_world->current_board;
int board_width = src_board->board_width;
int src_offset = src_x + (src_y * board_width);
int dest_offset = dest_x + (dest_y * board_width);
enum thing src_id = (enum thing)src_board->level_id[src_offset];
enum thing dest_id = (enum thing)src_board->level_id[dest_offset];
// Cannot copy from the player or onto the player. Check for the player at
// the destination now so this doesn't pointlessly waste storage object IDs.
if((src_id != PLAYER) && (dest_id != PLAYER))
{
int src_param = src_board->level_param[src_offset];
if(is_robot(src_id))
{
struct robot *src_robot = src_board->robot_list[src_param];
src_param = duplicate_robot(mzx_world, src_board, src_robot,
dest_x, dest_y, 1);
}
else
if(is_signscroll(src_id))
{
struct scroll *src_scroll = src_board->scroll_list[src_param];
src_param = duplicate_scroll(src_board, src_scroll);
}
else
if(src_id == SENSOR)
{
struct sensor *src_sensor = src_board->sensor_list[src_param];
src_param = duplicate_sensor(src_board, src_sensor);
}
// Now perform the copy; this should perform any necessary
// deletions at the destination as well.
if(src_param != -1)
{
place_at_xy(mzx_world, src_id,
src_board->level_color[src_offset], src_param, dest_x, dest_y);
}
}
}
void setup_overlay(struct board *src_board, int mode)
{
if(!mode && src_board->overlay_mode)
{
// Deallocate overlay
free(src_board->overlay);
free(src_board->overlay_color);
}
if(!src_board->overlay_mode && mode)
{
int board_size = src_board->board_width * src_board->board_height;
// Allocate an overlay
src_board->overlay = cmalloc(board_size);
src_board->overlay_color = cmalloc(board_size);
memset(src_board->overlay, 32, board_size);
memset(src_board->overlay_color, 7, board_size);
}
src_board->overlay_mode = mode;
}
static void copy_block(struct world *mzx_world, int id, int x, int y,
int src_type, int dest_type, int src_x, int src_y, int width, int height,
int dest_x, int dest_y, char *dest, int dest_param,
char dest_name_buffer[ROBOT_MAX_TR])
{
struct board *src_board = mzx_world->current_board;
int src_width = src_board->board_width;
int src_height = src_board->board_height;
int dest_width = src_board->board_width;
int dest_height = src_board->board_height;
int src_offset;
int dest_offset;
// Process for source type and handle prefixing
switch(src_type)
{
case 2:
{
src_width = mzx_world->vlayer_width;
src_height = mzx_world->vlayer_height;
break;
}
case 1:
{
if(!src_board->overlay_mode)
setup_overlay(src_board, 3);
}
}
prefix_first_xy_var(mzx_world, &src_x, &src_y, x, y,
src_width, src_height);
// Clip and verify; the prefixer already handled the
// base coordinates, so just handle the width/height
if(width < 1)
width = 1;
if(height < 1)
height = 1;
if((src_x + width) > src_width)
width = src_width - src_x;
if((src_y + height) > src_height)
height = src_height - src_y;
// Get the offset that will generally be used.
src_offset = src_x + (src_y * src_width);
// Process for dest type and handle prefixing if we aren't already done
switch(dest_type)
{
//string - dest_param is the terminator
case 3:
{
switch(src_type)
{
case 0:
{
// Board to string
load_string_board(mzx_world, dest_name_buffer,
src_board->level_param + src_offset, src_width,
width, height, dest_param);
break;
}
case 1:
{
// Overlay to string
load_string_board(mzx_world, dest_name_buffer,
src_board->overlay + src_offset, src_width,
width, height, dest_param);
break;
}
case 2:
{
// Vlayer to string
load_string_board(mzx_world, dest_name_buffer,
mzx_world->vlayer_chars + src_offset, src_width,
width, height, dest_param);
break;
}
}
return;
}
//mzm - if dest_param is !=0, the board is saved like a layer.
case 4:
{
char *translated_name = cmalloc(MAX_PATH);
int err;
switch(src_type)
{
case 0:
if(dest_param)
src_type = MZM_BOARD_TO_LAYER_STORAGE;
else
src_type = MZM_BOARD_TO_BOARD_STORAGE;
break;
case 1:
src_type = MZM_OVERLAY_TO_LAYER_STORAGE;
break;
case 2:
src_type = MZM_VLAYER_TO_LAYER_STORAGE;
break;
}
// Save MZM to string (2.90+)
if(mzx_world->version >= V290 && is_string(dest_name_buffer))
{
save_mzm_string(mzx_world, dest_name_buffer, src_x, src_y,
width, height, src_type, 1, id);
}
// Save MZM to file
else
{
err = fsafetranslate(dest_name_buffer, translated_name);
if(err == -FSAFE_SUCCESS || err == -FSAFE_MATCH_FAILED)
{
save_mzm(mzx_world, translated_name, src_x, src_y,
width, height, src_type, 1);
}
}
free(translated_name);
return;
}
case 2:
{
dest_width = mzx_world->vlayer_width;
dest_height = mzx_world->vlayer_height;
break;
}
case 1:
{
if(!src_board->overlay_mode)
setup_overlay(src_board, 3);
}
}
prefix_last_xy_var(mzx_world, &dest_x, &dest_y, x, y,
dest_width, dest_height);
// Clip to destination dimensions as well
if((dest_x + width) > dest_width)
width = dest_width - dest_x;
if((dest_y + height) > dest_height)
height = dest_height - dest_y;
// Get the offset that will generally be used.
dest_offset = dest_x + (dest_y * dest_width);
switch((dest_type << 2) | (src_type))
{
// Board to board
case 0:
{
copy_board_to_board(mzx_world,
src_board, src_offset,
src_board, dest_offset,
width, height);
break;
}
// Overlay to board
case 1:
{
copy_layer_to_board(
src_board->overlay, src_board->overlay_color, src_width, src_offset,
src_board, dest_offset,
width, height, CUSTOM_BLOCK);
break;
}
// Vlayer to board
case 2:
{
copy_layer_to_board(
mzx_world->vlayer_chars, mzx_world->vlayer_colors, src_width, src_offset,
src_board, dest_offset,
width, height, CUSTOM_BLOCK);
break;
}
// Board to overlay
case 4:
{
copy_board_to_layer(
src_board, src_offset,
src_board->overlay, src_board->overlay_color, dest_width, dest_offset,
width, height);
break;
}
// Overlay to overlay
case 5:
{
copy_layer_to_layer(
src_board->overlay, src_board->overlay_color, src_width, src_offset,
src_board->overlay, src_board->overlay_color, dest_width, dest_offset,
width, height);
break;
}
// Vlayer to overlay
case 6:
{
copy_layer_to_layer(
mzx_world->vlayer_chars, mzx_world->vlayer_colors, src_width, src_offset,
src_board->overlay, src_board->overlay_color, dest_width, dest_offset,
width, height);
break;
}
// Board to vlayer
case 8:
{
copy_board_to_layer(
src_board, src_offset,
mzx_world->vlayer_chars, mzx_world->vlayer_colors, dest_width, dest_offset,
width, height);
break;
}
// Overlay to vlayer
case 9:
{
copy_layer_to_layer(
src_board->overlay, src_board->overlay_color, src_width, src_offset,
mzx_world->vlayer_chars, mzx_world->vlayer_colors, dest_width, dest_offset,
width, height);
break;
}
// Vlayer to vlayer
case 10:
{
copy_layer_to_layer(
mzx_world->vlayer_chars, mzx_world->vlayer_colors, src_width, src_offset,
mzx_world->vlayer_chars, mzx_world->vlayer_colors, dest_width, dest_offset,
width, height);
break;
}
}
}
static void copy_xy_to_xy_wrapper(struct world *mzx_world, int id, int x, int y,
int src_type, int dest_type, int src_x, int src_y, int dest_x, int dest_y)
{
struct board *cur_board = mzx_world->current_board;
int board_width = cur_board->board_width;
int board_height = cur_board->board_height;
int vlayer_width = mzx_world->vlayer_width;
int vlayer_height = mzx_world->vlayer_height;
int src_offset;
int dest_offset;
switch(src_type | (dest_type << 4))
{
// Board-to-board. Use the original implementation...
case 0x00:
prefix_first_last_xy(mzx_world, &src_x, &src_y, &dest_x, &dest_y, x, y);
copy_xy_to_xy(mzx_world, src_x, src_y, dest_x, dest_y);
break;
// Overlay-to-overlay.
case 0x11:
if(!cur_board->overlay_mode)
setup_overlay(cur_board, 3);
prefix_first_last_xy(mzx_world, &src_x, &src_y, &dest_x, &dest_y, x, y);
src_offset = src_y * board_width + src_x;
dest_offset = dest_y * board_width + dest_x;
cur_board->overlay[dest_offset] = cur_board->overlay[src_offset];
cur_board->overlay_color[dest_offset] = cur_board->overlay_color[src_offset];
break;
// Vlayer-to-overlay.
case 0x12:
if(!cur_board->overlay_mode)
setup_overlay(cur_board, 3);
prefix_first_xy_var(mzx_world, &src_x, &src_y, x, y,
vlayer_width, vlayer_height);
prefix_last_xy_var(mzx_world, &dest_x, &dest_y, x, y,
board_width, board_height);
src_offset = src_y * vlayer_width + src_x;
dest_offset = dest_y * board_width + dest_x;
cur_board->overlay[dest_offset] = mzx_world->vlayer_chars[src_offset];
cur_board->overlay_color[dest_offset] = mzx_world->vlayer_colors[src_offset];
break;
// Overlay-to-vlayer.
case 0x21:
if(!cur_board->overlay_mode)
setup_overlay(cur_board, 3);
prefix_first_xy_var(mzx_world, &src_x, &src_y, x, y,
board_width, board_height);
prefix_last_xy_var(mzx_world, &dest_x, &dest_y, x, y,
vlayer_width, vlayer_height);
src_offset = src_y * board_width + src_x;
dest_offset = dest_y * vlayer_width + dest_x;
mzx_world->vlayer_chars[dest_offset] = cur_board->overlay[src_offset];
mzx_world->vlayer_colors[dest_offset] = cur_board->overlay_color[src_offset];
break;
// Vlayer-to-vlayer.
case 0x22:
prefix_first_xy_var(mzx_world, &src_x, &src_y, x, y,
vlayer_width, vlayer_height);
prefix_last_xy_var(mzx_world, &dest_x, &dest_y, x, y,
vlayer_width, vlayer_height);
src_offset = src_y * vlayer_width + src_x;
dest_offset = dest_y * vlayer_width + dest_x;
mzx_world->vlayer_chars[dest_offset] = mzx_world->vlayer_chars[src_offset];
mzx_world->vlayer_colors[dest_offset] = mzx_world->vlayer_colors[src_offset];
break;
// All others should just use copy_block.
case 0x01:
case 0x02:
case 0x10:
case 0x20:
copy_block(mzx_world, id, x, y, src_type, dest_type, src_x, src_y, 1, 1,
dest_x, dest_y, NULL, 0, NULL);
break;
}
}
// Gets the type for a single copy block param.
static int copy_block_param(struct world *mzx_world, int id, char *param,
int *coord)
{
int type = 0;
if(*param)
{
if(*(param + 1) == '+')
{
type = 1;
}
else
if(*(param + 1) == '#')
{
type = 2;
}
}
if(type == 0)
{
*coord = parse_param(mzx_world, param, id);
}
else
if((type == 1) || (type == 2))
{
char src_char_buffer[ROBOT_MAX_TR];
tr_msg(mzx_world, param + 2, id, src_char_buffer);
*coord = strtol(src_char_buffer, NULL, 10);
}
return type;
}
// Gets the type for the X coord of the copy block destination.
// This argument has two special cases: it can be a $string (after tr_msg) or
// an MZM (always prefixed with @). These cases need to also return the name
// of their destination string/MZM.
static int copy_block_param_special(struct world *mzx_world, int id,
char *param, int *coord, char dest_name_buffer[ROBOT_MAX_TR])
{
char first = *(param + 1);
if(!(*param) || first == '(' || first == '+' || first == '#')
return copy_block_param(mzx_world, id, param, coord);
if(first == '@')
{
tr_msg(mzx_world, param + 2, id, dest_name_buffer);
return 4;
}
tr_msg(mzx_world, param + 1, id, dest_name_buffer);
if(is_string(dest_name_buffer))
return 3;
*coord = get_counter(mzx_world, dest_name_buffer, id);
return 0;
}
void replace_player(struct world *mzx_world)
{
struct board *src_board = mzx_world->current_board;
char *level_id = src_board->level_id;
int board_width = src_board->board_width;
int board_height = src_board->board_height;
int dx, dy, offset;
for(dy = 0, offset = 0; dy < board_height; dy++)
{
for(dx = 0; dx < board_width; dx++, offset++)
{
if(A_UNDER & flags[(int)level_id[offset]])
{
// Place the player here
mzx_world->player_x = dx;
mzx_world->player_y = dy;
id_place(mzx_world, dx, dy, PLAYER, 0, 0);
return;
}
}
}
// Place the player here
mzx_world->player_x = 0;
mzx_world->player_y = 0;
place_at_xy(mzx_world, PLAYER, 0, 0, 0, 0);
}
// Returns the numeric value pointed to OR the numeric value represented
// by the counter string pointed to. (the ptr is at the param within the
// command)
// Sign extends the result, for now...
int parse_param(struct world *mzx_world, char *program, int id)
{
char ibuff[ROBOT_MAX_TR];
if(program[0] == 0)
{
// Numeric