Sign in
creature

Rockjaws

by Potluck · version 1 · Checked for all ages

Stony things that lie in wait in the dark of caves, looking just like a lump of rock against the wall. Come too close in survival and one wakes, lunges and bites, then lumbers after you before settling down as a rock again. Look for the seam of its mouth. They're hard as stone, so a pickaxe beats them twice as fast, and a beaten one leaves flint and sometimes whatever ore it swallowed. None on a Peaceful world, and fewer on a Gentle one.

1--!strict
2-- Rockjaws: stony things that lie in wait in the dark of caves, looking
3-- for all the world like a lump of rock against the wall. Come within a
4-- few blocks in survival and one wakes (self:become), lunges and bites,
5-- then lumbers after you for a while before settling down as a rock again
6-- wherever it is. Light doesn't scare them, but they never settle in it.
7-- They're hard as stone, so a pickaxe bops them twice as hard as a hand,
8-- and a beaten one leaves flint and sometimes whatever ore it swallowed.
9-- How many lie about and how hard they bite go by the world's danger
10-- (world.danger): none on Peaceful.
11
12local world = require("@potluck/world@0.33")
13local tweak = require("@potluck/tweak")
14local sound = require("@potluck/sound@0.2")
15
16local knobs = tweak {
17 how_many = { 2, min = 0, max = 6, label = "How many lie in wait about you in caves (0 for none)", order = 1 },
18 bite = { 3, min = 0, max = 8, label = "How hard they bite", order = 2 },
19 wakes = { 3.5, min = 1.5, max = 8, label = "How close you come before one wakes", order = 3 },
20 bops = { 6, min = 2, max = 16, label = "Bops to beat one by hand", order = 4 },
21}
22
23local DANGER: { [string]: { many: number, hurt: number, fast: number } } = {
24 peaceful = { many = 0, hurt = 0, fast = 0 },
25 gentle = { many = 0.5, hurt = 0.5, fast = 2 },
26 normal = { many = 1, hurt = 1, fast = 2.4 },
27 hard = { many = 1.5, hurt = 1.5, fast = 2.8 },
28}
29local function danger()
30 return DANGER[world.danger()] or DANGER.gentle
31end
32
33local SWORDS: { [string]: number } = {
34 ["Wooden sword"] = 2,
35 ["Stone sword"] = 3,
36 ["Copper sword"] = 3,
37 ["Iron sword"] = 4,
38 ["Moonsilver sword"] = 5,
39}
40
41-- what's in hand bops this hard: any pickaxe twice what a hand does, a
42-- sword, a weapon another creation made by its power, or a hand
43local function bops(held: string?): number
44 if not held then
45 return 1
46 end
47 if string.find(string.lower(held), "pickaxe") then
48 return 2.5
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 STONE = "#7c7a74"
58local DARK = "#5a5854"
59local LIGHT = "#9a9890"
60local EYE = "#f0a030"
61
62local function rock(awake: boolean): { any }
63 local b: { any } = {
64 { "lump", size = { 0.9, 0.62, 0.9 }, at = { 0, 0.31, 0.02 }, color = STONE },
65 { "top", size = { 0.6, 0.2, 0.62 }, at = { 0.05, 0.7, 0.08 }, color = DARK },
66 { "chip_1", size = { 0.24, 0.16, 0.2 }, at = { -0.3, 0.58, 0.25 }, color = LIGHT },
67 { "chip_2", size = { 0.2, 0.14, 0.2 }, at = { 0.3, 0.2, 0.3 }, color = DARK },
68 { "chip_3", size = { 0.16, 0.12, 0.16 }, at = { -0.28, 0.16, -0.36 }, color = LIGHT },
69 }
70 if awake then
71 table.insert(b, { "jaw", size = { 0.84, 0.18, 0.3 }, at = { 0, 0.12, -0.4 }, color = DARK, head = true })
72 table.insert(b, { "mouth", size = { 0.7, 0.1, 0.04 }, at = { 0, 0.26, -0.47 }, color = "#2a1410", head = true })
73 for i, x in { -0.24, -0.08, 0.08, 0.24 } do
74 table.insert(b, { "tooth_" .. i, size = { 0.07, 0.08, 0.04 }, at = { x, 0.33, -0.48 }, color = "#e8e0cc", head = true })
75 end
76 table.insert(b, { "eye_left", size = { 0.12, 0.08, 0.04 }, at = { -0.2, 0.48, -0.47 }, color = EYE, glow = true, head = true })
77 table.insert(b, { "eye_right", size = { 0.12, 0.08, 0.04 }, at = { 0.2, 0.48, -0.47 }, color = EYE, glow = true, head = true })
78 for _, s in { -1, 1 } do
79 local n = if s < 0 then "left" else "right"
80 table.insert(b, { "leg_front_" .. n, size = { 0.2, 0.18, 0.2 }, at = { s * 0.36, 0.06, -0.22 }, color = DARK, leg = true })
81 table.insert(b, { "leg_back_" .. n, size = { 0.2, 0.18, 0.2 }, at = { s * 0.36, 0.06, 0.3 }, color = DARK, leg = true })
82 end
83 else
84 -- a seam where its mouth is, if you look closely
85 table.insert(b, { "seam", size = { 0.6, 0.03, 0.03 }, at = { 0, 0.26, -0.44 }, color = DARK })
86 end
87 return b
88end
89
90local Still = world.creature {
91 name = "Rockjaw (still)",
92 speed = 0,
93 count = 0,
94 home = "here",
95 size = { 0.8, 0.7, 0.8 },
96 behaviors = { wander = false },
97 body = rock(false),
98}
99
100local Rockjaw = world.creature {
101 name = "Rockjaw",
102 speed = 2.4,
103 count = 0,
104 home = "here",
105 size = { 0.8, 0.7, 0.8 },
106 behaviors = { wander = false },
107 body = rock(true),
108}
109
110local MOST = 20
111local NEAR, FAR = 10, 18
112local CHASE = 14
113local LONELY = 48
114local BITE_EVERY = 1.6
115local AWAKE = 12 -- seconds it keeps after someone who's got away
116local DEEP = 4
117local UP = vector.create(0, 1, 0)
118local SIDES = { vector.create(1, 0, 0), vector.create(-1, 0, 0), vector.create(0, 0, 1), vector.create(0, 0, -1) }
119
120-- what it may have swallowed, by its chance: only what's in this world
121local SWALLOWED = {
122 { thing = "Coal", chance = 0.2 },
123 { thing = "Raw copper", chance = 0.12 },
124 { thing = "Raw iron", chance = 0.08 },
125}
126
127local where: { [number]: vector } = {}
128
129local function flat(a: vector, b: vector): number
130 local dx, dz = a.x - b.x, a.z - b.z
131 return math.sqrt(dx * dx + dz * dz)
132end
133
134local function solid(at: vector): boolean
135 local ok, kind = pcall(world.kind, world.block(at))
136 return ok and kind ~= "air" and kind ~= "water" and kind ~= "plant"
137end
138
139local function below_ground(at: vector): boolean
140 return at.y < world.top(at.x, at.z) - DEEP
141end
142
143local seen_at, seen = -1, {}
144local function prey()
145 local now = world.time()
146 if now ~= seen_at then
147 seen_at, seen = now, {}
148 for _, p in world.players() do
149 if world.pockets(p) ~= nil and below_ground(p.position) then
150 table.insert(seen, p)
151 end
152 end
153 end
154 return seen
155end
156
157local function nearest(pos: vector, within: number)
158 local best, best_d = nil, within
159 for _, p in prey() do
160 local dist = vector.magnitude(p.position - pos)
161 if dist < best_d then
162 best, best_d = p, dist
163 end
164 end
165 return best
166end
167
168local function gone(self)
169 where[self.id] = nil
170 self:remove()
171end
172
173function Still:spawned()
174 self.think = world.random(0, 0.5)
175end
176
177local function wake(self)
178 self:become(Rockjaw)
179 self.bite_in = 0.4
180 self.lost = 0
181 self.lunged = false
182 sound.play("grind", { at = self, volume = 0.9, caption = "A rock wakes up" })
183end
184
185function Still:tick(dt: number)
186 where[self.id] = self.position
187 self.think -= dt
188 if self.think > 0 then
189 return
190 end
191 self.think = world.random(0.25, 0.4)
192 local d = danger()
193 if d.many == 0 or knobs.how_many < 1 or not world.nearest_player(self.position, LONELY) then
194 gone(self)
195 return
196 end
197 local p = nearest(self.position, knobs.wakes)
198 if p then
199 wake(self)
200 end
201end
202
203function Rockjaw:tick(dt: number)
204 where[self.id] = self.position
205 local d = danger()
206 if d.many == 0 or knobs.how_many < 1 then
207 gone(self)
208 return
209 end
210 if (self.back_off or 0) > 0 then
211 self.back_off -= dt
212 return
213 end
214 local p = nearest(self.position, CHASE)
215 if not p then
216 -- they got away: it settles down as a rock again, out of the light
217 self.lost = (self.lost or 0) + dt
218 self:stop()
219 if self.lost > AWAKE and not world.find_light(self.position, 4) then
220 self:become(Still)
221 self.think = 1
222 end
223 return
224 end
225 self.lost = 0
226 self.bite_in = (self.bite_in or 0) - dt
227 local close = flat(self.position, p.position)
228 if close < 1.5 and math.abs(self.position.y - p.position.y) < 1.4 then
229 self:face(p.position)
230 self:stop()
231 if self.bite_in <= 0 then
232 self.bite_in = BITE_EVERY
233 if knobs.bite > 0 and world.hurt(p, knobs.bite * d.hurt) then
234 sound.play("chomp", { at = self, volume = 0.9 })
235 end
236 end
237 else
238 self:walk_to(p.position, d.fast)
239 -- its first move is a lunge
240 if not self.lunged and self:on_ground() then
241 self.lunged = true
242 self:hop(0.8)
243 self:walk_to(p.position, d.fast * 2)
244 end
245 end
246end
247
248function Rockjaw:hit(player, shot)
249 local bop = if shot then shot.power else bops(world.held(player)) * world.might(player)
250 self.bopped = (self.bopped or 0) + bop
251 if self.bopped < knobs.bops then
252 sound.play("crack", { at = self, volume = 0.6, pitch = 1.3 })
253 self.back_off = 0.5
254 self.bite_in = math.max(self.bite_in or 0, 0.8)
255 return
256 end
257 sound.play("crack", { at = self, pitch = 0.6, caption = "A rockjaw crumbles" })
258 world.give(player, "Flint", math.floor(world.random(1, 2.999)))
259 for _, s in SWALLOWED do
260 if world.random() < s.chance and pcall(world.kind, s.thing) then
261 world.give(player, s.thing, 1)
262 break
263 end
264 end
265 world.reward(player, 6)
266 gone(self)
267end
268
269-- woken by a hit while still: it wakes and goes for whoever did it
270function Still:hit(player, shot)
271 wake(self)
272 Rockjaw.hit(self, player, shot)
273end
274
275-- somewhere one can lie: a cave floor out of the light, as near a wall
276-- as it can find going on away from the player
277local function floor_at(p: vector): vector?
278 for dy = 3, -3, -1 do
279 local q = p + UP * dy
280 if world.block(q) == "Air" and world.block(q + UP) == "Air" and solid(q - UP) and below_ground(q) then
281 return vector.create(q.x, math.floor(q.y), q.z)
282 end
283 end
284 return nil
285end
286
287local function nook(x: number, y: number, z: number, from: vector): vector?
288 local at = floor_at(vector.create(math.floor(x) + 0.5, y, math.floor(z) + 0.5))
289 if not at then
290 return nil
291 end
292 local out = vector.create(at.x - from.x, 0, at.z - from.z)
293 out = if vector.magnitude(out) > 0.1 then vector.normalize(out) else vector.create(1, 0, 0)
294 local spot = at
295 for _ = 1, 6 do
296 local beside = false
297 for _, s in SIDES do
298 if solid(spot + s) then
299 beside = true
300 end
301 end
302 if beside then
303 break
304 end
305 local step_to = vector.create(math.floor(spot.x + out.x) + 0.5, spot.y, math.floor(spot.z + out.z) + 0.5)
306 local ahead = floor_at(step_to)
307 if not ahead then
308 break
309 end
310 spot = ahead
311 end
312 if world.find_light(spot, 6) then
313 return nil
314 end
315 return vector.create(spot.x, spot.y + 0.02, spot.z)
316end
317
318-- Every few seconds: one more lies in wait somewhere ahead of each player
319-- in survival down a cave, with fewer near them than the knob and the
320-- danger say.
321world.every(5, function()
322 local d = danger()
323 local want = math.floor(knobs.how_many * d.many + 0.5)
324 if want < 1 then
325 return
326 end
327 local live = {}
328 for _, id in world.creatures() do
329 live[id] = true
330 end
331 local total = 0
332 for id in where do
333 if live[id] then
334 total += 1
335 else
336 where[id] = nil
337 end
338 end
339 for _, p in prey() do
340 -- the Long Dusk has creatures of its own
341 if world.place(p.position) == "dusk" then
342 continue
343 end
344 local near = 0
345 for _, at in where do
346 if vector.magnitude(at - p.position) < FAR + 8 then
347 near += 1
348 end
349 end
350 if total < MOST and near < want then
351 for _ = 1, 4 do
352 local a = world.random(0, 2 * math.pi)
353 local r = world.random(NEAR, FAR)
354 local at = nook(p.position.x + math.cos(a) * r, math.floor(p.position.y), p.position.z + math.sin(a) * r, p.position)
355 if at then
356 where[world.spawn(Still, at)] = at
357 total += 1
358 break
359 end
360 end
361 end
362 end
363end)