Skip to content

Commit

Permalink
code and comment tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
OgelGames committed Nov 1, 2021
1 parent 6ac1e48 commit c8f1cd8
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 65 deletions.
26 changes: 12 additions & 14 deletions api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ function beacon.register_color(name, colorstring, coloring_item)
return
end

beacon.colors[id] = { desc = name, color = colorstring }
beacon.colors[id] = {desc = name, color = colorstring}

-- beam
-- Beam
minetest.register_node("beacon:"..id.."beam", {
description = name.." Beacon Beam",
tiles = {"beacon_beam.png^[multiply:"..colorstring},
Expand All @@ -106,12 +106,12 @@ function beacon.register_color(name, colorstring, coloring_item)
walkable = false,
diggable = false,
climbable = beacon.config.beam_climbable,
selection_box = { type = "fixed", fixed = {0.125, 0.5, 0.125, -0.125, -0.5, -0.125} },
selection_box = {type = "fixed", fixed = {0.125, 0.5, 0.125, -0.125, -0.5, -0.125}},
on_place = beacon.on_place,
on_rotate = false, -- no rotation with screwdriver
on_rotate = false, -- No rotation with screwdriver
})

-- beam base
-- Beam base
minetest.register_node("beacon:"..id.."base", {
description = name.." Beacon Beam Base",
tiles = {"beacon_beambase.png^[multiply:"..colorstring},
Expand All @@ -126,12 +126,12 @@ function beacon.register_color(name, colorstring, coloring_item)
walkable = false,
diggable = false,
climbable = beacon.config.beam_climbable,
selection_box = { type = "fixed", fixed = {0.125, 0.5, 0.125, -0.125, -0.5, -0.125} },
selection_box = {type = "fixed", fixed = {0.125, 0.5, 0.125, -0.125, -0.5, -0.125}},
on_place = beacon.on_place,
on_rotate = false, -- no rotation with screwdriver
on_rotate = false, -- No rotation with screwdriver
})

-- beacon node
-- Beacon node
minetest.register_node("beacon:"..id, {
description = name.." Beacon",
tiles = {"(beacon_baseglow.png^[multiply:"..colorstring..")^beacon_base.png"},
Expand Down Expand Up @@ -188,7 +188,7 @@ function beacon.register_color(name, colorstring, coloring_item)
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
if oldmetadata.inventory and oldmetadata.inventory.beacon_upgrades then
for _,item in ipairs(oldmetadata.inventory.beacon_upgrades) do
for _,item in pairs(oldmetadata.inventory.beacon_upgrades) do
local stack = ItemStack(item)
if not stack:is_empty() then
minetest.add_item(pos, stack)
Expand All @@ -198,9 +198,7 @@ function beacon.register_color(name, colorstring, coloring_item)
end,
on_movenode = function(from_pos, to_pos)
beacon.check_beacon(to_pos)
minetest.after(1, function()
beacon.mark_inactive(from_pos)
end)
minetest.after(1, beacon.mark_inactive, from_pos)
end,
digiline = {
receptor = {},
Expand All @@ -210,12 +208,12 @@ function beacon.register_color(name, colorstring, coloring_item)
},
})

-- coloring recipe
-- Coloring recipe
if type(coloring_item) == "string" and coloring_item ~= "" and minetest.registered_items[coloring_item] then
minetest.register_craft({
type = "shapeless",
output = "beacon:"..id,
recipe = { "group:beacon", coloring_item },
recipe = {"group:beacon", coloring_item},
})
end
end
Expand Down
4 changes: 2 additions & 2 deletions chatcommands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ minetest.register_chatcommand("beacon_effects", {
param = param:trim()
local name = (param ~= "" and param or caller)
if not minetest.get_player_by_name(name) or not beacon.players[name] then
return false, "Player " .. name .. " does not exist or is not online."
return false, "Player "..name.." does not exist or is not online."
end
local output = name == caller and {"Your active effects:"} or {name.."'s active effects:"}
for _,id in ipairs(beacon.sorted_effect_ids) do
Expand All @@ -25,7 +25,7 @@ minetest.register_chatcommand("beacon_nearby", {
param = param:trim()
local name = (param ~= "" and param or caller)
if not minetest.get_player_by_name(name) or not beacon.players[name] then
return false, "Player " .. name .. " does not exist or is not online."
return false, "Player "..name.." does not exist or is not online."
end
local output = name == caller and {"Beacons near you:"} or {"Beacons near "..name..":"}
for spos,info in pairs(beacon.players[name].beacons) do
Expand Down
44 changes: 22 additions & 22 deletions effects.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@ local forget_time = beacon.config.cache_time
local function get_beacon_info(pos)
local meta = minetest.get_meta(pos)
if meta:get_string("active") ~= "true" then
return -- not active
return -- Not active
end
local range = meta:get_int("range")
if range < 1 then
return -- zero range
return -- Zero range
end
local effect = meta:get_string("effect")
if not beacon.effects[effect] then
return -- no effect
return -- No effect
end
return {
effect = effect,
range = range + 0.5, -- 0.5 to reach node edge
range = range + 0.5, -- 0.5 to reach node edge
owner = meta:get_string("owner"),
pos = pos,
timer = 0,
}
end

function beacon.mark_active(pos)
local spos = pos.x..","..pos.y..",".. pos.z
local spos = pos.x..","..pos.y..","..pos.z
active_beacons[spos] = get_beacon_info(pos)
end

function beacon.mark_inactive(pos)
local spos = pos.x..","..pos.y..",".. pos.z
local spos = pos.x..","..pos.y..","..pos.z
active_beacons[spos] = nil
end

function beacon.check_beacon(pos)
local spos = pos.x..","..pos.y..",".. pos.z
local spos = pos.x..","..pos.y..","..pos.z
local info = get_beacon_info(pos)
if info then
active_beacons[spos] = info
Expand All @@ -55,32 +55,32 @@ local function within_range(a, b, range)
end

local function update_beacons(players)
-- get player positions
-- Get player positions
local player_positions = {}
for _,player in pairs(players) do
player_positions[player:get_player_name()] = player:get_pos()
end
-- clear old beacons
-- Clear old beacons
for name in pairs(player_positions) do
if not beacon.players[name] then
beacon.players[name] = {beacons = {}, effects = {}}
else
beacon.players[name].beacons = {}
end
end
-- check active beacons
-- Check active beacons
for spos,info in pairs(active_beacons) do
local loaded = minetest.get_node_or_nil(info.pos)
local in_range = false
if info.timer >= 5 and loaded then
-- loaded but not updated by node timer, check it
-- Loaded but not updated by node timer, check it
info = get_beacon_info(info.pos)
active_beacons[spos] = info
else
info.timer = info.timer + 1
end
if info then
-- add to player beacons
-- Add to player beacons
for name,player_pos in pairs(player_positions) do
if within_range(info.pos, player_pos, info.range) then
beacon.players[name].beacons[spos] = {
Expand All @@ -94,7 +94,7 @@ local function update_beacons(players)
if not loaded and in_range then
info.timer = 0
elseif info.timer > forget_time then
-- unloaded and out of range too long, remove it
-- Unloaded and out of range too long, remove it
active_beacons[spos] = nil
end
end
Expand All @@ -103,15 +103,15 @@ end

local function get_useable_effects(name, pos)
local effects = {}
-- check each of the nearby beacons
-- Check each of the nearby beacons
for _,info in pairs(beacon.players[name].beacons) do
if not effects[info.effect] then
if info.owner == "" or beacon.can_effect(pos, info.owner) then
effects[info.effect] = true
end
end
end
-- clear any overridden effects
-- Clear any overridden effects
for effect in pairs(effects) do
if beacon.effects[effect].overrides then
for _,override in pairs(beacon.effects[effect].overrides) do
Expand All @@ -136,32 +136,32 @@ end
local timer = 0

minetest.register_globalstep(function(dtime)
-- update the timer
-- Update the timer
timer = timer + dtime
if timer < 1 then return end
timer = 0
-- update effects for all players
-- Update effects for all players
local players = minetest.get_connected_players()
update_beacons(players)
for _,player in pairs(players) do
local name = player:get_player_name()
local useable = get_useable_effects(name, player:get_pos())
local active = beacon.players[name].effects
-- check the player's effects
-- Check the player's effects
for id in pairs(get_all_effect_ids(active, useable)) do
-- remove effect
-- Remove effect
if active[id] and not useable[id] then
active[id] = nil
if beacon.effects[id].on_remove then
beacon.effects[id].on_remove(player, name)
end
-- add effect
-- Add effect
elseif useable[id] and not active[id] then
active[id] = true
if beacon.effects[id].on_apply then
beacon.effects[id].on_apply(player, name)
end
-- update effect
-- Update effect
else
if beacon.effects[id].on_step then
beacon.effects[id].on_step(player, name)
Expand All @@ -180,7 +180,7 @@ minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
if not beacon.players[name] then return end
for id in pairs(beacon.players[name].effects) do
-- remove all effects before leaving
-- Remove all effects before leaving
if beacon.effects[id].on_remove then
beacon.effects[id].on_remove(player, name)
end
Expand Down
4 changes: 2 additions & 2 deletions effects/fly.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ beacon.register_effect("fly", {
info = "Temporarily grants the \"fly\" privilage",
on_apply = function(player, name)
local privs = minetest.get_player_privs(name)
if privs.privs then return end -- don't effect admins
if privs.privs then return end -- Don't effect admins

if beacon.has_player_monoids then
player_monoids.fly:add_change(player, true, "beacon_fly")
Expand All @@ -18,7 +18,7 @@ beacon.register_effect("fly", {
end,
on_remove = function(player, name)
local privs = minetest.get_player_privs(name)
if privs.privs then return end -- don't effect admins
if privs.privs then return end -- Don't effect admins

if beacon.has_player_monoids then
player_monoids.fly:del_change(player, "beacon_fly")
Expand Down
2 changes: 1 addition & 1 deletion formspec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local base_formspec = ""
local function get_base_formspec()
if base_formspec == "" then
base_formspec =
"size[8,8]" ..
"size[8,8]"..
"label[0,0;Beacon Effect]"..
"label[4,0;Upgrades]"..
"list[context;beacon_upgrades;4,0.5;4,1;]"..
Expand Down
24 changes: 12 additions & 12 deletions functions.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

function beacon.on_place(itemstack, placer, pointed_thing)
-- check for correct 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
return itemstack, false
end
-- calculate param2 direction from pointed_thing
-- Calculate param2 direction from pointed_thing
local param2 = 0
local pointed_dir = vector.subtract(pointed_thing.above, pointed_thing.under)
if pointed_dir.x ~= 0 then
Expand All @@ -14,7 +14,7 @@ function beacon.on_place(itemstack, placer, pointed_thing)
elseif pointed_dir.z ~= 0 then
param2 = pointed_dir.z > 0 and 4 or 8
end
-- place beacon
-- Place beacon
return minetest.item_place(itemstack, placer, pointed_thing, param2)
end

Expand All @@ -23,32 +23,32 @@ function beacon.place_beam(pos, player_name, dir)
local offset = beacon.dir_to_vector[dir]
local param2 = beacon.dir_to_param2[dir]
local can_break_nodes = beacon.config.beam_break_nodes
-- place base
-- Place base
pos = vector.add(pos, offset)
minetest.add_node(pos, { name = node_name.."base", param2 = param2 })
-- place beam
minetest.add_node(pos, {name = node_name.."base", param2 = param2})
-- Place beam
for _=1, beacon.config.beam_length - 1 do
pos = vector.add(pos, offset)
if minetest.is_protected(pos, player_name) then return end
if not can_break_nodes then
if not beacon.is_airlike_node(pos) then return end
end
minetest.add_node(pos, { name = node_name.."beam", param2 = param2 })
minetest.add_node(pos, {name = node_name.."beam", param2 = param2})
end
end

function beacon.remove_beam(pos)
local dir = minetest.get_meta(pos):get_string("beam_dir")
if not dir or not beacon.dir_to_vector[dir] then
return -- invalid meta
return -- Invalid meta
end
local offset = beacon.dir_to_vector[dir]
-- remove beam (no need to remove beam base seperately)
-- Remove beam (no need to remove beam base seperately)
for _=1, beacon.config.beam_length do
pos = vector.add(pos, offset)
local node = beacon.get_node(pos)
if minetest.get_item_group(node.name, "beacon_beam") ~= 1 or beacon.param2_to_dir[node.param2] ~= dir then
return -- end of beam
return -- End of beam
end
minetest.set_node(pos, {name = "air"})
end
Expand Down Expand Up @@ -90,9 +90,9 @@ end

function beacon.update(pos)
if not beacon.check_beacon(pos) then
return true -- no particles when not granting any effect
return true -- No particles when not granting any effect
end
-- spawn active beacon particles
-- Spawn active beacon particles
local dir = minetest.get_meta(pos):get_string("beam_dir")
local colordef = beacon.colors[string.gsub(beacon.get_node(pos).name, "beacon:", "")]
if dir and beacon.dir_to_vector[dir] and colordef and colordef.color then
Expand Down
6 changes: 3 additions & 3 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ dofile(beacon.modpath.."/chatcommands.lua")
dofile(beacon.modpath.."/effects/init.lua")

minetest.after(0, function()
-- check if upgrade item is registered
-- Check if upgrade item is registered
if not minetest.registered_items[beacon.config.upgrade_item] then
beacon.config.upgrade_item = "default:diamondblock"
end
-- check if default effect is registered
-- Check if default effect is registered
if not beacon.effects[beacon.config.default_effect] then
beacon.config.default_effect = "none"
end
-- sort effect ids
-- Sort effect ids
for id in pairs(beacon.effects) do
table.insert(beacon.sorted_effect_ids, id)
end
Expand Down
Loading

0 comments on commit c8f1cd8

Please sign in to comment.