Sign in
mod

Buckets and fishing

by Potluck · version 1 · Checked for all ages

A bucket carries water: use it on water to fill it, and on a block to pour the water out on top. A fishing rod catches fish in any water: use it on the water, keep it in hand, and wait for a bite. Perch and trout bite in rivers and ponds, mackerel and flounder out in deep water, and now and then something else comes up: driftwood, an old tangle of line, or treasure. Fish cook in a furnace or on a campfire. Make both at a workbench.

1--!strict
2-- Buckets and fishing, for survival. Interact on water with something in
3-- hand is heard as block_used on "Water" (world 0.17), so:
4-- a Bucket used on water fills it into a Water bucket, and a Water bucket
5-- used on a block pours the water out on top of it and empties again.
6-- A Fishing rod used on water casts its line: keep the rod in hand and
7-- stay near, and after a few seconds something bites (SHALLOW, DEEP, JUNK
8-- and TREASURE below say what: the Fish creation's fish when it's here, by how deep the water
9-- is, some junk and now and then treasure, two of them relics for the
10-- Curator). Fish cook into cooked fish in a furnace or on a campfire, and
11-- driftwood burns in a furnace.
12
13local world = require("@potluck/world@0.34")
14local tweak = require("@potluck/tweak")
15local ui = require("@potluck/ui@0.2")
16local sound = require("@potluck/sound@0.2")
17
18local knobs = tweak {
19 bite = { 6, min = 1, max = 30, label = "Seconds to a bite, about", order = 1 },
20 pour = { true, label = "A water bucket pours water out", order = 2 },
21 casts = { 64, min = 8, max = 256, label = "Catches before a rod wears out", order = 3 },
22}
23
24local UP = vector.create(0, 1, 0)
25local ROD, BUCKET, FULL = "Fishing rod", "Bucket", "Water bucket"
26
27-- What comes up on a line, each by its chance: mostly fish, which in
28-- shallow water (rivers, ponds, the creek) are the Fish creation's perch
29-- and trout and out in deep water its mackerel and flounder, when it's
30-- here; then junk, and now and then treasure. Anything that isn't in this
31-- world is left out, and the rest share its chance.
32type Catch = { thing: string, chance: number }
33local SHALLOW: { Catch } = {
34 { thing = "Perch", chance = 0.32 },
35 { thing = "Trout", chance = 0.24 },
36 { thing = "Raw fish", chance = 0.28 },
37}
38local DEEP: { Catch } = {
39 { thing = "Mackerel", chance = 0.34 },
40 { thing = "Flounder", chance = 0.2 },
41 { thing = "Raw fish", chance = 0.3 },
42}
43local JUNK: { Catch } = {
44 { thing = "Driftwood", chance = 0.08 },
45 { thing = "Flint", chance = 0.04 },
46 { thing = "Silk", chance = 0.02 }, -- a tangle of old line
47}
48local TREASURE: { Catch } = {
49 { thing = "Iron ingot", chance = 0.02 }, -- somebody dropped it
50 { thing = "Gold ingot", chance = 0.012 },
51 { thing = "Crystal", chance = 0.01 },
52 { thing = "Golden lure", chance = 0.004 },
53 { thing = "Ship in a bottle", chance = 0.004 },
54}
55local IS_TREASURE: { [string]: boolean } = {}
56for _, c in TREASURE do
57 IS_TREASURE[c.thing] = true
58end
59-- how many blocks of water count as deep
60local DEEP_WATER = 5
61-- what a catch is called when it's told
62local SAID: { [string]: string } = {
63 ["Raw fish"] = "a fish",
64 Driftwood = "some driftwood",
65 Silk = "a tangle of old line",
66 Flint = "a lump of flint",
67 Crystal = "a crystal",
68}
69
70-- Blocks other creations use, which a water bucket leaves alone so using
71-- them still works: a remix can add its own.
72local KEEP_DRY: { [string]: boolean } = {
73 Table = true, Furnace = true, ["Lit furnace"] = true, Campfire = true, Anvil = true,
74 Crate = true, Barrel = true, ["Old chest"] = true, Kiln = true, Farmland = true,
75}
76
77-- one line for a few seconds, for each player
78local shown: { [number]: number } = {}
79local function tell(player, text: string, seconds: number?)
80 ui.show(player, "water", text, { at = "bottom" })
81 local mine = (shown[player.id] or 0) + 1
82 shown[player.id] = mine
83 world.after(seconds or 3, function()
84 if shown[player.id] == mine then
85 shown[player.id] = nil
86 ui.hide(player, "water")
87 end
88 end)
89end
90
91-- swaps one thing in hand for another, or says why not
92local function swap(player, from: string, to: string): boolean
93 if not world.take(player, from, 1) then
94 return false
95 end
96 if world.give(player, to, 1) == 0 then
97 world.give(player, from, 1)
98 tell(player, "Your pockets are full.")
99 return false
100 end
101 return true
102end
103
104-- Fishing: who's waiting for a bite, where, and how long till it comes.
105type Line = { at: vector, left: number }
106local lines: { [number]: Line } = {}
107local caught: { [number]: number } = {} -- catches on each player's rod
108
109local function exists(name: string): boolean
110 return (pcall(world.kind, name))
111end
112
113-- how deep the water is at a spot on it
114local function depth_at(at: vector): number
115 local x, z = at.x, at.z
116 local y, n = world.top(x, z) + 1, 0
117 while n < 12 and world.block(vector.create(x, y + n, z)) == "Water" do
118 n += 1
119 end
120 return n
121end
122
123-- `luck` (world.perk) makes treasure turn up half as often again, a level
124-- at a time
125local function catch(at: vector, luck: number): string
126 local list: { Catch } = {}
127 local total = 0
128 for _, group in { if depth_at(at) >= DEEP_WATER then DEEP else SHALLOW, JUNK, TREASURE } do
129 for _, c in group do
130 if exists(c.thing) then
131 local chance = if IS_TREASURE[c.thing] then c.chance * (1 + 1.5 * luck) else c.chance
132 table.insert(list, { thing = c.thing, chance = chance })
133 total += chance
134 end
135 end
136 end
137 local roll = world.random() * total
138 for _, c in list do
139 roll -= c.chance
140 if roll <= 0 then
141 return c.thing
142 end
143 end
144 return "Raw fish"
145end
146
147local function said(thing: string): string
148 if SAID[thing] then
149 return SAID[thing]
150 end
151 local name = string.lower(thing)
152 return (if string.find(name, "^[aeiou]") then "an " else "a ") .. name
153end
154
155world.on("block_used", function(player, at: vector, block: string)
156 if world.pockets(player) == nil then
157 return
158 end
159 local held = world.held(player)
160 if block == "Water" then
161 if held == BUCKET then
162 if swap(player, BUCKET, FULL) then
163 sound.play("splash", { at = at, volume = 0.6 })
164 end
165 elseif held == ROD and not lines[player.id] then
166 lines[player.id] = { at = at, left = knobs.bite * world.random(0.6, 1.6) }
167 sound.play("cast", { at = at, volume = 0.7 })
168 tell(player, "Fishing. Keep the rod in hand and wait for a bite.", 4)
169 elseif held == ROD then
170 -- reeled in before anything bit
171 lines[player.id] = nil
172 ui.hide(player, "water")
173 end
174 elseif held == FULL and knobs.pour and not KEEP_DRY[block] and block ~= "Air" then
175 local above = at + UP
176 if world.block(above) == "Air" and swap(player, FULL, BUCKET) then
177 world.set_block(above, "Water")
178 sound.play("splash", { at = above })
179 end
180 end
181end)
182
183world.every(0.25, function()
184 local here = {}
185 for _, p in world.players() do
186 here[p.id] = p
187 end
188 for id, line in lines do
189 local p = here[id]
190 if not p or world.held(p) ~= ROD or vector.magnitude(p.position - line.at) > 12 then
191 lines[id] = nil
192 continue
193 end
194 line.left -= 0.25
195 if line.left <= 0 then
196 lines[id] = nil
197 local thing = catch(line.at, world.perk(p, "luck"))
198 if world.give(p, thing, 1) == 0 then
199 tell(p, "Something bit, but your pockets are full.")
200 continue
201 end
202 sound.play("bite", { at = line.at })
203 if IS_TREASURE[thing] then
204 sound.play_for(p, "sparkle", { caption = "Treasure!" })
205 world.reward(p, 4)
206 end
207 local what = said(thing)
208 tell(p, "You caught " .. what .. (if IS_TREASURE[thing] then "!" else "."))
209 -- every catch wears the rod a little
210 caught[id] = (caught[id] or 0) + 1
211 if caught[id] >= math.floor(knobs.casts) then
212 caught[id] = 0
213 if world.take(p, ROD, 1) then
214 tell(p, "You caught " .. what .. ", and your fishing rod broke.")
215 sound.play_for(p, "crack", { pitch = 1.3, caption = "A rod breaks" })
216 end
217 end
218 end
219 end
220end)
221
222world.on("player_left", function(p)
223 lines[p.id] = nil
224 caught[p.id] = nil
225 shown[p.id] = nil
226end)