9 Commits

Author SHA1 Message Date
9c74a3f85a Update docs 2021-01-10 16:30:05 +00:00
b4a4a1fd74 Improve API: add awards.get_award_states() 2021-01-10 16:25:20 +00:00
4e8d175714 Fixed a couple typos.
- The Mesecons-related award claimed to be awarded when placing mesecon wires, but instead was coded to be awarded when placing pipes from Pipeworks. Fixed code to match description.
 - The Basic-Materials-related award claimed to require crafting flint and steel one hundred times, but instead was coded to require crafting oil extract five hundred times. Fixed description to match code.
2020-06-16 07:47:41 +00:00
c7b60be880 player:getpos() is deprecated and calling it causes the game to halt if deprecated_lua_api_handling is set to "error" in minetest.conf. Call player:get_pos() instead to prevent the game from halting. 2020-04-19 19:05:06 +00:00
ce58720493 Fix group total, fix item increment count argument, add player checks, resize award icon, fix log output 2020-04-05 16:00:33 +00:00
d542042a50 Interpret group ratings of zero as not in group.
The MT API docs instruct to interpret `nil` and `0` as the same rating.
2020-04-04 17:40:48 -04:00
7e3064ef93 Check for valid players in default triggers instead of crashing. 2020-04-04 17:03:45 -04:00
9d98f950b8 Fix eat trigger to actually notify awards on item eat. 2020-04-04 15:35:14 -04:00
8a34ea8055 Add awards.add_defaults setting to control adding default awards.
Useful for games not based on MTG but using some of its mods.
2020-04-04 13:47:20 -04:00
8 changed files with 170 additions and 120 deletions

View File

@ -37,10 +37,11 @@ awards.register_award("mymod:award", {
The above trigger type is an example of a counted_key trigger: The above trigger type is an example of a counted_key trigger:
rather than a single counter there's a counter per key - in this rather than a single counter there's a counter per key - in this
case the key is the value of the `node` field. If you leave out case the key is the value of the `node` field.
the key in a `counted_key` trigger, then the total will be used
instead. For example, here is an award which unlocks after you've If you leave out the key in a `counted_key` trigger, then the total will be used
placed 10 nodes of any type: instead. For example, here is an award which unlocks after you've placed 10
nodes of any type:
```lua ```lua
awards.register_award("mymod:award", { awards.register_award("mymod:award", {
@ -125,6 +126,10 @@ awards.register_trigger("foo", {
type = "custom", type = "custom",
progress = "@1/@2 foos", progress = "@1/@2 foos",
auto_description = { "Do a foo", "Foo @1 times" }, auto_description = { "Do a foo", "Foo @1 times" },
on_register = function(self, award)
print(award.name .. " was registered with foo trigger type")
end,
}) })
minetest.register_on_foo(function() minetest.register_on_foo(function()
@ -168,7 +173,9 @@ end
# API # API
* awards.register_award(name, def), the def table has the following fields: ## Awards
* `awards.register_award(name, def)`, the def table has the following fields:
* `title` - title of the award (defaults to name) * `title` - title of the award (defaults to name)
* `description` - longer description of the award, displayed in Awards tab * `description` - longer description of the award, displayed in Awards tab
* `difficulty` - see [Award Difficulty](#award-difficulty). * `difficulty` - see [Award Difficulty](#award-difficulty).
@ -182,23 +189,48 @@ end
* `background` - the background image, use default otherwise. * `background` - the background image, use default otherwise.
* `trigger` - trigger definition, see [Builtin Trigger Types](#builtin-trigger-types). * `trigger` - trigger definition, see [Builtin Trigger Types](#builtin-trigger-types).
* `on_unlock(name, def)` - callback on unlock. * `on_unlock(name, def)` - callback on unlock.
* awards.register_trigger(name, def), the def table has the following fields: * `awards.registered_awards` - table of award name to definition.
* `type` - see [Trigger Types](#trigger-types). * `awards.register_on_unlock(func(name, def))`
* `name` is the player name
* `def` is the award def.
* return true to cancel HUD
* `awards.unlock(player_name, award_name)`
* gives an award to a player
* `awards.get_award_states(player_name)`
* Returns list of tables, sorted by `score`, each having the fields:
```lua
{
name = "mymod:awardname",
def = {}, -- Award definition
unlocked = true, -- Whether award has been unlocked
started = true, -- Whether any progress has been made
score = 0, -- Score used in sorting
-- Either a table or nil
-- Will be nil if progress is indeterminable or
-- if the award is unlocked
progress = {
current = 5,
target = 10,
label = "label", -- Label to show over progress bar
}
}
```
## Triggers
* `awards.register_trigger(name, def)`, the def table has the following fields:
* `type` - see trigger type types in [Trigger Types](#trigger-types).
* `progress` - used to format progress, defaults to "%1/%2". * `progress` - used to format progress, defaults to "%1/%2".
* `auto_description` - a table of two elements. Each element is a format string. Element 1 is singular, element 2 is plural. Used for the award description (not title) if none is given. * `auto_description` - a table of two elements. Each element is a format string. Element 1 is singular, element 2 is plural. Used for the award description (not title) if none is given.
* `on_register(award_def)` - called when an award registers with this type. * `on_register(self, award_def)` - called when an award registers with this type.
* "counted_key" only: * "counted_key" only:
* `auto_description_total` - Used if the trigger is for the total. * `auto_description_total` - Used if the trigger is for the total.
* `get_key(self, def)` - get key for particular award, return nil for a total. * `get_key(self, def)` - get key for particular award, return nil for a total.
* `key_is_item` - true if the key is an item name. On notify(), * `key_is_item` - true if the key is an item name. On notify(),
any watched groups will also be notified as `group:groupname` keys. any watched groups will also be notified as `group:groupname` keys.
* awards.register_on_unlock(func(name, def)) * `awards.registered_triggers` - table of trigger name to definition.
* name is the player name
* def is the award def.
* return true to cancel HUD
* awards.unlock(name, award)
* gives an award to a player
* name is the player name
## Builtin Trigger Types ## Builtin Trigger Types

View File

@ -3,7 +3,9 @@
-- The global award namespace -- The global award namespace
awards = { awards = {
show_mode = "hud", show_mode = "hud",
registered_awards = {},
registered_triggers = {}, registered_triggers = {},
on_unlock = {},
} }
-- Internationalization support. -- Internationalization support.
@ -16,7 +18,11 @@ dofile(minetest.get_modpath("awards").."/src/api_triggers.lua")
dofile(minetest.get_modpath("awards").."/src/chat_commands.lua") dofile(minetest.get_modpath("awards").."/src/chat_commands.lua")
dofile(minetest.get_modpath("awards").."/src/gui.lua") dofile(minetest.get_modpath("awards").."/src/gui.lua")
dofile(minetest.get_modpath("awards").."/src/triggers.lua") dofile(minetest.get_modpath("awards").."/src/triggers.lua")
dofile(minetest.get_modpath("awards").."/src/awards.lua")
-- Optionally add default awards.
if minetest.settings:get_bool("awards.add_defaults", true) then
dofile(minetest.get_modpath("awards").."/src/awards.lua")
end
awards.load() awards.load()
minetest.register_on_shutdown(awards.save) minetest.register_on_shutdown(awards.save)

2
settingtypes.txt Normal file
View File

@ -0,0 +1,2 @@
# Add default achievements from the awards mod
awards.add_defaults (Add Default Achievements) bool true

View File

@ -43,6 +43,11 @@ end
-- name - the name of the player -- name - the name of the player
-- award - the name of the award to give -- award - the name of the award to give
function awards.unlock(name, award) function awards.unlock(name, award)
-- Ensure the player is online.
if not minetest.get_player_by_name(name) then
return
end
-- Access Player Data -- Access Player Data
local data = awards.player(name) local data = awards.player(name)
local awdef = awards.registered_awards[award] local awdef = awards.registered_awards[award]
@ -60,7 +65,7 @@ function awards.unlock(name, award)
end end
-- Unlock Award -- Unlock Award
minetest.log("action", name.." has unlocked award "..name) minetest.log("action", name.." has unlocked award "..award)
data.unlocked[award] = award data.unlocked[award] = award
awards.save() awards.save()
@ -91,7 +96,7 @@ function awards.unlock(name, award)
local title = awdef.title or award local title = awdef.title or award
local desc = awdef.description or "" local desc = awdef.description or ""
local background = awdef.background or "awards_bg_default.png" local background = awdef.background or "awards_bg_default.png"
local icon = awdef.icon or "awards_unknown.png" local icon = (awdef.icon or "awards_unknown.png") .. "^[resize:32x32"
local sound = awdef.sound local sound = awdef.sound
if sound == nil then if sound == nil then
-- Explicit check for nil because sound could be `false` to disable it -- Explicit check for nil because sound could be `false` to disable it
@ -177,3 +182,71 @@ function awards.unlock(name, award)
end) end)
end end
end end
function awards.get_award_states(name)
local hash_is_unlocked = {}
local retval = {}
-- Add all unlocked awards
local data = awards.player(name)
if data and data.unlocked then
for awardname, _ in pairs(data.unlocked) do
local def = awards.registered_awards[awardname]
if def then
hash_is_unlocked[awardname] = true
local score = -100000
local difficulty = def.difficulty or 1
if def.trigger and def.trigger.target then
difficulty = difficulty * def.trigger.target
end
score = score + difficulty
retval[#retval + 1] = {
name = awardname,
def = def,
unlocked = true,
started = true,
score = score,
progress = nil,
}
end
end
end
-- Add all locked awards
for _, def in pairs(awards.registered_awards) do
if not hash_is_unlocked[def.name] and def:can_unlock(data) then
local progress = def.get_progress and def:get_progress(data)
local started = false
local score = def.difficulty or 1
if def.secret then
score = 1000000
elseif def.trigger and def.trigger.target and progress then
local perc = progress.current / progress.target
score = score * (1 - perc) * def.trigger.target
if perc < 0.001 then
score = score + 100
else
started = true
end
else
score = 100
end
retval[#retval + 1] = {
name = def.name,
def = def,
unlocked = false,
started = started,
score = score,
progress = progress,
}
end
end
table.sort(retval, function(a, b)
return a.score < b.score
end)
return retval
end

View File

@ -2,9 +2,7 @@
local S, NS = awards.gettext, awards.ngettext local S, NS = awards.gettext, awards.ngettext
awards.registered_awards = {}
awards.on = {} awards.on = {}
awards.on_unlock = {}
local default_def = {} local default_def = {}
@ -43,11 +41,12 @@ function awards.register_trigger(tname, tdef)
} }
tdef.register(tmp) tdef.register(tmp)
function def.getProgress(_, data) function def.get_progress(_, data)
local done = math.min(data[tname] or 0, tmp.target) local current = math.min(data[tname] or 0, tmp.target)
return { return {
perc = done / tmp.target, current = current,
label = S(tdef.progress, done, tmp.target), target = tmp.target,
label = S(tdef.progress, current, tmp.target),
} }
end end
@ -102,7 +101,7 @@ function awards.register_trigger(tname, tdef)
end end
-- Called to get progress values and labels -- Called to get progress values and labels
function def.getProgress(_, data) function def.get_progress(_, data)
data[tname] = data[tname] or {} data[tname] = data[tname] or {}
local done local done
@ -114,7 +113,8 @@ function awards.register_trigger(tname, tdef)
done = math.min(done, tmp.target) done = math.min(done, tmp.target)
return { return {
perc = done / tmp.target, current = done,
target = tmp.target,
label = S(tdef.progress, done, tmp.target), label = S(tdef.progress, done, tmp.target),
} }
end end
@ -144,8 +144,8 @@ function awards.register_trigger(tname, tdef)
if tdef.key_is_item and key:sub(1, 6) ~= "group:" then if tdef.key_is_item and key:sub(1, 6) ~= "group:" then
local itemdef = minetest.registered_items[key] local itemdef = minetest.registered_items[key]
if itemdef then if itemdef then
for groupname, _ in pairs(itemdef.groups or {}) do for groupname,rating in pairs(itemdef.groups or {}) do
if tdef.watched_groups[groupname] then if rating ~= 0 and tdef.watched_groups[groupname] then
tdef.notify(player, "group:" .. groupname, n) tdef.notify(player, "group:" .. groupname, n)
end end
end end
@ -160,8 +160,9 @@ function awards.register_trigger(tname, tdef)
data[tname] = data[tname] or {} data[tname] = data[tname] or {}
local currentVal = (data[tname][key] or 0) + n local currentVal = (data[tname][key] or 0) + n
data[tname][key] = currentVal data[tname][key] = currentVal
data[tname].__total = (data[tname].__total or 0)
if key:sub(1, 6) ~= "group:" then if key:sub(1, 6) ~= "group:" then
data[tname].__total = (data[tname].__total or 0) + n data[tname].__total = data[tname].__total + n
end end
tdef:run_callbacks(player, data, function(entry) tdef:run_callbacks(player, data, function(entry)
@ -173,7 +174,6 @@ function awards.register_trigger(tname, tdef)
else else
return return
end end
if current >= entry.target then if current >= entry.target then
return entry.award return entry.award
end end
@ -201,7 +201,7 @@ end
function awards.increment_item_counter(data, field, itemname, count) function awards.increment_item_counter(data, field, itemname, count)
itemname = minetest.registered_aliases[itemname] or itemname itemname = minetest.registered_aliases[itemname] or itemname
data[field][itemname] = (data[field][itemname] or 0) + 1 data[field][itemname] = (data[field][itemname] or 0) + (count or 1)
end end
function awards.get_item_count(data, field, itemname) function awards.get_item_count(data, field, itemname)

View File

@ -89,7 +89,7 @@ if minetest.get_modpath("fire") then
secret = true, secret = true,
}) })
awards.register_on_death(function(player,data) awards.register_on_death(function(player,data)
local pos = player:getpos() local pos = player:get_pos()
if pos and minetest.find_node_near(pos, 2, "fire:basic_flame") ~= nil then if pos and minetest.find_node_near(pos, 2, "fire:basic_flame") ~= nil then
return "award_burn" return "award_burn"
end end
@ -115,7 +115,7 @@ awards.register_award("award_deep_down", {
secret = true, secret = true,
}) })
awards.register_on_death(function(player,data) awards.register_on_death(function(player,data)
local pos = player:getpos() local pos = player:get_pos()
if pos and pos.y < -10000 then if pos and pos.y < -10000 then
return "award_deep_down" return "award_deep_down"
end end
@ -129,7 +129,7 @@ awards.register_award("award_no_screen", {
secret = true, secret = true,
}) })
awards.register_on_death(function(player,data) awards.register_on_death(function(player,data)
local pos = player:getpos() local pos = player:get_pos()
if pos and pos.y > 10000 then if pos and pos.y > 10000 then
return "award_no_screen" return "award_no_screen"
end end
@ -767,7 +767,7 @@ if minetest.get_modpath("default") then
secret = true, secret = true,
}) })
awards.register_on_death(function(player,data) awards.register_on_death(function(player,data)
local pos = player:getpos() local pos = player:get_pos()
if pos and (minetest.find_node_near(pos, 2, "default:lava_flowing") ~= nil or if pos and (minetest.find_node_near(pos, 2, "default:lava_flowing") ~= nil or
minetest.find_node_near(pos, 2, "default:lava_source") ~= nil) then minetest.find_node_near(pos, 2, "default:lava_source") ~= nil) then
return "award_in_the_flow" return "award_in_the_flow"
@ -782,7 +782,7 @@ if minetest.get_modpath("default") then
secret = true, secret = true,
}) })
awards.register_on_death(function(player,data) awards.register_on_death(function(player,data)
local pos = player:getpos() local pos = player:get_pos()
if pos and minetest.find_node_near(pos, 5, "default:stone_with_diamond") ~= nil then if pos and minetest.find_node_near(pos, 5, "default:stone_with_diamond") ~= nil then
return "award_this_is_sad" return "award_this_is_sad"
end end
@ -798,7 +798,7 @@ if minetest.get_modpath("bones") then
secret = true, secret = true,
}) })
awards.register_on_death(function(player,data) awards.register_on_death(function(player,data)
local pos = player:getpos() local pos = player:get_pos()
if pos and minetest.find_node_near(pos, 5, "bones:bones") ~= nil then if pos and minetest.find_node_near(pos, 5, "bones:bones") ~= nil then
return "award_the_stack" return "award_the_stack"
end end
@ -1090,7 +1090,7 @@ if minetest.get_modpath("mesecons") then
difficulty = 0.2, difficulty = 0.2,
trigger = { trigger = {
type = "place", type = "place",
node = "pipeworks:tube_1", node = "mesecons:wire_00000000_off",
target = 500, target = 500,
} }
}) })
@ -1099,7 +1099,7 @@ end
if minetest.get_modpath("basic_materials") then if minetest.get_modpath("basic_materials") then
awards.register_award("awards_oil", { awards.register_award("awards_oil", {
title = S("Oil Typhoon"), title = S("Oil Typhoon"),
description = S("Craft 100 times flint and steel."), description = S("Craft 500 times oil extract."),
trigger = { trigger = {
type = "craft", type = "craft",

View File

@ -2,73 +2,9 @@
local S = awards.gettext local S = awards.gettext
local function order_awards(name)
local hash_is_unlocked = {}
local retval = {}
local data = awards.player(name)
if data and data.unlocked then
for awardname, _ in pairs(data.unlocked) do
local def = awards.registered_awards[awardname]
if def then
hash_is_unlocked[awardname] = true
local score = -100000
local difficulty = def.difficulty or 1
if def.trigger and def.trigger.target then
difficulty = difficulty * def.trigger.target
end
score = score + difficulty
retval[#retval + 1] = {
name = awardname,
def = def,
unlocked = true,
started = true,
score = score,
}
end
end
end
for _, def in pairs(awards.registered_awards) do
if not hash_is_unlocked[def.name] and def:can_unlock(data) then
local started = false
local score = def.difficulty or 1
if def.secret then
score = 1000000
elseif def.trigger and def.trigger.target and def.getProgress then
local progress = def:getProgress(data).perc
score = score * (1 - progress) * def.trigger.target
if progress < 0.001 then
score = score + 100
else
started = true
end
else
score = 100
end
retval[#retval + 1] = {
name = def.name,
def = def,
unlocked = false,
started = started,
score = score,
}
end
end
table.sort(retval, function(a, b)
return a.score < b.score
end)
return retval
end
function awards.get_formspec(name, to, sid) function awards.get_formspec(name, to, sid)
local formspec = "" local formspec = ""
local awards_list = order_awards(name) local awards_list = awards.get_award_states(name)
local data = awards.player(name)
if #awards_list == 0 then if #awards_list == 0 then
formspec = formspec .. "label[3.9,1.5;"..minetest.formspec_escape(S("Error: No achivements available.")).."]" formspec = formspec .. "label[3.9,1.5;"..minetest.formspec_escape(S("Error: No achivements available.")).."]"
@ -106,15 +42,11 @@ function awards.get_formspec(name, to, sid)
if sdef and sdef.icon then if sdef and sdef.icon then
formspec = formspec .. "image[0.45,0;3.5,3.5;" .. sdef.icon .. "]" -- adjusted values from 0.6,0;3,3 formspec = formspec .. "image[0.45,0;3.5,3.5;" .. sdef.icon .. "]" -- adjusted values from 0.6,0;3,3
end end
local barwidth = 3.95
local perc = nil if sitem.progress then
local label = nil local barwidth = 3.95
if sdef.getProgress and data then local perc = sitem.progress.current / sitem.progress.target
local res = sdef:getProgress(data) local label = sitem.progress.label
perc = res.perc
label = res.label
end
if perc then
if perc > 1 then if perc > 1 then
perc = 1 perc = 1
end end
@ -124,6 +56,7 @@ function awards.get_formspec(name, to, sid)
formspec = formspec .. "label[1.6,8.15;" .. minetest.formspec_escape(label) .. "]" formspec = formspec .. "label[1.6,8.15;" .. minetest.formspec_escape(label) .. "]"
end end
end end
if sdef and sdef.description then if sdef and sdef.description then
formspec = formspec .. "box[-0.05,3.75;3.9,4.2;#000]" formspec = formspec .. "box[-0.05,3.75;3.9,4.2;#000]"
formspec = formspec .. "textarea[0.25,3.75;3.9,4.2;;" .. formspec = formspec .. "textarea[0.25,3.75;3.9,4.2;;" ..
@ -174,7 +107,7 @@ function awards.show_to(name, to, sid, text)
return return
end end
if text then if text then
local awards_list = order_awards(name) local awards_list = awards.get_award_states(name)
if #awards_list == 0 then if #awards_list == 0 then
minetest.chat_send_player(to, S("Error: No award available.")) minetest.chat_send_player(to, S("Error: No award available."))
return return

View File

@ -14,6 +14,10 @@
-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-- --
-- Check if a player object is valid for awards.
local function player_ok(player)
return player and player.is_player and player:is_player() and not player.is_fake_player
end
awards.register_trigger("chat", { awards.register_trigger("chat", {
type = "counted", type = "counted",
@ -22,7 +26,7 @@ awards.register_trigger("chat", {
}) })
minetest.register_on_chat_message(function(name, message) minetest.register_on_chat_message(function(name, message)
local player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
if not player or string.find(message, "/") then if not player_ok(player) or string.find(message, "/") then
return return
end end
@ -68,7 +72,7 @@ awards.register_trigger("dig", {
key_is_item = true, key_is_item = true,
}) })
minetest.register_on_dignode(function(pos, node, player) minetest.register_on_dignode(function(pos, node, player)
if not player or not pos or not node then if not player_ok(player) or not pos or not node then
return return
end end
@ -89,7 +93,7 @@ awards.register_trigger("place", {
key_is_item = true, key_is_item = true,
}) })
minetest.register_on_placenode(function(pos, node, player) minetest.register_on_placenode(function(pos, node, player)
if not player or not pos or not node then if not player_ok(player) or not pos or not node then
return return
end end
@ -110,7 +114,7 @@ awards.register_trigger("craft", {
key_is_item = true, key_is_item = true,
}) })
minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv) minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
if not player or itemstack:is_empty() then if not player_ok(player) or itemstack:is_empty() then
return return
end end
@ -131,11 +135,11 @@ awards.register_trigger("eat", {
key_is_item = true, key_is_item = true,
}) })
minetest.register_on_item_eat(function(_, _, itemstack, player, _) minetest.register_on_item_eat(function(_, _, itemstack, player, _)
if not player or itemstack:is_empty() then if not player_ok(player) or itemstack:is_empty() then
return return
end end
local itemname = itemstack:get_name() local itemname = itemstack:get_name()
itemname = minetest.registered_aliases[itemname] or itemname itemname = minetest.registered_aliases[itemname] or itemname
awards.notify_craft(player, itemname, itemstack:get_count()) awards.notify_eat(player, itemname)
end) end)