Compare commits

...

10 Commits

Author SHA1 Message Date
celeron55
8d6f1ade93
README.md: Return from example fold methods (#8) 2024-08-29 14:35:28 +02:00
Raymoo
08bc018f92 Don't save player ref after callback 2022-03-05 23:16:01 -06:00
raymoo
ed11a7395b
Merge pull request #6 from Panquesito7/master
Use mod.conf for description / remove extra whitespace / give monoid_master privilege to admin
2019-10-17 21:43:18 +00:00
Panquesito7
1e12f0e293
Give privilege automatically to admin
The 'monoid_master' privilege is given to admin automatically.
2019-10-17 13:37:03 -05:00
Panquesito7
4df04550e3
Remove empty lines 2019-10-17 13:35:10 -05:00
Panquesito7
ca996b5dcb
Use mod.conf for description
Deletes deprecated description.txt
2019-10-17 13:31:16 -05:00
Raymoo
668bce3772 Fix luacheck errors, thanks to kaeza 2017-03-13 18:55:14 -07:00
Raymoo
481ef9e2cd Fix missing argument 2017-03-11 11:33:47 -08:00
raymoo
cdb597629f Fix discrepancy between docs and actual functions 2016-08-05 13:45:44 -07:00
raymoo
ef6f07b6fa Fix readme nested monoids example 2016-06-14 17:01:53 -07:00
7 changed files with 47 additions and 55 deletions

9
.luacheckrc Normal file
View File

@ -0,0 +1,9 @@
read_globals = {
"minetest",
"vector",
}
globals = {
"player_monoids",
}

View File

@ -40,9 +40,10 @@ mymod.speed_monoid = player_monoids.make_monoid({
end, end,
fold = function(tab) fold = function(tab)
local res = 1 local res = 1
for _, speed in pairs(tab) do for _, speed in pairs(tab) do
res = res * speed res = res * speed
end end
return res
end, end,
identity = 1, identity = 1,
apply = function(speed, player) apply = function(speed, player)
@ -104,13 +105,14 @@ the strongest boost. Most of it could be done the same way:
-- My speed boosts monoid takes speed multipliers (numbers) that are at least 1. -- My speed boosts monoid takes speed multipliers (numbers) that are at least 1.
newmod.speed_boosts = player_monoids.make_monoid({ newmod.speed_boosts = player_monoids.make_monoid({
combine = function(speed1, speed2) combine = function(speed1, speed2)
return speed1 * speed2 return math.max(speed1, speed2)
end, end,
fold = function(tab) fold = function(tab)
local res = 1 local res = 1
for _, speed in pairs(tab) do for _, speed in pairs(tab) do
res = res * speed res = math.max(res, speed)
end end
return res
end, end,
identity = 1, identity = 1,
apply = ??? apply = ???
@ -202,4 +204,4 @@ global state.
* The order that different effects get combined together is based on key order, * The order that different effects get combined together is based on key order,
which may not be predictable. So you should try to make your monoids commutative which may not be predictable. So you should try to make your monoids commutative
in addition to associative, or at least not care if the order of two changes in addition to associative, or at least not care if the order of two changes
is swapped. is swapped.

View File

@ -1,2 +0,0 @@
player_monoids is a library for managing global player state, such as physics
overrides or player visual size.

View File

@ -59,13 +59,13 @@ local function monoid(def)
return mon return mon
end end
player_monoids.monoid = monoid player_monoids.make_monoid = monoid
function mon_meta:add_change(player, value) function mon_meta:add_change(player, value, id)
local p_name = player:get_player_name() local p_name = player:get_player_name()
local def = self.def local def = self.def
local p_effects = self.player_map[p_name] local p_effects = self.player_map[p_name]
if p_effects == nil then if p_effects == nil then
p_effects = {} p_effects = {}

View File

@ -1 +1,5 @@
name=player_monoids name = player_monoids
description = """
A library for managing global player state,
such as physics overrides or player visual size.
"""

View File

@ -5,7 +5,7 @@ local function mult(x, y) return x * y end
local function mult_fold(elems) local function mult_fold(elems)
local tot = 1 local tot = 1
for k,v in pairs(elems) do for _, v in pairs(elems) do
tot = tot * v tot = tot * v
end end
@ -26,7 +26,7 @@ local function v_mult_fold(identity)
return function(elems) return function(elems)
local tot = identity local tot = identity
for k, v in pairs(elems) do for _, v in pairs(elems) do
tot = v_mult(tot, v) tot = v_mult(tot, v)
end end
@ -34,24 +34,17 @@ local function v_mult_fold(identity)
end end
end end
local monoid = player_monoids.monoid local monoid = player_monoids.make_monoid
-- Speed monoid. Effect values are speed multipliers. Must be nonnegative -- Speed monoid. Effect values are speed multipliers. Must be nonnegative
-- numbers. -- numbers.
player_monoids.speed = monoid({ player_monoids.speed = monoid({
combine = function(x, y) return x * y end, combine = mult,
fold = function(elems) fold = mult_fold,
local res = 1
for k, v in pairs(elems) do
res = res * v
end
return res
end,
identity = 1, identity = 1,
apply = function(mult, player) apply = function(multiplier, player)
local ov = player:get_physics_override() local ov = player:get_physics_override()
ov.speed = mult ov.speed = multiplier
player:set_physics_override(ov) player:set_physics_override(ov)
end, end,
}) })
@ -60,50 +53,34 @@ player_monoids.speed = monoid({
-- Jump monoid. Effect values are jump multipliers. Must be nonnegative -- Jump monoid. Effect values are jump multipliers. Must be nonnegative
-- numbers. -- numbers.
player_monoids.jump = monoid({ player_monoids.jump = monoid({
combine = function(x, y) return x * y end, combine = mult,
fold = function(elems) fold = mult_fold,
local res = 1
for k, v in pairs(elems) do
res = res * v
end
return res
end,
identity = 1, identity = 1,
apply = function(mult, player) apply = function(multiplier, player)
local ov = player:get_physics_override() local ov = player:get_physics_override()
ov.jump = mult ov.jump = multiplier
player:set_physics_override(ov) player:set_physics_override(ov)
end, end,
}) })
-- Gravity monoid. Effect values are gravity multipliers. -- Gravity monoid. Effect values are gravity multipliers.
player_monoids.gravity = monoid({ player_monoids.gravity = monoid({
combine = function(x, y) return x * y end, combine = mult,
fold = function(elems) fold = mult_fold,
local res = 1
for k, v in pairs(elems) do
res = res * v
end
return res
end,
identity = 1, identity = 1,
apply = function(mult, player) apply = function(multiplier, player)
local ov = player:get_physics_override() local ov = player:get_physics_override()
ov.gravity = mult ov.gravity = multiplier
player:set_physics_override(ov) player:set_physics_override(ov)
end, end,
}) })
-- Fly ability monoid. The values are booleans, which are combined by or. A true -- Fly ability monoid. The values are booleans, which are combined by or. A true
-- value indicates having the ability to fly. -- value indicates having the ability to fly.
player_monoids.fly = monoid({ player_monoids.fly = monoid({
combine = function(p, q) return p or q end, combine = function(p, q) return p or q end,
fold = function(elems) fold = function(elems)
for k, v in pairs(elems) do for _, v in pairs(elems) do
if v then return true end if v then return true end
end end
@ -125,12 +102,11 @@ player_monoids.fly = monoid({
end, end,
}) })
-- Noclip ability monoid. Works the same as fly monoid. -- Noclip ability monoid. Works the same as fly monoid.
player_monoids.noclip = monoid({ player_monoids.noclip = monoid({
combine = function(p, q) return p or q end, combine = function(p, q) return p or q end,
fold = function(elems) fold = function(elems)
for k, v in pairs(elems) do for _, v in pairs(elems) do
if v then return true end if v then return true end
end end

View File

@ -4,6 +4,7 @@ local speed = player_monoids.speed
minetest.register_privilege("monoid_master", { minetest.register_privilege("monoid_master", {
description = "Allows testing of player monoids.", description = "Allows testing of player monoids.",
give_to_singleplayer = false, give_to_singleplayer = false,
give_to_admin = true,
}) })
local function test(player) local function test(player)
@ -11,8 +12,10 @@ local function test(player)
local p_name = player:get_player_name() local p_name = player:get_player_name()
minetest.chat_send_player(p_name, "Your speed is: " .. speed:value(player)) minetest.chat_send_player(p_name, "Your speed is: " .. speed:value(player))
minetest.after(3, function() minetest.after(3, function()
local player = minetest.get_player_by_name(p_name)
if not player then return end
speed:del_change(player, ch_id) speed:del_change(player, ch_id)
minetest.chat_send_player(p_name, "Your speed is: " .. speed:value(player)) minetest.chat_send_player(p_name, "Your speed is: " .. speed:value(player))
end) end)