Sign in
creature

Puffcaps

by Potluck · version 1 · Checked for all ages

Waddling mushrooms that live in the dark of caves. Underground, they keep their distance from anyone in survival and lob puffs of spores at them, so watch for them coming and step aside. They keep out of the light, but they'll throw into it. Bop one up close and it puffs in your face: an arrow or a sling is better. A popped one leaves a puffcap, good to eat, and a cooking pot makes puffcaps into mushroom stew. None on a Peaceful world, and fewer on a Gentle one.

1--!strict
2-- Puffcaps: waddling mushrooms that live in the dark of caves. Underground
3-- and away from any light, they keep a few blocks off anyone in survival
4-- and lob puffs of spores at them (self:throw, world 0.21, the "spores"
5-- look of world 0.31) in a high arc, so step aside when you see one come.
6-- They keep out of the light themselves, but they'll throw into it. Bop one
7-- up close and it puffs in your face; an arrow, a stone from a sling or a
8-- spear is better. A popped one leaves a puffcap, which you can eat, and
9-- which a cooking pot makes into mushroom stew. How many come, how often
10-- they throw and how hard the spores sting go by the world's danger
11-- (world.danger): none on Peaceful.
12
13local world = require("@potluck/world@0.33")
14local tweak = require("@potluck/tweak")
15local sound = require("@potluck/sound@0.2")
16
17local knobs = tweak {
18 how_many = { 2, min = 0, max = 6, label = "How many waddle about you in caves (0 for none)", order = 1 },
19 sting = { 2, min = 0, max = 6, label = "How hard spores sting", order = 2 },
20 every = { 4, min = 1.5, max = 12, label = "Seconds between puffs", order = 3 },
21 bops = { 2, min = 1, max = 6, label = "Bops to pop one by hand", order = 4 },
22 shy = { 4, min = 2, max = 10, label = "How far they keep from a light", order = 5 },
23 cap = { "#b84a3a", label = "Cap color", order = 6 },
24}
25
26local DANGER: { [string]: { many: number, hurt: number, pace: number } } = {
27 peaceful = { many = 0, hurt = 0, pace = 1 },
28 gentle = { many = 0.5, hurt = 0.5, pace = 1.4 },
29 normal = { many = 1, hurt = 1, pace = 1 },
30 hard = { many = 1.5, hurt = 1.5, pace = 0.7 },
31}
32local SPEED, DROP = 10, 0.55 -- how a puff of spores flies
33
34local function danger()
35 return DANGER[world.danger()] or DANGER.gentle
36end
37
38local SWORDS: { [string]: number } = {
39 ["Wooden sword"] = 2,
40 ["Stone sword"] = 3,
41 ["Copper sword"] = 3,
42 ["Iron sword"] = 4,
43 ["Moonsilver sword"] = 5,
44}
45
46local function bops(held: string?): number
47 if not held then
48 return 1
49 end
50 if SWORDS[held] then
51 return SWORDS[held]
52 end
53 local it = world.item(held)
54 return if it and it.kind == "weapon" then it.power or 1 else 1
55end
56
57local STALK = "#e8dcc0"
58local SPOT = "#f4ecd8"
59local Puffcap = world.creature {
60 name = "Puffcap",
61 speed = 1.4,
62 count = 0,
63 home = "here",
64 size = { 0.5, 0.9, 0.5 },
65 behaviors = { wander = false },
66 body = {
67 { "stalk", size = { 0.34, 0.44, 0.34 }, at = { 0, 0.36, 0 }, color = STALK },
68 { "cap", size = { 0.8, 0.26, 0.8 }, at = { 0, 0.7, 0 }, color = knobs.cap, head = true },
69 { "cap_top", size = { 0.56, 0.14, 0.56 }, at = { 0, 0.88, 0 }, color = knobs.cap, head = true },
70 { "spot_1", size = { 0.14, 0.04, 0.14 }, at = { -0.16, 0.96, 0.1 }, color = SPOT, head = true },
71 { "spot_2", size = { 0.12, 0.04, 0.12 }, at = { 0.18, 0.96, -0.12 }, color = SPOT, head = true },
72 { "spot_3", size = { 0.1, 0.1, 0.04 }, at = { 0.2, 0.74, -0.41 }, color = SPOT, head = true },
73 { "spot_4", size = { 0.1, 0.1, 0.04 }, at = { -0.24, 0.7, -0.41 }, color = SPOT, head = true },
74 { "gills", size = { 0.7, 0.04, 0.7 }, at = { 0, 0.56, 0 }, color = "#c8b898" },
75 { "eye_left", size = { 0.07, 0.08, 0.03 }, at = { -0.08, 0.46, -0.18 }, color = "#1a1410" },
76 { "eye_right", size = { 0.07, 0.08, 0.03 }, at = { 0.08, 0.46, -0.18 }, color = "#1a1410" },
77 { "foot_left", size = { 0.14, 0.14, 0.18 }, at = { -0.1, 0.07, 0 }, color = STALK, leg = true },
78 { "foot_right", size = { 0.14, 0.14, 0.18 }, at = { 0.1, 0.07, 0 }, color = STALK, leg = true },
79 },
80}
81
82local MOST = 24
83local NEAR, FAR = 9, 16
84local CHASE = 20
85local LONELY = 40
86local KEEP = 7 -- how far off they like to stay
87local REACH = 14 -- the farthest they lob
88local DEEP = 4 -- blocks under the ground over them that counts as a cave
89local UP = vector.create(0, 1, 0)
90
91local where: { [number]: vector } = {}
92
93local function flat(a: vector, b: vector): number
94 local dx, dz = a.x - b.x, a.z - b.z
95 return math.sqrt(dx * dx + dz * dz)
96end
97
98local function solid(at: vector): boolean
99 local ok, kind = pcall(world.kind, world.block(at))
100 return ok and kind ~= "air" and kind ~= "water" and kind ~= "plant"
101end
102
103local function below_ground(at: vector): boolean
104 return at.y < world.top(at.x, at.z) - DEEP
105end
106
107local seen_at, seen = -1, {}
108local function prey()
109 local now = world.time()
110 if now ~= seen_at then
111 seen_at, seen = now, {}
112 for _, p in world.players() do
113 if world.pockets(p) ~= nil and below_ground(p.position) then
114 table.insert(seen, p)
115 end
116 end
117 end
118 return seen
119end
120
121local function gone(self)
122 where[self.id] = nil
123 self:remove()
124end
125
126local function away_from(self, from: vector, far: number, speed: number)
127 local d = math.max(flat(self.position, from), 0.1)
128 local away = vector.create((self.position.x - from.x) / d, 0, (self.position.z - from.z) / d)
129 self:walk_to(self.position + away * far, speed)
130end
131
132function Puffcap:spawned()
133 self.throw_in = world.random(2, 4)
134 self.think = 0
135end
136
137local function sting(_at: vector, _block: string?, _creature: number?, player)
138 if player and knobs.sting > 0 and world.hurt(player, knobs.sting * danger().hurt) then
139 sound.play_for(player, "cough", { caption = "Spores make you cough" })
140 end
141end
142
143function Puffcap:tick(dt: number)
144 where[self.id] = self.position
145 local d = danger()
146 if d.many == 0 or knobs.how_many < 1 then
147 gone(self)
148 return
149 end
150 if (self.back_off or 0) > 0 then
151 self.back_off -= dt
152 return
153 end
154 self.think -= dt
155 if self.think <= 0 then
156 self.think = world.random(0.6, 1)
157 if not world.nearest_player(self.position, LONELY) then
158 gone(self)
159 return
160 end
161 self.light = world.find_light(self.position, knobs.shy)
162 local best, best_d = nil, CHASE
163 for _, p in prey() do
164 local dist = vector.magnitude(p.position - self.position)
165 if dist < best_d then
166 best, best_d = p, dist
167 end
168 end
169 self.prey = if best then best.id else nil
170 end
171 if self.light then
172 away_from(self, self.light, 3, 2)
173 return
174 end
175 local p = nil
176 for _, q in prey() do
177 if q.id == self.prey then
178 p = q
179 end
180 end
181 if not p then
182 self:wander(0.8)
183 return
184 end
185 -- keep their distance: back off when close, come on when far
186 local far = flat(self.position, p.position)
187 if far < KEEP - 2 then
188 away_from(self, p.position, 2, 1.8)
189 elseif far > KEEP + 3 then
190 self:walk_to(p.position, 1.4)
191 else
192 self:stop()
193 self:face(p.position)
194 end
195 self.throw_in -= dt
196 if self.throw_in <= 0 then
197 self.throw_in = knobs.every * d.pace * world.random(0.8, 1.25)
198 if vector.magnitude(p.position - self.position) < REACH then
199 -- lobbed: aimed high by as much as the puff will fall on its way (shots
200 -- fall at 16 blocks a second each second, times their drop)
201 local t = vector.magnitude(p.position - self.position) / SPEED
202 local lift = 0.5 * 16 * DROP * t * t
203 local ok = pcall(function()
204 self:throw({ at = p.position + UP * (0.9 + lift), look = "spores", speed = SPEED, drop = DROP, power = 1 }, sting)
205 end)
206 if ok then
207 sound.play("puff", { at = self, volume = 0.7 })
208 end
209 end
210 end
211end
212
213function Puffcap:hit(player, shot)
214 local held = world.held(player)
215 local bop = if shot then shot.power else bops(held) * world.might(player)
216 self.bopped = (self.bopped or 0) + bop
217 -- bopped by hand, it puffs in your face
218 if not shot and knobs.sting > 0 then
219 world.hurt(player, math.max(0.5, knobs.sting * 0.5 * danger().hurt))
220 sound.play_for(player, "cough", { caption = "Spores make you cough" })
221 end
222 if self.bopped < knobs.bops then
223 away_from(self, player.position, 3, 2.5)
224 self.back_off = 1
225 sound.play("puff", { at = self, volume = 0.6, pitch = 1.3 })
226 return
227 end
228 sound.play("pop", { at = self, pitch = 0.8, caption = "A puffcap pops" })
229 world.give(player, "Puffcap", math.floor(world.random(1, 2.999)))
230 world.reward(player, 3)
231 gone(self)
232end
233
234-- somewhere one can come out: two blocks of air on the ground, in a cave,
235-- out of the light
236local function hole(x: number, y: number, z: number): vector?
237 local at = vector.create(math.floor(x) + 0.5, y, math.floor(z) + 0.5)
238 for dy = 3, -3, -1 do
239 local p = at + UP * dy
240 if world.block(p) == "Air" and world.block(p + UP) == "Air" and solid(p - UP) and below_ground(p) then
241 if not world.find_light(p, knobs.shy + 1) then
242 return vector.create(p.x, math.floor(p.y) + 0.05, p.z)
243 end
244 return nil
245 end
246 end
247 return nil
248end
249
250world.every(4, function()
251 local d = danger()
252 local want = math.floor(knobs.how_many * d.many + 0.5)
253 if want < 1 then
254 return
255 end
256 local live = {}
257 for _, id in world.creatures() do
258 live[id] = true
259 end
260 local total = 0
261 for id in where do
262 if live[id] then
263 total += 1
264 else
265 where[id] = nil
266 end
267 end
268 for _, p in prey() do
269 -- the Long Dusk has creatures of its own
270 if world.place(p.position) == "dusk" then
271 continue
272 end
273 local near = 0
274 for _, at in where do
275 if vector.magnitude(at - p.position) < CHASE + 4 then
276 near += 1
277 end
278 end
279 if total < MOST and near < want then
280 for _ = 1, 4 do
281 local a = world.random(0, 2 * math.pi)
282 local r = world.random(NEAR, FAR)
283 local at = hole(p.position.x + math.cos(a) * r, math.floor(p.position.y), p.position.z + math.sin(a) * r)
284 if at then
285 where[world.spawn(Puffcap, at)] = at
286 total += 1
287 break
288 end
289 end
290 end
291 end
292end)