Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Container.CanFit(item)
- * Checks to see if we can fit the item in the buffer.
- */
- this.CanFit = function (item) {
- if(!this.FindSpot(item))
- this.SortItems();
- return (!!this.FindSpot(item));
- };
- /* Container.SortItems();
- * Loop Container backwards and try to replace all items
- */
- this.SortItems = function() {
- print("Sorting "+this.name+" ... ");
- var cube = me.findItem(549);
- if( this.location==7 && cube )
- sendPacket(1, 0x27, 4, me.findItem(-1, -1, me.findItems(-1, -1, 3) == false ? 1 : 3).gid, 4, cube.gid);
- if( this.location==7 && !cube )
- Town.openStash();
- Storage.Reload();
- var x, y, item, nPos;
- for ( y = this.width -1 ; y>=0 ; y-- ) {
- for ( x = this.height-1 ; x>=0 ; x-- ) {
- delay(1);
- if ( this.buffer[x][y] == 0 ) {
- continue; // nothing on this spot
- }
- item = this.itemList[this.buffer[x][y]-1]
- if ( item.classid==549 ) {
- continue; // dont touch the cube
- }
- if ( this.location==3 && this.IsLocked(item, Config.Inventory) ) {
- continue; // locked spot / item
- }
- var ix = item.y, iy = item.x; // WTF x and y is vice versa switched
- if( ix < x || iy < y ) {
- continue; // not top left part of item
- }
- //print(item.name+" in "+this.name+" Spot at X:"+x+" Y:"+y+" ... ");
- //Check if the item could fit an earlier position
- nPos = this.FindSpot(item);
- if ( !nPos || (nPos.x >= ix && nPos.y >= iy) ) {
- continue; // fits here or more backwards
- }
- if (!this.MoveTo( getUnit(-1,-1,-1,item.gid) )) {
- continue; // we couldnt move the item
- }
- // We moved an item so reload & restart
- Storage.Reload();
- y = this.width - 0;
- break; // Loop again from begin
- }}
- print("Sorting "+this.name+" done ");
- me.cancel();
- };
- /* Container.FindSpot(item)
- * Finds a spot available in the buffer to place the item.
- */
- this.FindSpot = function (item) {
- var x, y, nx, ny;
- //Make sure it's a valid item
- if (!item) {
- return false;
- }
- Storage.Reload();
- //Loop buffer looking for spot to place item.
- for (y = 0; y < this.width - (item.sizex - 1); y += 1) {
- Loop:
- for (x = 0; x < this.height - (item.sizey - 1); x += 1) {
- //Check if there is something in this spot.
- if (this.buffer[x][y] > 0) {
- if( item.gid != this.itemList[this.buffer[x][y]-1].gid ) // ignore same gid
- continue;
- }
- //Loop the item size to make sure we can fit it.
- for (nx = 0; nx < item.sizey; nx += 1) {
- for (ny = 0; ny < item.sizex; ny += 1) {
- if (this.buffer[x + nx][y + ny]) {
- if( item.gid != this.itemList[this.buffer[x+nx][y+ny]-1].gid ) // ignore same gid
- continue Loop;
- }
- }
- }
- return ({x: x, y: y});
- }
- }
- return false;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement