summaryrefslogtreecommitdiff
path: root/src/map/magic-interpreter.hpp
blob: 526617f5de717682c672c85273a71b4045948cb1 (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
#pragma once
//    magic-interpreter.hpp - Old magic.
//
//    Copyright © 2004-2011 The Mana World Development Team
//    Copyright © 2011-2014 Ben Longbons <b.r.longbons@gmail.com>
//
//    This file is part of The Mana World (Athena server)
//
//    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/>.

#include "fwd.hpp"

#include "magic-interpreter.t.hpp"

#include <cassert>

#include <memory>

#include "../strings/fwd.hpp"
#include "../strings/rstring.hpp"

#include "../generic/fwd.hpp"

#include "../sexpr/variant.hpp"

#include "../net/timer.t.hpp"

#include "../mmo/ids.hpp"
#include "../mmo/utils.hpp"

#include "map.hpp"
#include "script.hpp"
#include "skill.t.hpp"


namespace tmwa
{
namespace magic
{
struct location_t
{
    map_local *m;
    int x, y;
};

struct AreaUnion
{
    dumb_ptr<area_t> a_union[2];
};
struct AreaRect
{
    location_t loc;
    int width, height;
};
struct AreaBar
{
    location_t loc;
    int width, depth;
    DIR dir;
};

using AreaVariantBase = Variant<
    location_t,
    AreaUnion,
    AreaRect,
    AreaBar
>;

struct area_t : AreaVariantBase
{
    int size;

    area_t() = delete;
    area_t(area_t&&) = default;
    area_t(const area_t&) = delete;
    area_t& operator = (area_t&&) = default;
    area_t& operator = (const area_t&) = delete;

    area_t(location_t v, int sz) : AreaVariantBase(std::move(v)), size(sz) {}
    area_t(AreaUnion v, int sz) : AreaVariantBase(std::move(v)), size(sz) {}
    area_t(AreaRect v, int sz) : AreaVariantBase(std::move(v)), size(sz) {}
    area_t(AreaBar v, int sz) : AreaVariantBase(std::move(v)), size(sz) {}
};

struct ValUndef
{
};
struct ValInt
{
    int v_int;
};
struct ValDir
{
    DIR v_dir;
};
struct ValString
{
    RString v_string;
};
struct ValEntityInt
{
    BlockId v_eid;
};
struct ValEntityPtr
{
    dumb_ptr<block_list> v_entity;
};
struct ValLocation
{
    location_t v_location;
};
struct ValArea
{
    dumb_ptr<area_t> v_area;
};
struct ValSpell
{
    dumb_ptr<spell_t> v_spell;
};
struct ValInvocationInt
{
    BlockId v_iid;
};
struct ValInvocationPtr
{
    dumb_ptr<invocation> v_invocation;
};
struct ValFail
{
};
struct ValNegative1
{
};

using ValVariantBase = Variant<
    ValUndef,
    ValInt,
    ValDir,
    ValString,
    ValEntityInt,
    ValEntityPtr,
    ValLocation,
    ValArea,
    ValSpell,
    ValInvocationInt,
    ValInvocationPtr,
    ValFail,
    ValNegative1
>;
struct val_t : ValVariantBase
{
    val_t() noexcept : ValVariantBase(ValUndef{}) {}
    val_t(val_t&&) = default;
    val_t(const val_t&) = delete;
    val_t& operator = (val_t&&) = default;
    val_t& operator = (const val_t&) = delete;

    val_t(ValUndef v) : ValVariantBase(std::move(v)) {}
    val_t(ValInt v) : ValVariantBase(std::move(v)) {}
    val_t(ValDir v) : ValVariantBase(std::move(v)) {}
    val_t(ValString v) : ValVariantBase(std::move(v)) {}
    val_t(ValEntityInt v) : ValVariantBase(std::move(v)) {}
    val_t(ValEntityPtr v) : ValVariantBase(std::move(v)) {}
    val_t(ValLocation v) : ValVariantBase(std::move(v)) {}
    val_t(ValArea v) : ValVariantBase(std::move(v)) {}
    val_t(ValSpell v) : ValVariantBase(std::move(v)) {}
    val_t(ValInvocationInt v) : ValVariantBase(std::move(v)) {}
    val_t(ValInvocationPtr v) : ValVariantBase(std::move(v)) {}
    val_t(ValFail v) : ValVariantBase(std::move(v)) {}
    val_t(ValNegative1 v) : ValVariantBase(std::move(v)) {}
};


/* ----------- */
/* Expressions */
/* ----------- */

#define MAX_ARGS 7              /* Max. # of args used in builtin primitive functions */

struct e_area_t;

struct e_location_t
{
    dumb_ptr<expr_t> m, x, y;

    e_location_t() noexcept : m(), x(), y() {}
};
struct ExprAreaUnion
{
    dumb_ptr<e_area_t> a_union[2];
};
struct ExprAreaRect
{
    e_location_t loc;
    dumb_ptr<expr_t> width, height;
};
struct ExprAreaBar
{
    e_location_t loc;
    dumb_ptr<expr_t> width, depth, dir;
};

using ExprAreaVariantBase = Variant<
    e_location_t,
    ExprAreaUnion,
    ExprAreaRect,
    ExprAreaBar
>;

struct e_area_t : ExprAreaVariantBase
{
    e_area_t() = delete;
    e_area_t(e_area_t&&) = default;
    e_area_t(const e_area_t&) = delete;
    e_area_t& operator = (e_area_t&&) = default;
    e_area_t& operator = (const e_area_t&) = delete;

    e_area_t(e_location_t v) : ExprAreaVariantBase(std::move(v)) {}
    e_area_t(ExprAreaUnion v) : ExprAreaVariantBase(std::move(v)) {}
    e_area_t(ExprAreaRect v) : ExprAreaVariantBase(std::move(v)) {}
    e_area_t(ExprAreaBar v) : ExprAreaVariantBase(std::move(v)) {}
};

struct ExprFunApp
{
    fun_t *funp;
    int line_nr, column;
    int args_nr;
    dumb_ptr<expr_t> args[MAX_ARGS];
};
struct ExprId
{
    int e_id;
};
struct ExprField
{
    dumb_ptr<expr_t> expr;
    int id;
};

using ExprVariantBase = Variant<
    val_t,
    e_location_t,
    e_area_t,
    ExprFunApp,
    ExprId,
    ExprField
>;
struct expr_t : ExprVariantBase
{
    expr_t() = delete;
    expr_t(expr_t&&) = default;
    expr_t(const expr_t&) = delete;
    expr_t& operator = (expr_t&&) = default;
    expr_t& operator = (const expr_t&) = delete;

    expr_t(val_t v) : ExprVariantBase(std::move(v)) {}
    expr_t(e_location_t v) : ExprVariantBase(std::move(v)) {}
    expr_t(e_area_t v) : ExprVariantBase(std::move(v)) {}
    expr_t(ExprFunApp v) : ExprVariantBase(std::move(v)) {}
    expr_t(ExprId v) : ExprVariantBase(std::move(v)) {}
    expr_t(ExprField v) : ExprVariantBase(std::move(v)) {}
};


struct effect_t;

struct EffectSkip
{
};
struct EffectAbort
{
};
struct EffectAssign
{
    int id;
    dumb_ptr<expr_t> expr;
};
struct EffectForEach
{
    int id;
    dumb_ptr<expr_t> area;
    dumb_ptr<effect_t> body;
    FOREACH_FILTER filter;
};
struct EffectFor
{
    int id;
    dumb_ptr<expr_t> start, stop;
    dumb_ptr<effect_t> body;
};
struct EffectIf
{
    dumb_ptr<expr_t> cond;
    dumb_ptr<effect_t> true_branch, false_branch;
};
struct EffectSleep
{
    dumb_ptr<expr_t> e_sleep;        /* sleep time */
};
struct EffectScript
{
    dumb_ptr<const ScriptBuffer> e_script;
};
struct EffectBreak
{
};
struct EffectOp
{
    op_t *opp;
    int args_nr;
    int line_nr, column;
    dumb_ptr<expr_t> args[MAX_ARGS];
};
struct EffectEnd
{
};
struct EffectCall
{
    std::vector<int> *formalv;
    dumb_ptr<std::vector<dumb_ptr<expr_t>>> actualvp;
    dumb_ptr<effect_t> body;
};

using EffectVariantBase = Variant<
    EffectSkip,
    EffectAbort,
    EffectAssign,
    EffectForEach,
    EffectFor,
    EffectIf,
    EffectSleep,
    EffectScript,
    EffectBreak,
    EffectOp,
    EffectEnd,
    EffectCall
>;
struct effect_t : EffectVariantBase
{
    dumb_ptr<effect_t> next;

    effect_t() = delete;
    effect_t(effect_t&&) = default;
    effect_t(const effect_t&) = delete;
    effect_t& operator = (effect_t&&) = default;
    effect_t& operator = (const effect_t&) = delete;

    effect_t(EffectSkip v, dumb_ptr<effect_t> n) : EffectVariantBase(std::move(v)), next(n) {}
    effect_t(EffectAbort v, dumb_ptr<effect_t> n) : EffectVariantBase(std::move(v)), next(n) {}
    effect_t(EffectAssign v, dumb_ptr<effect_t> n) : EffectVariantBase(std::move(v)), next(n) {}
    effect_t(EffectForEach v, dumb_ptr<effect_t> n) : EffectVariantBase(std::move(v)), next(n) {}
    effect_t(EffectFor v, dumb_ptr<effect_t> n) : EffectVariantBase(std::move(v)), next(n) {}
    effect_t(EffectIf v, dumb_ptr<effect_t> n) : EffectVariantBase(std::move(v)), next(n) {}
    effect_t(EffectSleep v, dumb_ptr<effect_t> n) : EffectVariantBase(std::move(v)), next(n) {}
    effect_t(EffectScript v, dumb_ptr<effect_t> n) : EffectVariantBase(std::move(v)), next(n) {}
    effect_t(EffectBreak v, dumb_ptr<effect_t> n) : EffectVariantBase(std::move(v)), next(n) {}
    effect_t(EffectOp v, dumb_ptr<effect_t> n) : EffectVariantBase(std::move(v)), next(n) {}
    effect_t(EffectEnd v, dumb_ptr<effect_t> n) : EffectVariantBase(std::move(v)), next(n) {}
    effect_t(EffectCall v, dumb_ptr<effect_t> n) : EffectVariantBase(std::move(v)), next(n) {}
};

/* ---------- */
/* Components */
/* ---------- */

struct component_t
{
    dumb_ptr<component_t> next;
    ItemNameId item_id;
    int count;
};


struct spellguard_t;
struct GuardCondition
{
    dumb_ptr<expr_t> s_condition;
};
struct GuardMana
{
    dumb_ptr<expr_t> s_mana;
};
struct GuardCastTime
{
    dumb_ptr<expr_t> s_casttime;
};
struct GuardComponents
{
    dumb_ptr<component_t> s_components;
};
struct GuardCatalysts
{
    dumb_ptr<component_t> s_catalysts;
};
struct GuardChoice
{
    dumb_ptr<spellguard_t> s_alt;   /* either `next' or `s.s_alt' */
};
struct effect_set_t
{
    dumb_ptr<effect_t> effect, at_trigger, at_end;
};

using SpellGuardVariantBase = Variant<
    GuardCondition,
    GuardMana,
    GuardCastTime,
    GuardComponents,
    GuardCatalysts,
    GuardChoice,
    effect_set_t
>;
struct spellguard_t : SpellGuardVariantBase
{
    dumb_ptr<spellguard_t> next;

    spellguard_t() = delete;
    spellguard_t(spellguard_t&&) = default;
    spellguard_t(const spellguard_t&) = delete;
    spellguard_t& operator = (spellguard_t&&) = default;
    spellguard_t& operator = (const spellguard_t&) = delete;

    spellguard_t(GuardCondition v, dumb_ptr<spellguard_t> n) : SpellGuardVariantBase(std::move(v)), next(n) {}
    spellguard_t(GuardMana v, dumb_ptr<spellguard_t> n) : SpellGuardVariantBase(std::move(v)), next(n) {}
    spellguard_t(GuardCastTime v, dumb_ptr<spellguard_t> n) : SpellGuardVariantBase(std::move(v)), next(n) {}
    spellguard_t(GuardComponents v, dumb_ptr<spellguard_t> n) : SpellGuardVariantBase(std::move(v)), next(n) {}
    spellguard_t(GuardCatalysts v, dumb_ptr<spellguard_t> n) : SpellGuardVariantBase(std::move(v)), next(n) {}
    spellguard_t(GuardChoice v, dumb_ptr<spellguard_t> n) : SpellGuardVariantBase(std::move(v)), next(n) {}
    spellguard_t(effect_set_t v, dumb_ptr<spellguard_t> n) : SpellGuardVariantBase(std::move(v)), next(n) {}
};

/* ------ */
/* Spells */
/* ------ */

struct letdef_t
{
    int id;
    dumb_ptr<expr_t> expr;
};

struct spell_t
{
    RString name;
    RString invocation;
    SPELL_FLAG flags;
    int arg;
    SPELLARG spellarg_ty;

    std::vector<letdef_t> letdefv;

    dumb_ptr<spellguard_t> spellguard;
};

/* ------- */
/* Anchors */
/* ------- */

struct teleport_anchor_t
{
    RString name;
    RString invocation;
    dumb_ptr<expr_t> location;
};

/* ------------------- */
/* The big config blob */
/* ------------------- */

struct magic_conf_t
{
    struct mcvar
    {
        RString name;
        val_t val;
    };
    // This should probably be done by a dedicated "intern pool" class
    std::vector<mcvar> varv;

    std::map<RString, dumb_ptr<spell_t>> spells_by_name, spells_by_invocation;

    std::map<RString, dumb_ptr<teleport_anchor_t>> anchors_by_name, anchors_by_invocation;
};

/* Execution environment */

// these are not an enum they're a nasty intern hack
#define VAR_MIN_CASTTIME        0
#define VAR_OBSCURE_CHANCE      1
#define VAR_CASTER              2
#define VAR_SPELLPOWER          3
#define VAR_SPELL               4
#define VAR_INVOCATION          5
#define VAR_TARGET              6
#define VAR_SCRIPTTARGET        7
#define VAR_LOCATION            8

struct env_t
{
    magic_conf_t *base_env;
    std::unique_ptr<val_t[]> varu;

    val_t& VAR(size_t i)
    {
        assert (varu);
        if (varu[i].is<ValUndef>())
            return base_env->varv[i].val;
        else
            return varu[i];
    }

};

struct CarForEach
{
    int id;
    bool ty_is_spell_not_entity;
    dumb_ptr<effect_t> body;
    dumb_ptr<std::vector<BlockId>> entities_vp;
    int index;
};
struct CarFor
{
    int id;
    dumb_ptr<effect_t> body;
    int current;
    int stop;
};
struct CarProc
{
    int args_nr;
    int *formalap;
    dumb_ptr<val_t[]> old_actualpa;
};

using CarVariantBase = Variant<
    CarForEach,
    CarFor,
    CarProc
>;

struct cont_activation_record_t : CarVariantBase
{
    dumb_ptr<effect_t> return_location;

    cont_activation_record_t() = delete;
    cont_activation_record_t(cont_activation_record_t&&) = default;
    cont_activation_record_t(const cont_activation_record_t&) = delete;
    cont_activation_record_t& operator = (cont_activation_record_t&&) = default;
    cont_activation_record_t& operator = (const cont_activation_record_t&) = delete;

    cont_activation_record_t(CarForEach v, dumb_ptr<effect_t> rl) : CarVariantBase(std::move(v)), return_location(rl) {}
    cont_activation_record_t(CarFor v, dumb_ptr<effect_t> rl) : CarVariantBase(std::move(v)), return_location(rl) {}
    cont_activation_record_t(CarProc v, dumb_ptr<effect_t> rl) : CarVariantBase(std::move(v)), return_location(rl) {}
};

struct status_change_ref_t
{
    StatusChange sc_type;
    BlockId bl_id;
};

struct invocation : block_list
{
    dumb_ptr<invocation> next_invocation; /* used for spells directly associated with a caster: they form a singly-linked list */
    INVOCATION_FLAG flags;

    dumb_ptr<env_t> env;
    dumb_ptr<spell_t> spell;
    BlockId caster;                /* this is the person who originally invoked the spell */
    BlockId subject;               /* when this person dies, the spell dies with it */

    Timer timer;                 /* spell timer, if any */

    std::vector<cont_activation_record_t> stack;

    int script_pos;            /* Script position; if nonzero, resume the script we were running. */
    dumb_ptr<effect_t> current_effect;
    dumb_ptr<effect_t> trigger_effect;   /* If non-nullptr, this is used to spawn a cloned effect based on the same environment */
    dumb_ptr<effect_t> end_effect;       /* If non-nullptr, this is executed when the spell terminates naturally, e.g. when all status changes have run out or all delays are over. */

    /* Status change references:  for status change updates, keep track of whom we updated where */
    std::vector<status_change_ref_t> status_change_refv;

};
} // namespace magic

// inlines for map.hpp
inline dumb_ptr<magic::invocation> block_list::as_spell() { return dumb_ptr<magic::invocation>(static_cast<magic::invocation *>(this)); }
inline dumb_ptr<magic::invocation> block_list::is_spell() { return bl_type == BL::SPELL ? as_spell() : nullptr; }

namespace magic
{
/* The following is used only by the parser: */
struct args_rec_t
{
    dumb_ptr<std::vector<dumb_ptr<expr_t>>> argvp;
};

struct proc_t
{
    RString name;
    std::vector<int> argv;
    dumb_ptr<effect_t> body;

    proc_t()
    : name()
    , argv()
    , body()
    {}
};
} // namespace magic
} // namespace tmwa