Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.         /* Container.CanFit(item)
  3.          *      Checks to see if we can fit the item in the buffer.
  4.          */
  5.         this.CanFit = function (item) {
  6.                 if(!this.FindSpot(item))
  7.                         this.SortItems();
  8.                 return (!!this.FindSpot(item));
  9.         };
  10.        
  11.         /* Container.SortItems();
  12.          *  Loop Container backwards and try to replace all items
  13.          */
  14.         this.SortItems = function() {
  15.                 print("Sorting "+this.name+" ... ");
  16.                
  17.                 var cube = me.findItem(549);
  18.                
  19.                 if( this.location==7 && cube )
  20.                         sendPacket(1, 0x27, 4, me.findItem(-1, -1, me.findItems(-1, -1, 3) == false ? 1 : 3).gid, 4, cube.gid);
  21.                
  22.                 if( this.location==7 && !cube )        
  23.                         Town.openStash();
  24.                        
  25.                 Storage.Reload();
  26.                
  27.                 var x, y, item, nPos;
  28.                
  29.                 for ( y = this.width -1 ; y>=0 ; y-- ) {
  30.                 for ( x = this.height-1 ; x>=0 ; x-- ) {
  31.                
  32.                                 delay(1);
  33.                                
  34.                                 if ( this.buffer[x][y] == 0 ) {
  35.                                         continue; // nothing on this spot
  36.                                 }
  37.                                
  38.                                 item = this.itemList[this.buffer[x][y]-1]
  39.                                
  40.                                 if ( item.classid==549 ) {
  41.                                         continue; // dont touch the cube
  42.                                 }
  43.                                
  44.                                 if ( this.location==3 && this.IsLocked(item, Config.Inventory) ) {
  45.                                         continue; // locked spot / item
  46.                                 }
  47.                                
  48.                                 var ix = item.y, iy = item.x; // WTF x and y is vice versa switched
  49.                                
  50.                                 if( ix < x || iy < y ) {
  51.                                         continue; // not top left part of item
  52.                                 }
  53.                                
  54.                                 //print(item.name+" in "+this.name+" Spot at X:"+x+" Y:"+y+" ... ");
  55.  
  56.                                 //Check if the item could fit an earlier position
  57.                                 nPos = this.FindSpot(item);    
  58.                                 if ( !nPos || (nPos.x >= ix && nPos.y >= iy) ) {
  59.                                         continue; // fits here or more backwards
  60.                                 }
  61.  
  62.                                 if (!this.MoveTo( getUnit(-1,-1,-1,item.gid) )) {
  63.                                         continue; // we couldnt move the item
  64.                                 }
  65.  
  66.                                 // We moved an item so reload & restart
  67.                                 Storage.Reload();
  68.                                 y = this.width  - 0;
  69.                                 break; // Loop again from begin
  70.                                
  71.                 }}
  72.                 print("Sorting "+this.name+" done ");
  73.                 me.cancel();
  74.         };
  75.  
  76.         /* Container.FindSpot(item)
  77.          *      Finds a spot available in the buffer to place the item.
  78.          */
  79.         this.FindSpot = function (item) {
  80.                 var x, y, nx, ny;
  81.  
  82.                 //Make sure it's a valid item
  83.                 if (!item) {
  84.                         return false;
  85.                 }
  86.  
  87.                 Storage.Reload();
  88.  
  89.                 //Loop buffer looking for spot to place item.
  90.                 for (y = 0; y < this.width - (item.sizex - 1); y += 1) {
  91. Loop:
  92.                         for (x = 0; x < this.height - (item.sizey - 1); x += 1) {
  93.                                 //Check if there is something in this spot.
  94.                                 if (this.buffer[x][y] > 0) {
  95.                                         if( item.gid != this.itemList[this.buffer[x][y]-1].gid ) // ignore same gid
  96.                                                 continue;
  97.                                 }
  98.  
  99.                                 //Loop the item size to make sure we can fit it.
  100.                                 for (nx = 0; nx < item.sizey; nx += 1) {
  101.                                         for (ny = 0; ny < item.sizex; ny += 1) {
  102.                                                 if (this.buffer[x + nx][y + ny]) {
  103.                                                         if( item.gid != this.itemList[this.buffer[x+nx][y+ny]-1].gid ) // ignore same gid
  104.                                                                 continue Loop;
  105.                                                 }
  106.                                         }
  107.                                 }
  108.  
  109.                                 return ({x: x, y: y});
  110.                         }
  111.                 }
  112.  
  113.                 return false;
  114.         };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement