-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.lua
More file actions
1572 lines (1495 loc) · 73.7 KB
/
Copy pathmain.lua
File metadata and controls
1572 lines (1495 loc) · 73.7 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
-- Dramatic Shape Voxel Mod: a full 3D diorama overworld, shipped as a
-- rendering pipeline mod.
--
-- The engine's render_pipelines registry (src/mods/Schemas.lua) lets a mod
-- own part of the frame. This mod registers two:
--
-- voxel a drawWorld pipeline. Instead of the flat tile blit, the
-- overworld's terrain is extruded into real geometry, walked
-- by a depth-buffered 3D camera, with characters as leaning
-- sprite slabs and a shadow map throwing real cast shadows
-- across whatever they land on. Occlusion is the depth
-- buffer, not a y-sort: walk behind a building and the
-- building is simply in front.
--
-- tiltshift a worldPresent pipeline -- the stage that post-processes
-- the finished world BEFORE the UI composites over it. A
-- tilt-shift blur that sells the miniature-model look, on the
-- diorama only, leaving text boxes and menus crisp.
--
-- Everything a display mode needs beyond the two draw functions -- the
-- OFF/15/35/50 ladder, the options rows, the hotkeys, persistence in
-- save.options.pipelines, the free-roam gate, the mutual exclusion with
-- the engine's TILT mode -- is engine plumbing driven by the records
-- below. This file declares; lib/ draws.
--
-- Voxel mode is presentational: it changes what the world LOOKS like and
-- nothing about what it IS. TWO rungs are the deliberate exception. 1ST
-- (the camera in the player's own eyes) and 3RD (the same rig, boomed back
-- behind their shoulder) replace the grid WALK with a free,
-- camera-relative one while either is selected (lib/FreeMove.lua), because
-- a camera you can steer with a mouse demands feet that go where it looks.
-- Even there the game is untouched: the walk asks the engine's own
-- collision the same questions a grid step asks, keeps the player's
-- logical cell synced, and fires the engine's own landing pipeline per
-- cell crossed -- warps, encounters, ledges, gates and scripts all run
-- exactly as themselves. Step off the rung and the grid walk is back.
local mod = ...
-- ------- the mod namespace
--
-- lib/ modules require each other through V rather than package.path: a
-- mod directory is not on it, and may live inside a mounted .love archive
-- that plain require cannot reach. Each module is loaded once, with V
-- passed in as its vararg (`local V = ...`).
local V = { mod = mod, path = mod.path }
-- ...and the loader's record carries a handle back, so a TEST DRIVER can
-- reach the modules this mod actually loaded.
--
-- `modules` below is a local closed over by `V.require`, and a plain
-- `require("mods.DRAMATIC_SHAPE.lib.X")` cannot reach it -- a mod directory
-- is not on package.path, and the file's `local V = ...` would be handed a
-- module NAME rather than this namespace and raise on the first `V.require`.
-- That is not academic: `tests/drivers/g3_shots.lua` guarded its DAYTIME pin
-- in a pcall, the require raised inside it, and from the day the driver was
-- written until g3-mass-229 SHOT_DAY silently did nothing -- every QA
-- screenshot in NOTES.md was lit by the container's wall clock, and two
-- frames of identical geometry three hours apart differed by half the
-- brightness. One field, read-only by convention, and a driver can pin the
-- light.
mod.V = V
-- ...and one global, because the record above is not always the one the
-- loader keeps in `loader.loaded` (it hands the entry chunk its own table),
-- so a driver walking the loader finds no namespace at all. This is a test
-- seam and nothing in lib/ reads it.
_G.__DRAMATIC_SHAPE_V = V
local function chunkFor(rel)
local source = mod:read(rel)
if not source then
error(("DRAMATIC_SHAPE: %s is missing -- reinstall the mod"):format(rel), 0)
end
local chunk, err = load(source, "@" .. mod.path .. "/" .. rel)
if not chunk then
error(("DRAMATIC_SHAPE: %s did not compile: %s"):format(rel, tostring(err)), 0)
end
return chunk
end
-- ---------------------------------------------------------------------------
-- COMPANION MODULES: present is a bonus, absent is not an error.
--
-- Other mods extend this one by SPLICING requires into its files -- a ceiling
-- for first person, flora, a painted backdrop, a sky layer, a jump. That is a
-- fine way to extend a mod right up until the companion goes away, and then it
-- is a disaster: `V.require` raised, `main.lua` never finished, and the whole
-- of DRAMATIC_SHAPE failed to load with
--
-- FAILED: DRAMATIC SHAPE: lib/Ceiling.lua is missing -- reinstall the mod
--
-- ...over a feature nobody asked for and that had uninstalled ITSELF. The
-- companion had spliced requires into main.lua, VoxelScene, ChunkMesher,
-- Structures and FirstPerson, then removed its own payloads and restored only
-- the files it had backups for -- leaving the splices behind, pointing at
-- files it had just deleted.
--
-- So a companion's module is OPTIONAL by name. Missing, or broken, and the
-- name resolves to an inert table whose every field is a no-op function: the
-- spliced `Ceiling.draw(state)` call sites keep working and draw nothing, and
-- this mod loads. Anything NOT on this list still raises, because a missing
-- lib/ of our own is a real packaging fault and must be loud.
--
-- This is compatibility in one direction only, deliberately. Nothing here
-- requires the companion, references it, or degrades without it.
local COMPANION = {
Ceiling = true, Flora = true, Backdrop = true, SkyLayer = true, Jump = true,
}
local function inertModule()
-- every field is a function that does nothing and answers nothing, so both
-- `M.draw(x)` and `M.thing` are safe on a module that is not there
return setmetatable({}, { __index = function() return function() end end })
end
local companionSaid = {}
local function companionMissing(name, why)
if companionSaid[name] then return end
companionSaid[name] = true
pcall(function()
require("src.core.Logger").info(
"DRAMATIC_SHAPE: companion module %s is %s -- carrying on without it",
name, why)
end)
end
local modules = {}
function V.require(name)
local hit = modules[name]
if hit ~= nil then return hit end
local rel = "lib/" .. name .. ".lua"
if COMPANION[name] then
local source = mod:read(rel)
if not source then
companionMissing(name, "absent")
modules[name] = inertModule()
return modules[name]
end
local chunk, err = load(source, "@" .. mod.path .. "/" .. rel)
if not chunk then
companionMissing(name, "not compilable: " .. tostring(err))
modules[name] = inertModule()
return modules[name]
end
local ok, value = pcall(chunk, V)
if not (ok and value ~= nil) then
companionMissing(name, "failed to load: " .. tostring(value))
modules[name] = inertModule()
return modules[name]
end
modules[name] = value
return value
end
local value = chunkFor(rel)(V)
modules[name] = value
return value
end
-- The explicit form, for anything of ours that is genuinely optional: nil when
-- it is not there, never an error, never a stub.
function V.optional(name)
local hit = modules[name]
if hit ~= nil then return hit end
local rel = "lib/" .. name .. ".lua"
local source = mod:read(rel)
if not source then return nil end
local chunk = load(source, "@" .. mod.path .. "/" .. rel)
if not chunk then return nil end
local ok, value = pcall(chunk, V)
if not (ok and value ~= nil) then return nil end
modules[name] = value
return value
end
local dataFiles = {}
function V.data(name)
local hit = dataFiles[name]
if hit ~= nil then return hit end
local value = chunkFor("data/" .. name .. ".lua")(V)
dataFiles[name] = value
return value
end
-- ------- pipelines
local Perf = V.require("Perf")
-- The PERFORMANCE tier's ceilings. Required here as well as where it is
-- clamped so that a tree with a broken Tier.lua fails at LOAD, loudly, and
-- not on the first frame the player lowers the row.
local Tier = V.require("Tier")
local Voxel = V.require("VoxelState")
local Voxel3D = V.require("Voxel3D")
local VoxelScene = V.require("VoxelScene")
local TiltShift = V.require("TiltShift")
local ChunkMesher = V.require("ChunkMesher")
-- Forward declaration: the prebake pass is set up far below (it needs the
-- options schema first) but the update hook that drives it is written above
-- that, and a closure cannot capture a local that does not exist yet.
local pumpPrebake
local VoxelGrid = V.require("VoxelGrid")
local WorldCurve = V.require("WorldCurve")
local OverworldBattle = V.require("OverworldBattle")
local BattleExit = V.require("BattleExit")
local DayNight = V.require("DayNight")
local DayTint = V.require("DayTint")
local Water = V.require("Water")
local AntiAlias = V.require("AntiAlias")
local FirstPerson = V.require("FirstPerson")
local FreeMove = V.require("FreeMove")
local CamControl = V.require("CamControl")
local VR = V.require("VR")
-- HORDE MODE: the konami code's minigame. Horde owns the state machine and
-- every hook; the other four are the gun, the crowd, the readout and the
-- chip-synthesized sounds it fires. See lib/Horde.lua for the whole design.
local Horde = V.require("Horde")
local HordeGun = V.require("HordeGun")
local HordeHud = V.require("HordeHud")
local HordeSfx = V.require("HordeSfx")
-- Forward declaration: the voxel pipeline's update hook (registered below)
-- calls this, and it is defined further down with the settings it drives.
-- Declared rather than left global -- a mod writing to _G would leak into
-- every other mod's namespace.
local applyFull
-- The last VOID FILL the terrain was meshed under; see the update hook.
-- The scene canvas's size, in FRAMEBUFFER PIXELS.
--
-- `ctx.width/height` are the window measured in LOVE UNITS
-- (love.graphics.getDimensions), but the engine composites a pipeline's
-- returned canvas with `draw(canvas, 0, 0, 0, 1/dpiX, 1/dpiY)` -- a scale
-- that only covers the window when the canvas is at PIXEL resolution.
-- Sizing it in units costs the DPI scale TWICE: the canvas is that much
-- smaller, then it is drawn that much smaller again, so the diorama lands
-- in the top-left corner at 1/dpi of the screen. Desktop never sees it --
-- units and pixels are the same thing there -- but on Android the DPI scale
-- is the display density (2.625 on a 420dpi panel), and the world came out
-- a third of the size in each direction.
--
-- So ask for the pixel dimensions rather than trusting the ctx. That is
-- the number a fixed engine would hand over, so this keeps working either
-- way instead of double-correcting. It also squares the FX pass: ctx.scale
-- is ALREADY in pixels per world pixel (Zoom.scale over Renderer:fitScale,
-- which measures the drawable), so the closures ctx.drawFx runs were being
-- scaled for a canvas 2.6x bigger than the one they drew into.
local function sceneSize(ctx)
if love.graphics and love.graphics.getPixelDimensions then
local pw, ph = love.graphics.getPixelDimensions()
if pw and ph and pw > 0 and ph > 0 then return pw, ph end
end
return ctx.width, ctx.height
end
local voidFill = { last = nil }
function voidFill.check()
local TileRenderer = require("src.render.TileRenderer")
local now = TileRenderer.voidFill
if voidFill.last ~= nil and now ~= voidFill.last then
ChunkMesher.invalidate() -- no map id: every ring on every map is stale
end
voidFill.last = now
end
mod.content.render_pipelines:register("voxel", {
label = "VOXEL",
levels = Voxel.ANGLE_LABELS,
-- 3 is the engine's TILT key, which this mode supersedes -- see the
-- hotkey block near the bottom of this file for how it is claimed
hotkey = "3",
-- above tiltshift, so the two sort together in the options list with the
-- mode first and its post-process under it
priority = 20,
-- Headless runs and drivers without a depth canvas or shader support
-- answer false here, and the engine keeps the vanilla 2D path -- which
-- is why no caller ever has to guard for a missing 3D pass.
available = function()
return Voxel3D.available()
end,
-- the engine hands over the live level; we ease the camera toward it.
-- pump() advances queued mesh builds inside a few-millisecond budget,
-- so entering voxel mode (and streaming neighbours while walking)
-- costs frames nothing visible -- the old synchronous build froze the
-- first frame for seconds. prefetch() runs here as well as in the
-- draw, because update ticks even while a warp's Transition covers
-- the screen: the destination's meshes start building the moment the
-- map swaps behind the fade, and the fade-covered frames get a wider
-- pump slice -- so stepping out of a door lands on terrain that is
-- already there instead of a flat flash.
update = function(dt, level)
-- FULL is a preset, so it is applied ON THE PRESS rather than held every
-- frame: it SETS the other rows and then leaves them alone. Holding them
-- would make the zoom keys and the wheel dead while the mode was on, and
-- would fight anyone who changed one deliberately.
applyFull(level)
Voxel.update(dt, level)
-- the first-person head, on the same tick: its blend in and out of the
-- orbit, the mouse capture lifecycle, and the frame's stick-rate look.
-- Unconditional like Voxel.update, because the blend has to keep easing
-- OUT after the rung is left
FirstPerson.update(dt)
-- the day/night clock, on the same always-running tick: Pipelines.update
-- runs whatever the level, so time passes with the mode off, through
-- battles and menus, and a CYCLE evening falls mid-fight exactly as it
-- would mid-walk
DayNight.update(dt)
-- The overworld battle rides this hook rather than owning a pipeline of
-- its own, because it owns no pass of the FRAME: it draws under a battle
-- screen the engine composites, which is not a stage the registry has.
-- What it needs is a tick that keeps running once the overworld stops
-- being the top state, and this is one -- Game:update calls
-- Pipelines.update unconditionally, so it survives the transition wipe
-- and the whole battle. Ahead of the active() gate below, because a 3D
-- battle does not require the free-roam mode to be switched on.
OverworldBattle.update(dt)
-- The horde, on the same always-running tick and for the same reason:
-- it owns no pass of the frame, it is a MODE over the overworld, and
-- it has to keep thinking while a warp's wipe covers the screen (the
-- crowd follows the player through the door) and under the GAME OVER
-- card, which is a pushed state that stops everything below it.
Horde.update(dt)
-- VOID FILL picks the block the border ring is made of, and in this
-- mode that ring is BAKED INTO THE MESH rather than drawn each frame.
-- So the option has to reach the cache or nothing happens on screen
-- until the meshes are dropped for some other reason -- which reads
-- exactly like the option doing nothing at all. Polled rather than
-- hooked because the engine changes it from three places (the options
-- row, applyOptions on load, TileRenderer.setVoidFill) and none of
-- them announces it. Ahead of the active() gate, so switching it
-- while voxel mode is OFF still invalidates what is cached.
voidFill.check()
-- The whole VR frame -- session lifecycle, xrWaitFrame's pacing, both
-- eye renders, the layer submit -- rides this hook, because it is the
-- one tick that runs through menus, dialogs and battles, which is
-- what a headset needs the world (or at least the UI panel) to do.
-- Ahead of the active() gate: with the mode off, the headset still
-- shows the flat screen on the floating panel.
VR.update(dt)
if not Voxel.active() then return end
local Game = require("src.core.Game")
local ow = Game and Game.overworld
if ow and ow.map and ow.camera then
pcall(VoxelScene.prefetch, ow)
end
-- THE BUILD SLICE, MEASURED SEPARATELY FROM THE FRAME.
--
-- This is the whole of the mod's build cost as the player experiences
-- it: a map's shape analysis plus its geometry, drained a slice at a
-- time. It is worth its own span because it is felt as a HITCH and not
-- as fps -- `max` on this label is the number that matters, and it is
-- large: Structures.forMap is not interruptible (the budget can only
-- suspend the geometry coroutine), so the first pump on arriving at a
-- map carries the whole analysis in one frame.
local tPump = Perf.now()
ChunkMesher.pump(Game and Game.stack
and Game.stack:top() ~= ow)
Perf.add("ChunkMesher.pump", tPump)
pumpPrebake()
end,
drawWorld = function(ctx)
local tFrame = Perf.now()
-- the palette closure, stashed for the VR frame: it renders from the
-- update hook, where no ctx exists to carry one
VR.paletteFor = ctx.paletteFor
-- With a headset running, the window's world pass becomes the MIRROR
-- -- the left eye, fitted to the window -- rather than a third full
-- render of the scene. Everything else about the frame (the UI the
-- engine composites over this) is unchanged, which is exactly what
-- the headset's floating panel photographs.
if VR.active() then
local sw, sh = sceneSize(ctx)
local m = VR.mirror(sw, sh)
if m then return m end
end
-- Terrain and characters are geometry; the field FX stay ordinary 2D
-- draws composited on top, anchored through the same camera the 3D
-- pass used (ctx.drawFx below). The scene renders at the window's
-- PIXEL resolution (see sceneSize) so the 3D pass is crisp rather than
-- a magnified low-res image, while the FX closures keep drawing in
-- world-pixel units.
local sw, sh = sceneSize(ctx)
-- With AA on, the whole pass runs into a canvas BIGGER than the window
-- and is folded back down at the end (see AntiAlias). Nothing between
-- these two lines knows: every pass in the frame measures itself in the
-- canvas it was handed, so the sky's dither, the water's march and the
-- camera itself all come out the same picture at a higher sample rate.
local rw, rh = AntiAlias.expand(sw, sh)
local canvas = VoxelScene.render(ctx.state, rw, rh,
ctx.vw, ctx.vh, ctx.paletteFor)
if not canvas then return nil end -- fall back to the 2D path
if Voxel3D.beginOverlay() then
-- the FX closures are ordinary 2D draws sized in DISPLAY pixels, and
-- they are drawing into the supersampled canvas alongside everything
-- else -- so the scale goes up with it, or the "!" bubble lands the
-- right place at half the size. project() already answers in canvas
-- pixels, so only the scale needs saying.
-- ...at the floor the camera is centred on, not at the world
-- datum. The FX these closures draw belong to the player and to
-- what is under their feet -- the "!" bubble, a grass rustle, a
-- puff of sand -- so on a terrace they anchor to the terrace.
-- Projecting them at zero left them sunk into the deck the player
-- was standing on, by exactly the height of the climb.
ctx.drawFx(function(wx, wy)
return Voxel3D.project(wx, Voxel3D.groundY or 0, wy)
end,
ctx.scale * AntiAlias.factor())
-- the horde's readout rides the same overlay, over the FX: health,
-- ammunition, the crosshair and the banners, sized in the same
-- supersampled canvas pixels everything else here is drawn in. A
-- headset never reaches this line (drawWorld returns the mirror
-- above) -- lib/VR draws the same HUD onto each eye instead.
HordeHud.drawFlat(rw, rh, ctx.scale * AntiAlias.factor())
Voxel3D.endOverlay()
end
-- and back to the window's own size, which is what the engine composites
-- one canvas pixel to one display pixel. A pass-through when AA is off.
local out = AntiAlias.resolve(canvas, sw, sh, "world")
-- THE FRAME SEAM. Perf.frame stamps one whole-frame time per RENDERED
-- frame and this is the only place in the mod that is reached exactly
-- once per rendered world frame -- Pipelines calls drawWorld from
-- love.draw, and a scripted run that steps the game ten times per
-- render still passes here once. Every one of these three calls is a
-- boolean test away from doing nothing while DS_PERF is unset, which
-- is every player's session.
Perf.add("voxel.drawWorld", tFrame)
Perf.frame()
Perf.drawStats()
return out
end,
invalidate = function()
Voxel3D.invalidate()
OverworldBattle.invalidate()
AntiAlias.invalidate()
ChunkMesher.invalidate() -- no map id = every cached mesh
VR.invalidate() -- the mirror, and FBO ids of dead canvases
end,
})
mod.content.render_pipelines:register("tiltshift", {
label = "T-SHIFT",
levels = TiltShift.LABELS,
-- 6 is free: no engine branch claims it, so this one alone reaches the
-- registry by the documented route
hotkey = "6",
priority = 10,
update = function(dt, level)
TiltShift.update(dt, level)
end,
-- worldPresent, not present: the blur belongs on the diorama, not on the
-- dialog box in front of it. A pass-through when the level is 0 or the
-- shader is unavailable, so the frame is untouched in every other case.
worldPresent = function(canvas)
return TiltShift.apply(canvas)
end,
invalidate = function()
TiltShift.invalidate()
end,
})
-- ------- this mod's own settings
--
-- Neither of these is a pipeline: they own no pass of the frame, they
-- PARAMETERISE the voxel one, so they have nothing to put in drawWorld or
-- present and the registry would rightly reject them. Plain mod settings
-- instead -- see ModSetting for where they persist and how the two rows
-- each ends up on stay in step.
-- ------- the FULL preset
--
-- Everything the mode wants switched to at once. Applied when the VOXEL row
-- ARRIVES at FULL and not again, so the player can still move the camera or
-- the zoom afterwards -- it is a starting point, not a lock.
--
-- Leaving FULL deliberately does NOT undo any of it. A preset that reverted
-- would throw away whatever the player had changed since, and "put it back
-- how it was" is not a thing this can know.
local fullWas = nil
applyFull = function(level)
local isFull = Voxel.isFull(level)
local was = fullWas
fullWas = isFull
if not isFull or was == true or was == nil then return end
local Game = require("src.core.Game")
local Pipelines = require("src.render.Pipelines")
local Zoom = require("src.render.Zoom")
local opts = Game.save and Game.save.options
if not opts then return end
-- the miniature blur at its strongest: FULL is the diorama look, and the
-- tilt-shift is most of what makes it read as a model
Pipelines.setLevel("tiltshift", Pipelines.maxLevel("tiltshift"))
Pipelines.syncOptions(opts)
-- the horizon flat. The curve bends the world away from a walking player,
-- which fights a fixed diorama framing
WorldCurve.setting:setIndex(1, Game)
-- and the water reflecting everything it can: FULL is the diorama at its
-- most photographed, and a lake with the sky and the shoreline in it is
-- most of what makes the model read as being outdoors
Water.setting:setIndex(1, Game)
-- and the cast standing in it, for the reason the line above is here at
-- all. FULL takes both rows OFF the OPTIONS menu (they parameterise the
-- look, which is what the preset owns), and a row that is off the menu and
-- NOT set by the preset that removed it is a value the player can no
-- longer reach -- which is the trap TILT and GBC FX are pinned to avoid.
-- Index 1 is ON, which is what this mode has drawn since the reflection
-- pass existed.
Water.castSetting:setIndex(1, Game)
-- and the view fitted to the window
opts.zoom = 0
Zoom.applyOptions(opts)
-- battles on the map too: FULL means the whole mode, and a fight is where
-- half of it is spent. Set and then LET GO of -- unlike the rows above, both
-- battle rows stay on the menu under FULL (see the rows hook), so this is
-- where the preset puts them and not where they are held.
OverworldBattle.setting:setIndex(1, Game)
-- with both mons out there on it: BACK SPRITES keeps the player's own on the
-- menu, which is the one part of the old screen FULL is least about. Set the
-- same way, and changed back on the same row a keypress later.
OverworldBattle.backSetting:setIndex(1, Game)
-- and the battle screen the staged fight is composed for. WIDE re-lays that
-- screen out on a 304x144 surface, which moves every anchor the arena camera
-- is solved against (OverworldBattle.forceOG); FULL has just switched staged
-- fights on, so the layout follows them.
OverworldBattle.forceOG(Game)
-- and the sky on the clock on the wall: FULL sets DAYTIME to SYNC. Set and
-- then LET GO of, like the battle rows above -- the row stays on the menu
-- under FULL, because holding it there left a player who booted after dark
-- with no way to ask for daylight.
DayNight.forceSync(Game)
if Game.writeOptions then pcall(Game.writeOptions, Game) end
end
-- Whether a fight can be staged on the map, as far as the OPTIONS menu is
-- concerned: the 3D-BTL row, and nothing else.
--
-- It used to answer yes under FULL as well, on the grounds that FULL owned
-- that row and switched it on. FULL no longer owns it -- the row stays on the
-- menu under FULL and can be switched off there (see the rows hook) -- so that
-- clause would now claim staged battles for a preset the player had just
-- turned them off inside, pinning BATTLE LAYOUT to OG for a fight that is
-- never staged. The row is the only thing that decides, which is what every
-- other reader of this setting already believed: OverworldBattle.begin and
-- wantsFront both gate on enabled() alone.
--
-- Deliberately NOT gated on Voxel3D.available(): the engine offers a
-- pipeline's row whether or not the hardware can run it (Pipelines.rows), so
-- this mode's rows say ON on a machine without a depth buffer too, and a menu
-- that claims 3D battles are on must not also offer the layout they cannot be
-- drawn in.
local function stagedBattles()
return OverworldBattle.enabled()
end
local SETTINGS = {
{ VoxelGrid.setting, "One-pixel wireframe along every voxel edge." },
{ WorldCurve.setting,
"Bend the world down over the horizon, Animal Crossing style." },
{ Water.setting,
"Reflections on water. FULL adds screen-space reflections of the "
.. "shoreline, the trees and the buildings behind it; SKY is the sky, "
.. "the sun and the moon alone, which is most of the look for a "
.. "fraction of the cost." },
-- Directly under WATER, because it is the second half of the same
-- question and the grouped block keeps them together on the menu.
--
-- `when` gates it on there being a MIRROR for the cast to be in at all.
-- Below FULL the water shader's `rays` is 0 and it never samples the
-- reflection copy, so an ON here would decide precisely nothing -- and a
-- row that no longer decides anything is worse than no row, which is the
-- same call BACK SPRITES makes against a staged fight two entries down.
-- It is Water.level() rather than the stored value on purpose: the
-- PERFORMANCE tier's ceiling is what actually decides whether the march
-- runs, so a player on BALANCED -- where the ceiling holds WATER at SKY
-- whatever the row says -- correctly does not see this row either.
--
-- NOT marked `full`: this parameterises the LOOK of the diorama, exactly
-- like WATER, V-GRID and V-CURVE, so the FULL preset owns it and takes it
-- off the OPTIONS menu on the same reasoning it takes those three. The
-- mod manager's own page carries it either way.
{ Water.castSetting,
"Show people in the water: NPCs, Pokemon and your own character "
.. "reflected in the surface they are standing beside, along with the "
.. "shoreline behind them. This is the only part of the reflection "
.. "that costs DRAW CALLS rather than fill rate -- every character on "
.. "screen is drawn a second time, into the picture the water reflects "
.. "-- so it is the one to turn off on a busy map if the water is "
.. "costing you frames and you want to keep the shoreline. Needs WATER "
.. "on FULL; there is no reflection to be in below that.",
when = function() return Water.level() >= 2 end },
-- ...and the third half of the same question, under both of them.
--
-- `when` gates it the same way WATER SPRITES is gated and for the same
-- reason: below FULL the shader never reads the mirror, so the row would
-- decide nothing.
--
-- NOT set by the FULL preset, and that is the one place this row parts
-- company with WATER and WATER SPRITES. FULL pins the rows that describe
-- the diorama's LOOK, and this is a look the mode has never had at a price
-- nothing else in the mode charges -- the whole scene drawn a second time.
-- A preset that quietly switched it on would be the frame halving itself
-- on a machine whose owner asked for "the diorama", which is the opposite
-- of what a preset is for. So it stays where the player left it, and
-- because FULL does not pin it, FULL must not take it off the menu either:
-- it is marked `full` for exactly that reason -- a row the preset does not
-- set and does remove is a value the player can no longer reach, which is
-- the trap TILT and GBC FX are pinned to avoid.
{ Water.planarSetting,
"Reflect people and scenery that the surface reflection cannot reach: "
.. "the world is drawn a second time, upside down under the water, so "
.. "characters standing BESIDE a lake appear in it and so does anything "
.. "just off the top of the screen. This is the most expensive row in "
.. "the mode -- it is the whole scene again, not an effect over it -- "
.. "so it is OFF unless you ask for it. Needs WATER on FULL.",
full = true,
when = function() return Water.level() >= 2 end },
-- `full` marks a row FULL does not take away. FULL owns the diorama's own
-- knobs; what a battle is drawn over, and how it is framed, are not that.
-- Off the OPTIONS menu while VR is on: the headset REQUIRES staged
-- battles (OverworldBattle.enabled answers true regardless of this row)
-- and forbids back sprites (backPinned answers false), so both rows
-- decide nothing there and a dead switch on the menu reads as broken.
{ OverworldBattle.setting,
"Fight on the map: the battle draws over the nearest clear ground, "
.. "shot over the shoulder with a slow parallax drift.",
when = function() return not VR.enabled() end, full = true },
-- Only offered while a fight can actually be staged on the map: with 3D-BTL
-- off the engine draws the classic screen, which is this row's ON already,
-- and a row that no longer decides anything is worse than no row.
{ OverworldBattle.backSetting,
"Keep your own Pokemon on the battle menu, seen from behind in its "
.. "original slot, instead of standing it on the map facing the foe. "
.. "The foe is still out there on its own tile.",
when = function() return stagedBattles() and not VR.enabled() end,
full = true },
-- Marked `full` on the battle rows' reasoning, and then some. FULL SETS this
-- to SYNC on arrival (applyFull) because the diorama's sky should follow the
-- clock on the wall; it used to HOLD it there and take the row away, which
-- meant a player who started the game after dark had the whole world
-- multiplied by DayNight.TINTS.night with no row anywhere to say otherwise.
-- A preset that hides the one row deciding whether you can see is a lock,
-- not a preset.
{ DayNight.setting,
"What time it is outdoors: pin the sky to DAY, NIGHT, DUSK or DAWN, "
.. "let CYCLE run it -- ten minutes of sun, ten of moon, with the "
.. "shadows, the sky and the light following -- or SYNC it to the "
.. "clock on the wall, so Kanto's evening falls when yours does.",
full = true },
-- Marked `full` for the opposite reason the battle rows are: this is not a
-- knob on the look at all, it is what the look COSTS. FULL is a preset for
-- the diorama, not a licence to spend four times the fill rate on the
-- machine it happens to be running on, so it neither sets this nor takes
-- the row away -- the player decides what their hardware can carry, from
-- inside FULL like anywhere else.
{ AntiAlias.setting,
"Smooth the stair-stepped edges of the 3D world -- roof ridges, ledge "
.. "lips, a tree against the sky -- by rendering the diorama larger than "
.. "the window and folding it back down. Every edge in the picture "
.. "softens with them, the tileset's own texels included, so the diorama "
.. "reads smoother rather than sharper. 2X costs half again as many "
.. "pixels in each direction and 4X twice, which makes this the most "
.. "expensive row in the mod.",
full = true },
-- `full` for the same reason as AA: not a knob on the look, a question
-- about the hardware on the desk.
{ VR.setting,
"PCVR through OpenXR (SteamVR, Oculus, WMR). The diorama becomes a "
.. "tabletop model your head moves around; the 1ST rung stands you "
.. "inside the world at life size, looking where the headset looks. "
.. "Menus and dialogs float on a panel. Needs a Windows OpenXR runtime "
.. "and the mod running from a real folder; without them the row stays "
.. "and the game stays flat, with the reason on the console.",
-- on Windows the row stays even when a runtime is missing (the console
-- says why); off Windows -- mobile above all -- there is no VR to have
-- and the row does not exist
when = function() return VR.supported() end, full = true },
-- Under the VR row and only while it is ON: a comfort setting for a
-- device that is not plugged in decides nothing, and this one is read
-- exclusively by the headset's right stick.
{ VR.smoothTurn,
"Turn smoothly with the right stick instead of snapping 45 degrees a "
.. "flick. OFF by default, and deliberately: a software turn moves the "
.. "world past a head that did not move, which is the most reliable way "
.. "to make somebody ill in a headset. Turn it on if you have your sea "
.. "legs and want the continuity.",
when = function() return VR.enabled() end, full = true },
}
local schema = {}
for _, entry in ipairs(SETTINGS) do
-- the VR rows are absent from the mod manager's page too where the
-- platform cannot do VR at all -- the OPTIONS menu's `when` gates are
-- situational (a row hidden for now), this one is existential
local vrOnly = entry[1] == VR.setting or entry[1] == VR.smoothTurn
if not vrOnly or VR.supported() then
schema[#schema + 1] = entry[1]:schema(entry[2])
end
end
-- ------- the persistent voxel cache, and the pass that fills it
--
-- These two rows are written out longhand rather than through ModSetting: the
-- first is a plain engine toggle the cache module reads for itself, and the
-- second is an `action` -- a row that stores nothing and exists to be pressed.
schema[#schema + 1] = {
key = "voxelDiskCache",
type = "toggle",
label = "VOXEL DISK CACHE",
default = true,
description = "Keep terrain meshes on disk between sessions, so a map you "
.. "have already visited appears the moment you walk into it instead of "
.. "being rebuilt from scratch. The cache key covers the map, its "
.. "tileset, the editor's per-tile voxel pins and the ceiling mod's live "
.. "settings, so anything that changes what the world SHOULD look like "
.. "rebuilds it rather than serving the old shape. OFF meshes everything "
.. "fresh every session.",
}
schema[#schema + 1] = {
key = "prebakeVoxels",
type = "action",
label = "PREBAKE VOXELS",
action = "START",
description = "Build every map's terrain into the cache now, a few "
.. "milliseconds a frame, so no area has to be meshed while you are "
.. "walking into it. It runs in the background while you play and counts "
.. "up on this row; press again to cancel. Needs VOXEL DISK CACHE ON. "
.. "Editing a map, re-pinning a tile's voxel shape or changing the "
.. "ceiling mod's settings invalidates what was baked, so run it again "
.. "after a session in the map editor.",
}
mod.options:define(schema)
-- ------- prebake
--
-- The whole feature is a queue of map ids drained a slice at a time. It never
-- competes with the live mesher (it only advances on a frame with nothing
-- queued to draw) and it builds nothing on the GPU, so running it over two
-- hundred maps costs disk and CPU rather than VRAM.
local Prebake = nil
do
local okPre, preMod = pcall(V.require, "VoxelPrebake")
Prebake = (okPre and type(preMod) == "table" and preMod) or nil
if not okPre then
print("[warn] DRAMATIC_SHAPE voxel prebake unavailable: " .. tostring(preMod))
end
end
-- A one-off message from the last press, shown until a real figure replaces it.
local prebakeMessage = nil
-- Every map the game knows about, in a stable order so two runs bake the same
-- world in the same sequence.
local function allMapIds()
local okGame, Game = pcall(require, "src.core.Game")
local maps = okGame and Game and Game.data and Game.data.maps
if type(maps) ~= "table" then return {} end
local ids = {}
for id, def in pairs(maps) do
if type(def) == "table" then ids[#ids + 1] = id end
end
table.sort(ids, function(a, b) return tostring(a) < tostring(b) end)
return ids
end
-- A THROWAWAY Map per id, never the engine's resident one: MapLoader attaches
-- a TileRenderer and keeps what it builds, which is right for the handful of
-- maps around the player and ruinous across all of them. Geometry reads the
-- def, the tileset and the tile pins and nothing else -- no pass in
-- runGeometry touches map.renderer -- so a bare Map is the same input the
-- real build sees, and the collector takes it back once its mesh is on disk.
-- A map that IS already resident is reused as is: that is the very object the
-- live build would mesh.
local function bakeMapFor(id)
local okGame, Game = pcall(require, "src.core.Game")
local okMap, Map = pcall(require, "src.world.Map")
if not (okGame and okMap and Game and Game.data) then
return nil, "engine map data unavailable"
end
local okLoader, MapLoader = pcall(require, "src.world.MapLoader")
if not okLoader then MapLoader = nil end
if MapLoader and type(MapLoader.cached) == "function" then
local okHit, hit = pcall(MapLoader.cached, id)
if okHit and type(hit) == "table" then return hit end
end
local def = Game.data.maps and Game.data.maps[id]
if not def then return nil, "no map def" end
-- The engine's own tileset fallback chain, so a map whose tileset is only
-- reachable through it bakes under the tileset the game will actually load.
local ts
if MapLoader and type(MapLoader.tilesetFor) == "function" then
ts = MapLoader.tilesetFor(Game.data, def)
else
ts = Game.data.tilesets and Game.data.tilesets[def.tileset]
end
if not ts then return nil, "no tileset for " .. tostring(def.tileset) end
local built, mapOrErr = pcall(Map.new, def, ts)
if not built or type(mapOrErr) ~= "table" then
return nil, "Map.new failed: " .. tostring(mapOrErr)
end
return mapOrErr
end
local function prebakeStatusText()
if not Prebake then return prebakeMessage end
local okP, p = pcall(Prebake.progress)
if not (okP and type(p) == "table") then return prebakeMessage end
if p.running then
prebakeMessage = nil
return string.format("%d/%d", p.done or 0, p.total or 0)
end
if prebakeMessage then return prebakeMessage end
if (p.total or 0) > 0 then
if (p.failed or 0) > 0 then
return string.format("DONE %d/%d (%d FAILED)", p.done or 0, p.total or 0,
p.failed or 0)
end
return string.format("DONE %d/%d", p.done or 0, p.total or 0)
end
return nil
end
mod.events:on("mod.option_action", function(ev)
if not (type(ev) == "table" and ev.mod == mod.id
and ev.key == "prebakeVoxels") then return end
if not Prebake then
prebakeMessage = "UNAVAILABLE"
return
end
if Prebake.running() then
Prebake.cancel()
prebakeMessage = "CANCELLED"
return
end
local status = type(ChunkMesher.cacheStatus) == "function"
and ChunkMesher.cacheStatus() or nil
if not (status and status.enabled) then
prebakeMessage = "CACHE OFF"
return
end
local ids = allMapIds()
if #ids == 0 then
prebakeMessage = "NO MAPS"
return
end
local started, why = Prebake.begin(ids, bakeMapFor)
prebakeMessage = started and nil or tostring(why or "UNAVAILABLE"):upper()
end)
-- The manager calls this as it redraws the row, so the count moves while the
-- settings screen is open and the world behind it is not ticking.
pcall(function()
mod.options:status("prebakeVoxels", function()
return prebakeStatusText() or "START"
end)
end)
-- Advance the pass, after the live mesher has had its slice and only when it
-- has nothing left queued.
function pumpPrebake()
if not (Prebake and Prebake.running()) then return end
if type(ChunkMesher.pending) == "function" and ChunkMesher.pending() > 0 then
return
end
local spent = (type(ChunkMesher.lastSlice) == "function")
and ChunkMesher.lastSlice() or 0
local dt = (love and love.timer and love.timer.getDelta
and love.timer.getDelta()) or (1 / 60)
local headroom = (1 / 60) - math.max(0, dt - spent)
if headroom <= 0 then headroom = 0.0015 end
pcall(Prebake.pump, math.min(0.006, headroom * 0.5))
end
-- ------- this mod's hotkeys
--
-- 3 VOXEL cycle the camera ladder (was 6; skips FULL)
-- 5 V-GRID toggle the wireframe (new)
-- 6 T-SHIFT cycle the blur ladder (was 9)
-- 7 V-CURVE cycle the horizon bend (new)
-- 8 3D-BTL toggle overworld battles (new)
-- 9 WATER cycle the water reflections (new; 9 was T-SHIFT's old key)
--
-- Only 6 arrives by the documented route. Game:keypressed answers the
-- engine's own display keys FIRST and returns -- 2 COLORS, 3 TILT, 4 ZOOM,
-- 5 GBC FX -- and only then offers the key to Pipelines.hotkey, expressly
-- so "a pipeline can never shadow one" (Schemas, render_pipelines.hotkey).
-- 3 and 5 are two of those, and 7 and 8 belong to plain mod settings that
-- own no pass and so have no registry to claim a key from at all.
--
-- So this wraps Game:keypressed. It is the invasive option and it is the
-- only one: polling the keyboard in update() would fire alongside the
-- engine's handler rather than instead of it, so 3 would cycle this mode
-- AND the engine's TILT on the same press.
--
-- Consequences worth being explicit about: while this mod is enabled, TILT
-- (3) and GBC FX (5) are unreachable by key -- and unreachable on the OPTIONS
-- menu too, where both rows are taken away and both values held at zero (see
-- pinEngineFx). Nothing is being hidden that still does something: TILT is the
-- flat fake of what this mode does for real, the registry already forces it
-- off whenever a world pipeline takes the pass, and GBC FX is a full-screen
-- present pass over the top of the diorama. Uninstalling puts both back.
--
-- Everything the engine does around a pipeline hotkey has to happen here
-- too, so the work is DELEGATED rather than reimplemented: Pipelines.hotkey
-- applies its own gate and ladder, and the three lines after it are the
-- engine's own (syncOptions, the tilt exclusion, writeOptions).
local HOTKEYS = {
["3"] = "pipeline", -- voxel, by its declared hotkey
["6"] = "pipeline", -- tiltshift, likewise
["5"] = VoxelGrid.setting,
["7"] = WorldCurve.setting,
["8"] = OverworldBattle.setting,
["9"] = Water.setting,
}
-- One step of the VOXEL angle ladder: everything a "3" press does, named
-- so the pad's B+SELECT combo (below) can make exactly the same step. The
-- gate is the registry's own; the tilt/GBC FX clearing is the engine work
-- the key has always delegated (see the wrap below for why).
local function cycleVoxel(game)
local Pipelines = require("src.render.Pipelines")
-- HORDE MODE holds the rung at 1ST for as long as it runs. Refused HERE
-- rather than at each caller because this one function IS every way a
-- player can step the ladder: the "3" key, the pad's B+SELECT, and the VR
-- left-stick click all come through it.
if Horde.viewLocked() then return false end
local top = game.stack and game.stack:top()
if not Pipelines.canToggle("voxel", top, game.overworld) then return false end
Pipelines.setLevel("voxel", Voxel.nextHotkeyLevel(Pipelines.level("voxel")))
Pipelines.syncOptions(game.save.options)
-- 3 is the key that used to turn TILT on and sits next to the one that
-- used to turn GBC FX on, and this mod has taken both away. A player who
-- left either running before enabling the mod would otherwise have no
-- way back to off, and both fight the diorama -- so the VOXEL step
-- clears them on EVERY press, not just the press that switches on.
game.save.options.tilt = 0
game.save.options.gbcfx = 0
require("src.render.GBCFX").setLevel(0)
require("src.render.Tilt").setLevel(game.save.options.tilt or 0)
game:writeOptions()
return true
end
-- The VR stick click makes this same step (VR.stepView): the function is
-- a local of this file, so the handoff is explicit rather than a
-- reimplementation drifting out of date in lib/VR.lua.
VR.cycleVoxel = cycleVoxel
do
local Game = require("src.core.Game")
local Pipelines = require("src.render.Pipelines")
local inner = Game.keypressed
function Game:keypressed(key)
-- HORDE MODE owns the keyboard's spare keys while it runs: R reloads,
-- and the mode keys are swallowed rather than left to change the rung
-- or the post-processing out from under a locked camera.
if Horde.active then
if key == "r" then
HordeGun.reload()
return
end
if HOTKEYS[key] then return end
end
local claim = HOTKEYS[key]
local top = self.stack and self.stack:top()
-- Q and E work whichever camera is in front of the player -- the
-- battle's lens, the third-person boom, or the engine's own survey
-- zoom on an orbit rung. CamControl answers which, and answers "none"
-- for 1ST and for every screen with no camera of ours behind it, in
-- which case the key falls through untouched. Ahead of the hotkey
-- table because unlike those it is NOT free-roam only: a staged battle
-- is exactly where the zoom is most wanted.
if (key == "q" or key == "e")
and not (top and top.onKeyPressed) then
if CamControl.zoomBy(key == "q" and 1 or -1) then return end
end
-- A screen with its own key handler gets the key first, exactly as the
-- engine's first branch does: typing a nickname must not toggle a
-- render mode. Only free-roam presses are ours to take.
if claim and not (top and top.onKeyPressed) then
if claim == "pipeline" then
-- 3 walks the ANGLE rungs and steps over FULL (Voxel.HOTKEY_ORDER),
-- so the registry's plain "advance one and wrap" is not what it
-- wants; 6 still is. The gate is the registry's own either way.
-- The whole of 3's step lives in cycleVoxel, because the pad's
-- B+SELECT makes the same step (see the handleInput wrap).
if key == "3" then
if cycleVoxel(self) then return end
elseif Pipelines.hotkey(key, top, self.overworld) then
Pipelines.syncOptions(self.save.options)
require("src.render.Tilt").setLevel(self.save.options.tilt or 0)
self:writeOptions()