151 lines
5.5 KiB
Lua
151 lines
5.5 KiB
Lua
|
|
||
|
local dbg
|
||
|
if moddebug then dbg=moddebug.dbg("railcarts") else dbg={v1=function() end,v2=function() end,v3=function() end} end
|
||
|
|
||
|
minetest.register_craft({
|
||
|
output = 'node "railcarts:launcher_off" 5',
|
||
|
recipe = {
|
||
|
{"default:cobble", "default:mese_crystal_fragment","default:cobble"},
|
||
|
{"default:mese_crystal_fragment", "default:glass", "default:mese_crystal_fragment"},
|
||
|
{"default:cobble", "default:mese_crystal_fragment","default:cobble"},
|
||
|
}
|
||
|
})
|
||
|
|
||
|
minetest.register_craft({
|
||
|
output = 'node "railcarts:cart_detector_off" 5',
|
||
|
recipe = {
|
||
|
{"default:cobble", "default:glass","default:cobble"},
|
||
|
{"default:glass", "default:mese_crystal_fragment", "default:glass"},
|
||
|
{"default:cobble", "default:glass","default:cobble"},
|
||
|
}
|
||
|
})
|
||
|
|
||
|
minetest.register_craft({
|
||
|
output = 'node "railcarts:autolauncher" 2',
|
||
|
recipe = {
|
||
|
{"default:cobble", "default:mese_crystal_fragment","default:cobble"},
|
||
|
{"default:mese_crystal_fragment", "default:chest", "default:mese_crystal_fragment"},
|
||
|
{"default:cobble", "default:mese_crystal_fragment","default:cobble"},
|
||
|
}
|
||
|
})
|
||
|
|
||
|
minetest.register_craft({
|
||
|
output = 'node "railcarts:launcher_off" 2',
|
||
|
recipe = {
|
||
|
{"default:cobble", "default:mese_crystal_fragment","default:cobble"},
|
||
|
{"default:mese_crystal_fragment", "default:sand", "default:mese_crystal_fragment"},
|
||
|
{"default:cobble", "default:mese_crystal_fragment","default:cobble"},
|
||
|
}
|
||
|
})
|
||
|
minetest.register_node("railcarts:launcher_off", {
|
||
|
description = "Cart Launcher",
|
||
|
tiles ={"railcarts_launcher_off.png"},
|
||
|
is_ground_content = true,
|
||
|
groups = {cracky=3, mesecon=2},
|
||
|
|
||
|
mesecons = { conductor = {
|
||
|
state = "off",
|
||
|
onstate = "railcarts:launcher_on",
|
||
|
} }
|
||
|
})
|
||
|
|
||
|
minetest.register_node("railcarts:launcher_on", {
|
||
|
description = "Cart Launcher",
|
||
|
tiles ={"railcarts_launcher_on.png"},
|
||
|
is_ground_content = true,
|
||
|
groups = {cracky=3, mesecon=2},
|
||
|
drop = 'railcarts:launcher_off 1',
|
||
|
|
||
|
mesecons = { conductor = {
|
||
|
state = "on",
|
||
|
offstate = "railcarts:launcher_off",
|
||
|
} }
|
||
|
|
||
|
})
|
||
|
|
||
|
minetest.register_node("railcarts:autolauncher", {
|
||
|
description = "Cart Autolauncher",
|
||
|
tiles ={"railcarts_autolauncher.png"},
|
||
|
is_ground_content = true,
|
||
|
groups = {cracky=3},
|
||
|
drop = 'railcarts:autolauncher 1',
|
||
|
on_construct = function(pos)
|
||
|
local meta = minetest.get_meta(pos)
|
||
|
meta:set_string("formspec",
|
||
|
"size[8,8]"..
|
||
|
"list[current_name;main;0,0;8,3;]"..
|
||
|
"list[current_player;main;0,4;8,4;]")
|
||
|
meta:set_string("infotext", "Autolauncher")
|
||
|
local inv = meta:get_inventory()
|
||
|
inv:set_size("main", 24)
|
||
|
end,
|
||
|
can_dig = function(pos,player)
|
||
|
local meta = minetest.get_meta(pos);
|
||
|
local inv = meta:get_inventory()
|
||
|
return inv:is_empty("main")
|
||
|
end,
|
||
|
})
|
||
|
|
||
|
minetest.register_abm({
|
||
|
nodenames = {"railcarts:autolauncher"},
|
||
|
neighbors = {},
|
||
|
interval = 5.0,
|
||
|
chance = 1,
|
||
|
action =
|
||
|
function(pos, node, active_object_count, active_object_count_wider)
|
||
|
local meta = minetest.get_meta(pos)
|
||
|
local inv = meta:get_inventory()
|
||
|
|
||
|
tests = {{x=pos.x+1,y=pos.y,z=pos.z},
|
||
|
{x=pos.x-1,y=pos.y,z=pos.z},
|
||
|
{x=pos.x,y=pos.y,z=pos.z+1},
|
||
|
{x=pos.x,y=pos.y,z=pos.z-1},
|
||
|
{x=pos.x,y=pos.y-1,z=pos.z}}
|
||
|
|
||
|
for _, testpos in ipairs(tests) do
|
||
|
|
||
|
if is_rail(minetest.get_node(testpos)) then
|
||
|
|
||
|
-- Check there's not a cart (or anything else, like a mob) in the way
|
||
|
-- TODO: I think the block can be loaded, but the objects not active,
|
||
|
-- in which case this will drop another cart on top of an existing
|
||
|
-- one ad infinitum - needs fixing!
|
||
|
already = minetest.get_objects_inside_radius(testpos, 1)
|
||
|
if table.maxn(already) == 0 then
|
||
|
|
||
|
-- Get a cart from the autolauncher's inventory
|
||
|
local stack = inv:remove_item("main", ItemStack("railcarts:cart"))
|
||
|
local placecart = "railcarts:cart_ent"
|
||
|
if stack:is_empty() then
|
||
|
stack = inv:remove_item("main", ItemStack("railcarts:cargo_cart"))
|
||
|
if stack:is_empty() then
|
||
|
stack = inv:remove_item("main", ItemStack("railcarts:boring_cart"))
|
||
|
if stack:is_empty() then
|
||
|
dbg.v1("Autolauncher at "..postostr(pos).." is empty")
|
||
|
return
|
||
|
end
|
||
|
placecart = "railcarts:boring_cart_ent"
|
||
|
else
|
||
|
placecart = "railcarts:cargo_cart_ent"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
dbg.v1("Autolauncher placing "..placecart.." at"..postostr(testpos))
|
||
|
|
||
|
local object = minetest.add_entity(testpos, placecart)
|
||
|
if object then
|
||
|
local ent = object:get_luaentity()
|
||
|
local newdir = xz_to_direction({x=testpos.x-pos.x, z=testpos.z-pos.z})
|
||
|
current_state = {direction=newdir}
|
||
|
cartbase.setdirection(ent, current_state, newdir)
|
||
|
ent.direction = newdir
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
end,
|
||
|
})
|
||
|
|
||
|
|