Pardus Building Class

Building class used by Pardus Trader Autopilot

目前為 2023-06-10 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/468016/1202856/Pardus%20Building%20Class.js

  1. // ==UserScript==
  2. // @name Pardus Building Class
  3. // @namespace fear.math@gmail.com
  4. // @version 2.2.3
  5. // @description Building class used by Pardus Trader Autopilot
  6. // @author Math (Orion)
  7. // ==/UserScript==
  8.  
  9. class Building {
  10. static resourceNames = {
  11. '1': 'food', '2': 'energy', '3': 'water', '4': 'embryos', '5': 'ore', '6': 'metal', '7': 'electronics',
  12. '8': 'bots', '9': 'plastics', '10': 'hws', '11': 'medics', '12': 'gas', '13': 'chems', '14': 'gems',
  13. '15': 'liquor', '16': 'fuel', '17': 'ematter', '18': 'optics', '19': 'cells', '20': 'droids', '21': 'biowaste',
  14. '22': 'leech', '23': 'clod', '27': 'bwps', '28': 'tissue', '29': 'stims', '50': 'slaves', '51': 'drugs',
  15. };
  16.  
  17. // Credit to Taised (tiziano.m@infinito.it) who hosts the original version of this data at
  18. // 'http://www.fantamondi.it/pardus/common/building_data.js
  19. static buildingData = [
  20. { type: "Energy Well", tier: 0, resources: { "food": -1, "water": -1, "energy": 6 } },
  21. { type: "Nebula Plant", tier: 0, resources: { "food": -2, "water": -2, "ematter": -3, "energy": 35, "gas": 4 } },
  22. { type: "Space Farm", tier: 0, resources: { "energy": -4, "embryos": -5, "food": 8, "water": 2, "biowaste": 1 } },
  23.  
  24. { type: "Asteroid Mine", tier: 1, resources: { "food": -1, "energy": -1, "water": -1, "ore": 9, "gems": 2 } },
  25. { type: "Chemical Laboratory", tier: 1, resources: { "food": -1, "energy": -3, "water": -1, "chems": 9 } },
  26. { type: "Gas Collector", tier: 1, resources: { "food": -2, "energy": -2, "water": -2, "gas": 20 } },
  27. { type: "Radiation Collector", tier: 1, resources: { "food": -1, "energy": -3, "water": -1, "cells": 6 } },
  28.  
  29. { type: "Alliance Command Station", tier: 2, resources: { "energy": -6, "cells": -2 } },
  30. { type: "Brewery", tier: 2, resources: { "food": -2, "energy": -2, "water": -2, "chems": -4, "liquor": 4 } },
  31. { type: "Clod Generator", tier: 2, resources: { "biowaste": -18, "energy": -4, "chems": -4, "clod": 5 } },
  32. { type: "Fuel Collector", tier: 2, resources: { "energy": -4, "chems": -1, "fuel": 30 } },
  33. { type: "Medical Laboratory", tier: 2, resources: { "food": -2, "energy": -2, "water": -2, "gas": -7, "medics": 4 } },
  34. { type: "Military Outpost", tier: 2, resources: { "energy": -5, "fuel": -5 } },
  35. { type: "Optics Research Center", tier: 2, resources: { "food": -1, "energy": -3, "water": -1, "gems": -2, "optics": 10 } },
  36. { type: "Plastics Facility", tier: 2, resources: { "food": -2, "energy": -2, "water": -2, "chems": -3, "gas": -3, "plastics": 6 } },
  37. { type: "Recyclotron", tier: 2, resources: { "biowaste": -5, "energy": -3, "chems": -1, "food": 7, "water": 5 } },
  38. { type: "Smelting Facility", tier: 2, resources: { "food": -2, "energy": -2, "water": -2, "ore": -6, "metal": 12 } },
  39.  
  40. { type: "Electronics Facility", tier: 3, resources: { "food": -1, "energy": -4, "water": -1, "plastics": -2, "metal": -3, "electronics": 6 } },
  41. { type: "Leech Nursery", tier: 3, resources: { "food": -2, "energy": -6, "water": -10, "cells": -6, "clod": -40, "leech": 1, "biowaste": 3 } },
  42. { type: "Neural Laboratory", tier: 3, resources: { "food": -2, "water": -2, "energy": -2, "embryos": -12, "medics": -2, "tissue": 16 } },
  43. { type: "Slave Camp", tier: 3, resources: { "food": -3, "energy": -1, "water": -3, "liquor": -2, "medics": -2, "slaves": 3 } },
  44.  
  45. { type: "Battleweapons Factory", tier: 4, resources: { "food": -1, "energy": -2, "water": -1, "metal": -3, "optics": -4, "electronics": -3, "bwps": 2 } },
  46. { type: "Dark Dome", tier: 4, resources: { "slaves": -3, "energy": -1, "bodyparts": 12, "biowaste": 4 } },
  47. // { type: "Dark Dome (TSS)", tier: 4, resources: { "slaves": -2, "energy": -1, "bodyparts": 12, "biowaste": 4 } },
  48. { type: "Drug Station", tier: 4, resources: { "food": -3, "energy": -1, "water": -3, "slaves": -4, "ematter": -3, "drugs": 1 } },
  49. // { type: "Drug Station (TSS)", tier: 4, resources: { "food": -3, "energy": -1, "water": -3, "slaves": -3, "ematter": -3, "drugs": 1 } },
  50. { type: "Handweapons Factory", tier: 4, resources: { "food": -1, "energy": -2, "water": -1, "plastics": -3, "optics": -3, "electronics": -3, "hws": 2 } },
  51. { type: "Robots Factory", tier: 4, resources: { "food": -2, "energy": -2, "water": -2, "metal": -1, "optics": -2, "electronics": -4, "bots": 3 } },
  52. { type: "Stim Chip Mill", tier: 4, resources: { "food": -3, "water": -3, "ematter": -2, "tissue": -44, "electronics": -2, "stims": 2 } },
  53.  
  54. { type: "Droid Assembly Complex", tier: 5, resources: {"food": -1, "energy": -3, "water": -1, "cells": -3, "bots": -2, "droids": 1} }
  55. ];
  56.  
  57. constructor(type, sector, x, y, level = 8, lastStock = {}, lastViewed = null, balance = null) {
  58. this.type = type;
  59. this.sector = sector;
  60. this.x = x;
  61. this.y = y;
  62. this.level = level;
  63. this._balance = balance;
  64. this.lastStock = lastStock;
  65. this.lastViewed = lastViewed;
  66. }
  67.  
  68. get coords() {
  69. return `[${this.x},${this.y}]`;
  70. }
  71.  
  72. // If we know the balance, return it, otherwise guess the production
  73. get balance() {
  74. return this._balance || this.calculateProduction();
  75. }
  76.  
  77. // Calculate the guessed current stock based on lastViewed time and building characteristics
  78. get stock() {
  79. const ticksPassed = this.calculateTicksPassed();
  80. const production = this.balance;
  81. const stock = this.lastStock;
  82.  
  83. // Calculate the guessed stock for each commodity
  84. for (const commodity in production) {
  85. if (production.hasOwnProperty(commodity)) {
  86. const lastStock = stock[commodity] || 0;
  87. const change = production[commodity] * ticksPassed;
  88. stock[commodity] = lastStock + change;
  89. }
  90. }
  91.  
  92. return stock;
  93. }
  94.  
  95. // Setter for level
  96. setLevel(level) {
  97. this.level = level;
  98. }
  99.  
  100. // Called on the building trade screen. Updates balance, lastStock, and lastViewed
  101. update() {
  102. this.updateBalance();
  103. this.updateLastStock();
  104. this.updateLastViewed();
  105. }
  106.  
  107. updateBalance() {
  108. const balanceValues = Building.extractBalanceValues();
  109.  
  110. const balance = {};
  111.  
  112. // Add res_production to the balance
  113. for (const key in amount) {
  114. const resourceName = Building.resourceNames[key];
  115. balance[resourceName] = balanceValues.shift();
  116. }
  117.  
  118. this._balance = balance;
  119. }
  120.  
  121. updateLastStock() {
  122. const stock = {};
  123.  
  124. for (const key in amount) {
  125. const resourceName = Building.resourceNames[key];
  126. const balance = this.balance[resourceName];
  127.  
  128. // Production's stock level is the amount above the min
  129. if (balance > 0) {
  130. stock[resourceName] = amount[key] - amount_min[key];
  131. }
  132.  
  133. // Upkeep's stock level is the amount below the max
  134. if (balance < 0) {
  135. stock[resourceName] = amount_max[key] - amount[key];
  136. }
  137. }
  138.  
  139. this.lastStock = stock;
  140. }
  141.  
  142. updateLastViewed() {
  143. this.lastViewed = Date.now();
  144. }
  145.  
  146. // Calculate the number of building ticks that have passed since lastViewed
  147. calculateTicksPassed() {
  148. const millisecondsPerTick = 6 * 60 * 60 * 1000; // Milliseconds per tick
  149.  
  150. const now = new Date();
  151. const lastViewed = new Date(this.lastViewed);
  152.  
  153. // Adjust the last viewed time forward to the nearest tick time (1:25 GMT and every 6 hours)
  154. lastViewed.setUTCMinutes(Math.round(Math.ceil((lastViewed.getUTCMinutes() - 25) / 60) * 60 + 25));
  155. lastViewed.setUTCHours(Math.round(Math.ceil((lastViewed.getUTCHours() - 1) / 6) * 6 + 1));
  156.  
  157. // Calculate the time difference in milliseconds
  158. const timeDiff = now - lastViewed;
  159.  
  160. // Calculate the number of ticks passed
  161. const ticksPassed = Math.ceil(timeDiff / millisecondsPerTick);
  162.  
  163. return ticksPassed;
  164. }
  165.  
  166. // Calculate the production and consumption rates based on the building type and level
  167. calculateProduction() {
  168. const buildingData = Building.buildingData.find(building => building.type === this.type);
  169.  
  170. if (!buildingData) {
  171. console.log(`Building data not found for building type: ${this.type}`);
  172. return;
  173. }
  174.  
  175. const { resources } = buildingData;
  176. const production = {};
  177.  
  178. for (const resource in resources) {
  179. const value = resources[resource];
  180.  
  181. if (value > 0) {
  182. production[resource] = Math.round(value * (1 + 0.5 * (this.level - 1)));
  183. } else if (value < 0) {
  184. production[resource] = Math.round(value * (1 + 0.4 * (this.level - 1)));
  185. }
  186. }
  187.  
  188. return production;
  189. }
  190.  
  191. // Custom serialization method
  192. toJSON() {
  193. return {
  194. type: this.type,
  195. sector: this.sector,
  196. x: this.x,
  197. y: this.y,
  198. level: this.level,
  199. lastStock: this.lastStock,
  200. lastViewed: this.lastViewed,
  201. _balance: this._balance,
  202. };
  203. }
  204.  
  205. // Custom deserialization method
  206. static fromJSON(json) {
  207. const { type, sector, x, y, level, lastStock, lastViewed, _balance } = json;
  208. const building = new Building(type, sector, x, y, level, lastStock, lastViewed, _balance);
  209. return building;
  210. }
  211.  
  212. static extractBalanceValues() {
  213. var balanceValues = [];
  214. var tableRows = document.querySelectorAll('#building_trade tr[id^="baserow"]');
  215.  
  216. tableRows.forEach(function(row) {
  217. var balanceCell = row.querySelector('td:nth-child(4)');
  218. var balanceValue = balanceCell.textContent.trim();
  219. balanceValues.push(balanceValue);
  220. });
  221.  
  222. return balanceValues.map(Number);
  223. }
  224. }