Comments by Trixiz on scripts

1 comment

Comment on:
Travian3 Beyond - all language

May 7, 2008

I was wondering:

In a Danish version of this script we got a "new" feature on the NPC trade:
All units are listet and we can then click on, lets say Swordsman, and it will then list how many we could get of them and put in the numbers in the field under each resource.
The problem is that I cant get it to work by putting the section in to the new script(im got as much as no knowledge about script programming).

Thit code is this:

/**
* Add NPC-optimization
*/

var unitNames = new Array("Legion?r", "Pr?torianer", "Imperianer", "Equites Legati", "Equites Imperatoris", "Equites Caesaris", "Rambuk", "Brandkatapult", "Senator", "Bos?tter",
"K?llesvinger", "Spydk?mper", "?ksek?mper", "Spejder", "Paladin", "Teutonrytter", "Rambuk", "Katapult", "Stammef?rer", "Bos?tter",
"Falanks", "Sv?rdk?mper", "Spion", "Theutaterlyn", "Druide Rytter", "Haeduaner", "Rambuktr?", "Krigskatapult", "H?vding", "Bos?tter");

function getMin(numbers) {
if (numbers.length < 1) return;
var minSoFar = numbers[0];
for (var i = 1; i < numbers.length; i++) {
minSoFar = Math.min(numbers[i], minSoFar)
}
return minSoFar;
}

function createEventOptimizeForUnit(unitIndex) {
return function() {
var inputFields = find("//input[@class='fm']", XPList);
var unitCost = uc[unitIndex];

// Make int arrays out of the text arrays containing general data on current resources and capacity.
var t = new Array(parseInt(total[0]), parseInt(total[1]), parseInt(total[2]), parseInt(total[3]));
var a = new Array(parseInt(actual[0]), parseInt(actual[1]), parseInt(actual[2]), parseInt(actual[3]));

// Find the bottleneck capacity
var maxCapacityN = getMin(new Array(t[0]/unitCost[0], t[1]/unitCost[1], t[2]/unitCost[2], t[3]/unitCost[3]));

// Total resources in stock
var sumCurrentTotal = a[0] + a[1] + a[2] + a[3];

// Total resource cost of the unit
var sumUnitTotal = unitCost[0] + unitCost[1] + unitCost[2] + unitCost[3];

// The maximum number of units possible with no capacity restrictions
var maxCurrentN = sumCurrentTotal / sumUnitTotal;

// The maximum number of units that can be built is the minimum of the bottleneck capacity and the maximum possible N.
var N = parseInt(Math.min(maxCapacityN, maxCurrentN));

// Update the resource fields.
for (var i = 0; i < 4; i++) {
inputFields.snapshotItem(i).value = N*unitCost[i];
}

// Update the unit label with troop number
var nUnitLabel = document.getElementById("nTroopsField");
nUnitLabel.innerHTML = N + " " + unitNames[unitIndex - 1];
}
}

function addUnitOptimizationLinks() {
if (location.href.indexOf('&t=3') == -1) return;
// Add the links to optimize resources for a specific unit
var outerTBody = find("//table[@class='tbg']/tbody", XPFirst);

// Add the title
var titleRow = document.createElement("TR");
var titleTD = document.createElement("TD");
titleTD.innerHTML = "Maksimer antal mulige enheder:";
titleTD.setAttribute("colspan", 5);
titleTD.setAttribute("align", "center");
titleRow.appendChild(titleTD);
outerTBody.appendChild(titleRow);

// Add links
var raceTitles = new Array("Romere: ", "Germanere: ", "Gallere: ");
var nUnits = 10;
for (var i = 0; i < raceTitles.length; i++) {
var unitRow = document.createElement("TR");
var raceTitleTD = document.createElement("TD");
raceTitleTD.setAttribute("align", "left");
var unitTD = document.createElement("TD");
unitRow.appendChild(raceTitleTD);
unitRow.appendChild(unitTD);
outerTBody.appendChild(unitRow);

unitTD.setAttribute("colspan", 4);
var raceTitle = document.createTextNode(raceTitles[i]);
raceTitleTD.appendChild(raceTitle);

// Create table for troop icons
var troopIconTable = document.createElement("TABLE");
troopIconTable.setAttribute("width", "100%");
troopIconTable.style.tableLayout = "fixed";
unitTD.appendChild(troopIconTable);
var troopIconTBody = document.createElement("TBODY");
troopIconTable.appendChild(troopIconTBody)
var troopIconRow = document.createElement("TR");
troopIconTBody.appendChild(troopIconRow);
for (var j = i*nUnits + 1; j < (i+1)*nUnits +1; j++) {
var td = document.createElement("TD");
var anchor = document.createElement('A');
var imgUnitIcon = document.createElement('IMG');
imgUnitIcon.setAttribute("src", img("u/" + j + ".gif", false));
imgUnitIcon.setAttribute('alt', unitNames[j]);
anchor.appendChild(imgUnitIcon);
anchor.setAttribute("href", "javascript:void(0)");
anchor.addEventListener('click', createEventOptimizeForUnit(j), false);
anchor.setAttribute('onclick', "calculateRest()");
//unitTD.appendChild(anchor);
td.appendChild(anchor);
troopIconRow.appendChild(td);
}
}
var troopsBuiltRow = document.createElement("TR");
outerTBody.appendChild(troopsBuiltRow);

var titleTroopsBuiltTD = document.createElement("TD");
troopsBuiltRow.appendChild(titleTroopsBuiltTD);

var numberOfTroopsBuiltTD = document.createElement("TD");
numberOfTroopsBuiltTD.setAttribute("colspan", 4);
troopsBuiltRow.appendChild(numberOfTroopsBuiltTD);

titleTroopsBuiltTD.setAttribute("align", "left");
titleTroopsBuiltTD.innerHTML = "Antal: ";
numberOfTroopsBuiltTD.setAttribute("id", "nTroopsField");
numberOfTroopsBuiltTD.setAttribute("align", "left");
}

/**
* END NPC Optimization
*/

The name of the units is from a danish translation, so the names is in danish.

But is it something you can try to put in to the script or maybe make a feature like this. It save a LOT of time in the end because you dont have to calculate how many tropps you can make, by your self, but it can do it for you. :)