2016-05-26 16:58:23 +02:00
|
|
|
-- Copyright (c) raymoo 2016
|
|
|
|
-- Licensed under Apache 2.0 license. See COPYING for details.
|
|
|
|
|
|
|
|
-- Any documentation here are internal details, please avoid using them in your
|
|
|
|
-- mod.
|
|
|
|
|
2016-05-26 18:30:25 +02:00
|
|
|
local modpath = minetest.get_modpath(minetest.get_current_modname())
|
2016-05-26 16:58:23 +02:00
|
|
|
|
2016-05-26 18:30:25 +02:00
|
|
|
player_monoids = {}
|
2016-05-26 16:58:23 +02:00
|
|
|
|
|
|
|
local mon_meta = {}
|
|
|
|
|
|
|
|
mon_meta.__index = mon_meta
|
|
|
|
|
|
|
|
-- A monoid object is a table with the following fields:
|
2016-05-28 01:25:10 +02:00
|
|
|
-- def: The monoid definition
|
2016-05-26 16:58:23 +02:00
|
|
|
-- player_map: A map from player names to their effect tables. Effect tables
|
|
|
|
-- are maps from effect IDs to values.
|
|
|
|
-- value_cache: A map from player names to the cached value for the monoid.
|
|
|
|
-- next_id: The next unique ID to assign an effect.
|
|
|
|
|
|
|
|
local function monoid(def)
|
2016-05-28 01:25:10 +02:00
|
|
|
local mon = {}
|
2016-05-26 16:58:23 +02:00
|
|
|
|
2016-05-26 18:30:25 +02:00
|
|
|
local p_map = {}
|
|
|
|
mon.player_map = p_map
|
2016-05-26 16:58:23 +02:00
|
|
|
|
2016-05-28 01:25:10 +02:00
|
|
|
mon.next_id = 1
|
2016-05-26 16:58:23 +02:00
|
|
|
|
2016-05-26 18:30:25 +02:00
|
|
|
local v_cache = {}
|
|
|
|
mon.value_cache = v_cache
|
|
|
|
|
2016-05-28 01:25:10 +02:00
|
|
|
setmetatable(mon, mon_methods)
|
2016-05-26 16:58:23 +02:00
|
|
|
|
2016-05-26 18:30:25 +02:00
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
local p_name = player:get_player_name()
|
|
|
|
p_map[p_name] = nil
|
|
|
|
v_cache[p_name] = nil
|
|
|
|
end)
|
|
|
|
|
2016-05-28 01:25:10 +02:00
|
|
|
return mon
|
2016-05-26 16:58:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
player_monoids.make_monoid = monoid
|
|
|
|
|
|
|
|
function mon_meta:add_change(player, value)
|
2016-05-28 01:25:10 +02:00
|
|
|
local p_name = player:get_player_name()
|
|
|
|
|
|
|
|
local def = self.def
|
2016-05-26 16:58:23 +02:00
|
|
|
|
2016-05-28 01:25:10 +02:00
|
|
|
local p_effects = self.player_map[p_name]
|
|
|
|
if p_effects == nil then
|
|
|
|
p_effects = {}
|
|
|
|
self.player_map[p_name] = p_effects
|
|
|
|
end
|
|
|
|
|
|
|
|
local actual_id
|
|
|
|
|
|
|
|
if id then
|
|
|
|
actual_id = id
|
|
|
|
else
|
|
|
|
actual_id = self.next_id
|
|
|
|
self.next_id = actual_id + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
local old_total = self.value_cache[p_name]
|
|
|
|
p_effects[actual_id] = value
|
|
|
|
local new_total = def.fold(p_effects)
|
|
|
|
self.value_cache[p_name] = new_total
|
2016-05-26 16:58:23 +02:00
|
|
|
|
2016-05-28 01:25:10 +02:00
|
|
|
def.apply(new_total, player)
|
|
|
|
def.on_change(old_total, new_total, player)
|
2016-05-26 16:58:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function mon_meta:del_change(player, id)
|
2016-05-28 01:25:10 +02:00
|
|
|
local p_name = player:get_player_name()
|
2016-05-26 16:58:23 +02:00
|
|
|
|
2016-05-28 01:25:10 +02:00
|
|
|
local p_effects = self.player_map[p_name]
|
|
|
|
if p_effects == nil then return end
|
2016-05-26 16:58:23 +02:00
|
|
|
|
2016-05-28 01:25:10 +02:00
|
|
|
local old_total = self.value_cache[p_name]
|
|
|
|
p_effects[id] = nil
|
|
|
|
local new_total = def.fold(p_effects)
|
|
|
|
self.value_cache[p_name] = new_total
|
2016-05-26 16:58:23 +02:00
|
|
|
|
2016-05-28 01:25:10 +02:00
|
|
|
def.apply(new_total, player)
|
|
|
|
def.on_change(old_total, new_total, player)
|
2016-05-26 16:58:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function mon_meta:value(player)
|
2016-05-28 01:25:10 +02:00
|
|
|
local p_name = player:get_player_name()
|
|
|
|
return self.value_cache[p_name] or self.def.identity
|
2016-05-26 16:58:23 +02:00
|
|
|
end
|