forked from SiegeEngineers/aoe2techtree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
578 lines (529 loc) · 22.9 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
<meta charset="utf-8"/>
<title>Age of Empires II Tech Tree</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="container">
<div id="wrapper">
<div id="metainfo">
<div id="civinfo">
<div id="civtitle">
<select id="civselect" onchange="loadCiv()">
<option>Aztecs</option>
<option>Berbers</option>
<option>Britons</option>
<option>Bulgarians</option>
<option>Burmese</option>
<option>Byzantines</option>
<option>Celts</option>
<option>Chinese</option>
<option>Cumans</option>
<option>Ethiopians</option>
<option>Franks</option>
<option>Goths</option>
<option>Huns</option>
<option>Incas</option>
<option>Indians</option>
<option>Italians</option>
<option>Japanese</option>
<option>Khmer</option>
<option>Koreans</option>
<option>Lithuanians</option>
<option>Magyars</option>
<option>Malay</option>
<option>Malians</option>
<option>Mayans</option>
<option>Mongols</option>
<option>Persians</option>
<option>Portuguese</option>
<option>Saracens</option>
<option>Slavs</option>
<option>Spanish</option>
<option>Tatars</option>
<option>Teutons</option>
<option>Turks</option>
<option>Vietnamese</option>
<option>Vikings</option>
</select>
<img id="civlogo" height=52 width=52 data-transparent="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=">
</div>
<hr>
<p id="civtext"></p>
</div>
<div class="info">
<div class="info__margin">
Version: Age of Empires II DE Update <a href="https://www.ageofempires.com/news/aoe2de-update-36202/" target="_blank" title="Changelog">36202</a>
</div>
<div class="info__margin">
Contribute on <a href="https://github.com/SiegeEngineers/aoe2techtree" target="_blank">GitHub</a>
</div>
<div>
Made by hszemi, Anda, exterkamp, paulirish,<br>
with thanks to Jineapple, TriRem, pip, and NkoDragaš<br>
Item Metadata (cost, HP etc.) taken from <a href="https://github.com/HSZemi/aoe2dat" target="_blank">aoe2dat</a>
</div>
</div>
</div>
<div id="techtree">
<div id="helptext">
<div id="helptext__content"></div>
<div id="helptext__x_ref"></div>
</div>
</div>
</div>
</div>
<script src="js/svg.min.js"></script>
<script src="js/techtree.js"></script>
<script src="js/civs.js"></script>
<script type="text/javascript">
let tree;
let data = {};
let connections;
let parentConnections;
let connectionpoints;
let focusedNodeId = null;
const unitClasses = {
0: "Unused",
1: "Infantry",
2: "Turtle Ships",
3: "Base Pierce",
4: "Base Melee",
5: "War Elephants",
6: "Unused",
7: "Unused",
8: "Cavalry",
9: "Unused",
10: "Unused",
11: "<abbr title='(except Port)'>All Buildings</abbr>",
12: "Unused",
13: "Stone Defense",
14: "FE Predator Animals",
15: "Archers",
16: "Ships & Camels & Saboteurs",
17: "Rams",
18: "Trees",
19: "<abbr title='(except Turtle Ship)'>Unique Units</abbr>",
20: "Siege Weapons",
21: "Standard Buildings",
22: "Walls & Gates",
23: "FE Gunpowder Units",
24: "Boars",
25: "Monks",
26: "Castle",
27: "Spearmen",
28: "Cavalry Archers",
29: "Eagle Warriors",
30: "HD Camels",
31: "Anti-Leitis",
32: "Condottieros",
33: "Organ Gun Damage",
34: "Fishing Ships",
35: "Mamelukes",
36: "Heroes and Kings",
};
loadJson(function (response) {
data = JSON.parse(response);
tree = getDefaultTree();
connections = getConnections();
parentConnections = new Map(connections.map(([parent, child]) => [child, parent]));
connectionpoints = getConnectionPoints(tree);
let hash = window.location.hash.substr(1);
let capitalisedHash = hash.substring(0, 1).toUpperCase() + hash.substring(1).toLowerCase();
if (capitalisedHash in data.civ_names) {
document.getElementById('civselect').value = capitalisedHash;
}
const draw = SVG('techtree').size(tree.width, tree.height)
draw.rect(tree.width, tree.height).attr({fill: '#e7c28e'}).click(hideHelp);
draw.rect(tree.width, 6).attr({fill: '#4d3617'}).click(hideHelp).y(tree.height / 4);
draw.rect(tree.width, 6).attr({fill: '#4d3617'}).click(hideHelp).y(tree.height / 4 * 2);
draw.rect(tree.width, 6).attr({fill: '#4d3617'}).click(hideHelp).y(tree.height / 4 * 3);
const connectionGroup = draw.group().attr({id: 'connection_lines'});
for (let connection of connections) {
let from = connectionpoints.get(connection[0]);
let to = connectionpoints.get(connection[1]);
let intermediate_height = from.y + (tree.element_height * 2 / 3);
connectionGroup.polyline([from.x, from.y, from.x, intermediate_height, to.x, intermediate_height, to.x, to.y])
.attr({id: `connection_${connection[0]}_${connection[1]}`})
.addClass('connection')
.click(hideHelp);
}
for (let lane of tree.lanes) {
for (let r of Object.keys(lane.rows)) {
let row = lane.rows[r];
for (let caret of row) {
var item = draw.group().attr({id: caret.id}).addClass('node')
var rect = item.rect(caret.width, caret.height).attr({
fill: caret.type.colour,
id: `${caret.id}_bg`
}).move(caret.x, caret.y);
let name = formatName(caret.name);
var text = item.text(name.toString())
.font({family: 'Helvetica', size: 8})
.attr({fill: '#fff', 'text-anchor': 'middle', id: caret.id + '_text'})
.move(caret.x + caret.width / 2, caret.y + caret.height / 1.5);
var image_placeholder = item.rect(caret.width * 0.6, caret.height * 0.6)
.attr({fill: 'rgba(0,0,0,0.5)', id: caret.id + '_imgph'})
.move(caret.x + caret.width * 0.2, caret.y);
let prefix = 'img/';
var image = item.image(prefix + imagePrefix(caret.id) + '.png', caret.width * 0.6, caret.height * 0.6)
.attr({id: caret.id + '_img'})
.move(caret.x + caret.width * 0.2, caret.y);
var cross = item.polygon([1, 0, 3, 2, 5, 0, 6, 1, 4, 3, 6, 5, 5, 6, 3, 4, 1, 6, 0, 5, 2, 3, 0, 1])
.attr({fill: 'rgba(255,0,0,0.5)', id: caret.id + '_x'})
.addClass('cross')
.size(caret.width * 0.6, caret.height * 0.6)
.move(caret.x + caret.width * 0.2, caret.y);
var overlaytrigger = item.rect(caret.width, caret.height)
.attr({id: caret.id + '_overlay'})
.addClass('node__overlay')
.move(caret.x, caret.y)
.data({'type': caret.type.type, 'caret': caret, 'name': caret.name, 'id': caret.id})
.mouseover(function () {
highlightPath(caret.id);
})
.mouseout(resetHighlightPath)
.click(function () {
displayHelp(caret.id);
});
}
}
}
loadCiv();
// Make the xref badges
document.getElementById('helptext__x_ref').appendChild(createXRefBadges());
});
function imagePrefix(name) {
return name.replace('_copy','')
.replace("building_", "Buildings/")
.replace("unit_", "Units/")
.replace("tech_", "Techs/");
}
function loadCiv() {
const selectedCiv = document.getElementById('civselect').value;
civ(selectedCiv, tree);
if (selectedCiv in data.civ_helptexts) {
document.getElementById('civtext').innerHTML = data.strings[data.civ_helptexts[selectedCiv]];
document.getElementById('civlogo').src = `./img/Civs/${selectedCiv.toLowerCase()}.png`;
window.location.hash = selectedCiv;
} else {
document.getElementById('civtext').innerHTML = "";
document.getElementById('civlogo').src = document.getElementById('civlogo').dataset.transparent;
}
hideHelp();
}
function loadJson(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'data/data.json', true);
xobj.onreadystatechange = function () {
if (xobj.readyState === 4 && xobj.status === 200) {
// Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
callback(xobj.responseText);
}
};
xobj.send(null);
}
function resetHighlightPath() {
unhighlightPath();
if (focusedNodeId) {
highlightPath(focusedNodeId);
}
}
function unhighlightPath() {
SVG.select('.node.is-highlight, .connection.is-highlight')
.removeClass('is-highlight');
}
function highlightPath(caretId) {
recurse(caretId);
function recurse(caretId) {
SVG.get(caretId).addClass('is-highlight');
const parentId = parentConnections.get(caretId);
if (!parentId) return;
const line = SVG.get(`connection_${parentId}_${caretId}`);
if (line) {
// Move to the end of the <g> element so that it is drawn on top.
// Without this, the line would be highlighted, but other unhighlighted
// connection lines could be drawn on top, undoing the highlighting.
line.front().addClass('is-highlight');
}
recurse(parentId);
}
}
function displayHelp(caretId) {
focusedNodeId = caretId;
let helptextContent = document.getElementById("helptext__content");
let overlay = SVG.get(`${caretId}_overlay`);
let name = overlay.data('name');
let id = overlay.data('id').replace('_copy', '');
let caret = overlay.data('caret');
let type = overlay.data('type');
helptextContent.innerHTML = getHelpText(name, id, type);
styleXRefBadges(name, id, type);
positionHelptext(caret);
resetHighlightPath();
}
function hideHelp() {
focusedNodeId = null;
const helptext = document.getElementById("helptext");
helptext.style.display = "none";
resetHighlightPath();
}
function positionHelptext(caret) {
const helptext = document.getElementById("helptext");
helptext.style.display = "block";
positionHelptextBelow(caret, helptext)
|| positionHelptextAbove(caret, helptext)
|| positionHelptextToLeftOrRight(caret, helptext);
}
function positionHelptextBelow(caret, helptext) {
let top = caret.y + caret.height;
let helpbox = helptext.getBoundingClientRect();
if (top + helpbox.height > tree.height) {
return false;
}
let destX = caret.x - helpbox.width;
let techtree = document.getElementById('techtree');
if (destX < 0 || destX - techtree.scrollLeft < 0) {
destX = techtree.scrollLeft;
}
helptext.style.top = top + "px";
helptext.style.left = destX + 'px';
return true;
}
function positionHelptextAbove(caret, helptext) {
let helpbox = helptext.getBoundingClientRect();
let top = caret.y - helpbox.height;
if (top < 0) {
return false;
}
let destX = caret.x - helpbox.width;
let techtree = document.getElementById('techtree');
if (destX < 0 || destX - techtree.scrollLeft < 0) {
destX = techtree.scrollLeft;
}
helptext.style.top = top + "px";
helptext.style.left = destX + 'px';
return true;
}
function positionHelptextToLeftOrRight(caret, helptext) {
let helpbox = helptext.getBoundingClientRect();
let top = 0;
let destX = caret.x - helpbox.width;
let techtree = document.getElementById('techtree');
if (destX < 0 || destX - techtree.scrollLeft < 0) {
destX = caret.x + caret.width;
}
helptext.style.top = top + "px";
helptext.style.left = destX + 'px';
}
function getHelpText(name, id, type) {
let entitytype = 'buildings';
if (type === "UNIT" || type === "UNIQUEUNIT") {
entitytype = 'units';
}
if (type === "TECHNOLOGY") {
entitytype = 'techs';
}
const items = id.split('_', 1);
id = id.substring(items[0].length + 1);
let text = data.strings[data.data[entitytype][id]['LanguageHelpId']];
if (text === undefined) {
return "?";
}
let meta = data.data[entitytype][id];
if (meta !== undefined) {
text = text.replace(/‹cost›/, "Cost: " + cost(meta.Cost));
text = text.replace(/‹hp›/, "HP: " + meta.HP + ",");
text = text.replace(/‹attack›/, "Attack: " + meta.Attack + ",");
text = text.replace(/‹(A|a)rmor›/, "Armor: " + meta.MeleeArmor + ",");
text = text.replace(/‹(P|p)iercearmor›/, "Piercearmor: " + meta.PierceArmor + ",");
text = text.replace(/‹garrison›/, "Garrison: " + meta.GarrisonCapacity + ",");
text = text.replace(/‹range›/, "Range: " + meta.Range + ",");
text += minRangeIfDefined(meta.MinRange, "Min Range: ");
text += ifDefined(meta.LineOfSight, "Line of Sight: ");
text += ifDefined(meta.Speed, "Speed: ");
text += secondsIfDefined(meta.TrainTime, "Build Time: ");
text += secondsIfDefined(meta.ResearchTime, "Research Time: ");
text += ifDefined(meta.FrameDelay, "Frame Delay: ");
text += secondsIfDefined(meta.ReloadTime, "Reload Time: ");
text += accuracyIfDefined(meta.AccuracyPercent, "Accuracy: ");
text += arrayIfDefinedAndNonEmpty(meta.Attacks, "<br>Attacks: ");
text += arrayIfDefinedAndNonEmpty(meta.Armours, "<br>Armours: ");
} else {
console.log("No metadata found for " + name);
}
return text.substring(0, text.length - 1);
}
/**
* Create the Cross-Reference badges. This is done at load time in order to avoid re-making the
* badges at runtime per-click on a new unit.
*
* @return A container with buttons + images for each civ to be used in cross referencing.
*/
function createXRefBadges() {
let xRefLinks = document.createElement('div');
xRefLinks.id = "helptext__x_ref__container";
for (let civ of Object.keys(data.civ_names)) {
let xRefLink = document.createElement('button');
xRefLink.addEventListener('click', function() {
document.getElementById('civselect').value=civ;
loadCiv();
});
let xRefImage = document.createElement('img');
xRefImage.src = `./img/Civs/${civ.toLowerCase()}.png`;
xRefImage.title = `${civ}`;
xRefImage.id = `xRef__badge__${civ}`;
xRefImage.classList.add('xRef__badge')
xRefLink.appendChild(xRefImage);
xRefLinks.appendChild(xRefLink);
}
return xRefLinks;
}
/**
* Set on/off of all cross reference badges for a single unit.
*
* @param {string} name The name of the entity being cross-referenced.
* @param {string} id The id of the entity being cross-referenced.
* @param {string} type The type of the entity being cross-referenced.
*/
function styleXRefBadges(name, id, type) {
let horseDisabledAll = horseDisabledBuildings.map((id)=>`building_${id}`)
.concat(horseDisabledUnits.map((id)=>`unit_${id}`))
.concat(horseDisabledTechs.map((id)=>`tech_${id}`));
for (let civ of Object.keys(data.civ_names)) {
let xRefImage = document.getElementById(`xRef__badge__${civ}`);
// By default, assume that this unit can be built.
let found = true;
// Get the config objects
let enabled = civsConfig[`${civ}`]['enabled'];
let disabled = civsConfig[`${civ}`]['disabled'];
let unique = civsConfig[`${civ}`]['unique'];
// Make sure this civ exists
if (civsConfig[`${civ}`]) {
// Fast-Fail horses if civ disablesHorses
if (civsConfig[`${civ}`]['disableHorses'] &&
horseDisabledAll.includes(id)) {
found = false;
}
if (type === "UNIT") {
// There are 2 ways for a UNIT to be disabled.
// 1. The unit is disabled by default, and not
// specifically enabled in the config.
// 2. The unit is disabled in the specific config.
if (defaultDisabledUnits.map((id)=>`unit_${id}`).includes(id) &&
(!enabled ||
!enabled['units'] ||
!enabled['units'].map((id)=>`unit_${id}`).includes(id))) {
found = false;
} else if (disabled &&
disabled['units'] &&
disabled['units'].map((id)=>`unit_${id}`).includes(id)) {
found = false;
}
} else if (type === "UNIQUEUNIT") {
// There are 2 ways for a UNIQUEUNIT to be disabled.
// 1. The UNIQUEUNIT can be enabled in 'unique'.
// 2. The UNIQUEUNIT can be enabled in 'unit'.
// e.g. genitour
if ((!unique ||
!('unit_'+unique[0] === id || 'unit_'+unique[1] === id)) &&
(!enabled || !enabled['units'] ||
!enabled['units'].map((id)=>`unit_${id}`).includes(id))) {
found = false;
}
} else if (type === "TECHNOLOGY") {
// Technology is only ever disabled. See js/techtree.js#resetToDefault
// if there is a tech object in disabled, and this tech is in it
if (disabled['techs'] && disabled['techs'].map((id)=>`tech_${id}`).includes(id)) {
found = false;
}
}
else if (type === "BUILDING") {
// There are 2 ways for a BUILDING to be disabled.
// 1. The building is disabled by default, and not
// specifically enabled in the config.
// 2. The building is disabled in the specific config.
if (defaultDisabledBuildings.includes(id) &&
(!enabled ||
!enabled['buildings'] ||
!enabled['buildings'].map((id)=>`building_${id}`).includes(id))) {
found = false;
} else if (disabled &&
disabled['buildings'] &&
disabled['buildings'].map((id)=>`building_${id}`).includes(id)) {
found = false;
}
}
}
if (found) {
xRefImage.style.opacity = '100%';
} else {
xRefImage.style.opacity = '20%';
}
}
}
function ifDefined(value, prefix) {
if (value !== undefined) {
return " " + prefix + value + ",";
} else {
return "";
}
}
function secondsIfDefined(value, prefix) {
if (value !== undefined) {
return " " + prefix + value + "s,";
} else {
return "";
}
}
function accuracyIfDefined(value, prefix) {
if (value !== undefined && value < 100) {
return " " + prefix + value + "%,";
} else {
return "";
}
}
function minRangeIfDefined(value, prefix) {
if (value !== undefined && value > 0) {
return " " + prefix + value + ",";
} else {
return "";
}
}
function arrayIfDefinedAndNonEmpty(attacks, prefix) {
if (attacks === undefined || attacks.length < 1) {
return "";
} else {
const strings = [];
for (let attack of attacks) {
const amount = attack['Amount'];
const clazz = unitClasses[attack['Class']];
strings.push(`${amount} (${clazz})`);
}
return " " + prefix + strings.join(', ') + ",";
}
}
function cost(cost_object) {
let value = "";
if ("Food" in cost_object) {
value += " " + cost_object.Food + "F";
}
if ("Wood" in cost_object) {
value += " " + cost_object.Wood + "W";
}
if ("Gold" in cost_object) {
value += " " + cost_object.Gold + "G";
}
if ("Stone" in cost_object) {
value += " " + cost_object.Stone + "S";
}
return value;
}
</script>
</body>
</html>