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. In survival, Interact on grass or dirt to dig it into farmland; |
| 3 | -- with a hoe in hand (world.held) it digs a square at once, wider the |
| 4 | -- better the hoe (HOES below). Put a crop on farmland and it's planted: |
| 5 | -- wheat, carrots, potatoes, beans, a pumpkin or a melon, sugar cane, |
| 6 | -- cocoa, rice or beets (CROPS below), or use seeds |
| 7 | -- on it for wheat. Each starts as a shoot and ripens after a few minutes |
| 8 | -- (faster by day and near water). A watering can (fill it at water) takes |
| 9 | -- time off the crops around the one you use it on, and a sickle harvests |
| 10 | -- the ripe ones around the one it cuts. |
| 11 | -- The seasons (@potluck/sky) count too: crops grow a quarter faster in |
| 12 | -- spring and summer, and stop in winter unless they're under a roof (glass |
| 13 | -- makes a greenhouse). Rain waters the crops it falls on. |
| 14 | -- Break a ripe crop for more than you planted; break a shoot and you get |
| 15 | -- your seed back. Planted crops are kept with the world (store), so a |
| 16 | -- server's fields keep growing after a restart. |
| 17 | -- |
| 18 | -- Out in the wild: tall grass gives seeds now and then, oak leaves an |
| 19 | -- apple, and wild potatoes, beans, pumpkins and berry bushes come up on |
| 20 | -- the grass around survival players. Use a berry bush to pick its berries; |
| 21 | -- they grow back. The crops, shoots and bushes are this creation's own |
| 22 | -- blocks, drawn in potluck.toml. |
| 23 | |
| 24 | local world = require("@potluck/world@0.22") |
| 25 | local tweak = require("@potluck/tweak") |
| 26 | local store = require("@potluck/store") |
| 27 | local ui = require("@potluck/ui@0.2") |
| 28 | local sound = require("@potluck/sound@0.1") |
| 29 | local sky = require("@potluck/sky@0.1") |
| 30 | |
| 31 | local knobs = tweak { |
| 32 | minutes = { 4, min = 1, max = 20, label = "Minutes to ripen", order = 1 }, |
| 33 | harvest = { 2, min = 0, max = 4, label = "Extra from a ripe crop", order = 2 }, |
| 34 | till = { true, label = "Interact on grass digs farmland", order = 3 }, |
| 35 | watering = { 1, min = 0, max = 4, label = "Minutes a watering saves", order = 4 }, |
| 36 | fills = { 16, min = 1, max = 64, label = "Waterings in a full can", order = 5 }, |
| 37 | wild = { 0.3, min = 0, max = 1, label = "How often wild crops come up", order = 6 }, |
| 38 | berries = { 3, min = 1, max = 20, label = "Minutes for berries to grow back", order = 7 }, |
| 39 | seeds = { 0.5, min = 0, max = 1, label = "Share of tall grass with seeds", order = 8 }, |
| 40 | apples = { 0.08, min = 0, max = 1, label = "Share of oak leaves with an apple", order = 9 }, |
| 41 | seasons = { true, label = "Seasons change how crops grow", order = 10 }, |
| 42 | } |
| 43 | |
| 44 | -- how fast crops out under the sky grow in each season; under a roof |
| 45 | -- they grow as in autumn all year |
| 46 | local SEASON_RATE: { [string]: number } = { spring = 1.25, summer = 1.25, autumn = 1, winter = 0 } |
| 47 | |
| 48 | -- Each crop: what it looks like while it grows, and what a shoot broken |
| 49 | -- too soon gives back. A remix can add its own (with blocks of its own). |
| 50 | type Kind = { shoot: string, seed: string } |
| 51 | local CROPS: { [string]: Kind } = { |
| 52 | Wheat = { shoot = "Wheat shoots", seed = "Seeds" }, |
| 53 | Carrots = { shoot = "Carrot shoots", seed = "Carrots" }, |
| 54 | Potatoes = { shoot = "Potato shoots", seed = "Potatoes" }, |
| 55 | Beans = { shoot = "Bean shoots", seed = "Beans" }, |
| 56 | Pumpkin = { shoot = "Pumpkin vine", seed = "Pumpkin" }, |
| 57 | Melon = { shoot = "Melon vine", seed = "Melon" }, |
| 58 | ["Sugar cane"] = { shoot = "Sugar cane shoots", seed = "Sugar cane" }, |
| 59 | Cocoa = { shoot = "Cocoa shoots", seed = "Cocoa" }, |
| 60 | Rice = { shoot = "Rice shoots", seed = "Rice" }, |
| 61 | Beets = { shoot = "Beet shoots", seed = "Beets" }, |
| 62 | } |
| 63 | -- what's planted by using it on farmland, rather than putting it down |
| 64 | local SOWN: { [string]: string } = { Seeds = "Wheat" } |
| 65 | -- what a ripe crop gives besides itself: wheat gives seeds to sow again |
| 66 | local ALSO: { [string]: string } = { Wheat = "Seeds" } |
| 67 | -- what comes up in the wild, each as likely as its count here |
| 68 | local WILD = { "Potatoes", "Beans", "Pumpkin", "Berry bush", "Berry bush", "Carrots", "Melon", "Beets", "Sugar cane" } |
| 69 | local BUSH, BARE = "Berry bush", "Bare berry bush" |
| 70 | |
| 71 | local SOIL = "Farmland" |
| 72 | local MOST = 400 -- crops kept at once |
| 73 | local MOST_WILD = 48 -- wild ones about at once |
| 74 | local EVERY = 5 -- seconds between growing |
| 75 | |
| 76 | type Crop = { crop: string, left: number, wet: boolean, ripe: boolean } |
| 77 | |
| 78 | local UP = vector.create(0, 1, 0) |
| 79 | |
| 80 | -- how far each hoe digs farmland around the block you use it on, each way: |
| 81 | -- a copper hoe 3 by 3, iron 5 by 5 and moonsilver 7 by 7 |
| 82 | local HOES: { [string]: number } = { ["Copper hoe"] = 1, ["Iron hoe"] = 2, ["Moonsilver hoe"] = 3 } |
| 83 | |
| 84 | -- what doesn't dig farmland when it's in hand: what carries water and |
| 85 | -- the rod (the water creation's) |
| 86 | local NOT_FOR_DIGGING: { [string]: boolean } = { Bucket = true, ["Water bucket"] = true, ["Watering can"] = true, ["Fishing rod"] = true } |
| 87 | local CAN, SICKLE = "Watering can", "Sickle" |
| 88 | |
| 89 | local function key(at: vector): string |
| 90 | return string.format("%d,%d,%d", math.floor(at.x), math.floor(at.y), math.floor(at.z)) |
| 91 | end |
| 92 | |
| 93 | local function spot(k: string): vector |
| 94 | local x, y, z = string.match(k, "^(-?%d+),(-?%d+),(-?%d+)$") |
| 95 | return vector.create(tonumber(x) :: number + 0.5, tonumber(y) :: number + 0.5, tonumber(z) :: number + 0.5) |
| 96 | end |
| 97 | |
| 98 | -- what each crop looks like as it grows, by its shoot's name |
| 99 | local function shoot_of(crop: string): string? |
| 100 | local c = CROPS[crop] |
| 101 | return c and c.shoot |
| 102 | end |
| 103 | |
| 104 | local crops: { [string]: Crop } = store.get("crops") or {} |
| 105 | local count = 0 |
| 106 | for _ in crops do |
| 107 | count += 1 |
| 108 | end |
| 109 | -- bushes picked bare, and the seconds till their berries are back |
| 110 | local bare: { [string]: number } = store.get("bare") or {} |
| 111 | -- wild crops and bushes about, by where |
| 112 | local wild: { [string]: string } = store.get("wild") or {} |
| 113 | local wilds = 0 |
| 114 | for _ in wild do |
| 115 | wilds += 1 |
| 116 | end |
| 117 | local changed = false |
| 118 | |
| 119 | local function forget(k: string) |
| 120 | if crops[k] then |
| 121 | crops[k] = nil |
| 122 | count -= 1 |
| 123 | changed = true |
| 124 | end |
| 125 | end |
| 126 | |
| 127 | -- waterings left in each player's can |
| 128 | local cans: { [number]: number } = {} |
| 129 | |
| 130 | -- one line above the hotbar for a few seconds, for each player |
| 131 | local shown: { [number]: number } = {} |
| 132 | local function tell(player, text: string) |
| 133 | ui.show(player, "farming", text, { at = "bottom" }) |
| 134 | local mine = (shown[player.id] or 0) + 1 |
| 135 | shown[player.id] = mine |
| 136 | world.after(3, function() |
| 137 | if shown[player.id] == mine then |
| 138 | shown[player.id] = nil |
| 139 | ui.hide(player, "farming") |
| 140 | end |
| 141 | end) |
| 142 | end |
| 143 | |
| 144 | -- the crops in the square around `at` (at the same height), and where |
| 145 | local function round(at: vector): { { k: string, at: vector } } |
| 146 | local out = {} |
| 147 | for dx = -1, 1 do |
| 148 | for dz = -1, 1 do |
| 149 | local q = at + vector.create(dx, 0, dz) |
| 150 | if crops[key(q)] then |
| 151 | table.insert(out, { k = key(q), at = q }) |
| 152 | end |
| 153 | end |
| 154 | end |
| 155 | return out |
| 156 | end |
| 157 | |
| 158 | -- digs one block into farmland, if it's grass or dirt open to the sky |
| 159 | local function till(at: vector): boolean |
| 160 | local b = world.block(at) |
| 161 | if (b == "Grass" or b == "Dirt") and world.block(at + UP) == "Air" then |
| 162 | world.set_block(at, SOIL) |
| 163 | return true |
| 164 | end |
| 165 | return false |
| 166 | end |
| 167 | |
| 168 | -- starts a crop growing at `at` (on farmland), as its shoot |
| 169 | local function plant(at: vector, crop: string): boolean |
| 170 | local shoot = shoot_of(crop) |
| 171 | if not shoot or world.block(at - UP) ~= SOIL or count >= MOST then |
| 172 | return false |
| 173 | end |
| 174 | world.set_block(at, shoot) |
| 175 | crops[key(at)] = { crop = crop, left = knobs.minutes * 60, wet = world.find_block(at, 4, "Water") ~= nil, ripe = false } |
| 176 | count += 1 |
| 177 | changed = true |
| 178 | sound.play("rustle", { at = at, volume = 0.5, caption = "Planted" }) |
| 179 | return true |
| 180 | end |
| 181 | |
| 182 | -- watering: fills at water, and takes time off the crops around the one |
| 183 | -- (or the farmland) it's used on |
| 184 | local function water(player, at: vector, block: string) |
| 185 | if block == "Water" then |
| 186 | cans[player.id] = math.floor(knobs.fills) |
| 187 | sound.play("splash", { at = at, volume = 0.5, caption = "Filling a watering can" }) |
| 188 | tell(player, "Your watering can is full.") |
| 189 | return |
| 190 | end |
| 191 | local over = if block == SOIL then at + UP else at |
| 192 | local near = round(over) |
| 193 | if #near == 0 then |
| 194 | return |
| 195 | end |
| 196 | local left = cans[player.id] or 0 |
| 197 | if left <= 0 then |
| 198 | tell(player, "Fill your watering can at water first.") |
| 199 | return |
| 200 | end |
| 201 | cans[player.id] = left - 1 |
| 202 | for _, n in near do |
| 203 | local c = crops[n.k] |
| 204 | if not c.ripe then |
| 205 | c.left -= knobs.watering * 60 |
| 206 | c.wet = true |
| 207 | changed = true |
| 208 | end |
| 209 | end |
| 210 | sound.play("splash", { at = over, volume = 0.4, caption = "Watering" }) |
| 211 | tell(player, if left > 1 then "Watered. " .. (left - 1) .. " waterings left." else "Watered. Your can is empty.") |
| 212 | end |
| 213 | |
| 214 | -- picking a bush's berries, which grow back |
| 215 | local function pick(player, at: vector) |
| 216 | if world.pockets(player) ~= nil and world.give(player, "Berries", math.floor(world.random(2, 4))) == 0 then |
| 217 | tell(player, "Your pockets are full.") |
| 218 | return |
| 219 | end |
| 220 | world.set_block(at, BARE) |
| 221 | bare[key(at)] = knobs.berries * 60 |
| 222 | changed = true |
| 223 | sound.play("rustle", { at = at, volume = 0.6, caption = "Picking berries" }) |
| 224 | end |
| 225 | |
| 226 | -- digging farmland, sowing seeds and picking berries |
| 227 | world.on("block_used", function(player, at: vector, block: string) |
| 228 | if block == BUSH then |
| 229 | pick(player, at) |
| 230 | return |
| 231 | elseif block == BARE then |
| 232 | tell(player, "Its berries haven't grown back yet.") |
| 233 | return |
| 234 | end |
| 235 | if world.pockets(player) == nil then |
| 236 | return |
| 237 | end |
| 238 | local held = world.held(player) |
| 239 | if held == CAN then |
| 240 | water(player, at, block) |
| 241 | return |
| 242 | end |
| 243 | local sown = held and SOWN[held] |
| 244 | if sown then |
| 245 | -- seeds go in farmland, or in the farmland under a crop's spot |
| 246 | local soil = if block == SOIL then at else at - UP |
| 247 | if world.block(soil) == SOIL and world.block(soil + UP) == "Air" and count < MOST and world.take(player, held :: string, 1) then |
| 248 | plant(soil + UP, sown) |
| 249 | end |
| 250 | return |
| 251 | end |
| 252 | if not knobs.till or (held and NOT_FOR_DIGGING[held]) or not till(at) then |
| 253 | return |
| 254 | end |
| 255 | local reach = held and HOES[held] or 0 |
| 256 | for dx = -reach, reach do |
| 257 | for dz = -reach, reach do |
| 258 | if dx ~= 0 or dz ~= 0 then |
| 259 | till(at + vector.create(dx, 0, dz)) |
| 260 | end |
| 261 | end |
| 262 | end |
| 263 | sound.play("dig", { at = at, volume = 0.6, caption = "Digging" }) |
| 264 | end) |
| 265 | |
| 266 | -- planting, and bare bushes put down again |
| 267 | world.on("block_placed", function(_player, at: vector, block: string) |
| 268 | if block == BARE then |
| 269 | bare[key(at)] = knobs.berries * 60 |
| 270 | changed = true |
| 271 | return |
| 272 | end |
| 273 | plant(at, block) |
| 274 | end) |
| 275 | |
| 276 | -- harvesting, and what the wild gives |
| 277 | world.on("block_broken", function(player, at: vector, block: string) |
| 278 | local k = key(at) |
| 279 | local survival = world.pockets(player) ~= nil |
| 280 | if block == SOIL then |
| 281 | -- the crop on it goes with it |
| 282 | forget(key(at + UP)) |
| 283 | return |
| 284 | elseif block == "Tall grass" then |
| 285 | if survival and world.random() < knobs.seeds then |
| 286 | world.give(player, "Seeds", 1) |
| 287 | end |
| 288 | return |
| 289 | elseif block == "Oak leaves" then |
| 290 | if survival and world.random() < knobs.apples then |
| 291 | world.give(player, "Apple", 1) |
| 292 | end |
| 293 | return |
| 294 | elseif block == BUSH then |
| 295 | -- it comes away bare (world.drops below), and its berries with it |
| 296 | if survival then |
| 297 | world.give(player, "Berries", 2) |
| 298 | end |
| 299 | bare[k] = nil |
| 300 | return |
| 301 | elseif block == BARE then |
| 302 | bare[k] = nil |
| 303 | changed = true |
| 304 | return |
| 305 | end |
| 306 | local c = crops[k] |
| 307 | if not c then |
| 308 | return |
| 309 | end |
| 310 | forget(k) |
| 311 | local kind = CROPS[c.crop] |
| 312 | if c.ripe and block == c.crop then |
| 313 | -- a little XP for each ripe crop (world.reward) |
| 314 | local reaped = 1 |
| 315 | if knobs.harvest > 0 then |
| 316 | world.give(player, c.crop, knobs.harvest) |
| 317 | end |
| 318 | if ALSO[c.crop] then |
| 319 | world.give(player, ALSO[c.crop], 1) |
| 320 | end |
| 321 | -- a sickle reaps the ripe ones around it too |
| 322 | if world.held(player) == SICKLE then |
| 323 | for _, n in round(at) do |
| 324 | local o = crops[n.k] |
| 325 | if o.ripe and world.block(n.at) == o.crop then |
| 326 | forget(n.k) |
| 327 | world.set_block(n.at, "Air") |
| 328 | world.give(player, o.crop, 1 + knobs.harvest) |
| 329 | if ALSO[o.crop] then |
| 330 | world.give(player, ALSO[o.crop], 1) |
| 331 | end |
| 332 | reaped += 1 |
| 333 | end |
| 334 | end |
| 335 | end |
| 336 | world.reward(player, reaped) |
| 337 | sound.play("pop", { at = at, caption = "Harvest" }) |
| 338 | elseif not c.ripe and kind and block == kind.shoot then |
| 339 | -- too soon: the seed back |
| 340 | world.give(player, kind.seed, 1) |
| 341 | end |
| 342 | end) |
| 343 | |
| 344 | -- a shoot broken gives nothing of itself (block_broken gives the seed |
| 345 | -- back), and a bush with berries comes away bare |
| 346 | for _, kind in CROPS do |
| 347 | world.drops(kind.shoot, { gives = false }) |
| 348 | end |
| 349 | world.drops(BUSH, { gives = BARE }) |
| 350 | |
| 351 | -- whether there's anything over `at` but leaves: a roof keeps the rain |
| 352 | -- off, and a greenhouse the winter |
| 353 | local function under_sky(at: vector): boolean |
| 354 | return world.top(at.x, at.z) <= math.floor(at.y) |
| 355 | end |
| 356 | |
| 357 | -- Whether a block of another creation (the Power creation's, which may not |
| 358 | -- be here) is within reach. |
| 359 | local function near(at: vector, radius: number, name: string): boolean |
| 360 | local ok, found = pcall(world.find_block, at, radius, name) |
| 361 | return ok and found ~= nil |
| 362 | end |
| 363 | |
| 364 | -- growing: faster by day, near water and in spring and summer; and bushes |
| 365 | -- growing berries again |
| 366 | world.every(EVERY, function() |
| 367 | local rate = (0.5 + 0.5 * world.daylight()) * EVERY |
| 368 | local season = if knobs.seasons then (sky.season()) else "autumn" |
| 369 | local w = sky.weather() |
| 370 | local raining = w.kind == "rain" or w.kind == "storm" |
| 371 | for k, c in crops do |
| 372 | local at = spot(k) |
| 373 | local now = world.block(at) |
| 374 | if c.ripe then |
| 375 | if now ~= c.crop then |
| 376 | forget(k) |
| 377 | end |
| 378 | elseif now ~= shoot_of(c.crop) then |
| 379 | forget(k) |
| 380 | else |
| 381 | local open = under_sky(at) |
| 382 | if open and raining and not c.wet then |
| 383 | c.wet = true |
| 384 | end |
| 385 | -- a sprinkler that's running wets what's round it |
| 386 | if not c.wet and near(at, 4, "Lit sprinkler") then |
| 387 | c.wet = true |
| 388 | end |
| 389 | local by_season = if open then SEASON_RATE[season] or 1 else 1 |
| 390 | -- a lit grow lamp near the bed is as good as noon |
| 391 | local r = rate |
| 392 | if rate < EVERY and (near(at, 5, "Lit grow lamp") or near(at, 9, "Lit greenhouse array")) then |
| 393 | r = EVERY |
| 394 | end |
| 395 | c.left -= (if c.wet then r * 1.5 else r) * by_season |
| 396 | changed = true |
| 397 | if c.left <= 0 then |
| 398 | c.ripe = true |
| 399 | world.set_block(at, c.crop) |
| 400 | end |
| 401 | end |
| 402 | end |
| 403 | for k, left in bare do |
| 404 | local at = spot(k) |
| 405 | if world.block(at) ~= BARE then |
| 406 | bare[k] = nil |
| 407 | elseif season == "winter" and under_sky(at) then |
| 408 | -- no berries out in the winter |
| 409 | continue |
| 410 | elseif left <= EVERY then |
| 411 | bare[k] = nil |
| 412 | world.set_block(at, BUSH) |
| 413 | else |
| 414 | bare[k] = left - EVERY |
| 415 | end |
| 416 | changed = true |
| 417 | end |
| 418 | if changed then |
| 419 | store.set("crops", crops) |
| 420 | store.set("bare", bare) |
| 421 | changed = false |
| 422 | end |
| 423 | end) |
| 424 | |
| 425 | -- wild crops and bushes coming up on the grass around survival players, |
| 426 | -- out of sight (12 to 32 blocks off), a few dozen at most |
| 427 | world.every(20, function() |
| 428 | -- forget the ones that have gone (picked, or dug up) |
| 429 | for k, name in wild do |
| 430 | local now = world.block(spot(k)) |
| 431 | if now ~= name and now ~= BARE then |
| 432 | wild[k] = nil |
| 433 | wilds -= 1 |
| 434 | end |
| 435 | end |
| 436 | -- nothing comes up in winter |
| 437 | if knobs.seasons and (sky.season()) == "winter" then |
| 438 | store.set("wild", wild) |
| 439 | return |
| 440 | end |
| 441 | for _, p in world.players() do |
| 442 | if wilds >= MOST_WILD or world.pockets(p) == nil or world.random() >= knobs.wild then |
| 443 | continue |
| 444 | end |
| 445 | local turn = world.random() * 2 * math.pi |
| 446 | local far = world.random(12, 32) |
| 447 | local x, z = p.position.x + math.cos(turn) * far, p.position.z + math.sin(turn) * far |
| 448 | local ground = vector.create(x, world.top(x, z), z) |
| 449 | if world.block(ground) == "Grass" and world.block(ground + UP) == "Air" then |
| 450 | local name = WILD[math.floor(world.random(1, #WILD + 1))] or "Berry bush" |
| 451 | world.set_block(ground + UP, name) |
| 452 | wild[key(ground + UP)] = name |
| 453 | wilds += 1 |
| 454 | end |
| 455 | end |
| 456 | store.set("wild", wild) |
| 457 | end) |
| 458 | |
| 459 | world.on("player_left", function(p) |
| 460 | cans[p.id] = nil |
| 461 | shown[p.id] = nil |
| 462 | end) |