
This one is for player achievements gaining (made it quite hackish

and dumping them in character dump.
if you want the player achievements in the Actor.lua of the engine, or somewhere else more module oriented, i'm all in for it.
The patch also cnotains a small fix for object.lua getTextualDesc (fixing a previous mistake of mine)
Code: Select all
Index: engine/interface/WorldAchievements.lua
===================================================================
--- engine/interface/WorldAchievements.lua (revision 906)
+++ engine/interface/WorldAchievements.lua (working copy)
@@ -27,86 +27,102 @@
--- Loads achievements
function _M:loadDefinition(dir)
- for i, file in ipairs(fs.list(dir)) do
- if file:find("%.lua$") then
- local f, err = loadfile(dir.."/"..file)
- if not f and err then error(err) end
- setfenv(f, setmetatable({
- newAchievement = function(t) self:newAchievement(t) end,
- }, {__index=_G}))
- f()
- end
- end
+ for i, file in ipairs(fs.list(dir)) do
+ if file:find("%.lua$") then
+ local f, err = loadfile(dir.."/"..file)
+ if not f and err then error(err) end
+ setfenv(f, setmetatable({
+ newAchievement = function(t) self:newAchievement(t) end,
+ }, {__index=_G}))
+ f()
+ end
+ end
end
--- Make a new achivement with a name and desc
function _M:newAchievement(t)
- assert(t.name, "no achivement name")
- assert(t.desc, "no achivement desc")
+ assert(t.name, "no achivement name")
+ assert(t.desc, "no achivement desc")
- t.mode = t.mode or "none"
- t.id = t.id or t.name
- t.id = t.id:upper():gsub("[ ]", "_")
- t.order = #self.achiev_defs+1
+ t.mode = t.mode or "none"
+ t.id = t.id or t.name
+ t.id = t.id:upper():gsub("[ ]", "_")
+ t.order = #self.achiev_defs+1
- self.achiev_defs[t.id] = t
- self.achiev_defs[#self.achiev_defs+1] = t
- print("[ACHIEVEMENT] defined", t.order, t.id)
+ self.achiev_defs[t.id] = t
+ self.achiev_defs[#self.achiev_defs+1] = t
+ print("[ACHIEVEMENT] defined", t.order, t.id)
end
function _M:loadAchievements()
- self.achieved = {}
+ self.achieved = {}
+ self.playerachieved = {}
- for k, e in pairs(profile.mod) do
- if k:find('^achievement%.') then
- local id = k:gsub('^achievement%.', '')
- if self.achiev_defs[id] then
- self.achieved[id] = e
- end
- end
- end
+ for k, e in pairs(profile.mod) do
+ if k:find('^achievement%.') then
+ local id = k:gsub('^achievement%.', '')
+ if self.achiev_defs[id] then
+ self.achieved[id] = e
+ end
+ end
+ end
end
function _M:getAchievementFromId(id)
- return self.achiev_defs[id]
+ return self.achiev_defs[id]
end
+
+--- Gain Personal achievement for player only
+-- @param id the achivement to gain
+-- @param src who did it
+function _M:gainPersonalAchievement(id, src, a, ...)
+ if self.playerachieved[id] then return end
+
+ self.playerachieved[id] = {turn=game.turn, when=os.date("%Y-%m-%d %H:%M:%S")}
+ game.log("#LIGHT_GREEN#Personal New Achievement: %s!", a.name)
+ Dialog:simplePopup("Personal New Achievement: #LIGHT_GREEN#"..a.name, a.desc)
+end
+
--- Gain an achievement
-- @param id the achivement to gain
-- @param src who did it
function _M:gainAchievement(id, src, ...)
- local a = self.achiev_defs[id]
- if not a then error("Unknown achievement "..id) return end
- if self.achieved[id] then return end
+ local a = self.achiev_defs[id]
+ if not a then error("Unknown achievement "..id) return end
- if a.can_gain then
- local data = nil
- if a.mode == "world" then
- self.achievement_data = self.achievement_data or {}
- self.achievement_data[id] = self.achievement_data[id] or {}
- data = self.achievement_data[id]
- elseif a.mode == "game" then
- game.achievement_data = game.achievement_data or {}
- game.achievement_data[id] = game.achievement_data[id] or {}
- data = game.achievement_data[id]
- elseif a.mode == "player" then
- src.achievement_data = src.achievement_data or {}
- src.achievement_data[id] = src.achievement_data[id] or {}
- data = src.achievement_data[id]
- end
- if not a.can_gain(data, src, ...) then return end
- end
+ if self.achieved[id] and self.playerachieved[id] then return end
+
+ if a.can_gain then
+ local data = nil
+ if a.mode == "world" then
+ self.achievement_data = self.achievement_data or {}
+ self.achievement_data[id] = self.achievement_data[id] or {}
+ data = self.achievement_data[id]
+ elseif a.mode == "game" then
+ game.achievement_data = game.achievement_data or {}
+ game.achievement_data[id] = game.achievement_data[id] or {}
+ data = game.achievement_data[id]
+ elseif a.mode == "player" then
+ src.achievement_data = src.achievement_data or {}
+ src.achievement_data[id] = src.achievement_data[id] or {}
+ data = src.achievement_data[id]
+ end
+ if not a.can_gain(data, src, data, ...) then return end
+ end
+
+ if self.achieved[id] then self:gainPersonalAchievement(id, src, a, ...) return end
- self.achieved[id] = {turn=game.turn, who=self:achievementWho(src), when=os.date("%Y-%m-%d %H:%M:%S")}
- profile:saveModuleProfile("achievement."..id, self.achieved[id])
-
- game.log("#LIGHT_GREEN#New Achievement: %s!", a.name)
- Dialog:simplePopup("New Achievement: #LIGHT_GREEN#"..a.name, a.desc)
+ self.achieved[id] = {turn=game.turn, who=self:achievementWho(src), when=os.date("%Y-%m-%d %H:%M:%S")}
+ profile:saveModuleProfile("achievement."..id, self.achieved[id])
+ self.playerachieved[id] = {turn=game.turn, when=os.date("%Y-%m-%d %H:%M:%S")}
+ game.log("#LIGHT_GREEN#New Achievement: %s!", a.name)
+ Dialog:simplePopup("New Achievement: #LIGHT_GREEN#"..a.name, a.desc)
end
--- Format an achievement source
-- By default just uses the actor's name, you can overload it to do more
-- @param src the actor who did it
function _M:achievementWho(src)
- return src.name
+ return src.name
end
Index: modules/tome/class/Object.lua
===================================================================
--- modules/tome/class/Object.lua (revision 906)
+++ modules/tome/class/Object.lua (working copy)
@@ -154,10 +154,7 @@
--- Gets the full textual desc of the object without the name and requirements
function _M:getTextualDesc()
- local desc = {}
- if self.encumber then
- desc[#desc+1] = ("#67AD00#%0.2f Encumbrance."):format(self.encumber)
- end
+ local desc = {}
desc[#desc+1] = ("Type: %s / %s"):format(self.type, self.subtype)
@@ -295,6 +292,10 @@
if reqs then
desc[#desc+1] = reqs
end
+
+ if self.encumber then
+ desc[#desc+1] = ("#67AD00#%0.2f Encumbrance."):format(self.encumber)
+ end
local textdesc = table.concat(self:getTextualDesc(), "\n")
Index: modules/tome/dialogs/CharacterSheet.lua
===================================================================
--- modules/tome/dialogs/CharacterSheet.lua (revision 906)
+++ modules/tome/dialogs/CharacterSheet.lua (working copy)
@@ -397,9 +397,17 @@
end
end
end
- end
+ end
nl()
+ nl(" [Player Achievments]")
+ nl()
+ for id, data in pairs(world.playerachieved) do
+ local a = world:getAchievementFromId(id)
+ nl(("%s Was Achieved for %s At %s"):format(a.name,a.desc,data.when))
+ end
+
+ nl()
nl(" [Character Inventory]")
nl()
for item, o in ipairs(self.actor:getInven("INVEN")) do