forked from NiHoel/Anno1800Calculator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AnnoCalculator.js
706 lines (573 loc) · 23.8 KB
/
AnnoCalculator.js
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
products = new Map();
assetsMap = new Map();
view = {
regions: [],
populationLevels: [],
factories: [],
categories: [],
workforce: [],
buildingMaterialsNeeds: [],
multiFactoryProducts: [],
settings: {
language: ko.observable(navigator.language.startsWith("de") ? "german" : "english")
},
texts: {}
};
class NamedElement {
constructor(config) {
$.extend(this, config);
this.locaText = this.locaText || {}
this.name = ko.computed(() => {
let text = this.locaText[view.settings.language()];
if (text)
return text;
text = this.locaText["english"];
return text ? text : config.name;
})
}
}
class Region extends NamedElement { }
class Option extends NamedElement {
constructor(config) {
super(config);
this.checked = ko.observable(false);
this.visible = !!config;
}
}
class Factory extends NamedElement {
constructor(config) {
super(config);
if (config.region)
this.region = assetsMap.get(config.region);
this.amount = ko.observable(0);
this.percentBoost = ko.observable(100);
this.boost = ko.computed(() => parseInt(this.percentBoost()) / 100);
this.demands = new Set();
this.buildings = ko.computed(() => parseFloat(this.amount()) / this.tpmin / this.boost());
this.existingBuildings = ko.observable(0);
this.workforceDemand = this.getWorkforceDemand();
this.buildings.subscribe(val => this.workforceDemand.updateAmount(val));
}
getInputs() {
return this.inputs || [];
}
getOutputs() {
return this.outputs || [];
}
referenceProducts() {
this.getInputs().forEach(i => i.product = assetsMap.get(i.Product));
this.getOutputs().forEach(i => i.product = assetsMap.get(i.Product));
this.product = this.getProduct();
if (!this.icon)
this.icon = this.product.icon;
}
getProduct() {
return this.getOutputs()[0].product;
}
getWorkforceDemand() {
for (let m of this.maintenances) {
let a = assetsMap.get(m.Product);
if (a instanceof Workforce)
return new WorkforceDemand($.extend({ factory: this, workforce: a }, m));
}
}
getRegionExtendedName() {
if (!this.region || this.product.factories.length <= 1)
return this.name;
return `${this.name()} (${this.region.name()})`;
}
updateAmount() {
var sum = 0;
this.demands.forEach(d => sum += d.amount());
this.amount(sum);
}
add(demand) {
this.demands.add(demand);
this.updateAmount();
}
remove(demand) {
this.demands.delete(demand);
this.updateAmount();
}
incrementBuildings() {
if (this.buildings() <= 0 || parseInt(this.percentBoost()) <= 1)
return;
var minBuildings = Math.ceil(this.buildings() * parseInt(this.percentBoost()) / (parseInt(this.percentBoost()) - 1));
let nextBoost = Math.ceil(parseInt(this.percentBoost()) * this.buildings() / minBuildings)
this.percentBoost(Math.min(nextBoost, parseInt(this.percentBoost()) - 1));
}
decrementBuildings() {
let nextBuildings = Math.floor(this.buildings());
if (nextBuildings <= 0)
return;
if (this.buildings() - nextBuildings < 0.01)
nextBuildings = Math.floor(nextBuildings - 0.01);
var nextBoost = Math.ceil(100 * this.boost() * this.buildings() / nextBuildings);
if (nextBoost - parseInt(this.percentBoost()) < 1)
nextBoost = parseInt(this.percentBoost()) + 1;
this.percentBoost(nextBoost);
}
incrementPercentBoost() {
this.percentBoost(parseInt(this.percentBoost()) + 1);
}
decrementPercentBoost() {
this.percentBoost(parseInt(this.percentBoost()) - 1);
}
}
class Product extends NamedElement {
constructor(config) {
super(config);
this.amount = ko.observable(0);
this.factories = this.producers.map(p => assetsMap.get(p));
this.fixedFactory = ko.observable(null);
if (this.producers) {
this.amount = ko.computed(() => this.factories.map(f => f.amount()).reduce((a, b) => a + b));
}
}
}
class Demand extends NamedElement {
constructor(config) {
super(config);
this.amount = ko.observable(0);
this.product = assetsMap.get(this.guid);
this.factory = ko.observable(config.factory);
if (this.product) {
this.updateFixedProductFactory(this.product.fixedFactory());
this.product.fixedFactory.subscribe(f => this.updateFixedProductFactory(f));
if (this.consumer)
this.consumer.factory.subscribe(() => this.updateFixedProductFactory(this.product.fixedFactory()));
this.demands = this.factory().getInputs().map(input => {
let d = new Demand({ guid: input.Product, consumer: this });
this.amount.subscribe(val => d.updateAmount(val * input.Amount));
return d;
});
this.amount.subscribe(val => {
this.factory().updateAmount();
});
this.buildings = ko.computed(() => parseFloat(this.amount()) / this.factory().tpmin / this.factory().boost());
}
}
updateFixedProductFactory(f) {
if (f == null) {
if (this.consumer) { // find factory in the same region as consumer
let region = this.consumer.factory().region;
if (region) {
for (let fac of this.product.factories) {
if (fac.region === region) {
f = fac;
break;
}
}
}
}
}
if (f == null) // region based approach not successful
f = this.product.factories[0];
if (f != this.factory()) {
if (this.factory())
this.factory().remove(this);
this.factory(f);
f.add(this);
}
}
updateAmount(amount) {
this.amount(amount);
}
}
class Need extends Demand {
constructor(config) {
super(config);
this.allDemands = [];
let treeTraversal = node => {
this.allDemands.push(node);
(node.demands || []).forEach(treeTraversal);
}
treeTraversal(this);
}
}
class PopulationNeed extends Need {
constructor(config) {
super(config);
this.checked = ko.observable(true);
this.banned = ko.computed(() => {
var checked = this.checked();
var noOptionalNeeds = view.settings.noOptionalNeeds.checked();
return !checked || this.happiness && noOptionalNeeds;
})
this.optionalAmount = ko.observable(0);
this.banned.subscribe(banned => {
if (banned)
this.amount(0);
else
this.amount(this.optionalAmount());
});
}
updateAmount(inhabitants) {
this.optionalAmount(this.tpmin * inhabitants);
if (!this.banned())
this.amount(this.optionalAmount());
}
}
class BuildingMaterialsNeed extends Need {
constructor(config) {
super(config);
this.product = config.product;
this.factory(config.factory);
this.factory().add(this);
}
updateAmount(buildings) {
this.amount(buildings * this.factory().tpmin * this.factory().boost());
}
updateFixedProductFactory() { }
}
class PopulationLevel extends NamedElement {
constructor(config) {
super(config);
this.amount = ko.observable(0);
this.noOptionalNeeds = ko.observable(false);
this.needs = [];
config.needs.forEach(n => {
if (n.tpmin > 0)
this.needs.push(new PopulationNeed(n));
});
this.amount.subscribe(val => this.needs.forEach(n => n.updateAmount(val)));
}
incrementAmount() {
this.amount(parseFloat(this.amount()) + 1);
}
decrementAmount() {
this.amount(parseFloat(this.amount()) - 1);
}
}
class ProductCategory extends NamedElement {
constructor(config) {
super(config);
this.products = config.products.map(p => assetsMap.get(p));
}
}
class Workforce extends NamedElement {
constructor(config) {
super(config);
this.amount = ko.observable(0);
this.demands = [];
}
updateAmount() {
var sum = 0;
this.demands.forEach(d => sum += d.amount());
this.amount(sum);
}
add(demand) {
this.demands.push(demand);
}
}
class WorkforceDemand extends NamedElement {
constructor(config) {
super(config);
this.amount = ko.observable(0);
this.workforce.add(this);
this.amount.subscribe(val => this.workforce.updateAmount());
}
updateAmount(buildings) {
this.amount(Math.ceil(buildings) * this.Amount);
}
}
function reset() {
assetsMap.forEach(a => {
if (a instanceof Product)
a.fixedFactory(null);
if (a instanceof Factory)
a.percentBoost(100);
if (a instanceof Factory)
a.existingBuildings(0);
if (a instanceof PopulationLevel)
a.amount(0);
});
view.buildingMaterialsNeeds.forEach(b => b.factory().buildings(0));
view.populationLevels.forEach(l => l.needs.forEach(n => {
if (n.checked)
n.checked(true);
}))
}
function init() {
$(document).on("keydown", (evt)=>{
if(evt.altKey || evt.ctrlKey || evt.shiftKey)
return true;
var focused = false;
$(".ui-race-unit-name").filter(function() {
return (new RegExp(`^${evt.key}`, 'i')).test($(this).text());
}).each((i, ele) => {
focused = true;
return $(ele).closest('.ui-race-unit').find('input').focus().select()
});
if(evt.target.tagName === 'INPUT' && !isNaN(parseInt(evt.key)) || focused){
let isDigit = evt.key >= "0" && evt.key <= "9";
return ['ArrowUp','ArrowDown','Backspace','Delete'].includes(evt.key) || isDigit || evt.key === "." || evt.key === ",";
}
});
for (let attr in texts) {
view.texts[attr] = new NamedElement({ name: attr, locaText: texts[attr] });
}
view.settings.options = [];
for (let attr in options) {
let o = new Option(options[attr]);
o.id = attr;
view.settings[attr] = o;
view.settings.options.push(o);
if (localStorage) {
let id = "settings." + attr;
if (localStorage.getItem(id) != null)
o.checked(parseInt(localStorage.getItem(id)));
o.checked.subscribe(val => localStorage.setItem(id, val ? 1 : 0));
}
}
view.settings.languages = params.languages;
for (let region of params.regions) {
let r = new Region(region);
assetsMap.set(r.guid, r);
view.regions.push(r);
}
for (let workforce of params.workforce) {
let w = new Workforce(workforce)
assetsMap.set(w.guid, w);
view.workforce.push(w);
}
for (let factory of params.factories) {
let f = new Factory(factory)
assetsMap.set(f.guid, f);
view.factories.push(f);
if (localStorage) {
let id = f.guid + ".percentBoost";
if (localStorage.getItem(id))
f.percentBoost(parseInt(localStorage.getItem(id)));
f.percentBoost.subscribe(val => localStorage.setItem(id, val));
}
if (localStorage) {
let id = f.guid + ".existingBuildings";
if (localStorage.getItem(id))
f.existingBuildings(parseInt(localStorage.getItem(id)));
f.existingBuildings.subscribe(val => localStorage.setItem(id, val));
}
}
let products = [];
for (let product of params.products) {
if (product.producers && product.producers.length) {
let p = new Product(product);
products.push(p);
assetsMap.set(p.guid, p);
if (p.factories.length > 1)
view.multiFactoryProducts.push(p);
if (localStorage) {
{
let id = p.guid + ".percentBoost";
if (localStorage.getItem(id)) {
let b = parseInt(localStorage.getItem(id))
p.factories.forEach(f => f.percentBoost(b));
localStorage.removeItem(id);
}
}
{
let id = p.guid + ".fixedFactory";
if (localStorage.getItem(id))
p.fixedFactory(assetsMap.get(parseInt(localStorage.getItem(id))));
p.fixedFactory.subscribe(f => f ? localStorage.setItem(id, f.guid) : localStorage.removeItem(id));
}
}
}
}
view.factories.forEach(f => f.referenceProducts());
for (let level of params.populationLevels) {
let l = new PopulationLevel(level)
assetsMap.set(l.guid, l);
view.populationLevels.push(l);
if (localStorage) {
let id = l.guid + ".amount";
if (localStorage.getItem(id))
l.amount(parseInt(localStorage.getItem(id)));
l.amount.subscribe(val => localStorage.setItem(id, val));
}
for (let n of l.needs) {
if (localStorage) {
let id = `${l.guid}[${n.guid}].checked`;
if (localStorage.getItem(id))
n.checked(parseInt(localStorage.getItem(id)))
n.checked.subscribe(val => localStorage.setItem(id, val ? 1 : 0));
}
}
}
for (category of params.productFilter) {
let c = new ProductCategory(category);
assetsMap.set(c.guid, c);
view.categories.push(c);
}
for (let p of view.categories[1].products) {
for (let b of p.factories) {
if (b && b.demands.size == 0) {
b.editable = true;
let n = new BuildingMaterialsNeed({ guid: p.guid, factory: b, product: p });
b.buildings = ko.observable(0);
b.buildings.subscribe(val => {
if (!(typeof val === 'number'))
val = parseFloat(val);
n.updateAmount(val);
});
b.boost.subscribe(() => n.updateAmount(b.buildings()));
view.buildingMaterialsNeeds.push(n);
if (localStorage) {
let id = b.guid + ".buildings";
if (localStorage.getItem(id))
b.buildings(parseInt(localStorage.getItem(id)));
b.buildings.subscribe(val => localStorage.setItem(id, val));
}
}
}
}
ko.applyBindings(view, $(document.body)[0]);
}
function removeSpaces(string) {
if (typeof string === "function")
string = string();
return string.replace(/\W/g, "");
}
$(document).ready(function () {
if (window.params == null)
$('#params-dialog').modal("show");
else
init();
$('#params-dialog').on('hide.bs.modal', () => {
try {
window.params = JSON.parse($('textarea#input-params').val());
init();
} catch (e) {
console.log(e);
$('#params-dialog').modal("show");
}
})
})
texts = {
inhabitants: {
english: "Inhabitants",
german: "Bevölkerung"
},
workforce: {
english: "Required Workforce",
german: "Benötigte Arbeitskraft"
},
productionBoost: {
english: "Production Boost",
german: "Produktionsboost"
},
requiredNumberOfBuildings: {
english: "Required Number of Buildings",
german: "Benötigte Anzahl an Gebäuden"
},
existingNumberOfBuildings: {
english: "Existing Number of Buildings",
german: "Vorhandene Anzahl an Gebäuden"
},
existingNumberOfBuildingsIs: {
english: "Is:",
german: "Ist:"
},
tonsPerMinute: {
english: "Production in Tons per Minute",
german: "Produktion in Tonnen pro Minute"
},
language: {
english: "Language",
german: "Sprache"
},
settings: {
english: "Settings",
german: "Einstellungen"
},
help: {
english: "Help",
german: "Hilfe"
},
chooseFactories: {
english: "Choose Factory for each product",
german: "Wähle Fabrik für jedes Produkt"
},
noFixedFactory: {
english: "Automatic: same region as consumer",
german: "Automatisch: gleichen Region wie Verbraucher"
},
helpContent: {
german:
`Verwendung: Trage die aktuellen oder angestrebten Einwohner pro Stufe in die oberste Reihe ein. Die Produktionsketten aktualisieren sich automatisch sobald man die Eingabe verlässt. Es werden nur diejenigen Fabriken angezeigt, die benötigt werden.
In der darunterliegenden Reihe wird die Arbeitskraft angezeigt, die benötigt wird, um alle Gebäude zu betreiben (jeweils auf die nächste ganze Fabrik gerundet).
Danach folgen zwei große Abschnitte, die sich wiederum in Unterabschnitte unterteilen. Der erste gibt einen Überblick über alle benötigten Gebäude, sortiert nach dem produzierten Warentyp. Der zweite schlüsselt die einzelnen Produktionsketten nach Bevölkerungsstufen auf. Jeder der Abschnitte kann durch einen Klick auf die Überschrift zusammengeklappt werden. Durch das Abwählen des Kontrollkästchens wird das entsprechende Bedürfnis von der Berechnung ausgenommen.
In jeder Kachel wird der Name der Fabrik, das Icon der hergestellten Ware, der Boost für den Gebäudetyp, die Anzahl der benötigten Gebäude und die Produktionsrate in Tonnen pro Minute angezeigt. Die Anzahl der Gebäude wird mit zwei Nachkommastellen angezeigt, um die Höhe der Überkapazitäten direkt ablesen zu können. Daneben befinden sich zwei Buttons. Diese versuchen den Boost so einzustellen, dass alle Gebäude des Typs bestmöglich ausgelastet sind und dabei ein Gebäude mehr (+) bzw. eines weniger (-) benötigt wird.
Da Baumaterialien sich Zwischenmaterialien mit Konsumgütern teilen sind sie (im Gegensatz zu Warenrechnern früherer Annos) mit aufgeführt, um so den Verbrauch von Minen besser planen zu können. Es muss die Anzahl der Endbetriebe per Hand eingegeben werden.
Über das Zahnrad am rechten oberen Bildschirmrand gelangt man zu den Einstellungen. Dort können die Sprache ausgewählt und die Menge der dargestellten Informationen angepasst werden.
Über die drei Zahnräder neben dem Einstellungsdialog öffnet sich der Dialog zur Auswahl der Fabrik, die ein bestimmtes Produkt produzieren soll. Standardmäßig ist die Gleiche-Region-Regel eingestellt. Exemplarisch besagt diese, dass das Holz für die Destillerien in der Neuen Welt, das Holz für Nähmaschinen aber in der Alten Welt produziert wird.
Haftungsausschluss:
Der Warenrechner wird ohne irgendeine Gewährleistung zur Verfügung gestellt. Die Arbeit wurde in KEINER Weise von Ubisoft Blue Byte unterstützt. Alle Assets aus dem Spiel Anno 1800 sind © by Ubisoft.
Dies sind insbesondere, aber nicht ausschließlich alle Icons der Bevölkerung, Waren und Gegenstände sowie die Daten der Produktionsketten und die Verbrachswerte der Bevölkerung.
Diese Software steht unter der MIT-Lizenz.
Autor:
Nico Höllerich
Fehler und Verbesserungen:
Falls Sie auf Fehler oder Unannehmlichkeiten stoßen oder Verbesserungen vorschlagen möchten, erstellen Sie ein Issue auf Github (https://github.com/NiHoel/Anno1800Calculator/issues)`,
english:
`Usage: Enter the current or desired number of inhabitants per level into the top most row. The production chains will update automatically when one leaves the input field. Only the required factories are displayed.
The row below displays the workforce that is required to run all buildings (rounded towards the next complete factory).
Afterwards two big sections follow that are subdivided into smaller sections. The first one gives an overview of the required buildings sorted by the type of good that is produced. The second one lists the individual production chains for each population level. Clicking the heading collapses each section. Deselecting the checkbox leads to the need being excluded from the calculation.
Each card displays the name of the factory, the icon of the produced good, the boost for the given type of building, the number of required buildings, and the production rate in tons per minute. The number of buildings has two decimal places to directly show the amount of overcapacities. There are two buttons next to it. Those try to adjust the boost such that all buildings operate at full capacity and one more (+) or one building less (-) is required.
Since construction materials share intermediate products with consumables they are explicitly listed (unlike in calculators for previous Annos) to better plan the production of mines. The number of factories must be entered manually.
When clicking on the cog wheel in the upper right corner of the screen the settings dialog opens. There, one can chose the language and customize the information presented by the calculator.
The three cog wheels next to the settings dialog open a dialog to choose the factory that produces a certain good. By default, the same region policy is selected. By example, this means that the wood for desitilleries is produced in the New World while the wood for sewing machines is produced in the Old World.
Disclaimer:
The calculator is provided without warranty of any kind. The work was NOT endorsed by Ubisoft Blue Byte in any kind. All the assets from Anno 1800 game are © by Ubsioft.
These are especially but not exclusively all the icons of population, goods and items and the data of production chains and the consumptions values of population.
This software is under the MIT license.
Author:
Nico Höllerich
Bugs and improvements:
If you encounter any bugs or inconveniences or if you want to suggest improvements, create an Issue on Github (https://github.com/NiHoel/Anno1800Calculator/issues)`
}
}
options = {
"noOptionalNeeds": {
"name": "Do not produce luxury goods",
"locaText": {
"english": "Do not produce luxury goods",
"german": "Keine Luxusgüter produzieren"
}
},
"decimalsForBuildings": {
"name": "Show number of buildings with decimals",
"locaText": {
"english": "Show number of buildings with decimals",
"german": "Zeige Nachkommastellen bei der Gebäudeanzahl"
}
},
"missingBuildingsHighlight": {
"name": "Highlight missing buildings",
"locaText": {
"english": "Highlight missing buildings",
"german": "Fehlende Gebäude hervorheben"
}
},
"hideNames": {
"name": "Hide the names of products, factories, and population levels",
"locaText": {
"english": "Hide the names of products, factories, and population levels",
"german": "Verberge die Namen von Produkten, Fabriken und Bevölkerungsstufen"
}
},
"hideProductionBoost": {
"name": "Hide the input fields for production boost",
"locaText": {
"english": "Hide the input fields for production boost",
"german": "Verberge das Eingabefelder für Produktionsboosts"
}
},
"hideNewWorldConstructionMaterial": {
"name": "Hide factory cards for construction material that produce in the new world",
"locaText": {
"english": "Hide factory cards for construction material that is produced in the New world",
"german": "Verberge die Fabrikkacheln für Baumaterial, das in der Neuen Welt produziert wird"
}
}
}