Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*==========================================
- * WNRO autobuy - START
- * uatobuy selllist
- *------------------------------------------*/
- int vending_ab_selllist(struct map_session_data* sd, int n, unsigned short* item_list)
- {
- struct block_list* bl;
- struct map_session_data* vsd;
- double z;
- int i, nameid, idx, nameid_c, idx_c, eq, j=0, ab_count, k;
- short qty, qty_c;
- //char temp[250];
- //memset(temp, '\0', sizeof(temp));
- nullpo_retr(1, sd);
- nullpo_retr(1, item_list);
- if( (vsd = map_id2sd(sd->autobuyer_id)) == NULL )
- return 1;
- bl = &vsd->bl;
- if (bl->m!=sd->bl.m ||
- bl->x<sd->bl.x-AREA_SIZE-1 || bl->x>sd->bl.x+AREA_SIZE+1 ||
- bl->y<sd->bl.y-AREA_SIZE-1 || bl->y>sd->bl.y+AREA_SIZE+1)
- {
- clif_displaymessage(sd->fd, "Buyer out of sight!");
- return 1;
- }
- ab_count = vsd->autobuy_num;
- // Additional check on the excess: is there enough money to buy, if it's
- for(i=0,z=0,qty_c=0;i<n;i++) {
- idx = item_list[i*2]-2;
- qty = (short)item_list[i*2+1];
- nameid=sd->status.inventory[idx].nameid;
- for(j=0,k =-1;j<ab_count;j++)
- if (nameid == vsd->autobuy[j].id)
- {
- k = j;
- break;
- }
- if (k<0) continue;
- z += ((double)qty*(double)vsd->autobuy[k].price);
- qty_c += itemdb_weight(nameid)*qty;
- }
- if (z > (double)vsd->status.zeny)
- {
- clif_displaymessage(sd->fd, "Trying to sell goods more than it needs, or the buyer is unable to pay for your goods!");
- return 1;
- }
- if( qty_c + vsd->weight > vsd->max_weight )
- {
- clif_displaymessage(sd->fd, "The buyer is not able to pick up the requested weight!");
- return -1;
- }
- for(i=0,z=0;i<n;i++) {
- idx = item_list[i*2]-2;
- qty = (short)item_list[i*2+1];
- if (idx <0 || idx >=MAX_INVENTORY || qty < 0)
- break;
- nameid=sd->status.inventory[idx].nameid;
- if (nameid == 0 || !sd->inventory_data[idx] || sd->status.inventory[idx].amount < qty)
- break;
- // Counting the number of goods sold - for a product that takes each line item, for example, weapons
- for(j=0,eq=0;j<n;j++) {
- idx_c = item_list[j*2]-2;
- qty_c = (short)item_list[j*2+1];
- nameid_c=sd->status.inventory[idx_c].nameid;
- if (nameid == nameid_c) eq += qty_c;
- }
- // The index definition to the buyer at the current position of goods sold
- for(j=0,k =-1;j<ab_count;j++) {
- if (nameid == vsd->autobuy[j].id)
- {
- if (eq > vsd->autobuy[j].amount) // Attempt to sell more than is actually required
- {
- clif_displaymessage(sd->fd, "You cannot sell more than is required to purchase!");
- return 1;
- }
- k = j;
- break;
- }
- }
- // Extras. check in case some weird kind of miracle product will be here to see prices - we will not buy
- if (k<0)
- {
- clif_displaymessage(sd->fd, "You are trying to sell a product that is not required!");
- return 1;
- }
- 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
- if(log_config.enable_logs&0x20){ //Logs items, Sold to NPC (S)hop [Lupus]
- log_pick_pc(sd, "S", nameid, -qty, &sd->status.inventory[idx]);
- log_pick_pc(vsd, "S", nameid, qty, &sd->status.inventory[idx]);
- }
- pc_additem(vsd, &sd->status.inventory[idx], qty); // Add this position to the buyer
- pc_delitem(sd,idx,qty,0,0); // Remove an item from the seller
- vsd->autobuy[j].amount -= qty; // Bought amount - subtracted from the order amount purchased
- if (SQL_ERROR == Sql_Query(mmysql_handle,
- "UPDATE `autobuy` SET `amount`=%d WHERE `char_id`=%d AND `index`=%d",vsd->autobuy[j].amount, vsd->status.char_id, j))
- Sql_ShowDebug(mmysql_handle);
- }
- if (z > MAX_ZENY) z = MAX_ZENY;
- if(log_config.zeny){ //Logs (S)hopping Zeny [Lupus]
- log_zeny(sd, "S", sd, (int)z);
- log_zeny(vsd, "S", vsd, (int)z*-1);
- }
- pc_getzeny(sd,(int)z);
- pc_payzeny(vsd,(int)z);
- //sprintf(temp,"You sell the goods for %dz.");
- //clif_displaymessage(sd->fd, temp);
- if( save_settings&2 )
- {
- chrif_save(sd,0);
- chrif_save(vsd,0);
- }
- if (i<n) {
- //Error/Exploit... of some sort. If we return 1, the client will not mark
- //any item as deleted even though a few were sold. In such a case, we
- //have no recourse but to kick them out so their inventory will refresh
- //correctly on relog. [Skotlex]
- if (i) set_eof(sd->fd);
- return 1;
- }
- // Counting the number that's left to buy
- for(i=0,k=0;i<ab_count;i++) k += vsd->autobuy[i].amount;
- if (k<=0)
- { // Remove the sign of the buyer - there is nothing to buy more
- chat_leavechat(vsd,1);
- vsd->autobuy_num = 0;
- if (!vsd->vender_id)
- map_quit(vsd);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement