-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathradar.lua
155 lines (120 loc) · 3.54 KB
/
radar.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
local tiles = { }
local timer
local enabled = true
local ROW_COUNT = 12
function toggleCustomTiles ( )
-- Toggle!
disabled = not enabled
-- Check whether we enabled it
if enabled then
-- Load all tiles
handleTileLoading ( )
-- Set a timer to check whether new tiles should be loaded (less resource hungry than doing it on render)
timer = setTimer ( handleTileLoading, 250, 0 )
else
-- If our timer is still running, kill it with fire
if isTimer ( timer ) then killTimer ( timer ) end
-- Unload all tiles, so the memory footprint has disappeared magically
for name, data in pairs ( tiles ) do
unloadTile ( name )
end
end
end
function handleTileLoading ( )
-- Get all visible radar textures
local visibleTileNames = table.merge ( engineGetVisibleTextureNames ( "radar??" ), engineGetVisibleTextureNames ( "radar???" ) )
-- Unload tiles we don't see
for name, data in pairs ( tiles ) do
if not table.find ( visibleTileNames, name ) then
unloadTile ( name )
end
end
-- Load tiles we do see
for index, name in ipairs ( visibleTileNames ) do
loadTile ( name )
end
end
function table.merge ( ... )
local ret = { }
for index, tbl in ipairs ( {...} ) do
for index, val in ipairs ( tbl ) do
table.insert ( ret, val )
end
end
return ret
end
function table.find ( tbl, val )
for index, value in ipairs ( tbl ) do
if value == val then
return index
end
end
return false
end
-------------------------------------------
--
-- Tile loading and unloading functions
--
-------------------------------------------
function loadTile ( name )
-- Make sure we have a string
if type ( name ) ~= "string" then
return false
end
-- Check whether we already loaded this tile
if tiles[name] then
return true
end
-- Extract the ID
local id = tonumber ( name:match ( "%d+" ) )
-- If not a valid ID, abort
if not id then
return false
end
-- Calculate row and column
local row = math.floor ( id / ROW_COUNT )
local col = id - ( row * ROW_COUNT )
-- Now just calculate start and end positions
local posX = -3000 + 500 * col
local posY = 3000 - 500 * row
-- Fetch the filename
local file = string.format ( "sattelite/sattelite_%d_%d.jpeg", row, col )
-- Now, load that damn file! (Also create a transparent overlay texture)
local texture = dxCreateTexture ( file )
-- If it failed to load, abort
if not texture --[[or not overlay]] then
outputChatBox ( string.format ( "Failed to load texture for %q (%q)", tostring ( name ), tostring ( file ) ) )
return false
end
-- Now we just need the shader
local shader = dxCreateShader ( "fx/texturereplace.fx" )
-- Abort if failed (don't forget to destroy the texture though!!!)
if not shader then
outputChatBox ( "Failed to load shader" )
destroyElement ( texture )
return false
end
-- Now hand the texture to the shader
dxSetShaderValue ( shader, "gTexture", texture )
-- Now apply this stuff on the tile
engineApplyShaderToWorldTexture ( shader, name )
-- Store the stuff
tiles[name] = { shader = shader, texture = texture }
-- Return success
return true
end
function unloadTile ( name )
-- Get the tile data
local tile = tiles[name]
-- If no data is present, we failed
if not tile then
return false
end
-- Destroy the shader and texture elements, if they exist
if isElement ( tile.shader ) then destroyElement ( tile.shader ) end
if isElement ( tile.texture ) then destroyElement ( tile.texture ) end
-- Now remove all reference to it
tiles[name] = nil
-- We succeeded
return true
end