cidades/city_builder.lua

180 lines
4.8 KiB
Lua

--[[
Mod Cidades for Minetest
Copyright (C) 2020 BrunoMine (https://github.com/BrunoMine)
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>5.
City Builder
]]
minetest.register_node("cidades:area_mark", {
description = "Area Mark",
tiles = {"cidades_area_mark.png"},
groups = {choppy = 2, oddly_breakable_by_hand = 2},
drop = "",
})
-- Mark area
local mark_area = function(pos)
local meta = minetest.get_meta(pos)
local radius = meta:get_float("radius")
local height = meta:get_float("height")
local depth = meta:get_float("depth")
local minp = {
x = pos.x - radius - 1,
y = pos.y - depth - 1,
z = pos.z - radius - 1,
}
local maxp = {
x = pos.x + radius + 1,
y = pos.y + height + 1,
z = pos.z + radius + 1,
}
for y = minp.y, maxp.y do
minetest.set_node({x=maxp.x, y=y, z=maxp.z}, {name="cidades:area_mark"})
minetest.set_node({x=maxp.x, y=y, z=minp.z}, {name="cidades:area_mark"})
minetest.set_node({x=minp.x, y=y, z=maxp.z}, {name="cidades:area_mark"})
minetest.set_node({x=minp.x, y=y, z=minp.z}, {name="cidades:area_mark"})
end
for x = minp.x, maxp.x do
minetest.set_node({x=x, y=maxp.y, z=maxp.z}, {name="cidades:area_mark"})
minetest.set_node({x=x, y=maxp.y, z=minp.z}, {name="cidades:area_mark"})
end
for z = minp.z, maxp.z do
minetest.set_node({x=maxp.x, y=maxp.y, z=z}, {name="cidades:area_mark"})
minetest.set_node({x=minp.x, y=maxp.y, z=z}, {name="cidades:area_mark"})
end
end
-- Create Schematic
local export_city = function(pos)
local meta = minetest.get_meta(pos)
local radius = meta:get_float("radius")
local height = meta:get_float("height")
local depth = meta:get_float("depth")
local minp = {
x = pos.x - radius,
y = pos.y - depth,
z = pos.z - radius,
}
local maxp = {
x = pos.x + radius,
y = pos.y + height,
z = pos.z + radius,
}
local filename = meta:get_string("city_id")..".mts"
minetest.mkdir(minetest.get_worldpath().."/cities in work")
if minetest.create_schematic(minp, maxp, {}, minetest.get_worldpath().."/cities in work/"..filename) ~= true then
return minetest.chat_send_all("Export failed.")
end
minetest.chat_send_all("Schematic successfully exported. Restart the world to import it. "..filename.." file is in the world directory ('cities in work' folder).")
end
-- Load Schematic
local import_city = function(pos)
local meta = minetest.get_meta(pos)
local radius = meta:get_float("radius")
local height = meta:get_float("height")
local depth = meta:get_float("depth")
local minp = {
x = pos.x - radius,
y = pos.y - depth,
z = pos.z - radius,
}
local filename = meta:get_string("city_id")..".mts"
if minetest.place_schematic(minp, minetest.get_worldpath().."/cities in work/"..filename) ~= true then
return minetest.chat_send_all("Import failed.")
end
minetest.chat_send_all("Schematic successfully imported.")
end
-- Update formspec
local update_formspec = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", "size[8,8]"
.."field[1,1;6,1;city_id;City ID (eg. wood_ville);"..meta:get_string("city_id").."]"
.."field[1,4;2,1;radius;Radius;"..meta:get_float("radius").."]"
.."field[3,4;2,1;height;Height;"..meta:get_float("height").."]"
.."field[5,4;2,1;depth;Depth;"..meta:get_float("depth").."]"
.."label[0.5,5;This node is the reference for central position]"
.."label[0.5,5.5;Spawn position for this city is 6 blocks above this block.]"
.."button_exit[1,7;2,1;export;Export]"
.."button_exit[3,7;2,1;import;Import]"
.."button_exit[5,7;2,1;mark;Mark]")
end
-- City Builder node
minetest.register_node("cidades:city_builder", {
description = "City Builder",
paramtype2 = "facedir",
place_param2 = 0,
tiles = {"default_wood.png^default_apple.png"},
is_ground_content = false,
groups = {choppy = 2, oddly_breakable_by_hand = 2},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "City Builder")
meta:set_string("city_id", "")
meta:set_int("radius", 80)
meta:set_int("height", 50)
meta:set_int("depth", 15)
update_formspec(pos)
end,
on_receive_fields = function(pos, formname, fields, sender)
-- Check privs
if minetest.check_player_privs(sender:get_player_name(), {server = true}) ~= true then
return
end
-- Save settings
if fields.export or fields.import or fields.mark then
local meta = minetest.get_meta(pos)
meta:set_string("city_id", fields.city_id)
meta:set_float("radius", tonumber(fields.radius))
meta:set_float("height", tonumber(fields.height))
meta:set_float("depth", tonumber(fields.depth))
update_formspec(pos)
end
-- Mark area
if fields.mark then
mark_area(pos)
end
-- Import city
if fields.import then
import_city(pos)
end
-- Export city
if fields.export then
export_city(pos)
end
end,
})