// ==UserScript==
// @name Kittens tools
// @namespace http://bloodrizer.ru/games/kittens/
// @version 1.044
// @description Trimps tools (visual)
// @author Anton
// @match http://bloodrizer.ru/games/kittens/
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
var $ = jQuery, isStarted = false,
version = typeof GM_info == 'function' ? GM_info().script.version :
(typeof GM_info == 'object' ? GM_info.script.version : '?');
var _log = function(message) {
var mes = 'BOT: ' + message;
if (game && game.msg && game.ui) {
game.msg(mes, 'msg');
game.ui.renderConsoleLog();
}
};
var isUnlocked = function(res) {
var resource = game.resPool.get(res);
return resource.unlocked;
};
var _craftAll = function(res) {
if (isUnlocked(res)) {
_log("Crafting " + res);
game.craftAll(res);
}
};
var _getAstronomy = function() {
if (game.calendar.observeRemainingTime > 0) {
if (typeof game.calendar.observeHandler === 'function') {
_log('Collecting astronomy');
game.calendar.observeHandler();
}
}
};
var _flushCatnip = function() {
var catnip = game.resPool.get("catnip");
if (catnip.value >= catnip.maxValue) {
_log('Catnip to Wood');
game.craftAll('wood');
}
};
var _collectFaith = function() {
var faith = game.resPool.get("faith");
if (faith.value >= faith.maxValue) {
_log('Praise');
game.religion.praise();
}
};
var _sendHunters = function() {
var manpower = game.resPool.get("manpower");
if (manpower.value >= manpower.maxValue) {
_log('Sending hunters');
game.village.huntAll();
_craftAll('parchment');
}
};
var _getMinCraft = function(res) {
var allCount = game.workshop.getCraftAllCount(res);
var ratioCount = Math.floor(allCount*0.02);
return ratioCount < 1 ? 1 : ratioCount;
}
var _ironToSteel = function() {
var iron = game.resPool.get("iron");
var coal = game.resPool.get("coal");
if (iron.value >= iron.maxValue || coal.value >= coal.maxValue) {
var minPlate = _getMinCraft('plate');
if (coal.value >= 100 && iron.value >= 100 && isUnlocked('steel')) {
_log('Iron to Steel x ALL');
game.craftAll('steel');
} else if (iron.value >= (125 * minPlate) && isUnlocked('plate')) {
_log('Iron to Plate x ' + minPlate);
game.craft('plate', minPlate);
}
}
};
var _woodToBeams = function() {
var wood = game.resPool.get("wood");
if (wood.value >= wood.maxValue && isUnlocked('beam')) {
var minVal = _getMinCraft('beam');
_log('Wood to Beam x ' + minVal);
game.craft('beam', minVal);
}
};
var _mineralsToSlabs = function() {
var minerals = game.resPool.get("minerals");
if (minerals.value >= minerals.maxValue && isUnlocked('slab')) {
var minVal = _getMinCraft('slab');
_log('Minerals to Slab x ' + minVal);
game.craft('slab', minVal);
}
};
var _fixFontSize = function() {
var fnt1 = $('#leftColumn').css('font-size');
var fnt2 = $('#midColumn').css('font-size');
var fnt3 = $('#rightColumn').css('font-size');
if (fnt2 != fnt1 || fnt3 != fnt1) {
_log('Fixing font size');
$('#midColumn').css('font-size', fnt1)
$('#rightColumn').css('font-size', fnt1)
}
};
/*var _buyAll = function() {
for (var x in game.bld.metaCache) {
var name = game.bld.metaCache[x].meta.name;
if (game.bld.metaCache[x].meta.unlocked && game.resPool.hasRes(game.bld.getPrices(name))) {
_log('AUTOBUY ' + name);
// TODO
}
}
};*/
var _cultureToManuscript = function() {
var culture = game.resPool.get("culture");
var parchment = game.resPool.get("parchment");
var minVal = _getMinCraft('manuscript');
if (culture.value >= culture.maxValue && isUnlocked('manuscript') && culture.value >= (400 * minVal) && parchment.value >= (25 * minVal)) {
_log('Culture to Manuscript x ' + minVal);
game.craft('manuscript', minVal);
}
};
var _canBuy = function(bldName) {
var prices = game.bld.getPrices(bldName);
for (var x in prices) {
if (prices.hasOwnProperty(x)) {
if (prices[x].val > game.resPool.get(prices[x].name).value) {
return false;
}
}
}
return true;
};
var _makeABuy = function(itemName) {
var btn = $('.bldGroupContainer').find('div.btnContent').find('span').filter(function(){
return $(this).text().indexOf(itemName) === 0;
});
if (btn && btn.length == 1) {
_log('Autobuy ' + itemName);
btn.click();
}
};
var _autoBuyItem = function(bldName) {
var bld = game.bld.get(bldName);
if (bld.unlocked && _canBuy(bldName)) {
var itemName = bld.stages && bld.stages.length > 0 ? bld.stages[bld.stage].label : bld.label;
_makeABuy(itemName);
}
};
var _autoBuy = function() {
_autoBuyItem('field');
_autoBuyItem('pasture');
_autoBuyItem('unicornPasture');
_autoBuyItem('hut');
_autoBuyItem('logHouse');
_autoBuyItem('workshop');
_autoBuyItem('aqueduct');
_autoBuyItem('library');
_autoBuyItem('academy');
_autoBuyItem('barn');
_autoBuyItem('mine');
_autoBuyItem('lumberMill');
_autoBuyItem('temple');
_autoBuyItem('tradepost');
_autoBuyItem('warehouse');
_autoBuyItem('chapel');
_autoBuyItem('amphitheatre');
_autoBuyItem('smelter');
};
var _autoClick = function() {
var field = game.bld.get('field');
if (field.on < 5) {
var btn = $('.bldGroupContainer').find('div.btnContent').find('span').filter(function(){
return $(this).text().indexOf("Gather catnip") === 0;
});
btn.click();
}
};
var _kittensForPromote = function() {
var x=0;
for (var i = 0; i < game.village.sim.kittens.length; i++) {
var done = false;
if(game.village.sim.kittens[i].engineerSpeciality !== null) {
var kitten = game.workshop.getCraft(game.village.sim.kittens[i].engineerSpeciality);
if (kitten) {
var tier = kitten.tier;
if (game.village.sim.kittens[i].rank < tier) {
x++;
done = true;
}
}
}
if (!done) {
x++;
}
}
return x;
}
var _promote = function() {
var gold = game.resPool.get("gold");
if (!gold.unlocked) return;
if (gold.value >= 15 && gold.value >= gold.maxValue && _kittensForPromote() > 0) {
game.village.promoteKittens();
}
};
var _scienceToCompendium = function() {
var science = game.resPool.get("science");
var manuscript = game.resPool.get("manuscript");
var minVal = _getMinCraft('compedium');
if (science.value >= science.maxValue && isUnlocked('compedium') && manuscript.value >= (50 * minVal) && science.value >= (10000 * minVal)) {
_log('Science + Manuscript to Compendium x ' + minVal);
game.craft('compedium', minVal);
}
};
var _auto = function() {
_getAstronomy();
_fixFontSize();
if (!tStarted) return;
_autoClick();
_autoBuy();
_promote();
_collectFaith();
_ironToSteel();
_sendHunters();
_cultureToManuscript();
_flushCatnip();
_woodToBeams();
_mineralsToSlabs();
_scienceToCompendium();
};
var _fixStyles = function() {
var style = '';
$('body').append($(style));
}
var _init = function() {
var $a = $('Bot (' + (tStarted ? 'on' : 'off') + ')');
$a.on("click", function() {
tStarted = !tStarted;
_log((tStarted ? 'Started' : 'Stopped') + ' version ' + version);
$('#botbutton').text(tStarted ? 'Bot (on)' : 'Bot (off)');
})
$('#headerLinks .links-block').append(' | ').append($a);
$.extend($.expr[':'], {
startsWith: function(elem,match) {
return (elem.textContent || elem.innerText || "").indexOf(match[3]) == 0;
}
});
game.canBuy = _canBuy;
}
var tAuto, tStarted = true;
setTimeout(function() {
tAuto = setInterval(_auto, 1000);
_fixStyles();
_init();
_log('Started version ' + version);
}, 1000);
})();