-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.lua
More file actions
420 lines (385 loc) · 13.7 KB
/
api.lua
File metadata and controls
420 lines (385 loc) · 13.7 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
schemlib = {}
local extension = ".mtx"
local storage_dir = "schemlib"
local world_path = core.get_worldpath()
dofile(core.get_modpath("schemlib") .. "/util.lua")
dofile(core.get_modpath("schemlib") .. "/serialization.lua")
dofile(core.get_modpath("schemlib") .. "/mtx.lua")
local extents = {}
local function load_json_schematic(value)
local obj = {}
if value then
obj = core.parse_json(value)
return obj
end
return obj
end
local function load_to_map(origin_pos, obj)
local nodes = obj.cuboid
local o = obj.meta.offset
local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z
local add_node, get_meta, get_node_timer = core.add_node, core.get_meta, core.get_node_timer
-- local data = manip:get_data()
for i, entry in ipairs(nodes) do
entry.x, entry.y, entry.z = origin_x + (entry.x - o.x), origin_y + (entry.y - o.y), origin_z + (entry.z - o.z)
-- Entry acts as both position and node
add_node(entry, entry)
if entry.meta then
get_meta(entry):from_table(entry.meta)
end
if entry.timer then
get_node_timer(entry):set(entry.timer.timeout, entry.timer.elapsed)
if not entry.timer.started then
get_node_timer(entry):stop()
end
end
end
end
local function allocate_with_nodes(origin_pos, nodes)
local huge = math.huge
local pos1x, pos1y, pos1z = huge, huge, huge
local pos2x, pos2y, pos2z = -huge, -huge, -huge
local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z
for i, entry in ipairs(nodes) do
local x, y, z = origin_x + entry.x, origin_y + entry.y, origin_z + entry.z
if x < pos1x then
pos1x = x
end
if y < pos1y then
pos1y = y
end
if z < pos1z then
pos1z = z
end
if x > pos2x then
pos2x = x
end
if y > pos2y then
pos2y = y
end
if z > pos2z then
pos2z = z
end
end
local pos1 = {
x = pos1x,
y = pos1y,
z = pos1z
}
local pos2 = {
x = pos2x,
y = pos2y,
z = pos2z
}
return pos1, pos2, #nodes
end
function schemlib.get_selection(player)
local name = player:get_player_name()
return extents[name]
end
function schemlib.set_pos1(player, pos)
local name = player:get_player_name()
local tmp = extents[name]
if not tmp then
tmp = {}
end
if tmp.pos1 then
for obj in core.objects_inside_radius(tmp.pos1, 1) do
if obj:get_luaentity() ~= nil and obj:get_luaentity().name == "schemlib:pos1" then
if obj:get_luaentity():get_staticdata() == name then
obj:remove()
end
end
end
if tmp.pos2 then
-- get the min and max of the two positions
local min = vector.new(
math.min(tmp.pos1.x, tmp.pos2.x),
math.min(tmp.pos1.y, tmp.pos2.y),
math.min(tmp.pos1.z, tmp.pos2.z)
)
local max = vector.new(
math.max(tmp.pos1.x, tmp.pos2.x),
math.max(tmp.pos1.y, tmp.pos2.y),
math.max(tmp.pos1.z, tmp.pos2.z)
)
for obj in core.objects_in_area(min, max) do
if obj:get_luaentity() ~= nil and obj:get_luaentity().name == "schemlib:cuboid" then
local data = obj:get_luaentity():get_staticdata()
if data:split(";")[1] == name then
obj:remove()
end
end
end
end
end
tmp.pos1 = pos
extents[name] = tmp
if pos ~= nil then
core.add_entity(pos, "schemlib:pos1", player:get_player_name())
core.chat_send_player(name, "Position 1 set to " .. core.pos_to_string(pos))
if tmp.pos2 then
local middle = vector.divide(vector.add(tmp.pos1, tmp.pos2), 2)
core.add_entity(middle, "schemlib:cuboid", player:get_player_name() .. ";" .. core.pos_to_string(tmp.pos1) .. ";" .. core.pos_to_string(tmp.pos2))
local volume = schemlib.volume(tmp.pos1, tmp.pos2)
core.chat_send_player(name, "Region selected from " .. core.pos_to_string(tmp.pos1) .. " to " .. core.pos_to_string(tmp.pos2) .. " with volume " .. volume)
end
else
core.chat_send_player(name, "Position 1 cleared")
end
end
function schemlib.set_pos2(player, pos)
local name = player:get_player_name()
local tmp = extents[name]
if not tmp then
tmp = {}
end
if tmp.pos2 then
for obj in core.objects_inside_radius(tmp.pos2, 1) do
if obj:get_luaentity() ~= nil and obj:get_luaentity().name == "schemlib:pos2" then
if obj:get_luaentity():get_staticdata() == name then
obj:remove()
end
end
end
if tmp.pos1 then
local min = vector.new(
math.min(tmp.pos1.x, tmp.pos2.x),
math.min(tmp.pos1.y, tmp.pos2.y),
math.min(tmp.pos1.z, tmp.pos2.z)
)
local max = vector.new(
math.max(tmp.pos1.x, tmp.pos2.x),
math.max(tmp.pos1.y, tmp.pos2.y),
math.max(tmp.pos1.z, tmp.pos2.z)
)
for obj in core.objects_in_area(min, max) do
if obj:get_luaentity() ~= nil and obj:get_luaentity().name == "schemlib:cuboid" then
local data = obj:get_luaentity():get_staticdata()
if data:split(";")[1] == name then
obj:remove()
end
end
end
end
end
tmp.pos2 = pos
extents[name] = tmp
if pos ~= nil then
core.add_entity(pos, "schemlib:pos2", player:get_player_name())
core.chat_send_player(name, "Position 2 set to " .. core.pos_to_string(pos))
if tmp.pos1 then
local middle = vector.divide(vector.add(tmp.pos1, tmp.pos2), 2)
core.add_entity(middle, "schemlib:cuboid", player:get_player_name() .. ";" .. core.pos_to_string(tmp.pos1) .. ";" .. core.pos_to_string(tmp.pos2))
local volume = schemlib.volume(tmp.pos1, tmp.pos2)
core.chat_send_player(name, "Region selected from " .. core.pos_to_string(tmp.pos1) .. " to " .. core.pos_to_string(tmp.pos2) .. " with volume " .. volume)
end
else
core.chat_send_player(name, "Position 2 cleared")
end
end
function schemlib.clear_selection(player)
local name = player:get_player_name()
local tmp = extents[name]
if not tmp then
tmp = {}
end
if tmp.pos1 then
for obj in core.objects_inside_radius(tmp.pos1, 1) do
if obj:get_luaentity() ~= nil and obj:get_luaentity().name == "schemlib:pos1" then
if obj:get_luaentity():get_staticdata() == name then
obj:remove()
end
end
end
end
if tmp.pos2 then
for obj in core.objects_inside_radius(tmp.pos2, 1) do
if obj:get_luaentity() ~= nil and obj:get_luaentity().name == "schemlib:pos2" then
if obj:get_luaentity():get_staticdata() == name then
obj:remove()
end
end
end
end
if tmp.pos1 and tmp.pos2 then
local min = vector.new(
math.min(tmp.pos1.x, tmp.pos2.x),
math.min(tmp.pos1.y, tmp.pos2.y),
math.min(tmp.pos1.z, tmp.pos2.z)
)
local max = vector.new(
math.max(tmp.pos1.x, tmp.pos2.x),
math.max(tmp.pos1.y, tmp.pos2.y),
math.max(tmp.pos1.z, tmp.pos2.z)
)
for obj in core.objects_in_area(min, max) do
if obj:get_luaentity() ~= nil and obj:get_luaentity().name == "schemlib:cuboid" then
local data = obj:get_luaentity():get_staticdata()
if data:split(";")[1] == name then
obj:remove()
end
end
end
end
extents[name] = nil
core.chat_send_player(name, "Selection cleared")
end
--- Loads the nodes represented by string `json_obj` at position `origin_pos`.
-- @return The number of nodes deserialized.
function schemlib.process_emitted(origin_pos, obj_json, obj_table, emerge_cb)
if obj_json == nil and obj_table == nil then
return nil
end
-- core.log(">>> Loading Emitted...")
if obj_table == nil then
obj_table = load_json_schematic(obj_json)
end
local nodes = obj_table.cuboid
if not nodes then
return nil
end
if #nodes == 0 then
return #nodes
end
if not origin_pos or origin_pos == nil then
origin_pos = obj_table.meta.dest
end
if emerge_cb then
-- core.log(">>> Emerging Emitted...")
local pos1, pos2 = allocate_with_nodes(origin_pos, nodes)
-- emerge complete callback
local function emerge_area_callback_complete(blockpos, action, calls_remaining, param)
if calls_remaining == 0 then
-- preload area
local manip, area = schemlib.keep_loaded(pos1, pos2)
-- set nodes and meta
load_to_map(origin_pos, obj_table)
-- emerge completed callback
if emerge_cb ~= nil and type(emerge_cb) == 'function' then
emerge_cb(obj_table.meta, obj_table.flags)
end
end
end
-- load area to world
core.emerge_area(pos1, pos2, emerge_area_callback_complete)
end
return #nodes, obj_table.version, obj_table.meta
end
-- Save to file
function schemlib.emit(data, flags)
local min = data.min or data.pos1
local max = data.max or data.pos2
local serial_data, count = {}, 0
if flags.file_cache then
serial_data, count = schemlib.serialize_json(data, flags, min, max)
else
serial_data, count = schemlib.serialize_table(data, flags, min, max)
end
if flags.file_cache and flags.file_cache == true then
local path = world_path .. "/" .. storage_dir
core.mkdir(path)
local filename = path .. "/" .. data.filename
local file = io.open(filename .. extension, "w")
if file then
file:write(serial_data)
file:close()
end
end
return serial_data, count
end
-- Load from file in schemlib local storage
function schemlib.load_emitted(data)
local path = world_path .. "/" .. storage_dir
local filename = path .. "/" .. data.filename
local file = io.open(filename .. extension, "r")
if file then
local count, ver, meta = schemlib.process_emitted(data.origin, file:read("*all"), nil, data.emerge)
file:close()
return meta
end
return nil
end
-- Load from file from any path
function schemlib.load_emitted_file(data)
core.log(">>>> loading " .. data.filename)
local file = io.open(data.filepath .. "/" .. data.filename .. extension, "r")
local content = ""
local chunksize = 32768
if file then
local c = 0
while true do
local chunk = file:read(chunksize)
if not chunk then
break
end
core.log(">> Loaded chunk " .. c)
c = c + 1
content = content .. chunk
end
file:close()
end
-- local content = file:read("*all")
core.log(">>>> File Loaded " .. data.filename)
local count, ver, meta = schemlib.process_emitted(data.origin, content, nil, data.emerge)
core.log(">>> Emitted Load " .. data.filename)
return meta
end
function schemlib.get_dimensions(schem_name)
local origin = { x = 0, y = 0, z = 0 }
local meta = schemlib.load_emitted({
filename = schem_name,
origin = origin,
emerge = false,
})
if meta ~= nil then
return meta.size
end
return nil
end
function schemlib.clear_selections()
for name, extent in pairs(extents) do
if not extent then
return
end
if extent.pos1 then
for obj in core.objects_inside_radius(extent.pos1, 1) do
if obj:get_luaentity() ~= nil and obj:get_luaentity().name == "schemlib:pos1" then
if obj:get_luaentity():get_staticdata() == name then
obj:remove()
end
end
end
end
if extent.pos2 then
for obj in core.objects_inside_radius(extent.pos2, 1) do
if obj:get_luaentity() ~= nil and obj:get_luaentity().name == "schemlib:pos2" then
if obj:get_luaentity():get_staticdata() == name then
obj:remove()
end
end
end
end
if extent.pos1 and extent.pos2 then
local min = vector.new(
math.min(extent.pos1.x, extent.pos2.x),
math.min(extent.pos1.y, extent.pos2.y),
math.min(extent.pos1.z, extent.pos2.z)
)
local max = vector.new(
math.max(extent.pos1.x, extent.pos2.x),
math.max(extent.pos1.y, extent.pos2.y),
math.max(extent.pos1.z, extent.pos2.z)
)
for obj in core.objects_in_area(min, max) do
if obj:get_luaentity() ~= nil and obj:get_luaentity().name == "schemlib:cuboid" then
local data = obj:get_luaentity():get_staticdata()
if data:split(";")[1] == name then
obj:remove()
end
end
end
end
end
extents = {}
end