summaryrefslogtreecommitdiff
path: root/src/map/magic-expr.c
blob: 483a9eeaf3a7d55995ca6655357d6084c60292d2 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
#include "magic-expr.h"
#include "magic-expr-eval.h"
#include "itemdb.h"
#include <math.h>

#define IS_SOLID(c) ((c) == 1 || (c) == 5)

int
map_is_solid(int m, int x, int y)
{
    return (IS_SOLID(map_getcell(m, x, y)));
}

#undef IS_SOLID

static void
free_area(area_t *area)
{
        if (!area)
                return;

        switch (area->ty) {
        case AREA_UNION:
                free_area(area->a.a_union[0]);
                free_area(area->a.a_union[1]);
                break;
        default: break;
        }

        free(area);
}

static area_t *
dup_area(area_t *area)
{
        area_t *retval = malloc(sizeof(area_t));
        *retval = *area;

        switch (area->ty) {
        case AREA_UNION:
                retval->a.a_union[0] = dup_area(retval->a.a_union[0]);
                retval->a.a_union[1] = dup_area(retval->a.a_union[1]);
                break;
        default: break;
        }

        return retval;
}

void
magic_copy_var(val_t *dest, val_t *src)
{
        *dest = *src;

        switch (dest->ty) {
        case TY_STRING: dest->v.v_string = strdup(dest->v.v_string);
                break;
        case TY_AREA: dest->v.v_area = dup_area(dest->v.v_area);
                break;
        default: break;
        }
        
}

void
magic_clear_var(val_t *v)
{
        switch (v->ty) {
        case TY_STRING: free(v->v.v_string);
                break;
        case TY_AREA: free_area(v->v.v_area);
                break;
        default: break;
        }
}

static char *
show_entity(entity_t *entity)
{
        switch (entity->type) {
        case BL_PC:
                return ((struct map_session_data *)entity)->status.name;
        case BL_NPC:
                return ((struct npc_data *)entity)->name;
        case BL_MOB:
                return ((struct mob_data *)entity)->name;
        case BL_ITEM:
                /* Sorry about this one... */
                return ((struct item_data *)(&((struct flooritem_data *)entity)->item_data))->name;
        case BL_SKILL:
                return "%skill";
        case BL_PET:
                return ((struct pet_data *)entity)->name;
        case BL_SPELL:
                return "%invocation(ERROR:this-should-not-be-an-entity)";
        default:
                return "%unknown-entity";
        }
}

static void
stringify(val_t *v, int within_op)
{
        static char *dirs[8] = {"S", "SW", "W", "NW", "N", "NE", "E", "SE"};
        char *buf;

        switch (v->ty) {
        case TY_UNDEF:
                buf = strdup("UNDEF");
                break;

        case TY_INT:
                buf = malloc(32);
                sprintf(buf, "%i", v->v.v_int);
                break;

        case TY_STRING:
                return;

        case TY_DIR:
                buf = strdup(dirs[v->v.v_int]);
                break;

        case TY_ENTITY:
                buf = strdup(show_entity(v->v.v_entity));
                break;

        case TY_LOCATION:
                buf = malloc(128);
                sprintf(buf, "<\"%s\", %d, %d>", map[v->v.v_location.m].name,
                        v->v.v_location.x, v->v.v_location.y);
                break;

        case TY_AREA:
                buf = strdup("%area");
                free_area(v->v.v_area);
                break;

        case TY_SPELL:
                buf = strdup(v->v.v_spell->name);
                break;

        case TY_INVOCATION: {
                invocation_t *invocation = within_op
                                         ? v->v.v_invocation
                                         : (invocation_t *) map_id2bl(v->v.v_int);
                buf = strdup(invocation->spell->name);
        }
                break;

        default:
                fprintf(stderr, "[magic] INTERNAL ERROR: Cannot stringify %d\n", v->ty);
                return;
        }

        v->v.v_string = buf;
        v->ty = TY_STRING;
}

static void
intify(val_t *v)
{
        if (v->ty == TY_INT)
                return;

        magic_clear_var(v);
        v->ty = TY_INT;
        v->v.v_int = 1;
}


area_t *
area_new(int ty)
{
        area_t *retval = (area_t *)aCalloc(sizeof(area_t), 1);
        retval->ty = ty;
        return retval;
}

area_t *
area_union(area_t *area, area_t *other_area)
{
        area_t *retval = area_new(AREA_UNION);
        retval->a.a_union[0] = area;
        retval->a.a_union[1] = other_area;
        retval->size = area->size + other_area->size; /* Assume no overlap */
        return retval;
}

/**
 * Turns location into area, leaves other types untouched
 */
static void
make_area(val_t *v)
{
        if (v->ty == TY_LOCATION) {
                area_t *a = malloc(sizeof (area_t));
                v->ty = TY_AREA;
                a->ty = AREA_LOCATION;
                a->a.a_loc = v->v.v_location;
                v->v.v_area = a;
        }
}

static void
make_location(val_t *v)
{
        if (v->ty == TY_AREA &&
            v->v.v_area->ty == AREA_LOCATION) {
                location_t location = v->v.v_area->a.a_loc;
                free_area(v->v.v_area);
                v->ty = TY_LOCATION;
                v->v.v_location = location;
        }
}

static void
make_spell(val_t *v)
{
        if (v->ty == TY_INVOCATION) {
                invocation_t *invoc = v->v.v_invocation; //(invocation_t *) map_id2bl(v->v.v_int);
                if (!invoc)
                        v->ty = TY_FAIL;
                else {
                        v->ty = TY_SPELL;
                        v->v.v_spell = invoc->spell;
                }
        }
}

static int
fun_add(env_t *env, int args_nr, val_t *result, val_t *args)
{
        if (TY(0) == TY_INT  && TY(1) == TY_INT) {
                /* Integer addition */
                RESULTINT = ARGINT(0) + ARGINT(1);
                result->ty = TY_INT;
        } else if (ARG_MAY_BE_AREA(0) && ARG_MAY_BE_AREA(1)) {
                /* Area union */
                make_area(&args[0]);
                make_area(&args[1]);
                RESULTAREA = area_union(ARGAREA(0), ARGAREA(1));
                ARGAREA(0) = NULL;
                ARGAREA(1) = NULL;
                result->ty = TY_AREA;
        } else {
                /* Anything else -> string concatenation */
                stringify(&args[0], 1);
                stringify(&args[1], 1);
                /* Yes, we could speed this up. */
                RESULTSTR = (char *) malloc(1 + strlen(ARGSTR(0)) + strlen(ARGSTR(1)));
                strcpy(RESULTSTR, ARGSTR(0));
                strcat(RESULTSTR, ARGSTR(1));
                result->ty = TY_STRING;
        }
        return 0;
}

static int
fun_sub(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = ARGINT(0) - ARGINT(1);
        return 0;
}


static int
fun_mul(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = ARGINT(0) * ARGINT(1);
        return 0;
}


static int
fun_div(env_t *env, int args_nr, val_t *result, val_t *args)
{
        if (!ARGINT(1))
                return 1; /* division by zero */
        RESULTINT = ARGINT(0) / ARGINT(1);
        return 0;
}

static int
fun_mod(env_t *env, int args_nr, val_t *result, val_t *args)
{
        if (!ARGINT(1))
                return 1; /* division by zero */
        RESULTINT = ARGINT(0) % ARGINT(1);
        return 0;
}

static int
fun_or(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = ARGINT(0) || ARGINT(1);
        return 0;
}

static int
fun_and(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = ARGINT(0) && ARGINT(1);
        return 0;
}

static int
fun_not(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = !ARGINT(0);
        return 0;
}

static int
fun_neg(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = ~ARGINT(0);
        return 0;
}

static int
fun_gte(env_t *env, int args_nr, val_t *result, val_t *args)
{
        if (TY(0) == TY_STRING || TY(1) == TY_STRING) {
                stringify(&args[0], 1);
                stringify(&args[1], 1);
                RESULTINT = strcmp(ARGSTR(0), ARGSTR(1)) >= 0;
        } else {
                intify(&args[0]);
                intify(&args[1]);
                RESULTINT = ARGINT(0) >= ARGINT(1);
        }
        return 0;
}

static int
fun_gt(env_t *env, int args_nr, val_t *result, val_t *args)
{
        if (TY(0) == TY_STRING || TY(1) == TY_STRING) {
                stringify(&args[0], 1);
                stringify(&args[1], 1);
                RESULTINT = strcmp(ARGSTR(0), ARGSTR(1)) > 0;
        } else {
                intify(&args[0]);
                intify(&args[1]);
                RESULTINT = ARGINT(0) > ARGINT(1);
        }
        return 0;
}


static int
fun_eq(env_t *env, int args_nr, val_t *result, val_t *args)
{
        if (TY(0) == TY_STRING || TY(1) == TY_STRING) {
                stringify(&args[0], 1);
                stringify(&args[1], 1);
                RESULTINT = strcmp(ARGSTR(0), ARGSTR(1)) == 0;
        } else if (TY(0) == TY_DIR && TY(1) == TY_DIR)
                RESULTINT = ARGDIR(0) == ARGDIR(1);
        else if (TY(0) == TY_ENTITY && TY(1) == TY_ENTITY)
                RESULTINT = ARGENTITY(0) == ARGENTITY(1);
        else if (TY(0) == TY_LOCATION && TY(1) == TY_LOCATION)
                RESULTINT = (ARGLOCATION(0).x == ARGLOCATION(1).x
                             && ARGLOCATION(0).y == ARGLOCATION(1).y
                             && ARGLOCATION(0).m == ARGLOCATION(1).m);
        else if (TY(0) == TY_AREA && TY(1) == TY_AREA)
                RESULTINT = ARGAREA(0) == ARGAREA(1); /* Probably not that great an idea... */
        else if (TY(0) == TY_SPELL && TY(1) == TY_SPELL)
                RESULTINT = ARGSPELL(0) == ARGSPELL(1);
        else if (TY(0) == TY_INVOCATION && TY(1) == TY_INVOCATION)
                RESULTINT = ARGINVOCATION(0) == ARGINVOCATION(1);
        else {
                intify(&args[0]);
                intify(&args[1]);
                RESULTINT = ARGINT(0) == ARGINT(1);
        }
        return 0;
}

static int
fun_bitand(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = ARGINT(0) & ARGINT(1);
        return 0;
}

static int
fun_bitor(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = ARGINT(0) | ARGINT(1);
        return 0;
}

static int
fun_bitxor(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = ARGINT(0) ^ ARGINT(1);
        return 0;
}

static int
fun_bitshl(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = ARGINT(0) << ARGINT(1);
        return 0;
}

static int
fun_bitshr(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = ARGINT(0) >> ARGINT(1);
        return 0;
}

static int
fun_max(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = MAX(ARGINT(0), ARGINT(1));
        return 0;
}

static int
fun_min(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = MIN(ARGINT(0), ARGINT(1));
        return 0;
}

static int
fun_if_then_else(env_t *env, int args_nr, val_t *result, val_t *args)
{
        if (ARGINT(0))
                magic_copy_var(result, &args[1]);
        else
                magic_copy_var(result, &args[2]);
        return 0;
}

void
magic_area_rect(int *m, int *x, int *y, int *width, int *height, area_t *area)
{
        switch (area->ty) {
        case AREA_UNION: break;

        case AREA_LOCATION:
                *m = area->a.a_loc.m;
                *x = area->a.a_loc.x;
                *y = area->a.a_loc.y;
                *width = 1;
                *height = 1;
                break;

        case AREA_RECT:
                *m = area->a.a_rect.loc.m;
                *x = area->a.a_rect.loc.x;
                *y = area->a.a_rect.loc.y;
                *width = area->a.a_rect.width;
                *height = area->a.a_rect.height;
                break;

        case AREA_BAR: {
                int tx = area->a.a_bar.loc.x;
                int ty = area->a.a_bar.loc.y;
                int twidth = area->a.a_bar.width;
                int tdepth = area->a.a_bar.width;
                *m = area->a.a_bar.loc.m;

                switch (area->a.a_bar.dir) {
                case DIR_S:
                        *x = tx - twidth;
                        *y = ty;
                        *width = twidth * 2 + 1;
                        *height = tdepth;
                        break;

                case DIR_W:
                        *x = tx - tdepth;
                        *y = ty - twidth;
                        *width = tdepth;
                        *height = twidth * 2 + 1;
                        break;

                case DIR_N:
                        *x = tx - twidth;
                        *y = ty - tdepth;
                        *width = twidth * 2 + 1;
                        *height = tdepth;
                        break;

                case DIR_E:
                        *x = tx;
                        *y = ty - twidth;
                        *width = tdepth;
                        *height = twidth * 2 + 1;
                        break;

                default:
                        fprintf(stderr, "Error: Trying to compute area of NE/SE/NW/SW-facing bar");
                        *x = tx; *y = ty; *width = *height = 1;
                }
                break;
        }        
        }
}

int
magic_location_in_area(int m, int x, int y, area_t *area)
{
        switch (area->ty) {
        case AREA_UNION:
                return magic_location_in_area(m, x, y, area->a.a_union[0])
                        || magic_location_in_area(m, x, y, area->a.a_union[1]);
        case AREA_LOCATION:
        case AREA_RECT:
        case AREA_BAR: {
                int am;
                int ax, ay, awidth, aheight;
                magic_area_rect(&am, &ax, &ay, &awidth, &aheight, area);
                return (am == m
                        && (x >= ax) && (y >= ay)
                        && (x < ax + awidth) && (y < ay + aheight));
        }
        default:
                fprintf(stderr, "INTERNAL ERROR: Invalid area\n");
                return 0;
        }
}


static int
fun_is_in(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = magic_location_in_area(ARGLOCATION(0).m,
                                           ARGLOCATION(0).x,
                                           ARGLOCATION(0).y,
                                           ARGAREA(1));
        return 0;
}

static int
fun_skill(env_t *env, int args_nr, val_t *result, val_t *args)
{
        if (ETY(0) != BL_PC
            || ARGINT(1) < 0
            || ARGINT(1) >= MAX_SKILL
            || ARGPC(0)->status.skill[ARGINT(1)].id != ARGINT(1))
                RESULTINT = 0;
        else
                RESULTINT = ARGPC(0)->status.skill[ARGINT(1)].lv;
        return 0;
}

static int
fun_has_shroud(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = (ETY(0) == BL_PC
                     && ARGPC(0)->state.shroud_active);
        return 0;
}

#define BATTLE_GETTER(name) static int fun_get_##name(env_t *env, int args_nr, val_t *result, val_t *args) { RESULTINT = battle_get_##name(ARGENTITY(0)); return 0; }

BATTLE_GETTER(str);
BATTLE_GETTER(agi);
BATTLE_GETTER(vit);
BATTLE_GETTER(dex);
BATTLE_GETTER(luk);
BATTLE_GETTER(int);
BATTLE_GETTER(lv);
BATTLE_GETTER(hp);
BATTLE_GETTER(max_hp);
BATTLE_GETTER(dir);

#define MMO_GETTER(name) static int fun_get_##name(env_t *env, int args_nr, val_t *result, val_t *args) {	\
                if (ETY(0) == BL_PC)								\
                	RESULTINT = ARGPC(0)->status.name;					\
		else										\
		        RESULTINT = 0;								\
		return 0; }									

MMO_GETTER(sp);
MMO_GETTER(max_sp);

static int
fun_name_of(env_t *env, int args_nr, val_t *result, val_t *args)
{
        if (TY(0) == TY_ENTITY) {
                RESULTSTR = strdup(show_entity(ARGENTITY(0)));
                return 0;
        } else if (TY(0) == TY_SPELL) {
                RESULTSTR = strdup(ARGSPELL(0)->name);
                return 0;
        } else if (TY(0) == TY_INVOCATION) {
                RESULTSTR = strdup(ARGINVOCATION(0)->spell->name);
                return 0;
        }
        return 1;
}

#define COPY_LOCATION(dest, src) (dest).x = (src).x; (dest).y = (src).y; (dest).m = (src).m;

static int
fun_location(env_t *env, int args_nr, val_t *result, val_t *args)
{
        COPY_LOCATION(RESULTLOCATION, *(ARGENTITY(0)));
        return 0;
}

/* Recall that glibc's rand() isnt' too bad in the lower bits */

static int
fun_random(env_t *env, int args_nr, val_t *result, val_t *args)
{
        int delta = ARGINT(0);
        if (delta < 0)
                delta = -delta;
        if (delta == 0) {
                RESULTINT = 0;
                return 0;
        }
        RESULTINT = rand() % delta;
        if (ARGINT(0) < 0)
                RESULTINT = -RESULTINT;
        return 0;
}

static int
fun_random_dir(env_t *env, int args_nr, val_t *result, val_t *args)
{
        if (ARGINT(0))
                RESULTDIR = rand() & 0x7;
        else
                RESULTDIR = (rand() & 0x3) * 2;
        return 0;
}

static int
fun_hash_entity(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = ARGENTITY(0)->id;
        return 0;
}

int // ret -1: not a string, ret 1: no such item, ret 0: OK
magic_find_item(val_t *args, int index, struct item *item, int *stackable)
{
        struct item_data *item_data;
        int must_add_sequentially;

        if (TY(index) == TY_INT)
                item_data = itemdb_exists(ARGINT(index));
        else if (TY(index) == TY_STRING)
                item_data = itemdb_searchname(ARGSTR(index));
        else
                return -1;

        if (!item_data)
                return 1;

        must_add_sequentially = (item_data->type == 4
                                 || item_data->type == 5
                                 || item_data->type == 7
                                 || item_data->type == 8); /* Very elegant. */

        if (stackable)
            *stackable = !must_add_sequentially;

        memset(item, 0, sizeof(struct item));
        item->nameid = item_data->nameid;
        item->identify = 1;

        return 0;
}

static int
fun_count_item(env_t *env, int args_nr, val_t *result, val_t *args)
{
        character_t *chr = (ETY(0) == BL_PC) ? ARGPC(0) : NULL;
        int stackable;
        struct item item;

        GET_ARG_ITEM(1, item, stackable);

        if (!chr)
                return 1;

        RESULTINT = pc_count_all_items(chr, item.nameid);
        return 0;
}

static int
fun_is_equipped(env_t *env, int args_nr, val_t *result, val_t *args)
{
        character_t *chr = (ETY(0) == BL_PC) ? ARGPC(0) : NULL;
        int stackable;
        struct item item;
        int i;
        int retval = 0;

        GET_ARG_ITEM(1, item, stackable);

        if (!chr)
                return 1;

        fprintf(stderr, "Checking for equipped status of item %d\n", item.nameid);

        for (i = 0; i < 11; i++)
                if (chr->equip_index[i] >= 0
                    && chr->status.inventory[chr->equip_index[i]].nameid == item.nameid) {
                        retval = i + 1;
                        break;
                }

        RESULTINT = retval;
        return 0;
}

static int
fun_is_married(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = (ETY(0) == BL_PC
                     && ARGPC(0)->status.partner_id);
        return 0;
}

static int
fun_partner(env_t *env, int args_nr, val_t *result, val_t *args)
{
        if (ETY(0) == BL_PC
            && ARGPC(0)->status.partner_id) {
                RESULTENTITY = (entity_t *) map_nick2sd(map_charid2nick(ARGPC(0)->status.partner_id));
                return 0;
        } else
                return 1;
}

static int
fun_awayfrom(env_t *env, int args_nr, val_t *result, val_t *args)
{
        location_t *loc = &ARGLOCATION(0);
        int dx = heading_x[ARGDIR(1)];
        int dy = heading_y[ARGDIR(1)];
        int distance = ARGINT(2);
        while (distance-- && !map_is_solid(loc->m, loc->x + dx, loc->y + dy)) {
                loc->x += dx;
                loc->y += dy;
        }

        RESULTLOCATION = *loc;
        return 0;
}

static int
fun_failed(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = TY(0) == TY_FAIL;
        return 0;
}

static int
fun_npc(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTENTITY = (entity_t *)npc_name2id(ARGSTR(0));
        return RESULTENTITY == NULL;
}

static int
fun_pc(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTENTITY = (entity_t *)map_nick2sd(ARGSTR(0));
        return RESULTENTITY == NULL;
}

static int
fun_distance(env_t *env, int args_nr, val_t *result, val_t *args)
{
        if (ARGLOCATION(0).m != ARGLOCATION(1).m)
                RESULTINT = INT_MAX;
        else
                RESULTINT = MAX(abs(ARGLOCATION(0).x - ARGLOCATION(1).x),
                                abs(ARGLOCATION(0).y - ARGLOCATION(1).y));
        return 0;
}


static int
fun_rdistance(env_t *env, int args_nr, val_t *result, val_t *args)
{
        if (ARGLOCATION(0).m != ARGLOCATION(1).m)
                RESULTINT = INT_MAX;
        else {
                int dx = ARGLOCATION(0).x - ARGLOCATION(1).x;
                int dy = ARGLOCATION(0).y - ARGLOCATION(1).y;
                RESULTINT = (int) (sqrt((dx*dx) + (dy*dy)));
        }
        return 0;
}


static int
fun_anchor(env_t *env, int args_nr, val_t *result, val_t *args)
{
        teleport_anchor_t *anchor = magic_find_anchor(ARGSTR(0));

        if (!anchor)
                return 1;

        magic_eval(env, result, anchor->location);

        make_area(result);
        if (result->ty != TY_AREA) {
                magic_clear_var(result);
                return 1;
        } 

        return 0;
}



static int
fun_line_of_sight(env_t *env, int args_nr, val_t *result, val_t *args)
{
        entity_t e1, e2;

        COPY_LOCATION(e1, ARGLOCATION(0));
        COPY_LOCATION(e2, ARGLOCATION(1));

        RESULTINT = battle_check_range(&e1, &e2, 0);

        return 0;
}

void
magic_random_location(location_t *dest, area_t *area)
{
        switch (area->ty) {
        case AREA_UNION: {
                int rv = rand() % area->size;
                if (rv < area->a.a_union[0]->size)
                        magic_random_location(dest, area->a.a_union[0]);
                else
                        magic_random_location(dest, area->a.a_union[1]);
                break;
        }

        case AREA_LOCATION:
        case AREA_RECT:
        case AREA_BAR: {
                int m, x, y, w, h;
                magic_area_rect(&m, &x, &y, &w, &h, area);

                if (w <= 1)
                        w = 1;

                if (h <= 1)
                        h = 1;

                x += rand() % w;
                y += rand() % h;

                if (!map_is_solid(m, x, y)) {
                        int start_x = x;
                        int start_y = y;
                        int i;
                        int initial_dir = rand() & 0x7;
                        int dir = initial_dir;

                        /* try all directions, up to a distance to 10, for a free slot */
                        do {
                                x = start_x;
                                y = start_y;

                                for (i = 0; i < 10 && map_is_solid(m, x, y); i++) {
                                        x += heading_x[dir];
                                        y += heading_y[dir];
                                }

                                dir = (dir + 1) & 0x7;
                        } while (map_is_solid(m, x, y) && dir != initial_dir);

                }
                /* We've tried our best.  If the map is still solid, the engine will automatically randomise the target location if we try to warp. */

                dest->m = m;
                dest->x = x;
                dest->y = y;
                break;
        }

        default:
                fprintf(stderr, "Unknown area type %d\n", area->ty);
        }
}

static int
fun_pick_location(env_t *env, int args_nr, val_t *result, val_t *args)
{
        magic_random_location(&result->v.v_location, ARGAREA(0));
        return 0;
}

static int
fun_read_script_int(env_t *env, int args_nr, val_t *result, val_t *args)
{
        entity_t *subject_p = ARGENTITY(0);
        char *var_name = ARGSTR(1);

        if (subject_p->type != BL_PC)
                return 1;

        RESULTINT = pc_readglobalreg((character_t *) subject_p, var_name);
        return 0;
}

static int
fun_rbox(env_t *env, int args_nr, val_t *result, val_t *args)
{
        location_t loc = ARGLOCATION(0);
        int radius = ARGINT(1);

        RESULTAREA = area_new(AREA_RECT);
        RESULTAREA->a.a_rect.loc.m = loc.m;
        RESULTAREA->a.a_rect.loc.x = loc.x - radius;
        RESULTAREA->a.a_rect.loc.y = loc.y - radius;
        RESULTAREA->a.a_rect.width = radius * 2 + 1;
        RESULTAREA->a.a_rect.height = radius * 2 + 1;

        return 0;
}

static int
fun_running_status_update(env_t *env, int args_nr, val_t *result, val_t *args)
{
        if (ETY(0) != BL_PC)
                return 1;

        RESULTINT = battle_get_sc_data(ARGENTITY(0))[ARGINT(1)].timer != -1;
        return 0;
}

static int
fun_element(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = battle_get_element(ARGENTITY(0)) % 10;
        return 0;
}

static int
fun_element_level(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = battle_get_element(ARGENTITY(0)) / 10;
        return 0;
}

static int
fun_index(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = ARGSPELL(0)->index;
        return 0;
}

static int
fun_is_exterior(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = map[ARGLOCATION(0).m].name[4] == '1';
        return 0;
}

static int
fun_contains_string(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = NULL != strstr(ARGSTR(0), ARGSTR(1));
        return 0;
}

static int
fun_strstr(env_t *env, int args_nr, val_t *result, val_t *args)
{
        char *offset = strstr(ARGSTR(0), ARGSTR(1));
        RESULTINT = offset - ARGSTR(0);
        return offset == NULL;
}

static int
fun_strlen(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = strlen(ARGSTR(0));
        return 0;
}

static int
fun_substr(env_t *env, int args_nr, val_t *result, val_t *args)
{
        const char *src = ARGSTR(0);
        const int slen = strlen(src);
        int offset = ARGINT(1);
        int len = ARGINT(2);

        if (len < 0)
                len = 0;
        if (offset < 0)
                offset = 0;

        if (offset > slen)
                offset = slen;

        if (offset + len > slen)
                len = slen - offset;

        RESULTSTR = (char *) calloc(1, 1 + len);
        memcpy(RESULTSTR, src + offset, len);

        return 0;
}

static int
fun_sqrt(env_t *env, int args_nr, val_t *result, val_t *args)
{
        RESULTINT = (int) sqrt(ARGINT(0));
        return 0;
}


#define BATTLE_RECORD2(sname, name) { sname, "e", 'i', fun_get_##name }
#define BATTLE_RECORD(name) BATTLE_RECORD2(#name, name)
static fun_t functions[] = {
        { "+", "..", '.', fun_add },
        { "-", "ii", 'i', fun_sub },
        { "*", "ii", 'i', fun_mul },
        { "/", "ii", 'i', fun_div },
        { "%", "ii", 'i', fun_mod },
        { "||", "ii", 'i', fun_or },
        { "&&", "ii", 'i', fun_and },
        { ">", "..", 'i', fun_gt },
        { ">=", "..", 'i', fun_gte },
        { "=", "..", 'i', fun_eq },
        { "|", "..", 'i', fun_bitor },
        { "&", "ii", 'i', fun_bitand },
        { "^", "ii", 'i', fun_bitxor },
        { "<<", "ii", 'i', fun_bitshl },
        { ">>", "ii", 'i', fun_bitshr },
        { "not", "i", 'i', fun_not },
        { "neg", "i", 'i', fun_neg },
        { "max", "ii", 'i', fun_max },
        { "min", "ii", 'i', fun_min },
        { "is_in", "la", 'i', fun_is_in },
        { "if_then_else", "i__", '_', fun_if_then_else },
        { "skill", "ei", 'i', fun_skill },
        BATTLE_RECORD(str),
        BATTLE_RECORD(agi),
        BATTLE_RECORD(vit),
        BATTLE_RECORD(dex),
        BATTLE_RECORD(luk),
        BATTLE_RECORD(int),
        BATTLE_RECORD2("level", lv),
        BATTLE_RECORD(hp),
        BATTLE_RECORD(max_hp),
        BATTLE_RECORD(sp),
        BATTLE_RECORD(max_sp),
        { "dir", "e", 'd', fun_get_dir },
        { "name_of", ".", 's', fun_name_of },
        { "location", "e", 'l', fun_location },
        { "random", "i", 'i', fun_random },
        { "random_dir", "i", 'd', fun_random_dir },
        { "hash_entity", "e", 'i', fun_hash_entity },
        { "is_married", "e", 'i', fun_is_married },
        { "partner", "e", 'e', fun_partner },
        { "awayfrom", "ldi", 'l', fun_awayfrom },
        { "failed", "_", 'i', fun_failed },
        { "pc", "s", 'e', fun_pc },
        { "npc", "s", 'e', fun_npc },
        { "distance", "ll", 'i', fun_distance },
        { "rdistance", "ll", 'i', fun_rdistance },
        { "anchor", "s", 'a', fun_anchor },
        { "random_location", "a", 'l', fun_pick_location },
        { "script_int", "es", 'i', fun_read_script_int },
        { "rbox", "li", 'a', fun_rbox },
        { "count_item", "e.", 'i', fun_count_item },
        { "line_of_sight", "ll", 'i', fun_line_of_sight },
        { "running_status_update", "ei", 'i', fun_running_status_update },
        { "element", "e", 'i', fun_element },
        { "element_level", "e", 'i', fun_element_level },
        { "has_shroud", "e", 'i', fun_has_shroud },
        { "is_equipped", "e.", 'i', fun_is_equipped },
        { "spell_index", "S", 'i', fun_index },
        { "is_exterior", "l", 'i', fun_is_exterior },
        { "contains_string", "ss", 'i', fun_contains_string },
        { "strstr", "ss", 'i', fun_strstr },
        { "strlen", "s", 'i', fun_strlen },
        { "substr", "sii", 's', fun_substr },
        { "sqrt", "i", 'i', fun_sqrt },
        { NULL, NULL, '.', NULL }
};

static int functions_are_sorted = 0;

int
compare_fun(const void *lhs, const void *rhs)
{
        return strcmp(((fun_t *)lhs)->name,
                      ((fun_t *)rhs)->name);
}

fun_t *
magic_get_fun(char *name, int *index)
{
        static int functions_nr;
        fun_t *result;
        fun_t key;

        if (!functions_are_sorted) {
                fun_t *it = functions;

                while (it->name) ++it;
                functions_nr = it - functions;
                
                qsort(functions, functions_nr, sizeof(fun_t),
                      compare_fun);
                functions_are_sorted = 1;
        }

        key.name = name;
        result = (fun_t *) bsearch(&key, functions, functions_nr, sizeof(fun_t),
                                   compare_fun);

        if (result && index)
                *index = result - functions;

        return result;
}

static int // 1 on failure
eval_location(env_t *env, location_t *dest, e_location_t *expr)
{
        val_t m, x, y;
        magic_eval(env, &m, expr->m);
        magic_eval(env, &x, expr->x);
        magic_eval(env, &y, expr->y);

        if (CHECK_TYPE(&m, TY_STRING)
            && CHECK_TYPE(&x, TY_INT)
            && CHECK_TYPE(&y, TY_INT)) {
                int map_id = map_mapname2mapid(m.v.v_string);
                magic_clear_var(&m);
                if (map_id < 0)
                        return 1;
                dest->m = map_id;
                dest->x = x.v.v_int;
                dest->y = y.v.v_int;
                return 0;
        } else {
                magic_clear_var(&m);
                magic_clear_var(&x);
                magic_clear_var(&y);
                return 1;
        }
}

static area_t *
eval_area(env_t *env, e_area_t *expr)
{
        area_t *area = malloc(sizeof(area_t));
        area->ty = expr->ty;

        switch (expr->ty) {
        case AREA_LOCATION:
                area->size = 1;
                if (eval_location(env, &area->a.a_loc, &expr->a.a_loc)) {
                        free(area);
                        return NULL;
                } else
                        return area;

        case AREA_UNION: {
                int i, fail = 0;
                for (i = 0; i < 2; i++) {
                        area->a.a_union[i] = eval_area(env, expr->a.a_union[i]);
                        if (!area->a.a_union[i])
                                fail = 1;
                }

                if (fail) {
                        for (i = 0; i < 2; i++) {
                                if (area->a.a_union[i])
                                        free_area(area->a.a_union[i]);
                        }
                        free(area);
                        return NULL;
                }
                area->size = area->a.a_union[0]->size + area->a.a_union[1]->size;
                return area;
        }

        case AREA_RECT: {
                val_t width, height;
                magic_eval(env, &width, expr->a.a_rect.width);
                magic_eval(env, &height, expr->a.a_rect.height);

                area->a.a_rect.width = width.v.v_int;
                area->a.a_rect.height = height.v.v_int;

                if (CHECK_TYPE(&width, TY_INT)
                    && CHECK_TYPE(&height, TY_INT)
                    && !eval_location (env, &(area->a.a_rect.loc), &expr->a.a_rect.loc)) {
                        area->size = area->a.a_rect.width * area->a.a_rect.height;
                        magic_clear_var(&width);
                        magic_clear_var(&height);
                        return area;
                } else {
                        free(area);
                        magic_clear_var(&width);
                        magic_clear_var(&height);
                        return NULL;
                }
        }

        case AREA_BAR: {
                val_t width, depth, dir;
                magic_eval(env, &width, expr->a.a_bar.width);
                magic_eval(env, &depth, expr->a.a_bar.depth);
                magic_eval(env, &dir, expr->a.a_bar.dir);

                area->a.a_bar.width = width.v.v_int;
                area->a.a_bar.depth = depth.v.v_int;
                area->a.a_bar.dir = dir.v.v_int;

                if (CHECK_TYPE(&width, TY_INT)
                    && CHECK_TYPE(&depth, TY_INT)
                    && CHECK_TYPE(&dir, TY_DIR)
                    && !eval_location (env, &area->a.a_bar.loc, &expr->a.a_bar.loc)) {
                        area->size = (area->a.a_bar.width * 2 + 1) * area->a.a_bar.depth;
                        magic_clear_var(&width);
                        magic_clear_var(&depth);
                        magic_clear_var(&dir);
                        return area;
                } else {
                        free(area);
                        magic_clear_var(&width);
                        magic_clear_var(&depth);
                        magic_clear_var(&dir);
                        return NULL;
                }
        }

        default:
                fprintf(stderr, "INTERNAL ERROR: Unknown area type %d\n", area->ty);
                free(area);
                return NULL;
        }
}

static int
type_key(char ty_key)
{
        switch (ty_key) {
        case 'i': return TY_INT;
        case 'd': return TY_DIR;
        case 's': return TY_STRING;
        case 'e': return TY_ENTITY;
        case 'l': return TY_LOCATION;
        case 'a': return TY_AREA;
        case 'S': return TY_SPELL;
        case 'I': return TY_INVOCATION;
        default: return -1;
        }
}

int
magic_signature_check(char *opname, char *funname, char *signature, int args_nr, val_t *args, int line, int column)
{
        int i;
        for (i = 0; i < args_nr; i++) {
                val_t *arg = &args[i];
                char ty_key = signature[i];
                int ty = arg->ty;
                int desired_ty = type_key(ty_key);

                if (ty == TY_ENTITY) {
                        /* Dereference entities in preparation for calling function */
                        arg->v.v_entity = map_id2bl(arg->v.v_int);
                        if (!arg->v.v_entity)
                                ty = arg->ty = TY_FAIL;
                } else if (ty == TY_INVOCATION) {
                        arg->v.v_invocation = (invocation_t *) map_id2bl(arg->v.v_int);
                        if (!arg->v.v_entity)
                                ty = arg->ty = TY_FAIL;
                }

                if (!ty_key) {
                        fprintf(stderr, "[magic-eval]:  L%d:%d: Too many arguments (%d) to %s `%s'\n",
                                line, column, args_nr, opname, funname);
                        return 1;
                }

                if (ty == TY_FAIL
                    && ty_key != '_')
                        return 1; /* Fail `in a sane way':  This is a perfectly permissible error */

                if (ty == desired_ty
                    || desired_ty < 0 /* `dontcare' */)
                        continue;

                if (ty == TY_UNDEF) {
                        fprintf(stderr, "[magic-eval]:  L%d:%d: Argument #%d to %s `%s' undefined\n",
                                line, column, i + 1, opname, funname);
                        return 1;
                }

                /* If we are here, we have a type mismatch but no failure _yet_.  Try to coerce. */
                switch (desired_ty) {
                case TY_INT:	intify(arg); break; /* 100% success rate */
                case TY_STRING:	stringify(arg, 1); break; /* 100% success rate */
                case TY_AREA:	make_area(arg); break; /* Only works for locations */
                case TY_LOCATION: make_location(arg); break; /* Only works for some areas */
                case TY_SPELL:	make_spell(arg); break; /* Only works for still-active invocatoins */
                default: break; /* We'll fail right below */
                }

                ty = arg->ty;
                if (ty != desired_ty) { /* Coercion failed? */
                        if (ty != TY_FAIL)
                                fprintf(stderr, "[magic-eval]:  L%d:%d: Argument #%d to %s `%s' of incorrect type (%d)\n",
                                        line, column, i + 1, opname, funname, ty);
                        return 1;
                }
        }

        return 0;
}

void
magic_eval(env_t *env, val_t *dest, expr_t *expr)
{
        switch (expr->ty) {
        case EXPR_VAL:
                magic_copy_var(dest, &expr->e.e_val);
                break;

        case EXPR_LOCATION:
                if (eval_location(env, &dest->v.v_location, &expr->e.e_location))
                        dest->ty = TY_FAIL;
                else
                        dest->ty = TY_LOCATION;
                break;

        case EXPR_AREA:
                if ((dest->v.v_area = eval_area(env, &expr->e.e_area)))
                        dest->ty = TY_AREA;
                else
                        dest->ty = TY_FAIL;
                break;

        case EXPR_FUNAPP: {
                val_t arguments[MAX_ARGS];
                int args_nr = expr->e.e_funapp.args_nr;
                int i;
                fun_t *f = functions + expr->e.e_funapp.id;

                for (i = 0; i < args_nr; ++i)
                        magic_eval(env, &arguments[i], expr->e.e_funapp.args[i]);
                if (magic_signature_check("function", f->name, f->signature, args_nr, arguments,
                                          expr->e.e_funapp.line_nr, expr->e.e_funapp.column)
                    || f->fun(env, args_nr, dest, arguments))
                        dest->ty = TY_FAIL;
                else {
                        int dest_ty = type_key(f->ret_ty);
                        if (dest_ty != -1)
                                dest->ty = dest_ty;

                        /* translate entity back into persistent int */
                        if (dest->ty == TY_ENTITY) {
                                if (dest->v.v_entity)
                                        dest->v.v_int = dest->v.v_entity->id;
                                else
                                        dest->ty = TY_FAIL;
                        }
                }

                for (i = 0; i < args_nr; ++i)
                        magic_clear_var(&arguments[i]);
                break;
        }

        case EXPR_ID: {
                val_t v = VAR(expr->e.e_id);
                magic_copy_var(dest, &v);
                break;
        }

        case EXPR_SPELLFIELD: {
                val_t v;
                int id = expr->e.e_field.id;
                magic_eval(env, &v, expr->e.e_field.expr);

                if (v.ty == TY_INVOCATION) {
                        invocation_t *t = (invocation_t *) map_id2bl(v.v.v_int);

                        if (!t)
                                dest->ty = TY_UNDEF;
                        else {
                                env_t *env = t->env;
                                val_t v = VAR(id);
                                magic_copy_var(dest, &v);
                        }
                } else {
                        fprintf(stderr, "[magic] Attempt to access field %s on non-spell\n", env->base_env->var_name[id]);
                        dest->ty = TY_FAIL;
                }
                break;
        }

        default:
                fprintf(stderr, "[magic] INTERNAL ERROR: Unknown expression type %d\n", expr->ty);
                break;
        }
}

int
magic_eval_int(env_t *env, expr_t *expr)
{
        val_t result;
        magic_eval(env, &result, expr);

        if (result.ty == TY_FAIL
            || result.ty == TY_UNDEF)
                return 0;

        intify(&result);

        return result.v.v_int;
}

char *
magic_eval_str(env_t *env, expr_t *expr)
{
        val_t result;
        magic_eval(env, &result, expr);

        if (result.ty == TY_FAIL
            || result.ty == TY_UNDEF)
                return strdup("?");

        stringify(&result, 0);

        return result.v.v_string;
}

expr_t *
magic_new_expr(int ty)
{
        expr_t *expr = (expr_t *)malloc(sizeof(expr_t));
        expr->ty = ty;
        return expr;
}