summaryrefslogtreecommitdiff
path: root/src/emap/script.c
blob: d2a64e4a40888a22a845d39b9f43bb35a38d448a (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
// Copyright (c) Copyright (c) Hercules Dev Team, licensed under GNU GPL.
// Copyright (c) 2014 - 2015 Evol developers

#include "common/hercules.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common/HPMi.h"
#include "common/memmgr.h"
#include "common/nullpo.h"
#include "map/achievement.h"
#include "map/npc.h"
#include "map/pc.h"
#include "map/script.h"

#include "plugins/HPMHooking.h"

#include "ecommon/enum/gender.h"

#include "emap/script.h"
#include "emap/map.h"
#include "emap/data/itemd.h"
#include "emap/data/npcd.h"
#include "emap/struct/itemdext.h"
#include "emap/struct/npcdext.h"
#include "emap/skill_const.h"

#define getExt2() \
    TBL_NPC *nd = NULL; \
    int num = (int)reference_uid(script->add_str(".id"), 0); \
    int id = (int)i64db_iget(n->vars, num); \
    if (!id) \
        id = st->oid; \
    nd = map->id2nd(id); \
    if (!nd) \
        return; \
    struct NpcdExt *ext = npcd_get(nd); \
    if (!ext) \
        return;

#define getExt2Ret(r) \
    TBL_NPC *nd = NULL; \
    int num = (int)reference_uid(script->add_str(".id"), 0); \
    int id = (int)i64db_iget(n->vars, num); \
    if (!id) \
        id = st->oid; \
    nd = map->id2nd(id); \
    if (!nd) \
        return r; \
    struct NpcdExt *ext = npcd_get(nd); \
    if (!ext) \
        return r;

#define getExt1() \
    TBL_NPC *nd = NULL; \
    int num = (int)reference_uid(script->add_str(".id"), 0); \
    int id = (int)i64db_iget(n->vars, num); \
    if (!id) \
        id = st->oid; \
    nd = map->id2nd(id); \
    if (!nd) \
        return; \

#define getExt1Return(r) \
    TBL_NPC *nd = NULL; \
    int num = (int)reference_uid(script->add_str(".id"), 0); \
    int id = (int)i64db_iget(n->vars, num); \
    if (!id) \
        id = st->oid; \
    nd = map->id2nd(id); \
    if (!nd) \
        return r;

int escript_reload_pre(void)
{
    map_clear_data();
    return 0;
}

void escript_load_parameters_pre(void)
{
    script->constdb_comment("Evol parameters");
    script->set_constant("ClientVersion", 10000, true, false);
    script->constdb_comment(NULL);
}

void escript_hardcoded_constants_pre(void)
{
    script->constdb_comment("Evol constants");
    script->set_constant("MAX_SLOTS", MAX_SLOTS, false, false);

    // genders: for use with the Sex param
    script->set_constant("Gender", SP_SEX, true, false);
    script->set_constant("GENDER_FEMALE", GENDER_FEMALE, false, false);
    script->set_constant("GENDER_MALE", GENDER_MALE, false, false);
    script->set_constant("GENDER_HIDDEN", GENDER_HIDDEN, false, false);

    // for servers that do not use sex/gender
    script->set_constant("BodyType", SP_SEX, true, false);
    script->set_constant("BODYTYPE_1", GENDER_HIDDEN, false, false);
    script->set_constant("BODYTYPE_2", GENDER_FEMALE, false, false);
    script->set_constant("BODYTYPE_3", GENDER_MALE, false, false);

    // npc subtypes
    script->set_constant("NPCSUBTYPE_WARP", WARP, false, false);
    script->set_constant("NPCSUBTYPE_SHOP", SHOP, false, false);
    script->set_constant("NPCSUBTYPE_SCRIPT", SCRIPT, false, false);
    script->set_constant("NPCSUBTYPE_CASHSHOP", CASHSHOP, false, false);
    script->set_constant("NPCSUBTYPE_TOMB", TOMB, false, false);
    script->constdb_comment(NULL);
    eskill_addskill_conststants();
}

// stripped copy from script_load_translations without actual translation loading.
void escript_load_translations_pre(void)
{
    if (map->minimal)
    {
        hookStop();
        return;
    }

    script->translation_db = strdb_alloc(DB_OPT_DUP_KEY, NAME_LENGTH * 2 + 1);

    if (script->languages)
    {
        int i;
        for (i = 0; i < script->max_lang_id; i++)
            aFree(script->languages[i]);
        aFree(script->languages);
    }
    script->languages = NULL;
    script->max_lang_id = 0;

    script->add_language("English");

    if (script->string_list)
        aFree(script->string_list);

    script->string_list = NULL;
    script->string_list_pos = 0;
    script->string_list_size = 0;

    map->default_lang_id = 0;
    hookStop();
}

void eset_reg_npcscope_num_pre(struct script_state **stPtr,
                               struct reg_db **nPtr,
                               int64 *num __attribute__ ((unused)),
                               const char **namePtr,
                               int *val)
{
    struct script_state *st = *stPtr;
    struct reg_db *n = *nPtr;
    const char *name = *namePtr;

    if (!strcmp(name, ".lang"))
    {
        getExt2();
        ext->language = *val;
        hookStop();
    }
    else if (!strcmp(name, ".sex"))
    {
        getExt1();
        clif->clearunit_area(&nd->bl, CLR_OUTSIGHT);
        nd->vd.sex = *val;
        clif->spawn(&nd->bl);
        hookStop();
    }
    else if (!strcmp(name, ".bodytype"))
    {
        getExt1();
        clif->clearunit_area(&nd->bl, CLR_OUTSIGHT);
        nd->vd.sex = *val;
        clif->spawn(&nd->bl);
        hookStop();
    }
    else if (!strcmp(name, ".distance"))
    {
        getExt1();
        nd->area_size = *val;
        hookStop();
    }
    else if (!strcmp(name, ".dir"))
    {
        getExt1();
        int newdir = *val;

        if (newdir < 0)
            newdir = 0;
        else if (newdir > 7)
            newdir = 7;

        nd->dir = newdir;
        npc->enable(nd->exname, 1);
        hookStop();
    }
    else if (!strcmp(name, ".x"))
    {
        ShowWarning("you cant assign '.x'\n");
        script->reportsrc(st);
        hookStop();
    }
    else if (!strcmp(name, ".y"))
    {
        ShowWarning("you cant assign '.y'.\n");
        script->reportsrc(st);
        hookStop();
    }
    else if (!strcmp(name, ".class"))
    {
        getExt1();
        int class_ = *val;
        if (nd->class_ != class_)
            npc->setclass(nd, class_);
        hookStop();
    }
    else if (!strcmp(name, ".speed"))
    {
        getExt1();
        unit->bl2ud2(&nd->bl); // ensure nd->ud is safe to edit
        nd->speed = *val;
        nd->ud->state.speed_changed = 1;
        hookStop();
    }
    else if (!strcmp(name, ".chat"))
    {
        ShowWarning("you cant assign '.chat'.\n");
        script->reportsrc(st);
        hookStop();
    }
    else if (!strcmp(name, ".sit"))
    {
        getExt1();
        nd->vd.dead_sit = (*val) ? 2 : 0;
        clif->sitting(&nd->bl);
        hookStop();
    }
    else if (!strcmp(name, ".stand"))
    {
        getExt1();
        nd->vd.dead_sit = (*val) ? 0 : 2;
        clif->sitting(&nd->bl);
        hookStop();
    }
    else if (!strcmp(name, ".walkmask"))
    {
        getExt2();
        ext->walkMask = *val;
        hookStop();
    }
    else if (!strcmp(name, ".alwaysVisible"))
    {
        getExt1();
        if (*val)
            map_alwaysVisible_add(&nd->bl);
        else
            map_alwaysVisible_delete(&nd->bl);
        hookStop();
    }
    else if (!strcmp(name, ".srcId"))
    {
        ShowWarning("you cant assign '.srcId'\n");
        script->reportsrc(st);
        hookStop();
    }
}

int eget_val_npcscope_num_pre(struct script_state **stPtr,
                              struct reg_db **nPtr,
                              struct script_data **dataPtr)
{
    struct script_state *st = *stPtr;
    struct reg_db *n = *nPtr;
    struct script_data *data = *dataPtr;
    const char *name = reference_getname(data);
    if (!strcmp(name, ".lang"))
    {
        getExt2Ret(0);
        hookStop();
        return ext->language;
    }
    else if (!strcmp(name, ".sex"))
    {
        getExt1Return(0);
        hookStop();
        return nd->vd.sex;
    }
    else if (!strcmp(name, ".bodytype"))
    {
        getExt1Return(0);
        hookStop();
        return nd->vd.sex;
    }
    else if (!strcmp(name, ".distance"))
    {
        getExt1Return(0);
        hookStop();
        return nd->area_size;
    }
    else if (!strcmp(name, ".dir"))
    {
        getExt1Return(0);
        hookStop();
        return nd->dir;
    }
    else if (!strcmp(name, ".x"))
    {
        getExt1Return(0);
        hookStop();
        return nd->bl.x;
    }
    else if (!strcmp(name, ".y"))
    {
        getExt1Return(0);
        hookStop();
        return nd->bl.y;
    }
    else if (!strcmp(name, ".class"))
    {
        getExt1Return(0);
        hookStop();
        return nd->class_;
    }
    else if (!strcmp(name, ".speed"))
    {
        getExt1Return(0);
        hookStop();
        return nd->speed;
    }
    else if (!strcmp(name, ".chat"))
    {
        getExt1Return(0);
        hookStop();
        return nd->chat_id;
    }
    else if (!strcmp(name, ".sit"))
    {
        getExt1Return(0);
        hookStop();
        return nd->vd.dead_sit == 2 ? 1 : 0;
    }
    else if (!strcmp(name, ".stand"))
    {
        getExt1Return(0);
        hookStop();
        return nd->vd.dead_sit == 0 ? 1 : 0;
    }
    else if (!strcmp(name, ".walkmask"))
    {
        getExt2Ret(0);
        hookStop();
        return ext->walkMask;
    }
    else if (!strcmp(name, ".alwaysVisible"))
    {
        getExt1Return(0);
        bool res = map_alwaysVisible_find(&nd->bl);
        hookStop();
        return res;
    }
    else if (!strcmp(name, ".srcId"))
    {
        getExt1Return(0);
        hookStop();
        return nd->src_id;
    }
    return 0;
}

void eset_reg_npcscope_str_pre(struct script_state **stPtr,
                               struct reg_db **nPtr,
                               int64 *num __attribute__ ((unused)),
                               const char **namePtr,
                               const char **strPtr)
{
    struct script_state *st = *stPtr;
    struct reg_db *n = *nPtr;
    const char *name = *namePtr;
    const char *str = *strPtr;

    if (!strcmp(name, ".map$"))
    {
        ShowWarning("you cant assign '.map$'.\n");
        script->reportsrc(st);
        hookStop();
    }
    else if (!strcmp(name, ".name$"))
    {
        getExt1();
        npc->setdisplayname(nd, str);
//      not working because cant sent brodcast with translated npc name. need add for_each function for this.
//        clif->clearunit_area(&nd->bl, CLR_OUTSIGHT);
//        safestrncpy(nd->name, str, sizeof(nd->name));
//        clif->spawn(&nd->bl);
        hookStop();
    }
    else if (!strcmp(name, ".extname$"))
    {
        ShowWarning("you cant assign '.extname$'.\n");
        script->reportsrc(st);
        hookStop();
    }
}

char *eget_val_npcscope_str_pre(struct script_state **stPtr,
                                struct reg_db **nPtr,
                                struct script_data **dataPtr)
{
    struct script_state *st = *stPtr;
    struct reg_db *n = *nPtr;
    struct script_data *data = *dataPtr;
    const char *name = reference_getname(data);
    if (!strcmp(name, ".map$"))
    {
        getExt1Return(0);
        hookStop();
        return map->list[nd->bl.m].name;
    }
    else if (!strcmp(name, ".name$"))
    {
        getExt1Return(0);
        hookStop();
        return nd->name;
    }
    else if (!strcmp(name, ".extname$"))
    {
        getExt1Return(0);
        hookStop();
        return nd->exname;
    }
    return NULL;
}

void escript_run_use_script_pre(struct map_session_data **sdPtr,
                                struct item_data **itemDataPtr,
                                int *oidPtr)
{
    nullpo_retv(*itemDataPtr);
    struct map_session_data *sd = *sdPtr;
    struct item_data *itemData = *itemDataPtr;
    const int oid = *oidPtr;
    if (oid == 0)
    {
        pc->setreg(sd, script->add_variable("@useType"), 0);
        script->current_item_id = itemData->nameid;
        script->run(itemData->script, 0, sd->bl.id, oid);
        script->current_item_id = 0;
    }
    else
    {
        struct ItemdExt *data = itemd_get(itemData);
        if (!data)
        {
            data->tmpUseType = 0;
            hookStop();
            return;
        }

        pc->setreg(sd, script->add_variable("@useType"), data->tmpUseType);
        script->current_item_id = itemData->nameid;
        script->run(itemData->script, 0, sd->bl.id, oid);
        script->current_item_id = 0;
        pc->setreg(sd, script->add_variable("@useType"), 0);
        data->tmpUseType = 0;
    }
    hookStop();
}

void script_run_item_amount_script(TBL_PC *sd,
                                   struct script_code *itemScript,
                                   int itemId,
                                   int amount)
{
    if (!itemScript)
        return;

    script->current_item_id = itemId;
    pc->setreg(sd, script->add_variable("@itemId"), itemId);
    pc->setreg(sd, script->add_variable("@itemAmount"), amount);
    script->run(itemScript, 0, sd->bl.id, npc->fake_nd->bl.id);
    pc->setreg(sd, script->add_variable("@itemId"), 0);
    pc->setreg(sd, script->add_variable("@itemAmount"), 0);
    script->current_item_id = 0;
}

void script_run_card_script(TBL_PC *sd,
                            struct script_code *itemScript,
                            int itemId,
                            int cardId)
{
    if (!itemScript)
        return;

    script->current_item_id = itemId;
    pc->setreg(sd, script->add_variable("@itemId"), itemId);
    pc->setreg(sd, script->add_variable("@cardId"), cardId);
    script->run(itemScript, 0, sd->bl.id, npc->fake_nd->bl.id);
    pc->setreg(sd, script->add_variable("@itemId"), 0);
    pc->setreg(sd, script->add_variable("@cardId"), 0);
    script->current_item_id = 0;
}