Skip to content

Commit

Permalink
add basic digilines support
Browse files Browse the repository at this point in the history
fixes #4
  • Loading branch information
OgelGames committed Mar 20, 2020
1 parent cb00cd2 commit b7f26fd
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 72 deletions.
1 change: 1 addition & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ read_globals = {

-- Mod Deps
"player_monoids",
"digilines",
}
39 changes: 39 additions & 0 deletions digiline.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

function beacon.digiline_effector(pos, _, channel, msg)
if type(msg) ~= "table" then
return
end

local meta = minetest.get_meta(pos)
local set_channel = meta:get_string("channel")
if channel ~= set_channel then
return
end

if msg.command == "get" then
digilines.receptor_send(pos, digilines.rules.default, set_channel, {
radius = meta:get_int("range"),
effect = meta:get_string("effect")
})

elseif msg.command == "get_effects" then
digilines.receptor_send(pos, digilines.rules.default, set_channel, beacon.sorted_effect_ids)

elseif msg.command == "set" then
local level = beacon.get_level(pos)

if type(msg.effect) == "string" then
if beacon.effects[msg.effect] and beacon.effects[msg.effect].min_level <= level then
meta:set_string("effect", msg.effect)
elseif msg.effect == "none" then
meta:set_string("effect", "none")
end
end

if type(msg.range) == "number" then
meta:set_int("range", beacon.limit_range(msg.range, level))
elseif type(msg.radius) == "number" then
meta:set_int("range", beacon.limit_range(msg.radius, level))
end
end
end
69 changes: 24 additions & 45 deletions formspec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,40 @@ local function get_base_formspec()
"item_image[6,0.5;1,1;"..beacon.config.upgrade_item.."]"..
"item_image[7,0.5;1,1;"..beacon.config.upgrade_item.."]"..
"button_exit[4,2.875;4,1;save;Save]"
end
return base_formspec
end

local function get_beacon_level(pos)
local inv = minetest.get_meta(pos):get_inventory()
local level = 0
for i = 1, inv:get_size("beacon_upgrades") do
local stack = inv:get_stack("beacon_upgrades", i)
if not stack:is_empty() then
level = level + 1
end
end
return level
end

local function get_effects_for_level(level)
local str = "None"
local list = {"none"}
for _,id in ipairs(beacon.sorted_effect_ids) do
if beacon.effects[id].min_level <= level then
str = str..","..minetest.formspec_escape(beacon.effects[id].desc_name)
table.insert(list, id)
if beacon.has_digilines then
base_formspec = base_formspec..
"field[4.285,2.25;2,1;range;;${range}]"..
"label[6,1.55;Digiline Channel]"..
"field[6.285,2.25;2,1;channel;;${channel}]"
else
base_formspec = base_formspec..
"field[4.285,2.25;4,1;range;;${range}]"
end
end
return str, list
end

local function limit_range(range, level)
if not level then return 0 end
local max_range = beacon.config["effect_range_"..level]
range = tonumber(range)
if not range then return max_range end
range = math.min(range, max_range)
range = math.max(range, 1)
return range
return base_formspec
end

function beacon.update_formspec(pos)
local meta = minetest.get_meta(pos)
local level = get_beacon_level(pos)
local effects_string, effects_list = get_effects_for_level(level)
local level = beacon.get_level(pos)
local effect_names, effect_ids = beacon.get_effects_for_level(level)
local max_range = beacon.config["effect_range_"..level]
local set_range = meta:get_int("range")
local effect = meta:get_string("effect")

effect_names = table.concat(effect_names, ",")

local index = 1
for i=1, #effects_list do
if effects_list[i] == effect then
for i=1, #effect_ids do
if effect_ids[i] == effect then
index = i
break
end
end

local formspec =
get_base_formspec()..
"textlist[0,0.5;3.8,2.225;effects;"..effects_string..";"..index.."]"..
"label[4,1.55;Effect Radius (1-"..max_range..")]"..
"field[4.285,2.25;4,1;range;;"..set_range.."]"
"textlist[0,0.5;3.8,2.225;effects;"..effect_names..";"..index.."]"..
"label[4,1.55;Effect Radius (1-"..max_range..")]"

if meta:get_string("active") == "true" then
formspec = formspec.."button_exit[0,2.875;4,1;deactivate;Deactivate Beacon]"
Expand All @@ -89,15 +64,19 @@ function beacon.receive_fields(pos, formname, fields, player)
local name = player:get_player_name()
if minetest.is_protected(pos, name) then return end
local meta = minetest.get_meta(pos)
local level = get_beacon_level(pos)
local level = beacon.get_level(pos)

if fields.range then
meta:set_int("range", limit_range(fields.range, level))
meta:set_int("range", beacon.limit_range(fields.range, level))
end

if fields.channel then
meta:set_string("channel", fields.channel)
end

local event = minetest.explode_textlist_event(fields.effects)
if event.type == "CHG" then
local _,effect_list = get_effects_for_level(level)
local _,effect_list = beacon.get_effects_for_level(level)
local effect = effect_list[event.index] or "none"
meta:set_string("effect", effect)
end
Expand Down
26 changes: 0 additions & 26 deletions functions.lua
Original file line number Diff line number Diff line change
@@ -1,30 +1,4 @@

function beacon.get_node(pos)
local node = minetest.get_node(pos)
if node.name == "ignore" then
minetest.get_voxel_manip():read_from_map(pos, pos)
node = minetest.get_node(pos)
end
return node
end

function beacon.is_airlike_node(pos, name)
local node = beacon.get_node(pos)
if node.name == "ignore" then return false end
if node.name == "air" or node.name == "vacuum:vacuum" then return true end
local def = minetest.registered_nodes[node.name]
if def and def.drawtype == "airlike" and def.buildable_to then return true end
return false
end

function beacon.set_default_meta(pos)
local meta = minetest.get_meta(pos)
meta:set_int("range", beacon.config.effect_range_0)
meta:set_string("effect", beacon.config.default_effect)
meta:set_string("active", "false")
meta:get_inventory():set_size("beacon_upgrades", 4)
end

function beacon.on_place(itemstack, placer, pointed_thing)
-- check for correct pointed_thing
if not pointed_thing or not pointed_thing.above or not pointed_thing.under or pointed_thing.type ~= "node" then
Expand Down
60 changes: 60 additions & 0 deletions helpers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

function beacon.get_node(pos)
local node = minetest.get_node(pos)
if node.name == "ignore" then
minetest.get_voxel_manip():read_from_map(pos, pos)
node = minetest.get_node(pos)
end
return node
end

function beacon.is_airlike_node(pos, name)
local node = beacon.get_node(pos)
if node.name == "ignore" then return false end
if node.name == "air" or node.name == "vacuum:vacuum" then return true end
local def = minetest.registered_nodes[node.name]
if def and def.drawtype == "airlike" and def.buildable_to then return true end
return false
end

function beacon.set_default_meta(pos)
local meta = minetest.get_meta(pos)
meta:set_int("range", beacon.config.effect_range_0)
meta:set_string("effect", beacon.config.default_effect)
meta:set_string("active", "false")
meta:set_string("channel", "beacon_"..pos.x.."_"..pos.y.."_"..pos.z)
meta:get_inventory():set_size("beacon_upgrades", 4)
end

function beacon.get_level(pos)
local inv = minetest.get_meta(pos):get_inventory()
local level = 0
for i = 1, inv:get_size("beacon_upgrades") do
local stack = inv:get_stack("beacon_upgrades", i)
if not stack:is_empty() then
level = level + 1
end
end
return level
end

function beacon.get_effects_for_level(level)
local names = {"None"}
local ids = {"none"}
for _,id in ipairs(beacon.sorted_effect_ids) do
if beacon.effects[id].min_level <= level then
table.insert(names, minetest.formspec_escape(beacon.effects[id].desc_name))
table.insert(ids, id)
end
end
return names, ids
end

function beacon.limit_range(range, level)
local max_range = beacon.config["effect_range_"..level]
range = tonumber(range)
if not range then return max_range end
range = math.min(range, max_range)
range = math.max(range, 1)
return range
end
4 changes: 4 additions & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,16 @@ beacon.param2_to_dir = {

beacon.has_player_monoids = minetest.global_exists("player_monoids")

beacon.has_digilines = minetest.global_exists("digilines")

beacon.modpath = minetest.get_modpath(minetest.get_current_modname())

dofile(beacon.modpath.."/api.lua")
dofile(beacon.modpath.."/helpers.lua")
dofile(beacon.modpath.."/functions.lua")
dofile(beacon.modpath.."/formspec.lua")
dofile(beacon.modpath.."/effects.lua")
dofile(beacon.modpath.."/digiline.lua")
dofile(beacon.modpath.."/register.lua")
dofile(beacon.modpath.."/chatcommands.lua")

Expand Down
2 changes: 1 addition & 1 deletion mod.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name = beacon
description = Adds colored beacons that can grant effects to players
depends = default, dye
optional_depends = player_monoids
optional_depends = player_monoids, digilines
6 changes: 6 additions & 0 deletions register.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ for name,data in pairs(beacon.colors) do
end
end
end,
digiline = {
receptor = {},
effector = {
action = beacon.digiline_effector
},
},
})

-- coloring recipe
Expand Down

0 comments on commit b7f26fd

Please sign in to comment.