From b33fd0cb5eee7ba27319270d10f8cf36f168c4f0 Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Tue, 24 Apr 2018 11:34:07 +0200 Subject: [PATCH] use up mese crystals --- cannon.lua | 6 +++--- init.lua | 7 ++++++- util.lua | 26 +++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/cannon.lua b/cannon.lua index f7bf0be..a9d2c46 100644 --- a/cannon.lua +++ b/cannon.lua @@ -61,7 +61,7 @@ local register_spacecannon = function(color, range, timeout, speed) mesecons = {effector = { action_on = function (pos, node) - spacecannon.fire(pos, color, speed) + spacecannon.fire(pos, color, speed, range) end }}, @@ -103,7 +103,7 @@ local register_spacecannon = function(color, range, timeout, speed) meta:set_string("infotext", "Power: " .. eu_input .. "/" .. demand .. " Store: " .. store) - if store < spacecannon.config.powerstorage then + if store < spacecannon.config.powerstorage * range then -- charge meta:set_int("HV_EU_demand", spacecannon.config.powerrequirement) store = store + eu_input @@ -118,7 +118,7 @@ local register_spacecannon = function(color, range, timeout, speed) local meta = minetest.get_meta(pos); if fields.fire then - spacecannon.fire(pos, color, speed) + spacecannon.fire(pos, color, speed, range) end end diff --git a/init.lua b/init.lua index 6f4fc55..ac9d070 100644 --- a/init.lua +++ b/init.lua @@ -2,11 +2,16 @@ spacecannon = { config = { -- technic EU storage value - powerstorage = 100000, + powerstorage = 10000, -- charge value in EU powerrequirement = 2500, + -- fuel item and count + power_item = "default:mese_crystal", + power_item_count = 1 + + } } diff --git a/util.lua b/util.lua index ef6aa84..34b9ad2 100644 --- a/util.lua +++ b/util.lua @@ -9,7 +9,31 @@ spacecannon.update_formspec = function(meta) "list[current_player;main;0,5;8,4;]") end -spacecannon.fire = function(pos, color, speed) +spacecannon.fire = function(pos, color, speed, range) + -- check fuel/power + local meta = minetest.get_meta(pos) + local owner = meta:get_string("owner") + + if meta:get_int("powerstorage") < spacecannon.config.powerstorage * range then + + -- 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) + end + + local node = minetest.get_node(pos) local dir = spacecannon.facedir_to_down_dir(node.param2) local obj = minetest.add_entity({x=pos.x+dir.x, y=pos.y+dir.y, z=pos.z+dir.z}, "spacecannon:energycube_" .. color)