Sign in
mod

Farming

by Potluck · version 1 · Checked for all ages

Grow your own: Interact on grass to dig farmland (a hoe digs a square at once), then plant wheat, carrots, potatoes, beans, pumpkins, melons, sugar cane, cocoa, rice or beets. Crops grow faster by day, near water, in rain and in spring and summer, and stop outdoors in winter (a greenhouse keeps them going). A watering can brings them on and a sickle reaps a square at once. Tall grass gives seeds, and wild crops and berry bushes come up around you.

1--!strict
2-- Farming's own tests: `potluck-server --test=farming`.
3
4local test = require("@potluck/test@0.4")
5local world = require("@potluck/world@0.20")
6local sky = require("@potluck/sky@0.1")
7
8local UP = vector.create(0, 1, 0)
9
10-- a 7 by 7 patch of grass open to the sky, up in the air, and its middle
11local function patch(): vector
12 local mid = vector.create(200.5, 110.5, 200.5)
13 for dx = -3, 3 do
14 for dz = -3, 3 do
15 local at = mid + vector.create(dx, 0, dz)
16 world.set_block(at, "Grass")
17 world.set_block(at + UP, "Air")
18 end
19 end
20 return mid
21end
22
23-- how many of `block` there are in the square `reach` each way around `mid`
24local function count(mid: vector, reach: number, block: string): number
25 local n = 0
26 for dx = -reach, reach do
27 for dz = -reach, reach do
28 if world.block(mid + vector.create(dx, 0, dz)) == block then
29 n += 1
30 end
31 end
32 end
33 return n
34end
35
36-- nine shoots of wheat planted around `mid`, and a player with a can
37local function sown(): (any, vector)
38 local p = test.player("Ada", { survival = true, carrying = { ["Copper hoe"] = 1, ["Watering can"] = 1, Sickle = 1, Wheat = 9 } })
39 local mid = patch()
40 test.hold(p, "Copper hoe")
41 test.use_block(p, mid)
42 for dx = -1, 1 do
43 for dz = -1, 1 do
44 test.place(p, mid + vector.create(dx, 1, dz), "Wheat")
45 end
46 end
47 return p, mid
48end
49
50test.case("a hand digs one block of farmland, a copper hoe three by three", function()
51 local p = test.player("Ada", { survival = true, carrying = { ["Copper hoe"] = 1, ["Water bucket"] = 1 } })
52 local mid = patch()
53 test.hold(p, nil)
54 test.use_block(p, mid)
55 test.same(count(mid, 3, "Farmland"), 1, "one by hand")
56 world.set_block(mid, "Grass")
57 test.hold(p, "Copper hoe")
58 test.use_block(p, mid)
59 test.same(count(mid, 1, "Farmland"), 9, "the square")
60 test.same(count(mid, 3, "Farmland"), 9, "and no more")
61 -- what carries water doesn't dig
62 local other = mid + vector.create(3, 0, 3)
63 test.hold(p, "Water bucket")
64 test.use_block(p, other)
65 test.same(world.block(other), "Grass", "a water bucket leaves grass be")
66end)
67
68test.case("a watering can, filled at water, brings crops on", function()
69 local p, mid = sown()
70 test.same(count(mid + UP, 1, "Wheat shoots"), 9, "nine shoots")
71 test.hold(p, "Watering can")
72 test.use_block(p, mid + UP)
73 test.check(string.find(test.shown(p, "farming") or "", "Fill", 1, true) == 1, "an empty can says so")
74 local water = mid + vector.create(3, 1, 3)
75 world.set_block(water, "Water")
76 test.use_block(p, water)
77 test.same(test.shown(p, "farming"), "Your watering can is full.")
78 -- four minutes to ripen, and each watering takes one off
79 for _ = 1, 4 do
80 test.use_block(p, mid + UP)
81 end
82 test.check(string.find(test.shown(p, "farming") or "", "12 waterings left", 1, true) ~= nil, test.shown(p, "farming") or "")
83 test.wait(6)
84 test.same(count(mid + UP, 1, "Wheat"), 9, "all nine ripe")
85end)
86
87test.case("a sickle reaps the ripe crops around the one it cuts", function()
88 local p, mid = sown()
89 local water = mid + vector.create(3, 1, 3)
90 world.set_block(water, "Water")
91 test.hold(p, "Watering can")
92 test.use_block(p, water)
93 for _ = 1, 4 do
94 test.use_block(p, mid + UP)
95 end
96 test.wait(6)
97 test.hold(p, "Sickle")
98 test.dig(p, mid + UP)
99 test.same(count(mid + UP, 1, "Air"), 9, "all nine cut")
100 -- each gives itself and two more, and seeds to sow again
101 test.same(world.has(p, "Wheat"), 27, "wheat")
102 test.same(world.has(p, "Seeds"), 9, "seeds")
103end)
104
105test.case("seeds sow wheat, and potatoes, beans and pumpkins grow too", function()
106 local p = test.player("Ada", { survival = true, carrying = { Seeds = 2, Potatoes = 1, Beans = 1, Pumpkin = 1 } })
107 local mid = patch()
108 for dx = -2, 2 do
109 world.set_block(mid + vector.create(dx, 0, 0), "Farmland")
110 end
111 test.hold(p, "Seeds")
112 test.use_block(p, mid + vector.create(-2, 0, 0))
113 test.same(world.block(mid + vector.create(-2, 1, 0)), "Wheat shoots", "seeds on farmland")
114 test.same(world.has(p, "Seeds"), 1, "one seed sown")
115 test.place(p, mid + vector.create(-1, 1, 0), "Potatoes")
116 test.place(p, mid + vector.create(0, 1, 0), "Beans")
117 test.place(p, mid + vector.create(1, 1, 0), "Pumpkin")
118 test.same(world.block(mid + vector.create(-1, 1, 0)), "Potato shoots")
119 test.same(world.block(mid + vector.create(0, 1, 0)), "Bean shoots")
120 test.same(world.block(mid + vector.create(1, 1, 0)), "Pumpkin vine")
121 -- broken too soon, a shoot gives its seed back and nothing else
122 test.dig(p, mid + vector.create(-2, 1, 0))
123 test.same(world.has(p, "Seeds"), 2, "the seed back")
124 test.same(world.has(p, "Wheat shoots"), 0, "and no shoot")
125 test.wait(500)
126 test.same(world.block(mid + vector.create(-1, 1, 0)), "Potatoes", "potatoes ripen")
127 test.same(world.block(mid + vector.create(1, 1, 0)), "Pumpkin", "and a pumpkin")
128 test.dig(p, mid + vector.create(1, 1, 0))
129 test.same(world.has(p, "Pumpkin"), 3, "a pumpkin and two more")
130end)
131
132test.case("a berry bush gives berries, and they grow back", function()
133 local p = test.player("Ada", { survival = true })
134 local mid = patch()
135 local at = mid + UP
136 world.set_block(at, "Berry bush")
137 test.use_block(p, at)
138 local picked = world.has(p, "Berries")
139 test.check(picked >= 2 and picked <= 4, "two to four berries, got " .. picked)
140 test.same(world.block(at), "Bare berry bush", "picked bare")
141 test.use_block(p, at)
142 test.same(world.has(p, "Berries"), picked, "nothing more from a bare bush")
143 test.wait(190)
144 test.same(world.block(at), "Berry bush", "the berries are back")
145 -- dug up, a bush comes away bare, with its berries
146 test.dig(p, at)
147 test.same(world.has(p, "Bare berry bush"), 1, "a bare bush to plant again")
148 test.same(world.has(p, "Berries"), picked + 2, "and its berries")
149end)
150
151test.case("tall grass gives seeds now and then", function()
152 local p = test.player("Ada", { survival = true })
153 local mid = patch()
154 for dx = -3, 3 do
155 for dz = -3, 3 do
156 world.set_block(mid + vector.create(dx, 1, dz), "Tall grass")
157 end
158 end
159 for dx = -3, 3 do
160 for dz = -3, 3 do
161 test.dig(p, mid + vector.create(dx, 1, dz))
162 end
163 end
164 local n = world.has(p, "Seeds")
165 test.check(n > 5 and n < 45, "about half of 49 give a seed, got " .. n)
166end)
167
168-- two wheat shoots on farmland, one at each end of a patch, the second
169-- under a roof of `roof`
170local function two_shoots(roof: string): (vector, vector)
171 local p = test.player("Ada", { survival = true, carrying = { Wheat = 2 } })
172 local mid = patch()
173 local open, covered = mid + vector.create(-2, 0, 0), mid + vector.create(2, 0, 0)
174 for _, at in { open, covered } do
175 world.set_block(at, "Farmland")
176 test.place(p, at + UP, "Wheat")
177 end
178 world.set_block(covered + vector.create(0, 3, 0), roof)
179 return open + UP, covered + UP
180end
181
182test.case("winter stops crops out under the sky, and a greenhouse keeps them growing", function()
183 sky.set_season("winter", 0.5)
184 local open, covered = two_shoots("Glass")
185 test.same(world.block(open), "Wheat shoots")
186 test.wait(300)
187 test.same(world.block(open), "Wheat shoots", "out in the winter, it waits")
188 test.same(world.block(covered), "Wheat", "under glass, it ripens")
189 -- and when spring comes, the one outside grows again
190 sky.set_season("spring", 0)
191 test.wait(250)
192 test.same(world.block(open), "Wheat", "in spring it ripens")
193end)
194
195test.case("rain waters the crops it falls on", function()
196 sky.set_season("autumn", 0.5)
197 sky.set_weather("rain", { strength = 1, over = 0 })
198 local open, covered = two_shoots("Stone")
199 test.wait(200)
200 test.same(world.block(open), "Wheat", "the one in the rain's ripe")
201 test.same(world.block(covered), "Wheat shoots", "the one under the roof isn't yet")
202end)
203
204test.case("melons, sugar cane, cocoa, rice and beets grow and give more", function()
205 local names = { "Melon", "Sugar cane", "Cocoa", "Rice", "Beets" }
206 local shoots = { "Melon vine", "Sugar cane shoots", "Cocoa shoots", "Rice shoots", "Beet shoots" }
207 local p = test.player("Ada", { survival = true, carrying = { Melon = 1, ["Sugar cane"] = 1, Cocoa = 1, Rice = 1, Beets = 1 } })
208 local mid = patch()
209 for i, name in names do
210 local at = mid + vector.create(i - 3, 0, 0)
211 world.set_block(at, "Farmland")
212 test.place(p, at + UP, name)
213 test.same(world.block(at + UP), shoots[i], name .. " planted")
214 end
215 test.wait(500)
216 for i, name in names do
217 local at = mid + vector.create(i - 3, 1, 0)
218 test.same(world.block(at), name, name .. " ripens")
219 test.dig(p, at)
220 test.same(world.has(p, name), 3, name .. " and two more")
221 end
222end)