item drop

This commit is contained in:
Thomas Rudin 2018-11-15 14:40:14 +01:00
parent 686055a682
commit 2ceaa9857e
4 changed files with 38 additions and 55 deletions

@ -1,4 +1,3 @@
local has_technic_mod = minetest.get_modpath("technic")
local register_spacecannon = function(def)
@ -42,17 +41,25 @@ local register_spacecannon = function(def)
local node = minetest.get_node(pos)
if node.name == "air" then
if node.name == "air" or node.name == "vacuum:vacuum" then
local objs = minetest.get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, 3)
local collided = false
for k, obj in pairs(objs) do
if obj:get_luaentity() ~= nil and obj:get_luaentity().name ~= self.name then
obj:punch(self.object, 1.0, {
full_punch_interval=1.0,
damage_groups={fleshy=def.range*2},
}, nil)
if obj:get_luaentity() ~= nil and obj:get_luaentity().name ~= self.name then
collided = true
obj:punch(self.object, 1.0, {
full_punch_interval=1.0,
damage_groups={fleshy=def.range*2},
}, nil)
end
end
elseif node.name ~= "air" then
if collided then
spacecannon.destroy(pos, def.range)
self.object:remove()
end
elseif node.name ~= "air" and node.name ~= "vacuum:vacuum" then
-- collision
spacecannon.destroy(pos, def.range)
self.object:remove()
@ -109,24 +116,12 @@ local register_spacecannon = function(def)
local meta = minetest.get_meta(pos)
meta:set_int("powerstorage", 0)
local inv = meta:get_inventory()
inv:set_size("main", 8)
if has_technic_mod then
meta:set_int("HV_EU_input", 0)
meta:set_int("HV_EU_demand", 0)
end
meta:set_int("HV_EU_input", 0)
meta:set_int("HV_EU_demand", 0)
spacecannon.update_formspec(meta)
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
return inv:is_empty("main")
end,
technic_run = function(pos, node)
local meta = minetest.get_meta(pos)
local eu_input = meta:get_int("HV_EU_input")
@ -156,9 +151,7 @@ local register_spacecannon = function(def)
})
if has_technic_mod then
technic.register_machine("HV", "spacecannon:cannon_" .. def.color, technic.receiver)
end
technic.register_machine("HV", "spacecannon:cannon_" .. def.color, technic.receiver)
minetest.register_craft({
output = 'spacecannon:cannon_' .. def.color,

@ -1,3 +1,3 @@
default
technic?
technic
mesecons?

@ -5,12 +5,7 @@ spacecannon = {
powerstorage = 10000,
-- charge value in EU
powerrequirement = 2500,
-- fuel item and count
power_item = "default:mese_crystal",
power_item_count = 1
powerrequirement = 2500
}
}

@ -1,12 +1,8 @@
spacecannon.update_formspec = function(meta)
meta:set_string("formspec", "size[8,10;]" ..
"button_exit[0,2;8,1;fire;Fire]" ..
"list[context;main;0,3;8,1;]" ..
"list[current_player;main;0,5;8,4;]")
meta:set_string("formspec", "size[8,2;]" ..
"button_exit[0,2;8,1;fire;Fire]")
end
spacecannon.fire = function(pos, color, speed, range)
@ -15,19 +11,9 @@ spacecannon.fire = function(pos, color, speed, range)
local owner = meta:get_string("owner")
if meta:get_int("powerstorage") < spacecannon.config.powerstorage * range then
-- not enough power
return
-- check inventory
local inv = meta:get_inventory()
local power_item = spacecannon.config.power_item
local power_item_count = spacecannon.config.power_item_count * range
if not inv:contains_item("main", {name=power_item, count=power_item_count}) then
minetest.chat_send_player(owner, "Not enough fuel to fire cannon, expected " .. power_item_count .. " " .. power_item)
return
end
-- use up items
inv:remove_item("main", {name=power_item, count=power_item_count})
else
-- use power
meta:set_int("powerstorage", 0)
@ -53,10 +39,17 @@ spacecannon.destroy = function(pos,range)
return -- fail fast
end
--TODO: replace env* stuff
local n = minetest.env:get_node(np)
if n.name ~= "air" then
minetest.env:remove_node(np)
local n = minetest.get_node_or_nil(np)
if n and n.name ~= "air" then
minetest.set_node(np, {name="air"})
local itemstacks = minetest.get_node_drops(n.name)
for _, itemname in ipairs(itemstacks) do
if math.random(5) == 5 then
-- chance drop
minetest.add_item(np, itemname)
end
end
end
end
end
@ -97,4 +90,6 @@ spacecannon.facedir_to_down_dir = function(facing)
{x=-1, y=0, z=0},
{x=1, y=0, z=0},
{x=0, y=1, z=0}})[math.floor(facing/4)]
end
end