Bristly wild boars that root about in the woods by day. Leave them be: come too close in survival and one snorts, stamps and charges, then goes back to its rooting. They're tough, but bop one enough and it leaves leather, bristles and meat to cook. On a Peaceful world they never charge; on a Hard one they hit harder and come back at you sooner.
| 1 | --!strict |
| 2 | -- Bramble Boars: bristly wild boars that root about in the woods by day, |
| 3 | -- near players on the surface, where there are trees. Come too close in |
| 4 | -- survival and one snorts, stamps, and charges; it hits once and goes back |
| 5 | -- to rooting for a while. Bop one and it charges whoever did. They're |
| 6 | -- tough: bop one enough (a sword or a shot bops harder) and it leaves |
| 7 | -- leather, bristles and raw meat in the pockets of whoever beat it. The |
| 8 | -- world's danger (world.danger, world 0.21) says how hard they hit and how |
| 9 | -- soon they charge again; on Peaceful they never do, and run off when hit. |
| 10 | |
| 11 | local world = require("@potluck/world@0.33") |
| 12 | local tweak = require("@potluck/tweak") |
| 13 | local sound = require("@potluck/sound@0.2") |
| 14 | |
| 15 | local knobs = tweak { |
| 16 | how_many = { 2, min = 0, max = 6, label = "How many root about near you in the woods (0 for none)", order = 1 }, |
| 17 | charge = { 5, min = 0, max = 10, label = "How close you can come before one charges (0 never)", order = 2 }, |
| 18 | hits = { 3, min = 0, max = 10, label = "How hard a charge hits", order = 3 }, |
| 19 | bops = { 8, min = 2, max = 20, label = "Bops to beat one by hand", order = 4 }, |
| 20 | bristles = { "#4a3226", label = "Bristle color", order = 5 }, |
| 21 | } |
| 22 | |
| 23 | -- How hard they hit and how long they cool down after, by the world's |
| 24 | -- danger (0 never charges). |
| 25 | local DANGER: { [string]: { hurt: number, rest: number } } = { |
| 26 | peaceful = { hurt = 0, rest = 0 }, |
| 27 | gentle = { hurt = 0.5, rest = 14 }, |
| 28 | normal = { hurt = 1, rest = 10 }, |
| 29 | hard = { hurt = 1.5, rest = 6 }, |
| 30 | } |
| 31 | local function danger() |
| 32 | return DANGER[world.danger()] or DANGER.gentle |
| 33 | end |
| 34 | |
| 35 | -- How hard each sword bops (a hand bops 1): a remix can add its own. |
| 36 | local SWORDS: { [string]: number } = { |
| 37 | ["Wooden sword"] = 2, |
| 38 | ["Stone sword"] = 3, |
| 39 | ["Copper sword"] = 3, |
| 40 | ["Iron sword"] = 4, |
| 41 | ["Moonsilver sword"] = 5, |
| 42 | } |
| 43 | |
| 44 | -- What's in hand bops this hard: a sword, a weapon another creation made |
| 45 | -- ([items.*] with kind = "weapon", world.item) by its power, or a hand. |
| 46 | local 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 |
| 55 | end |
| 56 | |
| 57 | -- What one leaves: each thing, from least to most. |
| 58 | local LEAVES = { |
| 59 | { thing = "Leather", least = 1, most = 2 }, |
| 60 | { thing = "Bristles", least = 1, most = 2 }, |
| 61 | { thing = "Raw meat", least = 1, most = 3 }, |
| 62 | } |
| 63 | |
| 64 | -- Trees: what makes the woods. |
| 65 | local TREES = { "Oak log", "Birch log", "Pine log", "Maple log", "Willow log", "Palm log", "Redwood log" } |
| 66 | |
| 67 | local SNOUT = "#8a6a58" |
| 68 | local TUSK = "#ece2c8" |
| 69 | local Boar = world.creature { |
| 70 | name = "Bramble Boar", |
| 71 | speed = 1.6, |
| 72 | count = 0, |
| 73 | home = "here", |
| 74 | behaviors = { |
| 75 | chatter = { "Hrnk.", "Snrrf.", "Hrm-hrm." }, |
| 76 | sound = "grunt", |
| 77 | }, |
| 78 | body = { |
| 79 | { "body", size = { 0.62, 0.56, 1.04 }, at = { 0, 0.7, 0.06 }, color = knobs.bristles }, |
| 80 | { "mane", size = { 0.2, 0.16, 0.8 }, at = { 0, 1.02, 0.0 }, color = "#2e1e16" }, |
| 81 | { "tail", size = { 0.06, 0.18, 0.06 }, at = { 0, 0.82, 0.6 }, color = knobs.bristles }, |
| 82 | { "head", size = { 0.46, 0.42, 0.42 }, at = { 0, 0.72, -0.62 }, color = knobs.bristles, head = true }, |
| 83 | { "snout", size = { 0.24, 0.18, 0.16 }, at = { 0, 0.62, -0.88 }, color = SNOUT, head = true }, |
| 84 | { "tusk_left", size = { 0.05, 0.14, 0.05 }, at = { -0.15, 0.66, -0.88 }, color = TUSK, head = true }, |
| 85 | { "tusk_right", size = { 0.05, 0.14, 0.05 }, at = { 0.15, 0.66, -0.88 }, color = TUSK, head = true }, |
| 86 | { "ear_left", size = { 0.1, 0.14, 0.06 }, at = { -0.18, 0.98, -0.58 }, color = "#2e1e16", head = true }, |
| 87 | { "ear_right", size = { 0.1, 0.14, 0.06 }, at = { 0.18, 0.98, -0.58 }, color = "#2e1e16", head = true }, |
| 88 | { "eye_left", size = { 0.075, 0.079, 0.03 }, at = { -0.15, 0.82, -0.84 }, color = "#f6f2e6", head = true }, |
| 89 | { "pupil_left", size = { 0.041, 0.053, 0.03 }, at = { -0.15, 0.818, -0.863 }, color = "#140c08", head = true }, |
| 90 | { "eye_right", size = { 0.075, 0.079, 0.03 }, at = { 0.15, 0.82, -0.84 }, color = "#f6f2e6", head = true }, |
| 91 | { "pupil_right", size = { 0.041, 0.053, 0.03 }, at = { 0.15, 0.818, -0.863 }, color = "#140c08", head = true }, |
| 92 | { "leg_front_left", size = { 0.14, 0.42, 0.14 }, at = { -0.2, 0.21, -0.32 }, color = "#2e1e16", leg = true }, |
| 93 | { "leg_front_right", size = { 0.14, 0.42, 0.14 }, at = { 0.2, 0.21, -0.32 }, color = "#2e1e16", leg = true }, |
| 94 | { "leg_back_left", size = { 0.14, 0.42, 0.14 }, at = { -0.2, 0.21, 0.4 }, color = "#2e1e16", leg = true }, |
| 95 | { "leg_back_right", size = { 0.14, 0.42, 0.14 }, at = { 0.2, 0.21, 0.4 }, color = "#2e1e16", leg = true }, |
| 96 | }, |
| 97 | } |
| 98 | |
| 99 | local MOST = 24 -- at once, however many players are out |
| 100 | local NEAR, FAR = 14, 26 -- how far from a player they turn up |
| 101 | local LONELY = 56 -- they wander off with nobody this close |
| 102 | local STAMP = 0.8 -- seconds between the snort and the charge |
| 103 | local CHARGING = 3 -- seconds a charge lasts, at most |
| 104 | local GREEN = 34 -- never this near the Green |
| 105 | |
| 106 | local UP = vector.create(0, 1, 0) |
| 107 | |
| 108 | local where: { [number]: vector } = {} |
| 109 | |
| 110 | local function flat(a: vector, b: vector): number |
| 111 | local dx, dz = a.x - b.x, a.z - b.z |
| 112 | return math.sqrt(dx * dx + dz * dz) |
| 113 | end |
| 114 | |
| 115 | local function survivor(id: number?) |
| 116 | for _, p in world.players() do |
| 117 | if p.id == id and world.pockets(p) ~= nil then |
| 118 | return p |
| 119 | end |
| 120 | end |
| 121 | return nil |
| 122 | end |
| 123 | |
| 124 | -- the nearest player in survival within `within` |
| 125 | local function nearest(pos: vector, within: number) |
| 126 | local best, best_d = nil, within |
| 127 | for _, p in world.players() do |
| 128 | if world.pockets(p) ~= nil then |
| 129 | local d = vector.magnitude(p.position - pos) |
| 130 | if d < best_d then |
| 131 | best, best_d = p, d |
| 132 | end |
| 133 | end |
| 134 | end |
| 135 | return best |
| 136 | end |
| 137 | |
| 138 | local function gone(self) |
| 139 | where[self.id] = nil |
| 140 | self:remove() |
| 141 | end |
| 142 | |
| 143 | function Boar:spawned() |
| 144 | self.rest = world.random(2, 6) |
| 145 | self.look_in = 0 |
| 146 | end |
| 147 | |
| 148 | function Boar:tick(dt: number) |
| 149 | where[self.id] = self.position |
| 150 | local d = danger() |
| 151 | if knobs.how_many < 1 then |
| 152 | gone(self) |
| 153 | return |
| 154 | end |
| 155 | self.rest = math.max(0, (self.rest or 0) - dt) |
| 156 | -- running off after a bop on Peaceful |
| 157 | if (self.back_off or 0) > 0 then |
| 158 | self.back_off -= dt |
| 159 | return |
| 160 | end |
| 161 | -- winding up: snort, stamp, then go |
| 162 | if self.stamp then |
| 163 | self.stamp -= dt |
| 164 | local p = survivor(self.target) |
| 165 | if not p then |
| 166 | self.stamp, self.target = nil, nil |
| 167 | elseif self.stamp <= 0 then |
| 168 | self.stamp = nil |
| 169 | self.charging = CHARGING |
| 170 | else |
| 171 | self:stop() |
| 172 | self:face(p.position) |
| 173 | end |
| 174 | return |
| 175 | end |
| 176 | if self.charging then |
| 177 | self.charging -= dt |
| 178 | local p = survivor(self.target) |
| 179 | if not p or self.charging <= 0 then |
| 180 | self.charging, self.target = nil, nil |
| 181 | self.rest = d.rest |
| 182 | self:wander(1.2) |
| 183 | return |
| 184 | end |
| 185 | self:walk_to(p.position, 5.5) |
| 186 | if self:distance_to(p.position) < 1.5 then |
| 187 | self.charging, self.target = nil, nil |
| 188 | self.rest = d.rest |
| 189 | self:hop(0.4) |
| 190 | if knobs.hits > 0 and world.hurt(p, knobs.hits * d.hurt) then |
| 191 | sound.play("grunt", { at = self, volume = 0.9, pitch = 0.8, caption = "A boar hits you" }) |
| 192 | end |
| 193 | -- and off it goes |
| 194 | local away = self.position - p.position |
| 195 | self:walk_to(self.position + vector.normalize(vector.create(away.x, 0, away.z) + vector.create(0.01, 0, 0)) * 6, 2.5) |
| 196 | end |
| 197 | return |
| 198 | end |
| 199 | self.look_in -= dt |
| 200 | if self.look_in > 0 then |
| 201 | return |
| 202 | end |
| 203 | self.look_in = world.random(0.4, 0.7) |
| 204 | if not world.nearest_player(self.position, LONELY) then |
| 205 | gone(self) |
| 206 | return |
| 207 | end |
| 208 | if d.hurt > 0 and knobs.charge > 0 and self.rest <= 0 then |
| 209 | local p = nearest(self.position, knobs.charge) |
| 210 | if p and math.abs(p.position.y - self.position.y) < 3 then |
| 211 | self.target = p.id |
| 212 | self.stamp = STAMP |
| 213 | sound.play("snort", { at = self, volume = 0.8 }) |
| 214 | self:say("SNRRF!") |
| 215 | return |
| 216 | end |
| 217 | end |
| 218 | self:wander(1.2) |
| 219 | end |
| 220 | |
| 221 | function Boar:hit(player, shot) |
| 222 | local held = world.held(player) |
| 223 | -- a Keen sword hits harder (world.might); a shot's power says already |
| 224 | local bop = if shot then shot.power else bops(held) * world.might(player) |
| 225 | self.bopped = (self.bopped or 0) + bop |
| 226 | if self.bopped >= knobs.bops then |
| 227 | sound.play("squeal", { at = self, pitch = 0.7, caption = "A boar falls" }) |
| 228 | for _, l in LEAVES do |
| 229 | local n = math.floor(world.random(l.least, l.most + 0.999)) |
| 230 | world.give(player, l.thing, n) |
| 231 | end |
| 232 | world.reward(player, 5) |
| 233 | gone(self) |
| 234 | return |
| 235 | end |
| 236 | self:hop(0.4) |
| 237 | local d = danger() |
| 238 | if d.hurt > 0 and world.pockets(player) ~= nil then |
| 239 | -- it goes for whoever bopped it, straight away |
| 240 | self.target = player.id |
| 241 | self.stamp = STAMP * 0.5 |
| 242 | else |
| 243 | local away = self.position - player.position |
| 244 | self:walk_to(self.position + vector.normalize(vector.create(away.x, 0, away.z) + vector.create(0.01, 0, 0)) * 8, 4) |
| 245 | self.back_off = 2 |
| 246 | end |
| 247 | end |
| 248 | |
| 249 | -- somewhere in the woods: grass under it, room for it, and a tree near |
| 250 | local function in_woods(x: number, z: number): vector? |
| 251 | local y = world.top(x, z) |
| 252 | local ground = vector.create(math.floor(x) + 0.5, y, math.floor(z) + 0.5) |
| 253 | if world.block(ground) ~= "Grass" or world.block(ground + UP) ~= "Air" or world.block(ground + UP * 2) ~= "Air" then |
| 254 | return nil |
| 255 | end |
| 256 | for _, t in TREES do |
| 257 | if world.find_block(ground, 7, t) then |
| 258 | return ground + UP * 1.05 |
| 259 | end |
| 260 | end |
| 261 | return nil |
| 262 | end |
| 263 | |
| 264 | -- Every few seconds by day: one more near each player on the surface, |
| 265 | -- in the woods, with fewer near them than the knob says. |
| 266 | world.every(4, function() |
| 267 | local want = math.floor(knobs.how_many) |
| 268 | if want < 1 or world.daylight() < 0.5 then |
| 269 | return |
| 270 | end |
| 271 | local live = {} |
| 272 | for _, id in world.creatures(Boar) do |
| 273 | live[id] = true |
| 274 | end |
| 275 | local total = 0 |
| 276 | for id in where do |
| 277 | if live[id] then |
| 278 | total += 1 |
| 279 | else |
| 280 | where[id] = nil |
| 281 | end |
| 282 | end |
| 283 | local green = world.spot("green") |
| 284 | for _, p in world.players() do |
| 285 | -- the Long Dusk has creatures of its own |
| 286 | if world.place(p.position) == "dusk" then |
| 287 | continue |
| 288 | end |
| 289 | local on_top = p.position.y > world.top(p.position.x, p.position.z) - 3 |
| 290 | local near = 0 |
| 291 | for _, at in where do |
| 292 | if flat(at, p.position) < FAR + 8 then |
| 293 | near += 1 |
| 294 | end |
| 295 | end |
| 296 | if on_top and total < MOST and near < want then |
| 297 | for _ = 1, 3 do |
| 298 | local a = world.random(0, 2 * math.pi) |
| 299 | local r = world.random(NEAR, FAR) |
| 300 | local at = in_woods(p.position.x + math.cos(a) * r, p.position.z + math.sin(a) * r) |
| 301 | if at and flat(at, green) > GREEN and math.abs(at.y - p.position.y) < 12 then |
| 302 | where[world.spawn(Boar, at)] = at |
| 303 | total += 1 |
| 304 | break |
| 305 | end |
| 306 | end |
| 307 | end |
| 308 | end |
| 309 | end) |