vending.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/nullpo.h"
  4. #include "../common/strlib.h"
  5. #include "../common/utils.h"
  6. #include "clif.h"
  7. #include "itemdb.h"
  8. #include "atcommand.h"
  9. #include "map.h"
  10. #include "path.h"
  11. #include "chrif.h"
  12. #include "vending.h"
  13. #include "pc.h"
  14. #include "npc.h"
  15. #include "skill.h"
  16. #include "battle.h"
  17. #include "log.h"
  18. #include <stdio.h>
  19. #include <string.h>
  20. static int vending_nextid = 0;
  21. /// Returns an unique vending shop id.
  22. static int vending_getuid(void)
  23. {
  24. return vending_nextid++;
  25. }
  26. /*==========================================
  27. * Close shop
  28. *------------------------------------------*/
  29. void vending_closevending(struct map_session_data* sd)
  30. {
  31. nullpo_retv(sd);
  32. if( sd->state.vending )
  33. {
  34. sd->state.vending = false;
  35. clif_closevendingboard(&sd->bl, 0);
  36. }
  37. }
  38. /*==========================================
  39. * Request a shop's item list
  40. *------------------------------------------*/
  41. void vending_vendinglistreq(struct map_session_data* sd, int id)
  42. {
  43. struct map_session_data* vsd;
  44. nullpo_retv(sd);
  45. if( (vsd = map_id2sd(id)) == NULL )
  46. return;
  47. if( !vsd->state.vending )
  48. return; // not vending
  49. if (!pc_can_give_items(sd) || !pc_can_give_items(vsd)) //check if both GMs are allowed to trade
  50. { // GM is not allowed to trade
  51. clif_displaymessage(sd->fd, msg_txt(sd,246));
  52. return;
  53. }
  54. sd->vended_id = vsd->vender_id; // register vending uid
  55. clif_vendinglist(sd, id, vsd->vending);
  56. }
  57. /*==========================================
  58. * Purchase item(s) from a shop
  59. *------------------------------------------*/
  60. void vending_purchasereq(struct map_session_data* sd, int aid, int uid, const uint8* data, int count)
  61. {
  62. int i, j, cursor, w, new_ = 0, blank, vend_list[MAX_VENDING];
  63. double z;
  64. struct s_vending vending[MAX_VENDING]; // against duplicate packets
  65. struct map_session_data* vsd = map_id2sd(aid);
  66. nullpo_retv(sd);
  67. if( vsd == NULL || !vsd->state.vending || vsd->bl.id == sd->bl.id )
  68. return; // invalid shop
  69. if( vsd->vender_id != uid )
  70. {// shop has changed
  71. clif_buyvending(sd, 0, 0, 6); // store information was incorrect
  72. return;
  73. }
  74. if( !searchstore_queryremote(sd, aid) && ( sd->bl.m != vsd->bl.m || !check_distance_bl(&sd->bl, &vsd->bl, AREA_SIZE) ) )
  75. return; // shop too far away
  76. searchstore_clearremote(sd);
  77. if( count < 1 || count > MAX_VENDING || count > vsd->vend_num )
  78. return; // invalid amount of purchased items
  79. blank = pc_inventoryblank(sd); //number of free cells in the buyer's inventory
  80. // duplicate item in vending to check hacker with multiple packets
  81. memcpy(&vending, &vsd->vending, sizeof(vsd->vending)); // copy vending list
  82. // some checks
  83. z = 0.; // zeny counter
  84. w = 0; // weight counter
  85. for( i = 0; i < count; i++ )
  86. {
  87. short amount = *(uint16*)(data + 4*i + 0);
  88. short idx = *(uint16*)(data + 4*i + 2);
  89. idx -= 2;
  90. if( amount <= 0 )
  91. return;
  92. // check of item index in the cart
  93. if( idx < 0 || idx >= MAX_CART )
  94. return;
  95. ARR_FIND( 0, vsd->vend_num, j, vsd->vending[j].index == idx );
  96. if( j == vsd->vend_num )
  97. return; //picked non-existing item
  98. else
  99. vend_list[i] = j;
  100. z += ((double)vsd->vending[j].value * (double)amount);
  101. if( z > (double)sd->status.zeny || z < 0. || z > (double)MAX_ZENY )
  102. {
  103. clif_buyvending(sd, idx, amount, 1); // you don't have enough zeny
  104. return;
  105. }
  106. if( z + (double)vsd->status.zeny > (double)MAX_ZENY && !battle_config.vending_over_max )
  107. {
  108. clif_buyvending(sd, idx, vsd->vending[j].amount, 4); // too much zeny = overflow
  109. return;
  110. }
  111. w += itemdb_weight(vsd->status.cart[idx].nameid) * amount;
  112. if( w + sd->weight > sd->max_weight )
  113. {
  114. clif_buyvending(sd, idx, amount, 2); // you can not buy, because overweight
  115. return;
  116. }
  117. //Check to see if cart/vend info is in sync.
  118. if( vending[j].amount > vsd->status.cart[idx].amount )
  119. vending[j].amount = vsd->status.cart[idx].amount;
  120. // if they try to add packets (example: get twice or more 2 apples if marchand has only 3 apples).
  121. // here, we check cumulative amounts
  122. if( vending[j].amount < amount )
  123. {
  124. // send more quantity is not a hack (an other player can have buy items just before)
  125. clif_buyvending(sd, idx, vsd->vending[j].amount, 4); // not enough quantity
  126. return;
  127. }
  128. vending[j].amount -= amount;
  129. switch( pc_checkadditem(sd, vsd->status.cart[idx].nameid, amount) ) {
  130. case CHKADDITEM_EXIST:
  131. break; //We'd add this item to the existing one (in buyers inventory)
  132. case CHKADDITEM_NEW:
  133. new_++;
  134. if (new_ > blank)
  135. return; //Buyer has no space in his inventory
  136. break;
  137. case CHKADDITEM_OVERAMOUNT:
  138. return; //too many items
  139. }
  140. }
  141. pc_payzeny(sd, (int)z, LOG_TYPE_VENDING, vsd);
  142. if( battle_config.vending_tax )
  143. z -= z * (battle_config.vending_tax/10000.);
  144. pc_getzeny(vsd, (int)z, LOG_TYPE_VENDING, sd);
  145. for( i = 0; i < count; i++ )
  146. {
  147. short amount = *(uint16*)(data + 4*i + 0);
  148. short idx = *(uint16*)(data + 4*i + 2);
  149. idx -= 2;
  150. // vending item
  151. pc_additem(sd, &vsd->status.cart[idx], amount, LOG_TYPE_VENDING);
  152. vsd->vending[vend_list[i]].amount -= amount;
  153. pc_cart_delitem(vsd, idx, amount, 0, LOG_TYPE_VENDING);
  154. clif_vendingreport(vsd, idx, amount);
  155. //print buyer's name
  156. if( battle_config.buyer_name )
  157. {
  158. char temp[256];
  159. sprintf(temp, msg_txt(sd,265), sd->status.name);
  160. clif_disp_onlyself(vsd,temp,strlen(temp));
  161. }
  162. }
  163. // compact the vending list
  164. for( i = 0, cursor = 0; i < vsd->vend_num; i++ )
  165. {
  166. if( vsd->vending[i].amount == 0 )
  167. continue;
  168. if( cursor != i ) // speedup
  169. {
  170. vsd->vending[cursor].index = vsd->vending[i].index;
  171. vsd->vending[cursor].amount = vsd->vending[i].amount;
  172. vsd->vending[cursor].value = vsd->vending[i].value;
  173. }
  174. cursor++;
  175. }
  176. vsd->vend_num = cursor;
  177. //Always save BOTH: buyer and customer
  178. if( save_settings&2 )
  179. {
  180. chrif_save(sd,0);
  181. chrif_save(vsd,0);
  182. }
  183. //check for @AUTOTRADE users [durf]
  184. if( vsd->state.autotrade )
  185. {
  186. //see if there is anything left in the shop
  187. ARR_FIND( 0, vsd->vend_num, i, vsd->vending[i].amount > 0 );
  188. if( i == vsd->vend_num )
  189. {
  190. //Close Vending (this was automatically done by the client, we have to do it manually for autovenders) [Skotlex]
  191. vending_closevending(vsd);
  192. map_quit(vsd); //They have no reason to stay around anymore, do they?
  193. }
  194. }
  195. }
  196. /*==========================================
  197. * Open shop
  198. * data := {<index>.w <amount>.w <value>.l}[count]
  199. *------------------------------------------*/
  200. void vending_openvending(struct map_session_data* sd, const char* message, const uint8* data, int count) {
  201. int i, j;
  202. int vending_skill_lvl;
  203. nullpo_retv(sd);
  204. if ( pc_isdead(sd) || !sd->state.prevend || pc_istrading(sd))
  205. return; // can't open vendings lying dead || didn't use via the skill (wpe/hack) || can't have 2 shops at once
  206. vending_skill_lvl = pc_checkskill(sd, MC_VENDING);
  207. // skill level and cart check
  208. if( !vending_skill_lvl || !pc_iscarton(sd) )
  209. {
  210. clif_skill_fail(sd, MC_VENDING, USESKILL_FAIL_LEVEL, 0);
  211. return;
  212. }
  213. // check number of items in shop
  214. if( count < 1 || count > MAX_VENDING || count > 2 + vending_skill_lvl )
  215. { // invalid item count
  216. clif_skill_fail(sd, MC_VENDING, USESKILL_FAIL_LEVEL, 0);
  217. return;
  218. }
  219. // filter out invalid items
  220. i = 0;
  221. for( j = 0; j < count; j++ )
  222. {
  223. short index = *(uint16*)(data + 8*j + 0);
  224. short amount = *(uint16*)(data + 8*j + 2);
  225. unsigned int value = *(uint32*)(data + 8*j + 4);
  226. index -= 2; // offset adjustment (client says that the first cart position is 2)
  227. if( index < 0 || index >= MAX_CART // invalid position
  228. || pc_cartitem_amount(sd, index, amount) < 0 // invalid item or insufficient quantity
  229. //NOTE: official server does not do any of the following checks!
  230. || !sd->status.cart[index].identify // unidentified item
  231. || sd->status.cart[index].attribute == 1 // broken item
  232. || sd->status.cart[index].expire_time // It should not be in the cart but just in case
  233. || !itemdb_cantrade(&sd->status.cart[index], pc_get_group_level(sd), pc_get_group_level(sd)) ) // untradeable item
  234. continue;
  235. sd->vending[i].index = index;
  236. sd->vending[i].amount = amount;
  237. sd->vending[i].value = cap_value(value, 0, (unsigned int)battle_config.vending_max_value);
  238. i++; // item successfully added
  239. }
  240. if( i != j )
  241. clif_displaymessage (sd->fd, msg_txt(sd,266)); //"Some of your items cannot be vended and were removed from the shop."
  242. if( i == 0 )
  243. { // no valid item found
  244. clif_skill_fail(sd, MC_VENDING, USESKILL_FAIL_LEVEL, 0); // custom reply packet
  245. return;
  246. }
  247. sd->state.prevend = 0;
  248. sd->state.vending = true;
  249. sd->vender_id = vending_getuid();
  250. sd->vend_num = i;
  251. safestrncpy(sd->message, message, MESSAGE_SIZE);
  252. clif_openvending(sd,sd->bl.id,sd->vending);
  253. clif_showvendingboard(&sd->bl,message,0);
  254. }
  255. /// Checks if an item is being sold in given player's vending.
  256. bool vending_search(struct map_session_data* sd, unsigned short nameid)
  257. {
  258. int i;
  259. if( !sd->state.vending )
  260. {// not vending
  261. return false;
  262. }
  263. ARR_FIND( 0, sd->vend_num, i, sd->status.cart[sd->vending[i].index].nameid == (short)nameid );
  264. if( i == sd->vend_num )
  265. {// not found
  266. return false;
  267. }
  268. return true;
  269. }
  270. /// Searches for all items in a vending, that match given ids, price and possible cards.
  271. /// @return Whether or not the search should be continued.
  272. bool vending_searchall(struct map_session_data* sd, const struct s_search_store_search* s)
  273. {
  274. int i, c, slot;
  275. unsigned int idx, cidx;
  276. struct item* it;
  277. if( !sd->state.vending )
  278. {// not vending
  279. return true;
  280. }
  281. for( idx = 0; idx < s->item_count; idx++ )
  282. {
  283. ARR_FIND( 0, sd->vend_num, i, sd->status.cart[sd->vending[i].index].nameid == (short)s->itemlist[idx] );
  284. if( i == sd->vend_num )
  285. {// not found
  286. continue;
  287. }
  288. it = &sd->status.cart[sd->vending[i].index];
  289. if( s->min_price && s->min_price > sd->vending[i].value )
  290. {// too low price
  291. continue;
  292. }
  293. if( s->max_price && s->max_price < sd->vending[i].value )
  294. {// too high price
  295. continue;
  296. }
  297. if( s->card_count )
  298. {// check cards
  299. if( itemdb_isspecial(it->card[0]) )
  300. {// something, that is not a carded
  301. continue;
  302. }
  303. slot = itemdb_slot(it->nameid);
  304. for( c = 0; c < slot && it->card[c]; c ++ )
  305. {
  306. ARR_FIND( 0, s->card_count, cidx, s->cardlist[cidx] == it->card[c] );
  307. if( cidx != s->card_count )
  308. {// found
  309. break;
  310. }
  311. }
  312. if( c == slot || !it->card[c] )
  313. {// no card match
  314. continue;
  315. }
  316. }
  317. 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) )
  318. {// result set full
  319. return false;
  320. }
  321. }
  322. return true;
  323. }