Sign in
creature

Woolly

by Potluck · version 1 · Checked for all ages

Sheep that graze the Green. Hold wheat and they come to you, pat one and it follows you around for a while, bop one and it's off. In survival, patting one shears it for wool (more with shears in hand), which grows back in a few minutes. Feed two of them wheat and they have a lamb, which grows into a woolly.

1--!strict
2-- Woolly: sheep that graze the Green and nibble the tall grass.
3-- Hold wheat and they come to you; Interact with one and it follows you for
4-- a while; hit one and it runs. All that is `behaviors`, with no code.
5-- In survival, Interact shears it too: you get wool (more with shears in
6-- hand, world.held), and it's a Shorn woolly (self:become) until its fleece
7-- grows back. Feed two of them wheat and they have a lamb, which grows into
8-- a woolly: the lambs and the woollies they grow into are kept with the
9-- world (store), since the game only brings back as many as `count` after a
10-- restart.
11
12local world = require("@potluck/world@0.22")
13local tweak = require("@potluck/tweak")
14local store = require("@potluck/store")
15
16local knobs = tweak {
17 wool = { "#efe9dc", label = "Wool color", order = 1 },
18 follow = { 20, min = 5, max = 60, label = "Seconds it follows you", order = 2 },
19 flock = { 4, min = 1, max = 8, label = "How many", order = 3 },
20 shear = { 1, min = 1, max = 4, label = "Wool from shearing one", order = 4 },
21 shears = { 2, min = 0, max = 4, label = "More wool with shears", order = 6 },
22 regrow = { 3, min = 0.5, max = 20, label = "Minutes for its wool to grow back", order = 5 },
23 grow = { 5, min = 0.5, max = 30, label = "Minutes for a lamb to grow up", order = 7 },
24 rest = { 5, min = 0.5, max = 30, label = "Minutes before a woolly has another lamb", order = 8 },
25}
26
27local FOOD = "Wheat"
28-- room for everything else of this creation's under the 64 a creation may have
29local MOST = 48
30
31-- one sheep's body at scale `k`: a lumpy fleece (or a clipped one), a big
32-- friendly head with a pink muzzle and bright eyes, sturdy legs
33local DARK = "#3b3430"
34local function sheep(k: number, fleecy: boolean): { any }
35 local function part(name: string, size: { number }, at: { number }, color: string, kind: string?): any
36 local p: any = { name, size = { size[1] * k, size[2] * k, size[3] * k }, at = { at[1] * k, at[2] * k, at[3] * k }, color = color }
37 if kind == "head" then
38 p.head = true
39 elseif kind == "leg" then
40 p.leg = true
41 end
42 return p
43 end
44 local b: { any } = {}
45 local hy, hz = 1.05, -0.8
46 if fleecy then
47 table.insert(b, part("fleece", { 1.0, 0.8, 1.3 }, { 0, 0.85, 0.05 }, knobs.wool))
48 table.insert(b, part("fleece_shoulders", { 1.1, 0.5, 0.5 }, { 0, 0.95, -0.32 }, knobs.wool))
49 table.insert(b, part("fleece_rump", { 1.1, 0.5, 0.5 }, { 0, 0.95, 0.45 }, knobs.wool))
50 table.insert(b, part("fleece_top", { 0.8, 0.2, 1.0 }, { 0, 1.32, 0.05 }, knobs.wool))
51 table.insert(b, part("fleece_chest", { 0.7, 0.4, 0.2 }, { 0, 0.8, -0.62 }, knobs.wool))
52 table.insert(b, part("tail", { 0.24, 0.22, 0.16 }, { 0, 1.0, 0.75 }, knobs.wool))
53 else
54 hy, hz = 1.0, -0.72
55 table.insert(b, part("fleece", { 0.78, 0.58, 1.12 }, { 0, 0.8, 0.05 }, knobs.wool))
56 table.insert(b, part("tail", { 0.16, 0.14, 0.12 }, { 0, 0.95, 0.64 }, knobs.wool))
57 end
58 table.insert(b, part("head", { 0.56, 0.56, 0.54 }, { 0, hy, hz }, DARK, "head"))
59 if fleecy then
60 table.insert(b, part("fleece_crown", { 0.4, 0.14, 0.36 }, { 0, hy + 0.33, hz + 0.06 }, knobs.wool, "head"))
61 end
62 table.insert(b, part("muzzle", { 0.24, 0.16, 0.08 }, { 0, hy - 0.1, hz - 0.3 }, "#e8b7ad", "head"))
63 for _, side in { -1, 1 } do
64 local n = if side < 0 then "left" else "right"
65 table.insert(b, part("ear_" .. n, { 0.26, 0.1, 0.14 }, { side * 0.4, hy + 0.1, hz + 0.06 }, "#4a403a", "head"))
66 table.insert(b, part("eye_" .. n, { 0.13, 0.13, 0.04 }, { side * 0.14, hy + 0.08, hz - 0.29 }, "#f4f1e8", "head"))
67 table.insert(b, part("pupil_" .. n, { 0.07, 0.09, 0.04 }, { side * 0.14, hy + 0.07, hz - 0.31 }, "#15110f", "head"))
68 table.insert(b, part("leg_front_" .. n, { 0.2, 0.44, 0.2 }, { side * 0.3, 0.22, -0.4 }, DARK, "leg"))
69 table.insert(b, part("leg_back_" .. n, { 0.2, 0.44, 0.2 }, { side * 0.3, 0.22, 0.45 }, DARK, "leg"))
70 end
71 return b
72end
73
74-- what a sheep does by itself, sheared or not
75local SHEEP = {
76 follow_food = FOOD,
77 follow_when_used = knobs.follow,
78 flee_when_hit = 6,
79 hop_when_hit = 0.8,
80 chatter = { "Baa.", "Baa?", "Meh-eh-eh." },
81 sound = "bleat",
82}
83
84local Woolly = world.creature {
85 name = "Woolly",
86 speed = 2,
87 count = math.floor(knobs.flock),
88 home = "green",
89 behaviors = SHEEP,
90 body = sheep(1, true),
91}
92
93-- the same sheep with its fleece clipped short
94local Shorn = world.creature {
95 name = "Shorn woolly",
96 speed = 2.2,
97 count = 0,
98 home = "green",
99 behaviors = SHEEP,
100 body = sheep(1, false),
101}
102
103-- a young one, small and woolly, too young to shear
104local Lamb = world.creature {
105 name = "Lamb",
106 speed = 1.8,
107 count = 0,
108 home = "green",
109 behaviors = {
110 follow_food = FOOD,
111 flee_when_hit = 5,
112 hop_when_hit = 0.6,
113 chatter = { "Meh!", "Baa?" },
114 sound = "bleat",
115 },
116 body = sheep(0.55, true),
117}
118
119-- ---- lambs: two fed woollies have one ----
120
121-- who's keen and where, each saying every tick (an old word is someone gone)
122type Keen = { at: vector, t: number, by: number? }
123local keen: { [number]: Keen } = {}
124-- XP for each feeding that leads to a young one
125local BRED_XP = 3
126-- a woolly whose partner had the lamb: it rests too
127local done: { [number]: boolean } = {}
128-- how grown a lamb spawned from here is to be, by its id, for its `spawned`
129local born: { [number]: number } = {}
130
131-- every lamb born here and the woollies they grow into, each saying every
132-- tick, kept with the world so they're back after a restart
133type Seen = { at: vector, grow: number, t: number }
134local seen: { [number]: Seen } = {}
135type Kept = { at: vector, grow: number }
136
137local function lamb_at(at: vector, grow: number): boolean
138 if #world.creatures() >= MOST then
139 return false
140 end
141 local ok, id = pcall(world.spawn, Lamb, at)
142 if ok then
143 born[id] = grow
144 end
145 return ok
146end
147
148-- the nearest other keen woolly within ten blocks, and how far
149local function partner(self): (number?, number)
150 local best, far = nil, 10
151 local now = world.time()
152 for id, k in keen do
153 if id ~= self.id and now - k.t < 1 then
154 local d = self:distance_to(k.at)
155 if d < far then
156 best, far = id, d
157 end
158 end
159 end
160 return best, far
161end
162
163local function rested(self)
164 self.love = nil
165 self.rest = knobs.rest * 60
166 keen[self.id] = nil
167end
168
169-- what a fed woolly does: go to another fed one, and have a lamb between them
170local function court(self, dt: number)
171 if done[self.id] then
172 done[self.id] = nil
173 rested(self)
174 return
175 end
176 if self.rest then
177 self.rest -= dt
178 if self.rest <= 0 then
179 self.rest = nil
180 end
181 end
182 if not self.love then
183 return
184 end
185 self.love -= dt
186 if self.love <= 0 then
187 self.love = nil
188 keen[self.id] = nil
189 return
190 end
191 keen[self.id] = { at = self.position, t = world.time(), by = self.fed_by }
192 local other, far = partner(self)
193 if not other then
194 return
195 end
196 if far > 1.8 then
197 self:walk_to(keen[other].at)
198 elseif self.id < other then
199 -- the one of the two that goes first has it, and tells the other
200 local mid = (self.position + keen[other].at) * 0.5
201 if lamb_at(mid, knobs.grow * 60) then
202 done[other] = true
203 -- XP for whoever fed the two (world.reward)
204 for _, by in { self.fed_by, keen[other].by } do
205 world.reward(by, BRED_XP)
206 end
207 rested(self)
208 self:say("Baa-aa!")
209 end
210 end
211end
212
213-- a fed woolly gets keen; a fed lamb grows a little
214local function feed(self, player, young: boolean)
215 if young then
216 if world.take(player, FOOD, 1) then
217 self.grow = math.max(0, (self.grow or 0) - 60)
218 self:say("Meh!")
219 end
220 elseif self.love then
221 self:say("Baa.")
222 elseif self.rest then
223 self:say("Baa. Not now.")
224 elseif world.take(player, FOOD, 1) then
225 self.love = 30
226 self.fed_by = player.id
227 self:say("Nom nom.")
228 end
229end
230
231function Woolly:spawned()
232 self.nibble = world.random(10, 40)
233end
234
235function Woolly:tick(dt: number)
236 court(self, dt)
237 if self.bred then
238 seen[self.id] = { at = self.position, grow = 0, t = world.time() }
239 end
240 if self.regrow then
241 self.regrow -= dt
242 if self.regrow <= 0 then
243 self.regrow = nil
244 self:become(Woolly)
245 end
246 end
247 -- now and then a mouthful of the tall grass underfoot
248 self.nibble = (self.nibble or 30) - dt
249 if self.nibble <= 0 then
250 self.nibble = world.random(20, 50)
251 local spot = world.find_block(self.position, 3, "Tall grass")
252 if spot then
253 world.set_block(spot, "Air")
254 self:say("Nom.")
255 end
256 end
257end
258
259function Woolly:used(player)
260 if world.held(player) == FOOD then
261 feed(self, player, false)
262 return
263 end
264 -- in survival, a fleecy one gives up its wool
265 local wool = math.floor(knobs.shear) + (if world.held(player) == "Shears" then math.floor(knobs.shears) else 0)
266 if not self.regrow and world.pockets(player) ~= nil and world.give(player, "Cream wool", wool) > 0 then
267 self:become(Shorn)
268 self.regrow = knobs.regrow * 60
269 self:say("Baa! A bit chilly.")
270 end
271end
272
273function Woolly:hit(player)
274 self:say("Baa!")
275end
276
277-- a shorn one is the same sheep
278Shorn.spawned = Woolly.spawned
279Shorn.tick = Woolly.tick
280Shorn.used = Woolly.used
281Shorn.hit = Woolly.hit
282
283function Lamb:spawned()
284 self.bred = true
285 self.grow = born[self.id] or knobs.grow * 60
286 born[self.id] = nil
287end
288
289function Lamb:tick(dt: number)
290 self.grow = (self.grow or 0) - dt
291 if self.grow <= 0 then
292 self.grow = nil
293 self.nibble = world.random(10, 40)
294 self:become(Woolly)
295 return
296 end
297 seen[self.id] = { at = self.position, grow = self.grow, t = world.time() }
298end
299
300function Lamb:used(player)
301 if world.held(player) == FOOD then
302 feed(self, player, true)
303 end
304end
305
306-- ---- kept with the world ----
307
308world.every(15, function()
309 local list: { Kept } = {}
310 local now = world.time()
311 for id, s in seen do
312 if now - s.t > 2 then
313 seen[id] = nil
314 else
315 table.insert(list, { at = s.at, grow = s.grow })
316 end
317 end
318 store.set("flock", list)
319end)
320
321-- after a restart, the woollies born here come back (a reload keeps them,
322-- and they've said where they are by now)
323world.after(0.5, function()
324 if next(seen) ~= nil then
325 return
326 end
327 local kept: { Kept } = store.get("flock") or {}
328 for _, s in kept do
329 if not lamb_at(s.at, s.grow) then
330 break
331 end
332 end
333end)