Advertisement
Guest User

Untitled

a guest
Jan 9th, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 47.48 KB | None | 0 0
  1. // +-------------------------------------------+
  2. // | User Configuration (feel free to edit it) |
  3. // +-------------------------------------------+
  4.  
  5. var CONFIG_USE_AUTO_LOGIN = false;
  6. var CONFIG_AUTO_LOGIN_USERNAME = "";
  7. var CONFIG_AUTO_LOGIN_PASSWORD = "";
  8.  
  9. var CONFIG_AUTO_RECONNECT = true;
  10.  
  11. var CONFIG_AUTO_SHIP_REPAIR = true;
  12. var CONFIG_AUTO_SHIP_REPAIR_LOCATION = "base"; // Possible locations: "stage", "gate", "base"
  13. var CONFIG_MAX_SHIP_REPAIRS = 20; // The script will stop once they have been reached.
  14.  
  15. var CONFIG_USE_PET = false;
  16. var CONFIG_MAX_PET_REPAIRS = 20;
  17. var CONFIG_PET_GEAR_TO_USE = "autolooter"; // Allowed values: "autolooter", "collector", "passivemode", "guardmode"
  18. var CONFIG_PET_CHECK_TIMEOUT_IN_MS = 5 * 60 * 1000; // minutes * seconds * milliseconds
  19.  
  20. var CONFIG_USE_GLOBAL_NAV = false;
  21. var CONFIG_MAP = "4-1";
  22.  
  23. var CONFIG_COLLECT_LOOT = true; // Works only in 2D mode
  24. var CONFIG_COLLECTOR_TIMEOUT_IN_MS = 200; // Increase to reduce CPU/GPU usage, lower to miss less loot.
  25.  
  26. // +---------------------------------------------------------------------+
  27. // | Templates and Data (DON'T TOUCH ANYTHING BELOW THIS LINE AS A USER) |
  28. // +---------------------------------------------------------------------+
  29.  
  30. var SCRIPT_VERSION = "1.1.1";
  31.  
  32. var TEMPLATE_DIR = "templates/";
  33. var CLIENT_TPL_DIR = TEMPLATE_DIR + "client/";
  34. var MINIMAP_DIR = TEMPLATE_DIR + "minimap/";
  35. var PET_TPL_DIR = TEMPLATE_DIR + "pet/";
  36. var MM_LEVEL_DIR = MINIMAP_DIR + "levels/";
  37. var MM_MAPNAMES_DIR = MINIMAP_DIR + "mapnames/";
  38.  
  39. var JUMP_BUTTON_TPL = new Image(TEMPLATE_DIR + "jump_button.png");
  40. var NO_VELOCITY_REF = new Image(new Size(62, 21), new Color("black"));
  41.  
  42. var LOGOUT_BUTTON_TPL = new Image(TEMPLATE_DIR + "logout_button.png");
  43. var START_BUTTON_TPL = new Image(CLIENT_TPL_DIR + "start_button.png");
  44.  
  45. var REPAIR_CONFIRM_TPL = new Image(CLIENT_TPL_DIR + "confirm_repair_button_edge.png");
  46. var REPAIR_ON_BASE_TPL = new Image(CLIENT_TPL_DIR + "repair_on_base_option.png");
  47. var REPAIR_ON_GATE_TPL = new Image(CLIENT_TPL_DIR + "repair_on_gate_option.png");
  48. var REPAIR_ON_STAGE_TPL = new Image(CLIENT_TPL_DIR + "repair_on_stage_option.png");
  49.  
  50. var DISCONNECTED_TPL = new Image(CLIENT_TPL_DIR + "disconnected.png");
  51. var RECONNECTING_TPL = new Image(CLIENT_TPL_DIR + "reconnecting.png");
  52.  
  53. var PET_REPAIR_BTN_TPL = new Image(PET_TPL_DIR + "repair_button.png");
  54. var PET_PLAY_BTN_TPL = new Image(PET_TPL_DIR + "play_button.png");
  55. var PET_STOP_BTN_TPL = new Image(PET_TPL_DIR + "stop_button.png");
  56. var GENERIC_PET_BTN_TPL = new Image(PET_TPL_DIR + "generic_button.png");
  57. var GEAR_DROPDOWN_BTN_TPL = new Image(PET_TPL_DIR + "gear_dropdown_button.png");
  58.  
  59. var AUTOLOOTER_TPL = new Image(PET_TPL_DIR + "autolooter.png");
  60. var PASSIVEMODE_TPL = new Image(PET_TPL_DIR + "passivemode.png");
  61. var GUARDMODE_TPL = new Image(PET_TPL_DIR + "guardmode.png");
  62. var COLLECTOR_TPL = new Image(PET_TPL_DIR + "collector.png");
  63.  
  64. var AUTOLOOTER_ENTRY_TPL = new Image(PET_TPL_DIR + "autolooter_entry.png");
  65. var PASSIVEMODE_ENTRY_TPL = new Image(PET_TPL_DIR + "passivemode_entry.png");
  66. var GUARDMODE_ENTRY_TPL = new Image(PET_TPL_DIR + "guardmode_entry.png");
  67. var COLLECTOR_ENTRY_TPL = new Image(PET_TPL_DIR + "collector_entry.png");
  68.  
  69. var PET_WINDOW_CORNER_TPL = new Image(PET_TPL_DIR + "window_corner.png");
  70. var PET_WINDOW_SIZE = new Size(272, 320); // Including the gear dropdown menu with max gears
  71.  
  72. var LOOT_SWF_URL = "https://pbdo-bot.net/magic/2D/circle_ffff00.swf";
  73. var LOOT_MIN_HSV = new Color(55, 200, 200, "hsv");
  74. var LOOT_MAX_HSV = new Color(70, 255, 255, "hsv");
  75. var LOOT_BLOB_TPL = new BlobTpl(1200, 1220);
  76.  
  77. var GENERAL_CHECK_TIMEOUT_IN_MS = 1000 * 60 * 3; // Check every 3 minutes
  78.  
  79. var MM_LEVEL_1_TPL = new Image(MM_LEVEL_DIR + "1_tpl.png");
  80. var MM_LEVEL_2_TPL = new Image(MM_LEVEL_DIR + "2_tpl.png");
  81. var MM_LEVEL_3_TPL = new Image(MM_LEVEL_DIR + "3_tpl.png");
  82. var MM_LEVEL_4_TPL = new Image(MM_LEVEL_DIR + "4_tpl.png");
  83. var MM_LEVEL_5_TPL = new Image(MM_LEVEL_DIR + "5_tpl.png");
  84. var MM_LEVEL_6_TPL = new Image(MM_LEVEL_DIR + "6_tpl.png");
  85. var MM_LEVEL_7_TPL = new Image(MM_LEVEL_DIR + "7_tpl.png");
  86. var MM_LEVEL_8_TPL = new Image(MM_LEVEL_DIR + "8_tpl.png");
  87. var MM_LEVEL_9_TPL = new Image(MM_LEVEL_DIR + "9_tpl.png");
  88.  
  89. var MM_LEVEL_TPLS = [
  90. MM_LEVEL_1_TPL, MM_LEVEL_2_TPL, MM_LEVEL_3_TPL,
  91. MM_LEVEL_4_TPL, MM_LEVEL_5_TPL, MM_LEVEL_6_TPL,
  92. MM_LEVEL_7_TPL, MM_LEVEL_8_TPL, MM_LEVEL_9_TPL,
  93. ];
  94.  
  95. var MAP_11 = new Image(MM_MAPNAMES_DIR + "11.png");
  96. var MAP_12 = new Image(MM_MAPNAMES_DIR + "12.png");
  97. var MAP_13 = new Image(MM_MAPNAMES_DIR + "13.png");
  98. var MAP_14 = new Image(MM_MAPNAMES_DIR + "14.png");
  99. var MAP_15 = new Image(MM_MAPNAMES_DIR + "15.png");
  100. var MAP_16 = new Image(MM_MAPNAMES_DIR + "16.png");
  101. var MAP_17 = new Image(MM_MAPNAMES_DIR + "17.png");
  102. var MAP_18 = new Image(MM_MAPNAMES_DIR + "18.png");
  103. var MAP_21 = new Image(MM_MAPNAMES_DIR + "21.png");
  104. var MAP_22 = new Image(MM_MAPNAMES_DIR + "22.png");
  105. var MAP_23 = new Image(MM_MAPNAMES_DIR + "23.png");
  106. var MAP_24 = new Image(MM_MAPNAMES_DIR + "24.png");
  107. var MAP_25 = new Image(MM_MAPNAMES_DIR + "25.png");
  108. var MAP_26 = new Image(MM_MAPNAMES_DIR + "26.png");
  109. var MAP_27 = new Image(MM_MAPNAMES_DIR + "27.png");
  110. var MAP_28 = new Image(MM_MAPNAMES_DIR + "28.png");
  111. var MAP_31 = new Image(MM_MAPNAMES_DIR + "31.png");
  112. var MAP_32 = new Image(MM_MAPNAMES_DIR + "32.png");
  113. var MAP_33 = new Image(MM_MAPNAMES_DIR + "33.png");
  114. var MAP_34 = new Image(MM_MAPNAMES_DIR + "34.png");
  115. var MAP_35 = new Image(MM_MAPNAMES_DIR + "35.png");
  116. var MAP_36 = new Image(MM_MAPNAMES_DIR + "36.png");
  117. var MAP_37 = new Image(MM_MAPNAMES_DIR + "37.png");
  118. var MAP_38 = new Image(MM_MAPNAMES_DIR + "38.png");
  119. var MAP_41 = new Image(MM_MAPNAMES_DIR + "41.png");
  120. var MAP_42 = new Image(MM_MAPNAMES_DIR + "42.png");
  121. var MAP_43 = new Image(MM_MAPNAMES_DIR + "43.png");
  122. var MAP_44 = new Image(MM_MAPNAMES_DIR + "44.png");
  123. var MAP_45 = new Image(MM_MAPNAMES_DIR + "45.png");
  124. var MAP_51 = new Image(MM_MAPNAMES_DIR + "51.png");
  125. var MAP_52 = new Image(MM_MAPNAMES_DIR + "52.png");
  126. var MAP_53 = new Image(MM_MAPNAMES_DIR + "53.png");
  127.  
  128. var MAPNAME_TPLS = [
  129. MAP_11, MAP_12, MAP_13, MAP_14, MAP_15, MAP_16, MAP_17, MAP_18,
  130. MAP_21, MAP_22, MAP_23, MAP_24, MAP_25, MAP_26, MAP_27, MAP_28,
  131. MAP_31, MAP_32, MAP_33, MAP_34, MAP_35, MAP_36, MAP_37, MAP_38,
  132. MAP_41, MAP_42, MAP_43, MAP_44, MAP_45, MAP_51, MAP_52, MAP_53,
  133. ];
  134.  
  135. // We use different mapnames to represent the space map as a javascript object.
  136. // Instead of '4-1' we write '__4_1'.
  137.  
  138. var INTERN_MAPNAME_MAPPING = [ // Which mapname template in MAPNAME_TPLS corresponds to which mapname.
  139. '__1_1', '__1_2', '__1_3', '__1_4', '__1_5', '__1_6', '__1_7', '__1_8',
  140. '__2_1', '__2_2', '__2_3', '__2_4', '__2_5', '__2_6', '__2_7', '__2_8',
  141. '__3_1', '__3_2', '__3_3', '__3_4', '__3_5', '__3_6', '__3_7', '__3_8',
  142. '__4_1', '__4_2', '__4_3', '__4_4', '__4_5', '__5_1', '__5_2', '__5_3',
  143. ];
  144.  
  145. var OUTER_MINIMAP_SIZES = [
  146. { width: 228, height: 180 },
  147. { width: 247, height: 192 },
  148. { width: 270, height: 207 },
  149. { width: 300, height: 225 },
  150. { width: 337, height: 248 },
  151. { width: 387, height: 279 },
  152. { width: 457, height: 323 },
  153. { width: 562, height: 389 },
  154. { width: 737, height: 498 }
  155. ];
  156.  
  157. var INNER_MINIMAP_SIZES = [
  158. { width: 187, height: 115 },
  159. { width: 206, height: 127 },
  160. { width: 229, height: 142 },
  161. { width: 259, height: 160 },
  162. { width: 296, height: 183 },
  163. { width: 346, height: 214 },
  164. { width: 416, height: 258 },
  165. { width: 521, height: 324 },
  166. { width: 696, height: 433 }
  167. ];
  168.  
  169. var OUTER_MM_TO_INNER_MM_OFFSET = {
  170. x: 28,
  171. y: 49
  172. };
  173.  
  174. var VELOCITY_MARGIN = { x: 120, y: 26 };
  175. var VELOCITY_SIZE = new Size(62, 21);
  176.  
  177. var MAPNAME_WIDTH = 51;
  178. var MAPNAME_HEIGHT = 26;
  179. var MAPNAME_MARGIN_TOP = 23;
  180.  
  181. var SUPPORTED_MAPS = [
  182. "1-1", "1-2", "1-3", "1-4", "1-5", "1-6", "1-7", "1-8",
  183. "2-1", "2-2", "2-3", "2-4", "2-5", "2-6", "2-7", "2-8",
  184. "3-1", "3-2", "3-3", "3-4", "3-5", "3-6", "3-7", "3-8",
  185. "4-1", "4-2", "4-3", "4-4", "4-5",
  186. ];
  187.  
  188. var MAP_GRAPH = {
  189. __1_1: { __1_2: 1 },
  190. __1_2: { __1_1: 1, __1_3: 1, __1_4: 1 },
  191. __1_3: { __1_2: 1, __1_4: 1, __2_3: 1 },
  192. __1_4: { __1_2: 1, __1_3: 1, __3_4: 1, __4_1: 1 },
  193. __2_1: { __2_2: 1 },
  194. __2_2: { __2_1: 1, __2_3: 1, __2_4: 1 },
  195. __2_3: { __1_3: 1, __2_2: 1, __2_4: 1 },
  196. __2_4: { __2_2: 1, __2_3: 1, __3_3: 1, __4_2: 1 },
  197. __3_1: { __3_2: 1 },
  198. __3_2: { __3_1: 1, __3_3: 1, __3_4: 1 },
  199. __3_3: { __2_4: 1, __3_2: 1, __3_4: 1 },
  200. __3_4: { __1_4: 1, __3_2: 1, __3_3: 1, __4_3: 1 },
  201. __4_1: { __1_4: 1, __4_2: 1, __4_3: 1, __4_4: 1 },
  202. __4_2: { __2_4: 1, __4_1: 1, __4_3: 1, __4_4: 1 },
  203. __4_3: { __3_4: 1, __4_1: 1, __4_2: 1, __4_4: 1 },
  204. __4_4: { __1_5: 1, __2_5: 1, __3_5: 1, __4_1: 1, __4_2: 1, __4_3: 1 },
  205. __4_5: { __1_5: 1, __2_5: 1, __3_5: 1 },
  206. __1_5: { __1_6: 1, __1_7: 1, __4_4: 1, __4_5: 1 },
  207. __1_6: { __1_5: 1, __1_8: 1 },
  208. __1_7: { __1_5: 1, __1_8: 1 },
  209. __1_8: { __1_6: 1, __1_7: 1 },
  210. __2_5: { __2_6: 1, __2_7: 1, __4_4: 1, __4_5: 1 },
  211. __2_6: { __2_5: 1, __2_8: 1 },
  212. __2_7: { __2_5: 1, __2_8: 1 },
  213. __2_8: { __2_6: 1, __2_7: 1 },
  214. __3_5: { __3_6: 1, __3_7: 1, __4_4: 1, __4_5: 1 },
  215. __3_6: { __3_5: 1, __3_8: 1 },
  216. __3_7: { __3_5: 1, __3_8: 1 },
  217. __3_8: { __3_6: 1, __3_7: 1 }
  218. };
  219.  
  220. var GATE_TL = [ [ 17, 17 ], [ 19, 19 ], [ 21, 21 ], [ 24, 24 ], [ 27, 27 ], [ 32, 32 ], [ 39, 39 ], [ 49, 49 ], [ 65, 65 ] ];
  221. var GATE_TR = [ [ 167, 17 ], [ 184, 19 ], [ 204, 21 ], [ 230, 24 ], [ 263, 27 ], [ 307, 32 ], [ 369, 39 ], [ 461, 49 ], [ 615, 65 ] ];
  222. var GATE_BL = [ [ 17, 103 ], [ 19, 114 ], [ 21, 126 ], [ 24, 142 ], [ 27, 163 ], [ 32, 190 ], [ 39, 229 ], [ 49, 286 ], [ 65, 382 ] ];
  223. var GATE_BR = [ [ 167, 103 ], [ 184, 114 ], [ 204, 125 ], [ 230, 142 ], [ 263, 163 ], [ 307, 190 ], [ 369, 229 ], [ 461, 286 ], [ 615, 382 ] ];
  224. var GATE_TRO = [ [ 149, 12 ], [ 164, 14 ], [ 182, 15 ], [ 205, 17 ], [ 234, 20 ], [ 274, 24 ], [ 329, 29 ], [ 411, 36 ], [ 549, 49 ] ];
  225. var GATE_CT = [ [ 90, 12 ], [ 99, 14 ], [ 110, 15 ], [ 124, 17 ], [ 141, 20 ], [ 165, 24 ], [ 199, 29 ], [ 249, 36 ], [ 332, 49 ] ];
  226. var GATE_CR = [ [ 171, 53 ], [ 189, 59 ], [ 210, 65 ], [ 236, 74 ], [ 270, 84 ], [ 315, 99 ], [ 379, 119 ], [ 474, 149 ], [ 632, 199 ] ];
  227. var GATE_CB = [ [ 90, 108 ], [ 99, 119 ], [ 110, 132 ], [ 124, 149 ], [ 141, 170 ], [ 165, 199 ], [ 199, 239 ], [ 249, 299 ], [ 332, 399 ] ];
  228. var GATE_CL = [ [ 12, 53 ], [ 14, 59 ], [ 15, 65 ], [ 17, 74 ], [ 20, 84 ], [ 24, 99 ], [ 29, 119 ], [ 36, 149 ], [ 49, 199 ] ];
  229. var GATE_CC = [ [ 94, 60 ], [ 104, 66 ], [ 115, 74 ], [ 130, 83 ], [ 149, 95 ], [ 174, 111 ], [ 209, 134 ], [ 261, 167 ], [ 349, 254 ] ];
  230. var GATE_44T = [ [ 126, 5 ], [ 139, 5 ], [ 154, 6 ], [ 174, 7 ], [ 199, 8 ], [ 232, 10 ], [ 279, 12 ], [ 350, 17 ], [ 445, 22 ] ];
  231. var GATE_44L = [ [ 30, 60 ], [ 34, 66 ], [ 37, 74 ], [ 42, 83 ], [ 49, 95 ], [ 57, 111 ], [ 69, 134 ], [ 87, 168 ], [ 115, 224 ] ];
  232. var GATE_44B = [ [ 126, 113 ], [ 139, 124 ], [ 154, 138 ], [ 174, 156 ], [ 199, 178 ], [ 232, 208 ], [ 279, 250 ], [ 350, 314 ], [ 465, 417 ] ];
  233. var GATE_44TI = [ [ 98, 53 ], [ 108, 58 ], [ 120, 65 ], [ 135, 73 ], [ 155, 84 ], [ 181, 98 ], [ 218, 118 ], [ 272, 149 ], [ 364, 198 ] ];
  234. var GATE_44LI = [ [ 86, 60 ], [ 95, 66 ], [ 105, 74 ], [ 119, 83 ], [ 136, 95 ], [ 159, 111 ], [ 191, 134 ], [ 240, 168 ], [ 319, 224 ] ];
  235. var GATE_44BI = [ [ 97, 63 ], [ 107, 69 ], [ 119, 77 ], [ 134, 88 ], [ 153, 101 ], [ 180, 119 ], [ 217, 142 ], [ 271, 179 ], [ 363, 239 ] ];
  236. var GATE_45T = [ [ 126, 5 ], [ 139, 5 ], [ 154, 6 ], [ 174, 7 ], [ 199, 8 ], [ 232, 10 ], [ 279, 12 ], [ 349, 16 ], [ 465, 22 ] ];
  237. var GATE_45L = [ [ 30, 60 ], [ 34, 66 ], [ 37, 74 ], [ 42, 83 ], [ 49, 95 ], [ 57, 111 ], [ 69, 134 ], [ 86, 167 ], [ 115, 224 ] ];
  238. var GATE_45B = [ [ 126, 115 ], [ 139, 127 ], [ 154, 141 ], [ 174, 159 ], [ 199, 182 ], [ 232, 212 ], [ 279, 254 ], [ 349, 319 ], [ 445, 426 ] ];
  239. var GATE_45TI = [ [ 112, 27 ], [ 124, 30 ], [ 137, 34 ], [ 155, 38 ], [ 177, 44 ], [ 207, 51 ], [ 249, 62 ], [ 311, 77 ], [ 415, 104 ] ];
  240. var GATE_45LI = [ [ 54, 59 ], [ 60, 65 ], [ 66, 72 ], [ 75, 82 ], [ 86, 94 ], [ 100, 109 ], [ 121, 132 ], [ 151, 165 ], [ 202, 220 ] ];
  241. var GATE_45BI = [ [ 112, 93 ], [ 124, 102 ], [ 137, 114 ], [ 155, 128 ], [ 177, 146 ], [ 207, 171 ], [ 249, 206 ], [ 311, 257 ], [ 415, 344 ] ];
  242.  
  243. var JGATE_POS = {
  244. __1_1: { __1_2: GATE_BR },
  245. __1_2: { __1_1: GATE_TL, __1_3: GATE_TR, __1_4: GATE_BR },
  246. __1_3: { __1_2: GATE_BL, __1_4: GATE_BR, __2_3: GATE_TR },
  247. __1_4: { __1_2: GATE_TL, __1_3: GATE_TR, __1_5: GATE_CR, __3_4: GATE_BR },
  248. __1_5: { __1_4: GATE_CL, __2_5: GATE_TR, __3_5: GATE_BR, __4_1: GATE_CC },
  249. __2_1: { __2_2: GATE_BL },
  250. __2_2: { __2_1: GATE_TR, __2_3: GATE_BL, __2_4: GATE_BR },
  251. __2_3: { __2_2: GATE_TR, __2_4: GATE_BR, __1_3: GATE_BL },
  252. __2_4: { __2_2: GATE_TL, __2_3: GATE_TR, __2_5: GATE_CB, __3_3: GATE_BL },
  253. __2_5: { __2_4: GATE_CT, __1_5: GATE_BL, __3_5: GATE_BR, __4_1: GATE_CC },
  254. __3_1: { __3_2: GATE_TL },
  255. __3_2: { __3_1: GATE_BR, __3_3: GATE_TR, __3_4: GATE_TL },
  256. __3_3: { __3_2: GATE_BR, __3_4: GATE_BL, __2_4: GATE_TL },
  257. __3_4: { __3_2: GATE_BR, __3_3: GATE_TR, __3_5: GATE_CT, __1_4: GATE_TL },
  258. __3_5: { __3_4: GATE_CR, __1_5: GATE_BL, __2_5: GATE_TL, __4_1: GATE_CC },
  259. __4_1: { __1_5: GATE_44LI, __2_5: GATE_44TI, __3_5: GATE_44BI, __7_1: GATE_44L, __8_1: GATE_44T, __9_1: GATE_44B },
  260. __5_1: { __6_1: GATE_45BI, __7_1: GATE_45L, __8_1: GATE_45T, __9_1: GATE_45B },
  261. __7_1: { __4_1: GATE_CR, __5_1: GATE_BR, __7_2: GATE_TL, __7_3: GATE_BL },
  262. __7_2: { __7_1: GATE_BR, __7_4: GATE_BL },
  263. __7_3: { __7_1: GATE_TR, __7_4: GATE_TL },
  264. __7_4: { __7_2: GATE_TR, __7_3: GATE_BR },
  265. __8_1: { __4_1: GATE_BL, __5_1: GATE_BR, __8_2: GATE_TL, __8_3: GATE_TR },
  266. __8_2: { __8_1: GATE_BL, __8_4: GATE_TR },
  267. __8_3: { __8_1: GATE_BL, __8_4: GATE_TR },
  268. __8_4: { __8_2: GATE_BL, __8_3: GATE_BR },
  269. __9_1: { __4_1: GATE_TL, __5_1: GATE_TRO, __9_2: GATE_BL, __9_3: GATE_BR },
  270. __9_2: { __9_1: GATE_TL, __9_4: GATE_BR },
  271. __9_3: { __9_1: GATE_BL, __9_4: GATE_BR },
  272. __9_4: { __9_2: GATE_BL, __9_3: GATE_TL }
  273. };
  274.  
  275. var JGATE_POS = {
  276. __1_1: { __1_2: GATE_BR },
  277. __1_2: { __1_1: GATE_TL, __1_3: GATE_TR, __1_4: GATE_BR },
  278. __1_3: { __1_2: GATE_BL, __1_4: GATE_BR, __2_3: GATE_TR },
  279. __1_4: { __1_2: GATE_TL, __1_3: GATE_TR, __4_1: GATE_CR, __3_4: GATE_BR },
  280. __4_1: { __1_4: GATE_CL, __4_2: GATE_TR, __4_3: GATE_BR, __4_4: GATE_CC },
  281. __2_1: { __2_2: GATE_BL },
  282. __2_2: { __2_1: GATE_TR, __2_3: GATE_BL, __2_4: GATE_BR },
  283. __2_3: { __2_2: GATE_TR, __2_4: GATE_BR, __1_3: GATE_BL },
  284. __2_4: { __2_2: GATE_TL, __2_3: GATE_TR, __4_2: GATE_CB, __3_3: GATE_BL },
  285. __4_2: { __2_4: GATE_CT, __4_1: GATE_BL, __4_3: GATE_BR, __4_4: GATE_CC },
  286. __3_1: { __3_2: GATE_TL },
  287. __3_2: { __3_1: GATE_BR, __3_3: GATE_TR, __3_4: GATE_TL },
  288. __3_3: { __3_2: GATE_BR, __3_4: GATE_BL, __2_4: GATE_TL },
  289. __3_4: { __3_2: GATE_BR, __3_3: GATE_TR, __4_3: GATE_CT, __1_4: GATE_TL },
  290. __4_3: { __3_4: GATE_CR, __4_1: GATE_BL, __4_2: GATE_TL, __4_4: GATE_CC },
  291. __4_4: { __4_1: GATE_44LI, __4_2: GATE_44TI, __4_3: GATE_44BI, __1_5: GATE_44L, __2_5: GATE_44T, __3_5: GATE_44B },
  292. __4_5: { __5_1: GATE_45BI, __1_5: GATE_45L, __2_5: GATE_45T, __3_5: GATE_45B },
  293. __1_5: { __4_4: GATE_CR, __4_5: GATE_BR, __1_6: GATE_TL, __1_7: GATE_BL },
  294. __1_6: { __1_5: GATE_BR, __1_8: GATE_BL },
  295. __1_7: { __1_5: GATE_TR, __1_8: GATE_TL },
  296. __1_8: { __1_6: GATE_TR, __1_7: GATE_BR },
  297. __2_5: { __4_4: GATE_BL, __4_5: GATE_BR, __2_6: GATE_TL, __2_7: GATE_TR },
  298. __2_6: { __2_5: GATE_BL, __2_8: GATE_TR },
  299. __2_7: { __2_5: GATE_BL, __2_8: GATE_TR },
  300. __2_8: { __2_6: GATE_BL, __2_7: GATE_BR },
  301. __3_5: { __4_4: GATE_TL, __4_5: GATE_TRO, __3_6: GATE_BL, __3_7: GATE_BR },
  302. __3_6: { __3_5: GATE_TL, __3_8: GATE_BR },
  303. __3_7: { __3_5: GATE_BL, __3_8: GATE_BR },
  304. __3_8: { __3_6: GATE_BL, __3_7: GATE_TL }
  305. }
  306.  
  307. var OPTIMISTIC_ACCELERATION_TIME_IN_MS = 500;
  308. var SAFE_ACCELERATION_TIME_IN_MS = 2000;
  309. var JUMP_TIMEOUT_IN_MS = 5000;
  310.  
  311. // +--------------------------------+
  312. // | Convenience Methods and Helper |
  313. // +--------------------------------+
  314.  
  315. function convertInternToExternMapname(intern_mapname) {
  316. if (intern_mapname.indexOf("__") === -1) {
  317. Helper.debug(intern_mapname, "is not an intern mapname");
  318. }
  319. return intern_mapname.replace("__", "").replace("_", "-");
  320. }
  321.  
  322. function convertExternToInternMapname(extern_mapname) {
  323. if (extern_mapname.indexOf("-") === -1) {
  324. Helper.debug(extern_mapname, "is not an extern mapname");
  325. }
  326. return "__" + extern_mapname.replace("-", "_");
  327. }
  328.  
  329. function moveMouseToCenter() {
  330. // This methods helps prevents unwished popups of ingame objects.
  331. Browser.moveMouseTo(Browser.getRect().getCenter());
  332. }
  333.  
  334. function leftClickAndPreventHover(point) {
  335. Browser.leftClick(point);
  336. moveMouseToCenter();
  337. }
  338.  
  339. function getClosestMatch(matches) {
  340. var closest_match = new Match();
  341. var center = Browser.getRect().getCenter();
  342.  
  343. for (var i = 0; i < matches.length; i++) {
  344. var match = matches[i];
  345.  
  346. if (!closest_match.isValid() || match.getRect().getCenter().manhattanDistance(center)
  347. < closest_match.getRect().getCenter().manhattanDistance(center)) {
  348. closest_match = match;
  349. }
  350. }
  351.  
  352. return closest_match;
  353. }
  354.  
  355. // +--------------------------+
  356. // | Ingame Minimap Managment |
  357. // +--------------------------+
  358.  
  359. var Minimap = function() {
  360. this.cached_level = undefined;
  361. this.cached_position = new Point(-1, -1);
  362. this.cached_intern_mapname = undefined;
  363. };
  364.  
  365. Minimap.prototype.calculateTelemetry = function(screenshot) {
  366. for (var i = MM_LEVEL_TPLS.length - 1; i >= 0; i--) {
  367. var match = Vision.findMatch(screenshot, MM_LEVEL_TPLS[i], 0.98);
  368.  
  369. Helper.debug("Minimap icon:", match);
  370.  
  371. if (match.isValid()) {
  372. this.cached_level = i;
  373. this.cached_position = match.getRect().getTopLeft();
  374. return;
  375. }
  376. }
  377. };
  378.  
  379. Minimap.prototype.getLevel = function(ignore_cache) {
  380. if (ignore_cache === true || this.cached_level === undefined) {
  381. var screenshot = Browser.takeScreenshot();
  382. this.calculateTelemetry(screenshot);
  383. }
  384. return this.cached_level;
  385. }
  386.  
  387. Minimap.prototype.getPosition = function(ignore_cache) {
  388. if (ignore_cache === true || this.cached_position.x == -1) {
  389. var screenshot = Browser.takeScreenshot();
  390. this.calculateTelemetry(screenshot);
  391. }
  392. return this.cached_position;
  393. }
  394.  
  395. Minimap.prototype.getOuterRect = function(ignore_cache) {
  396. var level = this.getLevel(ignore_cache === true);
  397. var position = this.getPosition(ignore_cache === true);
  398. var outer_size = OUTER_MINIMAP_SIZES[level];
  399. return new Rect(
  400. position.getX(),
  401. position.getY(),
  402. outer_size.width,
  403. outer_size.height
  404. )
  405. }
  406.  
  407. Minimap.prototype.getInnerRect = function(ignore_cache) {
  408. var level = this.getLevel(ignore_cache === true);
  409. var position = this.getPosition(ignore_cache === true);
  410. var inner_size = INNER_MINIMAP_SIZES[level];
  411. return new Rect(
  412. position.getX() + OUTER_MM_TO_INNER_MM_OFFSET.x,
  413. position.getY() + OUTER_MM_TO_INNER_MM_OFFSET.y,
  414. inner_size.width,
  415. inner_size.height
  416. );
  417. }
  418.  
  419. Minimap.prototype.leftClick = function(coords) {
  420. var inner_rect = this.getInnerRect();
  421. var onscreen_x = inner_rect.getLeft() + coords.getX();
  422. var onscreen_y = inner_rect.getTop() + coords.getY();
  423. var realcoords = new Point(onscreen_x, onscreen_y);
  424. Browser.leftClick(realcoords);
  425. }
  426.  
  427. Minimap.prototype.getMinimapImage = function() {
  428. var screenshot = Browser.takeScreenshot();
  429. var position = this.getPosition();
  430. var outer_mm = this.getOuterRect();
  431.  
  432. return screenshot.copy(new Rect(
  433. position.getX(),
  434. position.getY(),
  435. outer_mm.getWidth(),
  436. outer_mm.getHeight()
  437. ));
  438. }
  439.  
  440. Minimap.prototype.getVelocityImage = function() {
  441. return this.getMinimapImage().copy(new Rect(
  442. VELOCITY_MARGIN.x,
  443. VELOCITY_MARGIN.y,
  444. VELOCITY_SIZE.getWidth(),
  445. VELOCITY_SIZE.getHeight()
  446. ));
  447. }
  448.  
  449. Minimap.prototype.getMapnameImage = function() {
  450. return this.getMinimapImage().copy(new Rect(
  451. 0,
  452. MAPNAME_MARGIN_TOP,
  453. MAPNAME_WIDTH,
  454. MAPNAME_HEIGHT
  455. ));
  456. }
  457.  
  458. Minimap.prototype.getInternMapname = function(use_cache) {
  459. if (use_cache === true) {
  460. return this.cached_intern_mapname;
  461. }
  462.  
  463. var mapname_image = this.getMapnameImage();
  464. var min_hsv = new Color(0, 0, 100, "hsv");
  465. var max_hsv = new Color(200, 15, 255, "hsv");
  466. var monochrome_mn = mapname_image.isolateColorRange(min_hsv, max_hsv);
  467.  
  468. for (var i = 0; i < MAPNAME_TPLS.length; i++) {
  469. var mapname_tpl = MAPNAME_TPLS[i];
  470. var match = Vision.findMatch(monochrome_mn, mapname_tpl, 0.95);
  471.  
  472. Helper.debug("Mapname match, #" + i + ":", match);
  473.  
  474. if (match.isValid()) {
  475. this.cached_intern_mapname = INTERN_MAPNAME_MAPPING[i];
  476. return this.cached_intern_mapname;
  477. }
  478. }
  479.  
  480. Helper.debug("No mapname matched.");
  481. return "";
  482. }
  483.  
  484. // +-----------------------------+
  485. // | Local and Global Navigation |
  486. // +-----------------------------+
  487.  
  488. var Navigator = function(minimap) {
  489. this.minimap = minimap;
  490. }
  491.  
  492. Navigator.prototype.getNextDestination = function() {
  493. return this.minimap.getInnerRect().randomPoint();
  494. }
  495.  
  496. Navigator.prototype.shipIsMoving = function() {
  497. // TODO: move this once a Ship class exists
  498.  
  499. // Isolating white on the image will render it black with white numbers on.
  500. // We compare it to an black image. If they are the same, we're not moving.
  501. var min_hsv = new Color(0, 0, 0, "hsv");
  502. var max_hsv = new Color(10, 255, 255, "hsv");
  503.  
  504. var velocity_image = this.minimap.getVelocityImage();
  505. var blacked_bg = velocity_image.isolateColorRange(min_hsv, max_hsv);
  506.  
  507. var pixel_equality = blacked_bg.pixelEquality(NO_VELOCITY_REF);
  508. return pixel_equality < 0.97;
  509. }
  510.  
  511. Navigator.prototype.navigateToMap = function(dest_intern_mapname) {
  512. var current_intern_mapname = this.minimap.getInternMapname();
  513.  
  514. if (current_intern_mapname == "") {
  515. Helper.debug("Current map unknown (navigateToMap)");
  516. return;
  517. }
  518.  
  519. if (current_intern_mapname === dest_intern_mapname) {
  520. Helper.log("We are already on the destination map.");
  521. return;
  522. }
  523.  
  524. var ex_cm = convertInternToExternMapname(current_intern_mapname);
  525. var ex_dm = convertInternToExternMapname(dest_intern_mapname);
  526.  
  527. var route = Algorithm.dijkstra(MAP_GRAPH, current_intern_mapname, dest_intern_mapname);
  528. if (route.length == 0) {
  529. Helper.log("No route from", ex_cm, "to", ex_dm, "found.");
  530. return;
  531. }
  532.  
  533. Helper.debug("Disjkstra path to destination map:", route);
  534. route.shift(); // remove first element (the map were currently on)
  535.  
  536. var route_found_message = "Route from " + ex_cm + " to " + ex_dm + " found: " + ex_cm;
  537. for (var i = 0; i < route.length; i++) {
  538. route_found_message += " -> " + convertInternToExternMapname(route[i]);
  539. }
  540. Helper.log(route_found_message);
  541.  
  542. // Travel and jump from map to map.
  543.  
  544. for (var i = 0; i < route.length; i++) {
  545. var waypoint = route[i];
  546.  
  547. var ex_wp = convertInternToExternMapname(waypoint);
  548. Helper.log("Trying to navigate to", ex_wp);
  549.  
  550. if (!this.jumpTo(waypoint)) {
  551. Helper.log("Unable to navigate to", ex_wp);
  552. return;
  553. }
  554. }
  555. }
  556.  
  557. Navigator.prototype.jumpTo = function(dest_intern_mapname) {
  558. var current_intern_mapname = this.minimap.getInternMapname(false); // Do not use the cached mapname
  559. var mm_level = this.minimap.getLevel(false); // Do not use the cached level
  560.  
  561. if (mm_level == -1 || current_intern_mapname === "") {
  562. return false;
  563. }
  564.  
  565. var dest_jgate_arr = JGATE_POS[current_intern_mapname][dest_intern_mapname][mm_level];
  566. var dest_jgate_pos = new Point(dest_jgate_arr[0], dest_jgate_arr[1]);
  567.  
  568. var rdm = convertInternToExternMapname(dest_intern_mapname);
  569. Helper.log("Trying to travel to", rdm);
  570.  
  571. if (!this.travelTo(dest_jgate_pos)) {
  572. Helper.log("Unable to travel to", rdm);
  573. return false;
  574. }
  575.  
  576. // TODO: Ask botfather devs to not only add keyboard support to their browser,
  577. // but make it availabe to the script engine, so that we can use the 'J'-key to jump.
  578.  
  579. Helper.log("Waiting for the jump button to become clickable.");
  580. Helper.sleep(3);
  581. Helper.log("Trying to find and click the jump button.");
  582.  
  583. var screenshot = Browser.takeScreenshot();
  584. var jump_button_match = Vision.findMatch(screenshot, JUMP_BUTTON_TPL, 0.8);
  585. Helper.debug("Best jump button match found:", jump_button_match);
  586.  
  587. if (!jump_button_match.isValid()) {
  588. Helper.log("Clicking the jump button failed.");
  589. return false;
  590. }
  591.  
  592. Browser.leftClick(jump_button_match.getRect().getCenter());
  593. Helper.log("Clicked the jump button.");
  594.  
  595. Helper.log("Wait for the jump to complete");
  596. Helper.msleep(JUMP_TIMEOUT_IN_MS); // Jumping takes some time. TODO: Make this dynamic
  597. return true;
  598. }
  599.  
  600. Navigator.prototype.travelTo = function(dest_coords) {
  601. this.minimap.leftClick(dest_coords);
  602. Helper.msleep(SAFE_ACCELERATION_TIME_IN_MS);
  603.  
  604. // TODO: Come up with something "better"?
  605. if (!this.monitorQuickFlight(1000 * 90)) {
  606. // The monitor method aborded after 90 seconds. Thats super long, something might went wrong.
  607. Helper.log("A flight took too long, something might went wrong.");
  608. return false;
  609. }
  610.  
  611. Helper.log("Ship no longer traveling, destination reached");
  612. return true;
  613. }
  614.  
  615. Navigator.prototype.monitorQuickFlight = function(max_flight_time_in_ms) {
  616. var timer = new Timer();
  617. timer.start();
  618.  
  619. while (this.shipIsMoving()) {
  620. if (timer.hasExpired(max_flight_time_in_ms)) {
  621. return false; // max flight time reached
  622. }
  623.  
  624. Helper.debug("Flying somewhere quickly...");
  625. Helper.msleep(100);
  626. }
  627.  
  628. Helper.debug("Destination reached, quick flight done.");
  629. return true; // Ship reached destination
  630. }
  631.  
  632. // +------------------+
  633. // | Client Managment |
  634. // +------------------+
  635.  
  636. var Client = function() {
  637. this.revives_done = 0;
  638. this.cached_logout_button = new Match();
  639. }
  640.  
  641. Client.prototype.numRevivesDone = function() {
  642. return this.revives_done;
  643. }
  644.  
  645. Client.prototype.isDestroyed = function() {
  646. var screenshot = Browser.takeScreenshot();
  647. return Vision.findMatch(screenshot, REPAIR_ON_BASE_TPL, 0.95).isValid();
  648. }
  649.  
  650. Client.prototype.revive = function(location) {
  651. var screenshot = Browser.takeScreenshot();
  652. var option_match = new Match();
  653.  
  654. switch (location) {
  655. case "stage":
  656. option_match = Vision.findMatch(screenshot, REPAIR_ON_STAGE_TPL, 0.95);
  657. break;
  658. case "gate":
  659. option_match = Vision.findMatch(screenshot, REPAIR_ON_GATE_TPL, 0.95);
  660. break;
  661. case "base":
  662. option_match = Vision.findMatch(screenshot, REPAIR_ON_BASE_TPL, 0.95);
  663. break;
  664. // base is selected by default anyway
  665. }
  666.  
  667. if (option_match.isValid()) {
  668. Browser.leftClick(option_match.getRect().getCenter());
  669. Helper.sleep(1);
  670. Helper.log("Selected ship repair location...");
  671. }
  672.  
  673. else {
  674. Helper.log("Couldn't select the desired ship repair location.");
  675. // This either means the ship is not destroyed or there is only the "base" location available.
  676. }
  677.  
  678. var button_match = Vision.findMatch(screenshot, REPAIR_CONFIRM_TPL, 0.95);
  679. if (!button_match.isValid())
  680. {
  681. Helper.log("Unable to find the ship repair confirm button.");
  682. return false;
  683. }
  684.  
  685. Browser.leftClick(button_match.getRect().getCenter());
  686. Helper.sleep(1);
  687.  
  688. Helper.log("Ship repair confirmed.");
  689.  
  690. this.revives_done++;
  691. return true;
  692. }
  693.  
  694. Client.prototype.isDisconnected = function(pretaken_screenshot) {
  695. var screenshot = pretaken_screenshot ? pretaken_screenshot : Browser.takeScreenshot();
  696. return Vision.findMatch(screenshot, DISCONNECTED_TPL, 0.95).isValid();
  697. }
  698.  
  699. Client.prototype.reconnect = function(pretaken_screenshot) {
  700. var screenshot = pretaken_screenshot ? pretaken_screenshot : Browser.takeScreenshot();
  701. var disconnected_match = Vision.findMatch(screenshot, DISCONNECTED_TPL, 0.95);
  702.  
  703. if (!disconnected_match.isValid()) {
  704. Helper.debug("The client seems to be not disconnected.");
  705. return false;
  706. }
  707.  
  708. // The reconnect button is slightly below the match (we're looking for the big X icon)
  709. var tpl_bl = disconnected_match.getRect().getBottomLeft();
  710. var new_connection_btn = tpl_bl.pointAdded(new Point(10, 10));
  711.  
  712. Browser.leftClick(new_connection_btn);
  713. Helper.log("Reconnect button clicked.");
  714.  
  715. do {
  716. Helper.log("Reconnecting...");
  717. Helper.sleep(1);
  718. }
  719. while (Vision.findMatch(Browser.takeScreenshot(), RECONNECTING_TPL, 0.95).isValid());
  720.  
  721. Helper.log("Reconnected.");
  722. return true;
  723. }
  724.  
  725. Client.prototype.isIngame = function() {
  726. if (Browser.getUrl().getQuery().indexOf("internalMapRevolution") === -1) {
  727. Helper.debug("Client not ingame. Url does not contain internalMapRevolution");
  728. return false;
  729. }
  730.  
  731. var screenshot = Browser.takeScreenshot();
  732. var logout_button_match = Vision.findMatch(Browser.takeScreenshot(), LOGOUT_BUTTON_TPL, 0.99);
  733.  
  734. Helper.debug("Logout button match (isIngame check):", logout_button_match);
  735. return logout_button_match.isValid() || this.isDisconnected() || this.isDestroyed();
  736. }
  737.  
  738. Client.prototype.autoLogin = function(username, password) {
  739. Helper.log("Loading game website...");
  740.  
  741. Browser.loadUrl("http://darkorbit.com/?lang=en");
  742. Browser.finishLoading();
  743.  
  744. Helper.log("Entering account credentials...");
  745.  
  746. var fill_uname_js = "document.getElementById('bgcdw_login_form_username').value = '" + username + "';";
  747. Browser.executeJavascript(fill_uname_js);
  748. Helper.sleep(1);
  749.  
  750. var fill_pword_js = "document.getElementById('bgcdw_login_form_password').value = '" + password + "';";
  751. Browser.executeJavascript(fill_pword_js);
  752. Helper.sleep(1);
  753.  
  754. var formsubmit_js = "document.bgcdw_login_form.submit();";
  755. Browser.executeJavascript(formsubmit_js);
  756.  
  757. Helper.log("Form submitted.");
  758. Helper.sleep(2);
  759.  
  760. Browser.finishLoading();
  761. this.getIngame();
  762. }
  763.  
  764. Client.prototype.getIngame = function() {
  765. var ingame_url = Browser.getUrl().getHost() + "/indexInternal.es?action=internalMapRevolution";
  766.  
  767. Browser.loadUrl(ingame_url);
  768. Browser.finishLoading();
  769.  
  770. while (!this.isIngame()) {
  771. Helper.log("Waiting for the game to load...")
  772.  
  773. var screenshot = Browser.takeScreenshot();
  774. var start_button_match = Vision.findMatch(screenshot, START_BUTTON_TPL, 0.97);
  775.  
  776. if (start_button_match.isValid()) {
  777. Browser.leftClick(start_button_match.getRect().getCenter());
  778. Helper.log("Game start button clicked.");
  779. }
  780.  
  781. Helper.sleep(3);
  782. }
  783. }
  784.  
  785. Client.prototype.modifyResources2D = function() {
  786. Browser.replaceResource("box2.swf", LOOT_SWF_URL);
  787. Browser.replaceResource("orangePumpkin.swf", LOOT_SWF_URL);
  788. Browser.replaceResource("winterGiftBox.swf", LOOT_SWF_URL);
  789. Browser.replaceResource("icyBox.swf", LOOT_SWF_URL);
  790. }
  791.  
  792. Client.prototype.haltShip = function(ignore_cache) {
  793. // TODO: move this once a Ship class exists
  794. if (ignore_cache === true || !this.cached_logout_button.isValid()) {
  795. this.cached_logout_button = Vision.findMatch(Browser.takeScreenshot(), LOGOUT_BUTTON_TPL, 0.99);
  796. Helper.debug("Cached logout button (haltShip):", this.cached_logout_button);
  797. }
  798. if (this.cached_logout_button.isValid()) {
  799. // Triggering the logout process makes the ship stop moving instantly
  800. Browser.leftClick(this.cached_logout_button.getRect().getCenter());
  801. Helper.msleep(50); // It takes a moment for the ship to stop
  802. return true;
  803. }
  804. return false;
  805. }
  806.  
  807. // +---------------+
  808. // | PET Managment |
  809. // +---------------+
  810.  
  811. var PET = function() {
  812. this.cached_window_position = undefined;
  813. this.revives_done = 0;
  814. }
  815.  
  816. PET.prototype.numRevivesDone = function() {
  817. return this.revives_done;
  818. }
  819.  
  820. PET.prototype.findWindow = function() {
  821. var screenshot = Browser.takeScreenshot();
  822. var corner_match = Vision.findMatch(screenshot, PET_WINDOW_CORNER_TPL, 0.99);
  823. if (corner_match.isValid()) {
  824. this.cached_window_position = corner_match.getRect().getTopLeft();
  825. return true;
  826. }
  827. return false;
  828. }
  829.  
  830. PET.prototype.getWindowRect = function(ignore_cache) {
  831. if (ignore_cache === true || this.cached_window_position === undefined) {
  832. this.findWindow();
  833. }
  834. if (this.cached_window_position === undefined) {
  835. return new Rect();
  836. }
  837. return new Rect(
  838. this.cached_window_position.getX(),
  839. this.cached_window_position.getY(),
  840. PET_WINDOW_SIZE.getWidth(),
  841. PET_WINDOW_SIZE.getHeight() // Including the dropdown with max amount of gears
  842. );
  843. }
  844.  
  845. PET.prototype.relativeToRealCoords = function(relative_coords) {
  846. return this.cached_window_position.pointAdded(relative_coords);
  847. }
  848.  
  849. PET.prototype.getWindowImage = function() {
  850. var screenshot = Browser.takeScreenshot();
  851. return screenshot.copy(this.getWindowRect());
  852. }
  853.  
  854. PET.prototype.hasFuel = function() {
  855. // The play/stop/repair button is only visible if there is enough fuel.
  856. // Those 3 buttons left part look the same, so if the find the left part
  857. // of one of the buttons, there is fuel.
  858. var window_image = this.getWindowImage();
  859. return Vision.findMatch(window_image, GENERIC_PET_BTN_TPL, 0.98).isValid();
  860. }
  861.  
  862. PET.prototype.isDestroyed = function() {
  863. var window_image = this.getWindowImage();
  864. return Vision.findMatch(window_image, PET_REPAIR_BTN_TPL, 0.98).isValid();
  865. }
  866.  
  867. PET.prototype.isActivated = function() {
  868. var window_image = this.getWindowImage();
  869. return Vision.findMatch(window_image, PET_STOP_BTN_TPL, 0.98).isValid();
  870. }
  871.  
  872. PET.prototype.clickButton = function(btn_tpl, min_score) {
  873. var window_image = this.getWindowImage();
  874. var btn_match = Vision.findMatch(window_image, btn_tpl, min_score);
  875.  
  876. if (btn_match.isValid()) {
  877. var in_window_pos = btn_match.getRect().getCenter();
  878. var real_coords = this.relativeToRealCoords(in_window_pos);
  879.  
  880. leftClickAndPreventHover(real_coords);
  881. Helper.sleep(1);
  882.  
  883. moveMouseToCenter();
  884. return true;
  885. }
  886.  
  887. Helper.debug("Button not found in PET.clickButton:", btn_match);
  888. return false;
  889. }
  890.  
  891. PET.prototype.revive = function() {
  892. var revived = this.clickButton(PET_REPAIR_BTN_TPL, 0.98);
  893. if (revived) {
  894. this.revives_done++;
  895. return true;
  896. }
  897. return false;
  898. }
  899.  
  900. PET.prototype.activate = function() {
  901. return this.clickButton(PET_PLAY_BTN_TPL, 0.98);
  902. }
  903.  
  904. PET.prototype.deactivate = function() {
  905. return this.clickButton(PET_STOP_BTN_TPL, 0.98);
  906. }
  907.  
  908. PET.prototype.openGearDropdown = function() {
  909. return this.clickButton(GEAR_DROPDOWN_BTN_TPL, 0.99);;
  910. }
  911.  
  912. PET.prototype.selectedGear = function() {
  913. var window_image = this.getWindowImage();
  914. if (Vision.findMatch(window_image, AUTOLOOTER_TPL, 0.99).isValid()) {
  915. return "autolooter";
  916. }
  917. else if (Vision.findMatch(window_image, PASSIVEMODE_TPL, 0.99).isValid()) {
  918. return "passivemode";
  919. }
  920. else if (Vision.findMatch(window_image, GUARDMODE_TPL, 0.99).isValid()) {
  921. return "guardmode";
  922. }
  923. else if (Vision.findMatch(window_image, COLLECTOR_TPL, 0.99).isValid()) {
  924. return "collector"; // Ressource-Collector
  925. }
  926. Helper.debug("Unknown gear selected");
  927. return "";
  928. }
  929.  
  930. PET.prototype.selectGear = function(gear_name) {
  931. var gear_entry_tpl = undefined;
  932.  
  933. switch (gear_name) {
  934. case "autolooter":
  935. gear_entry_tpl = AUTOLOOTER_ENTRY_TPL;
  936. break;
  937.  
  938. case "passivemode":
  939. gear_entry_tpl = PASSIVEMODE_ENTRY_TPL;
  940. break;
  941.  
  942. case "guardmode":
  943. gear_entry_tpl = GUARDMODE_ENTRY_TPL;
  944. break;
  945.  
  946. case "collector":
  947. gear_entry_tpl = COLLECTOR_ENTRY_TPL;
  948. break;
  949.  
  950. default:
  951. Helper.debug("Unknown gear name:", gear_name);
  952. return false;
  953. }
  954.  
  955. if (!this.openGearDropdown()) {
  956. Helper.log("Couldn't open the PETs gear dropdown.");
  957. return false;
  958. }
  959.  
  960. var window_image = this.getWindowImage();
  961. var gear_entry_match = Vision.findMatch(window_image, gear_entry_tpl, 0.99);
  962.  
  963. if (!gear_entry_match.isValid()) {
  964. Helper.log("Couldn't find the gear entry in the gear list.");
  965. return false;
  966. }
  967.  
  968. var in_window_pos = gear_entry_match.getRect().getCenter();
  969. var real_coords = this.relativeToRealCoords(in_window_pos);
  970.  
  971. leftClickAndPreventHover(real_coords);
  972. Helper.log("Gear selected:", gear_name);
  973.  
  974. Helper.sleep(1);
  975. return true;
  976. }
  977.  
  978. PET.prototype.manage = function() {
  979. if (this.isDestroyed()) {
  980. Helper.log("The PET is destroyed!");
  981.  
  982. if (this.numRevivesDone() >= CONFIG_MAX_PET_REPAIRS) {
  983. Helper.log("Max PET repairs already reached.");
  984. return;
  985. }
  986.  
  987. this.revive();
  988. Helper.log("Your PET has been repaired.");
  989. }
  990.  
  991. if (!this.isActivated()) {
  992. this.activate();
  993. Helper.log("PET activated!");
  994. }
  995.  
  996. if (this.selectedGear() != CONFIG_PET_GEAR_TO_USE) {
  997. Helper.log("The wrong PET gear is selected.");
  998.  
  999. if (this.selectGear(CONFIG_PET_GEAR_TO_USE)) {
  1000. Helper.log("Correct PET gear selected");
  1001. } else {
  1002. Helper.log("Couldn't select the PET gear:", CONFIG_PET_GEAR_TO_USE);
  1003. }
  1004. }
  1005. }
  1006.  
  1007. // +---------------------+
  1008. // | Bonusbox Collection |
  1009. // +---------------------+
  1010.  
  1011. var Collector = function(client, navi) {
  1012. this.client = client;
  1013. this.navi = navi; // Navigator
  1014. }
  1015.  
  1016. Collector.prototype.filterAnimationMatches = function(matches) {
  1017. var filtered_matches = [];
  1018. var browser_center = Browser.getRect().getCenter();
  1019.  
  1020. for (var i = 0; i < matches.length; i++) {
  1021. var match = matches[i];
  1022. var match_center = match.getRect().getCenter();
  1023.  
  1024. // Ignore centered matches below the ship
  1025. if (match_center.getY() > browser_center.getY() && match_center.getY() < browser_center.getY() + 100
  1026. && match_center.getX() > browser_center.getX() - 10 && match_center.getX() < browser_center.getX() + 10) {
  1027.  
  1028. Helper.debug("Loot match blow ship ignored:", match);
  1029. continue;
  1030. }
  1031.  
  1032. filtered_matches.push(match);
  1033. }
  1034.  
  1035. return filtered_matches;
  1036. }
  1037.  
  1038. Collector.prototype.findClosestLoot = function(prevent_animation_detection) {
  1039. var screenshot = Browser.takeScreenshot();
  1040. var isolated = screenshot.isolateColorRange(LOOT_MIN_HSV, LOOT_MAX_HSV);
  1041. var loot_matches = Vision.findBlobs(isolated, LOOT_BLOB_TPL);
  1042.  
  1043. if (prevent_animation_detection) {
  1044. loot_matches = this.filterAnimationMatches(loot_matches);
  1045. }
  1046.  
  1047. return getClosestMatch(loot_matches);
  1048. }
  1049.  
  1050. Collector.prototype.collectLoot = function() {
  1051. for (var loot_in_a_row = 0; loot_in_a_row < 20; loot_in_a_row++) {
  1052.  
  1053. if (loot_in_a_row == 0 && this.findClosestLoot().isValid()) {
  1054. this.client.haltShip(); // by triggering the logout process
  1055.  
  1056. // We assume that the logout will be canceled by the collector clicking loot
  1057. // or the navigator clicking on the minimap.
  1058. Helper.debug("Hope that the navigator cancels the logout-process.");
  1059. }
  1060.  
  1061. // After the first match try to prevent to find matches of loot we
  1062. // just collected but whichs animation is not finished yet.
  1063. // We could do a timeout instead, but that would slow us down.
  1064. var closest_loot = this.findClosestLoot(loot_in_a_row > 0);
  1065.  
  1066. if (!closest_loot.isValid()) {
  1067. // Nothing found and clicked this time, but maybe we found something before.
  1068. return loot_in_a_row > 0;
  1069. }
  1070.  
  1071. Helper.debug("Closest loot found:", closest_loot);
  1072.  
  1073. Browser.leftClick(closest_loot.getRect().getCenter());
  1074. Helper.msleep(OPTIMISTIC_ACCELERATION_TIME_IN_MS);
  1075.  
  1076. if (!this.navi.monitorQuickFlight(10 * 1000)) {
  1077. Helper.debug("It took longer than 10 seconds to reach the loot. Something might went wrong.");
  1078. return true;
  1079. }
  1080.  
  1081. Helper.log("Just collected something, looking for more...");
  1082. Helper.msleep(CONFIG_COLLECTOR_TIMEOUT_IN_MS);
  1083. }
  1084.  
  1085. Helper.debug("Finding more than 20 collectable in one spot is unlikely. We might be stuck somehow.");
  1086. return true;
  1087. }
  1088.  
  1089. // +----------------+
  1090. // | Task Scheduler |
  1091. // +----------------+
  1092.  
  1093. var Scheduler = function(client, minimap, pet, collector, navi) {
  1094. this.client = client;
  1095. this.minimap = minimap;
  1096. this.pet = pet;
  1097. this.collector = collector;
  1098. this.navi = navi;
  1099.  
  1100. this.just_collected_something = false;
  1101. this.script_stop_requested = false;
  1102.  
  1103. this.client_check_requested = true; // true => check on startup
  1104. this.pet_check_requested = true; // true => check on startup
  1105. this.map_check_requested = true; // true => check on startup
  1106.  
  1107. this.client_check_timer = new Timer();
  1108. this.pet_check_timer = new Timer();
  1109.  
  1110. this.client_check_timer.start();
  1111. this.pet_check_timer.start();
  1112. }
  1113.  
  1114. Scheduler.prototype.requestScriptStop = function() {
  1115. this.script_stop_requested = true;
  1116. }
  1117.  
  1118. Scheduler.prototype.requestClientCheck = function() {
  1119. this.client_check_requested = true;
  1120. }
  1121.  
  1122. Scheduler.prototype.requestPetCheck = function() {
  1123. this.pet_check_requested = true;
  1124. }
  1125.  
  1126. Scheduler.prototype.requestMapCheck = function() {
  1127. this.map_check_requested = true;
  1128. }
  1129.  
  1130. Scheduler.prototype.doneClientChecking = function() {
  1131. this.client_check_requested = false;
  1132. this.client_check_timer.restart();
  1133. }
  1134.  
  1135. Scheduler.prototype.donePetChecking = function() {
  1136. this.pet_check_requested = false;
  1137. this.pet_check_timer.restart();
  1138. }
  1139.  
  1140. Scheduler.prototype.doneMapChecking = function() {
  1141. this.map_check_requested = false;
  1142. }
  1143.  
  1144. Scheduler.prototype.itsTimeToCheckTheClient = function() {
  1145. var good_idea = !this.just_collected_something;
  1146. var necessary = this.client_check_requested || this.client_check_timer.hasExpired(GENERAL_CHECK_TIMEOUT_IN_MS);
  1147. return good_idea && necessary;
  1148. }
  1149.  
  1150. Scheduler.prototype.itsTimeToCheckThePET = function() {
  1151. var good_idea = !this.just_collected_something && CONFIG_USE_PET;
  1152. var necessary = this.pet_check_requested || this.pet_check_timer.hasExpired(CONFIG_PET_CHECK_TIMEOUT_IN_MS);
  1153. return good_idea && necessary;
  1154. }
  1155.  
  1156. Scheduler.prototype.itsTimeToCheckTheMap = function() {
  1157. var good_idea = !this.just_collected_something && CONFIG_USE_GLOBAL_NAV;
  1158. var necessary = this.map_check_requested;
  1159. return good_idea && necessary;
  1160. }
  1161.  
  1162. Scheduler.prototype.itsTimeToCollectLoot = function() {
  1163. var good_idea = CONFIG_COLLECT_LOOT;
  1164. var necessary = good_idea;
  1165. return good_idea && necessary;
  1166. }
  1167.  
  1168. Scheduler.prototype.itsTimeToMoveTheShip = function() {
  1169. var good_idea = this.just_collected_something || !this.navi.shipIsMoving();
  1170. var necessary = good_idea;
  1171. return good_idea && necessary;
  1172. }
  1173.  
  1174. Scheduler.prototype.checkTheConnection = function() {
  1175. if (!this.client.isDisconnected()) {
  1176. Helper.debug("Client is connected; check done.");
  1177. return;
  1178. }
  1179.  
  1180. Helper.log("Client disconnected.");
  1181.  
  1182. if (!CONFIG_AUTO_RECONNECT) {
  1183. Helper.log("Auto-reconnect disabled. Stopping the script...");
  1184. this.requestScriptStop();
  1185. return;
  1186. }
  1187.  
  1188. Helper.log("Trying to reconnect...");
  1189. this.client.reconnect();
  1190. }
  1191.  
  1192. Scheduler.prototype.checkTheShipStatus = function() {
  1193. if (!this.client.isDestroyed()) {
  1194. Helper.debug("Client is alive; check done.");
  1195. return;
  1196. }
  1197.  
  1198. Helper.log("Ship destroyed.");
  1199.  
  1200. if (!CONFIG_AUTO_SHIP_REPAIR) {
  1201. Helper.log("Auto-ship-repair disabled. Stopping the script...");
  1202. this.requestScriptStop();
  1203. return;
  1204. }
  1205.  
  1206. if (this.client.numRevivesDone() >= CONFIG_MAX_SHIP_REPAIRS) {
  1207. Helper.log("Max configured ship repairs reached. Stopping...");
  1208. this.requestScriptStop();
  1209. return;
  1210. }
  1211.  
  1212. Helper.log("Trying to repair the ship...");
  1213. this.client.revive(CONFIG_AUTO_SHIP_REPAIR_LOCATION);
  1214.  
  1215. Helper.sleep(2); // Wait for the revive animation to finish
  1216.  
  1217. this.requestMapCheck();
  1218. this.requestPetCheck();
  1219. }
  1220.  
  1221. Scheduler.prototype.checkTheCurrentMap = function() {
  1222. var dest_intern_mapname = convertExternToInternMapname(CONFIG_MAP);
  1223. this.navi.navigateToMap(dest_intern_mapname);
  1224.  
  1225. if (this.minimap.getInternMapname() === dest_intern_mapname) {
  1226. Helper.debug("We're on the correct map. Map check done.");
  1227. this.doneMapChecking();
  1228. } else {
  1229. Helper.debug("Map check not done.");
  1230. }
  1231. }
  1232.  
  1233. Scheduler.prototype.checkThePET = function() {
  1234. this.pet.manage();
  1235. this.donePetChecking();
  1236. }
  1237.  
  1238. Scheduler.prototype.checkForLoot = function() {
  1239. this.just_collected_something = this.collector.collectLoot();
  1240. Helper.msleep(CONFIG_COLLECTOR_TIMEOUT_IN_MS);
  1241. }
  1242.  
  1243. Scheduler.prototype.moveTheShip = function() {
  1244. Helper.log("Flying to a random location on the current map...");
  1245. Browser.leftClick(this.navi.getNextDestination());
  1246. }
  1247.  
  1248. Scheduler.prototype.runMainAlgorithm = function() {
  1249.  
  1250. while (!this.script_stop_requested) {
  1251.  
  1252. if (this.itsTimeToCheckTheClient()) {
  1253. Helper.debug("Time to check the client...");
  1254.  
  1255. // Reconnect, if disconnected and auto-reconnect is enabled
  1256. this.checkTheConnection();
  1257. if (this.script_stop_requested) return;
  1258.  
  1259. // Repair the ship, if destroyed and auto repair is enabled
  1260. this.checkTheShipStatus();
  1261. if (this.script_stop_requested) return;
  1262.  
  1263. // Reset check triggers
  1264. this.doneClientChecking();
  1265. }
  1266.  
  1267. if (this.itsTimeToCheckTheMap()) {
  1268. Helper.debug("Time to check whether we're on the correct map.");
  1269. this.checkTheCurrentMap();
  1270. }
  1271.  
  1272. if (this.itsTimeToCheckThePET()) {
  1273. Helper.debug("Time to check the PET...");
  1274. this.checkThePET();
  1275. }
  1276.  
  1277. if (this.itsTimeToCollectLoot()) {
  1278. Helper.debug("Time to check for loot...");
  1279. this.checkForLoot();
  1280. }
  1281.  
  1282. if (this.itsTimeToMoveTheShip()) {
  1283. Helper.debug("Time to move the ship...");
  1284. this.moveTheShip();
  1285. }
  1286.  
  1287. else if (!CONFIG_COLLECT_LOOT) {
  1288. // Sleep if we're moving and not looking for loot
  1289. Helper.sleep(2);
  1290. }
  1291. }
  1292.  
  1293. Helper.debug("The scheduler returned.");
  1294. }
  1295.  
  1296. // +---------------------------------------------------------------+
  1297. // | Main Method and Algorithm, this uses everything defined above |
  1298. // +---------------------------------------------------------------+
  1299.  
  1300. function main() {
  1301.  
  1302. // +--------------------------+
  1303. // | Inform and warn the user |
  1304. // +--------------------------+
  1305.  
  1306. Helper.log("### ! ! ! DO NOT RESIZE THE BROWSER WHILE RUNNING THIS SCRIPT ! ! ! ###");
  1307. Helper.log("Used script version:", SCRIPT_VERSION, "(You have to check for updates manually)");
  1308. var client = new Client();
  1309.  
  1310. // +------------------------------+
  1311. // | Prepare the client and login |
  1312. // +------------------------------+
  1313.  
  1314. if (CONFIG_COLLECT_LOOT) {
  1315. client.modifyResources2D();
  1316. Helper.log("REMEMBER: The loot collector currently only works in 2D mode.");
  1317. }
  1318.  
  1319. if (!client.isIngame()) {
  1320. if (CONFIG_USE_AUTO_LOGIN) {
  1321. Helper.log("Logging in automatically");
  1322. client.autoLogin(CONFIG_AUTO_LOGIN_USERNAME, CONFIG_AUTO_LOGIN_PASSWORD);
  1323. Helper.log("Logged in automatically.");
  1324. } else {
  1325. Helper.log("### ! ! ! Please login manually and start the game or configure the auto login feature ! ! ! ###");
  1326. return;
  1327. }
  1328. }
  1329.  
  1330. else if (CONFIG_COLLECT_LOOT) {
  1331. // The client is already ingame. Reload to make sure ressource modification works.
  1332. Helper.log("Reloading to make the loot collector work...");
  1333.  
  1334. Browser.reload();
  1335. Browser.finishLoading();
  1336.  
  1337. client.getIngame();
  1338. Helper.sleep(2);
  1339.  
  1340. Helper.log("Loot collector prepared.");
  1341. }
  1342.  
  1343. // +----------------------+
  1344. // | Bot components setup |
  1345. // +----------------------+
  1346.  
  1347. var minimap = new Minimap();
  1348. var pet = new PET();
  1349. var navi = new Navigator(minimap);
  1350. var collector = new Collector(client, navi);
  1351.  
  1352. // +------------------------------+
  1353. // | Find and measure the minimap |
  1354. // +------------------------------+
  1355.  
  1356. for (var tries = 1; tries <= 4; tries++) {
  1357. Helper.debug(tries, "time trying to find the minimap on startup.");
  1358.  
  1359. if (minimap.getLevel() !== undefined) {
  1360. Helper.debug("Minimap found on", tries, "try");
  1361. break;
  1362. }
  1363.  
  1364. Helper.log("Minimap not found", tries, "out of 4 tries. Trying again after 3 seconds");
  1365. Helper.sleep(3); // Try in 3 seconds again
  1366. }
  1367.  
  1368. if (minimap.getLevel() === undefined) {
  1369. Helper.log("FATAL! The bot was unable to find the ingame Minimap. Stopping now.")
  1370. return;
  1371. }
  1372.  
  1373. var outer_minimap = minimap.getOuterRect();
  1374. var inner_minimap = minimap.getInnerRect();
  1375.  
  1376. Helper.log("Minimap level:", minimap.getLevel());
  1377. Helper.log("Minimap position:", outer_minimap.getLeft(), outer_minimap.getTop());
  1378. Helper.log("Minimap size:", outer_minimap.getWidth(), outer_minimap.getHeight());
  1379.  
  1380. // +---------------------------------+
  1381. // | Find and measure the PET window |
  1382. // +---------------------------------+
  1383.  
  1384. if (CONFIG_USE_PET && !pet.findWindow()) {
  1385. Helper.log("FATAL! The bot was unable to find the PET window. Stopping now.");
  1386. return;
  1387. }
  1388.  
  1389. // +-------------------------------------+
  1390. // | Ready to bot, running bot algorithm |
  1391. // +-------------------------------------+
  1392.  
  1393. Helper.log("Starting to bot.");
  1394. var scheduler = new Scheduler(client, minimap, pet, collector, navi);
  1395. scheduler.runMainAlgorithm();
  1396. }
  1397.  
  1398. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement