This repository was archived by the owner on Oct 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
233 lines (131 loc) · 5.43 KB
/
main.lua
File metadata and controls
233 lines (131 loc) · 5.43 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
function Initialize(Plugin)
Plugin:SetName("StaticMap")
Plugin:SetVersion(1)
-- Initialize GlobalVars
ChunksPerRender = 1
TicksPerRender = 20
GlobalTick = 0
Chunks = {}
ColorTable = InitialiseColorTable()
Sep = cFile:GetPathSeparator()
Directory = "Plugins" .. Sep .. Plugin:GetFolderName() .. Sep
LOGINFO(" - Static Map Loading - ")
-- Find the specific operating system
local BinaryFormat = package.cpath:match("%p[\\|/]?%p(%a+)")
if BinaryFormat == "dll" then
OS = "Windows"
elseif BinaryFormat == "so" then
OS = "Linux"
elseif BinaryFormat == "dylib" then
OS = "MacOS"
end
BinaryFormat = nil
if OS == "Linux" then -- Find linux Architecture
n = os.tmpname()
os.execute("uname -a > " .. n)
for line in io.lines(n) do
if string.match(line, "x86_64") then
ARCH = "x86_64"
elseif string.match(line, "aarch64") then
ARCH = "aarch64"
end
end
os.remove(n)
end
LOG("Detected Operating System: " .. OS)
if OS == "Linux" then
LOG("Detected Arch: " .. ARCH)
end
-- Initialise Hooks
cPluginManager:AddHook(cPluginManager.HOOK_CHUNK_GENERATED, OnChunkGenerated);
cPluginManager:AddHook(cPluginManager.HOOK_TICK, OnTick);
cPluginManager:AddHook(cPluginManager.HOOK_WORLD_STARTED, OnWorldStarted);
LOGINFO(" - Static Map Loaded - ")
return true
end
function OnChunkGenerated(World, ChunkX, ChunkZ, ChunkDesc)
--[[
The chunk generated event is where heightmaps are generated and scheduled to be processed.
]]
local FileName = "Chunk" .. ChunkX .. "." .. ChunkZ
if not(cFile:IsFile("..\\..\\webadmin\\files\\images" .. Sep .. FileName)) then -- If file doesn't exist, start generating heightmap
local BlockMap = {}
for x = 1, 16 do
BlockMap[x] = {}
for y = 1, 16 do
BlockMap[x][y] = tonumber(ChunkDesc:GetBlockType(x, ChunkDesc:GetHeight(x, y), y))
end
end
table.insert(Chunks, {FileName, BlockMap}) -- Schedule Heightmap to be processed
end
end
function GenerateChunkImage(FileName, BlockMap)
--[[
This function first processes heightmaps into their colored ppm files
Then it uses image magick to convert the ppm file into a png and place it jnto the correct directory
]]
-- Start Heightmap Processing
local Temp = ""
local lines = {}
local line = {}
local out = {}
-- Iterate over every value and assign a color to the ppm file
for x = 1, 16 do
for y = 1, 16 do
local color = "000 000 000 "
for Key = 1, #ColorTable do -- Find and assign color
if ColorTable[Key][1] == BlockMap[x][y] then
color = ColorTable[Key][2]
end
end
Temp = Temp .. color -- Add color to file
if y % 4 == 0 then -- Add a line break every 4 color values
table.insert(lines, Temp)
Temp = "\n"
end
end
out[x] = table.concat(lines, "")
lines = {}
end
local Blocks = table.concat(out, '\n') -- Compile ppm file
-- Ensure folders exist
cFile:CreateFolder(Directory .. ".." .. Sep .. ".." .. Sep .. "webadmin/files/images")
cFile:CreateFolder(Directory .. "Images" .. Sep)
-- Write PPM metadata + RGB Values
local Image = io.open(Directory .. "Images" .. Sep .. "img.ppm", "w")
Image:write("P3\n" .. 16 .. " " .. 16 .. "\n255\n" .. Blocks)
Image:close()
local Command = ""
if OS == "Windows" then
for Key, Value in pairs(Chunks) do -- Get Windows Command
if Value[1] == FileName then
Command = Directory .. "Magick\\Windows\\convert " .. Directory .. "Images\\img.ppm " .. Directory .. "..\\..\\webadmin\\files\\images" .. Sep .. FileName .. ".png \n"
end
end
elseif OS == "Linux" then
for Key, Value in pairs(Chunks) do -- Get Linux Command
if Value[1] == FileName then
if ARCH == "x86_64" then
Command = Directory .. "Magick/linuxmagick convert " .. Directory .. "Images/img.ppm " .. Directory .. "../../webadmin/files/images" .. Sep .. FileName .. ".png \n"
elseif ARCH == "aarch64" then
Command = Directory .. "Magick/linuxmagick_aarch64 convert " .. Directory .. "Images/img.ppm " .. Directory .. "../../webadmin/files/images" .. Sep .. FileName .. ".png \n"
end
end
end
end
-- TODO: MacOS
os.execute(Command)
end
function OnTick(Delta)
if (GlobalTick % TicksPerRender == 0) and (#Chunks > 0.5) then -- If A render is currently allowed and more then one is scheduled
local Temp = 0
for Key, Value in pairs(Chunks) do -- Renders Per Tick Chunks
if Temp < ChunksPerRender then
GenerateChunkImage(Chunks[Key][1], Chunks[Key][2])
Temp = Temp + 1
Chunks[Key] = nil
end
end
end
GlobalTick = GlobalTick + 1
end