From c504577227409218aa80722dc1b222e2bb74efa0 Mon Sep 17 00:00:00 2001 From: OldCoder Date: Sun, 4 Sep 2022 22:03:06 -0700 Subject: [PATCH] Imported from trollstream "ContentDB" --- README.md | 30 +++++++++++++++++++ init.lua | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ mod.conf | 1 + twmlib_api.txt | 33 +++++++++++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 README.md create mode 100644 init.lua create mode 100644 mod.conf create mode 100644 twmlib_api.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..974ce76 --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +Timed World Modifiers library +============================= + +* Version 0.1 +* Licence LGPLv2 or, at your discretion, any later version. + +Twmlib (Timed World Modifier) adds a kind of environmental ABM that could be triggered on a very wide range of nodes but only a few at a time. +See twmlib_api.txt for documentation. + + +Licence +------- + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +https://www.gnu.org/licenses/licenses.html#LGPL + + +Authors of source code +---------------------- + +Karamel diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..cc9e0d7 --- /dev/null +++ b/init.lua @@ -0,0 +1,80 @@ + +twmlib = {} +twmlib.registered_twm = {} +function twmlib.next_interval() + return tonumber(minetest.settings:get("twmlib.interval")) or 2 +end +local mod_path = minetest.get_modpath(minetest.get_current_modname()) + +function twmlib.register_twm(twm_definition) + for _, nodename in ipairs(twm_definition.nodenames) do + if twmlib.registered_twm[nodename] == nil then + twmlib.registered_twm[nodename] = {} + end + table.insert(twmlib.registered_twm[nodename], twm_definition) + end +end + + +local range = (minetest.settings:get("active_block_range") or 3) * 16 +local function loop() + -- Take one player at random + local players = minetest.get_connected_players() + if #players == 0 then return end -- players not loaded yet + local picked = players[math.random(1, #players)] + -- Take a random position around + local pos = picked:get_pos() + if not pos then return end -- player not positioned yet + pos.y = pos.y + math.random(-range, range) + pos.x = pos.x + math.random(-range, range) + pos.z = pos.z + math.random(-range, range) + local rand_pos = vector.new(pos.x, pos.y, pos.z) + local node = minetest.get_node(rand_pos) + if node.name == "air" then + -- go up or down a few times until something is found + local dy = (math.random() > 0.5) + if dy then + dy = 1 + else + dy = -1 + end + local i = 0 + while i < range / 2 and node.name == "air" do + rand_pos = vector.add(rand_pos, vector.new(0, dy, 0)) + node = minetest.get_node(rand_pos) + i = i + 1 + end + end + if node.name == "air" or node.name == "ignore" then return end + -- look for a registered twm for the node or group + if twmlib.registered_twm[node.name] then + -- trigger twm by node + for _, def in ipairs(twmlib.registered_twm[node.name]) do + if def.chance then + if math.random(1, def.chance) == 1 then + def.action(rand_pos, node) + end + else + def.action(rand_pos, node) + end + end + end + local node_def = minetest.registered_nodes[node.name] + for group, level in ipairs(node_def.groups) do + local str_group = "group:" .. group + if twmlib.registered_twm[str_group] then + -- trigger twm by group + for _, def in ipairs(twmlib.registered_twm[str_group]) do + if def.chance then + if math.random(1, def.chance) == 1 then + def.action(rand_pos, node) + end + else + def.action(rand_pos, node) + end + end + end + end + minetest.after(twmlib.next_interval(), loop) +end +minetest.after(twmlib.next_interval(), loop) diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..d59c36d --- /dev/null +++ b/mod.conf @@ -0,0 +1 @@ +description = Timed World Modifiers, triggers on random nodes library diff --git a/twmlib_api.txt b/twmlib_api.txt new file mode 100644 index 0000000..092b08a --- /dev/null +++ b/twmlib_api.txt @@ -0,0 +1,33 @@ +Twmlib Lua API +============== + +Table of contents +-- Introduction +-- API +---- twmlib.register_twm +-- TWM definition + + +Introduction +------------ + +Twmlib (Timed World Modifier) adds a kind of environmental ABM that could be triggered on a very wide range of nodes but only a few at a time. + +On a regular and rather short interval, a random node is picked around a player. If this node has a registered TWM, it can be triggered. + + +API +--- + +### twmlib.register_twm(twm_definition) + +* `twm_definition` see below + +TWM definition +-------------- + +A TWM definition is like an ABM definition. + +* `nodenames` a table of node names, that can include groups with "group:" rather than simple node name. +* `chance` optional inverted chance of the TWM to be triggered. Default it 1. +* `action` the function(pos, node) to be called when the TWM is triggered.