Lighta

chekweight2

Feb 17th, 2012
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. BUILDIN_FUNC(checkweight2)
  2. {
  3.         int nameid=-1, amount=-1, nbitems=0, weight=0;
  4.         int i=0, amount2=0, slots=0;
  5.         struct item_data* id = NULL;
  6.     TBL_PC *sd = script_rid2sd(st);
  7.  
  8.     nullpo_retr(1,sd);
  9.    
  10.         nbitems = script_lastdata(st);
  11.         if(nbitems%2){
  12.             ShowError("buildin_checkweight2: Invalid nbitem='%d', must be a multiple of 2.\n", nbitems);
  13.             script_pushint(st,0);
  14.             return 1;
  15.         }
  16.         slots = pc_inventoryblank(sd);
  17.         for(i=2; i<nbitems+1; i=i+2){
  18.             nameid = script_getnum(st,i);
  19.             if( (id = itemdb_exists(nameid)) == NULL )
  20.             {
  21.                     ShowError("buildin_checkweight2: Invalid item '%d'.\n", nameid);
  22.                     script_pushint(st,0);
  23.                     return 1;
  24.             }
  25.             amount = script_getnum(st,i+1);
  26.             if( amount < 1 )
  27.             {
  28.         ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  29.         script_pushint(st,0);
  30.         return 1;
  31.             }
  32.             weight += itemdb_weight(nameid)*amount;
  33.             if( weight + sd->weight > sd->max_weight )
  34.             {// too heavy
  35.                     script_pushint(st,0);
  36.                     return 0;
  37.             }
  38.             switch( pc_checkadditem(sd, nameid, amount) )
  39.             {
  40.                     case ADDITEM_EXIST: // item is already in inventory, but there is still space for the requested amount
  41.                             break;
  42.                     case ADDITEM_NEW:  
  43.                             if( itemdb_isstackable(nameid) )
  44.                             {// stackable
  45.                     amount2++;
  46.                                     if( slots < amount2 ){
  47.                                             script_pushint(st,0);
  48.                         return 0;
  49.                     }
  50.                             }
  51.                             else
  52.                             {// non-stackable
  53.                                     amount2 += amount; //amount of new non stackables  
  54.                                     if( slots < amount2 ){ //do we still have enough place in inventory for them
  55.                                             script_pushint(st,0);
  56.                         return 0;
  57.                     }
  58.                             }
  59.                             break;
  60.                     case ADDITEM_OVERAMOUNT:
  61.                             script_pushint(st,0);
  62.                             return 0;
  63.             }      
  64.         }      
  65.  
  66.     script_pushint(st,1);
  67.     return 0;  
  68. }
Add Comment
Please, Sign In to add comment