1- /*
1+ --[[
22
3- The Cinema location system is built from old
4- GMod Tower code. Credit goes to Voided for
5- the beautiful debugging system.
3+ Original debug system from the old
4+ GMod Tower code, credits go to Void.
65
7- */
6+ Now with Spatial grid visualization!
7+
8+ ]]
89
910module ( " Location" , package.seeall )
1011
12+ local math_Round = math .Round
13+ local math_floor = math.floor
14+ local string_format = string.format
15+ local pairs = pairs
16+ local ipairs = ipairs
17+ local LocalPlayer = LocalPlayer
18+ local Color = Color
19+
1120DebugEnabled = CreateClientConVar ( " cinema_debug_locations" , " 0" , false , false )
21+ DebugGridEnabled = CreateClientConVar ( " cinema_debug_grid" , " 0" , false , false )
1222
1323DebugLocStart = nil
1424DebugLocEnd = nil
1525
16- // we use this so that the bottom of a box will be lower than the player ' s position
26+ -- we use this so that the bottom of a box will be lower than the player's position
1727FootOffset = Vector ( 0 , 0 , - 5 )
1828
19-
20- /*
29+ --[[
2130 Location editing utilities
2231
2332 These two concommands are designed to simplify location creation.
2433 Simply run cinema_loc_start, move to create a desired box, then run cinema_loc_end and grab the lua printed to the console.
2534 This requires you to be an admin!
26- */
35+ ]]
2736
2837concommand .Add ( " cinema_loc_start" , function ( ply , cmd , args )
2938 if not ply :IsAdmin () then return end
@@ -68,16 +77,97 @@ end )
6877concommand .Add ( " cinema_loc_vector" , function ( ply , cmd , args )
6978 if not ply :IsAdmin () then return end
7079 local pos = LocalPlayer ():GetPos ()
71- local posstr = " Vector( " .. math . Round (pos .x ) .. " , " .. math . Round (pos .y ) .. " , " .. math . Round (pos .z ) .. " ),"
80+ local posstr = " Vector( " .. math_Round (pos .x ) .. " , " .. math_Round (pos .y ) .. " , " .. math_Round (pos .z ) .. " ),"
7281 SetClipboardText ( posstr )
7382 MsgN ( posstr )
7483 MsgN ( " The above position has been copied to your clipboard." )
7584end )
7685
77- // location visualizer for debugging
86+ -- Spatial grid visualization function
87+ local function DrawSpatialGrid ()
88+ if not DebugGridEnabled :GetBool () then return end
89+
90+ local spatialGrid , gridSize = GetSpatialGrid ()
91+ if not spatialGrid or not gridSize then return end
92+
93+ local playerPos = LocalPlayer ():GetPos ()
94+ local playerGridX = math_floor (playerPos .x / gridSize )
95+ local playerGridY = math_floor (playerPos .y / gridSize )
96+
97+ -- Draw grid cells around player
98+ local renderDistance = 3 -- Show 3x3 grid around player
99+
100+ for x = playerGridX - renderDistance , playerGridX + renderDistance do
101+ for y = playerGridY - renderDistance , playerGridY + renderDistance do
102+ local key = x .. " ," .. y
103+ local candidates = spatialGrid [key ]
104+
105+ if candidates and # candidates > 0 then
106+ -- Calculate grid cell bounds
107+ local minX , minY = x * gridSize , y * gridSize
108+ local maxX , maxY = (x + 1 ) * gridSize , (y + 1 ) * gridSize
109+
110+ -- Use different colors based on location count in cell
111+ local cellColor = Color (0 , 255 , 0 , 50 ) -- Green for populated cells
112+ if # candidates > 3 then
113+ cellColor = Color (255 , 255 , 0 , 50 ) -- Yellow for busy cells
114+ elseif # candidates > 6 then
115+ cellColor = Color (255 , 0 , 0 , 50 ) -- Red for very busy cells
116+ end
117+
118+ -- Draw grid cell outline
119+ local cellMin = Vector (minX , minY , playerPos .z - 100 )
120+ local cellMax = Vector (maxX , maxY , playerPos .z + 100 )
121+
122+ Debug3D .DrawBox (cellMin , cellMax , cellColor )
123+
124+ -- Draw cell info
125+ local cellCenter = Vector ((minX + maxX ) / 2 , (minY + maxY ) / 2 , playerPos .z + 50 )
126+ local cellText = string_format (" Grid [%d,%d]\n %d locations" , x , y , # candidates )
127+ Debug3D .DrawText (cellCenter , cellText , " VideoInfoSmall" , Color (255 , 255 , 255 , 255 ), 0.5 )
128+
129+ -- Draw location names in cell
130+ for i , candidate in ipairs (candidates ) do
131+ if i <= 3 then -- Limit to first 3 to avoid clutter
132+ local textPos = Vector (cellCenter .x , cellCenter .y , cellCenter .z - (i * 20 ))
133+ Debug3D .DrawText (textPos , candidate .name , " VideoInfoSmall" , Color (200 , 200 , 255 , 255 ), 0.3 )
134+ end
135+ end
136+ else
137+ -- Draw empty grid cells with different color
138+ local minX , minY = x * gridSize , y * gridSize
139+ local maxX , maxY = (x + 1 ) * gridSize , (y + 1 ) * gridSize
140+
141+ local cellMin = Vector (minX , minY , playerPos .z - 50 )
142+ local cellMax = Vector (maxX , maxY , playerPos .z + 50 )
143+
144+ Debug3D .DrawBox (cellMin , cellMax , Color (100 , 100 , 100 , 20 )) -- Gray for empty cells
145+ end
146+ end
147+ end
148+
149+ -- Draw player's current grid cell highlight
150+ local playerKey = playerGridX .. " ," .. playerGridY
151+ local playerMinX , playerMinY = playerGridX * gridSize , playerGridY * gridSize
152+ local playerMaxX , playerMaxY = (playerGridX + 1 ) * gridSize , (playerGridY + 1 ) * gridSize
153+
154+ local playerCellMin = Vector (playerMinX , playerMinY , playerPos .z - 150 )
155+ local playerCellMax = Vector (playerMaxX , playerMaxY , playerPos .z + 150 )
156+
157+ Debug3D .DrawBox (playerCellMin , playerCellMax , Color (255 , 255 , 255 , 100 )) -- White highlight for player cell
158+ end
159+
160+ -- location visualizer for debugging
78161hook .Add ( " PostDrawTranslucentRenderables" , " CinemaDebugLocations" , function ()
79162
80- if ( not DebugEnabled :GetBool () ) then return end
163+ if ( not DebugEnabled :GetBool () ) then
164+ -- Still check for grid rendering even if locations are disabled
165+ DrawSpatialGrid ()
166+ return
167+ end
168+
169+ -- Draw spatial grid if enabled
170+ DrawSpatialGrid ()
81171
82172 for k , v in pairs ( GetLocations () or {} ) do
83173
@@ -102,4 +192,4 @@ hook.Add( "PostDrawTranslucentRenderables", "CinemaDebugLocations", function ()
102192 end
103193 end
104194
105- end )
195+ end )
0 commit comments