Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. /*==========================================
  2. * WNRO autobuy - START
  3. * uatobuy selllist
  4. *------------------------------------------*/
  5. int vending_ab_selllist(struct map_session_data* sd, int n, unsigned short* item_list)
  6. {
  7. struct block_list* bl;
  8. struct map_session_data* vsd;
  9. double z;
  10. int i, nameid, idx, nameid_c, idx_c, eq, j=0, ab_count, k;
  11. short qty, qty_c;
  12. //char temp[250];
  13. //memset(temp, '\0', sizeof(temp));
  14.  
  15. nullpo_retr(1, sd);
  16. nullpo_retr(1, item_list);
  17.  
  18. if( (vsd = map_id2sd(sd->autobuyer_id)) == NULL )
  19. return 1;
  20.  
  21. bl = &vsd->bl;
  22. if (bl->m!=sd->bl.m ||
  23. bl->x<sd->bl.x-AREA_SIZE-1 || bl->x>sd->bl.x+AREA_SIZE+1 ||
  24. bl->y<sd->bl.y-AREA_SIZE-1 || bl->y>sd->bl.y+AREA_SIZE+1)
  25. {
  26. clif_displaymessage(sd->fd, "Buyer out of sight!");
  27. return 1;
  28. }
  29.  
  30. ab_count = vsd->autobuy_num;
  31. // Additional check on the excess: is there enough money to buy, if it's
  32. for(i=0,z=0,qty_c=0;i<n;i++) {
  33. idx = item_list[i*2]-2;
  34. qty = (short)item_list[i*2+1];
  35. nameid=sd->status.inventory[idx].nameid;
  36. for(j=0,k =-1;j<ab_count;j++)
  37. if (nameid == vsd->autobuy[j].id)
  38. {
  39. k = j;
  40. break;
  41. }
  42. if (k<0) continue;
  43. z += ((double)qty*(double)vsd->autobuy[k].price);
  44. qty_c += itemdb_weight(nameid)*qty;
  45. }
  46.  
  47. if (z > (double)vsd->status.zeny)
  48. {
  49. clif_displaymessage(sd->fd, "Trying to sell goods more than it needs, or the buyer is unable to pay for your goods!");
  50. return 1;
  51. }
  52. if( qty_c + vsd->weight > vsd->max_weight )
  53. {
  54. clif_displaymessage(sd->fd, "The buyer is not able to pick up the requested weight!");
  55. return -1;
  56. }
  57.  
  58. for(i=0,z=0;i<n;i++) {
  59. idx = item_list[i*2]-2;
  60. qty = (short)item_list[i*2+1];
  61.  
  62. if (idx <0 || idx >=MAX_INVENTORY || qty < 0)
  63. break;
  64.  
  65. nameid=sd->status.inventory[idx].nameid;
  66.  
  67. if (nameid == 0 || !sd->inventory_data[idx] || sd->status.inventory[idx].amount < qty)
  68. break;
  69.  
  70. // Counting the number of goods sold - for a product that takes each line item, for example, weapons
  71. for(j=0,eq=0;j<n;j++) {
  72. idx_c = item_list[j*2]-2;
  73. qty_c = (short)item_list[j*2+1];
  74. nameid_c=sd->status.inventory[idx_c].nameid;
  75. if (nameid == nameid_c) eq += qty_c;
  76. }
  77. // The index definition to the buyer at the current position of goods sold
  78. for(j=0,k =-1;j<ab_count;j++) {
  79. if (nameid == vsd->autobuy[j].id)
  80. {
  81. if (eq > vsd->autobuy[j].amount) // Attempt to sell more than is actually required
  82. {
  83. clif_displaymessage(sd->fd, "You cannot sell more than is required to purchase!");
  84. return 1;
  85. }
  86. k = j;
  87. break;
  88. }
  89. }
  90. // Extras. check in case some weird kind of miracle product will be here to see prices - we will not buy
  91. if (k<0)
  92. {
  93. clif_displaymessage(sd->fd, "You are trying to sell a product that is not required!");
  94. return 1;
  95. }
  96. z+=((double)qty*(double)vsd->autobuy[j].price); // Counting the money as you need to buy up all the goods of this type on the current position
  97.  
  98. if(log_config.enable_logs&0x20){ //Logs items, Sold to NPC (S)hop [Lupus]
  99. log_pick_pc(sd, "S", nameid, -qty, &sd->status.inventory[idx]);
  100. log_pick_pc(vsd, "S", nameid, qty, &sd->status.inventory[idx]);
  101. }
  102.  
  103. pc_additem(vsd, &sd->status.inventory[idx], qty); // Add this position to the buyer
  104. pc_delitem(sd,idx,qty,0,0); // Remove an item from the seller
  105.  
  106. vsd->autobuy[j].amount -= qty; // Bought amount - subtracted from the order amount purchased
  107.  
  108. if (SQL_ERROR == Sql_Query(mmysql_handle,
  109. "UPDATE `autobuy` SET `amount`=%d WHERE `char_id`=%d AND `index`=%d",vsd->autobuy[j].amount, vsd->status.char_id, j))
  110. Sql_ShowDebug(mmysql_handle);
  111. }
  112.  
  113. if (z > MAX_ZENY) z = MAX_ZENY;
  114. if(log_config.zeny){ //Logs (S)hopping Zeny [Lupus]
  115. log_zeny(sd, "S", sd, (int)z);
  116. log_zeny(vsd, "S", vsd, (int)z*-1);
  117. }
  118. pc_getzeny(sd,(int)z);
  119. pc_payzeny(vsd,(int)z);
  120. //sprintf(temp,"You sell the goods for %dz.");
  121. //clif_displaymessage(sd->fd, temp);
  122. if( save_settings&2 )
  123. {
  124. chrif_save(sd,0);
  125. chrif_save(vsd,0);
  126. }
  127.  
  128. if (i<n) {
  129. //Error/Exploit... of some sort. If we return 1, the client will not mark
  130. //any item as deleted even though a few were sold. In such a case, we
  131. //have no recourse but to kick them out so their inventory will refresh
  132. //correctly on relog. [Skotlex]
  133. if (i) set_eof(sd->fd);
  134. return 1;
  135. }
  136. // Counting the number that's left to buy
  137. for(i=0,k=0;i<ab_count;i++) k += vsd->autobuy[i].amount;
  138.  
  139. if (k<=0)
  140. { // Remove the sign of the buyer - there is nothing to buy more
  141. chat_leavechat(vsd,1);
  142. vsd->autobuy_num = 0;
  143. if (!vsd->vender_id)
  144. map_quit(vsd);
  145. }
  146. return 0;
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement