Last active
April 4, 2017 23:08
-
-
Save Chronial/5981aff4706e6d59f1f5ff743cfe4350 to your computer and use it in GitHub Desktop.
Rector Idle HeatExchange fixer UserScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Reactor Idle HeatExchange fixer | |
// @namespace https://gist.github.com/Chronial/5981aff4706e6d59f1f5ff743cfe4350 | |
// @version 0.1 | |
// @description Fix heat exchanger in reactor idle | |
// @author Chronial | |
// @match http://reactoridle.com/ | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var doIt = function(){ | |
requirejs(['calculators/Calculator'], function(calculator) { | |
calculator.prototype.calculateHeat = function() { | |
var e = this.reactor.getTiles() | |
, a = [] | |
, r = []; | |
for (let o in e) { | |
var n = e[o]; | |
if (n && n.getStrategy() && n.getStrategy().calculateHeat) | |
n.getStrategy().calculateHeat(); | |
if (n && n.getStrategy()){ | |
if (n.getStrategy().inputToOutputHeat) | |
a.push(n); | |
if (n.getStrategy().acceptInletHeat) | |
r.push(n); | |
} | |
} | |
for (let o in a) | |
a[o].getStrategy().inputToOutputHeat(r); | |
let components = getBalanceComponents(this.reactor); | |
components.forEach(balanceComponent); | |
}; | |
}); | |
function balanceComponent(component){ | |
var normals = component.filter(function(t){ | |
return t.getMeta().givesHeatOut; | |
}); | |
var batteries = component.filter(function(t){ | |
return t.getMeta().acceptsHeat && !t.getMeta().givesHeatOut; | |
}); | |
var batteryFactor = 100; | |
var divisor = normals.length + batteries.length*batteryFactor; | |
var totalHeat = 0; | |
component.forEach(function(t) { | |
totalHeat += t.getHeat(); | |
}); | |
var heatPerTile = totalHeat / divisor; | |
normals.forEach(function(t) { | |
t.setHeat(heatPerTile); | |
}); | |
batteries.forEach(function(t) { | |
t.setHeat(heatPerTile*batteryFactor); | |
}); | |
} | |
function getBalanceComponents(reactor){ | |
let tiles = reactor.getTiles(); | |
for (let xy in tiles) { | |
tiles[xy]._touched = false; | |
} | |
let components = []; | |
for (let xy in tiles) { | |
let n = tiles[xy]; | |
if (!n._touched && n.getStrategy() && n.getStrategy().balanceHeat) | |
components.push(getComponent(tiles[xy], true)); | |
} | |
return components; | |
} | |
function getComponent(tile, isBalancer){ | |
let neighbours = tile.findOffsetTiles([[-1, 0], [0, -1], [1, 0], [0, 1]]); | |
let component = [tile]; | |
tile._touched = true; | |
neighbours.forEach(function(n){ | |
if (n._touched) | |
return; | |
let nm = n.getMeta(); | |
let nHasHeat = nm && (nm.acceptsHeat || nm.givesHeatOut); | |
if (!nHasHeat) | |
return; | |
let ns = n.getStrategy(); | |
let nIsBalancer = ns && (ns.balanceHeat); | |
if ((isBalancer && nHasHeat) || nIsBalancer){ | |
component = component.concat(getComponent(n, nIsBalancer)); | |
} | |
}); | |
return component; | |
} | |
}; | |
var waiter = function(){ | |
if(window.Handlebars){ | |
doIt(); | |
} else { | |
window.setTimeout(waiter, 100); | |
} | |
}; | |
waiter(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment