-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVisual Mode.lua
More file actions
1961 lines (1775 loc) · 54.1 KB
/
Visual Mode.lua
File metadata and controls
1961 lines (1775 loc) · 54.1 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
-- Visual Mode.lua
-- A Lua script to enable in-game texture editing
-- By Irons and Smith
-- Released under the JUICE LICENSE!
-- user configurable stuff here
walls = { 17, 18, 19, 20, 21 }
landscapes = { 27, 28, 29, 30 }
overlay_color = "white"
suppress_items = true
suppress_monsters = true
max_tags = 32
max_scripts = 150
-- snap textures to 1/x WU increments while dragging
snap_denominators = { 4, 5 }
-- choose the position of one of the denominators in the list above if
-- you want snap on by default; 0 is off
default_snap = 0
-- don't modify below this line!
Game.monsters_replenish = not suppress_monsters
CollectionsUsed = {}
for _, collection in pairs(walls) do
table.insert(CollectionsUsed, collection)
end
for _, collection in pairs(landscapes) do
table.insert(CollectionsUsed, collection)
end
transfer_modes = { TransferModes["normal"], TransferModes["pulsate"], TransferModes["wobble"], TransferModes["fast wobble"], TransferModes["landscape"], TransferModes["horizontal slide"], TransferModes["fast horizontal slide"], TransferModes["vertical slide"], TransferModes["fast vertical slide"], TransferModes["wander"], TransferModes["fast wander"], TransferModes["static"], TransferModes["reverse horizontal slide"], TransferModes["reverse fast horizontal slide"], TransferModes["reverse vertical slide"], TransferModes["reverse fast vertical slide"], TransferModes["2x"], TransferModes["4x"] }
-- short names for transfer modes
TransferModes["normal"]._short = "normal"
TransferModes["pulsate"]._short = "pulsate"
TransferModes["wobble"]._short = "wobble"
TransferModes["fast wobble"]._short = "f wobble"
TransferModes["landscape"]._short = "landscap"
TransferModes["horizontal slide"]._short = "h slide"
TransferModes["fast horizontal slide"]._short = "fh slide"
TransferModes["vertical slide"]._short = "v slide"
TransferModes["fast vertical slide"]._short = "fv slide"
TransferModes["wander"]._short = "wander"
TransferModes["fast wander"]._short = "f wander"
TransferModes["static"]._short = "static"
TransferModes["reverse horizontal slide"]._short = "h sl r"
TransferModes["reverse fast horizontal slide"]._short = "fh sl r"
TransferModes["reverse vertical slide"]._short = "v sl r"
TransferModes["reverse fast vertical slide"]._short = "fv sl r"
TransferModes["2x"]._short = "2x"
TransferModes["4x"]._short = "4x"
VERBOSE = true
TICKS_BETWEEN_INCREMENT = 1
TRIGGER_DELAY = 4
function quantize(player, value)
if player._quantize == 0 then
return value
end
local ratio = 1.0 / snap_denominators[player._quantize]
return math.floor(value / ratio + 0.5) * ratio
end
function find_line_intersection(line, x0, y0, z0, x1, y1, z1)
local dx = x1 - x0
local dy = y1 - y0
local dz = z1 - z0
local ldx = line.endpoints[1].x - line.endpoints[0].x
local ldy = line.endpoints[1].y - line.endpoints[0].y
local t
if ldx * dy - ldy * dx == 0 then
t = 0
else
t = (ldx * (line.endpoints[0].y - y0) + ldy * (x0 - line.endpoints[0].x)) / (ldx * dy - ldy * dx)
end
return x0 + t * dx, y0 + t * dy, z0 + t * dz
end
function find_floor_or_ceiling_intersection(height, x0, y0, z0, x1, y1, z1)
local dx = x1 - x0
local dy = y1 - y0
local dz = z1 - z0
local t
if dz == 0 then
t = 0
else
t = (height - z0) / dz
end
return x0 + t * dx, y0 + t * dy, z
end
function find_target(player, find_first_line, find_first_side)
local polygon = player.monster.polygon
local x0, y0, z0 = player.x, player.y, player.z + 0.6
local x1, y1, z1 = x0, y0, z0
local dx = math.cos(math.rad(player.pitch)) * math.cos(math.rad(player.yaw))
local dy = math.cos(math.rad(player.pitch)) * math.sin(math.rad(player.yaw))
local dz = math.sin(math.rad(player.pitch))
local line
x1 = x1 + dx
y1 = y1 + dy
z1 = z1 + dz
repeat
line = polygon:find_line_crossed_leaving(x0, y0, x1, y1)
if line then
local x, y, z = find_line_intersection(line, x0, y0, z0, x1, y1, z1)
if z > polygon.ceiling.height then
x, y, z = find_floor_or_ceiling_intersection(polygon.ceiling.height, x0, y0, z0, x1, y1, z1)
return polygon.ceiling, x, y, z, polygon
elseif z < polygon.floor.height then
x, y, z = find_floor_or_ceiling_intersection(polygon.ceiling.height, x0, y0, z0, x1, y1, z1)
return polygon.floor, x, y, z, polygon
else
local opposite_polygon
if line.clockwise_polygon == polygon then
opposite_polygon = line.counterclockwise_polygon
elseif line.counterclockwise_polygon == polygon then
opposite_polygon = line.clockwise_polygon
end
if not opposite_polygon or find_first_line then
-- always stop
-- locate the side
if line.clockwise_polygon == polygon then
if line.clockwise_side then
return line.clockwise_side, x, y, z, polygon
else
return line, x, y, z, polygon
end
else
if line.counterclockwise_side then
return line.counterclockwise_side, x, y, z, polygon
else
return line, x, y, z, polygon
end
end
elseif find_first_side and line.has_transparent_side then
if line.clockwise_polygon == polygon then
return line.clockwise_side, x, y, z, polygon
else
return line.counterclockwise_side, x, y, z, polygon
end
else
-- can we pass
if z < opposite_polygon.floor.height or z > opposite_polygon.ceiling.height then
if line.clockwise_polygon == polygon then
if line.clockwise_side then
return line.clockwise_side, x, y, z, polygon
else
return line, x, y, z, polygon
end
else
if line.counterclockwise_side then
return line.counterclockwise_side, x, y, z, polygon
else
return line, x, y, z, polygon
end
end
else
-- pass
polygon = opposite_polygon
end
end
end
else
-- check if we hit the floor, or ceiling
if z1 > polygon.ceiling.height then
local x, y, z = find_floor_or_ceiling_intersection(polygon.ceiling.height, x0, y0, z0, x1, y1, z1)
return polygon.ceiling, x, y, z, polygon
elseif z1 < polygon.floor.height then
local x, y, z = find_floor_or_ceiling_intersection(polygon.floor.height, x0, y0, z0, x1, y1, z1)
return polygon.floor, x, y, z, polygon
else
x1 = x1 + dx
y1 = y1 + dy
z1 = z1 + dz
end
end
until x1 > 32 or x1 < -32 or y1 > 32 or y1 < -32 or z1 > 32 or z1 < -32
-- uh oh
print("POOP!")
return nil
end
function set_collection(player, collection)
if player._collection ~= collection then
if collection == 0 then
-- not really interface, landscape!
player.texture_palette.size = #landscape_palette
if player.local_ then
for i = 1, #landscape_palette do
player.texture_palette.slots[i - 1].collection = landscape_palette[i].collection
player.texture_palette.slots[i - 1].texture_index = landscape_palette[i].texture_index
if Game.version >= "20090801" then
player.texture_palette.slots[i - 1].type = TextureTypes["landscape"]
end
end
end
if player._texture >= #landscape_palette then
player._texture = 0
end
else
player.texture_palette.size = collection.bitmap_count
if player.local_ then
for i = 0, collection.bitmap_count - 1 do
player.texture_palette.slots[i].collection = collection
player.texture_palette.slots[i].texture_index = i
if Game.version >= "20090801" then
player.texture_palette.slots[i].type = TextureTypes["wall"]
end
end
end
if player._texture >= collection.bitmap_count then
player._texture = 0
end
end
player._collection = collection
end
end
function get_clockwise_side_endpoint(side)
local line_is_clockwise = true
if side.line.clockwise_polygon ~= side.polygon then
-- counterclockwise line
return side.line.endpoints[0]
else
return side.line.endpoints[1]
end
end
function get_counterclockwise_side_endpoint(side)
local line_is_clockwise = true
if side.line.clockwise_polygon ~= side.polygon then
-- counterclockwise line
return side.line.endpoints[1]
else
return side.line.endpoints[0]
end
end
function new_side(polygon, line)
local side = Sides.new(polygon, line)
table.insert(cw_endpoint_sides[get_clockwise_side_endpoint(side)], side)
table.insert(ccw_endpoint_sides[get_counterclockwise_side_endpoint(side)], side)
return side
end
-- returns primary_side, secondary_side, or transparent_side
function side_surface(side, z)
if side.type == "full" then
local opposite_polygon
if side.line.clockwise_side == side then
opposite_polygon = side.line.counterclockwise_polygon
else
opposite_polygon = side.line.clockwise_polygon
end
if opposite_polygon then
return side.transparent
else
return side.primary
end
elseif side.type == "high" then
if z > side.line.lowest_adjacent_ceiling then
return side.primary
else
return side.transparent
end
elseif side.type == "low" then
if z < side.line.highest_adjacent_floor then
return side.primary
else
return side.transparent
end
else
if z > side.line.lowest_adjacent_ceiling then
return side.primary
elseif z < side.line.highest_adjacent_floor then
return side.secondary
else
return side.transparent
end
end
end
function surface_heights(surface)
local side = Sides[surface.index]
if is_primary_side(surface) then
if side.type == "full" then
return side.polygon.floor.height, side.polygon.ceiling.height
elseif side.type == "low" then
return side.polygon.floor.height, side.line.highest_adjacent_floor
else
return side.line.lowest_adjacent_ceiling, side.polygon.ceiling.height
end
elseif is_secondary_side(surface) then
if side.type == "split" then
return side.polygon.floor.height, side.line.highest_adjacent_floor
else
return nil
end
else -- transparent
if side.type == "full" then
return side.polygon.floor.height, side.polygon.ceiling.height
elseif side.type == "low" then
return side.line.highest_adjacent_floor, side.polygon.ceiling.height
elseif side.type == "high" then
return side.polygon.floor.height, side.line.lowest_adjacent_ceiling
else -- split
return side.line.highest_adjacent_floor, side.line.lowest_adjacent_ceiling
end
end
end
function apply_texture(player, surface, texture_x, texture_y)
if player._apply_textures then
if player._collection == 0 then
surface.collection = landscape_palette[player._texture + 1].collection
surface.texture_index = landscape_palette[player._texture + 1].texture_index
surface.transfer_mode = "landscape"
else
surface.collection = player._collection
surface.texture_index = player._texture
surface.transfer_mode = player._transfer
end
surface.texture_x = texture_x
surface.texture_y = texture_y
end
if player._apply_lights then
surface.light = Lights[player._light]
end
end
function build_undo(surface)
local collection = surface.collection
local texture_index = surface.texture_index
local transfer_mode = surface.transfer_mode
local light = surface.light
local texture_x = surface.texture_x
local texture_y = surface.texture_y
local empty = is_transparent_side(surface) and surface.empty
local device
if is_primary_side(surface) then
local side = Sides[surface.index]
if side.control_panel then
device = {}
device.device = side.control_panel.type
device.light_dependent = side.control_panel.light_dependent
device.permutation = side.control_panel.permutation
device.only_toggled_by_weapons = side.control_panel.only_toggled_by_weapons
device.repair = side.control_panel.repair
device.status = side.control_panel.status
end
end
local function undo()
if empty then
surface.empty = true
else
if collection then
surface.collection = collection
end
surface.texture_index = texture_index
surface.transfer_mode = transfer_mode
surface.light = light
if device then
save_control_panel(Sides[surface.index], device)
elseif is_primary_side(surface) then
Sides[surface.index].control_panel = false
end
end
surface.texture_x = texture_x
surface.texture_y = texture_y
end
return undo
end
function undo(player)
if not player._undo then return end
local redo = {}
for s, f in pairs(player._undo) do
redo[s] = build_undo(s)
f()
end
player._undo = redo
end
function copy_texture(player, surface)
if is_transparent_side(surface) and surface.empty then return end
for _, v in pairs(landscapes) do
if surface.collection == v then
-- find it in the landscape palette
for index, entry in pairs(landscape_palette) do
if surface.collection == entry.collection
and surface.texture_index == entry.texture_index
then
player._texture = index - 1
set_collection(player, Collections[0])
player._light = surface.light.index
return
end
end
end
end
player._texture = surface.texture_index
player._transfer = surface.transfer_mode
set_collection(player, surface.collection)
player._light = surface.light.index
end
function valid_surfaces(side)
local surfaces = {}
if side.type == "split" then
table.insert(surfaces, side.primary)
table.insert(surfaces, side.secondary)
table.insert(surfaces, side.transparent)
elseif side.type == "full" then
table.insert(surfaces, side.primary)
else
table.insert(surfaces, side.primary)
table.insert(surfaces, side.transparent)
end
return surfaces
end
function build_side_offsets_table(first_surface)
local surfaces = {}
local offsets = {} -- surface -> offset
table.insert(surfaces, first_surface)
offsets[first_surface] = 0
while # surfaces > 0 do
-- remove the first surface
local surface = table.remove(surfaces, 1)
local low, high = surface_heights(surface)
local side = Sides[surface.index]
-- consider neighboring surfaces on this side
local neighbors = {}
if side.type == "split" then
if is_transparent_side(surface) then
table.insert(neighbors, side.primary)
table.insert(neighbors, side.secondary)
else
-- check for "joined" split
local bottom, top = surface_heights(side.transparent)
if bottom == top then
if is_primary_side(surface) then
table.insert(neighbors, side.secondary)
else
table.insert(neighbors, side.primary)
end
else
table.insert(neighbors, side.transparent)
end
end
elseif side.type ~= "full" then
if is_primary_side(surface) then
table.insert(neighbors, side.transparent)
elseif is_transparent_side(surface) then
table.insert(neighbors, side.primary)
end
end
for _, neighbor in pairs(neighbors) do
if offsets[neighbor] == nil
and surface.texture_index == neighbor.texture_index
and surface.collection == neighbor.collection
then
offsets[neighbor] = offsets[surface]
table.insert(surfaces, neighbor)
end
end
local line = Sides[surface.index].line
local length = line.length
-- consider any clockwise adjacent surfaces within our height range
for _, side in pairs(ccw_endpoint_sides[get_clockwise_side_endpoint(Sides[surface.index])]) do
if side.line ~= line then
for _, neighbor_surface in pairs(valid_surfaces(side)) do
local bottom, top = surface_heights(neighbor_surface)
if offsets[neighbor_surface] == nil
and neighbor_surface.texture_index == surface.texture_index
and neighbor_surface.collection == surface.collection
and high > bottom and top > low
then
offsets[neighbor_surface] = offsets[surface] + length
table.insert(surfaces, neighbor_surface)
end
end
end
end
-- consider any counterclockwise adjacent surfaces within our height range
for _, side in pairs(cw_endpoint_sides[get_counterclockwise_side_endpoint(Sides[surface.index])]) do
if side.line ~= line then
for _, neighbor_surface in pairs(valid_surfaces(side)) do
local bottom, top = surface_heights(neighbor_surface)
if offsets[neighbor_surface] == nil
and neighbor_surface.texture_index == surface.texture_index
and neighbor_surface.collection == surface.collection
and high > bottom and top > low
then
offsets[neighbor_surface] = offsets[surface] - side.line.length
table.insert(surfaces, neighbor_surface)
end
end
end
end
end
return offsets
end
function align_sides(surface, offsets)
local x = surface.texture_x
local y = surface.texture_y
local _, top = surface_heights(surface)
for surface, offset in pairs(offsets) do
local _, new_top = surface_heights(surface)
surface.texture_x = x + offset
surface.texture_y = y + top - new_top
end
end
function build_polygon_align_table(polygon, surface)
local polygons = {}
local accessor
if is_polygon_floor(surface) then
accessor = "floor"
else
accessor = "ceiling"
end
local function recurse(p)
if not polygons[p] -- already visited
and p[accessor].texture_index == surface.texture_index
and p[accessor].collection == surface.collection
and p[accessor].z == surface.z
then
-- add this polygon, and search for any adjacent
polygons[p] = true
for adjacent in p:adjacent_polygons() do
recurse(adjacent)
end
end
end
recurse(polygon)
return polygons
end
function align_polygons(surface, align_table)
local x = surface.texture_x
local y = surface.texture_y
local accessor
if is_polygon_floor(surface) then
accessor = "floor"
else
accessor = "ceiling"
end
for p in pairs(align_table) do
p[accessor].texture_x = x
p[accessor].texture_y = y
end
end
function interleave_icons(icon1, icon2)
result = ""
for i = 1, 8 do
result = result .. icon1[i] .. icon2[i] .. "\n"
end
return result
end
function build_texture_icon(player)
local texture, light, align, transparent
if player._apply_textures then
texture = icon_textures
else
texture = icon_empty
end
if player._apply_lights then
light = icon_lights
else
light = icon_empty
end
if player._apply_aligned and player._apply_textures then
align = icon_align
else
align = icon_empty
end
if player._apply_transparent then
transparent = icon_transparent
else
transparent = icon_empty
end
player._texture_icon = icon_status_colors .. interleave_icons(texture, light) .. interleave_icons(align, transparent)
end
Modes = {}
Modes.texture = {}
Modes.move = {}
Modes.device = {}
Modes.texture.next = Modes.move
Modes.move.next = Modes.texture
Modes.texture.name = "texture"
Modes.move.name = "move"
Modes.device.name = "device"
function Modes.texture.handle(p)
-- handle undo and mode change
if p.action_flags.action_trigger then
if p.action_flags.microphone_button then
p.action_flags.action_trigger = false
undo(p)
return
elseif not p:find_action_key_target() then
p.action_flags.action_trigger = false
p._mode = Modes.move
return
end
end
if p._overhead then
if p.action_flags.microphone_button then
-- n: cycle modes forward
if p.action_flags.cycle_weapons_forward then
p.action_flags.cycle_weapons_forward = false
local index
-- find this transfer mode
for i, mode in ipairs(transfer_modes) do
if mode == p._transfer then
index = i
break
end
end
p._transfer = transfer_modes[(index % #transfer_modes) + 1]
end
-- p: cycle modes backward
if p.action_flags.cycle_weapons_backward then
p.action_flags.cycle_weapons_backward = false
local index
-- find this transfer mode
for i, mode in ipairs(transfer_modes) do
if mode == p._transfer then
index = i
break
end
end
p._transfer = transfer_modes[((index - 2) % #transfer_modes) + 1]
end
-- t1: toggle edit devices
if p.action_flags.left_trigger then
p.action_flags.left_trigger = false
if not p._trigger_release.left_trigger then
p._trigger_release.left_trigger = true
p._edit_control_panels = not p._edit_control_panels
end
else
p._trigger_release.left_trigger = false
end
-- t2: quantize
if p.action_flags.right_trigger then
p.action_flags.right_trigger = false
if not p._trigger_release.right_trigger then
p._trigger_release.right_trigger = true
p._quantize = (p._quantize + 1) % (# snap_denominators + 1)
end
else
p._trigger_release.right_trigger = false
end
p.overlays[2].text = "transfer-"
p.overlays[3].text = "transfer+"
if p._edit_control_panels then
p.overlays[4].text = "edit panels"
else
p.overlays[4].text = "no panels"
end
if p._quantize == 0 then
p.overlays[5].text = "no snap"
else
p.overlays[5].text = "snap 1/" .. tostring(snap_denominators[p._quantize])
end
else
-- n: toggle apply textures
if p.action_flags.cycle_weapons_forward then
p.action_flags.cycle_weapons_forward = false
p._apply_textures = not p._apply_textures
build_texture_icon(p)
end
-- p: toggle apply lights
if p.action_flags.cycle_weapons_backward then
p.action_flags.cycle_weapons_backward = false
p._apply_lights = not p._apply_lights
build_texture_icon(p)
end
-- t1: toggle apply aligned
if p.action_flags.left_trigger then
p.action_flags.left_trigger = false
if not p._trigger_release.left_trigger then
p._trigger_release.left_trigger = true
p._apply_aligned = not p._apply_aligned
build_texture_icon(p)
end
else
p._trigger_release.left_trigger = false
end
-- t2: toggle apply transparent
if p.action_flags.right_trigger then
p.action_flags.right_trigger = false
if not p._trigger_release.right_trigger then
p._trigger_release.right_trigger = true
p._apply_transparent = not p._apply_transparent
build_texture_icon(p)
end
else
p._trigger_release.right_trigger = false
end
if p._apply_textures then
p.overlays[3].text = "apply tex"
else
p.overlays[3].text = "no textures"
end
if p._apply_lights then
p.overlays[2].text = "apply light"
else
p.overlays[2].text = "no lights"
end
if p._apply_aligned then
p.overlays[4].text = "aligned"
else
p.overlays[4].text = "not aligned"
end
if p._apply_transparent then
p.overlays[5].text = "transparent"
else
p.overlays[5].text = "solid"
end
end
else
if p.action_flags.microphone_button then
p.crosshairs.active = false
-- n: cycle texture collections forward
if p.action_flags.cycle_weapons_forward then
p.action_flags.cycle_weapons_forward = false
-- find the collection in walls
local index
for i, v in ipairs(walls) do
if v == p._collection.index then
index = i
break
end
end
local collection_index = walls[(index % #walls) + 1]
set_collection(p, Collections[collection_index])
end
-- p: cycle texture collections backward
if p.action_flags.cycle_weapons_backward then
p.action_flags.cycle_weapons_backward = false
local index
for i, v in ipairs(walls) do
if v == p._collection.index then
index = i
break
end
end
local collection_index = walls[((index - 2) % #walls) + 1]
set_collection(p, Collections[collection_index])
end
-- t1: increment texture
if p.action_flags.left_trigger then
p.action_flags.left_trigger = false
if not p._trigger_release.left_trigger then
p._trigger_release.left_trigger = true
p._increment_texture = Game.ticks
end
if Game.ticks == p._increment_texture
or Game.ticks > p._increment_texture + TRIGGER_DELAY
then
if p._collection.index == 0 then
p._texture = (p._texture + 1) % #landscape_palette
else
p._texture = (p._texture + 1) % p._collection.bitmap_count
end
end
else
p._trigger_release.left_trigger = false
end
-- t2: decrement texture
if p.action_flags.right_trigger then
p.action_flags.right_trigger = false
if not p._trigger_release.right_trigger then
p._trigger_release.right_trigger = true
p._decrement_texture = Game.ticks
end
if Game.ticks == p._decrement_texture
or Game.ticks > p._decrement_texture + TRIGGER_DELAY
then
if p._collection == 0 then
p._texture = (p._texture - 1) % #landscape_palette
else
p._texture = (p._texture - 1) % p._collection.bitmap_count
end
end
else
p._trigger_release.right_trigger = false
end
p.overlays[3].text = "collection+"
p.overlays[2].text = "collection-"
p.overlays[4].text = "texture+"
p.overlays[5].text = "texture-"
-- Toggle transparent side edit
else
p.crosshairs.active = true
if p.action_flags.left_trigger then
p.action_flags.left_trigger = false
if not p._trigger_release.left_trigger then
p._trigger_release.left_trigger = true
-- apply texture, and start dragging
local surface
local o, x, y, z, polygon = find_target(p, p._apply_transparent, false)
if is_side(o) then
o:recalculate_type()
surface = side_surface(o, z)
elseif is_polygon_floor(o) or is_polygon_ceiling(o) then
surface = o
elseif is_polygon(o) then
surface = o.floor
elseif is_line(o) then
-- we need to make a new side
surface = side_surface(new_side(polygon, o), z)
end
if surface then
if p._apply_textures then
local dragging = {}
if surface.texture_index == p._texture and surface.collection == p._collection then
dragging.x = surface.texture_x
dragging.y = surface.texture_y
else
dragging.x = 0
if is_side(o) then
local bottom, top = surface_heights(surface)
dragging.y = bottom - top
else
dragging.y = 0
end
end
dragging.yaw = p.yaw
dragging.pitch = p.pitch
dragging.surface = surface
dragging.start = Game.ticks
p._undo = {}
p._undo[surface] = build_undo(surface)
apply_texture(p, surface, dragging.x, dragging.y)
if is_transparent_side(surface) then
-- put the same texture on the opposite side of the line
local side = Sides[surface.index]
local line = side.line
if line.clockwise_side == side then
if line.counterclockwise_side then
dragging.opposite_surface = line.counterclockwise_side.transparent
elseif line.counterclockwise_polygon then
dragging.opposite_surface = new_side(line.counterclockwise_polygon, line).transparent
end
else
if line.clockwise_side then
dragging.opposite_surface = line.clockwise_side.transparent
elseif line.clockwise_polygon then
dragging.opposite_surface = new_side(line.clockwise_polygon, line).transparent
end
end
if dragging.opposite_surface then
p._undo[dragging.opposite_surface] = build_undo(dragging.opposite_surface)
apply_texture(p, dragging.opposite_surface, -dragging.x, dragging.y)
end
end
if p._apply_aligned then
if is_polygon_floor(surface) or is_polygon_ceiling(surface) then
dragging.align_table = build_polygon_align_table(polygon, surface)
if is_polygon_floor(surface) then
for s in pairs(dragging.align_table) do
if not p._undo[s.floor] then
p._undo[s.floor] = build_undo(s.floor)
end
end
else
for s in pairs(dragging.align_table) do
if not p._undo[s.ceiling] then
p._undo[s.ceiling] = build_undo(s.ceiling)
end
end
end
align_polygons(surface, dragging.align_table)
else
dragging.offsets = build_side_offsets_table(surface)
for s in pairs(dragging.offsets) do
if not p._undo[s] then
p._undo[s] = build_undo(s)
end
end
align_sides(surface, dragging.offsets)
if dragging.opposite_surface then
dragging.opposite_offsets = build_side_offsets_table(dragging.opposite_surface)
-- for s in pairs(dragging.offsets) do
-- dragging.opposite_offsets[s] = nil
-- end
for s in pairs(dragging.opposite_offsets) do
if not p._undo[s] then
p._undo[s] = build_undo(s)
end
end
align_sides(dragging.opposite_surface, dragging.opposite_offsets)
end
end
end
p._dragging = dragging
elseif p._apply_lights then
p._undo = {}
p._undo[surface] = build_undo(surface)
apply_texture(p, surface)
end
end
elseif p._dragging and Game.ticks > p._dragging.start + 3 then
if is_polygon_floor(p._dragging.surface) or is_polygon_ceiling(p._dragging.surface) then
-- pitch slides texture parallel to player's initial yaw
-- yaw slides texture perpendicular to player's initial yaw
-- this isn't great, but hopefully it's enough
local delta_pitch = p._dragging.pitch - p.pitch
if is_polygon_ceiling(p._dragging.surface) then
delta_pitch = -delta_pitch
end
local delta_yaw = p._dragging.yaw - p.yaw
local x = p._dragging.x - delta_yaw / 180 * math.sin(math.rad(p.yaw))
local y = p._dragging.y + delta_yaw / 180 * math.cos(math.rad(p.yaw))
x = quantize(p, x + delta_pitch / 60 * math.cos(math.rad(p.yaw)))
y = quantize(p, y + delta_pitch / 60 * math.sin(math.rad(p.yaw)))
apply_texture(p, p._dragging.surface, x, y)
if p._apply_aligned then
align_polygons(p._dragging.surface, p._dragging.align_table)
end
else
local delta_pitch = p._dragging.pitch - p.pitch
local delta_yaw = p._dragging.yaw - p.yaw