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
|
#include "magic-interpreter-aux.hpp"
#include "magic-interpreter.hpp"
#include "../common/cxxstdio.hpp"
#include "../common/timer.hpp"
#include "magic-expr.hpp"
#include "pc.hpp"
#include "../poison.hpp"
static
void set_int_p(val_t *v, int i, TYPE t)
{
v->ty = t;
v->v.v_int = i;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-macros"
#define set_int(v, i) set_int_p(v, i, TYPE::INT)
#define set_dir(v, i) set_int_p(v, i, TYPE::DIR)
#define SETTER(tty, dyn_ty, field) (val_t *v, tty x) { v->ty = dyn_ty; v->v.field = x; }
static
void set_string SETTER(char *, TYPE::STRING, v_string)
static
void set_entity(val_t *v, entity_t *e)
{
v->ty = TYPE::ENTITY;
v->v.v_int = e->id;
}
static
void set_invocation(val_t *v, invocation_t *i)
{
v->ty = TYPE::INVOCATION;
v->v.v_int = i->bl.id;
}
static
void set_spell SETTER(spell_t *, TYPE::SPELL, v_spell)
#define setenv(f, v, x) f(&(env->vars[v]), x)
#define set_env_int(v, x) setenv(set_int, v, x)
#define set_env_dir(v, x) setenv(set_dir, v, x)
#define set_env_string(v, x) setenv(set_string, v, x)
#define set_env_entity(v, x) setenv(set_entity, v, x)
#define set_env_location(v, x) setenv(set_location, v, x)
#define set_env_area(v, x) setenv(set_area, v, x)
#define set_env_invocation(v, x) setenv(set_invocation, v, x)
#define set_env_spell(v, x) setenv(set_spell, v, x)
#pragma GCC diagnostic pop
magic_conf_t magic_conf; /* Global magic conf */
env_t magic_default_env = { &magic_conf, NULL };
static
int spells_sorted = 0;
const char *magic_find_invocation(const char *spellname)
{
int i;
for (i = 0; i < abs(magic_conf.spells_nr); i++)
if (!strcmp(magic_conf.spells[i]->name, spellname))
return magic_conf.spells[i]->invocation;
return NULL;
}
static
int spell_compare(const void *lhs, const void *rhs)
{
return strcmp((*((const spell_t *const*) lhs))->invocation,
(*((const spell_t *const*) rhs))->invocation);
}
spell_t *magic_find_spell(char *invocation)
{
spell_t key;
spell_t *keyp = &key;
spell_t **retval;
if (!spells_sorted)
{
qsort(magic_conf.spells, magic_conf.spells_nr, sizeof(spell_t *),
spell_compare);
spells_sorted = 1;
}
key.invocation = invocation;
retval =
((spell_t **)
bsearch(&keyp, magic_conf.spells, magic_conf.spells_nr,
sizeof(spell_t *), spell_compare));
if (!retval)
return NULL;
else
return *retval;
}
/* -------------------------------------------------------------------------------- */
/* Spell anchors */
/* -------------------------------------------------------------------------------- */
static
int compare_teleport_anchor(const void *lhs, const void *rhs)
{
return strcmp((*((const teleport_anchor_t *const*) lhs))->invocation,
(*((const teleport_anchor_t *const*) rhs))->invocation);
}
const char *magic_find_anchor_invocation(const char *anchor_name)
{
int i;
for (i = 0; i < abs(magic_conf.anchors_nr); i++)
if (!strcmp(magic_conf.anchors[i]->name, anchor_name))
return magic_conf.anchors[i]->invocation;
return NULL;
}
teleport_anchor_t *magic_find_anchor(char *name)
{
teleport_anchor_t key;
teleport_anchor_t *keyp = &key;
teleport_anchor_t **retval;
if (magic_conf.anchors_nr > 0)
{ /* unsorted */
qsort(magic_conf.anchors, magic_conf.anchors_nr,
sizeof(teleport_anchor_t *), compare_teleport_anchor);
magic_conf.anchors_nr = -magic_conf.anchors_nr;
}
key.invocation = name;
retval = (teleport_anchor_t **) bsearch(&keyp,
magic_conf.anchors,
-magic_conf.anchors_nr,
sizeof(teleport_anchor_t *),
compare_teleport_anchor);
if (!retval)
return NULL;
else
return *retval;
}
/* -------------------------------------------------------------------------------- */
/* Spell guard checks */
/* -------------------------------------------------------------------------------- */
static
env_t *alloc_env(magic_conf_t *conf)
{
env_t *env;
CREATE(env, env_t, 1);
CREATE(env->vars, val_t, conf->vars_nr);
env->base_env = conf;
return env;
}
static
env_t *clone_env(env_t *src)
{
env_t *retval = alloc_env(src->base_env);
int i;
for (i = 0; i < src->base_env->vars_nr; i++)
magic_copy_var(&retval->vars[i], &src->vars[i]);
return retval;
}
void magic_free_env(env_t *env)
{
int i;
for (i = 0; i < env->base_env->vars_nr; i++)
magic_clear_var(&env->vars[i]);
free(env);
}
env_t *spell_create_env(magic_conf_t *conf, spell_t *spell,
character_t *caster, int spellpower, char *param)
{
env_t *env = alloc_env(conf);
switch (spell->spellarg_ty)
{
case SPELLARG::STRING:
set_env_string(spell->arg, param);
break;
case SPELLARG::PC:
{
character_t *subject = map_nick2sd(param);
if (!subject)
subject = caster;
set_env_entity(spell->arg, &subject->bl);
free(param);
break;
}
case SPELLARG::NONE:
free(param);
break;
default:
free(param);
FPRINTF(stderr, "Unexpected spellarg type %d\n",
spell->spellarg_ty);
}
set_env_entity(VAR_CASTER, &caster->bl);
set_env_int(VAR_SPELLPOWER, spellpower);
set_env_spell(VAR_SPELL, spell);
return env;
}
static
void free_components(component_t ** component_holder)
{
if (*component_holder == NULL)
return;
free_components(&(*component_holder)->next);
free(*component_holder);
*component_holder = NULL;
}
void magic_add_component(component_t ** component_holder, int id, int count)
{
if (count <= 0)
return;
if (*component_holder == NULL)
{
component_t *component =
(component_t *) malloc(sizeof(component_t));
component->next = NULL;
component->item_id = id;
component->count = count;
*component_holder = component;
}
else
{
component_t *component = *component_holder;
if (component->item_id == id)
{
component->count += count;
return;
}
else
magic_add_component(&component->next, id, count);
/* Tail-recurse; gcc can optimise this. Not that it matters. */
}
}
static
void copy_components(component_t ** component_holder, component_t *component)
{
if (component == NULL)
return;
magic_add_component(component_holder, component->item_id,
component->count);
copy_components(component_holder, component->next);
}
typedef struct spellguard_check
{
component_t *catalysts, *components;
int mana;
interval_t casttime;
} spellguard_check_t;
static
int check_prerequisites(character_t *caster, component_t *component)
{
while (component)
{
if (pc_count_all_items(caster, component->item_id)
< component->count)
return 0; /* insufficient */
component = component->next;
}
return 1;
}
static
void consume_components(character_t *caster, component_t *component)
{
while (component)
{
pc_remove_items(caster, component->item_id, component->count);
component = component->next;
}
}
static
int spellguard_can_satisfy(spellguard_check_t *check, character_t *caster,
env_t *env, int *near_miss)
{
tick_t tick = gettick();
int retval = check_prerequisites(caster, check->catalysts);
if (retval && near_miss)
*near_miss = 1; // close enough!
retval = retval && caster->cast_tick <= tick /* Hasn't cast a spell too recently */
&& check->mana <= caster->status.sp
&& check_prerequisites(caster, check->components);
if (retval)
{
interval_t casttime = check->casttime;
if (VAR(VAR_MIN_CASTTIME).ty == TYPE::INT)
casttime = max(casttime, static_cast<interval_t>(VAR(VAR_MIN_CASTTIME).v.v_int));
caster->cast_tick = tick + casttime; /* Make sure not to cast too frequently */
consume_components(caster, check->components);
pc_heal(caster, 0, -check->mana);
}
return retval;
}
static
effect_set_t *spellguard_check_sub(spellguard_check_t *check,
spellguard_t *guard,
character_t *caster, env_t *env,
int *near_miss)
{
if (guard == NULL)
return NULL;
switch (guard->ty)
{
case SPELLGUARD::CONDITION:
if (!magic_eval_int(env, guard->s.s_condition))
return NULL;
break;
case SPELLGUARD::COMPONENTS:
copy_components(&check->components, guard->s.s_components);
break;
case SPELLGUARD::CATALYSTS:
copy_components(&check->catalysts, guard->s.s_catalysts);
break;
case SPELLGUARD::CHOICE:
{
spellguard_check_t altcheck = *check;
effect_set_t *retval;
altcheck.components = NULL;
altcheck.catalysts = NULL;
copy_components(&altcheck.catalysts, check->catalysts);
copy_components(&altcheck.components, check->components);
retval =
spellguard_check_sub(&altcheck, guard->next, caster, env,
near_miss);
free_components(&altcheck.catalysts);
free_components(&altcheck.components);
if (retval)
return retval;
else
return spellguard_check_sub(check, guard->s.s_alt, caster,
env, near_miss);
}
case SPELLGUARD::MANA:
check->mana += magic_eval_int(env, guard->s.s_mana);
break;
case SPELLGUARD::CASTTIME:
check->casttime += static_cast<interval_t>(magic_eval_int(env, guard->s.s_mana));
break;
case SPELLGUARD::EFFECT:
if (spellguard_can_satisfy(check, caster, env, near_miss))
return &guard->s.s_effect;
else
return NULL;
default:
FPRINTF(stderr, "Unexpected spellguard type %d\n",
guard->ty);
return NULL;
}
return spellguard_check_sub(check, guard->next, caster, env, near_miss);
}
static
effect_set_t *check_spellguard(spellguard_t *guard,
character_t *caster, env_t *env,
int *near_miss)
{
spellguard_check_t check;
effect_set_t *retval;
check.catalysts = NULL;
check.components = NULL;
check.mana = 0;
check.casttime = interval_t::zero();
retval = spellguard_check_sub(&check, guard, caster, env, near_miss);
free_components(&check.catalysts);
free_components(&check.components);
return retval;
}
/* -------------------------------------------------------------------------------- */
/* Public API */
/* -------------------------------------------------------------------------------- */
effect_set_t *spell_trigger(spell_t *spell, character_t *caster,
env_t *env, int *near_miss)
{
int i;
spellguard_t *guard = spell->spellguard;
if (near_miss)
*near_miss = 0;
for (i = 0; i < spell->letdefs_nr; i++)
magic_eval(env,
&env->vars[spell->letdefs[i].id], spell->letdefs[i].expr);
return check_spellguard(guard, caster, env, near_miss);
}
static
void spell_set_location(invocation_t *invocation, entity_t *entity)
{
magic_clear_var(&invocation->env->vars[VAR_LOCATION]);
invocation->env->vars[VAR_LOCATION].ty = TYPE::LOCATION;
invocation->env->vars[VAR_LOCATION].v.v_location.m = entity->m;
invocation->env->vars[VAR_LOCATION].v.v_location.x = entity->x;
invocation->env->vars[VAR_LOCATION].v.v_location.y = entity->y;
}
void spell_update_location(invocation_t *invocation)
{
if (bool(invocation->spell->flags & SPELL_FLAG::LOCAL))
return;
else
{
character_t *owner = (character_t *) map_id2bl(invocation->subject);
if (!owner)
return;
spell_set_location(invocation, (entity_t *) owner);
}
}
invocation_t *spell_instantiate(effect_set_t *effect_set, env_t *env)
{
invocation_t *retval;
CREATE(retval, invocation_t, 1);
entity_t *caster;
retval->env = env;
retval->caster = VAR(VAR_CASTER).v.v_int;
retval->spell = VAR(VAR_SPELL).v.v_spell;
retval->stack_size = 0;
retval->current_effect = effect_set->effect;
retval->trigger_effect = effect_set->at_trigger;
retval->end_effect = effect_set->at_end;
caster = map_id2bl(retval->caster); // must still exist
retval->bl.id = map_addobject(&retval->bl);
retval->bl.type = BL::SPELL;
retval->bl.m = caster->m;
retval->bl.x = caster->x;
retval->bl.y = caster->y;
map_addblock(&retval->bl);
set_env_invocation(VAR_INVOCATION, retval);
return retval;
}
invocation_t *spell_clone_effect(invocation_t *base)
{
invocation_t *retval = (invocation_t *) calloc(1, sizeof(invocation_t));
env_t *env;
memcpy(retval, base, sizeof(invocation_t));
retval->env = clone_env(retval->env);
env = retval->env;
retval->current_effect = retval->trigger_effect;
retval->next_invocation = NULL;
retval->end_effect = NULL;
retval->script_pos = 0;
retval->stack_size = 0;
// retval->timer = 0;
retval->subject = 0;
retval->status_change_refs_nr = 0;
retval->status_change_refs = NULL;
retval->flags = INVOCATION_FLAG::ZERO;
retval->bl.id = 0;
retval->bl.prev = NULL;
retval->bl.next = NULL;
retval->bl.id = map_addobject(&retval->bl);
set_env_invocation(VAR_INVOCATION, retval);
return retval;
}
void spell_bind(character_t *subject, invocation_t *invocation)
{
/* Only bind nonlocal spells */
if (!bool(invocation->spell->flags & SPELL_FLAG::LOCAL))
{
if (bool(invocation->flags & INVOCATION_FLAG::BOUND)
|| invocation->subject || invocation->next_invocation)
{
int *i = NULL;
FPRINTF(stderr,
"[magic] INTERNAL ERROR: Attempt to re-bind spell invocation `%s'\n",
invocation->spell->name);
*i = 1;
return;
}
invocation->next_invocation = subject->active_spells;
subject->active_spells = invocation;
invocation->flags |= INVOCATION_FLAG::BOUND;
invocation->subject = subject->bl.id;
}
spell_set_location(invocation, (entity_t *) subject);
}
int spell_unbind(character_t *subject, invocation_t *invocation)
{
invocation_t **seeker = &subject->active_spells;
while (*seeker)
{
if (*seeker == invocation)
{
*seeker = invocation->next_invocation;
invocation->flags &= ~INVOCATION_FLAG::BOUND;
invocation->next_invocation = NULL;
invocation->subject = 0;
return 0;
}
seeker = &((*seeker)->next_invocation);
}
return 1;
}
|