2019-08-17 09:47:16 +02:00
|
|
|
-- internationalization boilerplate
|
|
|
|
local MP = minetest.get_modpath(minetest.get_current_modname())
|
|
|
|
local S, NS = dofile(MP.."/intllib.lua")
|
|
|
|
|
2019-08-20 08:29:26 +02:00
|
|
|
-- This allows us to know which digtron the player has a formspec open for without
|
|
|
|
-- sending the digtron_id over the network
|
|
|
|
local player_interacting_with_digtron_id = {}
|
|
|
|
local player_interacting_with_digtron_pos = {}
|
|
|
|
|
2019-08-17 09:47:16 +02:00
|
|
|
local controller_nodebox = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = {
|
|
|
|
{-0.3125, -0.3125, -0.3125, 0.3125, 0.3125, 0.3125}, -- Core
|
|
|
|
{-0.1875, 0.3125, -0.1875, 0.1875, 0.5, 0.1875}, -- +y_connector
|
|
|
|
{-0.1875, -0.5, -0.1875, 0.1875, -0.3125, 0.1875}, -- -y_Connector
|
|
|
|
{0.3125, -0.1875, -0.1875, 0.5, 0.1875, 0.1875}, -- +x_connector
|
|
|
|
{-0.5, -0.1875, -0.1875, -0.3125, 0.1875, 0.1875}, -- -x_connector
|
|
|
|
{-0.1875, -0.1875, 0.3125, 0.1875, 0.1875, 0.5}, -- +z_connector
|
|
|
|
{-0.5, 0.125, -0.5, -0.125, 0.5, -0.3125}, -- back_connector_3
|
|
|
|
{0.125, 0.125, -0.5, 0.5, 0.5, -0.3125}, -- back_connector_1
|
|
|
|
{0.125, -0.5, -0.5, 0.5, -0.125, -0.3125}, -- back_connector_2
|
|
|
|
{-0.5, -0.5, -0.5, -0.125, -0.125, -0.3125}, -- back_connector_4
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-08-20 08:29:26 +02:00
|
|
|
local get_controller_unassembled_formspec = function(pos, player_name)
|
2019-08-18 23:29:15 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
return "size[9,9]"
|
|
|
|
.. "container[0.5,0]"
|
2019-08-20 08:29:26 +02:00
|
|
|
.. "button[0,0;1,1;assemble;Assemble]"
|
2019-08-18 23:51:25 +02:00
|
|
|
.. "field[1.2,0.25;2,1;digtron_name;Digtron name;"..meta:get_string("infotext").."]"
|
2019-08-18 23:29:15 +02:00
|
|
|
.. "field_close_on_enter[digtron_name;false]"
|
|
|
|
.. "container_end[]"
|
2019-08-17 09:47:16 +02:00
|
|
|
end
|
|
|
|
|
2019-08-20 08:29:26 +02:00
|
|
|
local get_controller_assembled_formspec = function(pos, digtron_id, player_name)
|
2019-08-18 23:29:15 +02:00
|
|
|
digtron.retrieve_inventory(digtron_id) -- ensures the detatched inventory exists and is populated
|
|
|
|
return "size[9,9]"
|
|
|
|
.. "container[0.5,0]"
|
2019-08-20 08:29:26 +02:00
|
|
|
.. "button[0,0;1,1;disassemble;Disassemble]"
|
2019-08-18 23:29:15 +02:00
|
|
|
.. "field[1.2,0.25;2,1;digtron_name;Digtron name;"..digtron.get_name(digtron_id).."]"
|
|
|
|
.. "field_close_on_enter[digtron_name;false]"
|
2019-08-20 08:29:26 +02:00
|
|
|
.. "button[3,0;1,1;move_forward;Move forward]"
|
2019-08-18 23:29:15 +02:00
|
|
|
.. "container_end[]"
|
|
|
|
.. "container[0.5,1]"
|
2019-08-20 04:02:16 +02:00
|
|
|
.. "list[detached:" .. digtron_id .. ";main;0,0;8,2]" -- TODO: paging system for inventory, guard against non-existent listname
|
|
|
|
.. "list[detached:" .. digtron_id .. ";fuel;0,2.5;8,2]" -- TODO: paging system for inventory, guard against non-existent listname
|
2019-08-18 23:29:15 +02:00
|
|
|
.. "container_end[]"
|
|
|
|
.. "container[0.5,5]list[current_player;main;0,0;8,1;]list[current_player;main;0,1.25;8,3;8]container_end[]"
|
|
|
|
.. "listring[current_player;main]"
|
|
|
|
.. "listring[detached:" .. digtron_id .. ";main]"
|
2019-08-17 09:47:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_node("digtron:controller", {
|
|
|
|
description = S("Digtron Control Module"),
|
|
|
|
_doc_items_longdesc = nil,
|
|
|
|
_doc_items_usagehelp = nil,
|
2019-08-20 04:02:16 +02:00
|
|
|
-- Note: this is not in the "digtron" group because we do not want it to be incorporated
|
|
|
|
-- into digtrons by mere adjacency; it must be the root node and only one root node is allowed.
|
|
|
|
groups = {cracky = 3, oddly_breakable_by_hand = 3},
|
2019-08-17 09:47:16 +02:00
|
|
|
paramtype = "light",
|
|
|
|
paramtype2= "facedir",
|
|
|
|
is_ground_content = false,
|
|
|
|
-- Aims in the +Z direction by default
|
|
|
|
tiles = {
|
|
|
|
"digtron_plate.png^[transformR90",
|
|
|
|
"digtron_plate.png^[transformR270",
|
|
|
|
"digtron_plate.png",
|
|
|
|
"digtron_plate.png^[transformR180",
|
|
|
|
"digtron_plate.png",
|
|
|
|
"digtron_plate.png^digtron_control.png",
|
|
|
|
},
|
|
|
|
drawtype = "nodebox",
|
|
|
|
node_box = controller_nodebox,
|
|
|
|
|
|
|
|
-- on_construct = function(pos)
|
|
|
|
-- end,
|
|
|
|
|
|
|
|
on_dig = function(pos, node, digger)
|
2019-08-19 02:23:51 +02:00
|
|
|
local player_name
|
|
|
|
if digger then
|
|
|
|
player_name = digger:get_player_name()
|
|
|
|
end
|
2019-08-17 09:47:16 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
2019-08-18 23:51:25 +02:00
|
|
|
local digtron_id = meta:get_string("digtron_id")
|
|
|
|
|
|
|
|
local stack = ItemStack({name=node.name, count=1, wear=0})
|
|
|
|
local stack_meta = stack:get_meta()
|
|
|
|
stack_meta:set_string("digtron_id", digtron_id)
|
|
|
|
stack_meta:set_string("description", meta:get_string("infotext"))
|
|
|
|
local inv = digger:get_inventory()
|
|
|
|
local stack = inv:add_item("main", stack)
|
|
|
|
if stack:get_count() > 0 then
|
|
|
|
minetest.add_item(pos, stack)
|
|
|
|
end
|
|
|
|
-- call on_dignodes callback
|
|
|
|
if digtron_id ~= "" then
|
2019-08-19 02:23:51 +02:00
|
|
|
digtron.remove_from_world(digtron_id, pos, player_name)
|
|
|
|
else
|
|
|
|
minetest.remove_node(pos)
|
2019-08-18 23:51:25 +02:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
preserve_metadata = function(pos, oldnode, oldmeta, drops)
|
|
|
|
for _, dropped in ipairs(drops) do
|
|
|
|
if dropped:get_name() == "digtron:controller" then
|
|
|
|
local stack_meta = dropped:get_meta()
|
|
|
|
stack_meta:set_string("digtron_id", oldmeta:get_string("digtron_id"))
|
|
|
|
stack_meta:set_string("description", oldmeta:get_string("infotext"))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2019-08-19 00:24:58 +02:00
|
|
|
on_place = function(itemstack, placer, pointed_thing)
|
|
|
|
-- Shall place item and return the leftover itemstack.
|
|
|
|
-- The placer may be any ObjectRef or nil.
|
2019-08-19 02:23:51 +02:00
|
|
|
local player_name
|
|
|
|
if placer then player_name = placer:get_player_name() end
|
|
|
|
|
2019-08-19 00:24:58 +02:00
|
|
|
local stack_meta = itemstack:get_meta()
|
|
|
|
local digtron_id = stack_meta:get_string("digtron_id")
|
|
|
|
if digtron_id ~= "" then
|
|
|
|
-- Test if Digtron will fit the surroundings
|
|
|
|
-- if not, try moving it up so that the lowest y-coordinate on the Digtron is
|
|
|
|
-- at the y-coordinate of the place clicked on and test again.
|
2019-08-19 02:23:51 +02:00
|
|
|
-- if that fails, show ghost of Digtron and fail to place.
|
|
|
|
local root_pos = minetest.get_pointed_thing_position(pointed_thing, true)
|
2019-08-19 02:37:47 +02:00
|
|
|
if digtron.build_to_world(digtron_id, root_pos, player_name) then
|
|
|
|
-- Note: DO NOT RESPECT CREATIVE MODE here.
|
|
|
|
-- If we allow multiple copies of a Digtron running around with the same digtron_id,
|
|
|
|
-- human sacrifice, dogs and cats living together, mass hysteria!
|
|
|
|
return ItemStack("")
|
|
|
|
end
|
|
|
|
return itemstack
|
|
|
|
else
|
|
|
|
return minetest.item_place(itemstack, placer, pointed_thing)
|
2019-08-19 00:24:58 +02:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2019-08-18 23:51:25 +02:00
|
|
|
after_place_node = function(pos, placer, itemstack, pointed_thing)
|
|
|
|
local stack_meta = itemstack:get_meta()
|
|
|
|
local title = stack_meta:get_string("description")
|
|
|
|
local digtron_id = stack_meta:get_string("digtron_id")
|
|
|
|
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
|
|
|
|
meta:set_string("infotext", title)
|
|
|
|
meta:set_string("digtron_id", digtron_id)
|
2019-08-20 08:29:26 +02:00
|
|
|
meta:mark_as_private("digtron_id")
|
2019-08-17 09:47:16 +02:00
|
|
|
end,
|
|
|
|
|
|
|
|
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
2019-08-20 08:29:26 +02:00
|
|
|
if clicker == nil then return end
|
2019-08-17 09:47:16 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
2019-08-18 10:23:08 +02:00
|
|
|
local digtron_id = meta:get_string("digtron_id")
|
2019-08-20 08:29:26 +02:00
|
|
|
local player_name = clicker:get_player_name()
|
2019-08-19 02:23:51 +02:00
|
|
|
|
2019-08-18 10:23:08 +02:00
|
|
|
if digtron_id == "" then
|
2019-08-20 08:29:26 +02:00
|
|
|
player_interacting_with_digtron_pos[player_name] = pos
|
2019-08-17 09:47:16 +02:00
|
|
|
minetest.show_formspec(player_name,
|
2019-08-20 08:29:26 +02:00
|
|
|
"digtron:controller_unassembled",
|
|
|
|
get_controller_unassembled_formspec(pos, player_name))
|
2019-08-17 09:47:16 +02:00
|
|
|
else
|
|
|
|
-- initialized
|
2019-08-20 08:29:26 +02:00
|
|
|
player_interacting_with_digtron_id[player_name] = digtron_id
|
2019-08-17 09:47:16 +02:00
|
|
|
minetest.show_formspec(player_name,
|
2019-08-20 08:29:26 +02:00
|
|
|
"digtron:controller_assembled",
|
|
|
|
get_controller_assembled_formspec(pos, digtron_id, player_name))
|
2019-08-17 09:47:16 +02:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
on_timer = function(pos, elapsed)
|
|
|
|
end,
|
2019-08-20 04:02:16 +02:00
|
|
|
|
|
|
|
on_blast = digtron.on_blast,
|
2019-08-17 09:47:16 +02:00
|
|
|
})
|
|
|
|
|
2019-08-20 08:29:26 +02:00
|
|
|
-- Dealing with an unassembled Digtron controller
|
2019-08-17 09:47:16 +02:00
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
2019-08-20 08:29:26 +02:00
|
|
|
if formname ~= "digtron:controller_unassembled" then
|
2019-08-17 09:47:16 +02:00
|
|
|
return
|
|
|
|
end
|
2019-08-20 08:29:26 +02:00
|
|
|
local name = player:get_player_name()
|
|
|
|
local pos = player_interacting_with_digtron_pos[name]
|
|
|
|
|
|
|
|
if pos == nil then return end
|
2019-08-17 09:47:16 +02:00
|
|
|
|
2019-08-20 08:29:26 +02:00
|
|
|
if fields.assemble then
|
|
|
|
local digtron_id = digtron.assemble(pos, name)
|
2019-08-17 09:47:16 +02:00
|
|
|
if digtron_id then
|
2019-08-18 07:12:12 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
2019-08-18 10:23:08 +02:00
|
|
|
meta:set_string("digtron_id", digtron_id)
|
2019-08-20 08:29:26 +02:00
|
|
|
meta:mark_as_private("digtron_id")
|
|
|
|
player_interacting_with_digtron_id[name] = digtron_id
|
2019-08-17 09:47:16 +02:00
|
|
|
minetest.show_formspec(name,
|
2019-08-20 08:29:26 +02:00
|
|
|
"digtron:controller_assembled",
|
|
|
|
get_controller_assembled_formspec(pos, digtron_id, name))
|
2019-08-17 09:47:16 +02:00
|
|
|
end
|
|
|
|
end
|
2019-08-18 23:29:15 +02:00
|
|
|
|
2019-08-19 00:24:58 +02:00
|
|
|
--TODO: this isn't recording the field when using ESC to exit the formspec
|
2019-08-18 23:29:15 +02:00
|
|
|
if fields.key_enter_field == "digtron_name" or fields.digtron_name then
|
|
|
|
local meta = minetest.get_meta(pos)
|
2019-08-18 23:51:25 +02:00
|
|
|
meta:set_string("infotext", fields.digtron_name)
|
2019-08-18 23:29:15 +02:00
|
|
|
end
|
2019-08-17 09:47:16 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
-- Controlling a fully armed and operational Digtron
|
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
2019-08-20 08:29:26 +02:00
|
|
|
if formname ~= "digtron:controller_assembled" then
|
2019-08-17 09:47:16 +02:00
|
|
|
return
|
|
|
|
end
|
2019-08-20 08:29:26 +02:00
|
|
|
local name = player:get_player_name()
|
|
|
|
local digtron_id = player_interacting_with_digtron_id[name]
|
|
|
|
if digtron_id == nil then return end
|
2019-08-17 09:47:16 +02:00
|
|
|
|
2019-08-20 08:29:26 +02:00
|
|
|
if fields.disassemble then
|
|
|
|
local pos = digtron.disassemble(digtron_id, name)
|
|
|
|
if pos then
|
|
|
|
player_interacting_with_digtron_pos[name] = pos
|
2019-08-18 07:12:12 +02:00
|
|
|
minetest.show_formspec(name,
|
2019-08-20 08:29:26 +02:00
|
|
|
"digtron:controller_unassembled",
|
|
|
|
get_controller_unassembled_formspec(pos, name))
|
2019-08-18 07:12:12 +02:00
|
|
|
end
|
2019-08-17 09:47:16 +02:00
|
|
|
end
|
|
|
|
|
2019-08-20 08:29:26 +02:00
|
|
|
if fields.move_forward then
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-08-19 00:24:58 +02:00
|
|
|
--TODO: this isn't recording the field when using ESC to exit the formspec
|
2019-08-18 23:29:15 +02:00
|
|
|
if fields.key_enter_field == "digtron_name" or fields.digtron_name then
|
2019-08-20 08:29:26 +02:00
|
|
|
local pos = digtron.get_pos(digtron_id)
|
2019-08-20 08:54:18 +02:00
|
|
|
if pos then
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
meta:set_string("infotext", fields.digtron_name)
|
|
|
|
digtron.set_name(digtron_id, fields.digtron_name)
|
|
|
|
end
|
2019-08-18 23:29:15 +02:00
|
|
|
end
|
|
|
|
|
2019-08-17 09:47:16 +02:00
|
|
|
end)
|