vending.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "skill.h"
  15. #include "battle.h"
  16. #include "log.h"
  17. #include <stdio.h>
  18. #include <string.h>
  19. /*==========================================
  20. * Close shop
  21. *------------------------------------------*/
  22. void vending_closevending(struct map_session_data* sd)
  23. {
  24. nullpo_retv(sd);
  25. sd->vender_id = 0;
  26. clif_closevendingboard(&sd->bl,0);
  27. }
  28. /*==========================================
  29. * Request a shop's item list
  30. *------------------------------------------*/
  31. void vending_vendinglistreq(struct map_session_data* sd, int id)
  32. {
  33. struct map_session_data* vsd;
  34. nullpo_retv(sd);
  35. if( (vsd = map_id2sd(id)) == NULL )
  36. return;
  37. if( vsd->vender_id == 0 )
  38. return; // not vending
  39. if ( !pc_can_give_items(pc_isGM(sd)) || !pc_can_give_items(pc_isGM(vsd)) ) //check if both GMs are allowed to trade
  40. { // GM is not allowed to trade
  41. clif_displaymessage(sd->fd, msg_txt(246));
  42. return;
  43. }
  44. clif_vendinglist(sd, id, vsd->vending);
  45. }
  46. /*==========================================
  47. * Purchase item(s) from a shop
  48. *------------------------------------------*/
  49. void vending_purchasereq(struct map_session_data* sd, int id, const uint8* data, int count)
  50. {
  51. int i, j, cursor, w, new_ = 0, blank, vend_list[MAX_VENDING];
  52. double z;
  53. struct s_vending vending[MAX_VENDING]; // against duplicate packets
  54. struct map_session_data* vsd = map_id2sd(id);
  55. nullpo_retv(sd);
  56. if( vsd == NULL || vsd->vender_id == 0 || vsd->vender_id == sd->bl.id )
  57. return; // invalid shop
  58. if( sd->bl.m != vsd->bl.m || !check_distance_bl(&sd->bl, &vsd->bl, AREA_SIZE) )
  59. return; // shop too far away
  60. if( count < 1 || count > MAX_VENDING || count > vsd->vend_num )
  61. return; // invalid amount of purchased items
  62. blank = pc_inventoryblank(sd); //number of free cells in the buyer's inventory
  63. // duplicate item in vending to check hacker with multiple packets
  64. memcpy(&vending, &vsd->vending, sizeof(vsd->vending)); // copy vending list
  65. // some checks
  66. z = 0.; // zeny counter
  67. w = 0; // weight counter
  68. for( i = 0; i < count; i++ )
  69. {
  70. short amount = *(uint16*)(data + 4*i + 0);
  71. short idx = *(uint16*)(data + 4*i + 2);
  72. idx -= 2;
  73. if( amount <= 0 )
  74. return;
  75. // check of item index in the cart
  76. if( idx < 0 || idx >= MAX_CART )
  77. return;
  78. ARR_FIND( 0, vsd->vend_num, j, vsd->vending[j].index == idx );
  79. if( j == vsd->vend_num )
  80. return; //picked non-existing item
  81. else
  82. vend_list[i] = j;
  83. z += ((double)vsd->vending[j].value * (double)amount);
  84. if( z > (double)sd->status.zeny || z < 0. || z > (double)MAX_ZENY )
  85. {
  86. clif_buyvending(sd, idx, amount, 1); // you don't have enough zeny
  87. return;
  88. }
  89. if( z + (double)vsd->status.zeny > (double)MAX_ZENY && !battle_config.vending_over_max )
  90. {
  91. clif_buyvending(sd, idx, vsd->vending[j].amount, 4); // too much zeny = overflow
  92. return;
  93. }
  94. w += itemdb_weight(vsd->status.cart[idx].nameid) * amount;
  95. if( w + sd->weight > sd->max_weight )
  96. {
  97. clif_buyvending(sd, idx, amount, 2); // you can not buy, because overweight
  98. return;
  99. }
  100. //Check to see if cart/vend info is in sync.
  101. if( vending[j].amount > vsd->status.cart[idx].amount )
  102. vending[j].amount = vsd->status.cart[idx].amount;
  103. // if they try to add packets (example: get twice or more 2 apples if marchand has only 3 apples).
  104. // here, we check cumulative amounts
  105. if( vending[j].amount < amount )
  106. {
  107. // send more quantity is not a hack (an other player can have buy items just before)
  108. clif_buyvending(sd, idx, vsd->vending[j].amount, 4); // not enough quantity
  109. return;
  110. }
  111. vending[j].amount -= amount;
  112. switch( pc_checkadditem(sd, vsd->status.cart[idx].nameid, amount) ) {
  113. case ADDITEM_EXIST:
  114. break; //We'd add this item to the existing one (in buyers inventory)
  115. case ADDITEM_NEW:
  116. new_++;
  117. if (new_ > blank)
  118. return; //Buyer has no space in his inventory
  119. break;
  120. case ADDITEM_OVERAMOUNT:
  121. return; //too many items
  122. }
  123. }
  124. //Logs (V)ending Zeny [Lupus]
  125. if( log_config.zeny > 0 )
  126. log_zeny(vsd, "V", sd, (int)z);
  127. pc_payzeny(sd, (int)z);
  128. if( battle_config.vending_tax )
  129. z -= z * (battle_config.vending_tax/10000.);
  130. pc_getzeny(vsd, (int)z);
  131. for( i = 0; i < count; i++ )
  132. {
  133. short amount = *(uint16*)(data + 4*i + 0);
  134. short idx = *(uint16*)(data + 4*i + 2);
  135. idx -= 2;
  136. //Logs sold (V)ending items [Lupus]
  137. if(log_config.enable_logs&0x4) {
  138. log_pick_pc(vsd, "V", vsd->status.cart[idx].nameid, -amount, &vsd->status.cart[idx]);
  139. log_pick_pc( sd, "V", vsd->status.cart[idx].nameid, amount, &vsd->status.cart[idx]);
  140. }
  141. // vending item
  142. pc_additem(sd, &vsd->status.cart[idx], amount);
  143. vsd->vending[vend_list[i]].amount -= amount;
  144. pc_cart_delitem(vsd, idx, amount, 0);
  145. clif_vendingreport(vsd, idx, amount);
  146. //print buyer's name
  147. if( battle_config.buyer_name )
  148. {
  149. char temp[256];
  150. sprintf(temp, msg_txt(265), sd->status.name);
  151. clif_disp_onlyself(vsd,temp,strlen(temp));
  152. }
  153. }
  154. // compact the vending list
  155. for( i = 0, cursor = 0; i < vsd->vend_num; i++ )
  156. {
  157. if( vsd->vending[i].amount == 0 )
  158. continue;
  159. if( cursor != i ) // speedup
  160. {
  161. vsd->vending[cursor].index = vsd->vending[i].index;
  162. vsd->vending[cursor].amount = vsd->vending[i].amount;
  163. vsd->vending[cursor].value = vsd->vending[i].value;
  164. }
  165. cursor++;
  166. }
  167. vsd->vend_num = cursor;
  168. //Always save BOTH: buyer and customer
  169. if( save_settings&2 )
  170. {
  171. chrif_save(sd,0);
  172. chrif_save(vsd,0);
  173. }
  174. //check for @AUTOTRADE users [durf]
  175. if( vsd->state.autotrade )
  176. {
  177. //see if there is anything left in the shop
  178. ARR_FIND( 0, vsd->vend_num, i, vsd->vending[i].amount > 0 );
  179. if( i == vsd->vend_num )
  180. {
  181. //Close Vending (this was automatically done by the client, we have to do it manually for autovenders) [Skotlex]
  182. vending_closevending(vsd);
  183. map_quit(vsd); //They have no reason to stay around anymore, do they?
  184. }
  185. }
  186. }
  187. /*==========================================
  188. * Open shop
  189. * data := {<index>.w <amount>.w <value>.l}[count]
  190. *------------------------------------------*/
  191. void vending_openvending(struct map_session_data* sd, const char* message, bool flag, const uint8* data, int count)
  192. {
  193. int i, j;
  194. int vending_skill_lvl;
  195. nullpo_retv(sd);
  196. if( !flag ) // cancelled
  197. return; // nothing to do
  198. if (pc_istrading(sd))
  199. return; // can't have 2 shops at once
  200. vending_skill_lvl = pc_checkskill(sd, MC_VENDING);
  201. // skill level and cart check
  202. if( !vending_skill_lvl || !pc_iscarton(sd) )
  203. {
  204. clif_skill_fail(sd, MC_VENDING, 0, 0);
  205. return;
  206. }
  207. // check number of items in shop
  208. if( count < 1 || count > MAX_VENDING || count > 2 + vending_skill_lvl )
  209. { // invalid item count
  210. clif_skill_fail(sd, MC_VENDING, 0, 0);
  211. return;
  212. }
  213. // filter out invalid items
  214. i = 0;
  215. for( j = 0; j < count; j++ )
  216. {
  217. short index = *(uint16*)(data + 8*j + 0);
  218. short amount = *(uint16*)(data + 8*j + 2);
  219. unsigned int value = *(uint32*)(data + 8*j + 4);
  220. index -= 2; // offset adjustment (client says that the first cart position is 2)
  221. if( index < 0 || index >= MAX_CART // invalid position
  222. || pc_cartitem_amount(sd, index, amount) < 0 // invalid item or insufficient quantity
  223. //NOTE: official server does not do any of the following checks!
  224. || !sd->status.cart[index].identify // unidentified item
  225. || sd->status.cart[index].attribute == 1 // broken item
  226. || sd->status.cart[index].expire_time // It should not be in the cart but just in case
  227. || !itemdb_cantrade(&sd->status.cart[index], pc_isGM(sd), pc_isGM(sd)) ) // untradeable item
  228. continue;
  229. sd->vending[i].index = index;
  230. sd->vending[i].amount = amount;
  231. sd->vending[i].value = cap_value(value, 1, (unsigned int)battle_config.vending_max_value);
  232. i++; // item successfully added
  233. }
  234. if( i != j )
  235. clif_displaymessage (sd->fd, msg_txt(266)); //"Some of your items cannot be vended and were removed from the shop."
  236. if( i == 0 )
  237. { // no valid item found
  238. clif_skill_fail(sd, MC_VENDING, 0, 0); // custom reply packet
  239. return;
  240. }
  241. sd->vender_id = sd->bl.id;
  242. sd->vend_num = i;
  243. safestrncpy(sd->message, message, MESSAGE_SIZE);
  244. pc_stop_walking(sd,1);
  245. clif_openvending(sd,sd->vender_id,sd->vending);
  246. clif_showvendingboard(&sd->bl,message,0);
  247. }