Sign in
creature

Sea turtles

by Potluck · version 1 · Checked for all ages

Big, gentle turtles out in deep water, now and then near anyone by the sea. Climb on one and it carries you over the water at the top, so you can breathe: steer it where you want to go. Bop one and it hides in its shell for a moment.

1--!strict
2-- Sea turtles: big, slow, gentle turtles out in deep water (swims, world
3-- 0.31), now and then near anyone by the sea. Climb on one (it's rideable)
4-- and it carries you across the water, at the top so you can breathe: steer
5-- with your movement, and get off as you would anything else. Bop one and
6-- it pulls into its shell for a moment (self:become). They give nothing:
7-- they're for riding and for looking at.
8
9local world = require("@potluck/world@0.33")
10local tweak = require("@potluck/tweak")
11local sound = require("@potluck/sound@0.2")
12
13local knobs = tweak {
14 how_many = { 1, min = 0, max = 3, label = "How many swim near you out at sea (0 for none)", order = 1 },
15 speed = { 2.6, min = 1, max = 5, label = "How fast one swims with a rider", order = 2 },
16 shell = { "#5a6a3a", label = "Shell color", order = 3 },
17}
18
19local SKIN = "#8a9a6a"
20local SPOT = "#3a4a28"
21local EYE, PUPIL = "#f4f2e8", "#141414"
22
23local function body(hidden: boolean): { any }
24 local b: { any } = {
25 { "shell", size = { 1.1, 0.34, 1.3 }, at = { 0, 0.42, 0.05 }, color = knobs.shell },
26 { "shell_top", size = { 0.8, 0.14, 1.0 }, at = { 0, 0.64, 0.05 }, color = knobs.shell },
27 { "plate_1", size = { 0.3, 0.04, 0.3 }, at = { 0, 0.72, -0.2 }, color = SPOT },
28 { "plate_2", size = { 0.3, 0.04, 0.3 }, at = { 0, 0.72, 0.25 }, color = SPOT },
29 { "plate_3", size = { 0.2, 0.04, 0.26 }, at = { -0.32, 0.6, 0.05 }, color = SPOT },
30 { "plate_4", size = { 0.2, 0.04, 0.26 }, at = { 0.32, 0.6, 0.05 }, color = SPOT },
31 { "belly", size = { 1.0, 0.1, 1.2 }, at = { 0, 0.24, 0.05 }, color = "#d8d0a0" },
32 }
33 if hidden then
34 return b
35 end
36 table.insert(b, { "head", size = { 0.34, 0.3, 0.4 }, at = { 0, 0.42, -0.78 }, color = SKIN, head = true })
37 table.insert(b, { "beak", size = { 0.2, 0.08, 0.1 }, at = { 0, 0.34, -1.0 }, color = "#5a5a40", head = true })
38 for _, s in { -1, 1 } do
39 local n = if s < 0 then "left" else "right"
40 table.insert(b, { "eye_" .. n, size = { 0.04, 0.1, 0.1 }, at = { s * 0.17, 0.48, -0.88 }, color = EYE, head = true })
41 table.insert(b, { "pupil_" .. n, size = { 0.04, 0.06, 0.06 }, at = { s * 0.19, 0.48, -0.9 }, color = PUPIL, head = true })
42 table.insert(b, { "flipper_front_" .. n, size = { 0.6, 0.06, 0.28 }, at = { s * 0.72, 0.36, -0.35 }, color = SKIN, leg = true })
43 table.insert(b, { "flipper_back_" .. n, size = { 0.3, 0.06, 0.22 }, at = { s * 0.5, 0.32, 0.65 }, color = SKIN, leg = true })
44 end
45 table.insert(b, { "tail", size = { 0.12, 0.08, 0.2 }, at = { 0, 0.34, 0.78 }, color = SKIN })
46 return b
47end
48
49local Turtle = world.creature {
50 name = "Sea turtle",
51 speed = knobs.speed,
52 count = 0,
53 home = "here",
54 swims = true,
55 rideable = true,
56 seat = 0.85,
57 size = { 1.0, 0.6, 1.0 },
58 behaviors = { wander = false },
59 body = body(false),
60}
61
62local Shy = world.creature {
63 name = "Sea turtle (in its shell)",
64 speed = 0,
65 count = 0,
66 home = "here",
67 swims = true,
68 size = { 1.0, 0.6, 1.0 },
69 behaviors = { wander = false },
70 body = body(true),
71}
72
73local MOST = 8
74local NEAR, FAR = 10, 24
75local LONELY = 64
76local DEEP = 4 -- blocks of water it wants under it
77
78local UP = vector.create(0, 1, 0)
79local where: { [number]: vector } = {}
80
81local function water(at: vector): boolean
82 return world.block(at) == "Water"
83end
84
85local function gone(self)
86 where[self.id] = nil
87 self:remove()
88end
89
90function Turtle:spawned()
91 self.think = 0
92 self.surface = world.random(8, 20)
93end
94
95local function common(self, dt: number): boolean
96 where[self.id] = self.position
97 if knobs.how_many < 1 and not self.rider then
98 gone(self)
99 return false
100 end
101 self.think -= dt
102 if self.think > 0 then
103 return false
104 end
105 self.think = world.random(0.8, 1.4)
106 if not self.rider and not world.nearest_player(self.position, LONELY) then
107 gone(self)
108 return false
109 end
110 return true
111end
112
113function Turtle:tick(dt: number)
114 if self.rider then
115 self.surface = 4
116 return
117 end
118 if not common(self, dt) then
119 return
120 end
121 -- now and then it comes up for air, then goes back down
122 self.surface -= 1
123 if self.surface <= 0 then
124 self:climb(0.8)
125 if not water(self.position + UP * 1.2) then
126 self.surface = world.random(10, 24)
127 self:climb(0)
128 end
129 return
130 end
131 self:wander(1.2)
132end
133
134function Turtle:hit(player)
135 self.hidden = 3
136 sound.play("thud", { at = self, volume = 0.5, pitch = 0.7, caption = "A turtle hides in its shell" })
137 self:become(Shy)
138 self:stop()
139end
140
141function Shy:tick(dt: number)
142 where[self.id] = self.position
143 self.hidden = (self.hidden or 0) - dt
144 if self.hidden <= 0 then
145 self.hidden = nil
146 self:become(Turtle)
147 end
148end
149
150function Turtle:ridden(player)
151 sound.play("splash", { at = self, volume = 0.5, pitch = 0.7 })
152end
153
154-- Now and then one more out in deep water near each player by the sea.
155world.every(6, function()
156 local want = math.floor(knobs.how_many)
157 if want < 1 then
158 return
159 end
160 local live = {}
161 for _, id in world.creatures() do
162 live[id] = true
163 end
164 local total = 0
165 for id in where do
166 if live[id] then
167 total += 1
168 else
169 where[id] = nil
170 end
171 end
172 for _, p in world.players() do
173 -- the Long Dusk has creatures of its own
174 if world.place(p.position) == "dusk" then
175 continue
176 end
177 local near = 0
178 for _, at in where do
179 if vector.magnitude(at - p.position) < FAR + 16 then
180 near += 1
181 end
182 end
183 if total >= MOST or near >= want or world.random() > 0.5 then
184 continue
185 end
186 for _ = 1, 3 do
187 local a = world.random(0, 2 * math.pi)
188 local r = world.random(NEAR, FAR)
189 local x, z = math.floor(p.position.x + math.cos(a) * r) + 0.5, math.floor(p.position.z + math.sin(a) * r) + 0.5
190 local floor = world.top(x, z) + 1
191 local n = 0
192 while n < DEEP + 1 and water(vector.create(x, floor + n, z)) do
193 n += 1
194 end
195 if n >= DEEP then
196 local at = vector.create(x, floor + 1, z)
197 where[world.spawn(Turtle, at)] = at
198 total += 1
199 break
200 end
201 end
202 end
203end)