Sign in
mod

Armor

by Potluck · version 1 · Checked for all ages

Caps, helmets, coats, leggings, boots and shields for survival, in wool, copper, iron and moonsilver. Hold a piece and use it on nothing (Interact looking at the sky or far off) to put it on; each keeps off part of every hurt, more the better it's made. Use an empty hand on nothing to take a piece off. Wool and leather keep winter nights' cold off. Make wool and copper ones and shields at a workbench, and iron and moonsilver ones at an anvil.

1--!strict
2-- Armor, for survival. Hold a piece and use it on nothing (Interact looking
3-- at the sky or far off: world.on("item_used"), world 0.18) to put it on,
4-- swapping it for what you wore there before. A cap or helmet, a coat,
5-- leggings, boots and a shield each keep off part of every hurt
6-- (world.guard), more for better stuff: ARMOR below says how much, and
7-- armor other creations make ([items.*] with kind = "armor", world.item,
8-- 0.25) guards as its slot and guard say. A bar beside your health shows
9-- how well you're guarded. Use an empty hand on nothing to see what you
10-- wear and take a piece off. What each player wears is kept with the world
11-- (store), by their name, and every game draws it on them (world.wear,
12-- 0.24).
13--
14-- A Stout piece (the Charms creation's charm) keeps a point more off, and
15-- each Hearty one heals you a quarter faster. A piece with a perk charm
16-- (Owl-eyed, Gilled, Springy, Feathery, Ashen, Fleet or Fortunate) gives
17-- its perk (world.perk): sight in the dark, breathing under water, a higher
18-- jump, a slow fall, fire resistance, a quicker step or luck. Pieces add up.
19--
20-- Winter nights out under the sky are cold (@potluck/sky), and so is
21-- falling snow. With two pieces of wool or leather (or made ones that say
22-- warm) on, or one that's Warm (the Charms creation's charm), or a fire or
23-- a light near, or a roof overhead, they're warm. Cold, a player doesn't
24-- heal by themselves, and in a Normal or Hard world the cold hurts, though
25-- never below 6.
26
27local world = require("@potluck/world@0.34")
28local tweak = require("@potluck/tweak")
29local store = require("@potluck/store")
30local ui = require("@potluck/ui@0.2")
31local sound = require("@potluck/sound@0.2")
32local sky = require("@potluck/sky@0.1")
33
34local knobs = tweak {
35 strong = { 1, min = 0, max = 2, label = "How much armor keeps off", order = 1 },
36 color = { "#8fb3d9", label = "Armor bar", order = 2 },
37 cold = { true, label = "Winter nights are cold", order = 3 },
38}
39
40-- Where a piece goes, and its guard: each point keeps off 3.5% of a hurt,
41-- up to 85% (a whole set of moonsilver and a shield is 23).
42type Piece = { slot: string, guard: number }
43local ARMOR: { [string]: Piece } = {
44 Shield = { slot = "arm", guard = 3 },
45}
46local PER_POINT, MOST = 0.035, 0.85
47-- what a Stout piece adds to its guard, and a Hearty one to healing
48local STOUT, HEARTY = 1, 0.25
49local FULL = 23 -- the bar's full at this
50
51-- each material: its pieces' names (head, body, legs, feet) and guards
52local MATERIALS = {
53 { names = { "Wool cap", "Wool coat", "Wool leggings", "Wool boots" }, guards = { 1, 3, 2, 1 } },
54 { names = { "Leather cap", "Leather coat", "Leather leggings", "Leather boots" }, guards = { 2, 4, 3, 1 } },
55 { names = { "Copper helmet", "Copper coat", "Copper leggings", "Copper boots" }, guards = { 2, 5, 4, 1 } },
56 { names = { "Iron helmet", "Iron coat", "Iron leggings", "Iron boots" }, guards = { 2, 6, 5, 2 } },
57 { names = { "Moonsilver helmet", "Moonsilver coat", "Moonsilver leggings", "Moonsilver boots" }, guards = { 3, 8, 6, 3 } },
58}
59local SLOTS = { "head", "body", "legs", "feet", "arm" }
60for _, m in MATERIALS do
61 for i, name in m.names do
62 ARMOR[name] = { slot = SLOTS[i], guard = m.guards[i] }
63 end
64end
65
66-- A piece by name: one of ARMOR's, or armor another creation made. `warm`
67-- is whether it keeps the cold off as wool does.
68local function piece_of(name: string): (Piece?, boolean)
69 if ARMOR[name] then
70 return ARMOR[name], string.sub(name, 1, 5) == "Wool " or string.sub(name, 1, 8) == "Leather "
71 end
72 local it = world.item(name)
73 if it and it.kind == "armor" and it.slot then
74 return { slot = it.slot, guard = it.guard or 0 }, it.warm == true
75 end
76 return nil, false
77end
78
79-- what each player wears, by name: slot to piece
80local worn: { [string]: { [string]: string } } = store.get("worn") or {}
81
82local function save()
83 store.set("worn", worn)
84end
85
86-- one line above the hotbar for a few seconds, for each player
87local shown: { [number]: number } = {}
88local function tell(player, text: string)
89 ui.show(player, "armor", text, { at = "bottom" })
90 local mine = (shown[player.id] or 0) + 1
91 shown[player.id] = mine
92 world.after(3, function()
93 if shown[player.id] == mine then
94 shown[player.id] = nil
95 ui.hide(player, "armor")
96 end
97 end)
98end
99
100-- a player's guard, all their pieces together: a Stout one (the Charms
101-- creation's charm) keeps a point more off
102local function guard_of(player): number
103 local n = 0
104 for _, piece in worn[player.name] or {} do
105 local a = piece_of(piece)
106 if a then
107 n += a.guard
108 if table.find(world.charms(player, piece), "Stout") then
109 n += STOUT
110 end
111 end
112 end
113 return n
114end
115
116-- how much faster they heal for the Hearty pieces they wear
117local function hearty_of(player): number
118 local n = 0
119 for _, piece in worn[player.name] or {} do
120 if table.find(world.charms(player, piece), "Hearty") then
121 n += 1
122 end
123 end
124 return 1 + n * HEARTY
125end
126
127-- Perk charms: what each gives, as world.perk names it
128local PERK_CHARMS: { [string]: string } = {
129 ["Owl-eyed"] = "night_sight",
130 Gilled = "gills",
131 Springy = "leap",
132 Feathery = "feather",
133 Ashen = "fireproof",
134 Fleet = "stride",
135 Fortunate = "luck",
136}
137local PERK_NAMES = { "night_sight", "gills", "leap", "feather", "fireproof", "stride", "luck" }
138
139-- the perks a player's pieces give together, and how much of each
140local function perks_of(player): { [string]: number }
141 local out: { [string]: number } = {}
142 for _, piece in worn[player.name] or {} do
143 for _, c in world.charms(player, piece) do
144 local perk = PERK_CHARMS[c]
145 if perk then
146 out[perk] = (out[perk] or 0) + 1
147 end
148 end
149 end
150 return out
151end
152
153-- What they wear, and a choice to take each off.
154local function wearing(player)
155 local mine = worn[player.name] or {}
156 local pieces, slots, choices = {}, {}, {}
157 for _, slot in SLOTS do
158 if mine[slot] then
159 table.insert(pieces, mine[slot])
160 table.insert(slots, slot)
161 table.insert(choices, "Take off " .. string.lower(mine[slot]))
162 end
163 end
164 if #pieces == 0 then
165 return
166 end
167 local text = "You wear " .. string.lower(table.concat(pieces, ", ")) .. ". Hold a piece and use it on nothing to put it on."
168 ui.ask(player, { title = "Armor", text = text, choices = choices }, function(who, choice)
169 local piece = choice and pieces[choice]
170 if not piece then
171 return
172 end
173 if world.give(who, piece, 1) == 0 then
174 tell(who, "Your pockets are full.")
175 return
176 end
177 mine[slots[choice]] = nil
178 save()
179 sound.play("don_soft", { at = who.position, volume = 0.6 })
180 wearing(who)
181 end)
182end
183
184world.on("item_used", function(player, item: string?, aim: vector)
185 if world.pockets(player) == nil then
186 return
187 end
188 if item == nil then
189 wearing(player)
190 return
191 end
192 local piece, warm = piece_of(item)
193 if not piece or not world.take(player, item, 1) then
194 return
195 end
196 local mine = worn[player.name] or {}
197 local before = mine[piece.slot]
198 if before and world.give(player, before, 1) == 0 then
199 -- no room for what comes off
200 world.give(player, item, 1)
201 tell(player, "Make room for your " .. string.lower(before) .. " first.")
202 return
203 end
204 mine[piece.slot] = item
205 worn[player.name] = mine
206 save()
207 sound.play(if warm then "don_soft" else "don_metal", { at = player.position })
208 tell(player, "You put on the " .. string.lower(item) .. ".")
209end)
210
211-- who's been told what the bar means
212local told: { [number]: boolean } = {}
213
214-- what every game draws each player wearing, as last said: in survival
215-- what they wear, and nothing otherwise
216local drawn: { [number]: { [string]: string } } = {}
217local function show_worn(p, on: boolean)
218 local mine = if on then worn[p.name] or {} else {}
219 local was = drawn[p.id] or {}
220 for _, slot in SLOTS do
221 if was[slot] ~= mine[slot] then
222 world.wear(p, slot, mine[slot])
223 end
224 end
225 drawn[p.id] = table.clone(mine)
226end
227
228-- the perks this creation last gave each player, by id
229local perked: { [number]: { [string]: number } } = {}
230
231-- how much each player's armor keeps off, and the bar
232world.every(0.5, function()
233 for _, p in world.players() do
234 local g = if world.pockets(p) ~= nil then guard_of(p) else 0
235 local perks = if world.pockets(p) ~= nil then perks_of(p) else {}
236 local had = perked[p.id] or {}
237 for _, name in PERK_NAMES do
238 local level = perks[name] or 0
239 if (had[name] or 0) ~= level then
240 world.perk(p, name, level)
241 end
242 end
243 perked[p.id] = perks
244 show_worn(p, world.pockets(p) ~= nil)
245 world.guard(p, 1 - math.min(g * PER_POINT * knobs.strong, MOST))
246 if g > 0 then
247 ui.meter(p, "guard", math.min(g / FULL, 1), { color = knobs.color })
248 if not told[p.id] then
249 told[p.id] = true
250 ui.show(p, "guard_says", "The blue bar is your armor. The fuller it is, the less hurts hurt.", { at = "left" })
251 world.after(6, function()
252 ui.hide(p, "guard_says")
253 end)
254 end
255 else
256 ui.meter(p, "guard", nil)
257 end
258 end
259end)
260
261-- What keeps the cold off: two warm pieces (wool, leather or made ones that
262-- say so), or one that's Warm.
263local function dressed_warm(p): boolean
264 -- a Hearth cider (the warm perk) keeps the cold off too
265 if world.perk(p, "warm") > 0 then
266 return true
267 end
268 local n = 0
269 for _, piece in worn[p.name] or {} do
270 if table.find(world.charms(p, piece), "Warm") then
271 return true
272 end
273 local _, warm = piece_of(piece)
274 if warm then
275 n += 1
276 end
277 end
278 return n >= 2
279end
280
281-- Whether it's cold where they are: out under the sky on a winter night,
282-- or in falling snow, with no fire or light near.
283local function out_in_the_cold(p): boolean
284 local snowing = sky.weather().kind == "snow"
285 if not snowing and ((sky.season()) ~= "winter" or world.daylight() > 0.3) then
286 return false
287 end
288 local head = p.position + vector.create(0, 1.6, 0)
289 if world.top(head.x, head.z) > math.floor(head.y) then
290 return false
291 end
292 return world.find_light(p.position, 5) == nil
293end
294
295-- Who's cold now. The cold hurts every few seconds in a Normal or Hard
296-- world, never below this.
297local cold: { [number]: boolean } = {}
298-- how fast each player heals by this creation's say: 0 in the cold, more
299-- for Hearty pieces
300local healing: { [number]: number } = {}
301local COLD_DOWN_TO = 6
302
303world.every(5, function()
304 local danger = world.danger()
305 for _, p in world.players() do
306 local now = knobs.cold and danger ~= "peaceful" and world.pockets(p) ~= nil and out_in_the_cold(p) and not dressed_warm(p)
307 if now ~= (cold[p.id] == true) then
308 cold[p.id] = if now then true else nil
309 tell(p, if now then "You're cold. Wool or leather, a fire, a light or a roof keeps you warm." else "You're warm again.")
310 end
311 local rate = if now then 0 elseif world.pockets(p) ~= nil then hearty_of(p) else 1
312 if rate ~= (healing[p.id] or 1) then
313 healing[p.id] = rate
314 world.healing(p, rate)
315 end
316 if now and (danger == "normal" or danger == "hard") then
317 world.hurt(p, if danger == "hard" then 2 else 1, { down_to = COLD_DOWN_TO, guarded = false })
318 end
319 end
320end)
321
322world.on("player_left", function(p)
323 told[p.id] = nil
324 shown[p.id] = nil
325 drawn[p.id] = nil
326 cold[p.id] = nil
327 healing[p.id] = nil
328 perked[p.id] = nil
329end)