Skip to content

Instantly share code, notes, and snippets.

@amiuhle
Last active January 14, 2024 09:40
Show Gist options
  • Save amiuhle/61b0334997805ada0e99 to your computer and use it in GitHub Desktop.
Save amiuhle/61b0334997805ada0e99 to your computer and use it in GitHub Desktop.
Tribal Wars building duration calculation
/*
* Installation: Save as snippet in Chrome DevTools. (Crtl + Shift + i, Tab 'Sources', Sub-Tab 'Snippets', right-click, 'New')
*
* Usage:
* 1) Go to http://<your-world>.die-staemme.de/interface.php?func=get_building_info
* 2) Adjust the 'currentMain' and 'jobs' to match your main building at start and the build jobs you want to simulate.
* 3) Run snippet.
*/
(function() {
'use strict';
// what stage is your main building currently?
var currentMain = 10;
// what do you want to build?
var jobs = [
['main', 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
['smith', /*5, 6, 7,*/ 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
];
// ---- No changes required beyond this line
var forEach = Array.prototype.forEach,
reduce = Array.prototype.reduce,
slice = Array.prototype.slice;
var config = document.querySelector('config');
var buildings = {};
forEach.call(config.children, function(building) {
buildings[building.tagName] = reduce.call(building.children, function(memo, child) {
memo[child.tagName] = parseFloat(child.textContent);
return memo;
}, {});
});
var calc = function(hg, level, buildTime, buildTimeFactor, wood, woodFactor, stone, stoneFactor, iron, ironFactor) {
// http://forum.tribalwars.us/showthread.php?389-guide-Building-Formulas
var exponent = Math.max(-13, (level - 1 - 14 / (level-1)));
var baseTime = buildTime * 1.18 * Math.pow(buildTimeFactor, exponent);
var time = baseTime * Math.pow(1.05, -hg);
return [Math.round(time), Math.round(wood * Math.pow(woodFactor, level - 1)), Math.round(stone * Math.pow(stoneFactor, level - 1)), Math.round(iron * Math.pow(ironFactor, level - 1))];
};
var queue = [];
var queueJobs = function(buildingName) {
var building = buildings[buildingName];
var stages = slice.call(arguments, 1);
stages.forEach(function(stage) {
queue.push([buildingName, stage].concat(calc(currentMain, stage, building.build_time, building.build_time_factor, building.wood, building.wood_factor, building.stone, building.stone_factor, building.iron, building.iron_factor)));
if(buildingName === 'main') {
currentMain = stage;
}
});
};
jobs.forEach(function(job) {
queueJobs.apply(this, job);
});
var add = function(index) {
return function(a, b) {
return a + b[index];
};
};
var pad = function(value) {
value = parseInt(value, 10);
return value < 10 ? '0' + value : '' + value;
}
var formatTime = function(seconds) {
var hours = Math.floor(seconds / 3600);
var minutes = Math.floor((seconds % 3600) / 60);
var seconds = seconds % 60;
return pad(hours) + ':' + pad(minutes) + ':' + pad(seconds);
}
console.table(queue.map(function(row) {
row = row.slice(0);
var seconds = Math.round(row[2]);
row[2] = formatTime(seconds);
return row;
}));
var totalSeconds = queue.reduce(add(2), 0);
var totalWood = queue.reduce(add(3), 0);
var totalStone = queue.reduce(add(4), 0);
var totalIron = queue.reduce(add(5), 0);
console.log('Total', formatTime(totalSeconds), totalWood, totalStone, totalIron);
var totalHours = totalSeconds / 3600;
console.log('Avg per hour', Math.round(totalWood / totalHours), Math.round(totalStone / totalHours), Math.round(totalIron / totalHours));
return '... done!';
})();
@lucianoshl
Copy link

and calculate of the population?

@Slav15
Copy link

Slav15 commented Jan 4, 2023

when I run it on a polish server it calculates the wrong time, any idea what could be wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment