Sign in
creature

Bees

by Potluck · version 1 · Checked for all ages

Bees buzz from flower to flower on the Green. Put down a beehive (a workbench makes one from planks and hay) near flowers, and bees come to live in it and fill it with honey: use a full hive to take two jars of honey. Leave them be: bop a bee in survival and it stings.

1--!strict
2-- Bees: they fly from flower to flower, and home to a beehive with what
3-- they gather. A hive the bees have brought enough to is full of honey: use
4-- it and you get two jars, and it's empty again. Each hive put down brings
5-- two more bees, and the hives (with how full each is) are kept with the
6-- world. Bop a bee in survival and it stings.
7
8local world = require("@potluck/world@0.20")
9local tweak = require("@potluck/tweak")
10local store = require("@potluck/store")
11local sound = require("@potluck/sound")
12
13local knobs = tweak {
14 swarm = { 4, min = 1, max = 8, label = "Bees on the Green", order = 1 },
15 per_hive = { 2, min = 0, max = 4, label = "More bees for each hive", order = 2 },
16 trips = { 8, min = 1, max = 40, label = "Trips to fill a hive", order = 3 },
17 honey = { 2, min = 1, max = 6, label = "Honey from a full hive", order = 4 },
18 sting = { 1, min = 0, max = 6, label = "How much a sting hurts", order = 5 },
19}
20
21local HIVE = "Beehive"
22local FULL = "Full beehive"
23local FLOWERS = { "Red flower", "Blue flower", "White flower", "Violet flower" }
24-- how far a bee goes for a flower, and from its hive
25local FORAGE = 14
26local RANGE = 24
27-- room for everything else of this creation's under the 64 a creation may have
28local MOST = 40
29
30local YELLOW = "#f2c430"
31local DARK = "#2a1c0c"
32local WING = "#e4eef6"
33
34local Bee = world.creature {
35 name = "Bee",
36 speed = 2.6,
37 count = math.floor(knobs.swarm),
38 home = "green",
39 flies = true,
40 behaviors = {
41 wander = false,
42 flee_when_hit = 6,
43 chatter = { "Bzz.", "Bzzz." },
44 sound = "buzz",
45 },
46 body = {
47 { "body", size = { 0.2, 0.18, 0.28 }, at = { 0, 0.2, 0.02 }, color = YELLOW },
48 { "stripe_front", size = { 0.21, 0.19, 0.05 }, at = { 0, 0.2, 0.02 }, color = DARK },
49 { "stripe_back", size = { 0.21, 0.19, 0.05 }, at = { 0, 0.2, 0.11 }, color = DARK },
50 { "head", size = { 0.15, 0.15, 0.1 }, at = { 0, 0.21, -0.16 }, color = DARK, head = true },
51 { "wing_left", size = { 0.16, 0.02, 0.12 }, at = { -0.12, 0.31, 0 }, color = WING },
52 { "wing_right", size = { 0.16, 0.02, 0.12 }, at = { 0.12, 0.31, 0 }, color = WING },
53 },
54}
55
56-- ---- hives ----
57
58type Hive = { at: vector, trips: number }
59local hives: { [string]: Hive } = store.get("hives") or {}
60
61local function key(at: vector): string
62 return math.floor(at.x) .. "," .. math.floor(at.y) .. "," .. math.floor(at.z)
63end
64
65local function save()
66 store.set("hives", hives)
67end
68
69local function count(): number
70 local n = 0
71 for _ in hives do
72 n += 1
73 end
74 return n
75end
76
77-- forget hives that have gone (broken, or changed by something else)
78local function tidy()
79 local gone = false
80 for k, h in hives do
81 local b = world.block(h.at)
82 if b ~= HIVE and b ~= FULL then
83 hives[k] = nil
84 gone = true
85 end
86 end
87 if gone then
88 save()
89 end
90end
91
92-- the nearest hive with room for honey, within reach
93local function nearest_hive(pos: vector): string?
94 local best, far = nil, RANGE
95 for k, h in hives do
96 if h.trips < knobs.trips then
97 local d = vector.magnitude(h.at - pos)
98 if d < far then
99 best, far = k, d
100 end
101 end
102 end
103 return best
104end
105
106-- a flower nearby, one of the near ones at random
107local function flower_near(pos: vector): vector?
108 local found = {}
109 for _, name in FLOWERS do
110 local at = world.find_block(pos, FORAGE, name)
111 if at then
112 table.insert(found, at)
113 end
114 end
115 if #found == 0 then
116 return nil
117 end
118 return found[math.floor(world.random(1, #found + 0.999))]
119end
120
121-- more bees for the hives there are, up to the most
122local function top_up()
123 local want = math.min(MOST, math.floor(knobs.swarm) + count() * math.floor(knobs.per_hive))
124 local have = #world.creatures(Bee)
125 if have >= want then
126 return
127 end
128 -- by each hive in turn
129 local at = {}
130 for _, h in hives do
131 table.insert(at, h.at + vector.create(0, 1, 0))
132 end
133 local i = 0
134 while have < want and #at > 0 do
135 if not pcall(world.spawn, Bee, at[i % #at + 1]) then
136 break
137 end
138 have += 1
139 i += 1
140 end
141end
142
143-- ---- a bee's day ----
144
145-- a bee came to its hive with what it gathered
146local function bring(k: string)
147 local h = hives[k]
148 if not h or world.block(h.at) ~= HIVE then
149 return
150 end
151 h.trips += 1
152 if h.trips >= knobs.trips then
153 world.set_block(h.at, FULL)
154 end
155 save()
156end
157
158-- where it goes next: home with pollen, or to a flower
159local function choose(self)
160 self.goal = nil
161 self.hive = nil
162 if self.pollen then
163 local k = nearest_hive(self.position)
164 if k then
165 self.hive = k
166 self.goal = hives[k].at + vector.create(0, 0.6, 0)
167 return
168 end
169 -- nowhere to take it
170 self.pollen = nil
171 end
172 local f = flower_near(self.position)
173 if f then
174 self.goal = f + vector.create(0, 0.2, 0)
175 else
176 -- nothing about: a little drift
177 self.goal = self.position + vector.create(world.random(-4, 4), world.random(-0.5, 1), world.random(-4, 4))
178 end
179end
180
181function Bee:spawned()
182 self.think = world.random(0, 2)
183end
184
185function Bee:tick(dt: number)
186 self.think = (self.think or 0) - dt
187 local goal: vector? = self.goal
188 if goal then
189 local d = goal - self.position
190 self:climb(math.clamp(d.y * 1.5, -2, 2))
191 local flat = vector.magnitude(vector.create(d.x, 0, d.z))
192 if flat > 0.4 then
193 self:walk_to(goal)
194 else
195 self:stop()
196 end
197 if flat < 0.8 and math.abs(d.y) < 0.8 then
198 self.goal = nil
199 if self.hive then
200 bring(self.hive)
201 self.pollen = nil
202 self.hive = nil
203 self.think = world.random(4, 10)
204 else
205 -- a flower's worth, then a moment on it before going home
206 self.pollen = true
207 self.think = world.random(1, 3)
208 end
209 end
210 end
211 if self.think <= 0 then
212 self.think = world.random(6, 12)
213 choose(self)
214 end
215end
216
217function Bee:hit(player)
218 self.goal = nil
219 self.pollen = nil
220 if knobs.sting > 0 and world.hurt(player, knobs.sting) then
221 self:say("BZZT!")
222 else
223 self:say("Bzz!")
224 end
225end
226
227-- ---- players and hives ----
228
229world.drops(FULL, { gives = HIVE })
230
231world.on("block_placed", function(player, at: vector, block: string)
232 if block == HIVE or block == FULL then
233 hives[key(at)] = { at = at, trips = if block == FULL then knobs.trips else 0 }
234 save()
235 top_up()
236 end
237end)
238
239world.on("block_broken", function(player, at: vector, block: string)
240 if block == HIVE or block == FULL then
241 hives[key(at)] = nil
242 save()
243 end
244end)
245
246world.on("block_used", function(player, at: vector, block: string)
247 if block ~= FULL then
248 return
249 end
250 local n = math.floor(knobs.honey)
251 if world.pockets(player) ~= nil and world.give(player, "Honey", n) == 0 then
252 return
253 end
254 world.set_block(at, HIVE)
255 local h = hives[key(at)]
256 if h then
257 h.trips = 0
258 else
259 hives[key(at)] = { at = at, trips = 0 }
260 end
261 save()
262 sound.play("buzz", { at = at })
263end)
264
265-- hives that have gone are forgotten, and bees come for new ones (and
266-- after a restart, back for the old)
267world.every(20, function()
268 tidy()
269 top_up()
270end)
271world.after(0.5, top_up)