Goats graze the hill and jump about for the fun of it. Hold wheat or carrots and they come to you. In survival, use a goat with a bucket in hand to milk it, for drinking or for cooking porridge, stews and honey cake in a cooking pot. Feed two goats and they have a kid, which grows into a goat.
| 1 | --!strict |
| 2 | -- Goats: they graze the hill and jump about for the fun of it. Hold wheat |
| 3 | -- or carrots and they come to you (behaviors, no code). In survival, use |
| 4 | -- one with a bucket in hand to milk it. Feed two goats and they have a kid, |
| 5 | -- which grows into a goat: the kids and the goats they grow into are kept |
| 6 | -- with the world (store), since the game only brings back as many as |
| 7 | -- `count` after a restart. |
| 8 | |
| 9 | local world = require("@potluck/world@0.22") |
| 10 | local tweak = require("@potluck/tweak") |
| 11 | local store = require("@potluck/store") |
| 12 | |
| 13 | local knobs = tweak { |
| 14 | herd = { 3, min = 1, max = 8, label = "How many", order = 1 }, |
| 15 | milk = { 2, min = 0.2, max = 20, label = "Minutes before a goat gives milk again", order = 2 }, |
| 16 | grow = { 5, min = 0.5, max = 30, label = "Minutes for a kid to grow up", order = 3 }, |
| 17 | rest = { 5, min = 0.5, max = 30, label = "Minutes before a goat has another kid", order = 4 }, |
| 18 | jump = { true, label = "Jump about", order = 5 }, |
| 19 | coat = { "#ddd6c8", label = "Coat color", order = 6 }, |
| 20 | } |
| 21 | |
| 22 | -- what they come for, and what makes two of them a kid |
| 23 | local FOOD = { "Wheat", "Carrots" } |
| 24 | -- room for everything else of this creation's under the 64 a creation may have |
| 25 | local MOST = 48 |
| 26 | |
| 27 | local function likes(name: string?): boolean |
| 28 | return name ~= nil and table.find(FOOD, name) ~= nil |
| 29 | end |
| 30 | |
| 31 | local GOAT = { |
| 32 | follow_food = FOOD, |
| 33 | flee_when_hit = 7, |
| 34 | hop_when_hit = 1, |
| 35 | chatter = { "Meh-eh-eh!", "Baa-aah.", "Mlem." }, |
| 36 | sound = "bleat", |
| 37 | } |
| 38 | |
| 39 | local KID = { |
| 40 | follow_food = FOOD, |
| 41 | flee_when_hit = 5, |
| 42 | hop_when_hit = 0.8, |
| 43 | chatter = { "Meh!", "Meh-eh?" }, |
| 44 | sound = "bleat", |
| 45 | } |
| 46 | |
| 47 | local HORN = "#6b5a48" |
| 48 | local LEG = "#a89f90" |
| 49 | local EYE = "#2a2018" |
| 50 | |
| 51 | local Goat = world.creature { |
| 52 | name = "Goat", |
| 53 | speed = 2.2, |
| 54 | count = math.floor(knobs.herd), |
| 55 | home = "start", |
| 56 | behaviors = GOAT, |
| 57 | body = { |
| 58 | { "body", size = { 0.6, 0.58, 1.0 }, at = { 0, 0.85, 0.05 }, color = knobs.coat }, |
| 59 | { "udder", size = { 0.24, 0.1, 0.2 }, at = { 0, 0.51, 0.25 }, color = "#e8b8b0" }, |
| 60 | { "tail", size = { 0.1, 0.18, 0.08 }, at = { 0, 1.12, 0.58 }, color = knobs.coat }, |
| 61 | { "head", size = { 0.36, 0.4, 0.46 }, at = { 0, 1.25, -0.62 }, color = knobs.coat, head = true }, |
| 62 | { "horn_left", size = { 0.08, 0.28, 0.08 }, at = { -0.11, 1.56, -0.52 }, color = HORN, head = true }, |
| 63 | { "horn_right", size = { 0.08, 0.28, 0.08 }, at = { 0.11, 1.56, -0.52 }, color = HORN, head = true }, |
| 64 | { "beard", size = { 0.1, 0.2, 0.08 }, at = { 0, 0.98, -0.8 }, color = "#bfb6a6", head = true }, |
| 65 | { "ear_left", size = { 0.18, 0.06, 0.1 }, at = { -0.26, 1.34, -0.58 }, color = knobs.coat, head = true }, |
| 66 | { "ear_right", size = { 0.18, 0.06, 0.1 }, at = { 0.26, 1.34, -0.58 }, color = knobs.coat, head = true }, |
| 67 | { "eye_left", size = { 0.09, 0.095, 0.04 }, at = { -0.11, 1.33, -0.86 }, color = "#f6f2e6", head = true }, |
| 68 | { "pupil_left", size = { 0.05, 0.063, 0.04 }, at = { -0.11, 1.327, -0.889 }, color = EYE, head = true }, |
| 69 | { "eye_right", size = { 0.09, 0.095, 0.04 }, at = { 0.11, 1.33, -0.86 }, color = "#f6f2e6", head = true }, |
| 70 | { "pupil_right", size = { 0.05, 0.063, 0.04 }, at = { 0.11, 1.327, -0.889 }, color = EYE, head = true }, |
| 71 | { "leg_front_left", size = { 0.14, 0.56, 0.14 }, at = { -0.2, 0.28, -0.35 }, color = LEG, leg = true }, |
| 72 | { "leg_front_right", size = { 0.14, 0.56, 0.14 }, at = { 0.2, 0.28, -0.35 }, color = LEG, leg = true }, |
| 73 | { "leg_back_left", size = { 0.14, 0.56, 0.14 }, at = { -0.2, 0.28, 0.4 }, color = LEG, leg = true }, |
| 74 | { "leg_back_right", size = { 0.14, 0.56, 0.14 }, at = { 0.2, 0.28, 0.4 }, color = LEG, leg = true }, |
| 75 | }, |
| 76 | } |
| 77 | |
| 78 | local Kid = world.creature { |
| 79 | name = "Kid", |
| 80 | speed = 2, |
| 81 | count = 0, |
| 82 | home = "start", |
| 83 | behaviors = KID, |
| 84 | body = { |
| 85 | { "body", size = { 0.36, 0.34, 0.6 }, at = { 0, 0.52, 0.03 }, color = knobs.coat }, |
| 86 | { "tail", size = { 0.06, 0.12, 0.05 }, at = { 0, 0.7, 0.35 }, color = knobs.coat }, |
| 87 | { "head", size = { 0.26, 0.28, 0.3 }, at = { 0, 0.8, -0.38 }, color = knobs.coat, head = true }, |
| 88 | { "ear_left", size = { 0.12, 0.05, 0.07 }, at = { -0.18, 0.86, -0.36 }, color = knobs.coat, head = true }, |
| 89 | { "ear_right", size = { 0.12, 0.05, 0.07 }, at = { 0.18, 0.86, -0.36 }, color = knobs.coat, head = true }, |
| 90 | { "eye_left", size = { 0.075, 0.079, 0.03 }, at = { -0.08, 0.86, -0.54 }, color = "#f6f2e6", head = true }, |
| 91 | { "pupil_left", size = { 0.041, 0.053, 0.03 }, at = { -0.08, 0.858, -0.563 }, color = EYE, head = true }, |
| 92 | { "eye_right", size = { 0.075, 0.079, 0.03 }, at = { 0.08, 0.86, -0.54 }, color = "#f6f2e6", head = true }, |
| 93 | { "pupil_right", size = { 0.041, 0.053, 0.03 }, at = { 0.08, 0.858, -0.563 }, color = EYE, head = true }, |
| 94 | { "leg_front_left", size = { 0.09, 0.36, 0.09 }, at = { -0.12, 0.18, -0.2 }, color = LEG, leg = true }, |
| 95 | { "leg_front_right", size = { 0.09, 0.36, 0.09 }, at = { 0.12, 0.18, -0.2 }, color = LEG, leg = true }, |
| 96 | { "leg_back_left", size = { 0.09, 0.36, 0.09 }, at = { -0.12, 0.18, 0.24 }, color = LEG, leg = true }, |
| 97 | { "leg_back_right", size = { 0.09, 0.36, 0.09 }, at = { 0.12, 0.18, 0.24 }, color = LEG, leg = true }, |
| 98 | }, |
| 99 | } |
| 100 | |
| 101 | -- ---- kids: two fed goats have one ---- |
| 102 | |
| 103 | -- who's keen and where, each saying every tick (an old word is someone gone) |
| 104 | type Keen = { at: vector, t: number, by: number? } |
| 105 | local keen: { [number]: Keen } = {} |
| 106 | -- XP for each feeding that leads to a young one |
| 107 | local BRED_XP = 3 |
| 108 | -- a goat whose partner had the kid: it rests too |
| 109 | local done: { [number]: boolean } = {} |
| 110 | -- how grown a kid spawned from here is to be, by its id, for its `spawned` |
| 111 | local born: { [number]: number } = {} |
| 112 | |
| 113 | -- every kid born here and the goats they grow into, each saying every |
| 114 | -- tick, kept with the world so they're back after a restart |
| 115 | type Seen = { at: vector, grow: number, t: number } |
| 116 | local seen: { [number]: Seen } = {} |
| 117 | type Kept = { at: vector, grow: number } |
| 118 | |
| 119 | local function kid_at(at: vector, grow: number): boolean |
| 120 | if #world.creatures() >= MOST then |
| 121 | return false |
| 122 | end |
| 123 | local ok, id = pcall(world.spawn, Kid, at) |
| 124 | if ok then |
| 125 | born[id] = grow |
| 126 | end |
| 127 | return ok |
| 128 | end |
| 129 | |
| 130 | -- the nearest other keen goat within ten blocks, and how far |
| 131 | local function partner(self): (number?, number) |
| 132 | local best, far = nil, 10 |
| 133 | local now = world.time() |
| 134 | for id, k in keen do |
| 135 | if id ~= self.id and now - k.t < 1 then |
| 136 | local d = self:distance_to(k.at) |
| 137 | if d < far then |
| 138 | best, far = id, d |
| 139 | end |
| 140 | end |
| 141 | end |
| 142 | return best, far |
| 143 | end |
| 144 | |
| 145 | local function rested(self) |
| 146 | self.love = nil |
| 147 | self.rest = knobs.rest * 60 |
| 148 | keen[self.id] = nil |
| 149 | end |
| 150 | |
| 151 | -- what a fed goat does: go to another fed goat, and have a kid between them |
| 152 | local function court(self, dt: number) |
| 153 | if done[self.id] then |
| 154 | done[self.id] = nil |
| 155 | rested(self) |
| 156 | return |
| 157 | end |
| 158 | if self.rest then |
| 159 | self.rest -= dt |
| 160 | if self.rest <= 0 then |
| 161 | self.rest = nil |
| 162 | end |
| 163 | end |
| 164 | if not self.love then |
| 165 | return |
| 166 | end |
| 167 | self.love -= dt |
| 168 | if self.love <= 0 then |
| 169 | self.love = nil |
| 170 | keen[self.id] = nil |
| 171 | return |
| 172 | end |
| 173 | keen[self.id] = { at = self.position, t = world.time(), by = self.fed_by } |
| 174 | local other, far = partner(self) |
| 175 | if not other then |
| 176 | return |
| 177 | end |
| 178 | if far > 1.8 then |
| 179 | self:walk_to(keen[other].at) |
| 180 | elseif self.id < other then |
| 181 | -- the one of the two that goes first has it, and tells the other |
| 182 | local mid = (self.position + keen[other].at) * 0.5 |
| 183 | if kid_at(mid, knobs.grow * 60) then |
| 184 | done[other] = true |
| 185 | -- XP for whoever fed the two (world.reward) |
| 186 | for _, by in { self.fed_by, keen[other].by } do |
| 187 | world.reward(by, BRED_XP) |
| 188 | end |
| 189 | rested(self) |
| 190 | self:say("Meh-eh-eh!") |
| 191 | end |
| 192 | end |
| 193 | end |
| 194 | |
| 195 | -- a fed goat gets keen; a fed kid grows a little |
| 196 | local function feed(self, player, young: boolean) |
| 197 | local food = world.held(player) :: string |
| 198 | if young then |
| 199 | if world.take(player, food, 1) then |
| 200 | self.grow = math.max(0, (self.grow or 0) - 60) |
| 201 | self:say("Mlem!") |
| 202 | end |
| 203 | elseif self.love then |
| 204 | self:say("Mlem.") |
| 205 | elseif self.rest then |
| 206 | self:say("Meh. Not now.") |
| 207 | elseif world.take(player, food, 1) then |
| 208 | self.love = 30 |
| 209 | self.fed_by = player.id |
| 210 | self:say("Mlem mlem!") |
| 211 | end |
| 212 | end |
| 213 | |
| 214 | -- ---- milk, and jumping about ---- |
| 215 | |
| 216 | function Goat:spawned() |
| 217 | self.boing = world.random(5, 30) |
| 218 | end |
| 219 | |
| 220 | local function jumpy(self, dt: number, power: number) |
| 221 | if not knobs.jump then |
| 222 | return |
| 223 | end |
| 224 | self.boing = (self.boing or 10) - dt |
| 225 | if self.boing <= 0 then |
| 226 | self.boing = world.random(8, 30) |
| 227 | if self:on_ground() then |
| 228 | self:hop(power) |
| 229 | end |
| 230 | end |
| 231 | end |
| 232 | |
| 233 | function Goat:tick(dt: number) |
| 234 | court(self, dt) |
| 235 | jumpy(self, dt, 1.1) |
| 236 | if self.milked then |
| 237 | self.milked -= dt |
| 238 | if self.milked <= 0 then |
| 239 | self.milked = nil |
| 240 | end |
| 241 | end |
| 242 | if self.bred then |
| 243 | seen[self.id] = { at = self.position, grow = 0, t = world.time() } |
| 244 | end |
| 245 | end |
| 246 | |
| 247 | function Goat:used(player) |
| 248 | local held = world.held(player) |
| 249 | if likes(held) then |
| 250 | feed(self, player, false) |
| 251 | elseif held == "Bucket" and world.pockets(player) ~= nil then |
| 252 | if self.milked then |
| 253 | self:say("Meh. Not yet.") |
| 254 | elseif world.take(player, "Bucket", 1) then |
| 255 | if world.give(player, "Milk bucket", 1) > 0 then |
| 256 | self.milked = knobs.milk * 60 |
| 257 | self:say("Meh-eh.") |
| 258 | else |
| 259 | world.give(player, "Bucket", 1) |
| 260 | end |
| 261 | end |
| 262 | end |
| 263 | end |
| 264 | |
| 265 | function Goat:hit(player) |
| 266 | self:say("MEH!") |
| 267 | end |
| 268 | |
| 269 | function Kid:spawned() |
| 270 | self.bred = true |
| 271 | self.grow = born[self.id] or knobs.grow * 60 |
| 272 | born[self.id] = nil |
| 273 | self.boing = world.random(3, 15) |
| 274 | end |
| 275 | |
| 276 | function Kid:tick(dt: number) |
| 277 | jumpy(self, dt, 0.8) |
| 278 | self.grow = (self.grow or 0) - dt |
| 279 | if self.grow <= 0 then |
| 280 | self.grow = nil |
| 281 | self:become(Goat) |
| 282 | return |
| 283 | end |
| 284 | seen[self.id] = { at = self.position, grow = self.grow, t = world.time() } |
| 285 | end |
| 286 | |
| 287 | function Kid:used(player) |
| 288 | if likes(world.held(player)) then |
| 289 | feed(self, player, true) |
| 290 | end |
| 291 | end |
| 292 | |
| 293 | -- ---- kept with the world ---- |
| 294 | |
| 295 | world.every(15, function() |
| 296 | local list: { Kept } = {} |
| 297 | local now = world.time() |
| 298 | for id, s in seen do |
| 299 | if now - s.t > 2 then |
| 300 | seen[id] = nil |
| 301 | else |
| 302 | table.insert(list, { at = s.at, grow = s.grow }) |
| 303 | end |
| 304 | end |
| 305 | store.set("herd", list) |
| 306 | end) |
| 307 | |
| 308 | -- after a restart, the goats born here come back (a reload keeps them, and |
| 309 | -- they've said where they are by now) |
| 310 | world.after(0.5, function() |
| 311 | if next(seen) ~= nil then |
| 312 | return |
| 313 | end |
| 314 | local kept: { Kept } = store.get("herd") or {} |
| 315 | for _, s in kept do |
| 316 | if not kid_at(s.at, s.grow) then |
| 317 | break |
| 318 | end |
| 319 | end |
| 320 | end) |