Sign in
creature

Hens

by Potluck · version 1 · Checked for all ages

Hens peck about the Green and lay eggs on the grass now and then: pick them up, cook them, or bake them into pies and cakes. Pat one for a feather now and then. Hold seeds and they come to you; feed two of them seeds and they make a chick, which grows into a hen.

1--!strict
2-- Hens: they peck about the Green and lay eggs on the grass now and then.
3-- Hold seeds or wheat and they come to you (behaviors, no code). In
4-- survival a pat gives a feather now and then. Feed two hens and they make
5-- a chick, which grows into a hen: the chicks and the hens they grow into
6-- are kept with the world (store), since the game only brings back as many
7-- as `count` after a restart.
8
9local world = require("@potluck/world@0.22")
10local tweak = require("@potluck/tweak")
11local store = require("@potluck/store")
12
13local knobs = tweak {
14 flock = { 4, min = 1, max = 8, label = "How many", order = 1 },
15 lay = { 4, min = 0.5, max = 20, label = "Minutes between a hen's eggs", order = 2 },
16 eggs = { 12, min = 1, max = 32, label = "Most eggs lying about", order = 3 },
17 feather = { 2, min = 0.2, max = 10, label = "Minutes between a hen's feathers", order = 4 },
18 grow = { 5, min = 0.5, max = 30, label = "Minutes for a chick to grow up", order = 5 },
19 rest = { 5, min = 0.5, max = 30, label = "Minutes before a hen makes another chick", order = 6 },
20 color = { "#b0673a", label = "Feather color", order = 7 },
21}
22
23local EGG = "Egg"
24local FEATHER = "Feather"
25-- what they come for, and what makes two of them a chick
26local FOOD = { "Seeds", "Wheat" }
27-- room for everything else of this creation's under the 64 a creation may have
28local MOST = 48
29
30local function likes(name: string?): boolean
31 return name ~= nil and table.find(FOOD, name) ~= nil
32end
33
34local HEN = {
35 follow_food = FOOD,
36 flee_when_hit = 5,
37 hop_when_hit = 0.5,
38 chatter = { "Cluck.", "Bawk?", "Buk-buk-buk." },
39 sound = "cluck",
40}
41
42local CHICK = {
43 follow_food = FOOD,
44 flee_when_hit = 4,
45 hop_when_hit = 0.3,
46 chatter = { "Peep.", "Peep peep!" },
47 sound = "peep",
48}
49
50local LEGS = "#e0a028"
51local BEAK = "#e8a020"
52local COMB = "#c8282c"
53local EYE = "#1c1814"
54
55local Hen = world.creature {
56 name = "Hen",
57 speed = 1.8,
58 count = math.floor(knobs.flock),
59 home = "green",
60 behaviors = HEN,
61 body = {
62 { "body", size = { 0.56, 0.46, 0.66 }, at = { 0, 0.5, 0.02 }, color = knobs.color },
63 { "chest", size = { 0.5, 0.34, 0.2 }, at = { 0, 0.52, -0.3 }, color = knobs.color },
64 { "tail", size = { 0.3, 0.34, 0.14 }, at = { 0, 0.72, 0.36 }, color = knobs.color },
65 { "tail_top", size = { 0.2, 0.3, 0.1 }, at = { 0, 0.82, 0.43 }, color = knobs.color },
66 { "wing_left", size = { 0.08, 0.3, 0.44 }, at = { -0.31, 0.52, 0.04 }, color = knobs.color },
67 { "wing_right", size = { 0.08, 0.3, 0.44 }, at = { 0.31, 0.52, 0.04 }, color = knobs.color },
68 { "head", size = { 0.32, 0.32, 0.3 }, at = { 0, 0.84, -0.32 }, color = knobs.color, head = true },
69 { "beak", size = { 0.12, 0.08, 0.12 }, at = { 0, 0.82, -0.52 }, color = BEAK, head = true },
70 { "comb", size = { 0.07, 0.12, 0.2 }, at = { 0, 1.04, -0.32 }, color = COMB, head = true },
71 { "wattle", size = { 0.07, 0.1, 0.05 }, at = { 0, 0.72, -0.46 }, color = COMB, head = true },
72 { "eye_left", size = { 0.08, 0.08, 0.04 }, at = { -0.14, 0.88, -0.4 }, color = "#f6f2e6", head = true },
73 { "eye_right", size = { 0.08, 0.08, 0.04 }, at = { 0.14, 0.88, -0.4 }, color = "#f6f2e6", head = true },
74 { "pupil_left", size = { 0.04, 0.05, 0.04 }, at = { -0.14, 0.875, -0.42 }, color = EYE, head = true },
75 { "pupil_right", size = { 0.04, 0.05, 0.04 }, at = { 0.14, 0.875, -0.42 }, color = EYE, head = true },
76 { "leg_left", size = { 0.07, 0.26, 0.07 }, at = { -0.11, 0.13, 0.02 }, color = LEGS, leg = true },
77 { "leg_right", size = { 0.07, 0.26, 0.07 }, at = { 0.11, 0.13, 0.02 }, color = LEGS, leg = true },
78 },
79}
80
81local Chick = world.creature {
82 name = "Chick",
83 speed = 1.6,
84 count = 0,
85 home = "green",
86 behaviors = CHICK,
87 body = {
88 { "body", size = { 0.28, 0.26, 0.3 }, at = { 0, 0.23, 0 }, color = "#f2d24a" },
89 { "head", size = { 0.26, 0.26, 0.24 }, at = { 0, 0.43, -0.12 }, color = "#f2d24a", head = true },
90 { "beak", size = { 0.08, 0.05, 0.07 }, at = { 0, 0.41, -0.26 }, color = BEAK, head = true },
91 { "eye_left", size = { 0.06, 0.06, 0.03 }, at = { -0.1, 0.47, -0.23 }, color = "#f6f2e6", head = true },
92 { "eye_right", size = { 0.06, 0.06, 0.03 }, at = { 0.1, 0.47, -0.23 }, color = "#f6f2e6", head = true },
93 { "pupil_left", size = { 0.03, 0.04, 0.03 }, at = { -0.1, 0.465, -0.245 }, color = EYE, head = true },
94 { "pupil_right", size = { 0.03, 0.04, 0.03 }, at = { 0.1, 0.465, -0.245 }, color = EYE, head = true },
95 { "leg_left", size = { 0.04, 0.12, 0.04 }, at = { -0.06, 0.06, 0 }, color = LEGS, leg = true },
96 { "leg_right", size = { 0.04, 0.12, 0.04 }, at = { 0.06, 0.06, 0 }, color = LEGS, leg = true },
97 },
98}
99
100-- ---- chicks: two fed hens make one ----
101
102-- who's keen and where, each saying every tick (an old word is someone gone)
103type Keen = { at: vector, t: number, by: number? }
104local keen: { [number]: Keen } = {}
105-- XP for each feeding that leads to a young one
106local BRED_XP = 3
107-- a hen whose partner made the chick: it rests too
108local done: { [number]: boolean } = {}
109-- how grown a chick spawned from here is to be, by its id, for its `spawned`
110local born: { [number]: number } = {}
111
112-- every hen and chick that hatched here, each saying every tick, kept with
113-- the world so they're back after a restart
114type Seen = { at: vector, grow: number, t: number }
115local seen: { [number]: Seen } = {}
116type Kept = { at: vector, grow: number }
117
118local function chick_at(at: vector, grow: number): boolean
119 if #world.creatures() >= MOST then
120 return false
121 end
122 local ok, id = pcall(world.spawn, Chick, at)
123 if ok then
124 born[id] = grow
125 end
126 return ok
127end
128
129-- the nearest other keen hen within ten blocks, and how far
130local function partner(self): (number?, number)
131 local best, far = nil, 10
132 local now = world.time()
133 for id, k in keen do
134 if id ~= self.id and now - k.t < 1 then
135 local d = self:distance_to(k.at)
136 if d < far then
137 best, far = id, d
138 end
139 end
140 end
141 return best, far
142end
143
144local function rested(self)
145 self.love = nil
146 self.rest = knobs.rest * 60
147 keen[self.id] = nil
148end
149
150-- what a fed hen does: go to another fed hen, and make a chick between them
151local function court(self, dt: number)
152 if done[self.id] then
153 done[self.id] = nil
154 rested(self)
155 return
156 end
157 if self.rest then
158 self.rest -= dt
159 if self.rest <= 0 then
160 self.rest = nil
161 end
162 end
163 if not self.love then
164 return
165 end
166 self.love -= dt
167 if self.love <= 0 then
168 self.love = nil
169 keen[self.id] = nil
170 return
171 end
172 keen[self.id] = { at = self.position, t = world.time(), by = self.fed_by }
173 local other, far = partner(self)
174 if not other then
175 return
176 end
177 if far > 1.4 then
178 self:walk_to(keen[other].at)
179 elseif self.id < other then
180 -- the one of the two that goes first makes it, and tells the other
181 local mid = (self.position + keen[other].at) * 0.5
182 if chick_at(mid, knobs.grow * 60) then
183 done[other] = true
184 -- XP for whoever fed the two (world.reward)
185 for _, by in { self.fed_by, keen[other].by } do
186 world.reward(by, BRED_XP)
187 end
188 rested(self)
189 self:say("Buk-buk-BAWK!")
190 end
191 end
192end
193
194-- a fed hen gets keen; a fed chick grows a little
195local function feed(self, player, young: boolean)
196 local food = world.held(player) :: string
197 if young then
198 if world.take(player, food, 1) then
199 self.grow = math.max(0, (self.grow or 0) - 60)
200 self:say("Peep!")
201 end
202 elseif self.love then
203 self:say("Cluck!")
204 elseif self.rest then
205 self:say("Bawk. Not now.")
206 elseif world.take(player, food, 1) then
207 self.love = 30
208 self.fed_by = player.id
209 self:say("Buk-buk!")
210 end
211end
212
213-- ---- eggs and feathers ----
214
215local laid: { vector } = store.get("eggs") or {}
216
217-- the eggs laid here that are still lying where they were laid
218local function lying(): number
219 local still = {}
220 for _, at in laid do
221 if world.block(at) == EGG then
222 table.insert(still, at)
223 end
224 end
225 laid = still
226 return #laid
227end
228
229local function lay(self)
230 local at = self.position
231 local below = at - vector.create(0, 1, 0)
232 if world.block(at) ~= "Air" or world.block(below) == "Air" or world.kind(world.block(below)) == "water" then
233 return
234 end
235 if world.find_block(at, 2, EGG) or lying() >= knobs.eggs then
236 return
237 end
238 world.set_block(at, EGG)
239 table.insert(laid, at)
240 store.set("eggs", laid)
241 self:say("Bawk! Bawk!")
242end
243
244function Hen:spawned()
245 self.egg = world.random(30, knobs.lay * 60)
246end
247
248function Hen:tick(dt: number)
249 court(self, dt)
250 self.egg = (self.egg or knobs.lay * 60) - dt
251 if self.egg <= 0 then
252 self.egg = knobs.lay * 60 * world.random(0.8, 1.2)
253 lay(self)
254 end
255 if self.plucked then
256 self.plucked -= dt
257 if self.plucked <= 0 then
258 self.plucked = nil
259 end
260 end
261 if self.bred then
262 seen[self.id] = { at = self.position, grow = 0, t = world.time() }
263 end
264end
265
266function Hen:used(player)
267 if likes(world.held(player)) then
268 feed(self, player, false)
269 elseif not self.plucked and world.pockets(player) ~= nil and world.give(player, FEATHER, 1) > 0 then
270 -- in survival, a pat now and then gives a feather
271 self.plucked = knobs.feather * 60
272 self:say("Bawk! That was mine.")
273 end
274end
275
276function Hen:hit(player)
277 self:say("BAWK!")
278end
279
280function Chick:spawned()
281 self.bred = true
282 self.grow = born[self.id] or knobs.grow * 60
283 born[self.id] = nil
284end
285
286function Chick:tick(dt: number)
287 self.grow = (self.grow or 0) - dt
288 if self.grow <= 0 then
289 self.grow = nil
290 self.egg = world.random(30, knobs.lay * 60)
291 self:become(Hen)
292 return
293 end
294 seen[self.id] = { at = self.position, grow = self.grow, t = world.time() }
295end
296
297function Chick:used(player)
298 if likes(world.held(player)) then
299 feed(self, player, true)
300 end
301end
302
303-- ---- kept with the world ----
304
305world.every(15, function()
306 local list: { Kept } = {}
307 local now = world.time()
308 for id, s in seen do
309 if now - s.t > 2 then
310 seen[id] = nil
311 else
312 table.insert(list, { at = s.at, grow = s.grow })
313 end
314 end
315 store.set("flock", list)
316end)
317
318-- after a restart, the hens hatched here come back (a reload keeps them, and
319-- they've said where they are by now)
320world.after(0.5, function()
321 if next(seen) ~= nil then
322 return
323 end
324 local kept: { Kept } = store.get("flock") or {}
325 for _, s in kept do
326 if not chick_at(s.at, s.grow) then
327 break
328 end
329 end
330end)