var units = [
[	{off:40, def_i:35, def_c:50, speed:6, cost:[120,100,150,30], cu:1, time:2000, cap:50, name:"легионер", type:'i', mask:'5', rs_time:7800},
	{off:30, def_i:65, def_c:35, speed:5, cost:[100,130,160,70], cu:1, time:2200, cap:20, name:"преторианец", type:'i', mask:'2', rs_time:8400},
	{off:70, def_i:40, def_c:25, speed:7, cost:[150,160,210,80], cu:1, time:2400, cap:50, name:"империанец", type:'i', mask:'1', rs_time:9000},
	{off:0, def_i:20, def_c:10, speed:16, cost:[140,160,20,40], cu:2, time:1700, cap:0, name:"конный разведчик", type:'c', mask:'48', rs_time:6900},
	{off:120, def_i:65, def_c:50, speed:14, cost:[550,440,320,100], cu:3, time:3300, cap:100, name:"конница императора", type:'c', mask:'1', rs_time:11700},
	{off:180, def_i:80, def_c:105, speed:10, cost:[550,640,800,180], cu:4, time:4400, cap:70, name:"конница Цезаря", type:'c', mask:'1', rs_time:15000},
	{off:60, def_i:30, def_c:75, speed:4, cost:[900,360,500,70], cu:3, time:4600, cap:0, name:"таран", type:'i', mask:'65', rs_time:15600},
	{off:75, def_i:60, def_c:10, speed:3, cost:[950,1350,600,90], cu:6, time:9000, cap:0, name:"огненная катапульта", type:'i', mask:'65', rs_time:28800},
	{off:50, def_i:40, def_c:30, speed:4, cost:[30750,27200,45000,37500], cu:5, time:90700, cap:0, name:"сенатор", type:'i', mask:'0', rs_time:24475, loyality:[20,30]},
	{off:0, def_i:80, def_c:80, speed:5, cost:[5800,5300,7200,5500], cu:1, time:26900, cap:3000, name:"поселенец", type:'i', mask:'0', rs_time:0}
],
[	{off:40, def_i:20, def_c:5, speed:7, cost:[95,75,40,40], cu:1, time:900, cap:60, name:"дубинщик", type:'i', mask:'1', rs_time:4500},
	{off:10, def_i:35, def_c:60, speed:7, cost:[145,70,85,40], cu:1, time:1400, cap:40, name:"копьеносец", type:'i', mask:'4', rs_time:6000},
	{off:60, def_i:30, def_c:30, speed:6, cost:[130,120,170,70], cu:1, time:1500, cap:50, name:"топорщик", type:'i', mask:'1', rs_time:6300},
	{off:0, def_i:10, def_c:5, speed:9, cost:[160,100,50,50], cu:1, time:1400, cap:0, name:"скаут", type:'c', mask:'48', rs_time:6000},
	{off:55, def_i:100, def_c:40, speed:10, cost:[370,270,290,75], cu:2, time:3000, cap:110, name:"паладин", type:'c', mask:'2', rs_time:10800},
	{off:150, def_i:50, def_c:75, speed:9, cost:[450,515,480,80], cu:3, time:3700, cap:80, name:"тевтонская конница", type:'c', mask:'1', rs_time:12900},
	{off:65, def_i:30, def_c:80, speed:4, cost:[1000,300,350,70], cu:3, time:4200, cap:0, name:"таран", type:'i', mask:'65', rs_time:14400},
	{off:50, def_i:60, def_c:10, speed:3, cost:[900,1200,600,60], cu:6, time:9000, cap:0, name:"катапульта", type:'i', mask:'65', rs_time:28800},
	{off:40, def_i:60, def_c:40, speed:4, cost:[35500,26600,25000,27200], cu:4, time:70500, cap:0, name:"вождь", type:'i', mask:'0', rs_time:19425, loyality:[20,25]},
	{off:10, def_i:80, def_c:80, speed:5, cost:[7200,5500,5800,6500], cu:1, time:31000, cap:3000, name:"поселенец", type:'i', mask:'0', rs_time:0}
],
[	{off:15, def_i:40, def_c:50, speed:7, cost:[100,130,55,30], cu:1, time:1300, cap:35, name:"фаланга", type:'i', mask:'6', rs_time:5700},
	{off:65, def_i:35, def_c:20, speed:6, cost:[140,150,185,60], cu:1, time:1800, cap:45, name:"мечник", type:'i', mask:'1', rs_time:7200},
	{off:0, def_i:20, def_c:10, speed:17, cost:[170,150,20,40], cu:2, time:1700, cap:0, name:"следопыт", type:'c', mask:'48', rs_time:6900},
	{off:90, def_i:25, def_c:40, speed:19, cost:[350,450,230,60], cu:2, time:3100, cap:75, name:"тевтатский гром", type:'c', mask:'1', rs_time:11100},
	{off:45, def_i:115, def_c:55, speed:16, cost:[360,330,280,120], cu:2, time:3200, cap:35, name:"друид-всадник", type:'c', mask:'2', rs_time:11400},
	{off:140, def_i:50, def_c:165, speed:13, cost:[500,620,675,170], cu:3, time:3900, cap:65, name:"эдуйская конница", type:'c', mask:'5', rs_time:13500},
	{off:50, def_i:30, def_c:105, speed:4, cost:[950,555,330,75], cu:3, time:5000, cap:0, name:"таран", type:'i', mask:'65', rs_time:16800},
	{off:70, def_i:45, def_c:10, speed:3, cost:[960,1450,630,90], cu:6, time:9000, cap:0, name:"требучет", type:'i', mask:'65', rs_time:28800},
	{off:40, def_i:50, def_c:50, speed:5, cost:[30750,45400,31000,37500], cu:4, time:90700, cap:0, name:"предводитель", type:'i', mask:'0', rs_time:24475, loyality:[20,25]},
	{off:0, def_i:80, def_c:80, speed:5, cost:[5500,7000,5300,4900], cu:1, time:22700, cap:3000, name:"поселенец", type:'i', mask:'0', rs_time:0}
],
[	{off:10, def_i:25, def_c:20, speed:20, cost:[0,0,0,100], cu:1, time:0, cap:0, name:"Крыса", type:'i', mask:'0', rs_time:1800},
	{off:20, def_i:35, def_c:40, speed:20, cost:[0,0,0,0], cu:1, time:0, cap:0, name:"Паук", type:'i', mask:'0', rs_time:1800},
	{off:60, def_i:40, def_c:60, speed:20, cost:[0,0,0,0], cu:1, time:0, cap:0, name:"Змея", type:'i', mask:'0', rs_time:1800},
	{off:80, def_i:66, def_c:50, speed:20, cost:[0,0,0,0], cu:1, time:0, cap:0, name:"Летучая мышь", type:'i', mask:'0', rs_time:1800},
	{off:50, def_i:70, def_c:33, speed:20, cost:[0,0,0,0], cu:2, time:0, cap:0, name:"Кабан", type:'i', mask:'0', rs_time:1800},
	{off:100, def_i:80, def_c:70, speed:20, cost:[0,0,0,0], cu:2, time:0, cap:0, name:"Волк", type:'i', mask:'0', rs_time:1800},
	{off:250, def_i:140, def_c:200, speed:20, cost:[0,0,0,0], cu:3, time:0, cap:0, name:"Медвед", type:'i', mask:'0', rs_time:1800},
	{off:450, def_i:380, def_c:240, speed:20, cost:[0,0,0,0], cu:3, time:0, cap:0, name:"Крокодил", type:'i', mask:'0', rs_time:1800},
	{off:200, def_i:170, def_c:250, speed:20, cost:[0,0,0,0], cu:3, time:0, cap:0, name:"Тигр", type:'i', mask:'0', rs_time:1800},
	{off:600, def_i:440, def_c:520, speed:20, cost:[0,0,0,0], cu:5, time:0, cap:0, name:"Слон", type:'i', mask:'0', rs_time:0}
],
[	{off:20, def_i:35, def_c:50, speed:0, cost:[0,0,0,0], cu:1, time:0, cap:0, name:"Пикейщик", type:'i', mask:'', rs_time:1800},
	{off:65, def_i:30, def_c:10, speed:0, cost:[0,0,0,0], cu:1, time:0, cap:0, name:"Воин с шипами", type:'i', mask:'', rs_time:1800},
	{off:100, def_i:90, def_c:75, speed:0, cost:[0,0,0,0], cu:1, time:0, cap:0, name:"Стражник", type:'i', mask:'', rs_time:1800},
	{off:0, def_i:10, def_c:0, speed:0, cost:[0,0,0,0], cu:1, time:0, cap:0, name:"Хищная птица", type:'i', mask:'', rs_time:1800},
	{off:155, def_i:80, def_c:50, speed:0, cost:[0,0,0,0], cu:2, time:0, cap:0, name:"Всадник с топором", type:'c', mask:'', rs_time:1800},
	{off:170, def_i:140, def_c:80, speed:0, cost:[0,0,0,0], cu:3, time:0, cap:0, name:"Рыцарь Натаров", type:'c', mask:'', rs_time:1800},
	{off:250, def_i:120, def_c:150, speed:0, cost:[0,0,0,0], cu:6, time:0, cap:0, name:"Боевой слон", type:'i', mask:'', rs_time:1800},
	{off:60, def_i:45, def_c:10, speed:0, cost:[0,0,0,0], cu:5, time:0, cap:0, name:"Баллиста", type:'i', mask:'', rs_time:1800},
	{off:80, def_i:50, def_c:50, speed:0, cost:[0,0,0,0], cu:0, time:0, cap:0, name:"Император Натаров", type:'i', mask:'', rs_time:1800, loyality:[200,200]},
	{off:30, def_i:40, def_c:40, speed:0, cost:[0,0,0,0], cu:0, time:0, cap:0, name:"Поселенец", type:'i', mask:'', rs_time:0}
]
];
units.each(function(raceUnits, race){
	raceUnits.each(function(unit, idx){
		unit.idx = idx;
		unit.race = race;
	});
});
var Combat = {
	upgCoeff: new Array(21),
	_std_upg: function (stat, cu, lvl) {
		return stat + (stat + 300*cu/7) * this.upgCoeff[lvl];
	},
	_cat_upg: function (stat, cu, lvl) {
		return Math.round(200 * Math.pow(1.0205, lvl)) / 10;
	},
	upgrade: function(unit, type, lvl) {
		switch (type) {
			case 'off':		return this._std_upg(unit.off,		unit.cu, lvl); break;
			case 'def_i':	return this._std_upg(unit.def_i,		unit.cu, lvl); break;
			case 'def_c':	return this._std_upg(unit.def_c,	unit.cu, lvl); break;
			case 'off_s':	return this._std_upg(35,				unit.cu, lvl); break;
			case 'def_s':	return this._std_upg(20,				unit.cu, lvl); break;
			case 'dmlsh':	return this._cat_upg(20,				unit.cu, lvl); break;
		default:
			return 0;
		}
	}
}

for (i=0; i<=20; i++) Combat.upgCoeff[i] = Math.pow(1.007, i) - 1;
	
var stdCost = {
	b: [100, 100, 200, 160],
	k: [6, 4, 8, 6],
	t: 3
}
var adminCost = {
	k: [0.5, 0.5, 0.8, 0.6],
	b: [500, 200, 400, 160],
	t: 0.25
}
var nullCost = {
	k: 0,
	b: 0,
	t: 0
}
var researchCostCoeffs = [stdCost, stdCost, stdCost, stdCost, stdCost, stdCost, stdCost, stdCost, adminCost, nullCost];

function unitResearchCost() {
	var _k = k, _b = b;
	if (this.loyality) {
		_k = [0.5, 0.5, 0.8, 0.6];
		_b = [500, 200, 400, 160];
	}
	return this.cost.affine(_k, _b);
}

function unitUpgradeCost(lvl) {
	if (typeof this.upgCost == "undefined") {
		var u = this.cu;
		this.upgCost = this.cost.map(function(val, idx){
			return (val * 7 + b[idx]) / u;
		});
	}
	var c = Math.pow(lvl, 0.8);
	return this.upgCost.map(function(val){
		return Round5(val * c);
	});
}

var CC = 450;

function rebuildUpgs(_unit_stats) {
	var result = Array(21);
	result[0] = Array(4);
	result[1] = Array(4);
	for(r=0; r<4; r++) {
		result[0][r] = k[r] * _unit_stats.cost[r] + b[r];
		result[1][r] = (_unit_stats.cost[r] * 7 + b[r]) / _unit_stats.cu;
	}
	for(l=20; l>0; l--) {
		if(!result[l]) result[l] = Array(4);
		for(r=0; r<4; r++) {
			result[l][r] = Round5(result[1][r] * coeff[l]);
		}
	}
	return result;
}

function rebuildUpgTime(_unit_stats, _smith_lvl, _spd) {
	var result = Array(21);
	result[0] = timeS2R(_unit_stats.rs_time) / _spd;
	result[1] = result[0] * Math.pow(0.964, _smith_lvl-1);
	for(l=2; l<=20; l++) {
		result[l] = result[1] * coeff[l];
	}
	return result;
}

function cost_upg(_unit_stats, lvl) {
	var result = Array(4);
	for(var r=0; r<4; r++) {
		result[r] = Round5(
			coeff[lvl] * (_unit_stats.cost[r] * 7 + b[r])
			/	_unit_stats.cu
		);
	}
	return result;
}

function calc_at(_unit_stats, type, lvl) {
	return sum(cost_upg(_unit_stats, lvl+1)) * calc_upg(_unit_stats, type, lvl) /
	((sum(_unit_stats.cost) + CC*_unit_stats.cu) * (calc_upg(_unit_stats, type, lvl+1) - calc_upg(_unit_stats, type, lvl)));
}

function rebuildUnitStats(_unit_stats) {
	var result = Array(21);
	var j;
	for (lvl = 0; lvl <= 20; lvl++) {
		result[lvl] = [];
		if (_unit_stats.mask & 0x01) result[lvl].push(calc_upg(_unit_stats, 'off', lvl));
		if (_unit_stats.mask & 0x02) result[lvl].push(calc_upg(_unit_stats, 'def_i', lvl));
		if (_unit_stats.mask & 0x04) result[lvl].push(calc_upg(_unit_stats, 'def_c', lvl));
		if (_unit_stats.mask & 0x10) result[lvl].push(calc_upg(_unit_stats, 'scan', lvl));
		if (_unit_stats.mask & 0x20) result[lvl].push(calc_upg(_unit_stats, 'def_s', lvl));
		if (_unit_stats.mask & 0x40) result[lvl].push(calc_upg(_unit_stats, 'dmlsh', lvl));
	}
	return result;
}

function rebuildUnitAt(_unit_stats) {
	var result = Array(20);
	var j;
	for (lvl = 0; lvl < 20; lvl++) {
		result[lvl] = [];
		if (_unit_stats.mask & 0x01) result[lvl].push(calc_at(_unit_stats, 'off', lvl));
		if (_unit_stats.mask & 0x02) result[lvl].push(calc_at(_unit_stats, 'def_i', lvl));
		if (_unit_stats.mask & 0x04) result[lvl].push(calc_at(_unit_stats, 'def_c', lvl));
		if (_unit_stats.mask & 0x10) result[lvl].push(calc_at(_unit_stats, 'scan', lvl));
		if (_unit_stats.mask & 0x20) result[lvl].push(calc_at(_unit_stats, 'def_s', lvl));
		if (_unit_stats.mask & 0x40) result[lvl].push(calc_at(_unit_stats, 'dmlsh', lvl));
	}
	return result;
}

function calcUnits(unit_cost, res) {
	var sum = 0;
	var tc = 0;
	var r;
	for (r = 0; r < 4; r++) {
		tc += unit_cost[r];
		sum += res[r]; 
	}
	var result = {
		nr: sum / tc,
		res: [0,0,0,0]
	}
	var accum = 0;
	for (r = 0; r < 4; r++) {
		accum += unit_cost[r] * result.nr;
		result.res[r] = Math.round(accum);
	}
	r = 3; while (r > 0) { result.res[r] -= result.res[--r]; }
	return result;
}