Sign in
mod

Goals

by Potluck · version 1 · Checked for all ages

Things worth doing, to light your way through the island: dig your first ore, smelt it, cook and brew, trade with a village, wake the Gloam, cross the mirror into the Long Dusk. Open Goals from the pause menu to see what's done and what's next; each one done says so with a chime, and gives XP in survival. Other creations can list goals of their own with world.goals.

1--!strict
2-- Goals: things worth doing (data in goals.luau), listed with world.goals so
3-- the Goals screen can show them, and handed out with world.achieve as
4-- players do them. Most are heard from what players do to blocks; carrying
5-- something, where you stand and your level are looked at every couple of
6-- seconds. Every goal can be done in any order: `after` only decides what the
7-- screen shows as locked.
8
9local world = require("@potluck/world@0.36")
10local data = require("./goals")
11
12type Goal = data.Goal
13
14local goals: { Goal } = data
15world.goals(goals :: any)
16
17-- whether a name matches a pattern: equal, or "* bed" for the end of one
18local function same(pattern: string, name: string): boolean
19 if string.sub(pattern, 1, 1) == "*" then
20 local tail = string.sub(pattern, 2)
21 return #name >= #tail and string.sub(name, -#tail) == tail
22 end
23 return pattern == name
24end
25
26local function among(list: { string }?, name: string): boolean
27 if not list then
28 return false
29 end
30 for _, p in list do
31 if same(p, name) then
32 return true
33 end
34 end
35 return false
36end
37
38-- the goals that are done by what a player does to a block
39local function heard(player: any, block: string, field: string)
40 for _, g in goals do
41 local names = (g :: any)[field] :: { string }?
42 if names and among(names, block) then
43 world.achieve(player, g.id)
44 end
45 end
46end
47
48world.on("block_used", function(player, _at, block)
49 heard(player, block, "used")
50end)
51world.on("block_broken", function(player, _at, block)
52 heard(player, block, "broke")
53end)
54world.on("block_placed", function(player, _at, block)
55 heard(player, block, "placed")
56end)
57
58-- whether what a player carries meets a goal
59local function carries(g: Goal, pockets: { [string]: number }): boolean
60 local need = g.kinds or 1
61 local each = g.count or 1
62 local have = 0
63 local seen: { [string]: boolean } = {}
64 for name, n in pockets do
65 if n >= each and among(g.any, name) and not seen[name] then
66 seen[name] = true
67 have += 1
68 end
69 end
70 if have >= need then
71 return true
72 end
73 if g.relic then
74 for name, n in pockets do
75 if n > 0 then
76 local it = world.item(name) :: any
77 if it and it.relic then
78 return true
79 end
80 end
81 end
82 end
83 return false
84end
85
86-- whether where a player is, or how far they've come, meets a goal
87local function here(g: Goal, player: any, level: number): boolean
88 local at: vector = player.position
89 if g.below and at.y <= g.below then
90 return true
91 end
92 if g.above and at.y >= g.above then
93 return true
94 end
95 if g.level and level >= g.level then
96 return true
97 end
98 if g.place and world.place(at) == g.place then
99 return true
100 end
101 if g.biome and world.biome(at) == g.biome then
102 return true
103 end
104 return false
105end
106
107local function look()
108 for _, player in world.players() do
109 local pockets = world.pockets(player)
110 local _, level = world.xp(player)
111 for _, g in goals do
112 if not world.achieved(player, g.id) then
113 local done = false
114 if g.any or g.relic then
115 done = pockets ~= nil and carries(g, pockets)
116 else
117 done = (g.below or g.above or g.level or g.place or g.biome) ~= nil and here(g, player, level)
118 end
119 if done then
120 world.achieve(player, g.id)
121 end
122 end
123 end
124 end
125end
126
127world.every(2, look)