DS-Minetest
2017-04-01 ef8bb38bfd8e52a511fbe930257b173ddab93f63
Make forcefield controlable with digilines.
1 files modified
77 ■■■■■ changed files
technic/machines/HV/forcefield.lua 77 ●●●●● patch | view | raw | blame | history
technic/machines/HV/forcefield.lua
@@ -6,6 +6,8 @@
-- How expensive is the generator?
-- Leaves room for upgrades lowering the power drain?
local digilines_path = minetest.get_modpath("digilines")
local forcefield_power_drain   = 10
local S = technic.getter
@@ -90,7 +92,14 @@
end
local function set_forcefield_formspec(meta)
    local formspec = "size[5,2.25]"..
    local formspec
    if digilines_path then
        formspec = "size[5,3.25]"..
            "field[0.3,3;5,1;channel;Digiline Channel;"..meta:get_string("channel").."]"
    else
        formspec = "size[5,2.25]"
    end
    formspec = formspec..
        "field[0.3,0.5;2,1;range;"..S("Range")..";"..meta:get_int("range").."]"
    -- The names for these toggle buttons are explicit about which
    -- state they'll switch to, so that multiple presses (arising
@@ -130,9 +139,10 @@
        update_forcefield(pos, meta, false)
    end
    if range then meta:set_int("range", range) end
    if fields.shape0 then meta:set_int("shape", 0) end
    if fields.shape1 then meta:set_int("shape", 1) end
    if fields.enable then meta:set_int("enabled", 1) end
    if fields.channel then meta:set_string("channel", fields.channel) end
    if fields.shape0  then meta:set_int("shape", 0) end
    if fields.shape1  then meta:set_int("shape", 1) end
    if fields.enable  then meta:set_int("enabled", 1) end
    if fields.disable then meta:set_int("enabled", 0) end
    if fields.mesecon_mode_0 then meta:set_int("mesecon_mode", 0) end
    if fields.mesecon_mode_1 then meta:set_int("mesecon_mode", 1) end
@@ -148,6 +158,60 @@
            minetest.get_meta(pos):set_int("mesecon_effect", 0)
        end
    }
}
local digiline_def = {
    receptor = {action = function() end},
    effector = {
        action = function(pos, node, channel, msg)
            local meta = minetest.get_meta(pos)
            if channel ~= meta:get_string("channel") then
                return
            end
            msg = msg:lower()
            if msg == "get" then
                digilines.receptor_send(pos, digilines.rules.default, channel, {
                    enabled = meta:get_int("enabled"),
                    range   = meta:get_int("range"),
                    shape   = meta:get_int("shape")
                })
                return
            elseif msg == "off" then
                meta:set_int("enabled", 0)
            elseif msg == "on" then
                meta:set_int("enabled", 1)
            elseif msg == "toggle" then
                local onn = meta:get_int("enabled")
                onn = 1-onn -- Mirror onn with pivot 0.5, so switch between 1 and 0.
                meta:set_int("enabled", onn)
            elseif msg:sub(1, 5) == "range" then
                local range = tonumber(msg:sub(7))
                if not range then
                    return
                end
                range = math.max(range, 5)
                range = math.min(range, 20)
                update_forcefield(pos, meta, false)
                meta:set_int("range", range)
            elseif msg:sub(1, 5) == "shape" then
                local shape = msg:sub(7):lower()
                if shape == "sphere" then
                    shape = 0
                elseif shape == "cube" then
                    shape = 1
                end
                shape = tonumber(shape)
                if not shape then
                    return
                end
                update_forcefield(pos, meta, false)
                meta:set_int("shape", shape)
            else
                return
            end
            set_forcefield_formspec(meta)
        end
    },
}
local function run(pos, node)
@@ -205,10 +269,14 @@
        meta:set_int("enabled", 0)
        meta:set_int("mesecon_mode", 0)
        meta:set_int("mesecon_effect", 0)
        if digilines_path then
            meta:set_string("channel", "forcefield"..minetest.pos_to_string(pos))
        end
        meta:set_string("infotext", S("%s Forcefield Emitter"):format("HV"))
        set_forcefield_formspec(meta)
    end,
    mesecons = mesecons,
    digiline = digiline_def,
    technic_run = run,
})
@@ -224,6 +292,7 @@
        update_forcefield(pos, meta, false)
    end,
    mesecons = mesecons,
    digiline = digiline_def,
    technic_run = run,
    technic_on_disable = function (pos, node)
        local meta = minetest.get_meta(pos)