Skip to content

Commit ce5fe4e

Browse files
committed
doom lua: add and display line and sector
1 parent 9f9c17c commit ce5fe4e

File tree

1 file changed

+138
-63
lines changed

1 file changed

+138
-63
lines changed

Assets/Lua/Doom/doom.lua

Lines changed: 138 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ local CHAR_HEIGHT <const> = 16
2121
local PADDING_WIDTH <const> = 240
2222
local MAP_CLICK_BLOCK <const> = "P1 Fire" -- prevent this input while clicking on map buttons
2323

24+
local TrackedType = {
25+
THING = 0,
26+
LINE = 1,
27+
SECTOR = 2
28+
}
29+
2430
-- Map colors (0xAARRGGBB or "name")
2531
local MapPrefs = {
2632
player = { color = 0xff60d0ff, radius_min_zoom = 0.00, text_min_zoom = 0.20, },
@@ -51,14 +57,17 @@ local MobjFlags = enums.mobjflags
5157

5258
-- TOP LEVEL VARIABLES
5359

54-
local Zoom = 1
55-
local Follow = false
56-
local Hilite = false
57-
local Init = true
58-
local Players = {}
60+
local Zoom = 1
61+
local Follow = false
62+
local Hilite = false
63+
local Init = true
64+
local LastFramecount = -1
65+
local ScreenWidth = client.screenwidth()
66+
local ScreenHeight = client.screenheight()
5967

6068
-- tables
6169

70+
local Players = {}
6271
-- view offset
6372
local Pan = {
6473
x = 0,
@@ -81,31 +90,40 @@ local LastMouse = {
8190
wheel = 0,
8291
left = false
8392
}
84-
85-
local TrackedThings = {}
86-
local TrackedLines = {}
87-
local TrackedSectors = {}
88-
local ThingIDs = {}
89-
local LineIDs = {}
90-
local SectorIDs = {}
91-
local LastFramecount = -1
92-
local ScreenWidth = client.screenwidth()
93-
local ScreenHeight = client.screenheight()
93+
local Tracked = {
94+
[TrackedType.THING] = {
95+
TrackedList = {},
96+
IDs = {},
97+
Current = -1,
98+
Name = "thing"
99+
},
100+
[TrackedType.LINE] = {
101+
TrackedList = {},
102+
IDs = {},
103+
Current = -1,
104+
Name = "line"
105+
},
106+
[TrackedType.SECTOR] = {
107+
TrackedList = {},
108+
IDs = {},
109+
Current = -1,
110+
Name = "sector"
111+
}
112+
}
94113

95114
-- forward declarations
96115

97116
local Input
98-
local LastEpisode
99-
local LastMap
100-
local LastInput
101-
102117
local Lines
103118
local PlayerTypes
104119
local EnemyTypes
105120
local MissileTypes
106121
local MiscTypes
107122
local InertTypes
108123
local CurrentPrompt
124+
local LastEpisode
125+
local LastMap
126+
local LastInput
109127

110128
--gui.defaultPixelFont("fceux")
111129
gui.use_surface("client")
@@ -392,7 +410,7 @@ local function init_cache()
392410
-- selectively cache certain properties. by assigning them manually the read function won't be called again
393411

394412
local lineId = line.iLineID
395-
LineIDs[lineId] = true
413+
Tracked[TrackedType.LINE].IDs[lineId] = true
396414

397415
-- assumption: lines can't become special, except for script command CmdSetLineSpecial
398416
-- exclude lines that have a line id set (and therefore can be targeted by scripts)
@@ -413,7 +431,7 @@ local function init_cache()
413431
end
414432

415433
for _, sector in pairs(Globals.sectors) do
416-
SectorIDs[sector.iSectorID] = true
434+
Tracked[TrackedType.SECTOR].IDs[sector.iSectorID] = true
417435
end
418436
end
419437

@@ -520,17 +538,58 @@ local function iterate()
520538
player.angle
521539
)
522540

541+
for _, sector in pairs(Globals.sectors) do
542+
local index = sector.iSectorID
543+
local entity = Tracked[TrackedType.SECTOR]
544+
local list = entity.TrackedList
545+
546+
if #list > 0 then
547+
local id = list[entity.Current]
548+
549+
if id == index then
550+
texts.sector = string.format(
551+
"SECTOR %d spec: %d\nflo: %.2f ceil: %.2f",
552+
index,
553+
sector.special,
554+
sector.floorheight / FRACUNIT,
555+
sector.ceilingheight / FRACUNIT)
556+
end
557+
end
558+
end
559+
523560
for i, line in ipairs(Lines) do
524-
local color = 0xffffffff
525561
local x1, y1, x2, y2 = game_to_screen(cached_line_coords(line))
562+
local color = 0xffffffff
526563
local special = line.special
564+
local index = line.iLineID
565+
local entity = Tracked[TrackedType.LINE]
566+
local list = entity.TrackedList
527567

528568
if special ~= 0 then color = 0xffff00ff end
529569

530570
drawline(x1, y1, x2, y2, color) -- no speedup from doing range check
571+
x1, y1, x2, y2 = cached_line_coords(line)
572+
573+
if #list > 0 then
574+
local id = list[entity.Current]
575+
local distance = distance_from_line(
576+
{ x = player.x, y = player.y },
577+
{ x = x1 / FRACUNIT, y = y1 / FRACUNIT },
578+
{ x = x2 / FRACUNIT, y = y2 / FRACUNIT }
579+
)
580+
581+
if id == index then
582+
texts.line = string.format(
583+
"LINEDEF %d dist: %.0f\nv1 x: %5d y: %5d\nv2 x: %5d y: %5d",
584+
index, distance,
585+
math.floor(x1 / FRACUNIT),
586+
math.floor(y1 / FRACUNIT),
587+
math.floor(x2 / FRACUNIT),
588+
math.floor(y2 / FRACUNIT))
589+
end
590+
end
531591

532592
if Hilite then
533-
x1, y1, x2, y2 = cached_line_coords(line)
534593

535594
local dist = distance_from_line(
536595
gameMousePos,
@@ -567,7 +626,7 @@ local function iterate()
567626
-- cached_line_coords gives some length error?
568627
local x1, y1, x2, y2 = game_to_screen(line:coords())
569628
drawline(x1, y1, x2, y2, 0xff00ffff)
570-
texts.sector = string.format(
629+
texts.sector = texts.sector or string.format(
571630
"SECTOR %d spec: %d\nflo: %.2f ceil: %.2f",
572631
selected_sector.iSectorID,
573632
selected_sector.special,
@@ -585,8 +644,8 @@ local function iterate()
585644
)
586645

587646
x1, y1, x2, y2 = game_to_screen(x1, y1, x2, y2)
588-
drawline(x1, y1, x2, y2, 0xffff8800)
589-
texts.line = string.format(
647+
drawline(x1, y1, x2, y2, 0xffff8800)
648+
texts.line = texts.line or string.format(
590649
"LINEDEF %d dist: %.0f\nv1 x: %5d y: %5d\nv2 x: %5d y: %5d",
591650
closest_line.iLineID, distance,
592651
math.floor(closest_line.v1.x / FRACUNIT),
@@ -597,16 +656,19 @@ local function iterate()
597656
end
598657

599658
for _, mobj in pairs(Globals.mobjs:readbulk()) do
600-
local type = mobj.type
601-
local index = mobj.index
659+
local entity = Tracked[TrackedType.THING]
660+
local list = entity.TrackedList
661+
local type = mobj.type
662+
local index = mobj.index
602663
local radius_color, text_color = get_mobj_color(mobj, type)
603664

665+
-- players have index -1, things to be removed have -2
604666
if index >= 0 then
605-
ThingIDs[index] = true
667+
entity.IDs[index] = true
606668
end
607-
608-
if #TrackedThings > 0 then
609-
local id = TrackedThings[#TrackedThings]
669+
670+
if #list > 0 then
671+
local id = list[entity.Current]
610672

611673
if id == index then
612674
texts.thing = string.format(
@@ -676,16 +738,14 @@ local function iterate()
676738
end
677739
end
678740

679-
box(0, 0, PADDING_WIDTH, ScreenHeight, 0xb0000000, 0xb0000000)
741+
box ( 0, 0, PADDING_WIDTH, ScreenHeight, 0xb0000000, 0xb0000000)
680742
text(10, 42, texts.player, MapPrefs.player.color)
681743

682744
if texts.thing then text(10, 222, texts.thing ) end
683745
if texts.line then text(10, 320, texts.line, 0xffff8800) end
684746
if texts.sector then text(10, 370, texts.sector, 0xff00ffff) end
685747

686748
texts.thing = nil
687-
688-
-- text(50,10,shortest_dist/FRACUNIT)
689749
end
690750

691751
local function init_mobj_bounds()
@@ -772,10 +832,16 @@ end
772832

773833
local function clear_cache()
774834
Lines = nil
775-
ThingIDs = {}
776-
LineIDs = {}
777-
SectorIDs = {}
778835
reset_view()
836+
Tracked[TrackedType.THING ].TrackedList = {}
837+
Tracked[TrackedType.THING ].IDs = {}
838+
Tracked[TrackedType.THING ].Current = -1
839+
Tracked[TrackedType.LINE ].TrackedList = {}
840+
Tracked[TrackedType.LINE ].IDs = {}
841+
Tracked[TrackedType.LINE ].Current = -1
842+
Tracked[TrackedType.SECTOR].TrackedList = {}
843+
Tracked[TrackedType.SECTOR].IDs = {}
844+
Tracked[TrackedType.SECTOR].Current = -1
779845
end
780846

781847
local function get_line_count(str)
@@ -903,45 +969,54 @@ end
903969
local function add_entity(type)
904970
if CurrentPrompt then return end
905971

906-
local lookup, array
907-
908-
if type == "thing" then
909-
lookup = ThingIDs
910-
array = TrackedThings
911-
elseif type == "line" then
912-
lookup = LineIDs
913-
array = TrackedLines
914-
elseif type == "sector" then
915-
lookup = SectorIDs
916-
array = TrackedSectors
917-
else print("ERROR: Wrong entity type: " .. type) return
918-
end
972+
local entity = Tracked[type]
973+
local lookup = entity.IDs
974+
local array = entity.TrackedList
975+
local name = entity.Name
919976

920977
CurrentPrompt = {
921978
msg = type,
922979
fun = function(id)
923980
if not lookup[id] then
924-
print(string.format("\nERROR: Can't add %s %d because it doesn't exist!\n", type, id))
981+
print(string.format(
982+
"\nERROR: Can't add %s %d because it doesn't exist!\n", name, id
983+
))
925984
return
926985
end
986+
987+
--[[
988+
we either look for items longer with big tracked lists, or we add by index
989+
and potentially waste memory if there are thousands of entities in a map
990+
because gaps will also be there. since people are unlikely to track hundreds
991+
of items, relying on traversing the whole list every time is probably fine.
992+
--]]
993+
for i = 1, #array do
994+
if array[i] == id then
995+
print(string.format(
996+
"\nERROR: Can't add %s %d because it's already there!\n", name, id
997+
))
998+
return
999+
end
1000+
end
9271001
table.insert(array, id)
928-
print(string.format("Added %s %d", type, id))
1002+
Tracked[type].Current = #array
1003+
print(string.format("Added %s %d", name, id))
9291004
end,
9301005
value = nil
9311006
}
9321007
end
9331008

9341009
local function make_buttons()
935-
make_button(-115, 30, "Add Sector", function() add_entity("sector") end)
936-
make_button(-210, 30, "Add Line", function() add_entity("line" ) end)
937-
make_button(-315, 30, "Add Thing", function() add_entity("thing" ) end)
938-
make_button( 10, -40, "+", function() zoom ( 1 ) end)
939-
make_button( 10, -10, "-", function() zoom (-1 ) end)
940-
make_button( 40, -24, "<", pan_left )
941-
make_button( 64, -40, "^", pan_up )
942-
make_button( 64, -10, "v", pan_down )
943-
make_button( 88, -24, ">", pan_right )
944-
make_button( 118, -40, "Reset View", reset_view )
1010+
make_button(-115, 30, "Add Sector", function() add_entity(TrackedType.SECTOR) end)
1011+
make_button(-210, 30, "Add Line", function() add_entity(TrackedType.LINE ) end)
1012+
make_button(-315, 30, "Add Thing", function() add_entity(TrackedType.THING ) end)
1013+
make_button( 10, -40, "+", function() zoom ( 1 ) end)
1014+
make_button( 10, -10, "-", function() zoom (-1 ) end)
1015+
make_button( 40, -24, "<", pan_left )
1016+
make_button( 64, -40, "^", pan_up )
1017+
make_button( 64, -10, "v", pan_down )
1018+
make_button( 88, -24, ">", pan_right )
1019+
make_button( 118, -40, "Reset View", reset_view)
9451020
make_button( 118, -10,
9461021
string.format("Follow %s", Follow and "ON " or "OFF"), follow_toggle)
9471022
make_button(-460, 30,

0 commit comments

Comments
 (0)