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

@ -43,6 +43,7 @@ mymod.speed_monoid = player_monoids.make_monoid({
for _, speed in pairs(tab) do
res = res * speed
end
return res
end,
identity = 1,
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.
newmod.speed_boosts = player_monoids.make_monoid({
combine = function(speed1, speed2)
return speed1 * speed2
return math.max(speed1, speed2)
end,
fold = function(tab)
local res = 1
for _, speed in pairs(tab) do
res = res * speed
res = math.max(res, speed)
end
return res
end,
identity = 1,
apply = ???

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,9 +59,9 @@ local function monoid(def)
return mon
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 def = self.def

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

View File

@ -4,6 +4,7 @@ local speed = player_monoids.speed
minetest.register_privilege("monoid_master", {
description = "Allows testing of player monoids.",
give_to_singleplayer = false,
give_to_admin = true,
})
local function test(player)
@ -13,6 +14,8 @@ local function test(player)
minetest.chat_send_player(p_name, "Your speed is: " .. speed:value(player))
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)
minetest.chat_send_player(p_name, "Your speed is: " .. speed:value(player))
end)