Old stone guardians that stand over the moonsilver deep in the earth. Come near their ore in survival and one wakes: slow, but it hits hard, and it's very tough. Beat it and it leaves a sentinel core, and sometimes moonsilver, and that seam is yours. Rare, and only far below the ground. None on a Peaceful world.
| 1 | --!strict |
| 2 | -- Stone Sentinels: old stone guardians over the moonsilver deep in the |
| 3 | -- earth. Near moonsilver ore, far down, one stands guard; come within a few |
| 4 | -- blocks of where it stands in survival and it wakes, walks at you (slowly) |
| 5 | -- and slams. It won't follow far from its ore. It's very tough: beat it |
| 6 | -- (swords and shots bop harder) and it leaves a sentinel core, and |
| 7 | -- sometimes raw moonsilver, in the pockets of whoever beat it, and nothing |
| 8 | -- guards that seam again (kept with the world, by where it stood). How |
| 9 | -- hard it slams and how quick it is go by the world's danger |
| 10 | -- (world.danger, world 0.21); none stand guard on Peaceful. |
| 11 | |
| 12 | local world = require("@potluck/world@0.33") |
| 13 | local tweak = require("@potluck/tweak") |
| 14 | local store = require("@potluck/store") |
| 15 | local sound = require("@potluck/sound@0.2") |
| 16 | |
| 17 | local knobs = tweak { |
| 18 | on = { true, label = "Sentinels guard moonsilver", order = 1 }, |
| 19 | slam = { 4, min = 0, max = 12, label = "How hard one slams", order = 2 }, |
| 20 | bops = { 16, min = 4, max = 40, label = "Bops to beat one by hand", order = 3 }, |
| 21 | wakes = { 6, min = 2, max = 12, label = "How close to its ore wakes it", order = 4 }, |
| 22 | stone = { "#6e6a66", label = "Stone color", order = 5 }, |
| 23 | glow = { "#7af0e6", label = "Glow color", order = 6 }, |
| 24 | } |
| 25 | |
| 26 | -- How hard it slams and how quick it is, by the world's danger. |
| 27 | local DANGER: { [string]: { hurt: number, speed: number } } = { |
| 28 | peaceful = { hurt = 0, speed = 0 }, |
| 29 | gentle = { hurt = 0.5, speed = 0.9 }, |
| 30 | normal = { hurt = 1, speed = 1.2 }, |
| 31 | hard = { hurt = 1.5, speed = 1.6 }, |
| 32 | } |
| 33 | local function danger() |
| 34 | return DANGER[world.danger()] or DANGER.gentle |
| 35 | end |
| 36 | |
| 37 | -- How hard each sword bops (a hand bops 1): a remix can add its own. |
| 38 | local 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 | |
| 46 | -- What's in hand bops this hard: a sword, a weapon another creation made |
| 47 | -- ([items.*] with kind = "weapon", world.item) by its power, or a hand. |
| 48 | local function bops(held: string?): number |
| 49 | if not held then |
| 50 | return 1 |
| 51 | end |
| 52 | if SWORDS[held] then |
| 53 | return SWORDS[held] |
| 54 | end |
| 55 | local it = world.item(held) |
| 56 | return if it and it.kind == "weapon" then it.power or 1 else 1 |
| 57 | end |
| 58 | |
| 59 | local ORE = "Moonsilver ore" |
| 60 | local DARK = "#4a4642" |
| 61 | local Sentinel = world.creature { |
| 62 | name = "Stone Sentinel", |
| 63 | speed = 1.2, |
| 64 | count = 0, |
| 65 | home = "here", |
| 66 | size = { 0.9, 2.1, 0.9 }, |
| 67 | behaviors = { wander = false }, |
| 68 | body = { |
| 69 | { "legs", size = { 0.7, 0.7, 0.5 }, at = { 0, 0.35, 0 }, color = DARK }, |
| 70 | { "body", size = { 1.0, 0.9, 0.7 }, at = { 0, 1.15, 0 }, color = knobs.stone }, |
| 71 | { "core", size = { 0.26, 0.26, 0.06 }, at = { 0, 1.2, -0.36 }, color = knobs.glow, glow = true }, |
| 72 | { "head", size = { 0.56, 0.46, 0.5 }, at = { 0, 1.84, -0.04 }, color = knobs.stone, head = true }, |
| 73 | { "brow", size = { 0.6, 0.1, 0.12 }, at = { 0, 1.98, -0.3 }, color = DARK, head = true }, |
| 74 | { "eye_left", size = { 0.12, 0.06, 0.03 }, at = { -0.13, 1.88, -0.3 }, color = knobs.glow, glow = true, head = true }, |
| 75 | { "eye_right", size = { 0.12, 0.06, 0.03 }, at = { 0.13, 1.88, -0.3 }, color = knobs.glow, glow = true, head = true }, |
| 76 | { "arm_left", size = { 0.3, 0.95, 0.34 }, at = { -0.66, 1.0, 0 }, color = DARK, leg = true }, |
| 77 | { "arm_right", size = { 0.3, 0.95, 0.34 }, at = { 0.66, 1.0, 0 }, color = DARK, leg = true }, |
| 78 | { "fist_left", size = { 0.36, 0.3, 0.38 }, at = { -0.66, 0.42, 0 }, color = knobs.stone, leg = true }, |
| 79 | { "fist_right", size = { 0.36, 0.3, 0.38 }, at = { 0.66, 0.42, 0 }, color = knobs.stone, leg = true }, |
| 80 | { "foot_left", size = { 0.3, 0.2, 0.42 }, at = { -0.2, 0.1, -0.04 }, color = DARK, leg = true }, |
| 81 | { "foot_right", size = { 0.3, 0.2, 0.42 }, at = { 0.2, 0.1, -0.04 }, color = DARK, leg = true }, |
| 82 | }, |
| 83 | } |
| 84 | |
| 85 | local MOST = 8 -- at once in the whole world |
| 86 | local DEEP = -80 -- only below this |
| 87 | local LEASH = 11 -- it never goes farther than this from where it stood |
| 88 | local SLAM_EVERY = 2.2 -- seconds |
| 89 | local SEAM = 24 -- one guards the seam this far around it |
| 90 | |
| 91 | local UP = vector.create(0, 1, 0) |
| 92 | |
| 93 | -- where each stands guard |
| 94 | local posts: { [number]: vector } = {} |
| 95 | |
| 96 | -- where sentinels were beaten: nothing guards the seam near there again |
| 97 | local beaten: { { number } } = store.get("beaten") or {} |
| 98 | local function was_beaten(at: vector): boolean |
| 99 | for _, b in beaten do |
| 100 | if vector.magnitude(vector.create(b[1], b[2], b[3]) - at) < SEAM then |
| 101 | return true |
| 102 | end |
| 103 | end |
| 104 | return false |
| 105 | end |
| 106 | |
| 107 | local function flat(a: vector, b: vector): number |
| 108 | local dx, dz = a.x - b.x, a.z - b.z |
| 109 | return math.sqrt(dx * dx + dz * dz) |
| 110 | end |
| 111 | |
| 112 | local function solid(at: vector): boolean |
| 113 | local ok, kind = pcall(world.kind, world.block(at)) |
| 114 | return ok and kind ~= "air" and kind ~= "water" and kind ~= "plant" |
| 115 | end |
| 116 | |
| 117 | local function post(self): vector |
| 118 | return vector.create(self.gx or self.position.x, self.gy or self.position.y, self.gz or self.position.z) |
| 119 | end |
| 120 | |
| 121 | local function survivor_near(at: vector, within: number) |
| 122 | local best, best_d = nil, within |
| 123 | for _, p in world.players() do |
| 124 | if world.pockets(p) ~= nil then |
| 125 | local d = vector.magnitude(p.position - at) |
| 126 | if d < best_d then |
| 127 | best, best_d = p, d |
| 128 | end |
| 129 | end |
| 130 | end |
| 131 | return best |
| 132 | end |
| 133 | |
| 134 | function Sentinel:spawned() |
| 135 | local at = self.position |
| 136 | self.gx, self.gy, self.gz = at.x, at.y, at.z |
| 137 | posts[self.id] = at |
| 138 | self.slam_in = SLAM_EVERY |
| 139 | self.look_in = 0 |
| 140 | end |
| 141 | |
| 142 | function Sentinel:tick(dt: number) |
| 143 | local d = danger() |
| 144 | if not knobs.on or d.hurt == 0 then |
| 145 | self:remove() |
| 146 | return |
| 147 | end |
| 148 | local home = post(self) |
| 149 | posts[self.id] = home |
| 150 | self.look_in -= dt |
| 151 | if self.look_in <= 0 then |
| 152 | self.look_in = world.random(0.4, 0.6) |
| 153 | if not world.nearest_player(self.position, 48) then |
| 154 | self:remove() |
| 155 | return |
| 156 | end |
| 157 | local p = survivor_near(home, if self.awake then LEASH else knobs.wakes) |
| 158 | if p and not self.awake then |
| 159 | sound.play("wake", { at = self, volume = 0.9 }) |
| 160 | self:say("...") |
| 161 | end |
| 162 | self.target = if p then p.id else nil |
| 163 | self.awake = p ~= nil |
| 164 | end |
| 165 | local p = nil |
| 166 | for _, q in world.players() do |
| 167 | if q.id == self.target then |
| 168 | p = q |
| 169 | end |
| 170 | end |
| 171 | if not p then |
| 172 | -- back to its post, and still |
| 173 | if flat(self.position, home) > 0.8 then |
| 174 | self:walk_to(home, d.speed) |
| 175 | else |
| 176 | self:stop() |
| 177 | end |
| 178 | return |
| 179 | end |
| 180 | self.slam_in -= dt |
| 181 | if self:distance_to(p.position) < 2.1 then |
| 182 | self:stop() |
| 183 | self:face(p.position) |
| 184 | if self.slam_in <= 0 then |
| 185 | self.slam_in = SLAM_EVERY / d.speed |
| 186 | self:hop(0.3) |
| 187 | sound.play("slam", { at = self, volume = 1 }) |
| 188 | if knobs.slam > 0 then |
| 189 | world.hurt(p, knobs.slam * d.hurt) |
| 190 | end |
| 191 | end |
| 192 | elseif flat(p.position, home) < LEASH then |
| 193 | self:walk_to(p.position, d.speed) |
| 194 | if world.random() < dt * 0.6 then |
| 195 | sound.play("grind", { at = self, volume = 0.5 }) |
| 196 | end |
| 197 | else |
| 198 | self.target = nil |
| 199 | end |
| 200 | end |
| 201 | |
| 202 | function Sentinel:hit(player, shot) |
| 203 | local held = world.held(player) |
| 204 | -- a Keen sword hits harder (world.might); a shot's power says already |
| 205 | local bop = if shot then shot.power else bops(held) * world.might(player) |
| 206 | self.bopped = (self.bopped or 0) + bop |
| 207 | if self.bopped < knobs.bops then |
| 208 | sound.play("grind", { at = self, volume = 0.8, pitch = 1.3, caption = "Stone cracks" }) |
| 209 | -- a bop wakes it, if it can reach whoever did it |
| 210 | if world.pockets(player) ~= nil and flat(player.position, post(self)) < LEASH then |
| 211 | self.target, self.awake = player.id, true |
| 212 | end |
| 213 | return |
| 214 | end |
| 215 | sound.play("crack", { at = self, pitch = 0.6, caption = "A sentinel crumbles" }) |
| 216 | world.give(player, "Sentinel core", 1) |
| 217 | world.reward(player, 20) |
| 218 | if world.random() < 0.5 then |
| 219 | world.give(player, "Raw moonsilver", 1) |
| 220 | end |
| 221 | local at = post(self) |
| 222 | if #beaten < 512 then |
| 223 | table.insert(beaten, { math.floor(at.x), math.floor(at.y), math.floor(at.z) }) |
| 224 | store.set("beaten", beaten) |
| 225 | end |
| 226 | self:remove() |
| 227 | end |
| 228 | |
| 229 | -- where one can stand by the ore: room for it, on solid ground |
| 230 | local function stand_by(ore: vector): vector? |
| 231 | for r = 1, 3 do |
| 232 | for _ = 1, 8 do |
| 233 | local a = world.random(0, 2 * math.pi) |
| 234 | local p = vector.create(math.floor(ore.x + math.cos(a) * r) + 0.5, 0, math.floor(ore.z + math.sin(a) * r) + 0.5) |
| 235 | for dy = 2, -3, -1 do |
| 236 | local q = vector.create(p.x, math.floor(ore.y) + dy, p.z) |
| 237 | if world.block(q) == "Air" and world.block(q + UP) == "Air" and world.block(q + UP * 2) == "Air" and solid(q - UP) then |
| 238 | return vector.create(q.x, q.y + 0.05, q.z) |
| 239 | end |
| 240 | end |
| 241 | end |
| 242 | end |
| 243 | return nil |
| 244 | end |
| 245 | |
| 246 | -- Every few seconds: by moonsilver near a player deep down, a sentinel, if |
| 247 | -- nothing guards that stretch of seam yet and none was beaten there. |
| 248 | world.every(5, function() |
| 249 | if not knobs.on or danger().hurt == 0 then |
| 250 | return |
| 251 | end |
| 252 | -- where each stands guard (gone ones forgotten) |
| 253 | local live = {} |
| 254 | for _, id in world.creatures(Sentinel) do |
| 255 | live[id] = true |
| 256 | end |
| 257 | local total = 0 |
| 258 | for id in posts do |
| 259 | if live[id] then |
| 260 | total += 1 |
| 261 | else |
| 262 | posts[id] = nil |
| 263 | end |
| 264 | end |
| 265 | if total >= MOST then |
| 266 | return |
| 267 | end |
| 268 | for _, p in world.players() do |
| 269 | -- the Long Dusk has creatures of its own |
| 270 | if world.place(p.position) == "dusk" then |
| 271 | continue |
| 272 | end |
| 273 | if p.position.y < DEEP and world.pockets(p) ~= nil then |
| 274 | local ore = world.find_block(p.position, 16, ORE) |
| 275 | if ore and not was_beaten(ore) then |
| 276 | local guarded = false |
| 277 | for _, at in posts do |
| 278 | if vector.magnitude(at - ore) < SEAM then |
| 279 | guarded = true |
| 280 | end |
| 281 | end |
| 282 | local at = if guarded then nil else stand_by(ore) |
| 283 | if at then |
| 284 | posts[world.spawn(Sentinel, at)] = at |
| 285 | return |
| 286 | end |
| 287 | end |
| 288 | end |
| 289 | end |
| 290 | end) |