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
|
// Copyright (c) Hercules Dev Team, licensed under GNU GPL.
// See the LICENSE file
// Portions Copyright (c) Athena Dev Teams
#include "../common/nullpo.h"
#include "../common/strlib.h"
#include "../common/utils.h"
#include "clif.h"
#include "itemdb.h"
#include "atcommand.h"
#include "map.h"
#include "path.h"
#include "chrif.h"
#include "vending.h"
#include "pc.h"
#include "npc.h"
#include "skill.h"
#include "battle.h"
#include "log.h"
#include <stdio.h>
#include <string.h>
struct vending_interface vending_s;
/// Returns an unique vending shop id.
static inline unsigned int getid(void) {
return vending->next_id++;
}
/*==========================================
* Close shop
*------------------------------------------*/
void vending_closevending(struct map_session_data* sd) {
nullpo_retv(sd);
if( sd->state.vending ) {
sd->state.vending = false;
clif->closevendingboard(&sd->bl, 0);
idb_remove(vending->db, sd->status.char_id);
}
}
/*==========================================
* Request a shop's item list
*------------------------------------------*/
void vending_vendinglistreq(struct map_session_data* sd, unsigned int id) {
struct map_session_data* vsd;
nullpo_retv(sd);
if( (vsd = map->id2sd(id)) == NULL )
return;
if( !vsd->state.vending )
return; // not vending
if (!pc->can_give_items(sd) || !pc->can_give_items(vsd)) { //check if both GMs are allowed to trade
// GM is not allowed to trade
clif->message(sd->fd, msg_txt(246));
return;
}
sd->vended_id = vsd->vender_id; // register vending uid
clif->vendinglist(sd, id, vsd->vending);
}
/*==========================================
* Purchase item(s) from a shop
*------------------------------------------*/
void vending_purchasereq(struct map_session_data* sd, int aid, unsigned int uid, const uint8* data, int count) {
int i, j, cursor, w, new_ = 0, blank, vend_list[MAX_VENDING];
double z;
struct s_vending vend[MAX_VENDING]; // against duplicate packets
struct map_session_data* vsd = map->id2sd(aid);
nullpo_retv(sd);
if( vsd == NULL || !vsd->state.vending || vsd->bl.id == sd->bl.id )
return; // invalid shop
if( vsd->vender_id != uid ) { // shop has changed
clif->buyvending(sd, 0, 0, 6); // store information was incorrect
return;
}
if( !searchstore->queryremote(sd, aid) && ( sd->bl.m != vsd->bl.m || !check_distance_bl(&sd->bl, &vsd->bl, AREA_SIZE) ) )
return; // shop too far away
searchstore->clearremote(sd);
if( count < 1 || count > MAX_VENDING || count > vsd->vend_num )
return; // invalid amount of purchased items
blank = pc->inventoryblank(sd); //number of free cells in the buyer's inventory
// duplicate item in vending to check hacker with multiple packets
memcpy(&vend, &vsd->vending, sizeof(vsd->vending)); // copy vending list
// some checks
z = 0.; // zeny counter
w = 0; // weight counter
for( i = 0; i < count; i++ ) {
short amount = *(uint16*)(data + 4*i + 0);
short idx = *(uint16*)(data + 4*i + 2);
idx -= 2;
if( amount <= 0 )
return;
// check of item index in the cart
if( idx < 0 || idx >= MAX_CART )
return;
ARR_FIND( 0, vsd->vend_num, j, vsd->vending[j].index == idx );
if( j == vsd->vend_num )
return; //picked non-existing item
else
vend_list[i] = j;
z += ((double)vsd->vending[j].value * (double)amount);
if( z > (double)sd->status.zeny || z < 0. || z > (double)MAX_ZENY ) {
clif->buyvending(sd, idx, amount, 1); // you don't have enough zeny
return;
}
if( z + (double)vsd->status.zeny > (double)MAX_ZENY && !battle_config.vending_over_max ) {
clif->buyvending(sd, idx, vsd->vending[j].amount, 4); // too much zeny = overflow
return;
}
w += itemdb_weight(vsd->status.cart[idx].nameid) * amount;
if( w + sd->weight > sd->max_weight ) {
clif->buyvending(sd, idx, amount, 2); // you can not buy, because overweight
return;
}
//Check to see if cart/vend info is in sync.
if( vend[j].amount > vsd->status.cart[idx].amount )
vend[j].amount = vsd->status.cart[idx].amount;
// if they try to add packets (example: get twice or more 2 apples if marchand has only 3 apples).
// here, we check cumulative amounts
if( vend[j].amount < amount ) {
// send more quantity is not a hack (an other player can have buy items just before)
clif->buyvending(sd, idx, vsd->vending[j].amount, 4); // not enough quantity
return;
}
vend[j].amount -= amount;
switch( pc->checkadditem(sd, vsd->status.cart[idx].nameid, amount) ) {
case ADDITEM_EXIST:
break; //We'd add this item to the existing one (in buyers inventory)
case ADDITEM_NEW:
new_++;
if (new_ > blank)
return; //Buyer has no space in his inventory
break;
case ADDITEM_OVERAMOUNT:
return; //too many items
}
}
pc->payzeny(sd, (int)z, LOG_TYPE_VENDING, vsd);
if( battle_config.vending_tax )
z -= z * (battle_config.vending_tax/10000.);
pc->getzeny(vsd, (int)z, LOG_TYPE_VENDING, sd);
for( i = 0; i < count; i++ ) {
short amount = *(uint16*)(data + 4*i + 0);
short idx = *(uint16*)(data + 4*i + 2);
idx -= 2;
// vending item
pc->additem(sd, &vsd->status.cart[idx], amount, LOG_TYPE_VENDING);
vsd->vending[vend_list[i]].amount -= amount;
pc->cart_delitem(vsd, idx, amount, 0, LOG_TYPE_VENDING);
clif->vendingreport(vsd, idx, amount);
//print buyer's name
if( battle_config.buyer_name ) {
char temp[256];
sprintf(temp, msg_txt(265), sd->status.name);
clif->disp_onlyself(vsd,temp,strlen(temp));
}
}
// compact the vending list
for( i = 0, cursor = 0; i < vsd->vend_num; i++ ) {
if( vsd->vending[i].amount == 0 )
continue;
if( cursor != i ) { // speedup
vsd->vending[cursor].index = vsd->vending[i].index;
vsd->vending[cursor].amount = vsd->vending[i].amount;
vsd->vending[cursor].value = vsd->vending[i].value;
}
cursor++;
}
vsd->vend_num = cursor;
//Always save BOTH: buyer and customer
if( map->save_settings&2 ) {
chrif->save(sd,0);
chrif->save(vsd,0);
}
//check for @AUTOTRADE users [durf]
if( vsd->state.autotrade ) {
//see if there is anything left in the shop
ARR_FIND( 0, vsd->vend_num, i, vsd->vending[i].amount > 0 );
if( i == vsd->vend_num ) {
//Close Vending (this was automatically done by the client, we have to do it manually for autovenders) [Skotlex]
vending->close(vsd);
map->quit(vsd); //They have no reason to stay around anymore, do they?
}
}
}
/*==========================================
* Open shop
* data := {<index>.w <amount>.w <value>.l}[count]
*------------------------------------------*/
void vending_openvending(struct map_session_data* sd, const char* message, const uint8* data, int count) {
int i, j;
int vending_skill_lvl;
nullpo_retv(sd);
if ( pc_isdead(sd) || !sd->state.prevend || pc_istrading(sd))
return; // can't open vendings lying dead || didn't use via the skill (wpe/hack) || can't have 2 shops at once
vending_skill_lvl = pc->checkskill(sd, MC_VENDING);
// skill level and cart check
if( !vending_skill_lvl || !pc_iscarton(sd) ) {
clif->skill_fail(sd, MC_VENDING, USESKILL_FAIL_LEVEL, 0);
return;
}
// check number of items in shop
if( count < 1 || count > MAX_VENDING || count > 2 + vending_skill_lvl ) {
// invalid item count
clif->skill_fail(sd, MC_VENDING, USESKILL_FAIL_LEVEL, 0);
return;
}
// filter out invalid items
i = 0;
for( j = 0; j < count; j++ ) {
short index = *(uint16*)(data + 8*j + 0);
short amount = *(uint16*)(data + 8*j + 2);
unsigned int value = *(uint32*)(data + 8*j + 4);
index -= 2; // offset adjustment (client says that the first cart position is 2)
if( index < 0 || index >= MAX_CART // invalid position
|| pc->cartitem_amount(sd, index, amount) < 0 // invalid item or insufficient quantity
//NOTE: official server does not do any of the following checks!
|| !sd->status.cart[index].identify // unidentified item
|| sd->status.cart[index].attribute == 1 // broken item
|| sd->status.cart[index].expire_time // It should not be in the cart but just in case
|| (sd->status.cart[index].bound && !pc->can_give_bounded_items(sd)) // can't trade bound items w/o permission
|| !itemdb_cantrade(&sd->status.cart[index], pc->get_group_level(sd), pc->get_group_level(sd)) ) // untradeable item
continue;
sd->vending[i].index = index;
sd->vending[i].amount = amount;
sd->vending[i].value = cap_value(value, 0, (unsigned int)battle_config.vending_max_value);
i++; // item successfully added
}
if( i != j )
clif->message (sd->fd, msg_txt(266)); //"Some of your items cannot be vended and were removed from the shop."
if( i == 0 ) { // no valid item found
clif->skill_fail(sd, MC_VENDING, USESKILL_FAIL_LEVEL, 0); // custom reply packet
return;
}
sd->state.prevend = sd->state.workinprogress = 0;
sd->state.vending = true;
sd->vender_id = getid();
sd->vend_num = i;
safestrncpy(sd->message, message, MESSAGE_SIZE);
clif->openvending(sd,sd->bl.id,sd->vending);
clif->showvendingboard(&sd->bl,message,0);
idb_put(vending->db, sd->status.char_id, sd);
}
/// Checks if an item is being sold in given player's vending.
bool vending_search(struct map_session_data* sd, unsigned short nameid) {
int i;
if( !sd->state.vending ) { // not vending
return false;
}
ARR_FIND( 0, sd->vend_num, i, sd->status.cart[sd->vending[i].index].nameid == (short)nameid );
if( i == sd->vend_num ) { // not found
return false;
}
return true;
}
/// Searches for all items in a vending, that match given ids, price and possible cards.
/// @return Whether or not the search should be continued.
bool vending_searchall(struct map_session_data* sd, const struct s_search_store_search* s) {
int i, c, slot;
unsigned int idx, cidx;
struct item* it;
if( !sd->state.vending ) // not vending
return true;
for( idx = 0; idx < s->item_count; idx++ ) {
ARR_FIND( 0, sd->vend_num, i, sd->status.cart[sd->vending[i].index].nameid == (short)s->itemlist[idx] );
if( i == sd->vend_num ) {// not found
continue;
}
it = &sd->status.cart[sd->vending[i].index];
if( s->min_price && s->min_price > sd->vending[i].value ) {// too low price
continue;
}
if( s->max_price && s->max_price < sd->vending[i].value ) {// too high price
continue;
}
if( s->card_count ) {// check cards
if( itemdb_isspecial(it->card[0]) ) {// something, that is not a carded
continue;
}
slot = itemdb_slot(it->nameid);
for( c = 0; c < slot && it->card[c]; c ++ ) {
ARR_FIND( 0, s->card_count, cidx, s->cardlist[cidx] == it->card[c] );
if( cidx != s->card_count )
{// found
break;
}
}
if( c == slot || !it->card[c] ) {// no card match
continue;
}
}
if( !searchstore->result(s->search_sd, sd->vender_id, sd->status.account_id, sd->message, it->nameid, sd->vending[i].amount, sd->vending[i].value, it->card, it->refine) )
{// result set full
return false;
}
}
return true;
}
void final(void) {
db_destroy(vending->db);
}
void init(bool minimal) {
vending->db = idb_alloc(DB_OPT_BASE);
vending->next_id = 0;
}
void vending_defaults(void) {
vending = &vending_s;
vending->init = init;
vending->final = final;
vending->close = vending_closevending;
vending->open = vending_openvending;
vending->list = vending_vendinglistreq;
vending->purchase = vending_purchasereq;
vending->search = vending_search;
vending->searchall = vending_searchall;
}
|