Trimps tools

Trimps tools (visual)

目前為 2017-04-11 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Trimps tools
// @namespace    trimps.github.io
// @version      1.109
// @description  Trimps tools (visual)
// @author       Anton
// @match        https://trimps.github.io
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// ==/UserScript==

(function() {
	'use strict';

	var tAutoBuy, tStyleFix, tPassiveWatcher, $ = jQuery, isStarted = false,
		version = typeof GM_info == 'function' ? GM_info().script.version :
			(typeof GM_info == 'object' ? GM_info.script.version : '?');

	var _isPortal = function() {
		return portalWindowOpen;
	}

	var _getJobPrice = function(jobName, resource) {
		return game.jobs[jobName].cost[resource][0] * Math.pow(game.jobs[jobName].cost[resource][1], game.jobs[jobName].owned);
	}

	var _log = function(mes, type) {
		if (typeof type === 'undefined') type = "Story";
		message("BOT: " + mes, type);
	}

	var _getBreedingBaseSpeed = function() {
		var trimps = game.resources.trimps;
		var base = 0.0085;
		var breeding = trimps.owned - trimps.employed;
		var currentCalc = breeding * base;
		if (game.upgrades.Potency.done > 0) {
		    var potencyStrength = Math.pow(1.1, game.upgrades.Potency.done);
    		currentCalc *= potencyStrength;
		}
		return currentCalc;
	}

	var _autoEquipment = function() {
		for (var x in game.equipment) {
			if (game.equipment.hasOwnProperty(x) && game.equipment[x].locked === 0 && game.equipment[x].level < 2) {
				if (canAffordBuilding(x, null, null, true)) {
					buyEquipment(x, true, true);
					_log('Upgrading equipment ' + x);
				}
			}
		}
	}

	var _buyStorage = function() {
		var barnThreshold = 0.9;  /*percentage at which minimum*/
		var shedThreshold = 0.9;  /*to buy storage*/
		var forgeThreshold = 0.9; /*from 0 (min) to 1 (max)*/

		if(game.resources.food.owned / (game.resources.food.max * (1 + game.portal.Packrat.level * (game.portal.Packrat.modifier * 100) / 100)) >= barnThreshold) {
			buyBuilding("Barn", true, true);
			_log('Building Barn');
		}
		if(game.resources.wood.owned / (game.resources.wood.max * (1 + game.portal.Packrat.level * (game.portal.Packrat.modifier * 100) / 100)) >= shedThreshold) {
			buyBuilding("Shed", true, true);
			_log('Building Shed');
		}
		if(game.resources.metal.owned / (game.resources.metal.max * (1 + game.portal.Packrat.level * (game.portal.Packrat.modifier * 100) / 100)) >= forgeThreshold) {
			buyBuilding("Forge", true, true);
			_log('Building Forge');
		}
	}

	var _earlyGameStrategy = function() {
		if (_isPortal()) return;

		var playerStrength = getPlayerModifier();
		var minimumSpeedToHelp = 1 + playerStrength;

		_buyStorage();
		_autoUpgrade();
		_autoJobs();

		if ((game.global.buildingsQueue.length > 0 && game.global.autoCraftModifier < 1) || (game.global.buildingsQueue.length > 5)) {
			setGather('buildings');
			return;
		}

		if (getPsString('food', true) < minimumSpeedToHelp && game.resources.food.owned < 10) {
			setGather('food');
			return;
		}

		if (getPsString('wood', true) < minimumSpeedToHelp && game.resources.wood.owned < 10) {
			setGather('wood');
			return;
		}

		if (game.buildings.Trap.owned < 1 && game.resources.food.owned >= 10 && game.resources.wood.owned >= 10) {
			buyBuilding('Trap', true, true);
			return;
		}

		if (game.resources.trimps.owned < game.resources.trimps.realMax() && _getBreedingBaseSpeed() < 1) {
			setGather('trimps');
			return;
		}

		if (getPsString('science', true) < minimumSpeedToHelp && game.resources.science.owned < 10) {
			setGather('science');
			return;
		}

		if (getPsString('metal', true) < minimumSpeedToHelp && game.resources.metal.owned < 100) {
			setGather('metal');
			return;
		}

		if (game.global.playerGathering == 'trimps' ||
			(game.global.playerGathering == 'buildings' && (game.global.buildingsQueue.length === 0 || game.global.autoCraftModifier >= 1)))
		{
			setGather('science');
		}
	}

	var _passiveWatcher = function() {
		if (_isPortal()) return;

		_earlyGameStrategy();
	}

	var _mapAttackStrategy = function() {
		if (game.global.fighting) return;
		if (game.global.preMapsActive) return;
		//if (!game.global.mapsUnlocked) return;
		if (game.global.pauseFight) {
			_log('Enabling auto attack!');
		    fightManual();
			game.global.pauseFight = false;
			pauseFight(true); // update only
		} else if (!game.global.preMapsActive && game.global.currentMapId == '') {
		    var mapFound = false, hasNotPass = false;
			for (var x in game.mapUnlocks) {
				var notPass = game.mapUnlocks[x].startAt <= game.global.world && (typeof game.passedMaps[game.mapUnlocks[x].startAt] === 'undefined' || game.passedMaps[game.mapUnlocks[x].startAt] < 1);
				if (notPass) {
				    hasNotPass = true;
				    for (var map_id in game.global.mapsOwnedArray) {
				        var map = game.global.mapsOwnedArray[map_id];
				        if (map.clears === 0 && map.level <= game.global.world) {
				            console.log('attacking ' + map.id + ' "' + map.name + '"');
				            map.clears = 1;
				            mapsClicked(true); // go to maps
				            selectMap(map.id);
				            runMap();
				            mapFound = true;
				            break;
				        }
				    }
                }
                if (mapFound) break;
			}
			if (!mapFound && hasNotPass) {
			    buyMap(); // new map
			}
		}
	}

	var _auto = function() {
		if (_isPortal()) return;
		_earlyGameStrategy();
		_autoBuy();
		_autoEquipment();
		_mapAttackStrategy();
	}

	var _onStartButton = function() {
		if (isStarted) {
			_stop();
			_log('Stop.');
			isStarted = false;
		} else {
			_start();
			_log('Started!');
			isStarted = true;
		}
	}

	var _styleUpdate = function() {
		// remove counts
		$('head').append('<style type="text/css">span.thingName{font-size:85%;}.queueItem,.btn{padding:0}.thingColorCanNotAfford.upgradeThing{background-color:#530053;}</style>');
		// remove tabs
		$('#buyTabs').hide();
		filterTabs('all');
		// remove captions
		$('#buildingsTitleDiv,#upgradesTitleDiv,#equipmentTitleDiv').hide();
		// fix height
		$('#topRow,#queueContainer').css('margin-bottom', '0');
		$('#jobsTitleDiv').css('padding', '0').css('font-size', 'smaller');
		$('#buyHere').css('margin', '0').css('padding', '0').css('overflow-x', 'hidden');
		$('#queueContainer').css('height', '70px');
		$('#numTabs').css('margin', '0');
		$('#buyContainer').css('height', 'calc(99vh - 20vw - 96px)');
		// add button
		$('#settingsTable tr').append('<td class="btn btn-info" id="botStart">Bot start</td>');
		$('#botStart').click(_onStartButton);
	}

	var _autoUpgrade = function() {
		for (var item in game.upgrades){
			var upgrade = game.upgrades[item];
			if (upgrade.locked == 1) continue;
			var canAfford = canAffordTwoLevel(upgrade);
			if (canAfford) {
			    if (item == "Coordination") {
                    if (!canAffordCoordinationTrimps()) continue;
			    }
				buyUpgrade(item, true, true);
				_log('Upgrading ' + item);
				return 1;
			}
		}
		return 0;
	}

	var _autoBuy = function() {
	    var toBuy;
    	for (var item in game.buildings) {
    	    if (item == 'Barn' || item == 'Shed' || item == 'Forge' || item == 'Wormhole' || item == 'Trap') continue;
    		building = game.buildings[item];
	    	if (building.locked == 1) continue;
	    	var canAfford = canAffordBuilding(item, false, false, false, true);
	    	if (canAfford) {
    	    	if (item == 'Nursery') {
            		if (game.buildings.Nursery.owned >= game.buildings.Tribute.owned ||
            		    game.buildings.Nursery.owned >= game.buildings.Gym.owned) continue;
            		    else toBuy = item;
    	    	} else {
    	    	    toBuy = item;
    	    	}
	    	}
    	}
    	if (typeof toBuy !== 'undefined') {
			buyBuilding(toBuy, true, true);
			_log('Building ' + toBuy);
        	return 1;
    	} else {
    	    return 0;
    	}
	}

	var needFarmer = 25, needLumber = 25, needMiner = 25, needScientist = 1;
	var needAllMax = needFarmer + needLumber + needMiner + needScientist;

	var _buyJobs = function($obj, unemployed, objName, jobId) {
		if ($obj.length > 0) {
			var cnt = 1;
			if (unemployed > needAllMax * 10) {numTab(4);cnt=100;}
			else if (unemployed > needAllMax * 2.5) {numTab(3);cnt=25;}
			else if (unemployed > needAllMax) {numTab(2);cnt=10;}
			buyJob(jobId, true, true); // confirmed, noTip
			numTab(1); // +1
			_log('New ' + objName + (cnt > 1 ? " x" + cnt : ''), 'Combat');
			return cnt;
		} else {
			return 0;
		}
	}

	var _autoJobs = function() {
		var jobsTotal =
			game.jobs.Farmer.owned +
			game.jobs.Lumberjack.owned +
			game.jobs.Miner.owned +
			game.jobs.Scientist.owned;

		var unemployed = Math.ceil(game.resources.trimps.realMax() / 2) - game.resources.trimps.employed;

		var trainerCost = _getJobPrice('Trainer', 'food');
		if ((trainerCost < game.resources.food.owned) && unemployed <= 0 && game.jobs.Farmer.owned > 1 && game.jobs.Trainer.locked === 0) {
			game.global.firing = true;
			_log('Fire farmer, sorry');
			buyJob('Farmer', true, true);
			game.global.firing = false;
		}

		if (unemployed <= 0) return;

		if (trainerCost <= game.resources.food.owned && game.jobs.Trainer.locked === 0) {
			buyJob('Trainer', true, true);
			_log('New trainer');
			return 1;
		}

		var cnt = 0;

		var $explorer = $('#jobsHere').find('.thingColorCanAfford[id=Explorer]');
		if ($explorer.length > 0) {
			buyJob('Explorer', true, true);
			_log('New explorer');
			return ++cnt;
		}

		var hasFarmer = game.jobs.Farmer.locked === 0;
		var hasLumber = game.jobs.Lumberjack.locked === 0;
		var hasMiner = game.jobs.Miner.locked === 0;
		var hasScientist = game.jobs.Scientist.locked === 0;
		var needAll =
			(hasFarmer ? needFarmer : 0) +
			(hasLumber ? needLumber : 0) +
			(hasMiner ? needMiner : 0) +
			(hasScientist ? needScientist : 0);
		if (needAll < 1) needAll = 1;

		var minOwned = Math.min(
			game.jobs.Farmer.owned,
			game.jobs.Lumberjack.owned,
			game.jobs.Miner.owned);
		if (minOwned > 15) minOwned = 15; // for science

		if (hasScientist && game.jobs.Scientist.owned < minOwned) {
			var $science = $('#jobsHere').find('.thingColorCanAfford[id=Scientist]');
			cnt += _buyJobs($science, unemployed, 'scientist', 'Scientist');
		} else if (hasFarmer && game.jobs.Farmer.owned < (jobsTotal * needFarmer / needAll)) {
			var $farmer = $('#jobsHere').find('.thingColorCanAfford[id=Farmer]');
			cnt += _buyJobs($farmer, unemployed, 'farmer', 'Farmer');
		} else if (hasLumber && game.jobs.Lumberjack.owned < (jobsTotal * needLumber / needAll)) {
			var $lumber = $('#jobsHere').find('.thingColorCanAfford[id=Lumberjack]');
			cnt += _buyJobs($lumber, unemployed, 'lumberjack', 'Lumberjack');
		} else if (hasMiner && game.jobs.Miner.owned < (jobsTotal * needMiner / needAll)) {
			var $miner = $('#jobsHere').find('.thingColorCanAfford[id=Miner]');
			cnt += _buyJobs($miner, unemployed, 'miner', 'Miner');
		} else if (hasScientist && game.jobs.Scientist.owned < (jobsTotal * needScientist / needAll)) {
			var $science = $('#jobsHere').find('.thingColorCanAfford[id=Scientist]');
			cnt += _buyJobs($science, unemployed, 'scientist', 'Scientist');
		}

		if (unemployed > 0 && cnt === 0) {
			$farmer = $('#jobsHere').find('.thingColorCanAfford[id=Farmer]');
			cnt += _buyJobs($farmer, unemployed, 'farmer', 'Farmer');
		}

		return cnt;
	}

	var _styleFix = function() {
	    _disableLog();
	    
		$('.buyBox').find('.thing').find('br').remove();
		$('.buyBox').find('.thing').find('.thingOwned').css('margin-left','4px');
		$('#buyHere').find('.alert.badge').text('');
		$('#buildingsQueue').css('height', '30px');
		for (var x in game.upgrades) if (game.upgrades[x].alert && game.upgrades[x].locked === 0) game.upgrades[x].alert = false;
		for (x in game.buildings) if (game.buildings[x].alert && game.buildings[x].locked === 0) game.buildings[x].alert = false;
		for (x in game.jobs) if (game.jobs[x].alert && game.jobs[x].locked === 0) game.jobs[x].alert = false;

		var hasItem = false;
		if (typeof game.passedMaps === 'undefined') game.passedMaps = {};
		if (typeof game.mapUnlocks === 'undefined') {
			console.log('Waiting for game.mapUnlocks...');
			clearInterval(tStyleFix);
			tStyleFix = setInterval(_styleFix, 2000);
		} else {
			for (x in game.mapUnlocks) {
				var notPass = game.mapUnlocks[x].startAt <= game.global.world && (typeof game.passedMaps[game.mapUnlocks[x].startAt] === 'undefined' || game.passedMaps[game.mapUnlocks[x].startAt] < 1);
				if (notPass) {
					$('#battleSideTitle').css('background-color', notPass ? '#A00' : '#600');
					hasItem = true;
					break;
				}
			}
		}
		if (!hasItem) {
			$('#battleSideTitle').css('background-color', 'transparent');
		}
		game.passedMaps[game.global.world] = game.global.mapBonus;
		if (game.global.mapBonus > 0) {
			for (x in game.mapUnlocks) {
				if (game.mapUnlocks[x].startAt < game.global.world) {
					game.passedMaps[game.mapUnlocks[x].startAt] = 1;
				}
			}
		}
	}

	var _start = function() {
		_log('Passive watcher stop');
		clearInterval(tPassiveWatcher);

		_log('BOT start');
		tAutoBuy = setInterval(_auto, 1000);

		$('#botStart').text('Bot stop');
	}

	var _stop = function() {
		_log('BOT stop');
		clearInterval(tAutoBuy);

		tPassiveWatcher = setInterval(_passiveWatcher, 1000);
		_log('Passive watcher started');

		$('#botStart').text('Bot start');
	}

    var logEnabled = true;

    var _disableLog = function() {
        if (logEnabled) {
            message = function() {};
            setTimeout(function() {
                $('#logContainer').html('');
            }, 2000);
            logEnabled = false;
        }
    }

	setTimeout(function() {

		_log('Trimps BOT version ' + version);

		tStyleFix = setInterval(_styleFix, 2000);

		_styleUpdate();
		_styleFix();
		
		_disableLog();

		_stop(); // start passive watcher

	}, 1000);

})();