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
|
// 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/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/craft.h"
#include "emap/lang.h"
struct DBMap *craftvar_db = NULL;
int craft_counter = 0;
void do_init_craft(void)
{
craftvar_db = idb_alloc(DB_OPT_RELEASE_BOTH);
}
static void delete_craft_var(struct craft_vardata *craft)
{
if (!craft)
return;
int index;
for (index = 0; index < craft_inventory_size; index ++)
{
struct craft_slot *slot = &craft->slots[index];
VECTOR_CLEAR(slot->items);
}
}
static int delete_craftvar_sub(DBKey key __attribute__ ((unused)),
DBData *data,
va_list args __attribute__ ((unused)))
{
struct craft_vardata *craft = DB->data2ptr(data);
if (!craft)
return 0;
delete_craft_var(craft);
return 0;
}
void do_final_craft(void)
{
craftvar_db->destroy(craftvar_db, delete_craftvar_sub);
}
bool craft_checkstr(TBL_PC *sd, const char *craftstr)
{
if (!sd || !craftstr)
return false;
int amounts[MAX_INVENTORY];
int f;
for (f = 0; f < MAX_INVENTORY; f ++)
amounts[f] = 0;
struct strutil_data *craftdata = strutil_split(craftstr, '|', craft_inventory_size + 1);
if (!craftdata)
return false;
if (craftdata->len < 1)
{
strutil_free(craftdata);
return false;
}
int index;
for (index = 0; index < craftdata->len; index ++)
{
const char *slotstr = VECTOR_INDEX(craftdata->parts, index + 1);
if (!slotstr || !*slotstr)
continue;
struct strutil_data *slotdata = strutil_split(slotstr, ';', MAX_INVENTORY + 1);
if (!slotdata)
{
strutil_free(slotdata);
continue;
}
int slot;
int item_id = 0;
for (slot = 0; slot < slotdata->len; slot ++)
{
const char *itemstr = VECTOR_INDEX(slotdata->parts, slot + 1);
if (!itemstr || !*itemstr)
continue;
int index;
int amount;
int cnt = sscanf(itemstr, "%d,%d", &index, &amount);
if (cnt != 2)
{
index = atoi(itemstr);
amount = 1;
}
if (index < 0 || index >= MAX_INVENTORY)
{ // wrong item index
strutil_free(slotdata);
strutil_free(craftdata);
return false;
}
const int new_item = sd->status.inventory[index].nameid;
if (item_id != 0 && new_item != item_id)
{ // different item id in same slot
strutil_free(slotdata);
strutil_free(craftdata);
return false;
}
if (new_item != 0)
item_id = new_item;
amounts[index] += amount;
if (amounts[index] > 32000)
{ // slot overflow
strutil_free(slotdata);
strutil_free(craftdata);
return false;
}
}
strutil_free(slotdata);
}
strutil_free(craftdata);
for (f = 0; f < MAX_INVENTORY; f ++)
{
const int amount = amounts[f];
if (!amount)
continue;
if(sd->status.inventory[f].nameid == 0 ||
sd->status.inventory[f].amount < amount)
{
return false;
}
}
return true;
}
struct craft_vardata *craft_str_to_craft(const char *craftstr)
{
struct strutil_data *craftdata = strutil_split(craftstr, '|', craft_inventory_size + 1);
if (!craftdata)
return false;
if (craftdata->len < 1)
{
strutil_free(craftdata);
return NULL;
}
struct craft_vardata *vardata = aCalloc(1, sizeof(struct craft_vardata));
int index;
for (index = 0; index < craftdata->len; index ++)
{
const char *slotstr = VECTOR_INDEX(craftdata->parts, index + 1);
if (!slotstr || !*slotstr)
continue;
struct strutil_data *slotdata = strutil_split(slotstr, ';', MAX_INVENTORY + 1);
if (!slotdata)
{
strutil_free(slotdata);
continue;
}
struct craft_slot *crslot = &vardata->slots[index];
VECTOR_INIT(crslot->items);
int slot;
for (slot = 0; slot < slotdata->len; slot ++)
{
const char *itemstr = VECTOR_INDEX(slotdata->parts, slot + 1);
if (!itemstr || !*itemstr)
continue;
int index;
int amount;
int cnt = sscanf(itemstr, "%d,%d", &index, &amount);
if (cnt != 2)
{
index = atoi(itemstr);
amount = 1;
}
if (index < 0 ||
index >= MAX_INVENTORY)
{ // wrong item index
strutil_free(slotdata);
strutil_free(craftdata);
return false;
}
VECTOR_ENSURE(crslot->items, slot + 1, 1);
VECTOR_INSERTZEROED(crslot->items, slot);
struct item_pair *pair = &VECTOR_INDEX(crslot->items, slot);
pair->index = index;
pair->amount = amount;
}
strutil_free(slotdata);
}
strutil_free(craftdata);
return vardata;
}
int str_to_craftvar(TBL_PC *sd, const char *craftstr)
{
if (!craft_checkstr(sd, craftstr))
{
if (sd)
ShowWarning("invalid craft: %d\n", sd->bl.id);
return -1;
}
struct craft_vardata *craft = craft_str_to_craft(craftstr);
if (!craft)
return -1;
craft_counter ++;
idb_put(craftvar_db, craft_counter, craft);
return craft_counter;
}
void craft_dump(TBL_PC *sd, const int id)
{
struct craft_vardata *craft = idb_get(craftvar_db, id);
if (!craft)
{
ShowError("Craft object with id %d not exists.\n", id);
return;
}
int index;
ShowInfo("craft dump: %d\n", id);
for (index = 0; index < craft_inventory_size; index ++)
{
struct craft_slot *crslot = &craft->slots[index];
const int len = VECTOR_LENGTH(crslot->items);
int slot;
if (len > 0)
ShowInfo(" index: %d\n", index);
for (slot = 0; slot < len; slot ++)
{
struct item_pair *pair = &VECTOR_INDEX(crslot->items, slot);
const int invIndex = pair->index;
if (invIndex >= 0 &&
invIndex < MAX_INVENTORY &&
sd->status.inventory[invIndex].nameid)
{
const int item_id = sd->status.inventory[invIndex].nameid;
const struct item_data *i_data = itemdb->search(item_id);
if (i_data)
{
ShowInfo(" item: %d (%s,%d), %d\n",
invIndex,
lang_pctrans(i_data->jname, sd),
item_id,
pair->amount);
}
else
{
ShowInfo(" item: %d, %d\n", invIndex, pair->amount);
}
}
else
{
ShowInfo(" item: %d, %d\n", invIndex, pair->amount);
}
}
}
}
void craft_delete(const int id)
{
struct craft_vardata *craft = idb_get(craftvar_db, id);
if (!craft)
{
ShowError("Craft object with id %d not exists.\n", id);
return;
}
delete_craft_var(craft);
idb_remove(craftvar_db, id);
}
struct craft_slot *craft_get_slot(const int id, const int slot)
{
struct craft_vardata *craft = idb_get(craftvar_db, id);
if (!craft)
{
ShowError("Craft object with id %d not exists.\n", id);
return NULL;
}
if (slot < 0 || slot > craft_inventory_size)
{
ShowError("Wrong slot %d for craft with id %d.\n", slot, id);
return NULL;
}
return &craft->slots[slot];
}
bool craft_validate(TBL_PC *sd, const int id)
{
struct craft_vardata *craft = idb_get(craftvar_db, id);
if (!craft)
{
ShowError("Craft object with id %d not exists.\n", id);
return false;
}
int amounts[MAX_INVENTORY];
int f;
for (f = 0; f < MAX_INVENTORY; f ++)
amounts[f] = 0;
int index;
for (index = 0; index < craft_inventory_size; index ++)
{
struct craft_slot *crslot = &craft->slots[index];
const int len = VECTOR_LENGTH(crslot->items);
int slot;
for (slot = 0; slot < len; slot ++)
{
struct item_pair *pair = &VECTOR_INDEX(crslot->items, slot);
const int invIndex = pair->index;
if (invIndex < 0 ||
invIndex >= MAX_INVENTORY ||
!sd->status.inventory[invIndex].nameid ||
!sd->status.inventory[invIndex].amount)
{
return false;
}
amounts[invIndex] += pair->amount;
}
}
for (f = 0; f < MAX_INVENTORY; f ++)
{
const int amount = amounts[f];
if (!amount)
continue;
if(sd->status.inventory[f].nameid == 0 ||
sd->status.inventory[f].amount < amount)
{
return false;
}
}
return true;
}
|