From 8442513208b0980053baa9e503d05248628b7a68 Mon Sep 17 00:00:00 2001 From: HybridDog Date: Tue, 28 Jul 2020 20:59:02 +0200 Subject: [PATCH] Add a few functions to be used by other mods --- README.md | 1 - api.md | 15 +++++++++++++++ init.lua | 39 +++++++++++++++++++++++---------------- 3 files changed, 38 insertions(+), 17 deletions(-) create mode 100644 api.md diff --git a/README.md b/README.md index c02a16e..98a5533 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,3 @@ For a description of this Minetest mod, please refer to TODO: * Take inventory items -* API diff --git a/api.md b/api.md new file mode 100644 index 0000000..b7836b4 --- /dev/null +++ b/api.md @@ -0,0 +1,15 @@ +# cave_lighting API + +This mod currently exposes only a few functions. +See also the explanation in the main mod description. + +* `cave_lighting.light_cave(player, maxlight)` + lights up a dark area, e.g. a cave, with the player's wield item; + maxlight specifies the maximum light value for positions in which a light + source node is placed. + Returns a success boolean and a message +* `cave_lighting.enable_auto_placing(player_name, maxlight)` + Enables the automatic light placing for the player, + or sets the maxlight to a different value +* `cave_lighting.disable_auto_placing(player_name)` + Disables the automatic light placing for the player diff --git a/init.lua b/init.lua index 82fec93..dec33bb 100644 --- a/init.lua +++ b/init.lua @@ -1,7 +1,8 @@ - local path = minetest.get_modpath"cave_lighting" local search_dfs = dofile(path .. "/fill_3d.lua") +cave_lighting = {} + local function inform(name, msg) minetest.chat_send_player(name, msg) minetest.log("info", "[cave_lighting] "..name..": "..msg) @@ -99,7 +100,7 @@ local function search_positions(startpos, maxlight, pname, max_positions) end -- Lights up a cave -local function place_torches(pos, maxlight, player, name) +local function place_torches(pos, maxlight, player) -- Get the light_source item local inv = player:get_inventory() local wi = player:get_wield_index() @@ -211,8 +212,7 @@ local function get_pointed_target(player) return pointed.above, pointed.under end --- Searches the position the player looked at and lights the cave -local function light_cave(player, maxlight) +function cave_lighting.light_cave(player, maxlight) local pos = get_pointed_target(player) if not pos then return false, "No valid position for a torch placement found" @@ -240,7 +240,7 @@ minetest.register_chatcommand("light_cave",{ if not player then return false, "Player not found" end - return light_cave(player, tonumber(param) or 7) + return cave_lighting.light_cave(player, tonumber(param) or 7) end }) @@ -249,6 +249,18 @@ minetest.register_chatcommand("light_cave",{ local light_making_players +function cave_lighting.enable_auto_placing(pname, maxlight) + light_making_players = light_making_players or {} + light_making_players[pname] = maxlight +end + +function cave_lighting.disable_auto_placing(pname) + light_making_players[pname] = nil + if not next(light_making_players) then + light_making_players = nil + end +end + -- Chatcommand to automatically light the way while playing minetest.register_chatcommand("auto_light_placing",{ description = "automatically places lights", @@ -259,9 +271,7 @@ minetest.register_chatcommand("auto_light_placing",{ if not player then return false, "Player not found" end - light_making_players = light_making_players or {} - light_making_players[name] = tonumber(param) or 3 - timer = -0.5 + cave_lighting.enable_auto_placing(name, tonumber(param) or 3) return true, "Placing lights automatically" end }) @@ -272,8 +282,8 @@ local function autoplace_step() return end - for name, maxlight in pairs(light_making_players) do - local player = minetest.get_player_by_name(name) + for pname, maxlight in pairs(light_making_players) do + local player = minetest.get_player_by_name(pname) local pt = {type = "node"} pt.above, pt.under = get_pointed_target(player) if pt.above then @@ -287,7 +297,7 @@ local function autoplace_step() player:get_wield_index()+1):get_name() data = minetest.registered_nodes[node] if not data then - inform(name, "You need to have a node next to or as " .. + inform(pname, "You need to have a node next to or as " .. "your wielded item.") failed = true end @@ -296,7 +306,7 @@ local function autoplace_step() local nodelight = data.light_source if not nodelight or nodelight < maxlight then - inform(name, + inform(pname, "You need a node emitting light (enough light).") failed = true end @@ -315,10 +325,7 @@ local function autoplace_step() end end if failed then - light_making_players[name] = nil - if not next(light_making_players) then - light_making_players = nil - end + cave_lighting.disable_auto_placing(pname) end end end