Long dark eels that hunt in deep water at night, and in the dark water of caves at any hour. Swim out there in survival and they come for you, snap and dart back, so climb out or fight. Bop one three times (a sword or a spear bops harder) and it leaves an eel to cook. None on a Peaceful world, and fewer on a Gentle one.
| 1 | --!strict |
| 2 | -- Snapjaw eels: long dark eels that hunt in deep water at night, and in |
| 3 | -- the dark water of caves at any hour (swims, world 0.31). Swim out there |
| 4 | -- in survival and they come for you, snap, and dart back to come again; |
| 5 | -- climb out of the water and they lose interest. Bop one three times (a |
| 6 | -- sword, a spear or an arrow bops harder) and it leaves an eel, which a |
| 7 | -- furnace or a campfire cooks into cooked fish. How many come and how hard |
| 8 | -- they snap go by the world's danger (world.danger): none on Peaceful. |
| 9 | |
| 10 | local world = require("@potluck/world@0.33") |
| 11 | local tweak = require("@potluck/tweak") |
| 12 | local sound = require("@potluck/sound@0.2") |
| 13 | |
| 14 | local knobs = tweak { |
| 15 | how_many = { 2, min = 0, max = 6, label = "How many come for you in dark water (0 for none)", order = 1 }, |
| 16 | snap = { 2, min = 0, max = 6, label = "How hard they snap", order = 2 }, |
| 17 | bops = { 3, min = 1, max = 8, label = "Bops to beat one by hand", order = 3 }, |
| 18 | color = { "#2e3a2a", label = "Eel color", order = 4 }, |
| 19 | } |
| 20 | |
| 21 | local DANGER: { [string]: { many: number, hurt: number, fast: number } } = { |
| 22 | peaceful = { many = 0, hurt = 0, fast = 0 }, |
| 23 | gentle = { many = 0.5, hurt = 0.5, fast = 2.2 }, |
| 24 | normal = { many = 1, hurt = 1, fast = 2.6 }, |
| 25 | hard = { many = 1.5, hurt = 1.5, fast = 3 }, |
| 26 | } |
| 27 | local function danger() |
| 28 | return DANGER[world.danger()] or DANGER.gentle |
| 29 | end |
| 30 | |
| 31 | local SWORDS: { [string]: number } = { |
| 32 | ["Wooden sword"] = 2, |
| 33 | ["Stone sword"] = 3, |
| 34 | ["Copper sword"] = 3, |
| 35 | ["Iron sword"] = 4, |
| 36 | ["Moonsilver sword"] = 5, |
| 37 | Spear = 3, |
| 38 | } |
| 39 | |
| 40 | -- what's in hand bops this hard: a sword, a weapon another creation made |
| 41 | -- by its power (world.item), or a hand |
| 42 | local function bops(held: string?): number |
| 43 | if not held then |
| 44 | return 1 |
| 45 | end |
| 46 | if SWORDS[held] then |
| 47 | return SWORDS[held] |
| 48 | end |
| 49 | local it = world.item(held) |
| 50 | return if it and it.kind == "weapon" then it.power or 1 else 1 |
| 51 | end |
| 52 | |
| 53 | local BELLY = "#7a8a50" |
| 54 | local Eel = world.creature { |
| 55 | name = "Snapjaw eel", |
| 56 | speed = 2.6, |
| 57 | count = 0, |
| 58 | home = "here", |
| 59 | swims = true, |
| 60 | size = { 0.36, 0.3, 0.36 }, |
| 61 | behaviors = { wander = false }, |
| 62 | body = { |
| 63 | { "head", size = { 0.22, 0.24, 0.36 }, at = { 0, 0.15, -0.7 }, color = knobs.color, head = true }, |
| 64 | { "jaw", size = { 0.18, 0.06, 0.28 }, at = { 0, 0.04, -0.72 }, color = BELLY, head = true }, |
| 65 | { "tooth_left", size = { 0.03, 0.05, 0.03 }, at = { -0.07, 0.08, -0.84 }, color = "#e8e4d0", head = true }, |
| 66 | { "tooth_right", size = { 0.03, 0.05, 0.03 }, at = { 0.07, 0.08, -0.84 }, color = "#e8e4d0", head = true }, |
| 67 | { "eye_left", size = { 0.03, 0.06, 0.06 }, at = { -0.12, 0.21, -0.78 }, color = "#e8d040", glow = true, head = true }, |
| 68 | { "eye_right", size = { 0.03, 0.06, 0.06 }, at = { 0.12, 0.21, -0.78 }, color = "#e8d040", glow = true, head = true }, |
| 69 | { "neck", size = { 0.2, 0.22, 0.4 }, at = { 0, 0.14, -0.34 }, color = knobs.color, leg = true }, |
| 70 | { "middle", size = { 0.2, 0.2, 0.4 }, at = { 0, 0.14, 0.02 }, color = knobs.color }, |
| 71 | { "belly", size = { 0.16, 0.04, 0.8 }, at = { 0, 0.04, -0.15 }, color = BELLY }, |
| 72 | { "back", size = { 0.18, 0.18, 0.4 }, at = { 0, 0.13, 0.38 }, color = knobs.color, leg = true }, |
| 73 | { "tail", size = { 0.12, 0.16, 0.36 }, at = { 0, 0.13, 0.74 }, color = knobs.color }, |
| 74 | { "fin", size = { 0.02, 0.08, 1.2 }, at = { 0, 0.27, 0.2 }, color = "#4a5a38" }, |
| 75 | }, |
| 76 | } |
| 77 | |
| 78 | local MOST = 24 |
| 79 | local NEAR, FAR = 8, 15 |
| 80 | local CHASE = 20 |
| 81 | local LONELY = 40 |
| 82 | local SNAP_EVERY = 1.6 |
| 83 | local UP = vector.create(0, 1, 0) |
| 84 | |
| 85 | local where: { [number]: vector } = {} |
| 86 | |
| 87 | local function water(at: vector): boolean |
| 88 | return world.block(at) == "Water" |
| 89 | end |
| 90 | |
| 91 | -- in water with the dark round them: at night, or under the ground |
| 92 | local function in_dark_water(p): boolean |
| 93 | if not (water(p.position) or water(p.position + UP)) then |
| 94 | return false |
| 95 | end |
| 96 | return world.daylight() < 0.2 or p.position.y < world.top(p.position.x, p.position.z) - 4 |
| 97 | end |
| 98 | |
| 99 | local seen_at, seen = -1, {} |
| 100 | local function prey() |
| 101 | local now = world.time() |
| 102 | if now ~= seen_at then |
| 103 | seen_at, seen = now, {} |
| 104 | for _, p in world.players() do |
| 105 | if world.pockets(p) ~= nil and in_dark_water(p) then |
| 106 | table.insert(seen, p) |
| 107 | end |
| 108 | end |
| 109 | end |
| 110 | return seen |
| 111 | end |
| 112 | |
| 113 | local function gone(self) |
| 114 | where[self.id] = nil |
| 115 | self:remove() |
| 116 | end |
| 117 | |
| 118 | function Eel:spawned() |
| 119 | self.snap_in = SNAP_EVERY |
| 120 | self.think = 0 |
| 121 | sound.play("splash", { at = self, volume = 0.3, pitch = 0.6, caption = "Something stirs in the water" }) |
| 122 | end |
| 123 | |
| 124 | local function back_off(self, from: vector, far: number) |
| 125 | local away = self.position - from |
| 126 | if vector.magnitude(away) < 0.1 then |
| 127 | away = vector.create(1, 0, 0) |
| 128 | end |
| 129 | local spot = self.position + vector.normalize(away) * far |
| 130 | if water(spot) then |
| 131 | self:walk_to(spot, 3.4) |
| 132 | end |
| 133 | self.back_off = 1.4 |
| 134 | end |
| 135 | |
| 136 | function Eel:tick(dt: number) |
| 137 | where[self.id] = self.position |
| 138 | local d = danger() |
| 139 | if d.many == 0 or knobs.how_many < 1 then |
| 140 | gone(self) |
| 141 | return |
| 142 | end |
| 143 | if not water(self.position + UP * 0.15) then |
| 144 | -- flopped out somehow: it slips away |
| 145 | gone(self) |
| 146 | return |
| 147 | end |
| 148 | if (self.back_off or 0) > 0 then |
| 149 | self.back_off -= dt |
| 150 | return |
| 151 | end |
| 152 | self.think -= dt |
| 153 | if self.think <= 0 then |
| 154 | self.think = world.random(0.4, 0.7) |
| 155 | if not world.nearest_player(self.position, LONELY) then |
| 156 | gone(self) |
| 157 | return |
| 158 | end |
| 159 | local best, best_d = nil, CHASE |
| 160 | for _, p in prey() do |
| 161 | local dist = vector.magnitude(p.position - self.position) |
| 162 | if dist < best_d then |
| 163 | best, best_d = p, dist |
| 164 | end |
| 165 | end |
| 166 | self.prey = if best then best.id else nil |
| 167 | if not best then |
| 168 | -- nobody in the water: back into the dark after a while |
| 169 | self.idle = (self.idle or 0) + 1 |
| 170 | if self.idle > 20 then |
| 171 | gone(self) |
| 172 | return |
| 173 | end |
| 174 | else |
| 175 | self.idle = 0 |
| 176 | end |
| 177 | end |
| 178 | local p = nil |
| 179 | for _, q in prey() do |
| 180 | if q.id == self.prey then |
| 181 | p = q |
| 182 | end |
| 183 | end |
| 184 | if not p then |
| 185 | self:wander(1.2) |
| 186 | return |
| 187 | end |
| 188 | local mouth = p.position + UP * 0.7 |
| 189 | local close = vector.magnitude(mouth - self.position) |
| 190 | self.snap_in -= dt |
| 191 | if close < 1.5 then |
| 192 | self:face(p.position) |
| 193 | if self.snap_in <= 0 then |
| 194 | self.snap_in = SNAP_EVERY |
| 195 | if knobs.snap > 0 and world.hurt(p, knobs.snap * d.hurt) then |
| 196 | sound.play("snap", { at = self, volume = 0.8 }) |
| 197 | end |
| 198 | back_off(self, mouth, 3) |
| 199 | end |
| 200 | else |
| 201 | self:walk_to(mouth, d.fast) |
| 202 | end |
| 203 | end |
| 204 | |
| 205 | function Eel:hit(player, shot) |
| 206 | local bop = if shot then shot.power else bops(world.held(player)) * world.might(player) |
| 207 | self.bopped = (self.bopped or 0) + bop |
| 208 | if self.bopped < knobs.bops then |
| 209 | back_off(self, player.position + UP, 4) |
| 210 | self.snap_in = 1.5 |
| 211 | sound.play("splash", { at = self, volume = 0.5, pitch = 1.2 }) |
| 212 | return |
| 213 | end |
| 214 | sound.play("splash", { at = self, volume = 0.7, pitch = 0.8, caption = "An eel is beaten" }) |
| 215 | world.give(player, "Eel", 1) |
| 216 | world.reward(player, 3) |
| 217 | gone(self) |
| 218 | end |
| 219 | |
| 220 | -- Every few seconds: one more near each player swimming in dark water, |
| 221 | -- with fewer near them than the knob and the danger say. |
| 222 | world.every(3, function() |
| 223 | local d = danger() |
| 224 | local want = math.floor(knobs.how_many * d.many + 0.5) |
| 225 | if want < 1 then |
| 226 | return |
| 227 | end |
| 228 | local live = {} |
| 229 | for _, id in world.creatures() do |
| 230 | live[id] = true |
| 231 | end |
| 232 | local total = 0 |
| 233 | for id in where do |
| 234 | if live[id] then |
| 235 | total += 1 |
| 236 | else |
| 237 | where[id] = nil |
| 238 | end |
| 239 | end |
| 240 | for _, p in prey() do |
| 241 | -- the Long Dusk has creatures of its own |
| 242 | if world.place(p.position) == "dusk" then |
| 243 | continue |
| 244 | end |
| 245 | local near = 0 |
| 246 | for _, at in where do |
| 247 | if vector.magnitude(at - p.position) < CHASE + 4 then |
| 248 | near += 1 |
| 249 | end |
| 250 | end |
| 251 | if total >= MOST or near >= want then |
| 252 | continue |
| 253 | end |
| 254 | for _ = 1, 4 do |
| 255 | local a = world.random(0, 2 * math.pi) |
| 256 | local r = world.random(NEAR, FAR) |
| 257 | local at = vector.create(math.floor(p.position.x + math.cos(a) * r) + 0.5, math.floor(p.position.y) - world.random(0, 2), math.floor(p.position.z + math.sin(a) * r) + 0.5) |
| 258 | at = vector.create(at.x, math.floor(at.y) + 0.3, at.z) |
| 259 | if water(at) and water(at + UP) then |
| 260 | where[world.spawn(Eel, at)] = at |
| 261 | total += 1 |
| 262 | break |
| 263 | end |
| 264 | end |
| 265 | end |
| 266 | end) |