Kitten Extrapolation Prototype

A script for outputting information about kitten survival (Other features may come later)

当前为 2019-05-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Kitten Extrapolation Prototype
  3. // @namespace https://greasyfork.org/en/scripts/10234-kitten-extrapolation
  4. // @version 0.7.0
  5. // @description A script for outputting information about kitten survival (Other features may come later)
  6. // @author Yuriy
  7. // @match http://bloodrizer.ru/games/kittens/
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. var KE_seasons = {
  12. "Spring": 1.5,
  13. "Summer": 1,
  14. "Autumn": 1,
  15. "Winter": 0.25
  16. };
  17.  
  18. var KE_weathers = {
  19. "Warm": 0.15,
  20. "Norm": 0.0,
  21. "Cold": -0.15
  22. };
  23.  
  24. var KE_seasons_reverse_Lookup = {
  25. 0 : "Spring",
  26. 1 : "Summer",
  27. 2 : "Autumn",
  28. 3 : "Winter"
  29. };
  30.  
  31. function KE_calcTitaniumChance()
  32. {
  33. if(0.35*gamePage.resPool.get("ship").value+15>100){
  34. return 100;
  35. }else{
  36. return 0.35*gamePage.resPool.get("ship").value+15;
  37. }
  38. }
  39.  
  40. var KE_seasonal_trading = {
  41. "Lizards" : {
  42. "wood" : {
  43. "Spring": "<font color=red>↓↓</font>",
  44. "Summer": "<font color=limegreen>↑↑</font>",
  45. "Autumn": "<font color=limegreen>↑</font>",
  46. "Winter": "<font color=red>↓</font>",
  47. "Best": "Summer",
  48. "Chance": 100
  49. }
  50. },
  51. "Sharks" : {
  52. "catnip" : {
  53. "Spring": "<font color=limegreen>↑</font>",
  54. "Summer": "<font color=red>↓↓</font>",
  55. "Autumn": "<font color=red>↓</font>",
  56. "Winter": "<font color=limegreen>↑↑</font>",
  57. "Best": "Winter",
  58. "Chance": 100
  59. }
  60. },
  61. "Griffins" : {
  62. "iron" : {
  63. "Spring": "<font color=red>↓↓</font>",
  64. "Summer": "<font color=limegreen>↑</font>",
  65. "Autumn": "<font color=limegreen>↑↑</font>",
  66. "Winter": "<font color=red>↓</font>",
  67. "Best": "Autumn",
  68. "Chance": 100
  69. }
  70. },
  71. "Nagas" : {
  72. "minerals" : {
  73. "Spring": "<font color=limegreen>↑↑</font>",
  74. "Summer": "<font color=limegreen>↑</font>",
  75. "Autumn": "<font color=red>↓↓</font>",
  76. "Winter": "<font color=red>↓</font>",
  77. "Best": "Spring",
  78. "Chance": 100
  79. }
  80. },
  81. "Zebras" : {
  82. "iron" : {
  83. "Spring": "<font color=limegreen>↑</font>",
  84. "Summer": "<font color=limegreen>↑↑</font>",
  85. "Autumn": "<font color=red>↓</font>",
  86. "Winter": "<font color=red>↓↓</font>",
  87. "Best": "Summer",
  88. "Chance": 100
  89. },
  90. "plate" : {
  91. "Spring": "<font color=yellow>=</font>",
  92. "Summer": "<font color=red>↓↓</font>",
  93. "Autumn": "<font color=yellow>=</font>",
  94. "Winter": "<font color=limegreen>↑↑</font>",
  95. "Best": "Winter",
  96. "Chance": 65
  97. },
  98. "titanium" : {
  99. "Chance": KE_calcTitaniumChance
  100. }
  101. },
  102. "Spiders" : {
  103. "coal" : {
  104. "Spring": "<font color=red>↓</font>",
  105. "Summer": "<font color=limegreen>↑</font>",
  106. "Autumn": "<font color=limegreen>↑↑</font>",
  107. "Winter": "<font color=red>↓↓</font>",
  108. "Best": "Autumn",
  109. "Chance": 100
  110. }
  111. },
  112. "Dragons" : {
  113. "uranium" : {
  114. // "Spring": "<font color=red>↓</font>",
  115. // "Summer": "<font color=limegreen>↑</font>",
  116. // "Autumn": "<font color=limegreen>↑↑</font>",
  117. // "Winter": "<font color=red>↓↓</font>",
  118. // "Best": "Autumn",
  119. "Chance": 95
  120. }
  121. }
  122. };
  123.  
  124. function KE_generate_food_table_cell(tag="", contents = "", colspan = 1){
  125. var cell = document.createElement("td");
  126. if(tag != ""){
  127. cell.setAttribute("id",tag);
  128. }
  129. cell.style.textAlign="text-align:center";
  130. cell.append(contents)
  131. if(colspan != 1){
  132. cell.setAttribute("colspan","4")
  133. }
  134. return cell;
  135. }
  136.  
  137. function KE_generate_food_table_line(line_name, line_tag) {
  138. var line = document.createElement("tr");
  139. var cell = document.createElement("td");
  140. cell.style.textAlign="text-align:center";
  141. cell.append(line_name);
  142. line.append(cell);
  143. line.append(KE_generate_food_table_cell(line_tag+'Sp'));
  144. line.append(KE_generate_food_table_cell(line_tag+'Su'));
  145. line.append(KE_generate_food_table_cell(line_tag+'A'));
  146. line.append(KE_generate_food_table_cell(line_tag+'W'));
  147. return line;
  148. }
  149.  
  150. function KE_generate_food_table() {
  151. //Creating the enclosing div for the table
  152. var enclosing_div = document.createElement("div");
  153. enclosing_div.setAttribute("width","340px");
  154. //Making the table itself
  155. var table = document.createElement("table");
  156. table.setAttribute("id","food_table_season");
  157. table.setAttribute("table-layout","fixed");
  158. //create/define the columns in the tables
  159. var column = document.createElement("col");
  160. column.setAttribute("width",60);
  161. table.append(column);
  162. for (var i = 0; i < 4; i++) {
  163. var column2 = document.createElement("col");
  164. column2.setAttribute("width",70);
  165. table.append(column2);
  166. }
  167. //create the top line of the table
  168. var topline = document.createElement("tr");
  169. topline.append(KE_generate_food_table_cell("cycle_warning"));
  170. topline.append(KE_generate_food_table_cell("","Food during seasons (/season)",4));
  171. table.append(topline);
  172. //Create second line of table
  173. var line2 = document.createElement("tr");
  174. line2.append(KE_generate_food_table_cell("","Weather"));
  175. line2.append(KE_generate_food_table_cell("KE_Spring","Spring"));
  176. line2.append(KE_generate_food_table_cell("KE_Summer","Summer"));
  177. line2.append(KE_generate_food_table_cell("KE_Autumn","Autumn"));
  178. line2.append(KE_generate_food_table_cell("KE_Winter","Winter"));
  179. table.append(line2);
  180. //Create the table's main cells/liens
  181. table.append(KE_generate_food_table_line('Warm', 'WS'));
  182. table.append(KE_generate_food_table_line('Norm', 'NS'));
  183. table.append(KE_generate_food_table_line('Cold', 'CS'));
  184. //append the table to the enclosing div
  185. enclosing_div.append(table);
  186. //Yearly food production calculation
  187. var yearly_food_produced = document.createElement("p");
  188. yearly_food_produced.setAttribute("id","yearly_food_produced");
  189. yearly_food_produced.style.marginBlockStart="2px";
  190. yearly_food_produced.style.marginBlockEnd="2px";
  191. enclosing_div.append(yearly_food_produced);
  192. return enclosing_div;
  193. }
  194.  
  195. //Calculates the reasource production for a season other then the current one.
  196. function KE_calcResourcePerTick_NCW(resName, season, weather)
  197. {
  198. if(resName == "catnip"){
  199. //Get data for the current weather
  200. var realWeatherMod = gamePage.calendar.getWeatherMod();
  201. //invert it
  202. var realWeatherModInverse = 0-realWeatherMod;
  203. //get the effective season+weather
  204. var mockSeasonWeatherMod = KE_seasons[season]+KE_weathers[weather];
  205. //Apply the inveser of the real weather to it to counter that it is going to be applied in the function
  206. mockSeasonWeatherMod = mockSeasonWeatherMod + realWeatherModInverse;
  207. return (gamePage.calcResourcePerTick(resName,{"modifiers" : {"catnip" : mockSeasonWeatherMod}})-gamePage.getResourcePerTickConvertion(resName));
  208. } else{
  209. return gamePage.calcResourcePerTick(resName)-gamePage.getResourcePerTickConvertion(resName);
  210. }
  211. }
  212.  
  213. function KE_update_food_table_cell(lable, season, weather)
  214. {
  215. var element_to_update = document.getElementById(lable);
  216. //Clear labeled element
  217. element_to_update.innerHTML = ""
  218. var updated_text = document.createElement("font");
  219. //Bold the current season/weather combo
  220. if(season == KE_seasons_reverse_Lookup[gamePage.calendar.season] && ((weather=="Norm" && gamePage.calendar.weather == null) || weather.toLowerCase() == gamePage.calendar.weather)){
  221. updated_text.setAttribute("class","msg type_date");
  222. updated_text.style.fontWeight = "bold";
  223. updated_text.style.borderBottomWidth = "0px";
  224. updated_text.style.fontSize = "14px";
  225. }
  226. //Get updated value
  227. updated_text.append(gamePage.getDisplayValueExt(KE_calcResourcePerTick_NCW("catnip",season,weather)*1000,true));
  228. //Close out font
  229. element_to_update.append(updated_text);
  230. return true;
  231. }
  232.  
  233. function KE_update_trade_screen()
  234. {
  235.  
  236. ///////////////////////////////////////////////////
  237. //Adding trade calculations, purely experimental //
  238. ///////////////////////////////////////////////////
  239. //Panel container is the class of all trade containers
  240. var trade_containers = document.getElementsByClassName("panelContainer")
  241. //Cycle through all trade containers
  242. for (var i = 0; i < trade_containers.length; i++) {
  243. //for every container
  244. if(typeof(trade_containers[i]) === 'object' && trade_containers[i].getElementsByClassName("title").length > 0)
  245. {
  246. try
  247. {
  248. var trade_partner = trade_containers[i].getElementsByClassName("title")[0].childNodes[0].nodeValue.trim()
  249. //////////////////////////////////////
  250. //Chance of success/bonus for trades//
  251. //////////////////////////////////////
  252. for (var j = 0; j<gamePage.diplomacy.races.length; j++){
  253. if(gamePage.diplomacy.races[j]["title"] == trade_partner){
  254. if(gamePage.diplomacy.races[j]["attitude"] != "neutral")
  255. {
  256. var standingRatio = gamePage.getEffect("standingRatio");
  257. standingRatio = standingRatio ? standingRatio : 0;
  258.  
  259. if (gamePage.prestige.getPerk("diplomacy").researched){
  260. standingRatio += 10;
  261. }
  262.  
  263. if(gamePage.diplomacy.races[j]["attitude"] == "friendly")
  264. {
  265. standingRatio = standingRatio/2;
  266. }
  267.  
  268. standingRatio += gamePage.diplomacy.races[j]["standing"]*100
  269. if (standingRatio > 100){
  270. standingRatio = 100;
  271. }
  272.  
  273. var append_string = "";
  274.  
  275. if(gamePage.diplomacy.races[j]["attitude"] == "friendly")
  276. {
  277. append_string = "(+"+gamePage.getDisplayValueExt(standingRatio)+"%)";
  278. }
  279.  
  280. if(gamePage.diplomacy.races[j]["attitude"] == "hostile")
  281. {
  282. append_string = "(-"+gamePage.getDisplayValueExt(100-standingRatio)+"%)";
  283. }
  284.  
  285. //Add standing info to the trade window.
  286. if(trade_containers[i].getElementsByClassName("title")[0].getElementsByClassName("attitude")[0].getElementsByClassName("trade-seasonal-appeal").length == 0){
  287. //If the block does not exist yet
  288. var appendnode = document.createElement("span");
  289. appendnode.setAttribute("class", "trade-seasonal-appeal");
  290. trade_containers[i].getElementsByClassName("title")[0].getElementsByClassName("attitude")[0].appendChild(appendnode);
  291. }
  292.  
  293. trade_containers[i].getElementsByClassName("title")[0].getElementsByClassName("attitude")[0].getElementsByClassName("trade-seasonal-appeal")[0].innerHTML = append_string;
  294. }
  295.  
  296. break;
  297. }
  298. }
  299.  
  300. //////////////////////////////////////////////
  301. //Colored arrows for race season prefrences.//
  302. //Should probobly be redone using the game's//
  303. //internal code //
  304. //////////////////////////////////////////////
  305. //Check if the trade partner is in seasonal_trading
  306. if(Object.keys(KE_seasonal_trading).indexOf(trade_partner) != -1){
  307. //get the list of all reasources for trade
  308. var reasource_containers = trade_containers[i].getElementsByClassName("trade-race")[0].getElementsByClassName("left")[0].children
  309. //cycle through them
  310. for (var j = 0; j < reasource_containers.length; j++) {
  311. //if this is a 'sell' type container (as opposed to the 'buy' containers)
  312. if(reasource_containers[j].getElementsByClassName("sells").length > 0)
  313. {
  314. //find out if this is a reasource that the race being looked at has a variable deal on based on seasons
  315. var trade_reasource = reasource_containers[j].childNodes[1].nodeValue.trim();
  316. if(Object.keys(KE_seasonal_trading[trade_partner]).indexOf(trade_reasource) != -1)
  317. {
  318. //Add the addjustment appearence to it.
  319. if(reasource_containers[j].getElementsByClassName("trade-seasonal").length == 0){
  320. //If the block does not exist yet
  321. var appendnode = document.createElement("span");
  322. appendnode.setAttribute("class", "trade-seasonal");
  323. reasource_containers[j].appendChild(appendnode);
  324. }
  325. var append_string = "";
  326. if(KE_seasonal_trading[trade_partner][trade_reasource]["Chance"]!=100){
  327. var string_add = document.createElement("span")
  328. if(typeof(KE_seasonal_trading[trade_partner][trade_reasource]["Chance"]) != 'function'){
  329. string_add.innerHTML = gamePage.getDisplayValueExt(KE_seasonal_trading[trade_partner][trade_reasource]["Chance"]) + "%";
  330. }else{
  331. string_add.innerHTML = gamePage.getDisplayValueExt(KE_seasonal_trading[trade_partner][trade_reasource]["Chance"]()) + "%";
  332. }
  333. string_add.setAttribute("class", "ammount");
  334. append_string += string_add.outerHTML;
  335. }
  336. if(KE_seasonal_trading[trade_partner][trade_reasource][KE_seasons_reverse_Lookup[gamePage.calendar.season]])
  337. {
  338. append_string += KE_seasonal_trading[trade_partner][trade_reasource][KE_seasons_reverse_Lookup[gamePage.calendar.season]];
  339. if(KE_seasons_reverse_Lookup[gamePage.calendar.season] != KE_seasonal_trading[trade_partner][trade_reasource]["Best"]){
  340. append_string += " (+" + KE_seasonal_trading[trade_partner][trade_reasource]["Best"] + ")";
  341. }
  342. }
  343. reasource_containers[j].getElementsByClassName("trade-seasonal")[0].innerHTML = append_string;
  344. }
  345. }
  346. }
  347. }
  348. }
  349. catch(err) {
  350. console.error("Error in adding trade container. ",err.message);
  351. }
  352. }else{
  353. console.log(typeof(trade_containers[i]), trade_containers[i]);
  354. }
  355.  
  356.  
  357. }
  358. }
  359.  
  360. function KE_update(){
  361. //Update all the food cells
  362. KE_update_food_table_cell("WSSp", "Spring", "Warm");
  363. KE_update_food_table_cell("WSSu", "Summer", "Warm");
  364. KE_update_food_table_cell("WSA", "Autumn", "Warm");
  365. KE_update_food_table_cell("WSW", "Winter", "Warm");
  366. KE_update_food_table_cell("NSSp", "Spring", "Norm");
  367. KE_update_food_table_cell("NSSu", "Summer", "Norm");
  368. KE_update_food_table_cell("NSA", "Autumn", "Norm");
  369. KE_update_food_table_cell("NSW", "Winter", "Norm");
  370. KE_update_food_table_cell("CSSp", "Spring", "Cold");
  371. KE_update_food_table_cell("CSSu", "Summer", "Cold");
  372. KE_update_food_table_cell("CSA", "Autumn", "Cold");
  373. KE_update_food_table_cell("CSW", "Winter", "Cold");
  374.  
  375. //Yearly production
  376. document.getElementById('yearly_food_produced').innerHTML = "Yearly food balance (avg): " + gamePage.getDisplayValueExt((KE_calcResourcePerTick_NCW("catnip", "Spring", "Norm")*1000) + (KE_calcResourcePerTick_NCW("catnip", "Summer", "Norm")*1000) + (KE_calcResourcePerTick_NCW("catnip", "Autumn", "Norm")*1000) + (KE_calcResourcePerTick_NCW("catnip", "Winter", "Norm")*1000), true);
  377.  
  378. var cycle = gamePage.calendar.cycles[gamePage.calendar.cycle];
  379. var element_to_update;
  380. element_to_update = document.getElementById("cycle_warning");
  381. if(cycle["name"]=="piscine"){
  382. element_to_update.innerHTML = '<font color="red">↑Piscine</font>'
  383. }else{
  384. element_to_update.innerHTML = ""
  385. }
  386.  
  387. //
  388.  
  389. //Trading
  390. KE_update_trade_screen();
  391.  
  392. //console.log(trade_reasource, Object.keys(seasonal_trading[trade_partner]).indexOf(trade_reasource));
  393.  
  394. return true;
  395. }
  396.  
  397. function KE_initiate_script() {
  398. var data_out = document.createElement('div');
  399. data_out.id = 'kitten_extrapolation_container';
  400. data_out.style.width = '100%';
  401. data_out.style.bottom = '0px';
  402. data_out.style.verticalAlign = 'bottom';
  403. data_out.innerHTML = "";
  404. data_out.append(KE_generate_food_table());
  405. var right_col = document.getElementById('rightColumn')
  406. right_col.style.width = '360px';
  407. var before_child = document.getElementsByClassName("right-tab-header")[0];
  408. right_col.insertBefore(data_out, before_child);
  409. setInterval(KE_update, 500);
  410. return true;
  411. }
  412.  
  413. function KE_initiate() {
  414. if (typeof gamePage == "object") {
  415. if (!document.getElementById('kitten_extrapolation_container')) {
  416. KE_initiate_script();
  417. }
  418. } else if(typeof gamePage == "undefined") {
  419. setTimeout(function(){
  420. KE_initiate();
  421. }, 100);
  422. }else{
  423. window.alert("Error E1 occured in Kitten Extrapolation! \nThis is most likely to occure if the game's code has been radically changed and Kitten Extrapolation needs updating or if the script is run on something other then the Kittens game.");
  424. }
  425. }
  426.  
  427. KE_initiate();
  428.  
  429. //Function for trimming strings.
  430. //Credit: David Andres (https://stackoverflow.com/questions/1418050/string-strip-for-javascript)
  431. if(typeof(String.prototype.trim) === "undefined")
  432. {
  433. String.prototype.trim = function()
  434. {
  435. return String(this).replace(/^\s+|\s+$/g, '');
  436. };
  437. }