Advertisement
kolton

Untitled

Dec 11th, 2014
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.95 KB | None | 0 0
  1. /**
  2. * @filename Crafting.js
  3. * @author kolton
  4. * @desc Part of CraftingSystem
  5. */
  6.  
  7. var info,
  8. gameRequest = false;
  9.  
  10. function Crafting() {
  11. var i, npcName, num;
  12.  
  13. info = CraftingSystem.getInfo();
  14.  
  15. if (!info || !info.worker) {
  16. throw new Error("Bad Crafting System config.");
  17. }
  18.  
  19. me.maxgametime = 0;
  20. Town.goToTown(1);
  21. Town.doChores();
  22. Town.move("stash");
  23. updateInfo();
  24. pickItems();
  25.  
  26. addEventListener('copydata',
  27. function (mode, msg) {
  28. var i, obj, rval;
  29.  
  30. if (mode === 0) {
  31. try {
  32. obj = JSON.parse(msg);
  33. } catch (e) {
  34. return false;
  35. }
  36.  
  37. if (obj) {
  38. switch (obj.name) {
  39. case "GetGame":
  40. if (info.Collectors.indexOf(obj.profile) > -1) {
  41. print("GetGame: " + obj.profile);
  42. sendCopyData(null, obj.profile, 4, me.gamename + "/" + me.gamepassword);
  43.  
  44. gameRequest = true;
  45. }
  46.  
  47. break;
  48. case "GetSetInfo":
  49. if (info.Collectors.indexOf(obj.profile) > -1) {
  50. print("GetSetInfo: " + obj.profile);
  51.  
  52. rval = [];
  53.  
  54. for (i = 0; i < info.Sets.length; i += 1) {
  55. rval.push(info.Sets[i].Enabled ? 1 : 0);
  56. }
  57.  
  58. print(rval);
  59.  
  60. sendCopyData(null, obj.profile, 4, JSON.stringify({name: "SetInfo", value: rval}));
  61. }
  62.  
  63. break;
  64. }
  65. }
  66. }
  67.  
  68. return true;
  69. });
  70.  
  71. for (i = 0; i < Cubing.recipes.length; i += 1) {
  72. Cubing.recipes[i].Level = 0;
  73. }
  74.  
  75. while (true) {
  76. for (i = 0; i < info.Sets.length; i += 1) {
  77. switch (info.Sets[i].Type) {
  78. case "crafting":
  79. num = 0;
  80. npcName = getNPCName(info.Sets[i].BaseItems);
  81.  
  82. if (npcName) {
  83. num = countItems(info.Sets[i].BaseItems, 4);
  84.  
  85. if (num < info.Sets[i].SetAmount) {
  86. shopStuff(npcName, info.Sets[i].BaseItems, info.Sets[i].SetAmount);
  87. }
  88. }
  89.  
  90. break;
  91. case "cubing": // Nothing to do currently
  92. break;
  93. case "runewords": // Nothing to do currently
  94. break;
  95. }
  96. }
  97.  
  98. if (me.act !== 1) {
  99. Town.goToTown(1);
  100. Town.move("stash");
  101. }
  102.  
  103. if (gameRequest) {
  104. for (i = 0; i < 10; i += 1) {
  105. if (othersInGame()) {
  106. while (othersInGame()) {
  107. delay(200);
  108. }
  109.  
  110. break;
  111. } else {
  112. break;
  113. }
  114.  
  115. delay(1000);
  116. }
  117.  
  118. gameRequest = false;
  119. }
  120.  
  121. pickItems();
  122. Cubing.update();
  123. Runewords.buildLists();
  124. Cubing.doCubing();
  125. Runewords.makeRunewords();
  126. delay(2000);
  127. }
  128. }
  129.  
  130. function getNPCName(idList) {
  131. var i;
  132.  
  133. for (i = 0; i < idList.length; i += 1) {
  134. switch (idList[i]) {
  135. case 345: // Light Belt
  136. case 391: // Sharkskin Belt
  137. return "elzix";
  138. case 346: // Belt
  139. case 392: // Mesh Belt
  140. case 342: // Light Plated Boots
  141. case 388: // Battle Boots
  142. return "fara";
  143. }
  144. }
  145.  
  146. return false;
  147. }
  148.  
  149. function othersInGame() {
  150. var p = getParty();
  151.  
  152. if (p) {
  153. do {
  154. if (p.name !== me.name) {
  155. return true;
  156. }
  157. } while (p.getNext());
  158. }
  159.  
  160. return false;
  161. }
  162.  
  163. function countItems(idList, quality) {
  164. var item,
  165. count = 0;
  166.  
  167. item = me.getItem(-1, 0);
  168.  
  169. if (item) {
  170. do {
  171. if (idList.indexOf(item.classid) > -1 && item.quality === quality) {
  172. count += 1;
  173. }
  174. } while (item.getNext());
  175. }
  176.  
  177. return count;
  178. }
  179.  
  180. function updateInfo() {
  181. var i, j, items;
  182.  
  183. if (info) {
  184. items = me.findItems(-1, 0);
  185.  
  186. for (i = 0; i < info.Sets.length; i += 1) {
  187. MainSwitch:
  188. switch (info.Sets[i].Type) {
  189. // Always enable crafting because the base can be shopped
  190. // Recipes with bases that can't be shopped don't need to be used with CraftingSystem
  191. case "crafting":
  192. info.Sets[i].Enabled = true;
  193.  
  194. break;
  195. // Enable only if we have a viable item to cube
  196. // Currently the base needs to be added manually to the crafter
  197. case "cubing":
  198. if (!items) {
  199. items = [];
  200. }
  201.  
  202. // Enable the recipe if we have an item that matches both bases list and Cubing list
  203. // This is not a perfect check, it might not handle every case
  204. for (j = 0; j < items.length; j += 1) {
  205. if (info.Sets[i].BaseItems.indexOf(items[j].classid) > -1 && // Item is on the bases list
  206. AutoMule.cubingIngredient(items[j])) { // Item is a valid Cubing ingredient
  207. print("Base found: " + items[j].classid);
  208.  
  209. info.Sets[i].Enabled = true;
  210.  
  211. break MainSwitch;
  212. }
  213. }
  214.  
  215. info.Sets[i].Enabled = false;
  216.  
  217. break;
  218. // Enable only if we have a viable runeword base
  219. // Currently the base needs to be added manually to the crafter
  220. case "runewords":
  221. if (!items) {
  222. items = [];
  223. }
  224.  
  225. // Enable the recipe if we have an item that matches both bases list and Cubing list
  226. // This is not a perfect check, it might not handle every case
  227. for (j = 0; j < items.length; j += 1) {
  228. if (info.Sets[i].BaseItems.indexOf(items[j].classid) > -1 && // Item is on the bases list
  229. runewordIngredient(items[j])) { // Item is a valid Runeword ingredient
  230. print("Base found: " + items[j].classid);
  231.  
  232. info.Sets[i].Enabled = true;
  233.  
  234. break MainSwitch;
  235. }
  236. }
  237.  
  238. info.Sets[i].Enabled = false;
  239.  
  240. break;
  241. }
  242. }
  243.  
  244. return true;
  245. }
  246.  
  247. return false;
  248. }
  249.  
  250. function runewordIngredient(item) {
  251. if (Runewords.validGids.indexOf(item.gid) > -1) {
  252. return true;
  253. }
  254.  
  255. var i, base, baseGids;
  256.  
  257. baseGids = [];
  258.  
  259. for (i = 0; i < Config.Runewords.length; i += 1) {
  260. base = Runewords.getBase(Config.Runewords[i][0], Config.Runewords[i][1]) || Runewords.getBase(Config.Runewords[i][0], Config.Runewords[i][1], true);
  261.  
  262. if (base) {
  263. baseGids.push(base.gid);
  264. }
  265. }
  266.  
  267. if (baseGids.indexOf(item.gid) > -1) {
  268. return true;
  269. }
  270.  
  271. return false;
  272. }
  273.  
  274. function pickItems() {
  275. var items = [],
  276. item = getUnit(4, -1, 3);
  277.  
  278. if (item) {
  279. updateInfo();
  280.  
  281. do {
  282. if (checkItem(item) || item.classid === 523 || Pickit.checkItem(item).result > 0) {
  283. items.push(copyUnit(item));
  284. }
  285. } while (item.getNext());
  286. }
  287.  
  288. while (items.length) {
  289. if (Pickit.canPick(items[0]) && Storage.Inventory.CanFit(items[0])) {
  290. Pickit.pickItem(items[0]);
  291. }
  292.  
  293. items.shift();
  294. delay(1);
  295. }
  296.  
  297. Town.stash();
  298. }
  299.  
  300. function checkItem(item) {
  301. var i;
  302.  
  303. for (i = 0; i < info.Sets.length; i += 1) {
  304. if (info.Sets[i].Enabled) {
  305. switch (info.Sets[i].Type) {
  306. case "crafting":
  307. // Magic item
  308. if (item.quality === 4 && info.Sets[i].BaseItems.indexOf(item.classid) > -1) {
  309. return true; // Valid crafting base
  310. }
  311.  
  312. if (info.Sets[i].Ingredients.indexOf(item.classid) > -1) {
  313. return true; // Valid crafting ingredient
  314. }
  315.  
  316. break;
  317. case "cubing":
  318. // There is no base check, item has to be put manually on the character
  319.  
  320. if (info.Sets[i].Ingredients.indexOf(item.classid) > -1) {
  321. return true;
  322. }
  323.  
  324. break;
  325. case "runewords":
  326. // There is no base check, item has to be put manually on the character
  327.  
  328. if (info.Sets[i].Ingredients.indexOf(item.classid) > -1) {
  329. return true;
  330. }
  331.  
  332. break;
  333. }
  334. }
  335. }
  336.  
  337. return false;
  338. }
  339.  
  340. function shopStuff(npcId, classids, amount) {
  341. print("shopStuff: " + npcId + " " + amount);
  342.  
  343. var wpArea, town, path, menuId, npc, tickCount,
  344. leadTimeout = 30,
  345. leadRetry = 3;
  346.  
  347. this.mover = function (npc, path) {
  348. var i, j;
  349.  
  350. path = this.processPath(npc, path);
  351.  
  352. for (i = 0; i < path.length; i += 2) {
  353. Pather.moveTo(path[i] - 3, path[i + 1] - 3);
  354. moveNPC(npc, path[i], path[i + 1]);
  355.  
  356. for (j = 0; j < leadTimeout; j += 1) {
  357. while (npc.mode === 2) {
  358. delay(100);
  359. }
  360.  
  361. if (getDistance(npc.x, npc.y, path[i], path[i + 1]) < 4) {
  362. break;
  363. }
  364.  
  365. if (j > 0 && j % leadRetry === 0) {
  366. moveNPC(npc, path[i], path[i + 1]);
  367. }
  368.  
  369. delay(1000);
  370. }
  371.  
  372. if (j === leadTimeout) {
  373. return false;
  374. }
  375. }
  376.  
  377. delay(1000);
  378.  
  379. return true;
  380. };
  381.  
  382. this.processPath = function (npc, path) {
  383. var i,
  384. cutIndex = 0,
  385. dist = 100;
  386.  
  387. for (i = 0; i < path.length; i += 2) {
  388. if (getDistance(npc, path[i], path[i + 1]) < dist) {
  389. cutIndex = i;
  390. dist = getDistance(npc, path[i], path[i + 1]);
  391. }
  392. }
  393.  
  394. return path.slice(cutIndex);
  395. };
  396.  
  397. this.shopItems = function (classids, amount) {
  398. var i, items,
  399. num = 0,
  400. npc = getInteractedNPC();
  401.  
  402. if (npc) {
  403. items = npc.getItems();
  404.  
  405. if (items) {
  406. for (i = 0; i < items.length; i += 1) {
  407. if (Storage.Inventory.CanFit(items[i]) &&
  408. Pickit.canPick(items[i]) &&
  409. me.getStat(14) + me.getStat(15) >= items[i].getItemCost(0) &&
  410. classids.indexOf(items[i].classid) > -1) {
  411.  
  412. //print("Bought " + items[i].name);
  413. items[i].buy();
  414.  
  415. num = countItems(classids, 4);
  416.  
  417. if (num >= amount) {
  418. return true;
  419. }
  420. }
  421. }
  422. }
  423. }
  424.  
  425. return gameRequest;
  426. };
  427.  
  428. Town.doChores();
  429.  
  430. switch (npcId.toLowerCase()) {
  431. case "fara":
  432. wpArea = 48;
  433. town = 40;
  434. path = [5112, 5094, 5092, 5096, 5078, 5098, 5070, 5085];
  435. menuId = "Repair";
  436.  
  437. if (!Town.goToTown(2) || !Town.move(NPC.Fara)) {
  438. throw new Error("Failed to get to NPC");
  439. }
  440.  
  441. npc = getUnit(1, NPC.Fara);
  442.  
  443. break;
  444. case "elzix":
  445. wpArea = 48;
  446. town = 40;
  447. path = [5038, 5099, 5059, 5102, 5068, 5090, 5067, 5086];
  448. menuId = "Shop";
  449.  
  450. Town.goToTown(2);
  451.  
  452. if (!getUnit(1, NPC.Elzix)) {
  453. Town.move(NPC.Elzix);
  454. }
  455.  
  456. npc = getUnit(1, NPC.Elzix);
  457.  
  458. break;
  459. case "drognan":
  460. wpArea = 48;
  461. town = 40;
  462. path = [5093, 5049, 5088, 5060, 5093, 5079, 5078, 5087, 5070, 5085];
  463. menuId = "Shop";
  464.  
  465. if (!Town.goToTown(2) || !Town.move(NPC.Drognan)) {
  466. throw new Error("Failed to get to NPC");
  467. }
  468.  
  469. npc = getUnit(1, NPC.Drognan);
  470.  
  471. break;
  472. case "ormus":
  473. wpArea = 101;
  474. town = 75;
  475. path = [5147, 5089, 5156, 5075, 5157, 5063, 5160, 5050];
  476. menuId = "Shop";
  477.  
  478. if (!Town.goToTown(3) || !Town.move(NPC.Ormus)) {
  479. throw new Error("Failed to get to NPC");
  480. }
  481.  
  482. npc = getUnit(1, NPC.Ormus);
  483.  
  484. break;
  485. case "anya":
  486. wpArea = 129;
  487. town = 109;
  488. path = [5122, 5119, 5129, 5105, 5123, 5087, 5115, 5068];
  489. menuId = "Shop";
  490.  
  491. if (!Town.goToTown(5) || !Town.move(NPC.Anya)) {
  492. throw new Error("Failed to get to NPC");
  493. }
  494.  
  495. npc = getUnit(1, NPC.Anya);
  496.  
  497. break;
  498. case "malah":
  499. wpArea = 113;
  500. town = 109;
  501. path = [5077, 5032, 5089, 5025, 5100, 5021, 5106, 5051, 5116, 5071];
  502. menuId = "Shop";
  503.  
  504. if (!Town.goToTown(5) || !Town.move(NPC.Malah)) {
  505. throw new Error("Failed to get to NPC");
  506. }
  507.  
  508. npc = getUnit(1, NPC.Malah);
  509.  
  510. break;
  511. default:
  512. throw new Error("Invalid shopbot NPC.");
  513. }
  514.  
  515. if (!npc) {
  516. throw new Error("Failed to find NPC.");
  517. }
  518.  
  519. if (!this.mover(npc, path)) {
  520. throw new Error("Failed to move NPC");
  521. }
  522.  
  523. Town.move("waypoint");
  524.  
  525. tickCount = getTickCount();
  526.  
  527. while (true) {
  528. if (me.area === town) {
  529. if (npc.startTrade(menuId)) {
  530. if (this.shopItems(classids, amount)) {
  531. return true;
  532. }
  533. }
  534.  
  535. me.cancel();
  536. }
  537.  
  538. if (me.area === town) {
  539. Pather.useWaypoint(wpArea);
  540. }
  541.  
  542. if (me.area === wpArea) {
  543. Pather.useWaypoint(town);
  544. }
  545.  
  546. delay(5);
  547. }
  548.  
  549. return true;
  550. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement