-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVasara_Script.lua
3303 lines (2981 loc) · 104 KB
/
Vasara_Script.lua
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
-- Vasara 1.0.4 (Script)
-- by Hopper and Ares Ex Machina, Aaron Freed, CryoS, and Solra Bizna
-- from work by Jon Irons and Gregory Smith, released under the JUICE LICENSE!
-- preferences now begin on line 154 - the following code must execute first
-- Copyright 2023 Solra Bizna. I expressly authorize you (the reader) to use
-- this script, change it to fit your needs, strip out my name and claim it as
-- your own, whatever. This copyright claim is solely to assert authorship long
-- enough to immediately disclaim all copy-rights.
load_order = load_order or {}
table.insert(load_order, "Vasara_Script.lua")
-- Part 0: Don't let me call pairs() [by accident]. I hate pairs().
_G.danger_pairs = pairs
pairs = nil
-- Part 1: Create a system that allows triggers to be created cooperatively.
-- AND some safety stuff that will make it so that we won't do things the old
-- way by accident and ruin everything.
local shadow_triggers = {}
local the_real_triggers = {}
local subtriggers = {}
Triggers = nil
setmetatable(_G, {
__index = {Triggers = shadow_triggers},
__newindex = function(t, key, value)
if key == "Triggers" then
for key, value in danger_pairs(value) do
shadow_triggers[key] = value
end
else
rawset(_G, key, value)
end
end,
})
setmetatable(shadow_triggers, {
__index = the_real_triggers,
__newindex = function(t, key, value)
if the_real_triggers[key] == nil then
the_real_triggers[key] = function(...)
local ret = true
for _, subtrigger in ipairs(subtriggers[key]) do
local success, result
if debug and debug.traceback and xpcall then
success, result = xpcall(subtrigger, debug.traceback, ...)
else
success, result = pcall(subtrigger, ...)
end
if not success then
-- There was a message. Print the error message.
print("Error in Triggers."..key..":\n"..result)
elseif result == false then
-- If the subtrigger explicitly returned false, don't call
-- any more subtriggers.
ret = false
break
end
end
return ret
end
end
if subtriggers[key] == nil then
subtriggers[key] = {}
end
table.insert(subtriggers[key], value)
end,
__call = function(me, t)
for key, value in danger_pairs(t) do
me[key] = value
end
end,
})
-- Part 2: Make print print *both* to the command line *and* to the screen,
-- even in Triggers.init(), even in the top level.
local messages_to_print_on_first_idle = {}
local real_print = print
function print(...)
-- print prints its arguments separated by tabs
local text = table.concat({...}, "\t")
real_print(text)
if messages_to_print_on_first_idle ~= nil then
table.insert(messages_to_print_on_first_idle, text)
else
Players.print(text)
end
end
function Triggers.idle()
if messages_to_print_on_first_idle ~= nil then
for _, message in ipairs(messages_to_print_on_first_idle) do
Players.print(message)
end
messages_to_print_on_first_idle = nil
end
end
-- Part 3: There's no downside to calling the restore_* functions if they're
-- not needed. They need to be called EXACTLY once per init() if they are
-- needed. So let's call them ourselves, and cache the result, so that other
-- triggers can still call them as normal and have them work as expected.
--
-- Bonus: make it so that calling the wrong restoration function throws an
-- error.
-- Part 3: There's no downside to calling the restore_* functions if they're
-- not needed. They need to be called EXACTLY once per init() if they are
-- needed. So let's call them ourselves, and cache the result, so that other
-- triggers can still call them as normal and have them work as expected.
--
-- Bonus: make it so that calling the wrong restoration function throws an
-- error.
function Triggers.init(restoring_game)
local RealGame = Game
Game = {}
if restoring_game then
local restore_result = RealGame.restore_saved()
function Game.restore_saved()
if Level.stash["debug"] then
print("Game.restore_saved() has been cached")
end
return restore_result
end
function Game.restore_passed()
error("Game.restore_passed() called, but we were loading from a save!")
end
else
local restore_result = RealGame.restore_passed()
function Game.restore_passed()
if Level.stash["debug"] then
print("Game.restore_passed() has been cached")
end
return restore_result
end
function Game.restore_saved()
error("Game.restore_saved() called, but we were not loading from a save!")
end
end
setmetatable(Game, {
__index=RealGame,
__newindex=RealGame,
})
end
-- PREFERENCES
-- what shapes file collections contain "wall" textures
-- (which can also be used as floors or ceilings, but never mind that)
-- defaults:
-- M1: { 2, 8, 17, 18, 19, 24 }
-- M2: { 17, 18, 19, 21 }
-- MI: { 17, 18, 19, 20, 21 }
WALLS = { 17, 18, 19, 20, 21 }
-- what shapes file collections contain "landscape" textures
-- ("walls" can also be used in landscape mode, but again, never mind that)
-- defaults:
-- M1: {}
-- M2/MI: { 27, 28, 29, 30 }
LANDSCAPES = { 27, 28, 29, 30 }
SUPPRESS_ITEMS = true -- default: true; set to false for items to appear within Vasara
SUPPRESS_MONSTERS = true -- default: true; set to false for monsters to appear within Vasara
MAX_TAGS = 90 -- max: 90
MAX_SCRIPTS = 90 -- max: 90
SHOW_VISUAL_MODE_HEADER = true -- default: true; set to false to hide the Visual Mode header on startup
SHOW_TELEPORT_DESTINATION = true -- default: true; whether to highlight destination polygon in Teleport mode (this still has some bugs)
-- cursor speed settings: larger numbers mean a slower mouse
MENU_VERTICAL_RANGE = 30 -- default: 30
MENU_HORIZONTAL_RANGE = 70 -- default: 70
DRAG_VERTICAL_RANGE = 80 -- default: 80
DRAG_HORIZONTAL_RANGE = 120 -- default: 120
-- how far you can drag a texture before it stops moving (in World Units)
DRAG_VERTICAL_LIMIT = 1
DRAG_HORIZONTAL_LIMIT = 1
-- how many ticks before you start dragging a texture
DRAG_INITIAL_DELAY = 3
-- how many ticks between fast forward/rewind steps
FFW_INITIAL_DELAY = 5
FFW_REPEAT_DELAY = 0
FFW_TEXTURE_SCRUB_SPEED = 0
FFW_TELEPORT_SCRUB_SPEED = 1
-- how many ticks to highlight a latched keypress in HUD
KEY_HIGHLIGHT_DELAY = 4
-- set to true to preserve "must be explored" polygons at the cost of exploration missions becoming incompletable within Vasara
-- (kind of a hack, but probably preferable to having to reset "must be explored" polygons after texturing)
RESTORE_EXPLORATION = true
-- which menu items should be in what state when Vasara starts up
APPLY_TEXTURES = true -- default: true
APPLY_LIGHTS = false -- Vasara AF default: false; Vasara 1.0.x default: true
ALIGN_ADJACENT = true -- default: true
REALIGN_WHEN_RETEXTURING = false -- Vasara AF default: false. Vasara 1.0.x automatically did this, and it was impossible to disable
EDIT_PANELS = true -- default: true
APPLY_TRANSPARENT = false -- default: false
APPLY_TRANSFER = true -- default: true
QUANTIZE_MODE = 0 -- 0 = absolute, 1 = negative (northwest), 2 = center, 3 = positive (southeast). default: 0
QUANTIZE_X = true -- Vasara AF default: true
QUANTIZE_Y = true -- Vasara AF default: true. set both to false to disable grid snap
DEFAULT_QUANTIZE = 10 -- see snap_denominators below for possible options here: first menu option is 1, second is 2, third is 3, etc. default: 10 (1/16 WU)
OVERRIDE_TERMINAL_COUNT = true -- whether to use the terminal count in merged maps (this tells you you're using a merged map, but it's annoying)
DECOUPLE_TRANSPARENT = false -- default: false. if false, Vasara edits transparent sides on both sides of a line (its traditional behaviour); set to true to edit only one side
-- END PREFERENCES -- no user serviceable parts below ;)
MAX_LIGHTS = 98 -- maximum number of lights we can accommodate
Vasara = {
stash_value = function(key, value)
Level.stash[key] = value
end,
stash_bool = function(key, value)
if value == true then
Level.stash[key] = "TRUE"
else
Level.stash[key] = "false"
end
end,
}
Game.monsters_replenish = not SUPPRESS_MONSTERS
snap_denominators = { 1, 2, 3, 4, 5, 6, 8, 10, 12, 16, 18, 20, 24, 30, 32, 36, 40, 48, 60, 64, 128 }
snap_modes = { 1 }
for _,d in ipairs(snap_denominators) do
if d ~= 1 then table.insert(snap_modes, string.format("1/%d",d)) end
end
transfer_modes = {
TransferModes["normal"], TransferModes["pulsate"],
TransferModes["wobble"], TransferModes["fast wobble"],
TransferModes["static"], TransferModes["landscape"],
TransferModes["horizontal slide"], TransferModes["fast horizontal slide"],
TransferModes["vertical slide"], TransferModes["fast vertical slide"],
TransferModes["wander"], TransferModes["fast wander"],
TransferModes["reverse horizontal slide"], TransferModes["reverse fast horizontal slide"],
TransferModes["reverse vertical slide"], TransferModes["reverse fast vertical slide"],
TransferModes["2x"], TransferModes["4x"]
}
transfer_mode_lookup = {}
for k, v in danger_pairs(transfer_modes) do transfer_mode_lookup[v] = k - 1 end
CollectionsUsed = {}
for _, collection in danger_pairs(WALLS) do
table.insert(CollectionsUsed, collection)
end
for _, collection in danger_pairs(LANDSCAPES) do
table.insert(CollectionsUsed, collection)
end
local function restore_exploration()
local i
for i = 1, #Level._explore do
if Level._explore[i].type == "normal" then
Level._explore[i].type = "must be explored"
end
end
end
Triggers = {}
function init()
VML.init()
for p in Players() do
p.weapons.active = false
local pal = p.texture_palette
pal.highlight = 0
if p.local_ then
local colldef = Collections[0]
local typedef = TextureTypes["interface"]
pal.size = 256
for s = 0,31 do
local slot = pal.slots[s]
slot.collection = colldef
slot.texture_index = 0
slot.type = typedef
end
end
end
if RESTORE_EXPLORATION then
Level._explore = {}
for p in Polygons() do
if p.type == "must be explored" then
table.insert(Level._explore, p)
end
end
end
if SUPPRESS_ITEMS then
for item in Items() do
item:delete()
end
function Triggers.item_created(item)
item:delete()
end
end
SKeys.init()
SCollections.init()
SPanel.init()
SPlatforms.init()
SFreeze.init()
SMode.init()
SUndo.init()
inited_script = true
end
function Triggers.idle()
if not Sides.new or Game.version < "20231125" then
Players.print("Vasara AF requires Aleph One 1.7 or later")
kill_script()
return
end
if not inited_script then init() end
SKeys.update()
SCounts.update()
SLights.update()
SPlatforms.update()
SFreeze.update()
SMode.update()
SUndo.update()
SStatus.update()
SCollections.update()
SPanel.update()
for p in Players() do
p.life = 450
p.oxygen = 10800
if p._mode == SMode.teleport then
UTeleport.idle(p)
end
VML.find_target(p, false, false)
end
if Level.stash["ERROR"] ~= nil then
print(Level.stash["ERROR"])
Level.stash["ERROR"] = nil
end
if RESTORE_EXPLORATION then restore_exploration() end
end
function Triggers.postidle()
SFreeze.postidle()
for p in Players() do
if p._mode == SMode.teleport then
UTeleport.postidle(p)
end
p.life = 409 -- signal to HUD that Vasara is active
end
if RESTORE_EXPLORATION then restore_exploration() end
end
function Triggers.terminal_enter(terminal, player)
if terminal then
player._terminal = true
end
end
function Triggers.terminal_exit(_, player)
player._terminal = false
end
function Triggers.player_damaged(p, ap, am, dt, da, pr)
p.life = 450
p.oxygen = 10800
end
function Triggers.cleanup()
for p in Players() do
if p._teleport.last_target ~= nil then
uTeleport.remove_highlight(p)
end
end
if RESTORE_EXPLORATION then restore_exploration() end
end
function PIN(v, min, max)
if v < min then return min end
if v > max then return max end
return v
end
SMode = {
apply = 0,
choose = 1,
attribute = 2,
teleport = 3,
panel = 4,
init = function()
for p in Players() do
p._mode = SMode.apply
p._prev_mode = SMode.apply
p._mic_dummy = false
p._target_poly = 0
p._quantize = 0
p._quantize_x = QUANTIZE_X
p._quantize_y = QUANTIZE_Y
p._menu_button = nil
p._menu_item = 0
p._cursor_x = 320
p._cursor_y = 240
p._advanced_mode = not SHOW_VISUAL_MODE_HEADER
p._override_landscape = not AUTOMATIC_LANDSCAPE
SMode.default_attribute(p)
p._teleport = {
last_target = nil,
last_target_mode = nil,
}
p._panel = {
editing = false,
classnum = 0,
permutation = 0,
light_dependent = false,
only_toggled_by_weapons = false,
repair = false,
status = false,
surface = nil,
sides = {},
dinfo = nil,
}
p._saved_facing = {
direction = 0,
elevation = 0,
x = 0,
y = 0,
z = 0,
just_set = false,
}
p._saved_surface = {
surface = nil,
polygon = nil,
x = 0,
y = 0,
dragstart = 0,
align_table = nil,
offset_table = nil,
opposite_surface = nil,
opposite_offsets = nil,
opposite_rem = 0,
}
-- p._annotation = Annotations.new(Polygons[0], "")
if p.local_ then
p.texture_palette.slots[40].texture_index = p._mode
end
end
end,
current_menu_name = function(p)
if p._mode == SMode.attribute then
return SMode.attribute
elseif p._mode == SMode.choose then
return "choose_" .. p._collections.current_collection
elseif p._mode == SMode.panel then
return SPanel.menu_name(p)
end
return nil
end,
update = function()
for p in Players() do
p._prev_mode = p._mode
-- process mode actions
if p._mode == SMode.apply then
SMode.handle_apply(p)
elseif p._mode == SMode.teleport then
SMode.handle_teleport(p)
elseif p._mode == SMode.choose then
SMode.handle_choose(p)
elseif p._mode == SMode.attribute then
SMode.handle_attribute(p)
elseif p._mode == SMode.panel then
SMode.handle_panel(p)
end
-- handle mode switches
if not p._keys.mic.down then
if p._keys.map.pressed then
if p._overhead then
p._mode = SMode.teleport
else
p._mode = SMode.apply
end
elseif p._keys.action.pressed then
-- only allow default action trigger in apply and teleport
if (p._mode ~= SMode.teleport and p._mode ~= SMode.apply) or (not p:find_action_key_target()) then
p.action_flags.action_trigger = false
SMode.toggle(p, SMode.attribute)
end
elseif p._keys.mic.released and (not p._mic_dummy) then
SMode.toggle(p, SMode.choose)
elseif p._keys.secondary.released and p._mode ~= SMode.apply then
SMode.toggle(p, p._mode)
end
end
-- track mic-as-modifier, and don't switch if we use that
if p._keys.mic.down then
if p._keys.prev_weapon.down or p._keys.next_weapon.down or p._keys.action.down or p._keys.primary.down or p._keys.primary.released or p._keys.secondary.down or p._keys.secondary.released then
p._mic_dummy = true
end
elseif p._keys.mic.released then
p._mic_dummy = false
end
local in_menu = SMode.menu_mode(p._mode)
if p._mode ~= p._prev_mode then
p._menu_button = nil
p._menu_item = 0
local was_menu = SMode.menu_mode(p._prev_mode)
-- special cleanup for exiting modes
if p._prev_mode == SMode.teleport then
UTeleport.remove_highlight(p)
elseif p._prev_mode == SMode.panel then
SPanel.stop_editing(p)
end
-- special setup for entering modes
if p._mode == SMode.attribute then
SMode.start_attribute(p)
end
if in_menu then
SFreeze.enter_mode(p, "menu")
else
SFreeze.enter_mode(p, nil)
end
end
if p.local_ then
p.texture_palette.slots[37].texture_index = p._target_poly % 128
p.texture_palette.slots[38].texture_index = math.floor(p._target_poly/128)
p.texture_palette.slots[40].texture_index = p._mode
-- set cursor
if in_menu then
p._cursor_x, p._cursor_y = SFreeze.coord(p)
elseif p._mode == SMode.apply then
p._cursor_x = 320
p._cursor_y = 72 + 160
if p._advanced_mode then p._cursor_y = 196 end
if SFreeze.in_mode(p, "drag") then
local delta_yaw, delta_pitch
delta_yaw, delta_pitch = SFreeze.coord(p)
p._cursor_x = p._cursor_x + math.floor(delta_yaw * 300.0/1024.0)
p._cursor_y = p._cursor_y + math.floor(delta_pitch * 140.0/1024.0)
end
elseif p._mode == SMode.teleport then
p._cursor_x = 320
p._cursor_y = math.floor((3*72 + 480)/4)
if p._advanced_mode then p._cursor_y = 480/4 end
end
end
end
end,
menu_mode = function(mode)
return mode == SMode.choose or mode == SMode.attribute or mode == SMode.panel
end,
toggle = function(p, mode)
if p._mode == mode then
p._mode = SMode.apply
else
p._mode = mode
end
if p._overhead then
p.action_flags.toggle_map = true
p._overhead = false
end
end,
handle_apply = function(p)
local clear_surface = true
if p._keys.mic.down then
if p._keys.next_weapon.held then
SFreeze.unfreeze(p)
p:accelerate(0, 0, 0.05)
elseif p._keys.prev_weapon.pressed then
SFreeze.toggle_freeze(p)
end
else
if p._keys.primary.down then
-- apply
clear_surface = false
local surface = p._saved_surface.surface
if p._keys.primary.pressed then
surface, polygon = SCollections.find_surface(p, false)
local coll = p._collections.current_collection
local landscape = false
if coll == 0 then
coll = p._collections.current_landscape_collection
landscape = true
end
local tex = p._collections.current_textures[coll]
p._saved_surface.surface = surface
p._saved_surface.opposite_surface = nil
p._saved_surface.polygon = polygon
if (not p._apply.texture) or (surface.collection and ((coll == surface.collection.index) and (tex == surface.texture_index)) or not p._apply.realign) then
p._saved_surface.x = surface.texture_x
p._saved_surface.y = surface.texture_y
else
p._saved_surface.x = 0
if is_side(o) then
local bottom, top = VML.surface_heights(surface)
p._saved_surface.y = bottom - top
else
p._saved_surface.y = 0
end
end
p._saved_surface.dragstart = Game.ticks
SUndo.add_undo(p, surface)
UApply.apply_texture(p, surface, coll, tex, landscape)
if is_transparent_side(surface) and not p._apply.decouple_transparent then
-- put the same texture on the opposite side of the line
local dsurface = nil
local side = Sides[surface.index]
local line = side.line
if line.clockwise_side == side then
if line.counterclockwise_side then
dsurface = line.counterclockwise_side.transparent
elseif line.counterclockwise_polygon then
dsurface = Sides.new(line.counterclockwise_polygon, line).transparent
VML.init()
end
else
if line.clockwise_side then
dsurface = line.clockwise_side.transparent
elseif line.clockwise_polygon then
dsurface = Sides.new(line.clockwise_polygon, line).transparent
VML.init()
end
end
if dsurface then
SUndo.add_undo(p, dsurface)
UApply.apply_texture(p, dsurface, coll, tex, landscape)
local rem = line.length - math.floor(line.length)
dsurface.texture_x = 0 - dsurface.texture_x - rem
p._saved_surface.opposite_surface = dsurface
p._saved_surface.opposite_rem = rem
end
end
if p._apply.align then
if is_polygon_floor(surface) or is_polygon_ceiling(surface) then
p._saved_surface.align_table = VML.build_polygon_align_table(polygon, surface)
local is_floor = is_polygon_floor(surface)
for s in danger_pairs(p._saved_surface.align_table) do
if is_floor then
SUndo.add_undo(p, s.floor)
else
SUndo.add_undo(p, s.ceiling)
end
end
VML.align_polygons(surface, p._saved_surface.align_table)
else
p._saved_surface.offset_table = VML.build_side_offsets_table(surface)
for s in danger_pairs(p._saved_surface.offset_table) do
SUndo.add_undo(p, s)
end
VML.align_sides(surface, p._saved_surface.offset_table)
local dsurface = p._saved_surface.opposite_surface
if dsurface then
local doffsets = VML.build_side_offsets_table(dsurface)
p._saved_surface.opposite_offsets = doffsets
for s in danger_pairs(doffsets) do
SUndo.add_undo(p, s)
end
VML.align_sides(dsurface, doffsets)
end
end
end
elseif surface and (Game.ticks > p._keys.primary.first + DRAG_INITIAL_DELAY) then
SFreeze.enter_mode(p, "drag")
local delta_yaw, delta_pitch
delta_yaw, delta_pitch = SFreeze.coord(p)
delta_yaw = delta_yaw / 1024.0
delta_pitch = delta_pitch / 1024.0
if is_polygon_floor(surface) or is_polygon_ceiling(surface) then
if is_polygon_ceiling(surface) then delta_pitch = -delta_pitch end
local orad = math.rad(SFreeze.orig_dir(p))
local xoff = delta_pitch * math.cos(orad) + delta_yaw * math.sin(orad)
local yoff = delta_pitch * math.sin(orad) - delta_yaw * math.cos(orad)
local dx = p._saved_surface.x + xoff
local dy = p._saved_surface.y + yoff
if p._apply.quantize_mode == 0 then -- absolute
if p._quantize_x then surface.texture_x = VML.quantize(p, dx) else surface.texture_x = dx end
if p._quantize_y then surface.texture_y = VML.quantize(p, dy) else surface.texture_y = dy end
elseif p._apply.quantize_mode == 2 then -- center
if p._quantize_x then
surface.texture_x = VML.quantize_polygon_center(p, dx, surface, true)
else
surface.texture_x = dx
end
if p._quantize_y then
surface.texture_y = VML.quantize_polygon_center(p, dy, surface, false)
else
surface.texture_y = dy
end
else -- negative, positive
local is_max = p._apply.quantize_mode == 3
if p._quantize_x then
surface.texture_x = VML.quantize_polygon_corner(p, dx, surface, true, is_max)
else
surface.texture_x = dx
end
if p._quantize_y then
surface.texture_y = VML.quantize_polygon_corner(p, dy, surface, false, is_max)
else
surface.texture_y = dy
end
end
if p._apply.align then
VML.align_polygons(surface, p._saved_surface.align_table)
end
else
local dx = p._saved_surface.x - delta_yaw
local dy = p._saved_surface.y - delta_pitch
if p._apply.quantize_mode == 2 then -- center
if p._quantize_x then
surface.texture_x = VML.quantize_side_center_x(p, dx, Sides[surface.index])
else
surface.texture_x = dx
end
if p._quantize_y then
surface.texture_y = VML.quantize_surface_center_y(p, dy, surface)
else
surface.texture_y = dy
end
elseif p._apply.quantize_mode == 3 then -- positive
if p._quantize_x then
surface.texture_x = VML.quantize_side_right(p, dx, Sides[surface.index])
else
surface.texture_x = dx
end
if p._quantize_y then
surface.texture_y = VML.quantize_surface_bottom_y(p, dy, surface)
else
surface.texture_y = dy
end
else -- negative, absolute
if p._quantize_x then
surface.texture_x = VML.quantize(p, dx)
else
surface.texture_x = dx
end
if p._quantize_y then
surface.texture_y = VML.quantize(p, dy)
else
surface.texture_y = dy
end
end
if p._apply.align then
VML.align_sides(surface, p._saved_surface.offset_table)
end
local dsurface = p._saved_surface.opposite_surface
if dsurface then
dsurface.texture_x = 0 - surface.texture_x - p._saved_surface.opposite_rem
dsurface.texture_y = surface.texture_y
if p._apply.align then
VML.align_sides(dsurface, p._saved_surface.opposite_offsets)
end
end
end
end
elseif p._keys.primary.released then
-- release any drag
SFreeze.enter_mode(p, nil)
-- are we editing control panels
if p._apply.texture and p._apply.edit_panels and is_primary_side(p._saved_surface.surface) then
if SPanel.surface_can_hold_panel(p._saved_surface.surface) then
-- valid for control panels; configure it
SPanel.start_editing(p, p._saved_surface.surface)
if p._apply.align then
for s in danger_pairs(p._saved_surface.offset_table) do
SPanel.add_for_editing(p, s)
end
end
clear_surface = false
SMode.toggle(p, SMode.panel)
else
-- not a valid texture for control panels; clear it
Sides[p._saved_surface.surface.index].control_panel = false
if p._apply.align then
for s in danger_pairs(p._saved_surface.offset_table) do
Sides[s.index].control_panel = false
end
end
end
end
elseif p._keys.secondary.released then
-- sample
local surface = SCollections.find_surface(p, true)
if surface and (not (is_transparent_side(surface) and surface.empty)) then
SCollections.set(p, surface.collection.index, surface.texture_index)
if p._collections.current_collection ~= 0 then
p._light = surface.light.index
p._transfer_mode = transfer_mode_lookup[surface.transfer_mode]
end
end
elseif p._keys.prev_weapon.pressed then
p._light = (p._light - 1) % #Lights
elseif p._keys.next_weapon.pressed then
p._light = (p._light + 1) % #Lights
end
end
if clear_surface then p._saved_surface.surface = nil end
end,
handle_teleport = function(p)
if p._saved_facing.just_set then
p._saved_facing.direction = p.direction
p._saved_facing.elevation = p.elevation
p._saved_facing.just_set = false
end
if (p._saved_facing.direction ~= p.direction) or
(p._saved_facing.elevation ~= p.elevation) or
(p._saved_facing.x ~= p.x) or
(p._saved_facing.y ~= p.y) or
(p._saved_facing.z ~= p.z) then
p._saved_facing.direction = p.direction
p._saved_facing.elevation = p.elevation
p._saved_facing.x = p.x
p._saved_facing.y = p.y
p._saved_facing.z = p.z
local o, x, y, z, poly = VML.find_target(p, false, false)
if poly then
p._target_poly = poly.index
UTeleport.highlight(p, poly)
end
SMode.annotate(p)
end
if p._keys.mic.down then
if p._keys.next_weapon.held then
SFreeze.unfreeze(p)
p:accelerate(0, 0, 0.05)
elseif p._keys.prev_weapon.pressed then
SFreeze.toggle_freeze(p)
end
end
if (not p._keys.mic.down) and p._keys.primary.released then
local poly = Polygons[p._target_poly]
p:position(poly.x, poly.y, poly.z, poly)
p.monster:play_sound("teleport in")
UTeleport.remove_highlight(p)
SFreeze.unfreeze(p)
return
end
if ((not p._keys.mic.down) and (p._keys.prev_weapon.held or p._keys.next_weapon.held)) or (p._keys.mic.down and (p._keys.primary.held or p._keys.secondary.held)) then
local diff = 1
if p._keys.prev_weapon.held and (not p._keys.mic.down) then
diff = -1
elseif p._keys.primary.held and p._keys.mic.down then
if p._keys.primary.repeated then
diff = 1 + FFW_TELEPORT_SCRUB_SPEED
else
diff = 1
end
elseif p._keys.secondary.held and p._keys.mic.down then
if p._keys.secondary.repeated then
diff = -1 - FFW_TELEPORT_SCRUB_SPEED
else
diff = -1
end
end
p._target_poly = (p._target_poly + diff) % #Polygons
SMode.annotate(p)
local poly = Polygons[p._target_poly]
UTeleport.highlight(p, poly)
local xdist = poly.x - p.x
local ydist = poly.y - p.y
local zdist = poly.z - (p.z + 614/1024)
local tdist = math.sqrt(xdist*xdist + ydist*ydist + zdist*zdist)
local el = math.asin(zdist/tdist)
local dir = math.atan2(ydist, xdist)
p.direction = math.deg(dir)
p.elevation = math.deg(el)
p._saved_facing.just_set = true
end
end,
annotate = function(p)
local poly = Polygons[p._target_poly]
-- p._annotation.polygon = poly
-- p._annotation.text = poly.index
-- p._annotation.x = poly.x
-- p._annotation.y = poly.y
end,
handle_choose = function(p)
-- cycle textures
if (p._keys.mic.down and (p._keys.primary.held or p._keys.secondary.held)) or ((not p._keys.mic.down) and (p._keys.prev_weapon.held or p._keys.next_weapon.held)) then
local diff = 1
if p._keys.prev_weapon.held then
diff = -1
elseif p._keys.mic.down and p._keys.primary.repeated then
diff = 1 + FFW_TEXTURE_SCRUB_SPEED
elseif p._keys.mic.down and p._keys.secondary.repeated then
diff = 0 - (1 + FFW_TEXTURE_SCRUB_SPEED)
elseif p._keys.mic.down and p._keys.secondary.held then
diff = -1
end
local cur = p._collections.current_collection
if cur == 0 then
local bct = 0
local tex = 0
for _, collection in danger_pairs(SCollections.landscape_collections) do
if collection == p._collections.current_landscape_collection then
local info = SCollections.collection_map[collection]
tex = info.offset + p._collections.current_textures[collection]
end
bct = bct + Collections[collection].bitmap_count
end
tex = (tex + diff) % bct
for _, collection in danger_pairs(SCollections.landscape_collections) do
local info = SCollections.collection_map[collection]
if tex >= info.offset and tex < (info.offset + info.count) then
local ct = tex - info.offset
SCollections.set(p, collection, ct)
break
end
end
else
local tex = p._collections.current_textures[cur]
local bct = Collections[cur].bitmap_count
local ct = (tex + diff) % bct
SCollections.set(p, cur, ct)
end
end
if p._keys.mic.down and (p._keys.next_weapon.held or p._keys.prev_weapon.held) then
-- cycle collections
local diff = 1
if p._keys.prev_weapon.held then diff = -1 end
local cur = p._collections.current_collection
local ci = 0
for i, c in ipairs(SCollections.wall_collections) do
if cur == c then
ci = i
break
end
end