-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDQ9_ChestTimer.html
More file actions
1134 lines (1130 loc) · 44.9 KB
/
Copy pathDQ9_ChestTimer.html
File metadata and controls
1134 lines (1130 loc) · 44.9 KB
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>DQ9 Chest Timer (for Marathon)</title>
<style>
*{box-sizing:border-box;margin:0;padding:0}
body{margin:0;overflow:hidden}
#app{position:fixed;inset:0;background:#0a0a1a;color:#ccc;font-family:'Courier New',monospace;display:flex;flex-direction:column}
.topbar{background:#111128;padding:4px 6px;display:flex;flex-wrap:wrap;align-items:center;gap:3px;border-bottom:2px solid #333;z-index:10}
.topbar label{color:#88b;font-size:11px}
.topbar input,.topbar select{background:#000;color:#ffd700;border:1px solid #555;padding:2px 4px;font-size:12px;font-family:inherit;border-radius:3px;height:26px}
.topbar input{width:48px;text-align:center}
.tb-group{display:flex;align-items:center;gap:3px}
.filter-btn{height:26px;padding:0 7px;border:1px solid #555;background:#1a1a2e;color:#aaa;cursor:pointer;font-size:11px;border-radius:3px;font-family:inherit}
.filter-btn.active{background:#2a2a4a;color:#ffd700;border-color:#ffd700}
.timer-box{display:flex;align-items:center;gap:3px}
.timer-display{background:#0f0f2a;padding:2px 6px;border:2px solid #333;border-radius:4px;font-size:16px;font-weight:bold;color:#f88;min-width:80px;text-align:center;letter-spacing:1px;height:26px;line-height:22px}
.ctrl-btn{width:26px;height:26px;border:none;border-radius:4px;font-size:13px;cursor:pointer;font-family:inherit;display:flex;align-items:center;justify-content:center}
.ctrl-btn.start{background:#060;color:#0f0;border:2px solid #0a0}
.ctrl-btn.start:active{background:#080}
.ctrl-btn.start.running{background:#600;color:#f88;border-color:#a00}
.ctrl-btn.reset{background:#333;color:#888;border:2px solid #555;font-size:11px}
.ctrl-btn.reset:active{background:#444}
#mainArea{display:block;flex:1;overflow-x:auto;overflow-y:hidden;-webkit-overflow-scrolling:touch}
.vgrid{display:grid;width:max-content}
.vhdr{position:sticky;top:0;z-index:4;background:#111128;padding:5px 6px;font-weight:bold;font-size:11px;white-space:nowrap;text-align:center;color:#88b;border-bottom:2px solid #333}
.vhdr-rk{font-size:13px;font-weight:bold}
.vhdr-floor{font-size:10px;color:#0ca}
.vhdr-time{position:sticky;top:0;left:0;z-index:6;background:#111128;color:#99b;min-width:42px;padding:5px 4px;text-align:center;border-bottom:2px solid #333}
.vcell{padding:3px 5px;font-size:11px;white-space:nowrap;border-bottom:1px solid #151525;text-align:center;color:#ddd;overflow:hidden;text-overflow:ellipsis}
.vtime{position:sticky;left:0;z-index:2;background:#0a0a1a;color:#aab;font-size:10px;min-width:42px;text-align:center;font-weight:normal;padding:3px 4px;border-bottom:1px solid #151525}
.vtime.vrow-cur{background:#0a1a0a;color:#4f4;font-weight:bold}
.vcell.vrow-cur{background:#0a1a0a}
.vcell-en{font-family:system-ui,-apple-system,sans-serif}
.hidden{display:none!important}
@media(max-width:600px){
.tb-group{width:100%}
.tb-row3{justify-content:flex-end}
.vcell,.vhdr{padding:3px 3px;font-size:10px}
.vtime{font-size:9px;min-width:36px}
}
</style>
</head>
<body>
<div id="app">
<div class="topbar">
<div class="tb-group tb-row1">
<label>Rank</label>
<select id="inRank" onchange="onInputChange()">
<option value="02">02</option><option value="38">38</option><option value="3D">3D</option>
<option value="4C">4C</option><option value="51">51</option><option value="65">65</option>
<option value="79">79</option><option value="8D">8D</option><option value="A1">A1</option>
<option value="B5">B5</option><option value="C9">C9</option><option value="DD" selected>DD</option>
</select>
<label>Seed</label>
<input id="inSeed" value="263C" maxlength="4" inputmode="text" oninput="debouncedInput()">
<select id="presets" onchange="applyPreset()" style="color:#0ca;flex:1;min-width:0;">
<option value="">— Presets —</option>
<option value="DD,263C">【あら風73】あらぶる風の巣Lv73(DD 263C)</option>
<option value="B5,3CA2">【川崎ロッカー】あらぶる光の世界Lv86(B5 3CA2)</option>
<option value="DD,2E7A">【ましゃ725ソーマ】残された空の道Lv70(DD 2E7A)</option>
<option value="C9,7FE0">【ゆりっぺ】あらぶる夢の水脈Lv64(C9 7FE0)</option>
<option value="C9,2AC6">【ゆりっぺ大富豪】見えざる獣の墓場Lv73(C9 2AC6)</option>
<option value="DD,32BB">【ジャギ大富豪】大いなる岩の世界Lv63(DD 32BB)</option>
<option value="DD,235E">【ルイスタソ大富豪】とどろく影の世界Lv78(DD 235E)</option>
<option value="C9,158D">【ウミサンゴ大富豪】けだかき花の世界Lv61(C9 158D)</option>
<option value="DD,5C43">【TK89】見えざる光の眠る地Lv89(DD 5C43)</option>
<option value="DD,47D0">【御柱げんま】とどろく花のじごくLv67(DD 47D0)</option>
</select>
</div>
<div class="tb-group tb-row2">
<button class="filter-btn active" data-f="ALL" onclick="setFilter('ALL')">All</button>
<button class="filter-btn" data-f="SA" onclick="setFilter('SA')">SA</button>
<button class="filter-btn" data-f="AB" onclick="setFilter('AB')">AB</button>
<button class="filter-btn" id="btnLang" onclick="toggleLang()" style="color:#0ca;border-color:#0ca;">JP</button>
<select id="customHL1" onchange="applyCustomHL()" style="background:#801;color:#fff;max-width:110px;font-size:10px;"><option value="">— HL1 —</option></select>
<select id="customHL2" onchange="applyCustomHL()" style="background:#3f48cc;color:#fff;max-width:110px;font-size:10px;"><option value="">— HL2 —</option></select>
</div>
<div class="tb-group tb-row3">
<div class="timer-box">
<label style="color:#88b;font-size:11px;">Start</label>
<select id="timerStart" style="background:#000;color:#ffd700;border:1px solid #555;padding:2px 4px;font-size:12px;font-family:inherit;border-radius:3px;height:26px;width:52px;">
<option value="-3">-3</option>
<option value="0">0</option>
<option value="4">4</option>
<option value="4.5">4.5</option>
<option value="5">5</option>
</select>
<div class="timer-display" id="timerDisp"><span id="timerText">-03.00</span></div>
<button class="ctrl-btn start" id="btnStart" onclick="toggleTimer()">▶</button>
<button class="ctrl-btn reset" id="btnReset" onclick="resetTimer()">⟲</button>
</div>
</div>
</div>
<div id="mainArea"></div>
</div>
<script>
//===Engine Code ===
const TableA=new Uint8Array([1,30,0,0,2,40,0,0,3,10,0,0,4,10,0,0,5,10,0,0]);
const TableB=new Uint8Array([2,55,2,4,56,75,4,6,76,100,6,10,101,120,8,12,121,140,10,14,141,180,10,16,181,200,11,16,201,220,12,16,221,248,14,16]);
const TableC=new Uint8Array([2,55,1,3,56,75,2,4,76,100,3,5,101,140,4,6,141,180,5,7,181,200,6,9,201,220,8,9,221,248,9,9]);
const TableD=new Uint8Array([2,60,1,3,61,80,2,5,81,100,3,7,101,120,4,7,121,140,5,9,141,160,6,9,161,180,7,10,181,200,8,12,201,248,1,12]);
const TableE=new Uint8Array([1,100,2,100,3,75,4,75,5,50,6,50,7,30,8,20,9,20,10,20,11,10,12,10]);
const TableF=new Uint8Array([1,1,2,0,2,1,2,0,3,1,3,0,4,1,4,0,5,2,5,0,6,2,6,0,7,3,7,0,8,3,8,0,9,4,9,0,10,5,9,0,11,1,10,0,12,4,10,0]);
const TableG=new Uint8Array([2,3,1,2,4,5,1,3,6,7,1,4,8,9,2,5,10,11,2,6,12,13,3,7,14,15,4,8,16,16,6,8]);
const TableH=new Uint8Array([1,2,1,5,3,4,4,8,5,6,7,12,7,8,7,16,9,9,12,16]);
const TableI=new Uint8Array([1,3,1,6,4,6,4,9,7,9,7,12,10,12,10,16]);
const TableO=new Uint8Array([0,14,28,41,55,71,88,108,125,141,162]);
const TableP=new Uint8Array([10,10,5,10,10,8,5,8,10,10,2,8,2,2,8,8,10,2,10,10,10,10,8,8,10,2,2,2,10,10,10,10,10,10,10,1,10,10,5,2,2,5,15,15,12,10,15,12,2,5,5,1,1,1,1,5,15,10,15,1,1,1,1,1,1,10,10,1,12,1,15,10,15,15,15,10,6,1,1,10,1,10,1,1,1,1,1,1,10,10,10,10,10,15,6,2,2,15,1,1,1,1,1,1,1,1,1,1,5,13,13,10,15,15,15,5,1,1,1,1,1,1,1,1,1,10,10,10,10,10,15,10,10,8,1,1,1,1,1,1,1,15,10,10,10,15,10,8,5,5,1,1,1,1,1,1,1,1,1,1,1,1]);
const TableQ=new Uint8Array([12,14,16,15,7,18,28,23,27,0,29,13,30,31,32,33,34,35,36,22,19,1,7,46,37,38,39,40,140,41,11,42,7,2,43,44,45,49,8,50,51,139,52,53,11,8,16,3,54,47,48,76,55,56,57,139,24,58,16,59,60,61,62,63,64,65,66,67,11,68,20,139,21,69,8,70,71,72,73,74,75,11,77,78,79,80,81,82,139,83,84,85,17,4,25,86,87,11,88,89,90,91,92,93,94,95,96,97,98,99,100,17,5,11,9,10,101,102,103,104,105,106,107,108,109,110,111,112,6,17,11,9,113,114,115,116,117,118,119,120,121,122,123,9,124,17,125,26,10,126,127,128,129,130,131,132,133,134,135,136,137,138]);
const TableR=[
["125G","125G"],["268G","268G"],["450G","450G"],["670G","670G"],["880G","880G"],["1500G","1500G"],["3000G","3000G"],
["Gleeban groat","グビアナどうか"],["Gleeban guinea","グビアナぎんか"],["Gleeban gold piece","グビアナきんか"],["Gold bar","きんかい"],
["Mini medal","ちいさなメダル"],["Medicinal herb","やくそう"],["Strong medicine","上やくそう"],["Evac-u-bell","おもいでのすず"],
["Holy water","せいすい"],["Magic water","まほうのせいすい"],["Sage's elixir","けんじゃのせいすい"],["Antidotal herb","どくけしそう"],
["Strong antidote","上どくけしそう"],["Narspicious","あやかしそう"],["Mystifying mixture","おかしなくすり"],["Superior medicine","いやしそう"],
["Moonwort bulb","まんげつそう"],["Panacea","ばんのうくすり"],["Perfect panacea","超ばんのうくすり"],["Yggdrasil leaf","せかいじゅのは"],
["Chimaera wing","キメラのつばさ"],["Oaken club","こんぼう"],["Pop socks","ニーソックス"],["Silver bracelets","ぎんのリスト"],
["Bunny tail","うさぎのおまもり"],["Royal soil","まりょくの土"],["Lava lump","ようがんのカケラ"],["Angel bell","天使のすず"],
["Silver platter","シルバートレイ"],["Fisticup","げんこつダケ"],["Iron nails","てつのクギ"],["Gold ring","きんのゆびわ"],
["Gold bracer","きんのプレスレット"],["Iron mask","てっかめん"],["Toad oil","ガマのあぶら"],["Fisticup","げんこつダケ"],
["Iron ore","てっこうせき"],["Slime shield","スライムトレイ"],["Corundum","ルビーのげんせき"],["Rockbomb shard","ばくだん石"],
["Flintstone","つけもの石"],["Mirrorstone","かがみ石"],["Resurrock","命の石"],["Strength ring","ちからのゆびわ"],
["Agility ring","はやてのリング"],["Manky mud","どくどくヘドロ"],["Nectar","花のみつ"],["Sorcerer's stone","ひらめきのジュエル"],
["Glombolero","ふしぎなボレロ"],["Saint's ashes","せいじゃのはい"],["Malicite","うらみのほうじゅ"],["Hephaestus' flame","ヘパイトスのひだね"],
["Muscle belt","あらくれベルト"],["Maid outfit","メイド服"],["Thug boots","あらくれブーツ"],["Thug's mug","あらくれマスク"],
["Maid's mop","ヘッドドレス"],["Toughie trousers","あらくれズボン"],["Finessence","ぶどうエキス"],["Aggressence","とうこんエキス"],
["Dangerous bustier","あぶないビスチェ"],["Brouhaha boomstick","まてきの杖"],["Hephaestus' flame","ヘパイトスのひだね"],["Astral plume","天使のはね"],
["Densinium","ヘビーメタル"],["Riotous wristbands","ぶしんのリスト"],["Fingerless gloves","オープンフィンガー"],["Mythril ore","ミスリルこうせき"],
["Veteran's gloves","古強者のグローブ"],["Fuddle bow","ゆうわくの弓"],["Oh-no bow","じごくの弓"],["Blessed boots","しんかんのブーツ"],
["Skull ring","ドクロのゆびわ"],["Hela's hammer","まじんのかなづち"],["Hades' helm","サタンヘルム"],["Demon whip","あくまのムチ"],
["Saint's ashes","せいじゃのはい"],["Densinium","ヘビーメタル"],["Lucida shard","ほしのカケラ"],["Depressing shoes","しわよせのくつ"],
["Unhappy hat","しわよせのぼうし"],["Veteran's armour","古強者のよろい"],["Spellspadrilles","だいまどうシューズ"],["Veteran's boots","古強者のブーツ"],
["Combat boots","ぶしんのブーツ"],["She-mage shoes","まじょのブーツ"],["Trinity tights","しんかんのタイツ"],["Ruinous shield","はめつの盾"],
["Divine dress","さとりのワンピース"],["Skull helm","ドクロのかぶと"],["Matador's gloves","マタドールグラブ"],["Pandora's box","パンドラボックス"],
["Enchanted stone","せいれいせき"],["Mythril ore","ミスリルこうせき"],["Hero spear","えいゆうのやり"],["Pruning knife","こがらしのダガー"],
["Wyrmwand","ドラゴンの杖"],["Wizardly whip","カルベロビュート"],["Beast claws","まじゅうのツメ"],["Attribeauty","風林火山"],
["Heavy hatchet","ふんさいのおおなた"],["Megaton hammer","メガトンハンマー"],["Pentarang","ペンタグラム"],["Pandora's box","パンドラボックス"],
["Astral plume","天使のはね"],["Ethereal stone","げんませき"],["Reckless necklace","しにがみの首かざり"],["Orichalcum","オリハルコン"],
["Metal slime sword","メタスラの剣"],["Metal slime spear","メタスラのやり"],["Metal slime shield","メタスラの盾"],["Metal slime armour","メタスラよろい"],
["Metal slime helm","メタスラヘルム"],["Metal slime gauntlets","メタスラのこて"],["Metal slime sollerets","メタスラブーツ"],["Pandora's box","パンドラボックス"],
["Reset stone","リサイクルストーン"],["Evac-u-bell","おもいでのすず"],["Sainted soma","天使のソーマ"],["Orichalcum","オリハルコン"],
["Stardust sword","ほしくずのつるぎ"],["Poker","きしんのまそう"],["Deft dagger","サウザンドダガー"],["Bright staff","ひかりの杖"],
["Gringham whip","グリンガムのムチ"],["Knockout rod","しゅらのこん"],["Dragonlord claws","竜王のツメ"],["Critical fan","ひっさつのおうぎ"],
["Bad axe","グレートアックス"],["Groundbreaker","大地くだき"],["Meteorang","メテオエッジ"],["Angel's bow","天使の弓"],
["Mimic","ミミック"],["Cannibox","ひとくいばこ"]
];
function lcg(seed){return(Math.imul(seed,1103515245)+12345)>>>0;}
// =============================================
class GrottoDetail{
constructor(){
// _dungeonInfo: 16 floors,each 1336 bytes
this.di=[];
for(let i=0;i<16;i++)this.di[i]=new Uint8Array(1336);
this._details=new Uint8Array(20);
this._seed=0;
this.MapSeed=0;
this.MapRank=0;
}
// --- Helper: read/write little-endian int32 from _dungeonInfo ---
_readI32(floor,offset){
const d=this.di[floor];
return d[offset]|(d[offset+1]<<8)|(d[offset+2]<<16)|(d[offset+3]<<24);
}
_writeI32(floor,offset,val){
const d=this.di[floor];
d[offset]=val & 0xFF;
d[offset+1]=(val>>>8) & 0xFF;
d[offset+2]=(val>>>16) & 0xFF;
d[offset+3]=(val>>>24) & 0xFF;
}
// --- RNG ---
gRNG(){
this._seed=lcg(this._seed);
return(this._seed>>>16) & 0x7FFF;
}
gRNGDiv(div){
return this.gRNG()%div;
}
gRNGRange(v1,v2){
if(v1===v2)return v1;
const r=this.gRNG();
const num=(v2|0)-(v1|0)+1;
return num===0?v1:((v1+r%num)>>>0);
}
// --- Table lookups ---
getItemRank(value1,value2){
const num=Math.fround(this.gRNG()-1);
return(Math.fround(((value2|0)-(value1|0)+1)*num / 32767)>>>0)+value1;
}
seek1(table,tableSize){
const random=this.gRNGDiv(100);
let num=0;
for(let i=0;i<tableSize;i++){
num+=table[i*4+1];
if(random<num)return table[i*4];
}
return 0;
}
seek2(table,value,tableSize){
for(let i=0;i<tableSize;i++)
if(value>=table[i*4]&&value<=table[i*4+1])
return this.seek3(table[i*4+2],table[i*4+3]);
return 0;
}
seek3(val1,val2){
const r=this.gRNG();
const num=val2-val1+1;
return num===0?val1:(val1+r%num);
}
seek4(table1,table2,roopCount){
for(let i1=0;i1<roopCount;i1++){
if(this.MapRank>=table1[i1*4]&&this.MapRank<=table1[i1*4+1]){
let num1=0;
for(let i2=table1[i1*4+2];i2<=table1[i1*4+3];i2++)
num1+=table2[(i2-1)*2+1];
const num2=this.gRNG()%num1;
let num3=0;
for(let i3=table1[i1*4+2];i3<=table1[i1*4+3];i3++){
num3+=table2[(i3-1)*2+1];
if(num2<num3)return i3;
}
break;
}
}
return 0;
}
routineRandom(value){
return(Math.fround(this.gRNG()-1)*value / 32767)>>>0;
}
// --- routine1: memset ---
routine1(floor,address,value1,value2){
for(let i=0;i<value2;i++)
this.di[floor][address+i]=value1 & 0xFF;
}
// --- SetValue ---
setValue(floor,address,v1,v2,v3,v4){
const d=this.di[floor];
d[address]=v1;d[address+1]=v2;d[address+2]=v3;d[address+3]=v4;
}
// --- routineA: horizontal split ---
routineA(floor,address,value1,value2){
const d=this.di[floor];
const num1=d[address+3]+1-d[address+1];
if(num1<7||d[address+4]!==0)return false;
const random=this.gRNGRange(0,num1-7);
const num2=d[address+1]+random+3;
for(let i=d[address];i<d[address+2]+1;i++)
d[(num2<<4)+i+792]=3;
this.setValue(floor,value2,d[address],num2,d[address+2],num2);
this.setValue(floor,value1,d[address],num2+1,d[address+2],d[address+3]);
d[value1+4]=0;d[value1+5]=0;
d[address+3]=num2-1;
d[address+4]=1;
this._writeI32(floor,value2+4,address);
this._writeI32(floor,value2+8,value1);
d[value2+12]=1;
return true;
}
// --- routineE: vertical split ---
routineE(floor,address,value1,value2){
const d=this.di[floor];
const num1=d[address+2]+1-d[address];
if(num1<7||d[address+5]!==0)return false;
const random=this.gRNGRange(0,num1-7);
const num2=d[address]+random+3;
for(let i=d[address+1];i<d[address+3]+1;i++)
d[(i<<4)+num2+792]=3;
this.setValue(floor,value2,num2,d[address+1],num2,d[address+3]);
this.setValue(floor,value1,num2+1,d[address+1],d[address+2],d[address+3]);
d[value1+4]=0;d[value1+5]=0;
d[address+2]=num2-1;
d[address+5]=1;
this._writeI32(floor,value2+4,address);
this._writeI32(floor,value2+8,value1);
d[value2+12]=2;
return true;
}
// --- routineB: recursive split ---
routineB(floor,address){
const d=this.di[floor];
const num=d[21];
d[21]++;d[22]++;
if((this.gRNG() & 1)!==0){
this.routineF(floor,address);
this.routineF(floor,num*12+24);
}else{
this.routineF(floor,num*12+24);
this.routineF(floor,address);
}
}
// --- routineF: split decision ---
routineF(floor,address){
const d=this.di[floor];
if(d[21]>=15)return;
if(d[address+5]!==0){
if(!this.routineA(floor,address,d[21]*12+24,(d[22]<<4)+216))return;
this.routineB(floor,address);
} else if(d[address+4]!==0){
if(!this.routineE(floor,address,d[21]*12+24,(d[22]<<4)+216))return;
this.routineB(floor,address);
} else if((this.gRNG() & 1)!==0){
if(!this.routineE(floor,address,d[21]*12+24,(d[22]<<4)+216))return;
this.routineB(floor,address);
}else{
if(!this.routineA(floor,address,d[21]*12+24,(d[22]<<4)+216))return;
this.routineB(floor,address);
}
}
// --- routineC: create room ---
routineC(floor,address1,address2){
const d=this.di[floor];
if(d[address1+2]+1-d[address1]<3||d[address1+3]+1-d[address1+1]<3)
return false;
const num1=d[address1],num2=d[address1+1],num3=d[address1+2],num4=d[address1+3];
let random1,random2,random3,random4;
if(this.gRNGRange(0,1)!==0){
random1=this.gRNGRange(num1,num1+((num3-num1+1)/3|0));
random2=this.gRNGRange(num2,num2+((num4-num2+1)/3|0));
}else{
random1=this.gRNGRange(num1+1,num1+((num3-num1+1)/3|0));
random2=this.gRNGRange(num2+1,num2+((num4-num2+1)/3|0));
}
if(this.gRNGRange(0,1)!==0){
random3=this.gRNGRange(num1+((num3-num1+1)/3|0)*2,num3);
random4=this.gRNGRange(num2+((num4-num2+1)/3|0)*2,num4);
}else{
random3=this.gRNGRange(num1+((num3-num1+1)/3|0)*2,num3-1);
random4=this.gRNGRange(num2+((num4-num2+1)/3|0)*2,num4-1);
}
this.setValue(floor,address2,random1,random2,random3,random4);
for(let i=4;i<20;i++) d[address2+i]=0;
this._writeI32(floor,address1+8,address2);
for(let y=random2;y<=random4;y++)
for(let x=random1;x<=random3;x++)
d[x+(y<<4)+792]=0;
this.routineD(floor,address2);
return true;
}
// --- routineD: create doors ---
routineD(floor,address){
const d=this.di[floor];
const num1=d[address],num2=d[address+2];
if(num1===0||d[address+1]===0)return false;
if(num2===0||d[address+3]===0)return false;
// Top/bottom doors
if(num2-num1+1<5){
d[address+12]=this.gRNGRange(num1,num2);
d[address+13]=d[address+1];
d[address+16]=this.gRNGRange(d[address],d[address+2]);
d[address+17]=d[address+3];
d[(d[address+13]<<4)+792+d[address+12]]=8;
d[(d[address+17]<<4)+792+d[address+16]]=8;
}else{
const num3=num1+((num2-num1+1)/2|0)-1;
d[address+12]=this.gRNGRange(num1,num3);
d[address+13]=d[address+1];
d[address+14]=this.gRNGRange(num3+1,d[address+2]);
d[address+15]=d[address+1];
d[address+16]=this.gRNGRange(d[address],num3);
d[address+17]=d[address+3];
d[address+18]=this.gRNGRange(num3+1,d[address+2]);
d[address+19]=d[address+3];
d[(d[address+13]<<4)+792+d[address+12]]=8;
d[(d[address+15]<<4)+792+d[address+14]]=8;
d[(d[address+17]<<4)+792+d[address+16]]=8;
d[(d[address+19]<<4)+792+d[address+18]]=8;
}
// Left/right doors
if(d[address+3]-d[address+1]+1<5){
d[address+4]=d[address];
d[address+5]=this.gRNGRange(d[address+1],d[address+3]);
d[address+8]=d[address+2];
d[address+9]=this.gRNGRange(d[address+1],d[address+3]);
d[(d[address+5]<<4)+792+d[address+4]]=8;
d[(d[address+9]<<4)+792+d[address+8]]=8;
}else{
const num4=d[address+1];
const num5=num4+((d[address+3]+1-num4)/2|0)-1;
d[address+4]=d[address];
d[address+5]=this.gRNGRange(d[address+1],num5);
d[address+6]=d[address];
d[address+7]=this.gRNGRange(num5+1,d[address+3]);
d[address+8]=d[address+2];
d[address+9]=this.gRNGRange(d[address+1],num5);
d[address+10]=d[address+2];
d[address+11]=this.gRNGRange(num5+1,d[address+3]);
d[(d[address+5]<<4)+792+d[address+4]]=8;
d[(d[address+7]<<4)+792+d[address+6]]=8;
d[(d[address+9]<<4)+792+d[address+8]]=8;
d[(d[address+11]<<4)+792+d[address+10]]=8;
}
return true;
}
// --- GenerateFloorMap: wall erosion ---
generateFloorMap(floor,address){
const d=this.di[floor];
const index1=this._readI32(floor,address+8);
const num1=((d[index1+2]-d[index1]+1)/2)|0;
const num2=((d[index1+3]-d[index1+1]+1)/2)|0;
let num3=0;
if(d[1]===0&&this.gRNGRange(0,15)===0)
num3=1;
let num4,num5,num6,num7;
if(num3===0){
num4=d[address+3]-d[index1+3];
num5=d[index1]-d[address];
num6=d[address+2]-d[index1+2];
num7=d[index1+1]-d[address+1];
}else{
num4=d[3]-d[index1+3]-1;
num5=d[index1]-1;
num6=d[2]-d[index1+2]-1;
num7=d[index1+1]-1;
d[1]=1;
}
// Top edge erosion
for(let i3=d[index1];i3<=d[index1+2];i3++){
const num8=d[i3+(d[index1+1]<<4)+792];
if(num8===1||num8===3)continue;
if(this.gRNGRange(0,1)===0){
if(num8!==8){
const above=d[i3+((d[index1+1]-1)<<4)+792];
if(above===1||above===8)continue;
const random1=this.gRNGRange(0,num2);
const num9=i3-1;
for(let i4=0;i4<random1&&
d[i3+((d[index1+1]+i4)<<4)+792]!==8&&
d[i3+((d[index1+1]+i4+1)<<4)+792]!==1&&
(d[((d[index1+1]+i4)<<4)+i3+1+792]===1 ||
d[((d[index1+1]+i4+1)<<4)+i3+1+792]!==1)&&
(d[((d[index1+1]+i4)<<4)+num9+792]===1 ||
d[num9+((d[index1+1]+i4+1)<<4)+792]!==1);
i4++)
d[i3+((d[index1+1]+i4)<<4)+792]=1;
}
}else{
const random2=this.gRNGRange(0,num7);
for(let i5=0;i5<random2;i5++){
const idx=i3+((d[index1+1]-1-i5)<<4)+792;
if(d[idx]!==8) d[idx]=0;
}
}
}
// Bottom edge erosion
for(let i7=d[index1];i7<=d[index1+2];i7++){
const num10=d[i7+(d[index1+3]<<4)+792];
if(num10===1||num10===3)continue;
if(this.gRNGRange(0,1)!==0){
const random=this.gRNGRange(0,num4);
for(let i8=0;i8<random;i8++)
if(d[i7+((d[index1+3]+i8+1)<<4)+792]!==8)
d[i7+((d[index1+3]+i8+1)<<4)+792]=0;
}else{
if(num10!==8){
const below=d[i7+((d[index1+3]+1)<<4)+792];
if(below===1||below===8)continue;
const random3=this.gRNGRange(0,num2);
const num11=i7-1;
for(let i9=0;i9<random3&&
d[i7+((d[index1+3]-i9)<<4)+792]!==8&&
d[i7+((d[index1+3]-i9-1)<<4)+792]!==1&&
(d[((d[index1+3]-i9)<<4)+i7+1+792]===1 ||
d[((d[index1+3]-i9-1)<<4)+i7+1+792]!==1)&&
(d[num11+((d[index1+3]-i9)<<4)+792]===1 ||
d[num11+((d[index1+3]-i9-1)<<4)+792]!==1);
i9++)
d[i7+((d[index1+3]-i9)<<4)+792]=1;
}
}
}
// Left edge erosion
for(let i10=d[index1+1];i10<=d[index1+3];i10++){
const num12=d[(i10<<4)+792+d[index1]];
if(num12===1||num12===3)continue;
if(this.gRNGRange(0,1)!==0){
const random=this.gRNGRange(0,num5);
for(let i11=0;i11<random;i11++)
if(d[(i10<<4)+792+d[index1]-1-i11]!==8)
d[(i10<<4)+792+d[index1]-1-i11]=0;
}else{
if(num12!==8){
const left=d[(i10<<4)+792+d[index1]-1];
if(left===1||left===8)continue;
const random4=this.gRNGRange(0,num1);
const num13=i10-1;
for(let i12=0;i12<random4&&
d[i12+(i10<<4)+d[index1]+792]!==8&&
d[i12+(i10<<4)+d[index1]+792+1]!==1&&
(d[i12+((i10+1)<<4)+d[index1]+792]===1 ||
d[i12+((i10+1)<<4)+d[index1]+792+1]!==1)&&
(d[i12+(num13<<4)+d[index1]+792]===1 ||
d[i12+(num13<<4)+d[index1]+792+1]!==1);
i12++)
d[i12+(i10<<4)+d[index1]+792]=1;
}
}
}
// Right edge erosion
for(let i13=d[index1+1];i13<=d[index1+3];i13++){
const num14=d[(i13<<4)+792+d[index1+2]];
if(num14===1||num14===3)continue;
if(this.gRNGRange(0,1)!==0){
const random=this.gRNGRange(0,num6);
for(let i14=0;i14<random;i14++)
if(d[i14+(i13<<4)+d[index1+2]+792+1]!==8)
d[i14+(i13<<4)+d[index1+2]+792+1]=0;
}else{
if(num14!==8){
const right=d[d[index1+1]+(i13<<4)+792+1];
if(right===1||right===8)continue;
const random5=this.gRNGRange(0,num1);
const num15=i13-1;
for(let i15=0;i15<random5&&d[(i13<<4)+792+d[index1+2]-i15]!==8&&d[(i13<<4)+792+d[index1+2]-i15-1]!==1&&(d[((i13+1)<<4)+792+d[index1+2]-i15]===1||d[((i13+1)<<4)+792+d[index1+2]-i15-1]!==1)&&(d[(num15<<4)+792+d[index1+2]-i15]===1||d[(num15<<4)+792+d[index1+2]-i15-1]!==1);i15++)
d[(i13<<4)+792+d[index1+2]-i15]=1;
}
}
}
}
// --- routineI: tile classification ---
routineI(floor,value1,value2,value3){
const d=this.di[floor];
const num1=d[value1+(value2<<4)+792];
if(num1===1||num1===3)return 255;
const num2=d[value1+((value2-1)<<4)+792];
const num3=d[value1+((value2-1)<<4)+792-1];
const num4=d[value1+((value2-1)<<4)+792+1];
const num5=d[value1+(value2<<4)+792+1];
const num6=d[value1+((value2+1)<<4)+792+1];
const num7=d[value1+((value2+1)<<4)+792];
const num8=d[value1+((value2+1)<<4)+792-1];
const num9=d[value1+(value2<<4)+792-1];
if(num1!==0&&num1!==2&&((num1+252) & 255)>4)return value3;
if(num3===1||num3===3)value3|=128;else value3&=127;
if(num4===1||num4===3)value3|=32;else value3&=223;
if(num6===1||num6===3)value3|=8;else value3&=247;
if(num8===1||num8===3)value3|=2;else value3&=253;
if(num2===1||num2===3)value3|=224;else value3&=191;
if(num5===1||num5===3)value3|=56;else value3&=239;
if(num7===1||num7===3)value3|=14;else value3&=251;
if(num9===1||num9===3)value3|=131;else value3&=254;
return value3;
}
// --- routineG: corridor generation ---
routineG(floor,address){
const d=this.di[floor];
const address3=address;
if(d[address3+12]===1){
const parentAddr=this._readI32(floor,address3+4);
const address1=this._readI32(floor,parentAddr+8);
const siblingAddr=this._readI32(floor,address3+8);
const address2=this._readI32(floor,siblingAddr+8);
const num1=this.gRNGRange(0,7)===0?1:0;
this.routineH(floor,address1,16,address2,12,address3,num1);
const num2=this.gRNGRange(0,7)===0?1:0;
this.routineH(floor,address1,18,address2,12,address3,num2);
const num3=this.gRNGRange(0,7)===0?1:0;
this.routineH(floor,address1,16,address2,14,address3,num3);
const num4=this.gRNGRange(0,7)===0?1:0;
this.routineH(floor,address1,18,address2,14,address3,num4);
} else if(d[address3+12]===2){
const parentAddr=this._readI32(floor,address3+4);
const address1=this._readI32(floor,parentAddr+8);
const siblingAddr=this._readI32(floor,address3+8);
const address2=this._readI32(floor,siblingAddr+8);
const num5=this.gRNGRange(0,7)===0?1:0;
this.routineH(floor,address1,8,address2,4,address3,num5);
const num6=this.gRNGRange(0,7)===0?1:0;
this.routineH(floor,address1,10,address2,4,address3,num6);
const num7=this.gRNGRange(0,7)===0?1:0;
this.routineH(floor,address1,8,address2,6,address3,num7);
const num8=this.gRNGRange(0,7)===0?1:0;
this.routineH(floor,address1,10,address2,6,address3,num8);
}
return 1;
}
// --- routineH: draw corridor between rooms ---
routineH(floor,address1,value1,address2,value2,address3,value3){
const d=this.di[floor];
const index1=address3;
const num1=d[address1+value1];
const num2=d[address1+value1+1];
const num3=d[address2+value2];
const num4=d[address2+value2+1];
if(num1===0||num2===0||num3===0||num4===0)return false;
// Helper: One-way extend corridor to the wall or non-corridor
const drawExt=(start,step,isHoriz,fixedCoord)=>{
for(let i=start+step;i>=0&&i<16;i+=step){
const idx=isHoriz?(i+(fixedCoord<<4)+792):((i<<4)+792+fixedCoord);
const v=d[idx];
if(v===1||v===3)continue;// Wall
for(let j=start+step;j!==i;j+=step){
const fillIdx=isHoriz?(j+(fixedCoord<<4)+792):((j<<4)+792+fixedCoord);
d[fillIdx]=2;// Corridor
}
break;
}
};
if(d[address3+12]===1){
// Horizontal split-vertical corridors
for(let i=num2+1;i<d[index1+1]+1;i++)
d[(i<<4)+792+num1]=2;
for(let i=num4-1;i>d[index1+1];i--)
d[(i<<4)+792+num3]=2;
if(num1<num3)
for(let i=num1;i<num3+1;i++)
d[i+(d[index1+1]<<4)+792]=2;
else if(num1>num3)
for(let i=num3;i<num1+1;i++)
d[i+(d[index1+1]<<4)+792]=2;
if(value3===0)return true;
if(num1<num3){
drawExt(num3,1,true,d[index1+1]);// ext1
drawExt(num1,-1,true,d[index1+1]);
}else{
drawExt(num1,1,true,d[index1+1]);// ext2
drawExt(num3,-1,true,d[index1+1]);
}
}else if(d[address3+12]===2){
// Vertical split-horizontal corridors
for(let i=num1+1;i<d[index1]+1;i++)
d[i+(num2<<4)+792]=2;
for(let i=num3-1;i>d[index1];i--)
d[i+(num4<<4)+792]=2;
if(num2<num4)
for(let i=num2;i<num4+1;i++)
d[(i<<4)+792+d[index1]]=2;
else if(num2>num4)
for(let i=num4;i<num2+1;i++)
d[(i<<4)+792+d[index1]]=2;
if(value3===0)return true;
if(num2<num4){
drawExt(num4,1,false,d[index1]);// ext3
drawExt(num2,-1,false,d[index1]);
}else{
drawExt(num2,1,false,d[index1]);// ext4
drawExt(num4,-1,false,d[index1]);
}
}
return true;
}
// --- routineJ: place stairs ---
routineJ(floor){
const d=this.di[floor];
let num1=0,num2=0;
let random1=0,random2=0;
let random3,random4,random5;
while (true){
random3=this.gRNGRange(0,d[23]-1);
const idx1=random3*20+472;
random4=this.gRNGRange(d[idx1],d[idx1+2]);
random5=this.gRNGRange(d[idx1+1],d[idx1+3]);
if(num2<100){
const n3=d[((random5-1)<<4)+random4+792];
const n4=d[((random5+1)<<4)+random4+792];
const n5=d[(random5<<4)+792+random4-1];
const n6=d[(random5<<4)+792+random4+1];
const w7=(n3===1||n3===3)?1:0;
const w8=(n4===1||n4===3)?1:0;
const w9=(n5===1||n5===3)?1:0;
const w10=(n6===1||n6===3)?1:0;
if((w7||w8)&&(w9||w10)){num2++;continue;}
const ri=this.routineI(floor,random4,random5,0) & 255;
if([46,58,139,142,163,171,174,184,186,226,232,234].includes(ri)){num2++;continue;}
}
d[random4+(random5<<4)+792]=4;
d[4]=random4;d[5]=random5;
// ========================================================
// Ghost stair fix: on overflow (num2>=100),game resets counters
// but still runs do-while DOWN search. If DOWN rejected,num2
// becomes 1,then 99 more rejects before next overflow.
// ========================================================
if(num2>=100){num1=0;num2=0;}
// =================================================================
// Original reverse-engineered C# was considered wrong. Its num13=0 made the stairs wrongly placed
// =================================================================
random1=random4;random2=random5;let num13=random3;
do{
const random6=this.gRNGRange(0,d[23]-1);
if(random6===(random3 & 255)&&(num1 & 255)<25){
num1++;
}else{
const idx2=random6*20+472;
random1=this.gRNGRange(d[idx2],d[idx2+2]);
num13=random6;
random2=this.gRNGRange(d[idx2+1],d[idx2+3]);
}
}while((random3 & 255)===num13&&random1===(random4 & 0xFFFF)&&random2===(random5 & 0xFFFF));
const n14=d[random1+((random2-1)<<4)+792];
const n15=d[random1+((random2+1)<<4)+792];
const n16=d[random1+(random2<<4)+792-1];
const n17=d[random1+(random2<<4)+792+1];
const w18=(n14===1||n14===3)?1:0;
const w19=(n15===1||n15===3)?1:0;
const w20=(n16===1||n16===3)?1:0;
const w21=(n17===1||n17===3)?1:0;
if((w18||w19)&&(w20||w21)){num2++;}
else break;
}
d[random1+(random2<<4)+792]=5;
d[6]=random1;d[7]=random2;
}
// --- routineK: place treasure boxes ---
routineK(floor){
const d=this.di[floor];
const random1=this.gRNGRange(1,3);
d[8]=random1;
let num1=0,num2=0;
do{
const idx=this.gRNGRange(0,d[23]-1)*20+472;
const random2=this.gRNGRange(d[idx],d[idx+2]);
const random3=this.gRNGRange(d[idx+1],d[idx+3]);
const num3=d[random2+((random3 & 255)<<4)+792];
if(num1<100&&(d[0]===3||d[0]===1)){
num1++;
}else if(num3===6||num3===4||num3===5){
num1++;
}else{
d[random2+((random3 & 255)<<4)+792]=6;
d[num2*2+13]=random2;
d[num2*2+14]=random3;
num2++;
}
}while(num2<(random1 & 255));
return 1;
}
// --- Main calculation ---
calculateDetail(){
for(let i=0;i<16;i++){
this.di[i].fill(0);
}
this._details.fill(0);
if(this.MapRank<2||this.MapRank>248)return;
this._seed=this.MapSeed;
// Skip 12 random calls
for(let i=0;i<12;i++)this.gRNGDiv(100);
this._details[3]=this.seek1(TableA,5);
this._details[1]=this.seek2(TableB,this.MapRank,9);
this._details[2]=this.seek2(TableC,this.MapRank,8);
this._details[0]=this.seek4(TableD,TableE,9);
for(let i=0;i<12;i++)
this._details[i+1+7]=this.seek3(TableF[i*4+1],TableF[i*4+2]);
this._details[5]=this.seek2(TableH,this._details[2],5);
this._details[6]=this.seek2(TableI,this._details[0],4);
this._details[7]=this.seek2(TableG,this._details[1],8);
let num1=(this._details[0]+this._details[1]+this._details[2]-4)*3+(this.gRNGDiv(11)-5);
if(num1<1) num1=1;
if(num1>99) num1=99;
this._details[4]=num1;
// Floor dimensions
for(let i=1;i<this._details[1]+1;i++){
let num9;
if(i>12) num9=16;
else if(i>8) num9=(this.MapSeed+i)%3+14;
else if(i>4) num9=(this.MapSeed+i)%4+12;
else num9=(this.MapSeed+i)%5+10;
this.di[i-1][2]=num9;
this.di[i-1][3]=num9;
}
this.createDungeonDetail();
}
createDungeonDetail(){
for(let index1=1;index1<this._details[1]+1;index1++){
const floor=index1-1;
const d=this.di[floor];
d[0]=index1;
d[8]=0;
this._seed=(this.MapSeed+index1)>>>0;
this.routine1(floor,792,1,256);
d[21]=1;d[22]=0;d[23]=0;d[1]=0;
this.setValue(floor,24,1,1,d[2]-2,d[3]-2);
d[28]=0;d[29]=0;
this.routineF(floor,24);
for(let i2=0;i2<d[21];i2++)
if(this.routineC(floor,i2*12+24,d[23]*20+472))
d[23]++;
for(let i3=0;i3<d[21];i3++)
this.generateFloorMap(floor,i3*12+24);
for(let i4=0;i4<d[22];i4++)
this.routineG(floor,(i4<<4)+216);
// Border walls
for(let i5=0;i5<d[2];i5++){
d[i5+792]=1;
d[((d[3]-1)<<4)+i5+792]=1;
}
for(let i6=0;i6<d[3];i6++){
d[(i6<<4)+792]=1;
d[(i6<<4)+792+d[2]-1]=1;
}
this.routineJ(floor);
if(d[0]<=2)d[8]=0;
else this.routineK(floor);
}
// Treasure box ranks (floors 3+)
for(let i12=2;i12<this._details[1];i12++){
const d=this.di[i12];
this._seed=(this.MapSeed+i12+1)>>>0;
for(let i13=0;i13<d[8]<<1;i13++) this.gRNG();
const num=this._details[2]+(i12/4|0);
for(let i14=0;i14<d[8];i14++){
d[i14+9]=this.getItemRank(TableF[(num-1)*4+1],TableF[(num-1)*4+2]);
}
}
}
// --- Accessors ---
get floorCount(){return this._details[1];}
getBoxCount(f){return this.di[f][8];}
getBoxInfo(f,i){
const d=this.di[f];
return{rank: d[9+i],x: d[i*2+13],y: d[i*2+14]};
}
getBoxItem(floor,boxIndex,second){
this._seed=(this.di[floor][0]+this.MapSeed+second)>>>0;
for(let i1=0;i1<this.di[floor][8];i1++){
const num1=this.routineRandom(100);
if(i1===boxIndex){
const index2=this.di[floor][i1+9];
const num2=TableO[index2-1],num3=TableO[index2];
let num4=0;
for(let i3=num2;i3<num3;i3++){
num4+=TableP[i3];
if(num1<num4)return [TableR[TableQ[i3]][0],TableR[TableQ[i3]][1]];
}
}
}
return [null,null];
}
}
//===Marathon Logic ===
const RANK_COLORS={10:'#f4f',9:'#fa0',8:'#4cf',7:'#8f8',6:'#ff8',5:'#aaa',4:'#888',3:'#666',2:'#555',1:'#444'};
const RANK_NAMES={10:'S',9:'A',8:'B',7:'C',6:'D',5:'E',4:'F',3:'G',2:'H',1:'I'};
const PREVIEW_ROWS=80;
// Item highlight
const S_WEAPONS=['Stardust sword','Poker','Deft dagger','Bright staff','Gringham whip','Knockout rod','Dragonlord claws','Critical fan','Bad axe','Groundbreaker','Meteorang',"Angel's bow"];
const MILLIONAIRE=['Hero spear','Pruning knife','Wyrmwand','Wizardly whip','Beast claws','Attribeauty','Heavy hatchet','Megaton hammer','Pentarang','Metal slime sword','Metal slime spear'];
const ITEM_HL={};
ITEM_HL['Sainted soma']={bg:'#FFC90E',fg:'#000',bd:'#da0'};
S_WEAPONS.forEach(w=>ITEM_HL[w]={bg:'#1a8a3c',fg:'#fff',bd:'#2a4'});
ITEM_HL['Ethereal stone']={bg:'#c018a0',fg:'#fff',bd:'#e4c'};
['Metal slime shield','Metal slime armour','Metal slime helm','Metal slime gauntlets','Metal slime sollerets'].forEach(i=>ITEM_HL[i]={bg:'#383850',fg:'#d0d8f0',bd:'#88a'});
MILLIONAIRE.forEach(i=>ITEM_HL[i]={bg:'#08c',fg:'#fff',bd:'#4af'});
ITEM_HL['Lucida shard']={bg:'#B5E61D',fg:'#000',bd:'#8c0'};
['Dangerous bustier','Fuddle bow'].forEach(i=>ITEM_HL[i]={bg:'#FFAEC9',fg:'#000',bd:'#f8a'});
let displayLang='jp';
const PRESETS_DATA={
'DD,263C':{custom:{3:[0],4:[0],7:[0],8:[1],9:[0],10:[0],11:[0],12:[0],13:[0],14:[0],15:[0],16:[0]}},
'B5,3CA2':{custom:{4:[0],5:[0],6:[0],7:[0],8:[0,2],9:[0],10:[1],13:[0],14:[0]}},
'DD,2E7A':{custom:{3:[0],4:[0],6:[0],7:[1],9:[0],10:[0],11:[0,2],12:[1]}},
'C9,7FE0':{custom:{3:[0],4:[0,2],5:[0],8:[0],9:[1],10:[0],11:[0],12:[0],13:[0]}},
'C9,2AC6':{custom:{3:[0,1]}},
'DD,32BB':{custom:{3:[0,1,2],4:[0,1]}},
'DD,235E':{custom:{3:[0,1]}},
'C9,158D':{custom:{3:[0,1]}},
'DD,5C43':{custom:{3:[0,1],4:[0],5:[1],6:[0],9:[0],10:[0],11:[0],13:[0,1],14:[1]}},
'DD,47D0':{custom:{3:[0,1],4:[1],5:[0],6:[0],7:[0,1,2],8:[0]}},
};
let engine=null;
let chestList=[];
let visibleChests=[];
let currentFilter='ALL';
let customFilter=null;
let hiddenChests=new Set();
let rankWidths={};
let timerRunning=false;
let timerRAF=null;
let timerOrigin=0;
let realSec=-3;
// Cached timer DOM elements (avoid getElementById at 60fps)
let _timerText=null,_timerDisp=null,_btnStart=null;
function cacheEls(){_timerText=document.getElementById('timerText');_timerDisp=document.getElementById('timerDisp');_btnStart=document.getElementById('btnStart');}
// Debounced seed input (avoid recompute on every keystroke)
let _seedTimer=null;
function debouncedInput(){clearTimeout(_seedTimer);_seedTimer=setTimeout(onInputChange,200);}
// Measure longest item name per chest rank -> exact per-column width
function measureRankWidths(){
const canvas=document.createElement('canvas');
const ctx=canvas.getContext('2d');
const isMobile=window.innerWidth<=600;
const isEN=displayLang!=='jp';
ctx.font=(isMobile?'10px ':'11px ')+(isEN?'system-ui,-apple-system,sans-serif':'"Hiragino Sans","PingFang TC",sans-serif');
rankWidths={};
const ranks=new Set(chestList.map(cd=>cd.rank));
for(const rank of ranks){
const sample=chestList.find(cd=>cd.rank===rank);
if(!sample)continue;
let maxW=0;
for(let s=0;s<300;s++){
const [en,jp]=engine.getBoxItem(sample.floor,sample.box,s);
const name=isEN?(en||'\u2014'):(jp||en||'\u2014');
const w=ctx.measureText(name).width;
if(w>maxW)maxW=w;
}
rankWidths[rank]=Math.ceil(maxW)+12;
}
}
function internalSec(){return realSec-5;}
function timerLoop(){
if(!timerRunning)return;
const elapsed=Date.now()-timerOrigin;
const newSec=Math.floor(elapsed / 1000);
updateStopwatch(elapsed);
if(newSec!==realSec){realSec=newSec;renderRows();}
timerRAF=requestAnimationFrame(timerLoop);
}
function updateStopwatch(elapsedMs){
const isPrep=elapsedMs<0;
const absMs=Math.abs(elapsedMs);
const totalSec=absMs/1000;
const s=Math.floor(totalSec);
const cs=Math.floor((totalSec-s)*100);
const text=isPrep?'-'+String(s).padStart(2,'0')+'.'+String(cs).padStart(2,'0'):String(s).padStart(3,'0')+'.'+String(cs).padStart(2,'0');
_timerText.textContent=text;
_timerDisp.style.color=isPrep?'#f88':'#0f0';
}
function getItemAtSec(floorIdx,boxIdx,sec){
if(sec<0)return[null,null];
return engine.getBoxItem(floorIdx,boxIdx,sec);
}
function compute(){
const rankStr=document.getElementById('inRank').value;
const seedStr=document.getElementById('inSeed').value.trim();
const seed=parseInt(seedStr,16);
if(isNaN(seed)||seed<0||seed>0xFFFF){
document.getElementById('mainArea').innerHTML='<p style="color:#f44;padding:20px;">Invalid Seed</p>';
return;
}
engine=new GrottoDetail();
engine.MapSeed=seed;
engine.MapRank=parseInt(rankStr,16);
engine.calculateDetail();
chestList=[];
for(let f=0;f<engine.floorCount;f++){
const d=engine.di[f];
for(let b=0;b<d[8];b++){
const info=engine.getBoxInfo(f,b);
chestList.push({
floor:f,floorNum:f+1,floorLabel:'B'+(f+1)+'F',
box: b,rank: info.rank,rankName:RANK_NAMES[info.rank]||'?'
});
}
}
measureRankWidths();
buildTable();
renderRows();
}
function isVisible(cd){
if(currentFilter==='CUSTOM'&&customFilter){
const allowed=customFilter[cd.floorNum];
return allowed?allowed.includes(cd.box):false;
}
if(currentFilter==='SA')return cd.rank>=9;
if(currentFilter==='AB')return cd.rank===9||cd.rank===8;
return true;
}
function buildTable(keepScroll){
const area=document.getElementById('mainArea');
const savedScroll=keepScroll?area.scrollLeft:0;
area.scrollLeft=0;
area.scrollTop=0;
visibleChests=chestList.filter(cd=>isVisible(cd)&&!hiddenChests.has(cd.floor+':'+cd.box));
if(visibleChests.length===0){
area.innerHTML='<p style="color:#555;padding:20px;text-align:center;">No chests</p>';
return;
}
const gridCols='42px '+visibleChests.map(cd=>(rankWidths[cd.rank]||100)+'px').join(' ');
let hdr='<div class="vhdr-time">sec</div>';
for(const cd of visibleChests){
const clr=RANK_COLORS[cd.rank]||'#888';
hdr+='<div class="vhdr" data-ck="'+cd.floor+':'+cd.box+'" style="cursor:pointer"><span class="vhdr-floor">'+cd.floorLabel+'</span><br><span class="vhdr-rk" style="color:'+clr+'">'+cd.rankName+(cd.box+1)+'</span></div>';
}
area.innerHTML='<div class="vgrid" id="vgrid" style="grid-template-columns:'+gridCols+'">'+hdr+'<div id="vbody" style="display:contents"></div></div>';
// Click a header or cell to hide that chest column
area.querySelector('.vgrid').addEventListener('click',function(e){
const cell=e.target.closest('[data-ck]');
if(!cell)return;
hiddenChests.add(cell.dataset.ck);
document.querySelectorAll('.filter-btn').forEach(b=>b.classList.remove('active'));
buildTable(true);renderRows();
});
if(savedScroll)area.scrollLeft=savedScroll;
}
function renderRows(){
const vbody=document.getElementById('vbody');
if(!vbody||visibleChests.length===0)return;
const is=internalSec();
const startSec=Math.max(0,is);
const endSec=Math.max(startSec+PREVIEW_ROWS,is+PREVIEW_ROWS);
const isEN=displayLang!=='jp';
const cellCls=isEN?'vcell vcell-en':'vcell';
let html='';
for(let s=startSec;s<=endSec;s++){
const isCur=(s===is);
const rCls=isCur?' vrow-cur':'';
html+='<div class="vtime'+rCls+'">'+String(s+5).padStart(3,'0')+'</div>';