forked from minetest-mods/player_monoids
Add standard monoids and tests
This commit is contained in:
parent
21bf2c8a70
commit
ebdee72684
33
init.lua
33
init.lua
@ -4,7 +4,7 @@
|
||||
-- Any documentation here are internal details, please avoid using them in your
|
||||
-- mod.
|
||||
|
||||
local modpath = minetest.get_modpath(minetest.get_current_modname())
|
||||
local modpath = minetest.get_modpath(minetest.get_current_modname()) .. "/"
|
||||
|
||||
player_monoids = {}
|
||||
|
||||
@ -12,6 +12,8 @@ local mon_meta = {}
|
||||
|
||||
mon_meta.__index = mon_meta
|
||||
|
||||
local nop = function() end
|
||||
|
||||
-- A monoid object is a table with the following fields:
|
||||
-- def: The monoid definition
|
||||
-- player_map: A map from player names to their effect tables. Effect tables
|
||||
@ -22,6 +24,22 @@ mon_meta.__index = mon_meta
|
||||
local function monoid(def)
|
||||
local mon = {}
|
||||
|
||||
local actual_def = {}
|
||||
|
||||
for k, v in pairs(def) do
|
||||
actual_def[k] = v
|
||||
end
|
||||
|
||||
if not actual_def.apply then
|
||||
actual_def.apply = nop
|
||||
end
|
||||
|
||||
if not actual_def.on_change then
|
||||
actual_def.on_change = nop
|
||||
end
|
||||
|
||||
mon.def = actual_def
|
||||
|
||||
local p_map = {}
|
||||
mon.player_map = p_map
|
||||
|
||||
@ -30,7 +48,7 @@ local function monoid(def)
|
||||
local v_cache = {}
|
||||
mon.value_cache = v_cache
|
||||
|
||||
setmetatable(mon, mon_methods)
|
||||
setmetatable(mon, mon_meta)
|
||||
|
||||
minetest.register_on_leaveplayer(function(player)
|
||||
local p_name = player:get_player_name()
|
||||
@ -41,7 +59,7 @@ local function monoid(def)
|
||||
return mon
|
||||
end
|
||||
|
||||
player_monoids.make_monoid = monoid
|
||||
player_monoids.monoid = monoid
|
||||
|
||||
function mon_meta:add_change(player, value)
|
||||
local p_name = player:get_player_name()
|
||||
@ -67,14 +85,18 @@ function mon_meta:add_change(player, value)
|
||||
p_effects[actual_id] = value
|
||||
local new_total = def.fold(p_effects)
|
||||
self.value_cache[p_name] = new_total
|
||||
|
||||
|
||||
def.apply(new_total, player)
|
||||
def.on_change(old_total, new_total, player)
|
||||
|
||||
return actual_id
|
||||
end
|
||||
|
||||
function mon_meta:del_change(player, 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 return end
|
||||
|
||||
@ -91,3 +113,6 @@ function mon_meta:value(player)
|
||||
local p_name = player:get_player_name()
|
||||
return self.value_cache[p_name] or self.def.identity
|
||||
end
|
||||
|
||||
dofile(modpath .. "standard_monoids.lua")
|
||||
dofile(modpath .. "test.lua")
|
||||
|
Loading…
Reference in New Issue
Block a user