From 674703f31ef061ade675e75a68c284aa2ccb0089 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Sat, 11 Aug 2018 15:47:25 +0300 Subject: [PATCH] Mesecons and toggleable machines! --- elepower_farming/nodes/harvester.lua | 19 +++- elepower_farming/nodes/planter.lua | 17 +++- elepower_farming/nodes/spawner.lua | 13 ++- elepower_machines/machines/bases/crafter.lua | 32 +++--- .../machines/bases/fluidgenerator.lua | 27 ++++-- .../machines/bases/generator.lua | 47 +++++---- elepower_machines/machines/bucketer.lua | 58 ++++++----- elepower_machines/machines/lava_cooler.lua | 15 ++- elepower_machines/machines/steam_turbine.lua | 3 +- elepower_papi/formspec.lua | 22 +++++ elepower_papi/helpers.lua | 20 ++++ elepower_papi/machine.lua | 91 +++++++++++++++++- elepower_papi/mod.conf | 2 +- .../textures/elepower_gui_cancel.png | Bin 0 -> 458 bytes elepower_papi/textures/elepower_gui_check.png | Bin 0 -> 613 bytes 15 files changed, 285 insertions(+), 81 deletions(-) create mode 100644 elepower_papi/textures/elepower_gui_cancel.png create mode 100644 elepower_papi/textures/elepower_gui_check.png diff --git a/elepower_farming/nodes/harvester.lua b/elepower_farming/nodes/harvester.lua index 136423a..75894f5 100644 --- a/elepower_farming/nodes/harvester.lua +++ b/elepower_farming/nodes/harvester.lua @@ -79,11 +79,12 @@ local function harvest(pos, harvested, fdir) return harvested end -local function get_formspec(timer, power, sludge) +local function get_formspec(timer, power, sludge, state) return "size[8,8.5]".. default.gui_bg.. default.gui_bg_img.. default.gui_slots.. + ele.formspec.state_switcher(7, 2.5, state).. ele.formspec.power_meter(power).. ele.formspec.fluid_bar(7, 0, sludge).. ele.formspec.create_bar(1, 0, 100 - timer, "#00ff11", true).. @@ -108,7 +109,11 @@ local function on_timer(pos, elapsed) local work = meta:get_int("src_time") local sludge = fluid_lib.get_buffer_data(pos, "sludge") - if storage > usage and sludge.amount + SLUDGE_PRODUCED < sludge.capacity then + local state = meta:get_int("state") + local is_enabled = ele.helpers.state_enabled(meta, pos, state) + local active = "Idle" + + if storage > usage and sludge.amount + SLUDGE_PRODUCED < sludge.capacity and is_enabled then if work == HARVESTER_TICK then local harvested = {} @@ -129,16 +134,24 @@ local function on_timer(pos, elapsed) work = work + 1 end + active = "Active" refresh = true ele.helpers.swap_node(pos, "elepower_farming:harvester_active") else + if not is_enabled then + active = "Off" + end + ele.helpers.swap_node(pos, "elepower_farming:harvester") end local power = {capacity = capacity, storage = storage, usage = usage} local work_percent = math.floor((work / HARVESTER_TICK)*100) - meta:set_string("formspec", get_formspec(work_percent, power, sludge)) + meta:set_string("formspec", get_formspec(work_percent, power, sludge, state)) + meta:set_string("infotext", ("Harvester %s\n%s"):format(active, + ele.capacity_text(capacity, storage))) + meta:set_int("storage", storage) meta:set_int("src_time", work) diff --git a/elepower_farming/nodes/planter.lua b/elepower_farming/nodes/planter.lua index 5e1fc6b..72ca97a 100644 --- a/elepower_farming/nodes/planter.lua +++ b/elepower_farming/nodes/planter.lua @@ -51,12 +51,13 @@ local ranges = { }, } -local function get_formspec(timer, power) +local function get_formspec(timer, power, state) return "size[8,10]".. default.gui_bg.. default.gui_bg_img.. default.gui_slots.. ele.formspec.power_meter(power).. + ele.formspec.state_switcher(7, 0, state).. ele.formspec.create_bar(1, 0, 100 - timer, "#00ff11", true).. "list[context;layout;2.5,0;3,3;]".. "list[context;src;0,3.5;8,2;]".. @@ -231,7 +232,11 @@ local function on_timer(pos, elapsed) local work = meta:get_int("src_time") - if storage > usage then + local state = meta:get_int("state") + local is_enabled = ele.helpers.state_enabled(meta, pos, state) + local active = "Idle" + + if storage > usage and is_enabled then if work == PLANTER_TICK then local planted = 0 for index, slot in ipairs(inv:get_list("layout")) do @@ -249,13 +254,19 @@ local function on_timer(pos, elapsed) work = work + 1 end + active = "Active" refresh = true + elseif not is_enabled then + active = "Off" end local power = {capacity = capacity, storage = storage, usage = usage} local work_percent = math.floor((work / PLANTER_TICK)*100) - meta:set_string("formspec", get_formspec(work_percent, power)) + meta:set_string("formspec", get_formspec(work_percent, power, state)) + meta:set_string("infotext", ("Planter %s\n%s"):format(active, + ele.capacity_text(capacity, storage))) + meta:set_int("storage", storage) meta:set_int("src_time", work) diff --git a/elepower_farming/nodes/spawner.lua b/elepower_farming/nodes/spawner.lua index 3dd4f67..fb1412b 100644 --- a/elepower_farming/nodes/spawner.lua +++ b/elepower_farming/nodes/spawner.lua @@ -89,12 +89,13 @@ local function spawn(pos, mob) return end -local function get_formspec(timer, power) +local function get_formspec(timer, power, state) return "size[8,8.5]".. default.gui_bg.. default.gui_bg_img.. default.gui_slots.. ele.formspec.power_meter(power).. + ele.formspec.state_switcher(7, 0, state).. ele.formspec.create_bar(1, 0, 100 - timer, "#00ff11", true).. "list[context;src;3.5,1.5;1,1;]".. "image[3.5,1.5;1,1;elefarming_egg_silhouette.png]".. @@ -117,11 +118,15 @@ local function on_timer(pos, elapsed) local work = meta:get_int("src_time") + local state = meta:get_int("state") + local is_enabled = ele.helpers.state_enabled(meta, pos, state) + local egg_slot = inv:get_stack("src", 1) local egg_name = egg_slot:get_name() local mob_desc = "None" local active = "Active" - if storage > usage and not egg_slot:is_empty() and ele.helpers.get_item_group(egg_name, "spawn_egg") then + + if storage > usage and not egg_slot:is_empty() and ele.helpers.get_item_group(egg_name, "spawn_egg") and is_enabled then local mob_name = egg_name:gsub("_set", "") if work == SPAWNER_TICK then @@ -142,6 +147,8 @@ local function on_timer(pos, elapsed) refresh = true mob_desc = minetest.registered_items[mob_name].description + elseif not is_enabled then + active = "Off" else work = 0 active = "Inactive" @@ -153,7 +160,7 @@ local function on_timer(pos, elapsed) local power = {capacity = capacity, storage = storage, usage = usage} local work_percent = math.floor((work / SPAWNER_TICK)*100) - meta:set_string("formspec", get_formspec(work_percent, power)) + meta:set_string("formspec", get_formspec(work_percent, power, state)) meta:set_int("storage", storage) meta:set_int("src_time", work) diff --git a/elepower_machines/machines/bases/crafter.lua b/elepower_machines/machines/bases/crafter.lua index a9aa166..1df7352 100644 --- a/elepower_machines/machines/bases/crafter.lua +++ b/elepower_machines/machines/bases/crafter.lua @@ -2,7 +2,7 @@ -- It accepts a recipe type registered beforehand. -- Specialized formspec for crafters -function ele.formspec.get_crafter_formspec(craft_type, power, percent) +function ele.formspec.get_crafter_formspec(craft_type, power, percent, pos, state) local craftstats = elepm.craft.types[craft_type] local input_size = craftstats.inputs @@ -45,6 +45,7 @@ function ele.formspec.get_crafter_formspec(craft_type, power, percent) default.gui_bg_img.. default.gui_slots.. ele.formspec.power_meter(power).. + ele.formspec.state_switcher(7, 0, state).. "list[context;src;"..x..","..y..";"..in_width..","..in_height..";]".. bar.. "list[context;dst;5,1;2,2;]".. @@ -92,11 +93,21 @@ function elepm.register_crafter(nodename, nodedef) local usage = ele.helpers.get_node_property(meta, pos, "usage") local storage = ele.helpers.get_node_property(meta, pos, "storage") local time = meta:get_int("src_time") + local state = meta:get_int("state") + local status = "Idle" + + local is_enabled = ele.helpers.state_enabled(meta, pos, state) local res_time = 0 local pow_buffer = {capacity = capacity, storage = storage, usage = 0} while true do + if not is_enabled then + time = 0 + status = "Off" + break + end + local result = elepm.get_recipe(craft_type, inv:get_list("src")) local power_operation = false @@ -112,24 +123,22 @@ function elepm.register_crafter(nodename, nodedef) if result.time == 0 then time = 0 - meta:set_string("infotext", ("%s Idle"):format(nodedef.description) .. - "\n" .. ele.capacity_text(capacity, storage)) + status = "Idle" else - meta:set_string("infotext", ("%s Out of Power!"):format(nodedef.description) .. - "\n" .. ele.capacity_text(capacity, storage)) + status = "Out of Power!" end break end refresh = true + status = "Active" -- One step - meta:set_int("storage", storage - usage) + storage = storage - usage + meta:set_int("storage", storage) pow_buffer = {capacity = capacity, storage = storage, usage = usage} time = time + ele.helpers.round(machine_speed * 10) - meta:set_string("infotext", ("%s Active"):format(nodedef.description) .. - "\n" .. ele.capacity_text(capacity, storage)) if nodedef.ele_active_node then local active_node = nodename.."_active" @@ -166,8 +175,7 @@ function elepm.register_crafter(nodename, nodedef) if not room_for_output then ele.helpers.swap_node(pos, machine_node) time = ele.helpers.round(res_time*10) - meta:set_string("infotext", ("%s Output Full!"):format(nodedef.description) .. - "\n" .. ele.capacity_text(capacity, storage)) + status = "Output Full!" break end @@ -182,7 +190,9 @@ function elepm.register_crafter(nodename, nodedef) pct = math.floor((time / ele.helpers.round(res_time * 10)) * 100) end - meta:set_string("formspec", get_formspec(craft_type, pow_buffer, pct, pos)) + meta:set_string("formspec", get_formspec(craft_type, pow_buffer, pct, pos, state)) + meta:set_string("infotext", ("%s %s"):format(nodedef.description, status) .. + "\n" .. ele.capacity_text(capacity, storage)) meta:set_int("src_time", time) return refresh diff --git a/elepower_machines/machines/bases/fluidgenerator.lua b/elepower_machines/machines/bases/fluidgenerator.lua index 4ca65b5..e1a4fe9 100644 --- a/elepower_machines/machines/bases/fluidgenerator.lua +++ b/elepower_machines/machines/bases/fluidgenerator.lua @@ -1,10 +1,11 @@ -local function get_formspec_default(power, percent, buffer) +local function get_formspec_default(power, percent, buffer, state) return "size[8,8.5]".. default.gui_bg.. default.gui_bg_img.. default.gui_slots.. ele.formspec.power_meter(power).. + ele.formspec.state_switcher(7, 2.5, state).. ele.formspec.fluid_bar(7, 0, buffer).. "image[3.5,1.5;1,1;default_furnace_fire_bg.png^[lowpart:".. (percent)..":default_furnace_fire_fg.png]".. @@ -58,12 +59,21 @@ function ele.register_fluid_generator(nodename, nodedef) local generation = ele.helpers.get_node_property(meta, pos, "usage") local storage = ele.helpers.get_node_property(meta, pos, "storage") + local state = meta:get_int("state") + local is_enabled = ele.helpers.state_enabled(meta, pos, state) + -- Fluid buffer local flbuffer = fluid_lib.get_buffer_data(pos, buffer_name) - local pow_buffer - if not flbuffer or flbuffer.fluid == "" then return false end + local pow_buffer = {capacity = capacity, storage = storage, usage = 0} + local status = "Idle" while true do + if not flbuffer or flbuffer.fluid == "" then break end + if not is_enabled then + status = "Off" + break + end + -- If more to burn and the energy produced was used: produce some more if burn_time > 0 then if storage + generation > capacity then @@ -77,7 +87,7 @@ function ele.register_fluid_generator(nodename, nodedef) meta:set_int("burn_time", burn_time) end - pow_buffer = {capacity = capacity, storage = storage, usage = 0} + status = "Active" -- Burn another bucket of lava if burn_time == 0 then @@ -95,10 +105,7 @@ function ele.register_fluid_generator(nodename, nodedef) refresh = true else - meta:set_string("formspec", get_formspec(pow_buffer, 0, flbuffer)) - meta:set_string("infotext", ("%s Idle\n%s\n%s"):format(nodedef.description, - ele.capacity_text(capacity, storage), fluid_lib.buffer_to_string(flbuffer))) - + status = "Idle" ele.helpers.swap_node(pos, nodename) end end @@ -107,8 +114,8 @@ function ele.register_fluid_generator(nodename, nodedef) end local percent = math.floor((burn_time / burn_totaltime) * 100) - meta:set_string("formspec", get_formspec(pow_buffer, percent, flbuffer)) - meta:set_string("infotext", ("%s Active\n%s\n%s"):format(nodedef.description, + meta:set_string("formspec", get_formspec(pow_buffer, percent, flbuffer, state)) + meta:set_string("infotext", ("%s %s\n%s\n%s"):format(nodedef.description, status, ele.capacity_text(capacity, storage), fluid_lib.buffer_to_string(flbuffer))) meta:set_int(buffer_name .. "_fluid_storage", flbuffer.amount) diff --git a/elepower_machines/machines/bases/generator.lua b/elepower_machines/machines/bases/generator.lua index 6196c08..da1dc0c 100644 --- a/elepower_machines/machines/bases/generator.lua +++ b/elepower_machines/machines/bases/generator.lua @@ -1,10 +1,11 @@ -local function get_formspec_default(power, percent) +local function get_formspec_default(power, percent, state) return "size[8,8.5]".. default.gui_bg.. default.gui_bg_img.. default.gui_slots.. ele.formspec.power_meter(power).. + ele.formspec.state_switcher(7, 0, state).. "list[context;src;3,1.5;1,1;]".. "image[4,1.5;1,1;default_furnace_fire_bg.png^[lowpart:".. percent..":default_furnace_fire_fg.png]".. @@ -44,22 +45,34 @@ function elepm.register_fuel_generator(nodename, nodedef) local generation = ele.helpers.get_node_property(meta, pos, "usage") local storage = ele.helpers.get_node_property(meta, pos, "storage") - -- If more to burn and the energy produced was used: produce some more - if burn_time > 0 then - if storage + generation > capacity then - return false - end - - storage = storage + generation - meta:set_int("storage", storage) - - burn_time = burn_time - 1 - meta:set_int("burn_time", burn_time) - end + local state = meta:get_int("state") + local is_enabled = ele.helpers.state_enabled(meta, pos, state) + local status = "Idle" local pow_buffer = {capacity = capacity, storage = storage, usage = 0} while true do + if not is_enabled then + status = "Off" + break + end + + -- If more to burn and the energy produced was used: produce some more + if burn_time > 0 then + if storage + generation > capacity then + return false + end + + storage = storage + generation + pow_buffer.storage = storage + meta:set_int("storage", storage) + + burn_time = burn_time - 1 + meta:set_int("burn_time", burn_time) + end + + status = "Active" + -- Burn another piece of fuel if burn_time == 0 then local inv = meta:get_inventory() @@ -88,9 +101,7 @@ function elepm.register_fuel_generator(nodename, nodedef) refresh = true else - meta:set_string("formspec", get_formspec(pow_buffer, 0)) - meta:set_string("infotext", ("%s Idle"):format(nodedef.description) .. - "\n" .. ele.capacity_text(capacity, storage)) + status = "Idle" ele.helpers.swap_node(pos, nodename) end end @@ -99,8 +110,8 @@ function elepm.register_fuel_generator(nodename, nodedef) end local percent = math.floor((burn_time / burn_totaltime) * 100) - meta:set_string("formspec", get_formspec(pow_buffer, percent)) - meta:set_string("infotext", ("%s Active"):format(nodedef.description) .. + meta:set_string("formspec", get_formspec(pow_buffer, percent, state)) + meta:set_string("infotext", ("%s %s"):format(nodedef.description, status) .. "\n" .. ele.capacity_text(capacity, storage)) return refresh diff --git a/elepower_machines/machines/bucketer.lua b/elepower_machines/machines/bucketer.lua index 34e6048..63b068e 100644 --- a/elepower_machines/machines/bucketer.lua +++ b/elepower_machines/machines/bucketer.lua @@ -1,5 +1,5 @@ -local function get_formspec(mode, buffer) +local function get_formspec(mode, buffer, state) if not mode then mode = 0 end @@ -13,6 +13,7 @@ local function get_formspec(mode, buffer) default.gui_bg.. default.gui_bg_img.. default.gui_slots.. + ele.formspec.state_switcher(0, 0, state).. ele.formspec.fluid_bar(7, 0.75, buffer).. "list[context;src;3.5,1;1,1;]".. "list[context;dst;3.5,2;1,1;]".. @@ -35,38 +36,44 @@ local function on_timer(pos, elapsed) local buffer = fluid_lib.get_buffer_data(pos, "input") local mode = meta:get_int("mode") + local state = meta:get_int("state") + + local is_enabled = ele.helpers.state_enabled(meta, pos, state) local bucket_slot = inv:get_stack("src", 1) local bucket_name = bucket_slot:get_name() - if mode == 0 and bucket_name == "bucket:bucket_empty" and buffer.amount >= 1000 then - -- Fill bucket - local bitem = bucket.liquids[buffer.fluid] - if bitem and bitem.itemname then - local bstack = ItemStack(bitem.itemname) - if inv:room_for_item("dst", bstack) then - inv:add_item("dst", bstack) - buffer.amount = buffer.amount - 1000 - bucket_slot:take_item() - inv:set_stack("src", 1, bucket_slot) + if is_enabled then + if mode == 0 and bucket_name == "bucket:bucket_empty" and buffer.amount >= 1000 then + -- Fill bucket + local bitem = bucket.liquids[buffer.fluid] + if bitem and bitem.itemname then + local bstack = ItemStack(bitem.itemname) + if inv:room_for_item("dst", bstack) then + inv:add_item("dst", bstack) + buffer.amount = buffer.amount - 1000 - refresh = true + bucket_slot:take_item() + inv:set_stack("src", 1, bucket_slot) + + refresh = true + end end - end - elseif mode == 1 and bucket.get_liquid_for_bucket(bucket_name) then - -- Empty bucket - local fluid = bucket.get_liquid_for_bucket(bucket_name) - if buffer.fluid == fluid or buffer.fluid == "" then - local bitem = ItemStack("bucket:bucket_empty") - if inv:room_for_item("dst", bitem) and buffer.amount + 1000 <= buffer.capacity then - buffer.amount = buffer.amount + 1000 - buffer.fluid = fluid - inv:add_item("dst", bitem) + elseif mode == 1 and bucket.get_liquid_for_bucket(bucket_name) then + -- Empty bucket + local fluid = bucket.get_liquid_for_bucket(bucket_name) + if buffer.fluid == fluid or buffer.fluid == "" then + local bitem = ItemStack("bucket:bucket_empty") + if inv:room_for_item("dst", bitem) and buffer.amount + 1000 <= buffer.capacity then + buffer.amount = buffer.amount + 1000 + buffer.fluid = fluid + inv:add_item("dst", bitem) - bucket_slot:take_item() - inv:set_stack("src", 1, bucket_slot) + bucket_slot:take_item() + inv:set_stack("src", 1, bucket_slot) - refresh = true + refresh = true + end end end end @@ -114,6 +121,7 @@ ele.register_base_device("elepower_machines:bucketer", { accepts = true, }, }, + paramtype2 = "facedir", on_timer = on_timer, on_construct = function (pos) local meta = minetest.get_meta(pos) diff --git a/elepower_machines/machines/lava_cooler.lua b/elepower_machines/machines/lava_cooler.lua index 04b9fc9..2b66bef 100644 --- a/elepower_machines/machines/lava_cooler.lua +++ b/elepower_machines/machines/lava_cooler.lua @@ -16,7 +16,7 @@ local cooler_recipes = { }, } -local function get_formspec(item_percent, coolant_buffer, hot_buffer, power, recipes, recipe) +local function get_formspec(item_percent, coolant_buffer, hot_buffer, power, recipes, recipe, state) local rclist = {} local x = 2.5 @@ -34,6 +34,7 @@ local function get_formspec(item_percent, coolant_buffer, hot_buffer, power, rec default.gui_bg_img.. default.gui_slots.. ele.formspec.power_meter(power).. + ele.formspec.state_switcher(3.5, 2.5, state).. ele.formspec.fluid_bar(1, 0, coolant_buffer).. ele.formspec.fluid_bar(7, 0, hot_buffer).. "list[context;dst;3.5,1.5;1,1;]".. @@ -50,7 +51,6 @@ local function get_formspec(item_percent, coolant_buffer, hot_buffer, power, rec default.get_hotbar_bg(0, 4.25) end - local function lava_cooler_timer(pos, elapsed) local refresh = false @@ -69,7 +69,10 @@ local function lava_cooler_timer(pos, elapsed) local time = meta:get_int("src_time") local active = "Active" - if storage > usage then + local state = meta:get_int("state") + local is_enabled = ele.helpers.state_enabled(meta, pos, state) + + if storage > usage and is_enabled then if coolant_buffer.amount >= 1000 and hot_buffer.amount >= 1000 then if time >= TIME then local room_for_output = true @@ -101,6 +104,8 @@ local function lava_cooler_timer(pos, elapsed) active = "Idle" refresh = false end + elseif not is_enabled then + active = "Off" else active = "Idle" end @@ -113,7 +118,7 @@ local function lava_cooler_timer(pos, elapsed) meta:set_string("infotext", ("Lava Cooler %s\n%s"):format(active, ele.capacity_text(capacity, storage))) meta:set_string("formspec", get_formspec(timer, coolant_buffer, hot_buffer, - power, cooler_recipes, recipe)) + power, cooler_recipes, recipe, state)) return refresh end @@ -142,7 +147,7 @@ ele.register_machine("elepower_machines:lava_cooler", { inv:set_size("dst", 1) meta:set_string("recipe", "default:cobble") - meta:set_string("formspec", get_formspec(0,nil,nil,0,cooler_recipes, "default:cobble")) + meta:set_string("formspec", get_formspec(0,nil,nil,nil,cooler_recipes, "default:cobble")) end, on_timer = lava_cooler_timer, on_receive_fields = function (pos, formname, fields, sender) diff --git a/elepower_machines/machines/steam_turbine.lua b/elepower_machines/machines/steam_turbine.lua index 69738ec..294bead 100644 --- a/elepower_machines/machines/steam_turbine.lua +++ b/elepower_machines/machines/steam_turbine.lua @@ -1,10 +1,11 @@ -local function get_formspec(power, percent, buffer) +local function get_formspec(power, percent, buffer, state) return "size[8,8.5]".. default.gui_bg.. default.gui_bg_img.. default.gui_slots.. ele.formspec.power_meter(power).. + ele.formspec.state_switcher(3.5, 1.5, state).. ele.formspec.fluid_bar(7, 0, buffer).. "list[current_player;main;0,4.25;8,1;]".. "list[current_player;main;0,5.5;8,3;8]".. diff --git a/elepower_papi/formspec.lua b/elepower_papi/formspec.lua index 6c7d748..3f8d6dc 100644 --- a/elepower_papi/formspec.lua +++ b/elepower_papi/formspec.lua @@ -1,6 +1,28 @@ -- Formspec helpers ele.formspec = {} +ele.formspec.gui_switcher_icons = { + [0] = "elepower_gui_check.png", + "elepower_gui_cancel.png", + "mesecons_wire_on.png", + "mesecons_wire_off.png", +} + +function ele.formspec.state_switcher(x, y, state) + if not state then state = 0 end + local icon = ele.formspec.gui_switcher_icons[state] + local statedesc = ele.default.states[state] + + if statedesc then + statedesc = statedesc.d + else + statedesc = "" + end + statedesc = statedesc .. "\nPress to toggle" + + return "image_button["..x..","..y..";1,1;"..icon..";cyclestate;]".. + "tooltip[cyclestate;"..statedesc.."]" +end function ele.formspec.create_bar(x, y, metric, color, small) if not metric or type(metric) ~= "number" or metric < 0 then metric = 0 end diff --git a/elepower_papi/helpers.lua b/elepower_papi/helpers.lua index 262f2ce..6769030 100644 --- a/elepower_papi/helpers.lua +++ b/elepower_papi/helpers.lua @@ -103,3 +103,23 @@ function ele.helpers.comma_value(n) -- credit http://richard.warburton.it local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$') return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right end + +function ele.helpers.state_enabled(meta, pos, state) + if not state then + state = meta:get_int("state") + end + + if state == 0 then + return true + elseif state == 1 then + return false + end + + if state == 2 and meta:get_int("signal_interrupt") == 1 then + return true + elseif state == 3 and meta:get_int("signal_interrupt") == 0 then + return true + end + + return false +end diff --git a/elepower_papi/machine.lua b/elepower_papi/machine.lua index 3dd826e..f59c194 100644 --- a/elepower_papi/machine.lua +++ b/elepower_papi/machine.lua @@ -2,6 +2,7 @@ -- Machine definitions local pw = minetest.get_modpath("pipeworks") ~= nil +local mc = minetest.get_modpath("mesecons") ~= nil local tl = minetest.get_modpath("tubelib") ~= nil --[[ @@ -84,6 +85,14 @@ function ele.default.metadata_inventory_changed(pos) end end +-- State machine descriptions +ele.default.states = { + [0] = {s = "on", d = "Always on", e = "toggle"}, + {s = "off", d = "Always off", e = "toggle"}, + {s = "signal", d = "Enable by Mesecons signal", e = "mesecons"}, + {s = "interrupt", d = "Disable by Mesecons signal", e = "mesecons"}, +} + -- Preserve power storage in the item stack dropped local function preserve_metadata(pos, oldnode, oldmeta, drops) local meta = minetest.get_meta(pos) @@ -125,6 +134,7 @@ function ele.capacity_text(capacity, storage) ele.helpers.comma_value(capacity), ele.unit) end +-- API support local tube = { insert_object = function(pos, node, stack, direction) local meta = minetest.get_meta(pos) @@ -163,6 +173,55 @@ local tubelib_tube = { end, } +local mesecons_def = {} +if mc then + mesecons_def = { + effector = { + action_on = function (pos, node) + local meta = minetest.get_meta(pos) + meta:set_int("signal_interrupt", 1) + end, + action_off = function (pos, node) + local meta = minetest.get_meta(pos) + meta:set_int("signal_interrupt", 0) + end, + action_change = function (pos, node) + local t = minetest.get_node_timer(pos) + if not t:is_started() then + t:start(1.0) + end + end, + } + } +end + +-- Functions + +local function switch_state(pos, state_def) + local meta = minetest.get_meta(pos) + local state = meta:get_int("state") + local states = {} + for id,state in pairs(ele.default.states) do + if state_def[state.e] then + states[#states + 1] = id + end + end + + if #states == 0 then return end + + state = state + 1 + if state >= #states then + state = 0 + end + state = states[state + 1] + meta:set_int("state", state) + + local t = minetest.get_node_timer(pos) + if not t:is_started() then + t:start(1.0) + end +end + -- Register a base device function ele.register_base_device(nodename, nodedef) local tlsupp = tl and nodedef.groups and (nodedef.groups["tubedevice"] or nodedef.groups["tube"]) @@ -228,12 +287,16 @@ function ele.register_base_device(nodename, nodedef) end end - -- Prevent digging when there's items inside if not nodedef.can_dig then nodedef.can_dig = can_dig end + -- Explicitly allow the disabling of the state machine + if nodedef.groups["state_machine"] ~= 0 and not nodedef["states"] then + nodedef.states = {toggle = true} + end + -- Pipeworks support if pw and nodedef.groups and (nodedef.groups["tubedevice"] or nodedef.groups["tube"]) then if nodedef['tube'] == false then @@ -251,6 +314,32 @@ function ele.register_base_device(nodename, nodedef) end end + -- Mesecons support + if mc then + nodedef["mesecons"] = mesecons_def + if nodedef.groups["state_machine"] ~= 1 then + nodedef.states["mesecons"] = true + end + end + + -- STATE MACHINE + local original_on_receive_fields = nodedef.on_receive_fields + nodedef.on_receive_fields = function (pos, formname, fields, sender) + if sender and sender ~= "" and minetest.is_protected(pos, sender:get_player_name()) then + return + end + + if nodedef.states then + if fields["cyclestate"] then + switch_state(pos, nodedef.states) + end + end + + if original_on_receive_fields then + return original_on_receive_fields(pos, formname, fields, sender) + end + end + -- Finally, register the damn thing already minetest.register_node(nodename, nodedef) local active_name = nil diff --git a/elepower_papi/mod.conf b/elepower_papi/mod.conf index 317ecd0..b239f82 100644 --- a/elepower_papi/mod.conf +++ b/elepower_papi/mod.conf @@ -1,3 +1,3 @@ name = elepower_papi description = Elepower Power Network API -optional_depends = default,pipeworks,tubelib +optional_depends = default,pipeworks,tubelib,mesecons diff --git a/elepower_papi/textures/elepower_gui_cancel.png b/elepower_papi/textures/elepower_gui_cancel.png new file mode 100644 index 0000000000000000000000000000000000000000..d894afc9ca6da77c681253344c1f689b4b09237f GIT binary patch literal 458 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!73?$#)eFPF@P5LY05l|lOkgE}1D zWKg@#paJ0mWgCswIshrok|4ie29+Dlo!vdX(;KGSwgLqilf2zs7&=&GJwPf7JR*yM zPQ4Doj7}P}D}aLRC7!;n?2kCOc@-7TGgM6k3Wa;RIEHXsPZlsrU|>pfWjm{>(Dqz~ z?et7W9-cME>c$(+%sF;og5~ALM#Z8u#@}aFI%IB}X>66Q)*upgZjPm_i*do3IX4?l zHFzErJ-p>GZ%Z`ec9Ca149u|#szpcstpi%2TH+c}l9E`GYL#4+3Zxi}3=A!F4J>pG z4MPl#t&D-lNZY{3%D{jrW14Ba#1H&(%P{RubhEf9thF1v;3|2E37{m+a>*roROUJzGkxg-6 z(AD|N8n1bT2a0`OB0oWVV)~1cn|rnL%3Wn@*W1r);L+Vb{n+;~`F%@2Pfq+ZIiE?< zo^AEx+z0GH7pRuFMwFx^mZVxG7o`Fz1|tJQ3ta;XT|>hVLt`srBP(N5Z381K0|O1q zrt)x7$D3zhSyj(9cFS|H7u^?41zbJk7I~ysW OA_h-aKbLh*2~7Z^Q0Dyr literal 0 HcmV?d00001