// Build UI

var linebreak1 = document.createElement("BR");

var linebreak2 = document.createElement("BR");

var linebreak3 = document.createElement("BR");

var linebreak4 = document.createElement("BR");

var EMCConversionSpan = document.createElement('SPAN');

var EMCConversionTextBox = document.createElement('INPUT');

var EMCConversionText = document.createTextNode('Resource to convert through EMC: \xa0\xa0\xa0\xa0\xa0');

EMCConversionTextBox.setAttribute('type', 'text');

EMCConversionSpan.appendChild(EMCConversionText);

EMCConversionSpan.appendChild(EMCConversionTextBox);

EMCConversionSpan.appendChild(linebreak1);

document.getElementById('donatingTab').appendChild(EMCConversionSpan);

var minimumPlasmaSpan = document.createElement('SPAN');

var minimumPlasmaTextBox = document.createElement('INPUT');

var minimumPlasmaText = document.createTextNode('Minimum Plasma in Storage: \xa0\xa0\xa0\xa0\xa0');

minimumPlasmaTextBox.setAttribute('type', 'text');

minimumPlasmaSpan.appendChild(minimumPlasmaText);

minimumPlasmaSpan.appendChild(minimumPlasmaTextBox);

minimumPlasmaSpan.appendChild(linebreak2);

document.getElementById('donatingTab').appendChild(minimumPlasmaSpan);

var ringsAndSwarms = document.createElement('SPAN');

var ringsAndSwarmsCheckbox = document.createElement('INPUT');

var ringsAndSwarmsText = document.createTextNode('Build Rings and Swarms \xa0\xa0\xa0\xa0\xa0');

ringsAndSwarmsCheckbox.setAttribute('type', 'checkbox');

ringsAndSwarms.appendChild(ringsAndSwarmsText);

ringsAndSwarms.appendChild(ringsAndSwarmsCheckbox);

ringsAndSwarms.appendChild(linebreak3);

document.getElementById('donatingTab').appendChild(ringsAndSwarms);

var spherePush = document.createElement('SPAN');

var spherePushCheckbox = document.createElement('INPUT');

var spherePushText = document.createTextNode('Build Spheres \xa0\xa0\xa0\xa0\xa0 ');

spherePushCheckbox.setAttribute('type', 'checkbox');

spherePush.appendChild(spherePushText);

spherePush.appendChild(spherePushCheckbox);

spherePush.appendChild(linebreak4);

document.getElementById('donatingTab').appendChild(spherePush);

var achievementBuilder = document.createElement('SPAN');

var achievementBuilderCheckbox = document.createElement('INPUT');

var achievementBuilderText = document.createTextNode('Build Machines for Achievements \xa0\xa0\xa0\xa0\xa0');

achievementBuilderCheckbox.setAttribute('type', 'checkbox');

achievementBuilder.appendChild(achievementBuilderText);

achievementBuilder.appendChild(achievementBuilderCheckbox);

document.getElementById('donatingTab').appendChild(achievementBuilder);

EMCConversionTextBox.value = 'silicon';

minimumPlasmaTextBox.value = '10200';

// Create variables for building resource machines for higher Achievement Rank

var tierOneBuildingAmount = 1; // Tier 1 resource machines to build

var tierTwoBuildingAmount = 1;

var tierThreeBuildingAmount = 1;

var tierFourBuildingAmount = 1;

var tierFiveBuildingAmount = 500;

// Create variables for storage upgrades. className will be used.

var uraniumFull = document.getElementById('uranium');

var lavaFull = document.getElementById('lava');

var oilFull = document.getElementById('oil');

var metalFull = document.getElementById('metal');

var gemFull = document.getElementById('gem');

var charcoalFull = document.getElementById('charcoal');

var woodFull = document.getElementById('wood');

var siliconFull = document.getElementById('silicon');

var lunariteFull = document.getElementById('lunarite');

var methaneFull = document.getElementById('methane');

var titaniumFull = document.getElementById('titanium');

var goldFull = document.getElementById('gold');

var silverFull = document.getElementById('silver');

var hydrogenFull = document.getElementById('hydrogen');

var heliumFull = document.getElementById('helium');

var iceFull = document.getElementById('ice');

var meteoriteFull = document.getElementById('meteorite');

// Main loop

var loop = setInterval(function() {

upgradeStorage();

increaseResources();

convertEnergyAndPlasma();

buildDysonStuff();

engineBuilder();

}, 1) ;

// Increase resources

function increaseResources() {

var minimumPlasma = ~~minimumPlasmaTextBox.value; // Minimum Plasma level to be maintained at all times

if (document.getElementById('plasmaNav').className != 'sideTab hidden') {

gainResource('plasma'); // Game defined function

}

if (document.getElementById('uraniumNav').className != 'sideTab hidden') {

gainResource('uranium');

}

if (document.getElementById('lavaNav').className != 'sideTab hidden') {

gainResource('lava');

}

if (document.getElementById('oilNav').className != 'sideTab hidden') {

gainResource('oil');

}

gainResource('metal');

gainResource('gem');

gainResource('wood');

if (document.getElementById('siliconNav').className != 'sideTab hidden') {

gainResource('silicon');

}

if (document.getElementById('lunariteNav').className != 'sideTab hidden') {

gainResource('lunarite');

}

if (document.getElementById('methaneNav').className != 'sideTab hidden') {

gainResource('methane');

}

if (document.getElementById('titaniumNav').className != 'sideTab hidden') {

gainResource('titanium');

}

if (document.getElementById('goldNav').className != 'sideTab hidden') {

gainResource('gold');

}

if (document.getElementById('silverNav').className != 'sideTab hidden') {

gainResource('silver');

}

if (document.getElementById('hydrogenNav').className != 'sideTab hidden') {

gainResource('hydrogen');

}

if (document.getElementById('heliumNav').className != 'sideTab hidden') {

gainResource('helium');

}

if (document.getElementById('iceNav').className != 'sideTab hidden') {

gainResource('ice');

}

if (plasma > minimumPlasma && document.getElementById('meteoriteNav').className != 'sideTab hidden' ) {

gainResource('meteorite'); // Game definied function for clicking gain button

}

}

/* Upgrade storage. Full storage is indicated by green text

which is changed using class.

*/

function upgradeStorage() {

if (Game.tech.isPurchased('unlockStorage')) {

if (uraniumFull.className == 'green') {

upgradeUraniumStorage(); // Game defined function

}

if (lavaFull.className == 'green') {

upgradeLavaStorage();

}

if (oilFull.className == 'green') {

upgradeOilStorage();

}

if (metalFull.className == 'green') {

upgradeMetalStorage();

}

if (gemFull.className == 'green') {

upgradeGemStorage();

}

if (charcoalFull.className == 'green') {

upgradeCharcoalStorage();

}

if (woodFull.className == 'green') {

upgradeWoodStorage();

}

if (siliconFull.className == 'green') {

upgradeSiliconStorage();

}

if (lunariteFull.className == 'green') {

upgradeLunariteStorage();

}

if (methaneFull.className == 'green') {

upgradeMethaneStorage();

}

if (titaniumFull.className == 'green') {

upgradeTitaniumStorage();

}

if (goldFull.className == 'green') {

upgradeGoldStorage();

}

if (silverFull.className == 'green') {

upgradeSilverStorage();

}

if (hydrogenFull.className == 'green') {

upgradeHydrogenStorage();

}

if (heliumFull.className == 'green') {

upgradeHeliumStorage();

}

if (iceFull.className == 'green') {

upgradeIceStorage();

}

if (meteoriteFull.className == 'green') {

upgradeMeteoriteStorage();

}

}

}

function buildDysonStuff() {

if (spherePushCheckbox.checked == true && Game.tech.isPurchased('unlockDysonSphere') != true || sphere > Game.interstellar.stars.systemsConquered || swarm < 35 ) {

spherePushCheckbox.checked = false;

}

if (ringsAndSwarmsCheckbox.checked == true && spherePushCheckbox.checked != true) {

if (Game.tech.isPurchased('unlockDyson')) {

if (ring < 3 && titanium > 7000000 && gold > 2500000 && silicon > 4700000 && meteorite > 23000 && ice > 2500000) {

buildDysonTo(50); // Game defined function

buildRing(); // Game defined function

}

if (Game.tech.isPurchased('unlockDysonSphere')) {

if (ring >= 3 && energyps < 5000000 && titanium > 25000000 && gold > 900000 && silicon > 17000000 && meteorite > 85000 && ice > 9000000) {

buildDysonTo(100); // Game defined function

buildSwarm(); // Game defined function

} else if (ring >= 3 && titanium > 100000000 && gold > 3600000 && silicon > 51000000 && meteorite > 340000 && ice > 3600000) {

buildDysonTo(100); // Game defined function

buildSwarm(); // Game defined function

}

}

}

} else if (Game.tech.isPurchased('unlockDysonSphere') && spherePushCheckbox.checked == true) {

getDyson();

if (dyson == 250) {

buildSphere(); // Game defined function

spherePushCheckbox.checked = false;

}

}

}

function convertEnergyAndPlasma() {

var resourceToConvertEMC = EMCConversionTextBox.value; // Resource that you want to convert using the EMC

var offsetEnergyFlux; // Needed due to energy fluctuations near MaxEnergy

if (getMaxEnergy() < 1000000) {

offsetEnergyFlux = 1000;

} else {

offsetEnergyFlux = 100000;

}

if (Game.tech.isPurchased('unlockEmc')) {

if ((getMaxEnergy() - offsetEnergyFlux) < energy) {

convertEnergy(resourceToConvertEMC); // Game defined function

}

if ((getMaxPlasma() - 1000) < plasma) {

convertPlasma('meteorite'); // Game defined function

}

}

}

function engineBuilder() {

if (achievementBuilderCheckbox.checked == true){

ringsAndSwarmsCheckbox.checked = false;

spherePushCheckbox.checked = false;

getNebulous()

getOverexchange()

getCage()

getHarvester()

getWerewolf()

getPhilosopher()

getClub()

getInterCow()

getCloner()

getTardis()

getForest()

getMicroPollutor()

getDiamondChamber()

getMultiDrill()

getFossilator()

getCondensator()

getBath()

getPlanetNuke()

getMrFreeze();

getSkimmer();

getHindenburg();

getCannon();

getActuator();

getTitanDrill();

getVent();

getPlanetExcavator();

getDesert();

getInfuser();

getFryer();

getCarbyneDrill();

getQuantumDrill();

getOilRig();

getVeluptuator();

getRecycler();

getFreezer();

getCompressor();

getECell();

getBertha();

getDeathStar();

getPentaDrill();

getSpaceCow();

getMoonQuarry();

getAnnihilator();

getDeforester();

getKiln();

getDiamondDrill();

getGigaDrill();

getOilField();

getExtruder();

getEnricher();

getIceDrill();

getTanker();

getMagnet();

getSpaceLaser();

getDestroyer();

getLunariteDrill();

getSuctionExcavator();

getMoonDrill();

getScorcher();

getLaserCutter();

getFurnace();

getAdvancedDrill();

getHeavyDrill();

getPumpjack();

getExtractor();

getCubic();

getIcePick();

getDrone();

getCollector();

getScout();

getDroid();

getExplorer();

getVacuum();

getMoonWorker();

getBlowtorch();

getWoodcutter();

getWoodburner();

getGemMiner();

getMiner();

getCrucible();

getGrinder();

getPump();

getFusionReactor();

getMagmatic();

getNuclearStation();

getMethaneStation();

getSolarPanel();

getCharcoalEngine();

}