summaryrefslogtreecommitdiff
path: root/src/emap/mob.c
blob: b4f4f3106304421375a6249c56f82ad2e3016845 (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
// 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/cbasetypes.h"
#include "common/conf.h"
#include "common/HPMi.h"
#include "common/memmgr.h"
#include "common/mmo.h"
#include "common/socket.h"
#include "common/strlib.h"
#include "common/timer.h"
#include "map/battle.h"
#include "map/itemdb.h"
#include "map/mob.h"
#include "map/script.h"

#include "plugins/HPMHooking.h"

#include "emap/map.h"
#include "emap/mob.h"

#include "emap/data/mobd.h"
#include "emap/struct/mobdext.h"

int emob_deleteslave_sub_pre(struct block_list **blPtr,
                             va_list ap)
{
    struct block_list *bl = *blPtr;
    if (!bl)
    {
        hookStop();
        return 0;
    }
    TBL_MOB *md = (TBL_MOB *)bl;
    if (!md)
    {
        hookStop();
        return 0;
    }

    const int id = va_arg(ap, int);
    if (md->master_id > 0 && md->master_id == id)
    {
        if (md->db->status.mode & MD_SURVIVE_WITHOUT_MASTER)
        {
            md->master_id = 0;
            md->master_dist = 0;
        }
        else
        {
            status_kill(bl);
        }
    }

    hookStop();
    return 0;
}

static void emob_load_weaponattacks(const char *type,
                                    int val,
                                    struct MobdExt *data,
                                    struct mob_db *entry)
{
    int key = -1;
    if (strcmp(type, "NoWeapon") == 0)
    {
        key = W_FIST;
    }
    else if (strcmp(type, "Daggers") == 0)
    {
        key = W_DAGGER;
    }
    else if (strcmp(type, "1HSwords") == 0)
    {
        key = W_1HSWORD;
    }
    else if (strcmp(type, "2HSwords") == 0)
    {
        key = W_2HSWORD;
    }
    else if (strcmp(type, "1HSpears") == 0)
    {
        key = W_1HSPEAR;
    }
    else if (strcmp(type, "2HSpears") == 0)
    {
        key = W_2HSPEAR;
    }
    else if (strcmp(type, "1HAxes") == 0)
    {
        key = W_1HAXE;
    }
    else if (strcmp(type, "2HAxes") == 0)
    {
        key = W_2HAXE;
    }
    else if (strcmp(type, "Maces") == 0)
    {
        key = W_MACE;
    }
    else if (strcmp(type, "2HMaces") == 0)
    {
        key = W_2HMACE;
    }
    else if (strcmp(type, "Staves") == 0)
    {
        key = W_STAFF;
    }
    else if (strcmp(type, "Bows") == 0)
    {
        key = W_BOW;
    }
    else if (strcmp(type, "Knuckles") == 0)
    {
        key = W_KNUCKLE;
    }
    else if (strcmp(type, "Instruments") == 0)
    {
        key = W_MUSICAL;
    }
    else if (strcmp(type, "Whips") == 0)
    {
        key = W_WHIP;
    }
    else if (strcmp(type, "Books") == 0)
    {
        key = W_BOOK;
    }
    else if (strcmp(type, "Katars") == 0)
    {
        key = W_KATAR;
    }
    else if (strcmp(type, "Revolvers") == 0)
    {
        key = W_REVOLVER;
    }
    else if (strcmp(type, "Rifles") == 0)
    {
        key = W_RIFLE;
    }
    else if (strcmp(type, "GatlingGuns") == 0)
    {
        key = W_GATLING;
    }
    else if (strcmp(type, "Shotguns") == 0)
    {
        key = W_SHOTGUN;
    }
    else if (strcmp(type, "GrenadeLaunchers") == 0)
    {
        key = W_GRENADE;
    }
    else if (strcmp(type, "FuumaShurikens") == 0)
    {
        key = W_HUUMA;
    }
    else if (strcmp(type, "2HStaves") == 0)
    {
        key = W_2HSTAFF;
    }
    else if (strcmp(type, "DWDaggers") == 0)
    {
        key = W_DOUBLE_DD;
    }
    else if (strcmp(type, "DWSwords") == 0)
    {
        key = W_DOUBLE_SS;
    }
    else if (strcmp(type, "DWAxes") == 0)
    {
        key = W_DOUBLE_AA;
    }
    else if (strcmp(type, "DWDaggerSword") == 0)
    {
        key = W_DOUBLE_DS;
    }
    else if (strcmp(type, "DWDaggerAxe") == 0)
    {
        key = W_DOUBLE_DA;
    }
    else if (strcmp(type, "DWSwordAxe") == 0)
    {
        key = W_DOUBLE_SA;
    }
    else if (strcmp(type, "All") == 0)
    {
        for (int f = 0; f < MAX_WEAPON_TYPE; f ++)
            data->weaponAttacks[f] = val;
    }
    else
    {
        ShowError("Mob %d. Unknown weapon type %s\n", entry->mob_id, type);
        return;
    }
    data->weaponAttacks[key] = val;
}

static void emob_load_skillattacks(const char *type,
                                   int val,
                                   struct MobdExt *data,
                                   struct mob_db *entry)
{
    int skill_id = 0;
    if (script->get_constant(type, &skill_id))
    {
        const int idx = skill->get_index(skill_id);
        data->skillAttacks[idx] = val;
    }
    else
    {
        ShowError("Mob %d. Unknown skill name %s\n", entry->mob_id, type);
    }
}

void emob_read_db_additional_fields_pre(struct mob_db **entryPtr,
                                        struct config_setting_t **itPtr,
                                        int *nPtr __attribute__ ((unused)),
                                        const char **sourcePtr __attribute__ ((unused)))
{
    int i32 = 0;

    struct MobdExt *data = mobd_get(*entryPtr);
    if (!data)
    {
        hookStop();
        return;
    }

    if (mob->lookup_const(*itPtr, "WalkMask", &i32))
        data->walkMask = i32;

    struct config_setting_t *tt = libconfig->setting_get_member(*itPtr, "WeaponAttacks");

    if (tt && config_setting_is_group(tt))
    {
        int j = 0;
        struct config_setting_t *wpt = NULL;
        while ((wpt = libconfig->setting_get_elem(tt, j++)) != NULL)
        {
            emob_load_weaponattacks(config_setting_name(wpt), libconfig->setting_get_int(wpt), data, *entryPtr);
        }
    }

    tt = libconfig->setting_get_member(*itPtr, "SkillAttacks");

    if (tt && config_setting_is_group(tt))
    {
        int j = 0;
        struct config_setting_t *wpt = NULL;
        while ((wpt = libconfig->setting_get_elem(tt, j++)) != NULL)
        {
            emob_load_skillattacks(config_setting_name(wpt), libconfig->setting_get_int(wpt), data, *entryPtr);
        }
    }

    if ((tt = libconfig->setting_get_member(*itPtr, "SpawnCollisionSize")))
    {
        if (config_setting_is_aggregate(tt))
        {
            data->collisionDx = libconfig->setting_get_int_elem(tt, 0);
            if (libconfig->setting_length(tt) >= 2)
                data->collisionDy = libconfig->setting_get_int_elem(tt, 1);
            else if (libconfig->setting_length(tt) >= 1)
                data->collisionDy = data->collisionDx;
        }
        else if (libconfig->setting_lookup_int(*itPtr, "SpawnCollisionSize", &i32))
        {
            data->collisionDx = i32;
            data->collisionDy = i32;
        }
    }

    if (mob->lookup_const(*itPtr, "SpawnCollisionMask", &i32))
        data->collisionMask = i32;
}

uint32 emob_read_db_mode_sub_post(uint32 retVal,
                                  struct mob_db *entry  __attribute__ ((unused)),
                                  struct config_setting_t *t)
{
    struct config_setting_t *t2;

    if ((t2 = libconfig->setting_get_member(t, "SurviveWithoutMaster")))
        retVal |= libconfig->setting_get_bool(t2) ? MD_SURVIVE_WITHOUT_MASTER : 0;

    return retVal;
}

struct mob_data *emob_spawn_dataset_post(struct mob_data *retVal,
                                         struct spawn_data *data __attribute__ ((unused)))
{
    if (retVal)
    {
        struct MobdExt *ext = mobd_get_by_mob(retVal);
        const int dx = ext->collisionDx;
        const int dy = ext->collisionDy;
        if (ext && dx != -1 && dy != -1)
        {
            char name[100];
            const int m = retVal->bl.m;
            sprintf(name, "mob_spawn_%d_%d", m, retVal->bl.id);
            emap_iwall_set2(m,
                0,
                retVal->bl.x - dx, retVal->bl.y - dy,
                retVal->bl.x + dx, retVal->bl.y + dy,
                ext->collisionMask,
                name);
        }
    }
    return retVal;
}

int emob_dead_pre(struct mob_data **mdPtr,
                  struct block_list **srcPtr __attribute__ ((unused)),
                  int *typePtr __attribute__ ((unused)))
{
    struct mob_data *md = *mdPtr;
    if (md)
    {
        struct MobdExt *ext = mobd_get_by_mob(md);
        if (ext)
        {
            char name[100];
            const int m = md->bl.m;
            sprintf(name, "mob_spawn_%d_%d", m, md->bl.id);
            map->iwall_remove(name);
        }
    }
    return 3;
}