From 2ceaa9857e45bdfae24d4d9760623a18646334b6 Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Thu, 15 Nov 2018 14:40:14 +0100 Subject: [PATCH] item drop --- cannon.lua | 43 ++++++++++++++++++------------------------- depends.txt | 2 +- init.lua | 7 +------ util.lua | 41 ++++++++++++++++++----------------------- 4 files changed, 38 insertions(+), 55 deletions(-) diff --git a/cannon.lua b/cannon.lua index c99c2d5..9310d39 100644 --- a/cannon.lua +++ b/cannon.lua @@ -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, diff --git a/depends.txt b/depends.txt index a77d267..eeb653d 100644 --- a/depends.txt +++ b/depends.txt @@ -1,3 +1,3 @@ default -technic? +technic mesecons? diff --git a/init.lua b/init.lua index ac9d070..42ac1f8 100644 --- a/init.lua +++ b/init.lua @@ -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 } } diff --git a/util.lua b/util.lua index a3bab9d..0a9485a 100644 --- a/util.lua +++ b/util.lua @@ -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 \ No newline at end of file +end + +