summaryrefslogtreecommitdiff
path: root/src/emap/craftconf.c
blob: 89b31ea5562463fbffd39bde0fc5c8e11be776a5 (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
// 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/conf.h"
#include "common/HPMi.h"
#include "common/memmgr.h"
#include "common/mmo.h"
#include "common/nullpo.h"
#include "common/socket.h"
#include "common/strlib.h"
#include "map/itemdb.h"
#include "map/npc.h"
#include "map/pc.h"
#include "map/quest.h"

#include "ecommon/utils/strutil.h"

#include "ecommon/struct/strutildata.h"

#include "emap/craftconf.h"

struct DBMap *craftconf_db = NULL;

struct craft_db_entry *craft_create_db_entry(const int id)
{
    struct craft_db_entry *entry = aCalloc(sizeof(struct craft_db_entry), 1);
    if (!entry)
        return NULL;
    VECTOR_INIT(entry->inventories);
    VECTOR_INIT(entry->create_items);
    VECTOR_INIT(entry->delete_items);
    VECTOR_INIT(entry->required_items);
    VECTOR_INIT(entry->required_equips);
    VECTOR_INIT(entry->required_skills);
    VECTOR_INIT(entry->required_quests);
    entry->selected_inventory = NULL;
//    entry->return_code = 0;
    return entry;
}

static bool craft_lookup_const(const struct config_setting_t *it, const char *name, int *value)
{
    if (libconfig->setting_lookup_int(it, name, value))
    {
        return true;
    }
    else
    {
        const char *str = NULL;
        if (libconfig->setting_lookup_string(it, name, &str))
        {
            if (*str && script->get_constant(str, value))
                return true;
        }
    }
    return false;
}

static bool craft_get_const(const struct config_setting_t *it, int *value)
{
    const char *str = libconfig->setting_get_string(it);
    if (str && *str && script->get_constant(str, value))
        return true;

    *value = libconfig->setting_get_int(it);
    return true;
}

static int craft_get_item_id(struct craft_db_entry *entry,
                             const char *const errorMessage,
                             const char *const name,
                             const char *const fieldName)
{
    if (!strcmp(name, "Empty") || !entry || !errorMessage)
        return 0;

    struct item_data* id = itemdb->search_name(name);
    if (!id)
    {
        ShowWarning(errorMessage, entry->id, fieldName, name);
        return 0;
    }
    return id->nameid;
}

static void craft_read_source_inventory(struct craft_db_entry *entry,
                                        struct config_setting_t *tt)
{
    int i32;
    int i = 0;
    if (!entry || !tt || !config_setting_is_group(tt))
        return;

    struct config_setting_t *item;

    int invLen = VECTOR_LENGTH(entry->inventories);
    VECTOR_ENSURE(entry->inventories, invLen + 1, 1);
    VECTOR_INSERTZEROED(entry->inventories, invLen);
    struct craft_db_inventory *inventory = &VECTOR_INDEX(entry->inventories, invLen);

    while((item = libconfig->setting_get_elem(tt, i)) && i < craft_inventory_size)
    {
        int amount = 0;
        const char *name = config_setting_name(item);
        int itemId = craft_get_item_id(entry, "Wrong item name in craft %d in field %s in: %s\n", name, "SourceItems");
        if (!itemId)
        {
            inventory->items[i].index = 0;
            inventory->items[i].amount = 0;
            i ++;
            continue;
        }
        if (craft_get_const(item, &i32) && i32 >= 0)
            amount = i32;

        if (amount < 1)
        {
            inventory->items[i].index = 0;
            inventory->items[i].amount = 0;
            ShowWarning("Wrong item amount in craft %d in field SourceItems in: %d\n", entry->id, amount);
            i ++;
            continue;
        }

        inventory->items[i].index = itemId;
        inventory->items[i].amount = amount;
        i ++;
    }
}

static void craft_read_create_items(struct craft_db_entry *entry,
                                    struct config_setting_t *tt)
{
    int i32;
    int i = 0;
    if (!entry || !tt || !config_setting_is_group(tt))
        return;

    struct config_setting_t *item;

    int invLen = VECTOR_LENGTH(entry->create_items);
    VECTOR_ENSURE(entry->create_items, invLen + 1, 1);
    VECTOR_INSERTZEROED(entry->create_items, invLen);
    struct craft_items_collection *collection = &VECTOR_INDEX(entry->create_items, invLen);
    VECTOR_INIT(*collection);
    int collecitonLen = VECTOR_LENGTH(*collection);

    while((item = libconfig->setting_get_elem(tt, i)))
    {
        int amount = 0;
        const char *name = config_setting_name(item);
        int itemId = craft_get_item_id(entry, "Wrong item name in craft %d in field %s in: %s\n", name, "CreateItems");
        if (!itemId)
        {
            i ++;
            continue;
        }
        if (craft_get_const(item, &i32) && i32 >= 0)
            amount = i32;

        if (amount < 1)
        {
            ShowWarning("Wrong item amount in craft %d in field CreateItems in: %d\n", entry->id, amount);
            i ++;
            continue;
        }

        VECTOR_ENSURE(*collection, collecitonLen + 1, 1);
        VECTOR_INSERTZEROED(*collection, collecitonLen);
        struct item_pair *pair = &VECTOR_INDEX(*collection, collecitonLen);
        pair->index = itemId;
        pair->amount = amount;

        collecitonLen ++;
        i ++;
    }
}

static void craft_read_items_collection(struct craft_db_entry *entry,
                                        struct craft_items_collection *vector,
                                        struct config_setting_t *t,
                                        const char *const fieldName,
                                        enum craft_field_type fieldType)
{
    int i32;
    int i = 0;
    if (!entry || !t || !vector)
        return;

    struct config_setting_t *tt = libconfig->setting_get_member(t, fieldName);

    if (!tt || !config_setting_is_group(tt))
        return;

    struct config_setting_t *item;

    int len = 0;
    while((item = libconfig->setting_get_elem(tt, i)))
    {
        int amount = 0;
        const char *name = config_setting_name(item);
        int itemId = 0;
        if (fieldType == CRAFT_ITEM)
        {
            itemId = craft_get_item_id(entry, "Wrong item name in craft %d in field %s in: %s\n", name, fieldName);
            if (!itemId)
            {
                i ++;
                continue;
            }
        }
        else if (fieldType == CRAFT_QUEST)
        {
            if (!script->get_constant(name, &itemId))
            {
                ShowWarning("Wrong quest name in craft %d in field %s in: %s\n", entry->id, fieldName, name);
                i ++;
                continue;
            }
            if (!quest->db(itemId))
            {
                ShowWarning("Wrong quest id in craft %d in field %s in: %s\n", entry->id, fieldName, name);
                i ++;
                continue;
            }
        }
        else if (fieldType == CRAFT_SKILL)
        {
            itemId = skill->name2id(name);
            if (!itemId)
            {
                ShowWarning("Wrong skill name in craft %d in field %s in: %s\n", entry->id, fieldName, name);
                i ++;
                continue;
            }
        }
        else if (fieldType == CRAFT_BOOL)
        {
            itemId = craft_get_item_id(entry, "Wrong item name in craft %d in field %s in: %s\n", name, fieldName);
            if (!itemId)
            {
                i ++;
                continue;
            }
        }

        if (fieldType == CRAFT_BOOL)
        {
            if (!libconfig->setting_get_bool(item))
            {
                i ++;
                continue;
            }
            amount = 1;
        }
        else if (craft_get_const(item, &i32) && i32 >= 0)
        {
            amount = i32;
        }

        if (amount < 1)
        {
            if (fieldType == CRAFT_ITEM)
            {
                ShowWarning("Wrong item amount in craft %d in field %s in: %d\n", entry->id, fieldName, amount);
            }
            else if (fieldType == CRAFT_QUEST)
            {
                ShowWarning("Wrong quest level in craft %d in field %s in: %d\n", entry->id, fieldName, amount);
            }
            else if (fieldType == CRAFT_SKILL)
            {
                ShowWarning("Wrong skill level in craft %d in field %s in: %d\n", entry->id, fieldName, amount);
            }
            i ++;
            continue;
        }

        VECTOR_ENSURE(*vector, len + 1, 1);
        VECTOR_INSERTZEROED(*vector, len);
        struct item_pair *pair = &VECTOR_INDEX(*vector, len);
        len ++;

        pair->index = itemId;
        pair->amount = amount;
        //ShowInfo("%s: itemId=%d:%d\n", fieldName, itemId, amount);
        i ++;
    }
}

#define readField(name, var, def) \
    if (craft_lookup_const(craftt, name, &i32) && i32 >= 0) \
    { \
        entry->var = i32; \
    } \
    else \
    { \
        entry->var = def; \
    }

static bool craft_read_db_sub(struct config_setting_t *craftt, int id, const char *source)
{
    int class_;
    int i32;
    const char *str = NULL;
    struct config_setting_t *t;

    if (!libconfig->setting_lookup_int(craftt, "Id", &class_))
    {
        ShowWarning("craft_read_db_sub: Missing id in \"%s\", entry #%d, skipping.\n", source, class_);
        return false;
    }
    if (!libconfig->setting_lookup_string(craftt, "Name", &str) || !*str)
    {
        ShowWarning("craft_read_db_sub: Missing Name in craft %d of \"%s\", skipping.\n", class_, source);
        return false;
    }

    struct craft_db_entry *entry = craft_create_db_entry(class_);
    entry->id = class_;
    idb_put(craftconf_db, class_, entry);

    safestrncpy(entry->name, str, sizeof(entry->name));

    script->set_constant2(entry->name, entry->id, false, false);

    //ShowInfo("Craft: id=%d, name=%s\n", class_, entry->name);

    readField("Priority", priority, 0);
    readField("Price", price, 0);
    readField("Lv", level, 0);
    readField("Flag", flag, -1);
    readField("ReturnCode", return_code, 0);

    if ((t = libconfig->setting_get_member(craftt, "SourceItems")) && config_setting_is_list(t))
    {
        int i, len = libconfig->setting_length(t);

        for (i = 0; i < len; i++)
        {
            craft_read_source_inventory(entry, libconfig->setting_get_elem(t, i));
        }
    }

    if ((t = libconfig->setting_get_member(craftt, "CreateItems")) && config_setting_is_list(t))
    {
        int i, len = libconfig->setting_length(t);

        for (i = 0; i < len; i++)
        {
            craft_read_create_items(entry, libconfig->setting_get_elem(t, i));
        }
    }

    craft_read_items_collection(entry, &entry->delete_items, craftt, "DeleteItems", CRAFT_ITEM);
    craft_read_items_collection(entry, &entry->required_items, craftt, "RequiredItems", CRAFT_ITEM);
    craft_read_items_collection(entry, &entry->required_skills, craftt, "RequiredSkills", CRAFT_ITEM);
    craft_read_items_collection(entry, &entry->required_quests, craftt, "RequiredQuests", CRAFT_QUEST);
    craft_read_items_collection(entry, &entry->required_equips, craftt, "RequiredEquips", CRAFT_BOOL);

    return true;
}
#undef readField

static void load_craft_db(const char *filename)
{
    struct config_t craft_db_conf;
    char filepath[256];
    struct config_setting_t *cdb;
    struct config_setting_t *t;
    int i = 0;

    nullpo_retv(filename);
    sprintf(filepath, "%s/%s", map->db_path, filename);

    if (!libconfig->load_file(&craft_db_conf, filepath) ||
        !(cdb = libconfig->setting_get_member(craft_db_conf.root, "craft_db")))
    {
        ShowError("can't read %s\n", filepath);
        return;
    }

    while ((t = libconfig->setting_get_elem(cdb, i++)))
    {
        craft_read_db_sub(t, i - 1, filepath);
    }
    libconfig->destroy(&craft_db_conf);
    ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", i, filepath);

}

void do_init_craftconf(void)
{
    craftconf_db = idb_alloc(DB_OPT_RELEASE_BOTH);
    load_craft_db("craft_db.conf");
}

static void delete_craft_entry(struct craft_db_entry *entry)
{
    if (!entry)
        return;
    VECTOR_CLEAR(entry->inventories);
    VECTOR_CLEAR(entry->delete_items);
    VECTOR_CLEAR(entry->required_items);
    VECTOR_CLEAR(entry->required_equips);
    VECTOR_CLEAR(entry->required_skills);
    VECTOR_CLEAR(entry->required_quests);
    const int len = VECTOR_LENGTH(entry->create_items);
    int f;
    for (f = 0; f < len; f ++)
    {
        struct craft_items_collection *collection = &VECTOR_INDEX(
            entry->create_items, f);
        VECTOR_CLEAR(*collection);
    }
    VECTOR_CLEAR(entry->create_items);
}

static int delete_craftconf_sub(union DBKey key __attribute__ ((unused)),
                                struct DBData *data,
                                va_list args __attribute__ ((unused)))
{
    struct craft_db_entry *craft = DB->data2ptr(data);
    if (!craft)
        return 0;

    delete_craft_entry(craft);
    return 0;
}

void do_final_craftconf(void)
{
    craftconf_db->destroy(craftconf_db, delete_craftconf_sub);
}