vending.c 7.6 KB

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