The sun never moves in the Long Dusk. Make a Dusk mirror (glass, gold and crystal, at the Workbench) and use it to step into the Gold, where duskwood hangs with ember pods and lanternbacks graze. West lie the salt flats of the Glare, where glints wait and sunstone runs under the crust; east, the Gloaming and the Long Night, frozen under the stars, where starglass grows and hushers drift. Sunstone, starglass and moonsilver make evensteel. A mirror waits where you came out, to take you home.
| 1 | --!strict |
| 2 | -- The Long Dusk: a land far past the wild where the sun never moves, in a |
| 3 | -- band of the world nobody can walk to (world.place calls it "dusk"). |
| 4 | -- West to east it runs from the Glare, salt flats and mesas under a high |
| 5 | -- sun, through the Gold, copper-leaved duskwood at the golden hour, and the |
| 6 | -- Gloaming, to the Long Night, frozen under the stars. |
| 7 | -- |
| 8 | -- A Dusk mirror (glass, gold and crystal, at the Workbench) is the way |
| 9 | -- there. Use one at home and it clouds over, and you step through |
| 10 | -- (world.send) into the Gold, where this creation builds a mirror on a |
| 11 | -- dais of duskstone bricks for the way back. Using that one takes you to |
| 12 | -- the mirror you came through. A mirror put down in the Long Dusk takes |
| 13 | -- you to the Green. Each pair of mirrors is kept with @potluck/store. |
| 14 | -- |
| 15 | -- Why go: sunstone under the Glare's salt (a moonsilver pickaxe breaks it), |
| 16 | -- starglass in the Long Night (an iron one), ember fruit hanging under the |
| 17 | -- duskwood, and evensteel, made of moonsilver, sunstone and starglass, for |
| 18 | -- the best tools and armor there are: an evensteel pickaxe is the only |
| 19 | -- thing that breaks nightglass. Glints lie still on the salt and dart out |
| 20 | -- to bite; hushers drift in the Long Night's dark and keep off light; and |
| 21 | -- lanternbacks graze in the Gold with a lamp on their backs, and follow |
| 22 | -- whoever holds ember fruit, so one led into the night keeps hushers off. |
| 23 | -- No glints or hushers on a Peaceful world. |
| 24 | |
| 25 | local world = require("@potluck/world@0.33") |
| 26 | local tweak = require("@potluck/tweak") |
| 27 | local store = require("@potluck/store") |
| 28 | local ui = require("@potluck/ui@0.2") |
| 29 | local sound = require("@potluck/sound@0.2") |
| 30 | |
| 31 | local knobs = tweak { |
| 32 | glints = { 3, min = 0, max = 8, label = "Glints near you on the salt (0 for none)", order = 1 }, |
| 33 | hushers = { 2, min = 0, max = 6, label = "Hushers near you in the Long Night (0 for none)", order = 2 }, |
| 34 | lanternbacks = { 3, min = 0, max = 8, label = "Lanternbacks grazing near you in the Gold", order = 3 }, |
| 35 | bite = { 2, min = 0, max = 8, label = "How hard a glint bites", order = 4 }, |
| 36 | touch = { 3, min = 0, max = 8, label = "How hard a husher's touch hurts", order = 5 }, |
| 37 | shy = { 6, min = 2, max = 12, label = "How far hushers keep from a light", order = 6 }, |
| 38 | glow = { 0.7, min = 0.1, max = 1, label = "How brightly a lanternback's lamp glows", order = 7 }, |
| 39 | } |
| 40 | |
| 41 | local MIRROR, LAMP, BRICKS = "Dusk mirror", "Sun lamp", "Duskstone bricks" |
| 42 | local GOLD_X = 24600 -- mirrors come out between here and 256 east: the Gold |
| 43 | local EDGE = 29000 -- and never past this far north or south |
| 44 | local SEA = 19 -- the water's top |
| 45 | local UP = vector.create(0, 1, 0) |
| 46 | local MOST_PAIRS = 64 |
| 47 | |
| 48 | local function v(x: number, y: number, z: number): vector |
| 49 | return vector.create(x, y, z) |
| 50 | end |
| 51 | |
| 52 | local function key(at: vector): string |
| 53 | return string.format("%d,%d,%d", math.floor(at.x), math.floor(at.y), math.floor(at.z)) |
| 54 | end |
| 55 | |
| 56 | local function flat(a: vector, b: vector): number |
| 57 | local dx, dz = a.x - b.x, a.z - b.z |
| 58 | return math.sqrt(dx * dx + dz * dz) |
| 59 | end |
| 60 | |
| 61 | local function in_dusk(at: vector): boolean |
| 62 | return world.place(at) == "dusk" |
| 63 | end |
| 64 | |
| 65 | local shown: { [number]: number } = {} |
| 66 | local function tell(player, text: string) |
| 67 | ui.show(player, "dusk", text, { at = "bottom" }) |
| 68 | local mine = (shown[player.id] or 0) + 1 |
| 69 | shown[player.id] = mine |
| 70 | world.after(5, function() |
| 71 | if shown[player.id] == mine then |
| 72 | shown[player.id] = nil |
| 73 | ui.hide(player, "dusk") |
| 74 | end |
| 75 | end) |
| 76 | end |
| 77 | |
| 78 | world.on("player_left", function(p) |
| 79 | shown[p.id] = nil |
| 80 | end) |
| 81 | |
| 82 | -- ---- what the land gives ---- |
| 83 | |
| 84 | world.drops("Sunstone ore", { gives = "Sunstone", count = { 1, 2 }, level = 5 }) |
| 85 | world.drops("Starglass", { gives = "Starglass shard", count = { 1, 3 }, level = 4 }) |
| 86 | world.drops("Nightglass", { level = 6 }) |
| 87 | world.drops("Ember pod", { gives = "Ember fruit", count = { 1, 2 } }) |
| 88 | -- the mirrors' dais and lamps where they came out break as any do |
| 89 | |
| 90 | -- XP for the land's finds, once a block (what breaking gives is the drops') |
| 91 | local FIND_XP: { [string]: number } = { ["Sunstone ore"] = 9, Starglass = 5, Nightglass = 3 } |
| 92 | world.on("block_broken", function(player, _at: vector, block: string) |
| 93 | local xp = FIND_XP[block] |
| 94 | if xp and world.pockets(player) ~= nil then |
| 95 | world.reward(player, xp) |
| 96 | end |
| 97 | end) |
| 98 | |
| 99 | -- ---- mirrors ---- |
| 100 | |
| 101 | -- each pair: the mirror at home and the one in the Long Dusk |
| 102 | type Pair = { home: vector, dusk: vector } |
| 103 | local pairs_: { Pair } = store.get("pairs") or {} |
| 104 | local function save() |
| 105 | store.set("pairs", pairs_) |
| 106 | end |
| 107 | |
| 108 | local function pair_of(at: vector): Pair? |
| 109 | local k = key(at) |
| 110 | for _, p in pairs_ do |
| 111 | if key(p.home) == k or key(p.dusk) == k then |
| 112 | return p |
| 113 | end |
| 114 | end |
| 115 | return nil |
| 116 | end |
| 117 | |
| 118 | -- where to stand by a mirror: on the side it was put down from, or the |
| 119 | -- spot itself (the game finds the ground) |
| 120 | local SIDES: { [string]: vector } = { north = v(0, 0, 1), south = v(0, 0, -1), east = v(-1, 0, 0), west = v(1, 0, 0) } |
| 121 | local function before(at: vector): vector |
| 122 | local side = SIDES[world.facing(at) or "north"] or SIDES.north |
| 123 | return v(math.floor(at.x) + 0.5, math.floor(at.y), math.floor(at.z) + 0.5) + side |
| 124 | end |
| 125 | |
| 126 | -- where a mirror at home comes out: in the Gold, as far north or south as |
| 127 | -- it stands, and east or west by where it stands, on dry land |
| 128 | local function landing_for(home: vector): vector? |
| 129 | local x0 = GOLD_X + (math.floor(home.x) % 256) |
| 130 | local z0 = math.clamp(math.floor(home.z), -EDGE, EDGE) |
| 131 | for i = 0, 15 do |
| 132 | local a = i * 2.4 |
| 133 | local r = i * 6 |
| 134 | local x, z = math.floor(x0 + math.cos(a) * r), math.floor(z0 + math.sin(a) * r) |
| 135 | local top = world.top(x, z) |
| 136 | if top > SEA and in_dusk(v(x, 0, z)) then |
| 137 | return v(x, top + 1, z) |
| 138 | end |
| 139 | end |
| 140 | return nil |
| 141 | end |
| 142 | |
| 143 | -- the dais a mirror stands on where you come out: duskstone bricks 5 by |
| 144 | -- 5, cleared over and filled under, with a sun lamp at two corners |
| 145 | local function build_dais(at: vector) |
| 146 | for dx = -2, 2 do |
| 147 | for dz = -2, 2 do |
| 148 | local c = at + v(dx, -1, dz) |
| 149 | world.set_block(c, BRICKS) |
| 150 | for dy = 1, 3 do |
| 151 | if world.block(c - v(0, dy, 0)) == "Air" or world.block(c - v(0, dy, 0)) == "Water" then |
| 152 | world.set_block(c - v(0, dy, 0), "Duskstone") |
| 153 | end |
| 154 | end |
| 155 | for dy = 1, 3 do |
| 156 | world.set_block(c + v(0, dy, 0), "Air") |
| 157 | end |
| 158 | end |
| 159 | end |
| 160 | world.set_block(at + v(-2, 0, -2), LAMP) |
| 161 | world.set_block(at + v(2, 0, -2), LAMP) |
| 162 | world.set_block(at, MIRROR, "south") |
| 163 | end |
| 164 | |
| 165 | -- one trip at a time for each player, while a dais is built |
| 166 | local going: { [number]: boolean } = {} |
| 167 | |
| 168 | local function step_through(player, to: vector, name: string) |
| 169 | sound.play("mirror", { at = player.position }) |
| 170 | world.send(player, to, name) |
| 171 | end |
| 172 | |
| 173 | world.on("block_used", function(player, at: vector, block: string) |
| 174 | if block ~= MIRROR or going[player.id] then |
| 175 | return |
| 176 | end |
| 177 | local p = pair_of(at) |
| 178 | if in_dusk(at) then |
| 179 | if p then |
| 180 | step_through(player, before(p.home), "home") |
| 181 | else |
| 182 | -- a mirror put down here: home to the Green |
| 183 | step_through(player, world.spot("green"), "the Green") |
| 184 | end |
| 185 | return |
| 186 | end |
| 187 | if p then |
| 188 | step_through(player, before(p.dusk), "the Long Dusk") |
| 189 | return |
| 190 | end |
| 191 | if #pairs_ >= MOST_PAIRS then |
| 192 | tell(player, "This mirror stays clear: the dusk has room for no more.") |
| 193 | return |
| 194 | end |
| 195 | local out = landing_for(at) |
| 196 | if not out then |
| 197 | tell(player, "The glass clouds, then clears again. Try another spot.") |
| 198 | return |
| 199 | end |
| 200 | -- the far mirror first, then through it |
| 201 | going[player.id] = true |
| 202 | tell(player, "The glass clouds over, and warm light comes through.") |
| 203 | sound.play("mirror", { at = at }) |
| 204 | local pair: Pair = { home = v(math.floor(at.x), math.floor(at.y), math.floor(at.z)), dusk = out } |
| 205 | table.insert(pairs_, pair) |
| 206 | save() |
| 207 | world.after(0.1, function() |
| 208 | build_dais(out) |
| 209 | world.after(0.2, function() |
| 210 | going[player.id] = nil |
| 211 | step_through(player, before(out), "the Long Dusk") |
| 212 | end) |
| 213 | end) |
| 214 | end) |
| 215 | |
| 216 | -- a home mirror broken: its pair forgets it, and the one in the dusk goes |
| 217 | -- to the Green from then on |
| 218 | world.on("block_broken", function(_player, at: vector, block: string) |
| 219 | if block ~= MIRROR then |
| 220 | return |
| 221 | end |
| 222 | local k = key(at) |
| 223 | for i, p in pairs_ do |
| 224 | if key(p.home) == k or key(p.dusk) == k then |
| 225 | table.remove(pairs_, i) |
| 226 | save() |
| 227 | return |
| 228 | end |
| 229 | end |
| 230 | end) |
| 231 | |
| 232 | -- ---- telling players where they are ---- |
| 233 | |
| 234 | local LANDS: { [string]: string } = { |
| 235 | glare = "The Glare: the sun stands high over the salt.", |
| 236 | gold = "The Gold: the sun hangs low and never sets.", |
| 237 | gloaming = "The Gloaming: the last of the light.", |
| 238 | long_night = "The Long Night: frozen under the stars.", |
| 239 | } |
| 240 | local last_land: { [number]: string } = {} |
| 241 | |
| 242 | -- ---- the land's creatures ---- |
| 243 | |
| 244 | local DANGER: { [string]: { many: number, hurt: number, pace: number } } = { |
| 245 | peaceful = { many = 0, hurt = 0, pace = 0 }, |
| 246 | gentle = { many = 0.5, hurt = 0.5, pace = 1.4 }, |
| 247 | normal = { many = 1, hurt = 1, pace = 1 }, |
| 248 | hard = { many = 1.5, hurt = 1.5, pace = 0.75 }, |
| 249 | } |
| 250 | local function danger() |
| 251 | return DANGER[world.danger()] or DANGER.gentle |
| 252 | end |
| 253 | |
| 254 | local SWORDS: { [string]: number } = { |
| 255 | ["Wooden sword"] = 2, |
| 256 | ["Stone sword"] = 3, |
| 257 | ["Copper sword"] = 3, |
| 258 | ["Iron sword"] = 4, |
| 259 | ["Moonsilver sword"] = 5, |
| 260 | } |
| 261 | local function bops(held: string?): number |
| 262 | if not held then |
| 263 | return 1 |
| 264 | end |
| 265 | if SWORDS[held] then |
| 266 | return SWORDS[held] |
| 267 | end |
| 268 | local it = world.item(held) |
| 269 | return if it and it.kind == "weapon" then it.power or 1 else 1 |
| 270 | end |
| 271 | |
| 272 | local LIGHTS: { [string]: boolean } = { Torch = true, Lantern = true, Candle = true, Brazier = true, ["Sun lamp"] = true } |
| 273 | -- a light near a place: put down, a lit creature (a lanternback), or in the |
| 274 | -- hand of a player near it |
| 275 | local function light_near(at: vector, within: number): vector? |
| 276 | local lit = world.find_light(at, within) |
| 277 | if lit then |
| 278 | return lit |
| 279 | end |
| 280 | for _, p in world.players() do |
| 281 | local held = world.held(p) |
| 282 | if held and LIGHTS[held] and vector.magnitude(p.position - at) < within then |
| 283 | return p.position |
| 284 | end |
| 285 | end |
| 286 | return nil |
| 287 | end |
| 288 | |
| 289 | -- lanternbacks: broad, slow grazers with a lamp of their own on a stalk |
| 290 | local Lanternback = world.creature { |
| 291 | name = "Lanternback", |
| 292 | speed = 1.2, |
| 293 | count = 0, |
| 294 | home = "here", |
| 295 | size = { 1.4, 1.4, 1.8 }, |
| 296 | behaviors = { wander = true, follow_food = "Ember fruit", chatter = { "Hrrm.", "Mmm." }, sound = "low" }, |
| 297 | body = { |
| 298 | { "body", size = { 1.3, 0.8, 1.6 }, at = { 0, 0.85, 0 }, color = "#6e4a3a" }, |
| 299 | { "shell", size = { 1.1, 0.35, 1.3 }, at = { 0, 1.35, 0.05 }, color = "#a86a3c" }, |
| 300 | { "head", size = { 0.6, 0.55, 0.55 }, at = { 0, 0.9, -1.0 }, color = "#7e5644", head = true }, |
| 301 | { "eye_left", size = { 0.1, 0.1, 0.04 }, at = { -0.18, 1.0, -1.28 }, color = "#1a1210", head = true }, |
| 302 | { "eye_right", size = { 0.1, 0.1, 0.04 }, at = { 0.18, 1.0, -1.28 }, color = "#1a1210", head = true }, |
| 303 | { "stalk", size = { 0.08, 0.6, 0.08 }, at = { 0, 1.8, 0.3 }, color = "#4a3020" }, |
| 304 | { "lamp", size = { 0.3, 0.3, 0.3 }, at = { 0, 2.2, 0.3 }, color = "#ffc868", glow = true, light = knobs.glow }, |
| 305 | { "leg_1", size = { 0.3, 0.45, 0.3 }, at = { -0.45, 0.22, -0.55 }, color = "#5a3a2c", leg = true }, |
| 306 | { "leg_2", size = { 0.3, 0.45, 0.3 }, at = { 0.45, 0.22, -0.55 }, color = "#5a3a2c", leg = true }, |
| 307 | { "leg_3", size = { 0.3, 0.45, 0.3 }, at = { -0.45, 0.22, 0.55 }, color = "#5a3a2c", leg = true }, |
| 308 | { "leg_4", size = { 0.3, 0.45, 0.3 }, at = { 0.45, 0.22, 0.55 }, color = "#5a3a2c", leg = true }, |
| 309 | }, |
| 310 | } |
| 311 | |
| 312 | -- glints: salt-white lizards that lie still on the Glare's crust, then |
| 313 | -- dart out to bite whoever comes near |
| 314 | local Glint = world.creature { |
| 315 | name = "Glint", |
| 316 | speed = 4.5, |
| 317 | count = 0, |
| 318 | home = "here", |
| 319 | size = { 0.7, 0.4, 0.9 }, |
| 320 | behaviors = { wander = false }, |
| 321 | body = { |
| 322 | { "body", size = { 0.5, 0.25, 0.8 }, at = { 0, 0.18, 0 }, color = "#e8dcc0" }, |
| 323 | { "back", size = { 0.3, 0.1, 0.6 }, at = { 0, 0.34, 0.02 }, color = "#fff6e0", glow = true }, |
| 324 | { "head", size = { 0.34, 0.2, 0.3 }, at = { 0, 0.2, -0.52 }, color = "#d8c8a4", head = true }, |
| 325 | { "eye_left", size = { 0.06, 0.06, 0.03 }, at = { -0.12, 0.26, -0.67 }, color = "#c84a1a", head = true, glow = true }, |
| 326 | { "eye_right", size = { 0.06, 0.06, 0.03 }, at = { 0.12, 0.26, -0.67 }, color = "#c84a1a", head = true, glow = true }, |
| 327 | { "tail", size = { 0.14, 0.1, 0.5 }, at = { 0, 0.14, 0.62 }, color = "#c8b894" }, |
| 328 | { "leg_1", size = { 0.12, 0.12, 0.12 }, at = { -0.3, 0.06, -0.25 }, color = "#c8b894", leg = true }, |
| 329 | { "leg_2", size = { 0.12, 0.12, 0.12 }, at = { 0.3, 0.06, -0.25 }, color = "#c8b894", leg = true }, |
| 330 | { "leg_3", size = { 0.12, 0.12, 0.12 }, at = { -0.3, 0.06, 0.25 }, color = "#c8b894", leg = true }, |
| 331 | { "leg_4", size = { 0.12, 0.12, 0.12 }, at = { 0.3, 0.06, 0.25 }, color = "#c8b894", leg = true }, |
| 332 | }, |
| 333 | } |
| 334 | |
| 335 | -- hushers: tall, pale, silent shapes that drift toward people in the Long |
| 336 | -- Night's dark, and away from any light |
| 337 | local Husher = world.creature { |
| 338 | name = "Husher", |
| 339 | speed = 2, |
| 340 | count = 0, |
| 341 | home = "here", |
| 342 | flies = true, |
| 343 | size = { 0.7, 2.2, 0.7 }, |
| 344 | behaviors = { wander = false }, |
| 345 | body = { |
| 346 | { "veil", size = { 0.6, 1.6, 0.4 }, at = { 0, 1.1, 0 }, color = "#c8cee0" }, |
| 347 | { "hem", size = { 0.7, 0.3, 0.5 }, at = { 0, 0.25, 0 }, color = "#a8b0c8" }, |
| 348 | { "head", size = { 0.42, 0.42, 0.38 }, at = { 0, 2.05, 0 }, color = "#e8ecf6", head = true }, |
| 349 | { "eye_left", size = { 0.08, 0.12, 0.03 }, at = { -0.1, 2.08, -0.2 }, color = "#2a3048", head = true }, |
| 350 | { "eye_right", size = { 0.08, 0.12, 0.03 }, at = { 0.1, 2.08, -0.2 }, color = "#2a3048", head = true }, |
| 351 | { "arm_left", size = { 0.14, 0.9, 0.14 }, at = { -0.4, 1.2, -0.05 }, color = "#c8cee0", leg = true }, |
| 352 | { "arm_right", size = { 0.14, 0.9, 0.14 }, at = { 0.4, 1.2, -0.05 }, color = "#c8cee0", leg = true }, |
| 353 | }, |
| 354 | } |
| 355 | |
| 356 | local LONELY = 40 -- creatures go with nobody this close |
| 357 | local MOST = { glint = 24, husher = 16, lanternback = 24 } |
| 358 | local where: { [string]: { [number]: vector } } = { glint = {}, husher = {}, lanternback = {} } |
| 359 | |
| 360 | local function gone(kind: string, self) |
| 361 | where[kind][self.id] = nil |
| 362 | self:remove() |
| 363 | end |
| 364 | |
| 365 | -- survival players in the Long Dusk, in a land |
| 366 | local function in_land(land: string, survival_only: boolean) |
| 367 | local out = {} |
| 368 | for _, p in world.players() do |
| 369 | if in_dusk(p.position) and world.biome(p.position) == land and (not survival_only or world.pockets(p) ~= nil) then |
| 370 | table.insert(out, p) |
| 371 | end |
| 372 | end |
| 373 | return out |
| 374 | end |
| 375 | |
| 376 | local function nearest_in(list, at: vector, within: number) |
| 377 | local best, best_d = nil, within |
| 378 | for _, p in list do |
| 379 | local d = vector.magnitude(p.position - at) |
| 380 | if d < best_d then |
| 381 | best, best_d = p, d |
| 382 | end |
| 383 | end |
| 384 | return best |
| 385 | end |
| 386 | |
| 387 | function Lanternback:tick(_dt: number) |
| 388 | where.lanternback[self.id] = self.position |
| 389 | self.look_in = (self.look_in or 0) - 1 |
| 390 | if self.look_in <= 0 then |
| 391 | self.look_in = 20 |
| 392 | if not world.nearest_player(self.position, LONELY + 20) then |
| 393 | gone("lanternback", self) |
| 394 | end |
| 395 | end |
| 396 | end |
| 397 | |
| 398 | function Lanternback:used(player) |
| 399 | if world.held(player) == "Ember fruit" and world.pockets(player) ~= nil and world.take(player, "Ember fruit", 1) then |
| 400 | self:say("Mmm") |
| 401 | sound.play("low", { at = self }) |
| 402 | world.reward(player, 1) |
| 403 | end |
| 404 | end |
| 405 | |
| 406 | function Glint:spawned() |
| 407 | self.rest = true |
| 408 | self.bite_in = 0 |
| 409 | end |
| 410 | |
| 411 | function Glint:tick(dt: number) |
| 412 | where.glint[self.id] = self.position |
| 413 | local d = danger() |
| 414 | if d.many == 0 or knobs.glints < 1 then |
| 415 | gone("glint", self) |
| 416 | return |
| 417 | end |
| 418 | self.look_in = (self.look_in or 0) - dt |
| 419 | if self.look_in <= 0 then |
| 420 | self.look_in = 0.5 |
| 421 | if not world.nearest_player(self.position, LONELY) then |
| 422 | gone("glint", self) |
| 423 | return |
| 424 | end |
| 425 | local p = nearest_in(in_land("glare", true), self.position, if self.rest then 6 else 14) |
| 426 | self.prey = if p then p.id else nil |
| 427 | end |
| 428 | local p = nil |
| 429 | for _, q in world.players() do |
| 430 | if q.id == self.prey then |
| 431 | p = q |
| 432 | end |
| 433 | end |
| 434 | if not p then |
| 435 | -- back to lying still on the salt |
| 436 | if not self.rest then |
| 437 | self.rest = true |
| 438 | self:stop() |
| 439 | end |
| 440 | return |
| 441 | end |
| 442 | if self.rest then |
| 443 | self.rest = false |
| 444 | sound.play("chitter", { at = self }) |
| 445 | end |
| 446 | self.bite_in -= dt |
| 447 | if (self.back_off or 0) > 0 then |
| 448 | self.back_off -= dt |
| 449 | return |
| 450 | end |
| 451 | self:walk_to(p.position, 4.5) |
| 452 | if flat(self.position, p.position) < 1.1 and self.bite_in <= 0 then |
| 453 | self.bite_in = 1.6 * d.pace |
| 454 | if knobs.bite > 0 and world.hurt(p, knobs.bite * d.hurt) then |
| 455 | sound.play("chitter", { at = self, pitch = 0.8 }) |
| 456 | end |
| 457 | local away = self.position - p.position |
| 458 | self:walk_to(self.position + vector.normalize(v(away.x, 0, away.z) + v(0.01, 0, 0)) * 3, 4) |
| 459 | self.back_off = 0.7 |
| 460 | end |
| 461 | end |
| 462 | |
| 463 | function Glint:hit(player, shot) |
| 464 | local bop = if shot then shot.power else bops(world.held(player)) * world.might(player) |
| 465 | self.bopped = (self.bopped or 0) + bop |
| 466 | if self.bopped < 3 then |
| 467 | local away = self.position - player.position |
| 468 | self:walk_to(self.position + vector.normalize(v(away.x, 0, away.z) + v(0.01, 0, 0)) * 3, 5) |
| 469 | self.back_off = 0.8 |
| 470 | sound.play("chitter", { at = self, pitch = 1.3 }) |
| 471 | return |
| 472 | end |
| 473 | sound.play("shatter", { at = self, pitch = 1.6, caption = "A glint cracks" }) |
| 474 | world.give(player, "Glint scale", math.floor(world.random(1, 2.999))) |
| 475 | world.reward(player, 4) |
| 476 | gone("glint", self) |
| 477 | end |
| 478 | |
| 479 | function Husher:spawned() |
| 480 | self.reach_in = world.random(1, 2) |
| 481 | end |
| 482 | |
| 483 | function Husher:tick(dt: number) |
| 484 | where.husher[self.id] = self.position |
| 485 | local d = danger() |
| 486 | if d.many == 0 or knobs.hushers < 1 then |
| 487 | gone("husher", self) |
| 488 | return |
| 489 | end |
| 490 | self.look_in = (self.look_in or 0) - dt |
| 491 | if self.look_in <= 0 then |
| 492 | self.look_in = world.random(0.5, 0.8) |
| 493 | if not world.nearest_player(self.position, LONELY) then |
| 494 | gone("husher", self) |
| 495 | return |
| 496 | end |
| 497 | local p = nearest_in(in_land("long_night", true), self.position, 24) |
| 498 | self.prey = if p then p.id else nil |
| 499 | self.lit = light_near(self.position, knobs.shy) |
| 500 | end |
| 501 | if (self.back_off or 0) > 0 then |
| 502 | self.back_off -= dt |
| 503 | return |
| 504 | end |
| 505 | -- keeps off a light, and fades if it's caught in one |
| 506 | if self.lit then |
| 507 | local away = self.position - self.lit |
| 508 | local len = math.max(flat(self.position, self.lit), 0.1) |
| 509 | if len < 2 then |
| 510 | sound.play("hush", { at = self, volume = 0.6, caption = "A husher fades in the light" }) |
| 511 | gone("husher", self) |
| 512 | return |
| 513 | end |
| 514 | self:climb(0) |
| 515 | self:walk_to(self.position + v(away.x / len, 0, away.z / len) * 5, 2.5) |
| 516 | return |
| 517 | end |
| 518 | local p = nil |
| 519 | for _, q in world.players() do |
| 520 | if q.id == self.prey then |
| 521 | p = q |
| 522 | end |
| 523 | end |
| 524 | if not p then |
| 525 | self:climb(0) |
| 526 | self:wander(1) |
| 527 | return |
| 528 | end |
| 529 | -- drifts at the height of their feet |
| 530 | self:climb(math.clamp((p.position.y + 0.2 - self.position.y) * 1.5, -2, 2)) |
| 531 | self:face(p.position) |
| 532 | self.reach_in -= dt |
| 533 | if flat(self.position, p.position) > 1.3 then |
| 534 | self:walk_to(p.position, 2.2) |
| 535 | elseif self.reach_in <= 0 then |
| 536 | self.reach_in = 2.4 * d.pace * world.random(0.8, 1.3) |
| 537 | if knobs.touch > 0 and world.hurt(p, knobs.touch * d.hurt) then |
| 538 | sound.play("hush", { at = self }) |
| 539 | end |
| 540 | local away = self.position - p.position |
| 541 | self:walk_to(self.position + vector.normalize(v(away.x, 0, away.z) + v(0.01, 0, 0)) * 3, 3) |
| 542 | self.back_off = 0.8 |
| 543 | end |
| 544 | end |
| 545 | |
| 546 | function Husher:hit(player, shot) |
| 547 | local bop = if shot then shot.power else bops(world.held(player)) * world.might(player) |
| 548 | self.bopped = (self.bopped or 0) + bop |
| 549 | if self.bopped < 6 then |
| 550 | local away = self.position - player.position |
| 551 | self:walk_to(self.position + vector.normalize(v(away.x, 0, away.z) + v(0.01, 0, 0)) * 3, 3) |
| 552 | self.back_off = 0.8 |
| 553 | sound.play("hush", { at = self, volume = 0.5, pitch = 1.3 }) |
| 554 | return |
| 555 | end |
| 556 | sound.play("hush", { at = self, pitch = 0.7, caption = "A husher comes apart" }) |
| 557 | world.give(player, "Hush silk", math.floor(world.random(1, 2.999))) |
| 558 | world.reward(player, 6) |
| 559 | gone("husher", self) |
| 560 | end |
| 561 | |
| 562 | -- open ground near a spot, for something that walks |
| 563 | local function ground(x: number, z: number): vector? |
| 564 | local top = world.top(x, z) |
| 565 | if top <= SEA then |
| 566 | return nil |
| 567 | end |
| 568 | local at = v(math.floor(x) + 0.5, top + 1, math.floor(z) + 0.5) |
| 569 | if world.block(at) ~= "Air" or world.block(at + UP) ~= "Air" then |
| 570 | return nil |
| 571 | end |
| 572 | return at |
| 573 | end |
| 574 | |
| 575 | -- counts what's still about of a kind, forgetting the gone |
| 576 | local function live(kind: string, made): number |
| 577 | local ids = {} |
| 578 | for _, id in world.creatures(made) do |
| 579 | ids[id] = true |
| 580 | end |
| 581 | local n = 0 |
| 582 | for id in where[kind] do |
| 583 | if ids[id] then |
| 584 | n += 1 |
| 585 | else |
| 586 | where[kind][id] = nil |
| 587 | end |
| 588 | end |
| 589 | return n |
| 590 | end |
| 591 | |
| 592 | local function near(kind: string, at: vector, r: number): number |
| 593 | local n = 0 |
| 594 | for _, pos in where[kind] do |
| 595 | if vector.magnitude(pos - at) < r then |
| 596 | n += 1 |
| 597 | end |
| 598 | end |
| 599 | return n |
| 600 | end |
| 601 | |
| 602 | -- one more near a player, at most `want` within `r` |
| 603 | local function bring(kind: string, made, p, want: number, r: number, lo: number, hi: number, high: boolean) |
| 604 | if near(kind, p.position, r) >= want then |
| 605 | return |
| 606 | end |
| 607 | for _ = 1, 4 do |
| 608 | local a = world.random(0, 2 * math.pi) |
| 609 | local d = world.random(lo, hi) |
| 610 | local at = ground(p.position.x + math.cos(a) * d, p.position.z + math.sin(a) * d) |
| 611 | if at then |
| 612 | if high then |
| 613 | at += v(0, 0.5, 0) |
| 614 | end |
| 615 | where[kind][world.spawn(made, at)] = at |
| 616 | return |
| 617 | end |
| 618 | end |
| 619 | end |
| 620 | |
| 621 | world.every(3, function() |
| 622 | -- where each player is, said once each time they come into a land |
| 623 | for _, p in world.players() do |
| 624 | local land = if in_dusk(p.position) then world.biome(p.position) else "home" |
| 625 | if last_land[p.id] ~= land then |
| 626 | last_land[p.id] = land |
| 627 | if LANDS[land] and not shown[p.id] then |
| 628 | tell(p, LANDS[land]) |
| 629 | end |
| 630 | end |
| 631 | end |
| 632 | local d = danger() |
| 633 | -- lanternbacks for anyone in the Gold, in any mode |
| 634 | if knobs.lanternbacks >= 1 and live("lanternback", Lanternback) < MOST.lanternback then |
| 635 | for _, p in in_land("gold", false) do |
| 636 | bring("lanternback", Lanternback, p, math.floor(knobs.lanternbacks), 40, 14, 30, false) |
| 637 | end |
| 638 | end |
| 639 | local glints = math.floor(knobs.glints * d.many + 0.5) |
| 640 | if glints >= 1 and live("glint", Glint) < MOST.glint then |
| 641 | for _, p in in_land("glare", true) do |
| 642 | bring("glint", Glint, p, glints, 24, 9, 18, false) |
| 643 | end |
| 644 | end |
| 645 | local hushers = math.floor(knobs.hushers * d.many + 0.5) |
| 646 | if hushers >= 1 and live("husher", Husher) < MOST.husher then |
| 647 | for _, p in in_land("long_night", true) do |
| 648 | if world.daylight(p.position) < 0.15 and not light_near(p.position, knobs.shy) then |
| 649 | bring("husher", Husher, p, hushers, 28, 12, 20, true) |
| 650 | end |
| 651 | end |
| 652 | end |
| 653 | end) |
| 654 | |
| 655 | world.on("player_left", function(p) |
| 656 | last_land[p.id] = nil |
| 657 | going[p.id] = nil |
| 658 | end) |