Imported from trollstream "ContentDB"

master
OldCoder 2022-09-04 22:03:06 -07:00
commit c504577227
4 changed files with 144 additions and 0 deletions

30
README.md Normal file
View File

@ -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

80
init.lua Normal file
View File

@ -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)

1
mod.conf Normal file
View File

@ -0,0 +1 @@
description = Timed World Modifiers, triggers on random nodes library

33
twmlib_api.txt Normal file
View File

@ -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:<name>" 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.