2018-12-22 22:03:31 +01:00
|
|
|
--[[
|
|
|
|
|
|
|
|
Tube Library
|
|
|
|
============
|
|
|
|
|
2020-11-20 14:45:28 +01:00
|
|
|
Copyright (C) 2017-2020 Joachim Stolberg
|
2018-12-22 22:03:31 +01:00
|
|
|
|
2020-11-20 14:45:28 +01:00
|
|
|
AGPL v3
|
2018-12-22 22:03:31 +01:00
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
forceload.lua:
|
2022-06-09 23:49:50 +02:00
|
|
|
|
2018-12-22 22:03:31 +01:00
|
|
|
]]--
|
|
|
|
|
2020-11-03 20:19:38 +01:00
|
|
|
-- Load support for I18n
|
|
|
|
local S = tubelib.S
|
|
|
|
|
2018-12-22 22:03:31 +01:00
|
|
|
-- for lazy programmers
|
2020-11-03 20:19:38 +01:00
|
|
|
local P2S = function(pos) if pos then return minetest.pos_to_string(pos) end end
|
2018-12-22 22:03:31 +01:00
|
|
|
local P = minetest.string_to_pos
|
|
|
|
local M = minetest.get_meta
|
|
|
|
|
2019-01-03 19:40:28 +01:00
|
|
|
local function calc_area(pos)
|
|
|
|
local xpos = (math.floor(pos.x / 16) * 16)
|
|
|
|
local ypos = (math.floor(pos.y / 16) * 16)
|
|
|
|
local zpos = (math.floor(pos.z / 16) * 16)
|
|
|
|
local pos1 = {x=xpos, y=ypos, z=zpos}
|
|
|
|
local pos2 = {x=xpos+15, y=ypos+15, z=zpos+15}
|
|
|
|
return pos1, pos2
|
|
|
|
end
|
|
|
|
|
2018-12-22 22:03:31 +01:00
|
|
|
local function in_list(list, x)
|
2019-01-03 19:40:28 +01:00
|
|
|
local pos1 = calc_area(x)
|
2018-12-22 22:03:31 +01:00
|
|
|
for _,v in ipairs(list) do
|
2019-01-03 19:40:28 +01:00
|
|
|
local pos2 = calc_area(v)
|
|
|
|
if vector.equals(pos1, pos2) then return true end
|
2018-12-22 22:03:31 +01:00
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local function remove_list_elem(list, x)
|
|
|
|
local n = nil
|
|
|
|
for idx, v in ipairs(list) do
|
2022-06-09 23:49:50 +02:00
|
|
|
if vector.equals(v, x) then
|
2018-12-22 22:03:31 +01:00
|
|
|
n = idx
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if n then
|
|
|
|
table.remove(list, n)
|
|
|
|
end
|
|
|
|
return list
|
|
|
|
end
|
|
|
|
|
|
|
|
local function chat(player, text)
|
|
|
|
minetest.chat_send_player(player:get_player_name(), "[Tubelib] "..text)
|
|
|
|
end
|
|
|
|
|
2018-12-31 21:00:19 +01:00
|
|
|
local function get_node_lvm(pos)
|
|
|
|
local node = minetest.get_node_or_nil(pos)
|
|
|
|
if node then
|
|
|
|
return node
|
|
|
|
end
|
|
|
|
local vm = minetest.get_voxel_manip()
|
|
|
|
local MinEdge, MaxEdge = vm:read_from_map(pos, pos)
|
|
|
|
local data = vm:get_data()
|
|
|
|
local param2_data = vm:get_param2_data()
|
|
|
|
local area = VoxelArea:new({MinEdge = MinEdge, MaxEdge = MaxEdge})
|
|
|
|
local idx = area:index(pos.x, pos.y, pos.z)
|
|
|
|
node = {
|
|
|
|
name = minetest.get_name_from_content_id(data[idx]),
|
|
|
|
param2 = param2_data[idx]
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
end
|
|
|
|
|
2018-12-22 22:03:31 +01:00
|
|
|
local function add_pos(pos, player)
|
2023-05-20 17:41:18 +02:00
|
|
|
local meta = player:get_meta()
|
|
|
|
local lPos = minetest.deserialize(meta:get_string("tubelib_forceload_blocks")) or {}
|
2019-01-03 19:40:28 +01:00
|
|
|
if not in_list(lPos, pos) and #lPos < tubelib.max_num_forceload_blocks then
|
|
|
|
lPos[#lPos+1] = pos
|
2023-05-20 17:41:18 +02:00
|
|
|
meta:set_string("tubelib_forceload_blocks", minetest.serialize(lPos))
|
2018-12-22 22:03:31 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
2022-06-09 23:49:50 +02:00
|
|
|
|
2018-12-22 22:03:31 +01:00
|
|
|
local function del_pos(pos, player)
|
2023-05-20 17:41:18 +02:00
|
|
|
local meta = player:get_meta()
|
|
|
|
local lPos = minetest.deserialize(meta:get_string("tubelib_forceload_blocks")) or {}
|
2019-01-03 19:40:28 +01:00
|
|
|
lPos = remove_list_elem(lPos, pos)
|
2023-05-20 17:41:18 +02:00
|
|
|
meta:set_string("tubelib_forceload_blocks", minetest.serialize(lPos))
|
2018-12-22 22:03:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local function get_pos_list(player)
|
2023-05-20 17:41:18 +02:00
|
|
|
local meta = player:get_meta()
|
|
|
|
return minetest.deserialize(meta:get_string("tubelib_forceload_blocks")) or {}
|
2018-12-22 22:03:31 +01:00
|
|
|
end
|
|
|
|
|
2018-12-31 21:00:19 +01:00
|
|
|
local function set_pos_list(player, lPos)
|
2023-05-20 17:41:18 +02:00
|
|
|
local meta = player:get_meta()
|
|
|
|
meta:set_string("tubelib_forceload_blocks", minetest.serialize(lPos))
|
2018-12-31 21:00:19 +01:00
|
|
|
end
|
|
|
|
|
2018-12-22 22:03:31 +01:00
|
|
|
local function get_data(pos, player)
|
|
|
|
local pos1, pos2 = calc_area(pos)
|
2023-05-20 17:41:18 +02:00
|
|
|
local meta = player:get_meta()
|
|
|
|
local num = #minetest.deserialize(meta:get_string("tubelib_forceload_blocks")) or 0
|
2018-12-22 22:03:31 +01:00
|
|
|
local max = tubelib.max_num_forceload_blocks
|
|
|
|
return pos1, pos2, num, max
|
|
|
|
end
|
|
|
|
|
2018-12-28 17:16:14 +01:00
|
|
|
local function formspec(player)
|
|
|
|
local lPos = get_pos_list(player)
|
|
|
|
local tRes = {}
|
|
|
|
tRes[1] = "size[7,9]"..
|
|
|
|
default.gui_bg..
|
|
|
|
default.gui_bg_img..
|
|
|
|
default.gui_slots..
|
2020-11-03 20:19:38 +01:00
|
|
|
"label[0,0;"..S("List of your Forceload Blocks")..":]"
|
2022-06-09 23:49:50 +02:00
|
|
|
|
2018-12-28 17:16:14 +01:00
|
|
|
for idx,pos in ipairs(lPos) do
|
|
|
|
local pos1, pos2 = calc_area(pos)
|
|
|
|
local ypos = 0.2 + idx * 0.4
|
|
|
|
tRes[#tRes+1] = "label[0,"..ypos..";"..idx.."]"
|
2020-11-03 20:19:38 +01:00
|
|
|
tRes[#tRes+1] = "label[0.8,"..ypos..";"..P2S(pos1).."]"
|
2020-11-13 20:32:21 +01:00
|
|
|
tRes[#tRes+1] = "label[3.2,"..ypos..";"..S("to").."]"
|
2020-11-03 20:19:38 +01:00
|
|
|
tRes[#tRes+1] = "label[4,"..ypos..";"..P2S(pos2).."]"
|
2018-12-28 17:16:14 +01:00
|
|
|
end
|
|
|
|
return table.concat(tRes)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2018-12-22 22:03:31 +01:00
|
|
|
minetest.register_node("tubelib:forceload", {
|
2020-11-03 20:19:38 +01:00
|
|
|
description = S("Tubelib Forceload Block"),
|
2018-12-22 22:03:31 +01:00
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
|
|
|
'tubelib_front.png',
|
|
|
|
'tubelib_front.png',
|
|
|
|
{
|
|
|
|
image = "tubelib_forceload.png",
|
|
|
|
backface_culling = false,
|
|
|
|
animation = {
|
|
|
|
type = "vertical_frames",
|
|
|
|
aspect_w = 32,
|
|
|
|
aspect_h = 32,
|
|
|
|
length = 0.5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
after_place_node = function(pos, placer, itemstack)
|
|
|
|
if add_pos(pos, placer) then
|
|
|
|
minetest.forceload_block(pos, true)
|
|
|
|
local pos1, pos2, num, max = get_data(pos, placer)
|
2020-11-13 20:32:21 +01:00
|
|
|
M(pos):set_string("infotext", S("Area").." "..P2S(pos1).." "..S("to").." "..P2S(pos2).." "..S("loaded!").."\n"..
|
2020-11-03 20:19:38 +01:00
|
|
|
S("Punch the block to make the area visible."))
|
|
|
|
chat(placer, S("Area").." ("..num.."/"..max..") "..P2S(pos1).." "..S("to").." "..P2S(pos2).." "..S("loaded!"))
|
2018-12-23 12:00:56 +01:00
|
|
|
tubelib.mark_region(placer:get_player_name(), pos1, pos2)
|
|
|
|
M(pos):set_string("owner", placer:get_player_name())
|
2018-12-22 22:03:31 +01:00
|
|
|
else
|
2020-11-03 20:19:38 +01:00
|
|
|
chat(placer, S("Area already loaded or max. number of Forceload Blocks reached!"))
|
2018-12-22 22:03:31 +01:00
|
|
|
minetest.remove_node(pos)
|
|
|
|
return itemstack
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
after_dig_node = function(pos, oldnode, oldmetadata, digger)
|
2019-01-01 18:02:26 +01:00
|
|
|
local player = minetest.get_player_by_name(oldmetadata.fields.owner)
|
2019-02-16 10:35:33 +01:00
|
|
|
if player then
|
|
|
|
del_pos(pos, player)
|
|
|
|
end
|
2018-12-22 22:03:31 +01:00
|
|
|
minetest.forceload_free_block(pos, true)
|
2018-12-23 12:00:56 +01:00
|
|
|
tubelib.unmark_region(oldmetadata.fields.owner)
|
|
|
|
end,
|
2022-06-09 23:49:50 +02:00
|
|
|
|
2018-12-28 17:16:14 +01:00
|
|
|
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
|
|
|
if M(pos):get_string("owner") == clicker:get_player_name() or
|
|
|
|
minetest.check_player_privs(clicker:get_player_name(), "server") then
|
|
|
|
local s = formspec(clicker)
|
|
|
|
minetest.show_formspec(clicker:get_player_name(), "tubelib:forceload", s)
|
|
|
|
end
|
|
|
|
end,
|
2022-06-09 23:49:50 +02:00
|
|
|
|
2018-12-23 12:00:56 +01:00
|
|
|
on_punch = function(pos, node, puncher, pointed_thing)
|
|
|
|
local pos1, pos2 = calc_area(pos)
|
|
|
|
tubelib.switch_region(puncher:get_player_name(), pos1, pos2)
|
2018-12-22 22:03:31 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
paramtype = "light",
|
|
|
|
sunlight_propagates = true,
|
2022-06-09 23:49:50 +02:00
|
|
|
groups = {choppy=2, cracky=2, crumbly=2,
|
2018-12-23 19:36:11 +01:00
|
|
|
not_in_creative_inventory = tubelib.max_num_forceload_blocks == 0 and 1 or 0},
|
2018-12-22 22:03:31 +01:00
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_wood_defaults(),
|
2022-06-09 23:49:50 +02:00
|
|
|
on_blast = function() end,
|
2018-12-22 22:03:31 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-12-23 19:36:11 +01:00
|
|
|
if tubelib.max_num_forceload_blocks > 0 then
|
|
|
|
minetest.register_craft({
|
|
|
|
output = "tubelib:forceload",
|
|
|
|
recipe = {
|
|
|
|
{"group:wood", "", "group:wood"},
|
|
|
|
{"", "basic_materials:energy_crystal_simple", ""},
|
|
|
|
{"group:wood", "tubelib:wlanchip", "group:wood"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
end
|
2018-12-22 22:03:31 +01:00
|
|
|
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
2018-12-31 21:00:19 +01:00
|
|
|
local lPos = {}
|
2018-12-22 22:03:31 +01:00
|
|
|
for _,pos in ipairs(get_pos_list(player)) do
|
2018-12-31 21:00:19 +01:00
|
|
|
local node = get_node_lvm(pos)
|
|
|
|
if node.name == "tubelib:forceload" then
|
|
|
|
minetest.forceload_block(pos, true)
|
|
|
|
lPos[#lPos+1] = pos
|
|
|
|
end
|
2018-12-22 22:03:31 +01:00
|
|
|
end
|
2018-12-31 21:00:19 +01:00
|
|
|
set_pos_list(player, lPos)
|
2018-12-22 22:03:31 +01:00
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
for _,pos in ipairs(get_pos_list(player)) do
|
|
|
|
minetest.forceload_free_block(pos, true)
|
|
|
|
end
|
|
|
|
end)
|