Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Mine Guardian
- // @namespace http://scholtek.com
- // @version 0.4
- // @description Various automation options for Mine Defense.
- // @author Innominate
- // @match http://scholtek.com/minedefense
- // @run-at document-end
- // @grant none
- // @downloadURL http://pastebin.com/raw.php?i=w5WLP7bZ
- // @updateURL http://pastebin.com/raw.php?i=w5WLP7bZ
- // ==/UserScript==
- window.inn = {};
- var inn = window.inn;
- inn.debug = false;
- inn.debugLog =
- function(message)
- {
- if(inn.debug)
- {
- console.log(message);
- }
- };
- var _tickers = {};
- inn.htmlFriendlyName =
- function(string)
- {
- string = string.replace(" ", "");
- string = string.replace(/([a-z][A-Z])/g, function(g) { return g[0] + '-' + g[1].toLowerCase(); });
- return string.toLowerCase();
- };
- inn.modules = {};
- inn.Module =
- function(name, description, ticker, definitions)
- {
- var _enabled = false;
- this.name = name;
- this.checkboxID = "inn-" + inn.htmlFriendlyName(this.name) + "-checkbox";
- this.description = description || "No description.";
- this.settings = {};
- this.ticker = ticker.bind(this.settings);
- this.definitions = definitions || {};
- inn.modules[name] = this;
- Object.defineProperty(this, "enabled",
- {
- get:function()
- {
- return _enabled;
- },
- set:function(value)
- {
- _enabled = !!value;
- if(value)
- {
- _tickers[this.name] = this.ticker;
- }
- else
- {
- delete _tickers[this.name];
- }
- }
- });
- for(var label in definitions)
- {
- var def = definitions[label];
- try
- {
- this.settings[label] = def.defaultValue;
- }
- catch(error)
- {
- console.log("Error setting default value from definition " + label + " (" + error.message + "):");
- console.log(def);
- }
- }
- };
- inn.Module.prototype.createModuleSection =
- function()
- {
- this.checkbox = $('<input>', { type:"checkbox", id:this.checkboxID });
- var result = $('<div></div>');
- result.append($('<h1>' + this.name + '</h1>').append(this.checkbox));
- result.append('<p>' + this.description + '</p>');
- for(var label in this.definitions)
- {
- result.append(this.createSettingSection(label));
- }
- return result;
- };
- inn.Module.prototype.createSettingSection =
- function(label)
- {
- var definition = this.definitions[label];
- var result = $('<div></div>');
- result.append('<h3>' + definition.name + '</h3>');
- result.append('<p>' + definition.description + '</p>');
- result.append(this.createSettingComponent(label));
- return result;
- };
- inn.Module.prototype.createSettingComponent =
- function(label)
- {
- var definition = this.definitions[label];
- definition.componentID = "inn-" + inn.htmlFriendlyName(this.name) + "-" + inn.htmlFriendlyName(definition.name);
- var content = undefined;
- switch(definition.type)
- {
- case "number":
- content = $('<input>', { type: "text" });
- definition.retrieve =
- function()
- {
- var result = parseFloat(content.val());
- if(!isNaN(result))
- {
- this[label] = result;
- }
- };
- break;
- case "integer":
- content = $('<input>', { type: "text" });
- definition.retrieve =
- function()
- {
- var result = parseInt(content.val(), 10);
- if(!isNaN(result))
- {
- this[label] = result;
- }
- };
- break;
- case "select":
- content = $('<select></select>'); //falls through to populate
- case "select-multiple":
- content = content || $('<select></select>', { multiple: "multiple" });
- for(var optionLabel in definition.options)
- {
- var option = $('<option value="' + definition.options[optionLabel] + '">' + optionLabel + '</option>');
- content.append(option);
- }
- break;
- case "boolean":
- content = $('<input>', { type: "checkbox" });
- definition.update =
- function()
- {
- content.prop("checked", this[label]);
- };
- definition.retrieve =
- function()
- {
- this[label] = content.is(":checked");
- };
- break;
- default:
- content = 'Unable to create a component for this setting.';
- inn.debugLog("Unable to create a component for setting:");
- inn.debugLog(definition);
- break;
- };
- definition.update = (definition.update || //set sensible defaults and bind to settings
- function()
- {
- content.val(this[label]);
- }).bind(this.settings);
- definition.retrieve = (definition.retrieve ||
- function()
- {
- this[label] = content.val();
- }).bind(this.settings);
- content.attr("id", definition.componentID);
- definition.component = content;
- return content;
- };
- inn.Module.prototype.updateModuleSection =
- function()
- {
- this.checkbox.prop("checked", this.enabled);
- inn.debugLog(this.name + " is " + this.enabled);
- for(var label in this.definitions)
- {
- this.definitions[label].update();
- }
- };
- inn.Module.prototype.retrieveModuleSection =
- function()
- {
- this.enabled = this.checkbox.is(":checked");
- for(var label in this.definitions)
- {
- this.definitions[label].retrieve();
- }
- };
- inn.Module.prototype.retrieveSettingComponent =
- function(label)
- {
- var definition = this.definitions[label];
- };
- /* MODIFY DOM TO ALLOW OPTIONS TO BE CHANGED */
- inn.showMenu =
- function()
- {
- inn.settingsDialog.dialog("open");
- inn.settingsDialog.css({ height: "500px", overflow: "auto" });
- for(var label in inn.modules)
- {
- inn.modules[label].updateModuleSection();
- }
- };
- inn.updateSettings =
- function()
- {
- for(var label in inn.modules)
- {
- inn.modules[label].retrieveModuleSection();
- }
- inn.settingsDialog.dialog("close");
- inn.save();
- };
- $("#menu-area").append('<br><button class="btn oval-btn menu-button" onclick="inn.showMenu()">Mine Guardian Settings</button>');
- inn.settingsDialogID = "inn-mine-guardian-menu";
- inn.settingsDialog = $('<div id="' + inn.settingsDialogID + '"></div>');
- $("div.ui-dialog:last").after(inn.settingsDialog);
- inn.initialiseSettingsMenu =
- function()
- {
- for(var label in inn.modules)
- {
- var result = inn.modules[label].createModuleSection();
- inn.settingsDialog.append(result);
- }
- inn.settingsDialog.dialog(
- {
- autoOpen: false,
- title: "Mine Guardian Settings",
- minWidth: 500,
- buttons: [ { text: "Save Settings", click: inn.updateSettings } ]
- });
- };
- /* UPDATE SETTINGS FROM STORAGE */
- inn.storageName = "inndefSettings";
- inn.load =
- function()
- {
- var settings = JSON.parse(localStorage.getItem(inn.storageName));
- for(var label in settings)
- {
- var saveSettings = settings[label] || {};
- var module = inn.modules[label];
- $.extend(module.settings, saveSettings);
- module.enabled = saveSettings.enabled;
- }
- };
- inn.save =
- function()
- {
- var settings = {};
- for(var label in inn.modules)
- {
- settings[label] = {};
- var saveSettings = settings[label];
- var module = inn.modules[label];
- $.extend(saveSettings, module.settings);
- saveSettings.enabled = module.enabled;
- }
- localStorage.setItem(inn.storageName, JSON.stringify(settings));
- };
- /* AUTO CLICK */
- var autoClick = new inn.Module(
- "Mine Clicker",
- "Automatically clicks the mine.",
- function()
- {
- if(this.clickAmount > 0)
- {
- MD.dig(250, 250, this.clickAmount);
- }
- },
- {
- clickAmount: {
- name: "Click Amount",
- description: "The number of times per second to click the mine.",
- type: "integer",
- defaultValue: 1
- }
- });
- /* MAGE CLICKER */
- var mageClick = new inn.Module(
- "Mage Clicker",
- "Automatically powers up your mages to maximum power.",
- function()
- {
- var numMages = Math.ceil(MD.MAGES/5);
- for(var i = 0;i < 6 && i < numMages;++i)
- {
- MD.clickMage(i);
- }
- });
- /* AUTO COMBINE */
- inn.debugLog("TODO: make the gem cap test for the Gem Combiner smarter so that it's more efficient.");
- var autoCombine = new inn.Module(
- "Gem Combiner",
- "Combines gems using the \"Combine Up To\" setting on the gems page while maintaining at least enough gems to maximise your discovery gem caches.",
- function()
- {
- var totalGems = Math.round(sumArray(MD.GEMS));
- var gemCap = MD.getStatueGemAmount(MD.CONSTRUCT_LEVELS[4])*200;
- if(totalGems > 8*gemCap)
- {
- MD.combineGems();
- }
- });
- /* AUTO DISCOVER */
- var autoDiscover = new inn.Module(
- "Quarry Discoverer",
- "Automatically checks the quarry when a discovery is found.",
- function()
- {
- if(get("quarry-discovery").style.display !== 'none')
- {
- MD.findQuarryReward();
- }
- });
- /* AUTO CAMPAIGN */
- var autoCampaign = new inn.Module(
- "Campaign Launcher",
- "Automatically launches campaigns when you are a certain proportion stronger than your enemy. Use with caution; it's easy to accidentally make the invading enemies extremely strong.",
- function()
- {
- if(MD.getMilitaryStrength() >= MD.getEnemyStrength()*this.minimumForceRatio)
- {
- MD.launchCampaign();
- if(this.bombEnemies)
- {
- MD.bombEnemies();
- }
- }
- },
- {
- minimumForceRatio: {
- name: "Minimum Force Ratio",
- description: "The minimum ratio of your strength to your enemy's strength for automatic campaigns.",
- type: "number",
- defaultValue: 1
- },
- bombEnemies: {
- name: "Bomb Enemies",
- description: "If enabled, a bomb will be dropped after every automatically launched campaign.",
- type: "boolean",
- defaultValue: false
- }
- });
- /* AUTO SCHOLAR */
- var autoScholar = new inn.Module(
- "Scholar Maker",
- "",
- function()
- {
- if(MD.getPopulation() >= MD.getPopulationMax())
- {
- MD.addScholars();
- $("#construct-dialog-10").dialog("close");
- }
- });
- /* AUTO BUY */
- var autoBuy = new inn.Module(
- "Hireling Buyer",
- "Automatically buys unlocked hirelings up to a certain limit.",
- function()
- {
- var list = this.indexList;
- var limit = this.limit;
- var amount = this.amount;
- for(var index = 0;index < list.length;++index)
- {
- var number = parseInt(list[index], 10);
- var buying = Math.min(limit - MD.HIRELINGS_OWNED[number]);
- for(var i = 0;i < buying;++i)
- {
- MD.hire(number);
- }
- }
- },
- {
- indexList: {
- name: "Hireling Index List",
- description: "The list of hirelings to automatically buy up to the limit. You can select multiple.",
- type: "select-multiple",
- defaultValue: [],
- options: {
- "Large Ant": 0,
- "Woodpecker": 1,
- "Bloodhound": 2,
- "Sandshrew": 3,
- "Goblin": 4,
- "Miner": 5,
- "Rock Golem": 6,
- "Bagger 288": 7,
- "Titan of Earth": 8,
- "World Eater": 9,
- "Hive Queen": 10,
- "Wyvern": 11,
- "Neurochrysalis": 12,
- "Dragon Hunter": 13
- }
- },
- limit: {
- name: "Limit",
- description: "The limit up to which hirelings will be purchased.",
- type: "number",
- defaultValue: 1
- },
- amount: {
- name: "Amount",
- description: "The maximum number of hirelings of each type that will be purchased per tick.",
- type: "integer",
- defaultValue: 10
- }
- });
- /* AUTO DRAGON */
- var essenceSelectID = "dragon-essence-type";
- var essenceLabels = ["fire", "water", "earth", "lightning"];
- var getEssenceCount =
- function(label)
- {
- return MD[label.toUpperCase() + "_ESSENCE"];
- };
- eval('inn.sacrificeDragonSilently = ' + MD.sacrificeDragon.toString().replace(/confirm\("[^()"]*?"\)/, 'true')); //this is an awful hack to eliminate the confirmation, but it's better than hardcoding the function without it
- var autoDragon = new inn.Module(
- "Dragon Breeder",
- "Automatically feeds dragons up to a limit and sacrifices them when they are above a threshold (preferring to feed before sacrificing).",
- function()
- {
- if(MD.DRAGON_LEVEL < this.feedLimit && $("#dragon-100p.unaffordable").length <= 0)
- {
- if(MD.DRAGONS[MD.CURRENT_DRAGON].name == "Master")
- {
- var maximum = essenceLabels[0];
- var maximumValue = getEssenceCount(maximum);
- for(var index = 1;index < essenceLabels.length;++index)
- {
- var label = essenceLabels[index];
- var value = getEssenceCount(label);
- if(value > maximumValue)
- {
- maximum = label;
- maximumValue = value;
- }
- }
- $("#" + essenceSelectID).val(maximum);
- MD.selectEssenceType();
- }
- MD.feedDragon('100P');
- }
- else if(MD.DRAGON_LEVEL >= this.sacrificeThreshold && $("#sacDragonButton.unaffordable").length <= 0)
- {
- inn.sacrificeDragonSilently();
- $("#construct-dialog-23").dialog("close");
- }
- },
- {
- feedLimit: {
- name: "Feeding Limit",
- description: "The maximum level to which dragons will be fed.",
- type: "number",
- defaultValue: 0
- },
- sacrificeThreshold: {
- name: "Sacrifice Threshold",
- description: "The minimum level over which dragons will be sacrificed if they cannot be fed any further.",
- type: "number",
- defaultValue: 1e308
- }
- });
- /* AUTO DEFEND */
- var autoDefend = new inn.Module(
- "Defender",
- "Automatically clicks enemies attacking the mine to damage them greatly.",
- function()
- {
- for(var index = 0;index < MD.GOBLIN_OBJECT.length;++index)
- {
- if(MD.GOBLIN_OBJECT[index] !== null)
- {
- MD.clickGoblin(index);
- }
- }
- for(index = 0;index < MD.SPIDER_OBJECT.length;++index)
- {
- if(MD.SPIDER_OBJECT[index] !== null)
- {
- MD.clickSpider(index);
- }
- }
- });
- /* AUTO UPGRADE */
- var shrineIndices = [15, 17, 19, 21];
- inn.simpleConstructUpgrade =
- function(index)
- {
- MD.increaseConstructLevel(index);
- $("#construct-dialog-" + index).dialog("close");
- };
- var autoUpgrade = new inn.Module(
- "Construct Upgrader",
- "Automatically upgrades some constructs without regard to preserving minimum amounts of resources. Use with caution; you may end up using more resources than you want.",
- function()
- {
- if(this.fletcher)
- {
- inn.simpleConstructUpgrade(14);
- }
- if(this.smelter)
- {
- inn.simpleConstructUpgrade(5);
- }
- if(this.statue)
- {
- inn.simpleConstructUpgrade(4);
- }
- if(this.wall)
- {
- MD.buyWall();
- }
- if(this.shrines)
- {
- var lowest = shrineIndices[0];
- var lowestValue = MD.CONSTRUCT_LEVELS[lowest];
- for(var shrine = 1;shrine < shrineIndices.length;++shrine) //skip 0th, since we assume that's the lowest
- {
- var index = shrineIndices[shrine];
- var level = MD.CONSTRUCT_LEVELS[index];
- if(level < lowestValue)
- {
- lowest = index;
- lowestValue = level;
- }
- }
- inn.simpleConstructUpgrade(lowest);
- }
- },
- {
- fletcher: {
- name: "Fletcher",
- description: "Upgrades the fletcher automatically.",
- type: "boolean",
- defaultValue: false
- },
- smelter: {
- name: "Smelter",
- description: "Upgrades the smelter automatically.",
- type: "boolean",
- defaultValue: false
- },
- statue: {
- name: "Regal Statue",
- description: "Upgrades the regal statue automatically.",
- type: "boolean",
- defaultValue: false
- },
- wall: {
- name: "Wall",
- description: "Upgrades the wall automatically.",
- type: "boolean",
- defaultValue: false
- },
- shrines: {
- name: "Elemental Shrines",
- description: "Upgrades the elemental shrines automatically. Keeps them the same level so none feel left out.",
- type: "boolean",
- defaultValue: false
- },
- });
- /* AUTO MACHINIST */
- var autoMachinist = new inn.Module(
- "Machinist Manager",
- "Toggles whether the Machinist will auto-build units based on the essence you have compared to a threshold.",
- function()
- {
- var desired = Math.min(MD.FIRE_ESSENCE, MD.WATER_ESSENCE, MD.EARTH_ESSENCE, MD.LIGHTNING_ESSENCE) > this.threshold;
- for(var index = 0;index < this.controlling.length;++index)
- {
- MD.MACHINIST_AUTO[this.controlling[index]] = desired;
- }
- MD.updateMechPage();
- },
- {
- threshold: {
- name: "Essence Threshold",
- description: "Below this threshold all managed auto-builds will be set to off. Above it, they will be set to on.",
- type: "number",
- defaultValue: 1e6
- },
- controlling: {
- name: "Controlling",
- description: "The machinist units to control based on the available essence. You can select multiple.",
- type: "select-multiple",
- defaultValue: [],
- options: {
- "Mage": 0,
- "Mason": 1,
- "Alchemist": 2
- }
- }
- });
- /* INITIALISE MASTER TICKER */
- inn.initialiseSettingsMenu();
- inn.tickerInterval = setInterval(
- function()
- {
- for(var label in _tickers)
- {
- _tickers[label]();
- }
- }, 1000);
- inn.load();
Advertisement
Add Comment
Please, Sign In to add comment