-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.ts
776 lines (703 loc) · 27.2 KB
/
build.ts
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
import CRC32 from 'crc-32';
import fs from 'fs';
import path from 'path';
import sqlite3 from 'better-sqlite3';
const beco = require('./beco');
const yaml = require('js-yaml');
import { PlacementMap, PlacementObj, PlacementLink, ResPlacementObj } from './app/PlacementMap';
import * as util from './app/util';
let parseArgs = require('minimist');
let argv = parseArgs(process.argv);
if (!argv.a) {
console.log("Error: Must specify a path to directory with ActorLink and DropTable YAML files");
console.log(" e.g. % ts-node build.ts -a ../botw/Actor")
console.log(" YAML data files are available from https://github.com/leoetlino/botw");
process.exit(1);
}
const botwData = argv.a;
const actorinfodata = JSON.parse(fs.readFileSync(path.join(util.APP_ROOT, 'content', 'ActorInfo.product.json'), 'utf8'));
const names: { [actor: string]: string } = JSON.parse(fs.readFileSync(path.join(util.APP_ROOT, 'content', 'names.json'), 'utf8'));
const getUiName = (name: string) => names[name] || name;
const locationMarkerTexts: { [actor: string]: string } = JSON.parse(fs.readFileSync(path.join(util.APP_ROOT, 'content', 'text', 'StaticMsg', 'LocationMarker.json'), 'utf8'));
const dungeonTexts: { [actor: string]: string } = JSON.parse(fs.readFileSync(path.join(util.APP_ROOT, 'content', 'text', 'StaticMsg', 'Dungeon.json'), 'utf8'));
const korok_data: [any] = JSON.parse(fs.readFileSync(path.join(util.APP_ROOT, 'korok_ids.json'), 'utf8'));
let korok_ids: any = {};
korok_data.forEach(k => { korok_ids[k.hash_id] = k; });
const polys = JSON.parse(fs.readFileSync(path.join(util.APP_ROOT, "castle.json"), 'utf-8'));
const mapTower = new beco.Beco(path.join(util.APP_ROOT, 'content', 'ecosystem', 'MapTower.beco'));
// Tower Names taken from Messages/Msg_USen.product.sarc/StaticMsg/LocationMarker.msyt Tower01 - Tower15
const towerNames = ["Hebra", "Tabantha", "Gerudo", "Wasteland", "Woodland",
"Central", "Great Plateau", "Dueling Peaks", "Lake",
"Eldin", "Akkala", "Lanayru", "Hateno", "Faron", "Ridgeland"];
const fieldArea = new beco.Beco(path.join(util.APP_ROOT, 'content', 'ecosystem', 'FieldMapArea.beco'));
const locations: { [key: string]: string } = JSON.parse(fs.readFileSync(path.join(util.APP_ROOT, 'content', 'locations.json'), 'utf8'));
// Create Special tags for YAML: !obj, !list, !io, !str64
const objType = new yaml.Type('!obj', {
kind: 'mapping', instanceOf: Object,
resolve: function(data: any) { return true; },
construct: function(data: any) { return data; },
});
const listType = new yaml.Type('!list', {
kind: 'mapping', instanceOf: Object,
resolve: function(data: any) { return true; },
construct: function(data: any) { return data; },
});
const ioType = new yaml.Type('!io', {
kind: 'mapping', instanceOf: Object,
resolve: function(data: any) { return true; },
construct: function(data: any) { return data; },
});
const str64Type = new yaml.Type('!str64', {
kind: 'scalar', instanceOf: String,
resolve: function(data: any) { return true; },
construct: function(data: any) { return data; },
});
// Add Special Tags to the Default schema (to facilitate reading)
let schema = yaml.DEFAULT_SCHEMA.extend([objType, listType, ioType, str64Type]);
function readYAML(filePath: string) {
let doc: any = null;
try {
doc = yaml.load(fs.readFileSync(filePath, 'utf-8'), { schema: schema });
} catch (e) {
console.log(e);
process.exit(1);
}
return doc;
}
function getDropTableNameFromActorLinkFile(doc: { [key: string]: any }): string | null {
if ('DropTableUser' in doc.param_root.objects.LinkTarget) {
let dropTableUser = doc.param_root.objects.LinkTarget.DropTableUser;
return dropTableUser;
}
return null;
}
function getTagsFromActorLinkFile(doc: { [key: string]: any }): string[] | null {
if ('Tags' in doc.param_root.objects) {
let tags = doc.param_root.objects.Tags;
return Object.values(tags);
}
return null;
}
function readDropTableFile(file: string) {
let doc = readYAML(file)
let tables: any = Object.keys(doc.param_root.objects)
.filter(key => key != 'Header')
.map(key => {
let dropTable = doc.param_root.objects[key];
let items: { [key: string]: any } = {};
for (var i = 1; i <= dropTable.ColumnNum; i++) {
let itemName = `ItemName${String(i).padStart(2, '0')}`;
let itemProb = `ItemProbability${String(i).padStart(2, '0')}`;
items[dropTable[itemName]] = dropTable[itemProb];
}
let data = {
items: items,
repeat_num: [dropTable.RepeatNumMin, dropTable.RepeatNumMax],
};
return { name: key, data: data };
});
return tables;
}
function readDropTablesByName(table: string) {
return readDropTableFile(path.join(botwData, 'DropTable', `${table}.drop.yml`));
}
function readDropTables(lootTables: { [key: string]: string }) {
let data: any[] = [];
Object.keys(lootTables)
.filter(name => lootTables[name] != "Dummy") // Ignore empty Dummy tables
.forEach(name => {
let tables = readDropTablesByName(lootTables[name]);
tables.forEach((table: any) => table.actor_name = name); // Matches unit_config_name in table objs
data.push(...tables);
});
return data;
}
function readYAMLData(): [any[], { [key: string]: string[] }] {
let itemTags: { [key: string]: string[] } = {};
let lootTables: { [key: string]: string } = {};
let dirPath = path.join(botwData, 'ActorLink');
let files = fs.readdirSync(dirPath);
files.forEach(file => {
let actorName = path.basename(file, '.yml'); // ==> UnitConfigName
let filePath = path.join(botwData, 'ActorLink', file);
let doc = readYAML(filePath);
let tableName = getDropTableNameFromActorLinkFile(doc);
if (tableName) {
lootTables[actorName] = tableName;
}
let tags = getTagsFromActorLinkFile(doc);
if (tags) {
itemTags[actorName] = tags;
}
});
let dropData: any[] = readDropTables(lootTables);
return [dropData, itemTags];
}
let [dropData, itemTags] = readYAMLData();
const db = sqlite3('map.db.tmp');
db.pragma('journal_mode = WAL');
db.exec(`
CREATE TABLE objs (
objid INTEGER PRIMARY KEY,
map_type TEXT NOT NULL,
map_name TEXT NOT NULL,
map_static BOOL,
gen_group INTEGER,
hash_id INTEGER,
unit_config_name TEXT NOT NULL,
ui_name TEXT NOT NULL,
data JSON NOT NULL,
one_hit_mode BOOL DEFAULT 0,
last_boss_mode BOOL DEFAULT 0,
hard_mode BOOL DEFAULT 0,
disable_rankup_for_hard_mode BOOL DEFAULT 0,
scale INTEGER DEFAULT 0,
sharp_weapon_judge_type INTEGER DEFAULT 0,
'drop' JSON,
equip JSON,
ui_drop TEXT,
ui_equip TEXT,
messageid TEXT,
region TEXT NOT NULL,
field_area INTEGER,
spawns_with_lotm BOOL,
korok_id TEXT,
korok_type TEXT,
location TEXT
);
`);
db.exec(`
CREATE TABLE drop_table (
actor_name TEXT NOT NULL,
name TEXT NOT NULL,
data JSON
);
`);
db.exec(`
CREATE TABLE rails (
hash_id INTEGER NOT NULL,
data JSON
);
`);
const insertObj = db.prepare(`INSERT INTO objs
(map_type, map_name, map_static, gen_group, hash_id, unit_config_name, ui_name, data, one_hit_mode, last_boss_mode, hard_mode, disable_rankup_for_hard_mode, scale, sharp_weapon_judge_type, 'drop', equip, ui_drop, ui_equip, messageid, region, field_area, spawns_with_lotm, korok_id, korok_type, location)
VALUES
(@map_type, @map_name, @map_static, @gen_group, @hash_id, @unit_config_name, @ui_name, @data, @one_hit_mode, @last_boss_mode, @hard_mode, @disable_rankup_for_hard_mode, @scale, @sharp_weapon_judge_type, @drop, @equip, @ui_drop, @ui_equip, @messageid, @region, @field_area, @spawns_with_lotm, @korok_id, @korok_type, @location)`);
const insertRail = db.prepare(`INSERT INTO rails (hash_id, data) VALUES (@hash_id, @data)`);
function getActorData(name: string) {
const h = CRC32.str(name) >>> 0;
const hashes = actorinfodata['Hashes'];
let a = 0, b = hashes.length - 1;
while (a <= b) {
const m = (a + b) >> 1;
if (hashes[m] < h)
a = m + 1;
else if (hashes[m] > h)
b = m - 1;
else
return actorinfodata['Actors'][m];
}
return null;
}
function isFlag4Actor(name: string) {
if (name == 'Enemy_GanonBeast')
return false;
const info = getActorData(name);
for (const x of ['Enemy', 'GelEnemy', 'SandWorm', 'Prey', 'Dragon', 'Guardian']) {
if (info['profile'] == x)
return true;
}
if (info['profile'].includes('NPC'))
return true;
return false;
}
function shouldSpawnObjForLastBossMode(obj: PlacementObj) {
const name: string = obj.data.UnitConfigName;
if (isFlag4Actor(name))
return false;
if (name == 'Enemy_Guardian_A')
return false;
if (name.includes('Entrance') || name.includes('WarpPoint') || name.includes('Terminal'))
return false;
return true;
}
function objGetUiName(obj: PlacementObj) {
if (obj.data.UnitConfigName === 'LocationTag') {
const id = obj.data['!Parameters'].MessageID;
const locationName = locationMarkerTexts[id] || dungeonTexts[id];
let s = `Location: ${locationName}`;
const dungeonSub = dungeonTexts[id + '_sub'];
if (dungeonSub)
s += ' - ' + dungeonSub;
return s;
}
return getUiName(obj.data.UnitConfigName);
}
function objGetDrops(params: any) {
if (params.DropActor)
return [1, params.DropActor];
if (!params.DropActor && params.DropTable && params.DropTable != 'Normal')
return [2, params.DropTable];
return null;
}
function objGetUiDrops(params: any) {
const info: string[] = [];
if (params.DropActor)
info.push(getUiName(params.DropActor));
else if (params.DropTable && params.DropTable != 'Normal')
info.push('Table:' + params.DropTable);
return info.join('|');
}
function objGetEquipment(params: any) {
const info: string[] = [];
for (const prop of ['EquipItem1', 'EquipItem2', 'EquipItem3', 'EquipItem4', 'EquipItem5', 'RideHorseName']) {
if ((prop in params) && params[prop] != 'Default')
info.push(params[prop]);
}
if (params['ArrowName'] && params['ArrowName'] != 'NormalArrow') {
info.push(params['ArrowName']);
}
return info;
}
function objGetUiEquipment(params: any) {
return objGetEquipment(params).map(getUiName).join(', ');
}
function hasLiftRock(group: any[]) {
return group.map(objGetUiName)
.some(name => name.includes("LiftRock") && !name.includes("Korok"));
}
function arrayEquals(a: string[], b: string[]): boolean {
if (a.length != b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
function korokGetType(group: any[], obj: any): string {
let len = group.length;
let names: string[] = group.map(objGetUiName);
names.sort();
if (hasLiftRock(group)) {
switch (len) {
case 7: return "Rock Lift"; // 174
case 9:
return "Rock Lift (Rock Pile)"; // 40
case 11:
if (names.includes("Treasure Chest")) {
return "Rock Lift (Rock Pile)"; // 1
}
if (names.includes("Obj_BoardIron_C_01")) {
return "Rock Lift (Door)"; // 8
}
if (names.includes("FldObj_PushRock_A_M_01")) {
return "Rock Lift (Boulder)"; // 6
}
return "Rock Lift (Slab)"; // 21
case 23:
return "Rock Lift (Leaves)"; // 19
case 14:
case 22:
case 30:
return "Rock Pattern"; // 72
default:
break;
}
}
if (names.includes("FldObj_KorokPinwheel_A_01")) { // 56
switch (len) {
case 5: return "Stationary Lights";
case 15: return "Pinwheel Balloons";
case 23: return "Pinwheel Balloons";
case 27: return "Pinwheel Acorns";
case 31: return "Pinwheel Balloons";
case 46: return "Pinwheel Acorns";
case 64: return "Pinwheel Acorns";
default:
break;
}
}
let identifiers: { [key: string]: string } = {
"Obj_Plant_KorokColor_A_01": "Flower Order", // 11
"Obj_Plant_Korok_A_01": "Flower Trail", // 46
"FldObj_RuinStonePavement_A_06": "Offering Plate", // 27 + 1 (egg)
"Obj_KorokPlate_A_01": "Offering Plate", //
"FldObj_KorokGoal_A_01": "Goal Ring (Race)", // 51
"Obj_TreeCactusMini_A_01": "Matching Trees", // 5
"Obj_TreeDorian_A_01": "Matching Trees", // 3
"Obj_Plant_IvyBurn_A_01": "Burn the Leaves (Goatee)", // 1
"FidObj_TorchStandOff_A_01": "Light Torch", // 1
"Tree Branch": "Take the Stick", // 1
"Luminous Stone": "Remove Luminous Stone", // 1
"YabusameBow": "Shoot the Targets", // 1
"TwnObj_Village_FishingHouse_S_A_02": "Take Apple from Palm Tree", // 1
"Obj_TreeApple_A_M_01": "Matching Trees", // 12
"SignalFlowchart": "Jump the Fences", // 2
"Obj_BoxIron_A_M_01": "Rock Pattern", // 1 "Arrange Metal cubes under water"
"BrokenSnowBall": "Roll a Boulder", // 2 "Push Snowball in Hole"
"IceWall": "Melt Ice Block", // 18
"PointWindSetTag": "Roll a Boulder", // 1 "Push Boulder into Hole off Pillar"
};
if (len == 1) {
if ('LinksToRail' in group[0].data) {
return "Moving Lights"; // 39
}
return "Stationary Lights"; // 46
}
for (const name of names) {
if (name in identifiers) {
return identifiers[name];
}
}
if (names.includes("Obj_KorokPot_A_01")) {
if (names.length == 6) {
return "Acorn in a Hole"; // 29
}
return "Hanging Acorn"; // 14
}
if (names.includes("FldObj_ChainEyeBolt_A_01")) { // Must follow Acorn
return "Ball and Chain"; // 14
}
if (names.includes("FldObj_KorokTarget_A_01")) { // Must follow Acorn
return "Stationary Balloon"; // 26
}
if (len == 21 && names.includes("FldObj_KorokStoneLift_A_01") && names.includes("FldObj_KorokStone_A_01")) {
return "Cube Puzzle"; // 66
}
if (arrayEquals(names, ['ActorObserverTag', 'Area', 'Area', 'FldObj_PushRock_Korok', 'FldObj_PushRock_Korok',
'FldObj_PushRock_Korok', 'Korok', 'KorokAnswerResponce', 'LinkTagAnd', 'LinkTagAnd',
'LinkTagNone', 'LinkTagOr', 'LinkTagOr', 'SwitchTimeLag'])) {
return "Roll a Boulder"; // 1 Must preceed Push Boulder into Hole
}
if (names.includes("FldObj_PushRock_A_M_01")) {
return "Roll a Boulder";
}
if (names.includes("FldObj_PushRock_Korok")) {
return "Roll a Boulder";
}
if (names.includes("Obj_KorokIronRock_A_01")) {
return "Ball and Chain";
}
if (names.includes("FldObj_PushRockIron_A_M_01")) {
return "Roll a Boulder"; // Must follow Boulder Between Trees
}
// Specific Korok Types without Specific Tags
if (arrayEquals(names, ['Area', 'Korok', 'LinkTagOr', 'LinkTagOr'])) {
return "Stationary Lights"; // 41
}
if (arrayEquals(names, ['Area', 'Korok', 'LinkTagAnd', 'LinkTagOr'])) {
return "Dive"; // 35
}
if (arrayEquals(names, ['ActorObserverTag', 'ActorObserverTag', 'ActorObserverTag', 'Area', 'Korok',
'KorokAnswerResponce', 'LinkTagAnd', 'LinkTagAnd', 'LinkTagOr', 'LinkTagOr', 'SwitchTimeLag'])) {
return "Circle of Rocks"; // 20
}
if (arrayEquals(names, ['ActorObserverByGroupTag', 'Area', 'Area', 'Korok', 'LinkTagAnd', 'LinkTagAnd',
'LinkTagNAnd', 'LinkTagOr'])) {
return "Shoot the Crest"; // 4
}
if (arrayEquals(names, ['ActorObserverTag', 'ActorObserverTag', 'Area', 'Area', 'Korok',
'KorokAnswerResponce', 'KorokAnswerResponce', 'LinkTagAnd', 'LinkTagAnd', 'LinkTagAnd',
'LinkTagOr', 'SwitchTimeLag'])) {
return "Ball and Chain"; // 1
}
if (arrayEquals(names, ['ActorObserverTag', 'ActorObserverTag', 'ActorObserverTag', 'Area',
'Area', 'Area', 'Korok', 'LinkTagAnd', 'LinkTagAnd', 'LinkTagOr', 'SwitchTimeLag'])) {
return "Offering Plate"; //Put Egg in Water"; 1
}
if (arrayEquals(names, ['ActorObserverTag', 'Area', 'Area', 'Korok', 'KorokAnswerResponce',
'LinkTagAnd', 'LinkTagAnd', 'LinkTagAnd', 'LinkTagNone', 'LinkTagOr', 'SwitchTimeLag'])) {
return "Roll a Boulder"; //"Push Boulder into Hole"; // 6
}
if (arrayEquals(names, ['Korok', 'LinkTagAnd', 'LinkTagAnd', 'LinkTagAnd', 'LinkTagNAnd'])) {
return "Stationary Lights"; //"Shrine of Resurrection"; // 1
}
console.error(names);
console.error(len);
console.error(`Unhandled Korok Type: ${objGetUiName(obj)} ${obj.data.HashId}`);
process.exit(1);
}
// Check is a point (x,y,z) is contained within a polygon's bounding box
// Bounding box is defined in properties (xmin, xmax, zmin, zmax)
function isPointInsideBoundingBox(poly: any, pt: any): boolean {
let prop = poly.properties;
return ((prop.xmin && pt[0] >= prop.xmin) ||
(prop.zmin && pt[2] >= prop.zmin) ||
(prop.xmax && pt[0] <= prop.xmax) ||
(prop.zmax && pt[2] <= prop.zmax));
}
// Check is a point (x,y,z) is contained within a polygon
// The bounding box is first checked then the polygon is checked
function isPointInsidePolygon(poly: any, pt: any): boolean {
return isPointInsideBoundingBox(poly, pt) && isPointInsidePolygonRCA(pt, poly.geometry.coordinates[0]);
}
// Check if a point is within a polygon (pts)
// https://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm
function isPointInsidePolygonRCA(point: any, pts: any) {
let n = pts.length;
let xp = point[0];
let yp = point[2];
let xv: any = pts.map((p: any) => p[0]);
let yv: any = pts.map((p: any) => p[1]);
if (Math.abs(xv[0] - xv[n - 1]) < 1e-7 && Math.abs(yv[0] - yv[n - 1]) < 1e-7) {
n -= 1;
}
let x2 = xv[n - 1]
let y2 = yv[n - 1]
let nleft = 0
let x1 = x2;
let y1 = y2;
// Loop over line segments (assuming the polygon is closed)
for (let i = 0; i < n; i++) {
x1 = x2
y1 = y2
x2 = xv[i]
y2 = yv[i]
if (y1 >= yp && y2 >= yp) {
continue;
}
if (y1 < yp && y2 < yp) {
continue;
}
if (y1 == y2) {
if (x1 >= xp && x2 >= xp) {
continue;
}
if (x1 < xp && x2 < xp) {
continue;
}
nleft += 1;
} else {
let xi = x1 + (yp - y1) * (x2 - x1) / (y2 - y1);
if (xi == xp) {
nleft = 1;
break;
}
if (xi > xp) {
nleft += 1;
}
}
}
let xin = nleft % 2;
return xin == 1;
}
// Test all polygons (polys) if a point lies in any
// polygons are assumed to be in GeoJSON format and have:
// - a bounding box (xmin,ymin,zmin,xmax,ymax,zmax)
// - a priority where overlapping polygons with higher priority are chosen
// Returns the found polygon or null in the case of no match
function findPolygon(p: any, polys: any) {
let found = null;
for (let j = 0; j < polys.features.length; j++) {
const poly = polys.features[j];
if ((poly.properties.ymin && p[1] < poly.properties.ymin) ||
(poly.properties.ymax && p[1] > poly.properties.ymax)) {
continue;
}
if (found && poly.properties.priority < found.properties.priority) {
continue;
}
if (isPointInsidePolygon(poly, p)) {
found = polys.features[j];
}
}
return found;
}
function processMap(pmap: PlacementMap, isStatic: boolean): void {
process.stdout.write(`processing ${pmap.type}/${pmap.name} (static: ${isStatic})`);
const hashIdToObjIdMap: Map<number, any> = new Map();
const genGroups: Map<number, PlacementObj[]> = new Map();
const genGroupSkipped: Map<number, boolean> = new Map();
for (const obj of pmap.getObjs()) {
if (!genGroups.has(obj.genGroupId))
genGroups.set(obj.genGroupId, []);
genGroups.get(obj.genGroupId)!.push(obj);
}
for (const [id, genGroup] of genGroups.entries())
genGroupSkipped.set(id, genGroup.some(o => !shouldSpawnObjForLastBossMode(o)));
for (const obj of pmap.getObjs()) {
const params = obj.data['!Parameters'];
let scale = params ? params.LevelSensorMode : 0;
if (!obj.data.UnitConfigName.startsWith('Weapon_') && !obj.data.UnitConfigName.startsWith('Enemy_'))
scale = null;
let area = -1;
if (pmap.type == 'MainField') {
area = fieldArea.getCurrentAreaNum(obj.data.Translate[0], obj.data.Translate[2]);
}
let lotm = false;
let objTags = itemTags[obj.data.UnitConfigName];
if (area == 64 && objTags) {
lotm = objTags.includes('UnderGodForest');
}
let korok = null;
if (obj.data.HashId in korok_ids) {
korok = korok_ids[obj.data.HashId].id;
}
let korok_type = null;
if (objGetUiName(obj) == "Korok") {
let group = genGroups.get(obj.genGroupId)!;
korok_type = korokGetType(group, obj);
}
let location = null;
if (obj.data.HashId in locations) {
location = locations[obj.data.HashId];
}
let poly = findPolygon(obj.data.Translate, polys);
if (poly) {
location = poly.properties.name;
}
const result = insertObj.run({
map_type: pmap.type,
map_name: pmap.name,
map_static: isStatic ? 1 : 0,
gen_group: obj.genGroupId,
hash_id: obj.data.HashId,
unit_config_name: obj.data.UnitConfigName,
ui_name: objGetUiName(obj),
data: JSON.stringify(obj.data),
one_hit_mode: (params && params.IsIchigekiActor) ? 1 : 0,
last_boss_mode: genGroupSkipped.get(obj.genGroupId) ? 0 : 1,
hard_mode: (params && params.IsHardModeActor) ? 1 : 0,
disable_rankup_for_hard_mode: (params && params.DisableRankUpForHardMode) ? 1 : 0,
scale,
sharp_weapon_judge_type: params ? params.SharpWeaponJudgeType : 0,
drop: params ? JSON.stringify(objGetDrops(params)) : null,
equip: params ? JSON.stringify(objGetEquipment(params)) : null,
ui_drop: params ? objGetUiDrops(params) : null,
ui_equip: params ? objGetUiEquipment(params) : null,
messageid: params ? (params['MessageID'] || null) : null,
region: pmap.type == 'MainField' ? towerNames[mapTower.getCurrentAreaNum(obj.data.Translate[0], obj.data.Translate[2])] : "",
field_area: area >= 0 ? area : null,
spawns_with_lotm: lotm ? 1 : 0,
korok_id: korok ? korok : null,
korok_type: korok_type,
location: location,
});
hashIdToObjIdMap.set(obj.data.HashId, result.lastInsertRowid);
}
for (const rail of pmap.getRails()) {
insertRail.run({ hash_id: rail.data.HashId, data: JSON.stringify(rail.data) });
}
process.stdout.write('.\n');
}
function processMaps() {
const MAP_PATH = path.join(util.APP_ROOT, 'content/map');
for (const type of fs.readdirSync(MAP_PATH)) {
const typeP = path.join(MAP_PATH, type);
for (const name of fs.readdirSync(typeP)) {
const nameP = path.join(typeP, name);
if (!util.isDirectory(nameP))
continue;
let fileName = `${name}_Static.json`;
let data: object = JSON.parse(fs.readFileSync(path.join(nameP, fileName), 'utf8'));
const staticMap = new PlacementMap(type, name, data);
fileName = `${name}_Dynamic.json`;
data = JSON.parse(fs.readFileSync(path.join(nameP, fileName), 'utf8'));
const dynamicMap = new PlacementMap(type, name, data);
processMap(staticMap, true);
processMap(dynamicMap, false);
}
}
}
db.transaction(() => processMaps())();
function createDropTable() {
let stmt = db.prepare(`INSERT INTO drop_table (actor_name, name, data) VALUES (@actor_name, @name, @data)`);
dropData.forEach((row: any) => {
let result = stmt.run({ actor_name: row.actor_name, name: row.name, data: JSON.stringify(row.data) });
});
}
console.log('creating drop data table...');
db.transaction(() => createDropTable())();
function createIndexes() {
db.exec(`
CREATE INDEX objs_map ON objs (map_type, map_name);
CREATE INDEX objs_map_type ON objs (map_type);
CREATE INDEX objs_hash_id ON objs (hash_id);
CREATE INDEX objs_gen_group ON objs (gen_group);
CREATE INDEX objs_unit_config_name ON objs (unit_config_name);
`);
}
console.log('creating indexes...');
createIndexes();
function checkKorokTypes() {
const counts = { // Notes based on https://lepelog.github.io/korokmap/
"Moving Lights": 39, //
"Stationary Lights": 51, //
"Rock Lift (Door)": 8, //
"Rock Lift (Boulder)": 6, //
"Rock Lift (Rock Pile)": 41, // Z54 is under Rock Pile
"Rock Lift (Slab)": 12, //
"Rock Lift": 174, //
"Rock Pattern": 73, // C51, L34, and N13 are not Rock Patterns
"Cube Puzzle": 66, //
"Goal Ring (Race)": 51, //
"Flower Trail": 46, //
"Pinwheel Balloons": 44, //
"Dive": 35, //
"Acorn in a Hole": 29, //
"Roll a Boulder": 31, // C51, L34, and N13 are not Rock Patterns but Push Boulder
"Offering Plate": 28, // 27 + 1 (egg)
"Stationary Balloon": 26, //
"Circle of Rocks": 20, //
"Matching Trees": 20, //
"Rock Lift (Leaves)": 19, //
"Melt Ice Block": 18, //
"Ball and Chain": 16, //
"Hanging Acorn": 14, //
"Flower Order": 11, //
"Pinwheel Acorns": 10, //
"Shoot the Crest": 4, //
"Jump the Fences": 2, //
"Light Torch": 1, //
"Burn the Leaves (Goatee)": 1, //
"Take the Stick": 1, //
"Shoot the Targets": 1, //
"Take Apple from Palm Tree": 1, //
"Remove Luminous Stone": 1, //
};
// Check the number of koroks in each category
let expectedNum = 0;
const stmt = db.prepare("select count(korok_type) as num from objs where korok_type = @kt");
for (const [key, num] of Object.entries(counts)) {
const res = stmt.all({ kt: key });
if (res.length != 1) {
console.error(`Expected a single value, got ${res}, ${res.length}`);
process.exit(1);
}
let out = res[0].num;
if (out != num) {
console.error(`Number of korok types mismatch: ${key}: expected ${num} returned ${out}`);
process.exit(1);
}
expectedNum += out;
}
// Check we have 900 koroks
if (expectedNum != 900) {
console.error(`Error: Expected 900 koroks, got ${expectedNum}`);
process.exit(1);
}
// Checking for unknown korok types
const res = db.prepare("select distinct(korok_type) as name from objs where korok_type is not NULL").all();
const names = res.map(row => row.name);
if (names.some(name => !(name in counts))) {
const ktypes = names.filter(name => !(name in counts));
console.error(`Error: Unknown korok types: ${ktypes}`);
process.exit(1);
}
}
console.log("checking korok types ...");
checkKorokTypes();
function createFts() {
db.exec(`
CREATE VIRTUAL TABLE objs_fts USING fts5(content="", tokenize="unicode61", map, actor, name, data, 'drop', equip, onehit, lastboss, hard, no_rankup, scale, bonus, static, region, fieldarea, lotm, korok, korok_type, location);
INSERT INTO objs_fts(rowid, map, actor, name, data, 'drop', equip, onehit, lastboss, hard, no_rankup, scale, bonus, static, region, fieldarea, lotm, korok, korok_type, location)
SELECT objid, map_type || '/' || map_name, unit_config_name, ui_name, data, ui_drop, ui_equip, one_hit_mode, last_boss_mode, hard_mode, disable_rankup_for_hard_mode, scale, sharp_weapon_judge_type, map_static, region, field_area, spawns_with_lotm, korok_id, korok_type, location FROM objs;
`);
}
console.log('creating FTS tables...');
createFts();
db.close();
fs.renameSync('map.db.tmp', 'map.db');