Advertisement
kolton

Untitled

Dec 16th, 2011
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. var Container = function (name, width, height, location) {
  2. this.name = name;
  3. this.width = width;
  4. this.height = height;
  5. this.location = location;
  6. this.buffer = new Array();
  7. this.itemList = new Array();
  8. this.openPositions = this.height * this.width;
  9.  
  10. // Initalize the buffer array for use, set all as empty.
  11. for (var h = 0; h < this.height; h++) {
  12. this.buffer.push(new Array());
  13. for (var w = 0; w < this.width; w++)
  14. this.buffer[h][w] = 0;
  15. }
  16.  
  17. /* Container.Mark(item)
  18. * Marks the item in the buffer, and adds it to the item list.
  19. */
  20. this.Mark = function(item) {
  21. //Make sure it is in this container.
  22. if (item.location != this.location || item.mode != 0)
  23. return false;
  24. //Interface.message(DetailedDebug, "Marking " + item.name + "ÿc0 in " + this.name + " buffer.");
  25. //Mark item in buffer.
  26. for(var x = item.x; x < (item.x + item.sizex); x++)
  27. for (var y = item.y; y < (item.y + item.sizey); y++) {
  28. this.buffer[y][x] = this.itemList.length + 1;
  29. this.openPositions--;
  30. }
  31. //Add item to list.
  32. this.itemList.push(copyUnit(item));
  33. //if (this.location == 0)
  34. // Interface.writeLog("Test.log", JSON.stringify(this.buffer));
  35.  
  36. return true;
  37. }
  38.  
  39. this.Reset = function () {
  40. for (var h = 0; h < this.height; h++)
  41. for (var w = 0; w < this.width; w++)
  42. this.buffer[h][w] = 0;
  43. this.itemList = new Array();
  44. return true;
  45. }
  46.  
  47. /* Container.CanFit(item)
  48. * Checks to see if we can fit the item in the buffer.
  49. */
  50. this.CanFit = function(item) { return (!!this.FindSpot(item)); };
  51.  
  52. /* Container.FindSpot(item)
  53. * Finds a spot available in the buffer to place the item.
  54. */
  55. this.FindSpot = function(item) {
  56. //Make sure it's a valid item
  57. if (!item)
  58. return false;
  59.  
  60. Storage.Reload();
  61.  
  62. //Loop buffer looking for spot to place item.
  63. for (var x = 0; x < this.height - (item.sizey - 1); x++) {
  64. itemLoop:for (var y = 0; y < this.width - (item.sizex - 1); y++) {
  65. //Check if there is something in this spot.
  66. if (this.buffer[x][y] > 0)
  67. continue;
  68. //Loop the item size to make sure we can fit it.
  69. for (var nx = 0; nx < item.sizey; nx++)
  70. for (var ny = 0; ny < item.sizex; ny++)
  71. if (this.buffer[x + nx][y + ny])
  72. continue itemLoop;
  73.  
  74. return({x:x, y:y});
  75. }
  76. }
  77. return false;
  78. }
  79.  
  80. /* Container.MoveTo(item)
  81. * Takes any item and moves it into given buffer.
  82. */
  83. this.MoveTo = function(item) {
  84. try {
  85. //Can we even fit it in here?
  86. var nPos = this.FindSpot(item);
  87. if (!nPos)
  88. return false;
  89.  
  90. //Can't deal with items on ground!
  91. if (item.mode == Mode.Item.OnGround)
  92. return false;
  93.  
  94. //Item already on the cursor.
  95. if (me.itemoncursor && item.mode != Mode.Item.OnCursor)
  96. return false;
  97.  
  98. //Pick to cursor if not already.
  99. item.toCursor();
  100.  
  101. //Loop three times to try and place it.
  102. for (var n = 0; n < 3; n++) {
  103. clickItem(0, nPos.y, nPos.x, (this.location == 2 ? 5 : this.location));
  104.  
  105. var nDelay = getTickCount();
  106. while((getTickCount() - nDelay) < 2000) {
  107. if (!me.itemoncursor) {
  108. Interface.message(DetailedDebug, "Successfully placed " + item.name + " at X: " + nPos.x + " Y: " + nPos.y);
  109. return true;
  110. }
  111. delay(1);
  112. }
  113. }
  114. return true;
  115.  
  116. } catch(e) {
  117. mBot.throwError(e);
  118. return false;
  119. }
  120. }
  121.  
  122. /* Container.Dump()
  123. * Prints all known information about container.
  124. */
  125. this.Dump = function () {
  126. print(this.name + " has the width of " + this.width + " and the height of " + this.height);
  127. print(this.name + " has " + this.itemList.length + " items inside, and has " + this.openPositions + " spots left.");
  128. for (var x = 0; x < this.height; x++) {
  129. var string = "";
  130. for (var y = 0; y < this.width; y++)
  131. string += (this.buffer[x][y] > 0) ? "ÿc1x" : "ÿc0o";
  132. print(string);
  133. }
  134. }
  135.  
  136. /* Container.compare(reference)
  137. * Compare given container versus the current one, return all new items in current buffer.
  138. */
  139. this.Compare = function(baseRef) {
  140. try {
  141. var itemList = new Array();
  142. var reference = baseRef.slice(0, baseRef.length);
  143. //Insure valid reference.
  144. if (!isObject(reference) || reference.length != this.buffer.length || reference[0].length != this.buffer[0].length)
  145. throw new Error("Unable to compare different containers.");
  146.  
  147. for (var h = 0; h < this.height; h++) {
  148. Loop:for (var w = 0; w < this.width; w++) {
  149. var item = this.itemList[this.buffer[h][w] - 1];
  150.  
  151. if (!item)
  152. continue;
  153.  
  154. for (var n = 0; n < itemList.length; n++) {
  155. if (itemList[n].gid == item.gid)
  156. continue Loop;
  157. }
  158. //Check if the buffers changed and the current buffer has an item there.
  159. if (this.buffer[h][w] > 0 && reference[h][w] == 0)
  160. itemList.push(copyUnit(item));
  161. }
  162. }
  163. return itemList;
  164. } catch(e) {
  165. mBot.throwError(e);
  166. return false;
  167. }
  168. }
  169.  
  170. this.toSource = function() {
  171. return this.buffer.toSource();
  172. }
  173. }
  174.  
  175. var Storage = new function () {
  176. this.Inventory = new Container("Inventory", 10, 4, 0);
  177. this.Stash = new Container("Stash", 6, 8, 4);
  178. this.Belt = new Container("Belt", 16, 1, 2);
  179. this.Cube = new Container("Horadric Cube", 3, 4, 3);
  180. this.InvRef = [];
  181.  
  182. this.Init = function() {
  183. this.Reload();
  184. }
  185.  
  186. this.Reload = function () {
  187. this.Inventory.Reset();
  188. this.Stash.Reset();
  189. this.Belt.Reset();
  190. this.Cube.Reset();
  191. var item = me.getItem();
  192. do {
  193. switch(item.loc) {
  194. case itemLocation.Inventory:
  195. this.Inventory.Mark(item);
  196. break;
  197. case itemLocation.Belt:
  198. this.Belt.Mark(item);
  199. break;
  200. case itemLocation.Cube:
  201. this.Cube.Mark(item);
  202. break;
  203. case itemLocation.Stash:
  204. this.Stash.Mark(item);
  205. break;
  206. }
  207. } while(item.getNext());
  208. debugLog(this.Inventory.toSource());
  209. return true;
  210. }
  211.  
  212. this.FindNewItems = function() {
  213.  
  214. }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement