Greasy Fork

Greasy Fork is available in English.

Kittens tools

Kittens tools (visual)

目前为 2018-12-11 提交的版本,查看 最新版本

// ==UserScript==
// @name         Kittens tools
// @namespace    http://bloodrizer.ru/games/kittens/
// @version      1.049
// @description  Kittens tools (visual)
// @author       Anton
// @match        http://bloodrizer.ru/games/kittens/
// ==/UserScript==

(function() {
	'use strict';

	var $ = jQuery;

	var _Helpers = {
        log: function(message) {
            var mes = 'BOT: ' + message;
            if (game && game.msg && game.ui) {
                game.msg(mes, 'msg');
                game.ui.renderConsoleLog();
            }
        },
        isResourceUnlocked: function(res) {
            return game.resPool.get(res).unlocked;
        },
        getBotVersion: function () {
            return typeof GM_info == 'function' ? GM_info().script.version :
                (typeof GM_info == 'object' ? GM_info.script.version : '?');
        },
        getMinCraft: function(res) {
            // craft no more than 2% of possible craft
            var allCount = game.workshop.getCraftAllCount(res);
            var ratioCount = Math.floor(allCount*0.02);
            return ratioCount < 1 ? 1 : ratioCount;
        },
        canBuyBuilding: 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;
        },
        getCountKittensForPromote: 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 _BotActions = {
        craftAll: function(res) {
            if (_Helpers.isResourceUnlocked(res)) {
                _Helpers.log("Crafting " + res);
                game.craftAll(res);
            }
        },
        collectAstronomy: function() {
            if (game.calendar.observeRemainingTime > 0) {
                if (typeof game.calendar.observeHandler === 'function') {
                    game.calendar.observeHandler();
                }
            }
        },
        allCatnipToWood: function() {
            var catnip = game.resPool.get("catnip");
            if (catnip.value >= catnip.maxValue) {
                _Helpers.log('Catnip to Wood');
                game.craftAll('wood');
            }
        },
        collectFaith: function() {
            var faith = game.resPool.get("faith");
            if (faith.value >= faith.maxValue) {
                _Helpers.log('Praise');
                game.religion.praise();
            }
        },
        sendAllHunters: function() {
            var manpower = game.resPool.get("manpower");
            if (manpower.value >= manpower.maxValue) {
                _Helpers.log('Sending hunters');
                game.village.huntAll();
                _BotActions.craftAll('parchment');
            }
        },
        ironToSteelOrPlates: function() {
            var iron = game.resPool.get("iron");
            var coal = game.resPool.get("coal");
            if (iron.value >= iron.maxValue || coal.value >= coal.maxValue) {
                var minPlate = _Helpers.getMinCraft('plate');
                if (coal.value >= 100 && iron.value >= 100 && _Helpers.isResourceUnlocked('steel')) {
                    _Helpers.log('Iron to Steel x ALL');
                    game.craftAll('steel');
                } else if (iron.value >= (125 * minPlate) && _Helpers.isResourceUnlocked('plate')) {
                    _Helpers.log('Iron to Plate x ' + minPlate);
                    game.craft('plate', minPlate);
                }
            }
        },
        woodToBeams: function() {
            var wood = game.resPool.get("wood");
            if (wood.value >= wood.maxValue && _Helpers.isResourceUnlocked('beam')) {
                var minVal = _Helpers.getMinCraft('beam');
                _Helpers.log('Wood to Beam x ' + minVal);
                game.craft('beam', minVal);
            }
        },
        mineralsToSlabs: function() {
            var minerals = game.resPool.get("minerals");
            if (minerals.value >= minerals.maxValue && _Helpers.isResourceUnlocked('slab')) {
                var minVal = _Helpers.getMinCraft('slab');
                _Helpers.log('Minerals to Slab x ' + minVal);
                game.craft('slab', minVal);
            }
        },
        cultureToManuscript: function() {
            var culture = game.resPool.get("culture");
            var parchment = game.resPool.get("parchment");
            var minVal = _Helpers.getMinCraft('manuscript');
            if (culture.value >= culture.maxValue && _Helpers.isResourceUnlocked('manuscript') && culture.value >= (400 * minVal) && parchment.value >= (25 * minVal)) {
                _Helpers.log('Culture to Manuscript x ' + minVal);
                game.craft('manuscript', minVal);
            }
        },
        makeABuy: function(itemName) {
            var btn = $('.bldGroupContainer').find('div.btnContent').find('span').filter(function(){
                var t = $(this).text();
                return t.indexOf(itemName + " (") === 0 || t === itemName;
            });
            if (btn&& btn.length === 1) {
                _Helpers.log('Autobuy ' + itemName);
                btn.click();
            }
        },
        promoteKittens: function() {
            var gold = game.resPool.get("gold");
            if (!gold.unlocked) return;
            if (gold.value >= 15 && gold.value >= gold.maxValue && _Helpers.getCountKittensForPromote() > 0) {
                game.village.promoteKittens();
            }
        },
        scienceToCompendium: function() {
            var science = game.resPool.get("science");
            var manuscript = game.resPool.get("manuscript");
            var minVal = _Helpers.getMinCraft('compedium');
            if (science.value >= science.maxValue && _Helpers.isResourceUnlocked('compedium') && manuscript.value >= (50 * minVal) && science.value >= (10000 * minVal)) {
                _Helpers.log('Science + Manuscript to Compendium x ' + minVal);
                game.craft('compedium', minVal);
            }
        }
	};

	var _BotUI = {
        fixFontSize: function() {
            var $midColumn = $('#midColumn');
            var $rightColumn = $('#rightColumn');
            var fnt1 = $('#leftColumn').css('font-size');
            var fnt2 = $midColumn.css('font-size');
            var fnt3 = $rightColumn.css('font-size');
            if (fnt2 !== fnt1 || fnt3 !== fnt1) {
                _Helpers.log('Fixing font size');
                $midColumn.css('font-size', fnt1);
                $rightColumn.css('font-size', fnt1);
            }
        },
        fixStyles: function() {
            var style = '<style type="text/css">' +
                '.modern .btnContent, .btn.bldEnabled.modern div.btnContent, .btn.bldlackResConvert.modern div.btnContent {padding: 5px 0 5px 10px;}'+
                '.btn.modern a {padding: 5px 6px 5px 6px !important;margin:-5px 0;}'+
                '</style>';
            $('body').append($(style));
        },
        addBotButton: function () {
            var $a = $('<a href="#" id="botbutton">Bot (' + (_BotLogic.isAutoLogicStarted ? 'on' : 'off') + ')</a>');
            $a.on("click", function() {
                _BotLogic.isAutoLogicStarted = !_BotLogic.isAutoLogicStarted;
                _Helpers.log((_BotLogic.isAutoLogicStarted ? 'Started' : 'Stopped') + ' version ' + _Helpers.getBotVersion());
                $('#botbutton').text(_BotLogic.isAutoLogicStarted ? 'Bot (on)' : 'Bot (off)');
            });
            $('#headerLinks .links-block').append(' | ').append($a);
        }
    };

	var _BotLogic = {
        tAutoLogic: undefined,
        isAutoLogicStarted: true,
        autoBuyItem: function(bldName) {
            var bld = game.bld.get(bldName);
            if (bld.unlocked && _Helpers.canBuyBuilding(bldName)) {
                var itemName = bld.stages && bld.stages.length > 0 ? bld.stages[bld.stage].label : bld.label;
                _BotActions.makeABuy(itemName);
            }
        },
        autoBuyAll: function() {
            _BotLogic.autoBuyItem('field');
            _BotLogic.autoBuyItem('pasture');
            _BotLogic.autoBuyItem('unicornPasture');
            _BotLogic.autoBuyItem('hut');
            _BotLogic.autoBuyItem('logHouse');
            _BotLogic.autoBuyItem('workshop');
            _BotLogic.autoBuyItem('aqueduct');
            _BotLogic.autoBuyItem('library');
            _BotLogic.autoBuyItem('academy');
            _BotLogic.autoBuyItem('barn');
            _BotLogic.autoBuyItem('mine');
            _BotLogic.autoBuyItem('lumberMill');
            _BotLogic.autoBuyItem('temple');
            _BotLogic.autoBuyItem('tradepost');
            _BotLogic.autoBuyItem('warehouse');
            _BotLogic.autoBuyItem('chapel');
            _BotLogic.autoBuyItem('amphitheatre');
            _BotLogic.autoBuyItem('smelter');
        },
        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;
                });
                if (btn && btn.length) btn.click();
            }
        },
        autoLogic: function() {
            _BotUI.fixFontSize();

            _BotActions.collectAstronomy();
            if (!_BotLogic.isAutoLogicStarted) return;
            _BotLogic.autoClick();
            _BotLogic.autoBuyAll();
            _BotActions.promoteKittens();
            _BotActions.collectFaith();
            _BotActions.ironToSteelOrPlates();
            _BotActions.sendAllHunters();
            _BotActions.cultureToManuscript();
            _BotActions.allCatnipToWood();
            _BotActions.woodToBeams();
            _BotActions.mineralsToSlabs();
            _BotActions.scienceToCompendium();
        }
	};

    setTimeout(function() {
        _BotLogic.tAutoLogic = setInterval(_BotLogic.autoLogic, 1000);
        _BotUI.fixStyles();
        _BotUI.addBotButton();
        _Helpers.log('Started version ' + _Helpers.getBotVersion());
    }, 1000);
})();