mirror of
https://github.com/minetest-mods/player_monoids.git
synced 2025-01-09 17:10:19 +01:00
Fix def missing and spacetabs
This commit is contained in:
parent
025f1d398a
commit
de25711530
71
init.lua
71
init.lua
@ -13,23 +13,24 @@ local mon_meta = {}
|
|||||||
mon_meta.__index = mon_meta
|
mon_meta.__index = mon_meta
|
||||||
|
|
||||||
-- A monoid object is a table with the following fields:
|
-- 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
|
-- player_map: A map from player names to their effect tables. Effect tables
|
||||||
-- are maps from effect IDs to values.
|
-- are maps from effect IDs to values.
|
||||||
-- value_cache: A map from player names to the cached value for the monoid.
|
-- value_cache: A map from player names to the cached value for the monoid.
|
||||||
-- next_id: The next unique ID to assign an effect.
|
-- next_id: The next unique ID to assign an effect.
|
||||||
|
|
||||||
local function monoid(def)
|
local function monoid(def)
|
||||||
local mon = {}
|
local mon = {}
|
||||||
|
|
||||||
local p_map = {}
|
local p_map = {}
|
||||||
mon.player_map = p_map
|
mon.player_map = p_map
|
||||||
|
|
||||||
mon.next_id = 1
|
mon.next_id = 1
|
||||||
|
|
||||||
local v_cache = {}
|
local v_cache = {}
|
||||||
mon.value_cache = v_cache
|
mon.value_cache = v_cache
|
||||||
|
|
||||||
setmetatable(mon, mon_methods)
|
setmetatable(mon, mon_methods)
|
||||||
|
|
||||||
minetest.register_on_leaveplayer(function(player)
|
minetest.register_on_leaveplayer(function(player)
|
||||||
local p_name = player:get_player_name()
|
local p_name = player:get_player_name()
|
||||||
@ -37,54 +38,56 @@ local function monoid(def)
|
|||||||
v_cache[p_name] = nil
|
v_cache[p_name] = nil
|
||||||
end)
|
end)
|
||||||
|
|
||||||
return mon
|
return mon
|
||||||
end
|
end
|
||||||
|
|
||||||
player_monoids.make_monoid = monoid
|
player_monoids.make_monoid = monoid
|
||||||
|
|
||||||
function mon_meta:add_change(player, value)
|
function mon_meta:add_change(player, value)
|
||||||
local p_name = player:get_player_name()
|
local p_name = player:get_player_name()
|
||||||
|
|
||||||
|
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 = {}
|
||||||
self.player_map[p_name] = p_effects
|
self.player_map[p_name] = p_effects
|
||||||
end
|
end
|
||||||
|
|
||||||
local actual_id
|
local actual_id
|
||||||
|
|
||||||
if id then
|
if id then
|
||||||
actual_id = id
|
actual_id = id
|
||||||
else
|
else
|
||||||
actual_id = self.next_id
|
actual_id = self.next_id
|
||||||
self.next_id = actual_id + 1
|
self.next_id = actual_id + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
local old_total = self.value_cache[p_name]
|
local old_total = self.value_cache[p_name]
|
||||||
p_effects[actual_id] = value
|
p_effects[actual_id] = value
|
||||||
local new_total = self.fold(p_effects)
|
local new_total = def.fold(p_effects)
|
||||||
self.value_cache[p_name] = new_total
|
self.value_cache[p_name] = new_total
|
||||||
|
|
||||||
self.apply(new_total, player)
|
def.apply(new_total, player)
|
||||||
self.on_change(old_total, new_total, player)
|
def.on_change(old_total, new_total, player)
|
||||||
end
|
end
|
||||||
|
|
||||||
function mon_meta:del_change(player, id)
|
function mon_meta:del_change(player, id)
|
||||||
local p_name = player:get_player_name()
|
local p_name = player:get_player_name()
|
||||||
|
|
||||||
local p_effects = self.player_map[p_name]
|
local p_effects = self.player_map[p_name]
|
||||||
if p_effects == nil then return end
|
if p_effects == nil then return end
|
||||||
|
|
||||||
local old_total = self.value_cache[p_name]
|
local old_total = self.value_cache[p_name]
|
||||||
p_effects[id] = nil
|
p_effects[id] = nil
|
||||||
local new_total = self.fold(p_effects)
|
local new_total = def.fold(p_effects)
|
||||||
self.value_cache[p_name] = new_total
|
self.value_cache[p_name] = new_total
|
||||||
|
|
||||||
self.apply(new_total, player)
|
def.apply(new_total, player)
|
||||||
self.on_change(old_total, new_total, player)
|
def.on_change(old_total, new_total, player)
|
||||||
end
|
end
|
||||||
|
|
||||||
function mon_meta:value(player)
|
function mon_meta:value(player)
|
||||||
local p_name = player:get_player_name()
|
local p_name = player:get_player_name()
|
||||||
return self.value_cache[p_name] or self.identity
|
return self.value_cache[p_name] or self.def.identity
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user