forked from voxelearth/luanti-earth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
383 lines (310 loc) · 14.1 KB
/
init.lua
File metadata and controls
383 lines (310 loc) · 14.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
-- Luanti Earth Mod
-- Loads voxelized Google Earth 3D tiles
--------------------------------------------------
-- Mod name / paths
--------------------------------------------------
local modname = minetest.get_current_modname()
local modpath = minetest.get_modpath(modname)
local worldpath = minetest.get_worldpath()
-- All cache / downloaded data goes into the world folder,
-- not into mods/, to satisfy Luanti security.
local cache_root = worldpath .. "/luanti_earth_cache"
-- Ensure root cache dir exists
minetest.mkdir(cache_root)
-- Seed RNG once so /visit spawn positions are different each time
math.randomseed(os.time())
-- Keep random within a safe chunk of the map (Minetest map limit is ~±30927)
local RANDOM_SPAWN_RANGE = 20000
--------------------------------------------------
-- HTTP API (needs secure.http_mods = luanti_earth)
--------------------------------------------------
local http = minetest.request_http_api and minetest.request_http_api()
if not http then
minetest.log("warning",
"[" .. modname .. "] HTTP API unavailable. " ..
"Is " .. modname .. " in secure.http_mods?")
end
--------------------------------------------------
-- Insecure environment for external commands
-- (needs secure.trusted_mods = luanti_earth)
--------------------------------------------------
local insecure_env = minetest.request_insecure_environment
and minetest.request_insecure_environment()
local os_execute = insecure_env and insecure_env.os
and insecure_env.os.execute
if not os_execute then
minetest.log("warning",
"[" .. modname .. "] No insecure os.execute available. " ..
"Add " .. modname .. " to secure.trusted_mods to enable Node.js calls.")
end
--------------------------------------------------
-- Load voxel importer
--------------------------------------------------
local voxel_importer = dofile(modpath .. "/voxel_importer.lua")
luanti_earth = {
voxel_importer = voxel_importer,
path = modpath,
use_pure_colors = true -- Default to true for pretty custom blocks
}
minetest.log("action", "[luanti_earth] Voxel-based mod loaded")
--------------------------------------------------
-- Load colors and register pure color nodes
--------------------------------------------------
local colors = dofile(modpath .. "/colors.lua")
for i = 0, 255 do
local hex = colors[tostring(i)]
if hex then
minetest.register_node("luanti_earth:color_" .. i, {
description = "Pure Color " .. i .. " (" .. hex .. ")",
tiles = {"default_stone.png^[colorize:" .. hex .. ":255"},
groups = {cracky = 3, oddly_breakable_by_hand = 3},
is_ground_content = false,
})
end
end
--------------------------------------------------
-- Progress bar helper
--------------------------------------------------
local function make_progress_bar(pct, width)
width = width or 20
if pct < 0 then pct = 0 end
if pct > 100 then pct = 100 end
local filled = math.floor((pct / 100) * width + 0.5)
local bar = string.rep("#", filled) .. string.rep(".", width - filled)
return "[" .. bar .. "] " .. pct .. "%"
end
local function send_progress(name, pct, stage)
minetest.chat_send_player(name, make_progress_bar(pct) .. " " .. stage)
end
--------------------------------------------------
-- Chat command to toggle pure color mode
--------------------------------------------------
minetest.register_chatcommand("earth_use_pure_colors", {
params = "<true/false>",
description = "Toggle pure color mode (prioritizes solid colored blocks)",
privs = {server = true},
func = function(name, param)
if param == "true" then
luanti_earth.use_pure_colors = true
minetest.chat_send_player(name, "Pure color mode ENABLED. Future imports will prioritize solid colored blocks.")
elseif param == "false" then
luanti_earth.use_pure_colors = false
minetest.chat_send_player(name, "Pure color mode DISABLED. Future imports will use natural blocks.")
else
return false, "Usage: /earth_use_pure_colors <true/false>"
end
return true
end
})
--------------------------------------------------
-- API key storage & commands
--------------------------------------------------
local storage = minetest.get_mod_storage()
minetest.register_chatcommand("earth_apikey", {
params = "<key>",
description = "Set Google API Key for 3D Tiles",
privs = {server = true},
func = function(name, param)
if not param or param == "" then
return false, "Usage: /earth_apikey <key>"
end
storage:set_string("google_api_key", param)
return true, "API Key saved."
end
})
--------------------------------------------------
-- Helper to get safe filename
--------------------------------------------------
local function get_safe_filename(str)
return str:gsub("[^%w%-_]", "_")
end
--------------------------------------------------
-- /visit command: geocode + download + voxelize + import
--------------------------------------------------
minetest.register_chatcommand("visit", {
params = "<location>",
description = "Teleport to a real-world location (e.g. /visit Paris)",
privs = {server = true, teleport = true},
func = function(name, param)
if not param or param == "" then
return false, "Usage: /visit <location>"
end
local api_key = storage:get_string("google_api_key")
if not api_key or api_key == "" then
return false, "API Key not set. Use /earth_apikey <key> first."
end
local player = minetest.get_player_by_name(name)
if not player then
return false, "Player not found"
end
if not http then
return false,
"HTTP API unavailable. Add luanti_earth to secure.http_mods and restart the server."
end
minetest.chat_send_player(name, "Geocoding '" .. param .. "'...")
send_progress(name, 5, "Geocoding location...")
-- 1. Geocode
local url = "https://maps.googleapis.com/maps/api/geocode/json?address=" ..
minetest.urlencode(param) .. "&key=" .. api_key
http.fetch({url = url, timeout = 10}, function(res)
if not res.succeeded then
minetest.chat_send_player(name, "Geocoding failed: Request failed")
send_progress(name, 0, "Geocoding failed")
return
end
local data = minetest.parse_json(res.data)
if not data or not data.results or #data.results == 0 then
minetest.chat_send_player(name, "Geocoding failed: Location not found")
send_progress(name, 0, "Location not found")
return
end
local loc = data.results[1].geometry.location
local lat, lng = loc.lat, loc.lng
minetest.chat_send_player(name, "Found: " .. lat .. ", " .. lng)
send_progress(name, 10, "Location resolved")
--------------------------------------------------
-- 2. Prepare directories (in world folder)
--------------------------------------------------
local location_name = get_safe_filename(param)
local cache_dir = cache_root .. "/" .. location_name
local glb_dir = cache_dir .. "/glb"
local json_dir = cache_dir .. "/json"
-- Use Luanti's mkdir (recursive) – safe in world dir
minetest.mkdir(cache_dir)
minetest.mkdir(glb_dir)
minetest.mkdir(json_dir)
--------------------------------------------------
-- Ensure we have permission to run external commands
--------------------------------------------------
if not os_execute then
minetest.chat_send_player(name,
"Server not configured to allow external commands.\n" ..
"Add luanti_earth to secure.trusted_mods and restart.")
send_progress(name, 0, "Missing insecure environment")
return
end
--------------------------------------------------
-- 3. Download Tiles (Node.js)
--------------------------------------------------
minetest.chat_send_player(name, "Downloading 3D Tiles... (this can take a bit)")
send_progress(name, 25, "Downloading 3D Tiles...")
local node_cmd_dl = string.format(
'node "%s/tile_downloader.js" --key "%s" --lat %f --lng %f --radius 200 --out "%s"',
modpath, api_key, lat, lng, glb_dir
)
local ret_dl = os_execute(node_cmd_dl)
if ret_dl ~= 0 and ret_dl ~= true then
minetest.chat_send_player(name,
"Download failed. Make sure you ran 'npm install' in the Voxel Earth mod folder.")
send_progress(name, 0, "Download failed")
return
end
send_progress(name, 50, "Download complete")
--------------------------------------------------
-- 4. Voxelize (Node.js)
--------------------------------------------------
minetest.chat_send_player(name, "Voxelizing tiles...")
send_progress(name, 60, "Voxelizing tiles...")
local node_cmd_vox = string.format(
'node "%s/voxelize_tiles.js" "%s" "%s" 100',
modpath, glb_dir, json_dir
)
local ret_vox = os_execute(node_cmd_vox)
if ret_vox ~= 0 and ret_vox ~= true then
minetest.chat_send_player(name,
"Voxelization failed.")
send_progress(name, 0, "Voxelization failed")
return
end
send_progress(name, 80, "Voxelization complete")
--------------------------------------------------
-- 5. Import into the world
--------------------------------------------------
minetest.chat_send_player(name, "Importing voxels into the world...")
send_progress(name, 90, "Importing voxels...")
-- Pick a random location **far away** to avoid overlapping visits
local spawn_pos = {
x = math.random(-RANDOM_SPAWN_RANGE, RANDOM_SPAWN_RANGE),
y = 50,
z = math.random(-RANDOM_SPAWN_RANGE, RANDOM_SPAWN_RANGE),
}
-- Import from JSON dir; true = use_color / pure mode auto-detect
local count = voxel_importer.import_from_directory(json_dir, spawn_pos, true)
minetest.chat_send_player(name, "Imported " .. count .. " blocks at (" ..
spawn_pos.x .. ", " .. spawn_pos.y .. ", " .. spawn_pos.z .. ").")
send_progress(name, 100, "Done")
-- Teleport player slightly above the structure
player:set_pos({x = spawn_pos.x, y = spawn_pos.y + 20, z = spawn_pos.z})
minetest.chat_send_player(name, "Teleported to " .. param)
end)
end
})
--------------------------------------------------
-- /earth_load_voxels: load voxel JSON at player pos
--------------------------------------------------
minetest.register_chatcommand("earth_load_voxels", {
params = "<voxel_json_path>",
description = "Load and place voxelized tile data from a JSON file",
privs = {server = true},
func = function(name, param)
if param == "" then
return false, "Usage: /earth_load_voxels <path_to_voxel_json>"
end
minetest.chat_send_player(name, "Loading voxel data from: " .. param)
send_progress(name, 10, "Loading voxel JSON...")
-- Load voxel data
local voxel_data, err = voxel_importer.load_voxel_file(param)
if not voxel_data then
send_progress(name, 0, "Load failed")
return false, "Failed to load: " .. (err or "unknown error")
end
minetest.chat_send_player(name,
string.format("Loaded %d voxels. Placing...", voxel_data.voxelCount or 0))
send_progress(name, 40, "Placing voxels...")
-- Get player position as offset
local player = minetest.get_player_by_name(name)
if not player then
send_progress(name, 0, "Player not found")
return false, "Player not found."
end
local pos = player:get_pos()
-- Place voxels with color mapping
local placed = voxel_importer.place_voxels(voxel_data, pos, true)
send_progress(name, 100, "Done")
minetest.chat_send_player(name,
string.format("Placed %d blocks!", placed))
return true
end
})
--------------------------------------------------
-- /earth_export_viz: export for web visualizer
--------------------------------------------------
minetest.register_chatcommand("earth_export_viz", {
params = "<input_json> <output_json>",
description = "Export voxel data to simplified format for web visualizer",
privs = {server = true},
func = function(name, param)
local input_path, output_path = string.match(param, "^(%S+)%s+(%S+)$")
if not input_path or not output_path then
return false, "Usage: /earth_export_viz <input_json> <output_json>"
end
minetest.chat_send_player(name,
"Exporting voxel data from " .. input_path .. " to " .. output_path .. "...")
send_progress(name, 20, "Loading voxel JSON...")
-- Load voxel data
local voxel_data, err = voxel_importer.load_voxel_file(input_path)
if not voxel_data then
send_progress(name, 0, "Load failed")
return false, "Failed to load: " .. (err or "unknown error")
end
send_progress(name, 60, "Writing export JSON...")
-- Export for viz
local success, err_msg = voxel_importer.export_for_viz(voxel_data, output_path)
if not success then
send_progress(name, 0, "Export failed")
return false, "Export failed: " .. (err_msg or "unknown error")
end
send_progress(name, 100, "Export complete")
return true, "Exported to: " .. output_path
end
})