ATTRIB_D = {}
setmetatable(ATTRIB_D, {__index = _G})
local _ENV = ATTRIB_D
attrib_formula = {}
attrib_max_list = {}
function get_max_attrib(ob_type, key)
local attrib_max = attrib_max_list[ob_type] or {}
return attrib_max[key]
end
function query_attrib(ob, attrib)
local ob_type = ob:query("ob_type")
local attrib_info = attrib_formula[ob_type]
if not attrib_info then
return (ob:query(attrib))
end
local formula = attrib_info[attrib]
if not formula then
return (ob:query(attrib))
end
return (formula(ob, attrib))
end
function add_attrib(ob, field, value)
if not is_object(ob) or value == 0 then
return false
end
if field == "money" then
if not ob or ob:query("ob_type") ~= OB_TYPE_USER then
return false
end
end
local value = math.floor(value)
local cur_value = ob:query(field)
if not cur_value then
cur_value = 0
end
local final_value = value + math.floor(cur_value)
local attrib_max = attrib_max_list[ob:query("ob_type")] or {}
if field == "exp" and not attrib_max["lv"] then
if attrib_max["lv"] <= ob:query("lv") then
return false
end
end
if attrib_max[field] and final_value > attrib_max[field] then
final_value = attrib_max[field]
end
ob:set(field, final_value)
ob:notify_fields_updated(field)
if field == "exp" then
raise_issue(EVENT_EXP_CHANGE, ob)
end
return true
end
function cost_attrib(ob, field, value)
if not is_object(ob) then
return false
end
local value = math.ceil(value)
local cur_value = ob:query(field)
if not cur_value or cur_value - value < 0 or value == 0 then
return false
end
local final_value = cur_value - value
ob:set(field, final_value)
ob:notify_fields_updated(field)
if field == "gold" then
raise_issue(EVENT_GOLD_COST, ob, -1*value)
elseif field == "stone" then
raise_issue(EVENT_STONE_COST, ob, -1*value)
elseif field == "sp" then
raise_issue(EVENT_PHY_COST, ob, -1*value)
end
return true
end
function create()
local data = IMPORT_D.readcsv_to_tables("data/txt/attrib_formula.txt")
local attrib, ob_type
for _, info in ipairs(data) do
attrib = info["attrib"]
info["ob_type"] = _G[info["ob_type"]]
ob_type = info["ob_type"]
if not is_mapping(attrib_formula[ob_type]) then
attrib_formula[ob_type] = {}
end
attrib_formula[ob_type][attrib] = _G[info["formula"]]
end
local max_list = IMPORT_D.readcsv_to_tables("data/txt/attrib_max.txt")
for _, info in pairs(max_list) do
info["ob_type"] = _G[info["ob_type"]]
attrib_max_list[info.ob_type] = attrib_max_list[info.ob_type] or {}
attrib_max_list[info.ob_type][info.name] = info.max
end
end
create()