Compare commits
10 Commits
d76cde2dc1
...
8d6f1ade93
Author | SHA1 | Date | |
---|---|---|---|
|
8d6f1ade93 | ||
|
08bc018f92 | ||
|
ed11a7395b | ||
|
1e12f0e293 | ||
|
4df04550e3 | ||
|
ca996b5dcb | ||
|
668bce3772 | ||
|
481ef9e2cd | ||
|
cdb597629f | ||
|
ef6f07b6fa |
9
.luacheckrc
Normal file
9
.luacheckrc
Normal file
@ -0,0 +1,9 @@
|
||||
|
||||
read_globals = {
|
||||
"minetest",
|
||||
"vector",
|
||||
}
|
||||
|
||||
globals = {
|
||||
"player_monoids",
|
||||
}
|
16
README.md
16
README.md
@ -40,9 +40,10 @@ mymod.speed_monoid = player_monoids.make_monoid({
|
||||
end,
|
||||
fold = function(tab)
|
||||
local res = 1
|
||||
for _, speed in pairs(tab) do
|
||||
for _, speed in pairs(tab) do
|
||||
res = res * speed
|
||||
end
|
||||
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
|
||||
end
|
||||
for _, speed in pairs(tab) do
|
||||
res = math.max(res, speed)
|
||||
end
|
||||
return res
|
||||
end,
|
||||
identity = 1,
|
||||
apply = ???
|
||||
@ -202,4 +204,4 @@ global state.
|
||||
* 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
|
||||
in addition to associative, or at least not care if the order of two changes
|
||||
is swapped.
|
||||
is swapped.
|
||||
|
@ -1,2 +0,0 @@
|
||||
player_monoids is a library for managing global player state, such as physics
|
||||
overrides or player visual size.
|
6
init.lua
6
init.lua
@ -59,13 +59,13 @@ 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
|
||||
|
||||
|
||||
local p_effects = self.player_map[p_name]
|
||||
if p_effects == nil then
|
||||
p_effects = {}
|
||||
|
6
mod.conf
6
mod.conf
@ -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.
|
||||
"""
|
@ -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
|
||||
|
||||
|
5
test.lua
5
test.lua
@ -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)
|
||||
@ -11,8 +12,10 @@ local function test(player)
|
||||
local p_name = player:get_player_name()
|
||||
|
||||
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user