diff --git a/docs/modding/blocks/crops.md b/docs/modding/blocks/crops.md index 95c0cd6..95c2fdd 100644 --- a/docs/modding/blocks/crops.md +++ b/docs/modding/blocks/crops.md @@ -4,8 +4,10 @@ A crop is a block with an `age` state, the base game's `bworld:crop` [component] places it. Adding one takes no code. Crops grow on Stardew Valley's clock with Minecraft's rules: every morning at 06:00, a crop whose farmland was watered -the day before grows a day, then all farmland dries out. The crop shows the stage for how far along it is. -`mods/farmersdelight` adds its cabbages, onions, tomatoes and rice this way. +the day before grows a day, then all farmland dries out. The crop shows the stage for how far along it is. A crop with +`seasons` dies the first morning that's in another season, turning into a `bworld:dead_bush`, so a summer crop has to be +harvested before autumn comes. The season is its biome's, so a crop that only grows in summer is always out of season in +a snowy biome. `mods/farmersdelight` adds its cabbages, onions, tomatoes and rice this way. ## A Basic Crop @@ -96,15 +98,16 @@ and 1 to 4 seeds, like Minecraft's wheat: ## Parameters -| Param | Meaning | -| ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `days` | Watered mornings from planting to fully grown. | -| `stages` | How many ages it shows, 0 to `stages - 1`, spread evenly over `days`. The `age` state must fit them. | -| `fertilizer` | An item that grows it a day right away when used on it, like bone meal. | -| `water` | `"farmland"` (the default) needs watered farmland under it. `"always"` grows every morning. | -| `regrow_days`, `harvest` | Right clicking it once grown picks it, dropping what the [loot table](../resources/loot_tables.md) `harvest` gives, and it grows back after `regrow_days`. | -| `upper` | `{ block, stages, supporting_age }`: its last stages grow a second block on top, see below. | -| `leaves` | What the spot becomes when it's broken, like the water rice was planted in. | +| Param | Meaning | +| ------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `days` | Watered mornings from planting to fully grown. | +| `stages` | How many ages it shows, 0 to `stages - 1`, spread evenly over `days`. The `age` state must fit them. | +| `fertilizer` | An item that grows it a day right away when used on it, like bone meal. | +| `water` | `"farmland"` (the default) needs watered farmland under it. `"always"` grows every morning. | +| `regrow_days`, `harvest` | Right clicking it once grown picks it, dropping what the [loot table](../resources/loot_tables.md) `harvest` gives, and it grows back after `regrow_days`. | +| `upper` | `{ block, stages, supporting_age }`: its last stages grow a second block on top, see below. | +| `leaves` | What the spot becomes when it's broken, like the water rice was planted in. | +| `seasons` | The [seasons](../worldgen/biomes.md#seasons) it grows in, like `["spring", "summer"]`. A morning in any other season turns it into a dead bush. Every season when not set. | ## Harvesting By Hand diff --git a/mods/bworld/blocks/carrots.json b/mods/bworld/blocks/carrots.json index cfe9372..7da235e 100644 --- a/mods/bworld/blocks/carrots.json +++ b/mods/bworld/blocks/carrots.json @@ -49,7 +49,11 @@ "bworld:crop": { "days": 3, "stages": 4, - "fertilizer": "bworld:bone_meal" + "fertilizer": "bworld:bone_meal", + "seasons": [ + "spring", + "autumn" + ] } } } diff --git a/mods/bworld/blocks/potatoes.json b/mods/bworld/blocks/potatoes.json index 0b6d11c..9b44a03 100644 --- a/mods/bworld/blocks/potatoes.json +++ b/mods/bworld/blocks/potatoes.json @@ -49,7 +49,11 @@ "bworld:crop": { "days": 6, "stages": 4, - "fertilizer": "bworld:bone_meal" + "fertilizer": "bworld:bone_meal", + "seasons": [ + "spring", + "summer" + ] } } } diff --git a/mods/bworld/blocks/wheat.json b/mods/bworld/blocks/wheat.json index 2547d3f..d0d15b9 100644 --- a/mods/bworld/blocks/wheat.json +++ b/mods/bworld/blocks/wheat.json @@ -69,7 +69,13 @@ "bworld:crop": { "days": 7, "stages": 8, - "fertilizer": "bworld:bone_meal" + "fertilizer": "bworld:bone_meal", + "seasons": [ + "spring", + "summer", + "autumn", + "winter" + ] } } } diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index e1a0d83..780df50 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -1,5 +1,5 @@ // the base game's block and item behavior, written against the same api as any other mod -import type { BlockRef, Container, EntityRef, Player, ServerContext } from "bworld/server"; +import type { BlockRef, Container, EntityRef, Player, Season, ServerContext } from "bworld/server"; import type { Position } from "bworld/shared"; import { register_curios } from "./curios.ts"; import { register_effects } from "./effects.ts"; @@ -331,6 +331,7 @@ function register_smithing_table(ctx: ServerContext) { // it's dug up const FARMLAND = "bworld:hoed_dirt"; +const DEAD_BUSH = "bworld:dead_bush"; // every tier of watering can is a tool of this type, and shares the bworld:watering_can component const WATERING_CAN = "watering_can"; @@ -359,6 +360,8 @@ interface CropParams { upper?: { block: string; stages: number; supporting_age: number }; // what the spot becomes when it's broken, like water where rice was leaves?: string; + // the seasons it grows in. a morning in any other season turns it into a dead bush. every season when not set + seasons?: Season[]; } // the top of a two block crop. it grows with the crop under it, and breaking it breaks the whole plant @@ -414,6 +417,15 @@ function register_farming(ctx: ServerContext) { }, }); + // out of season it dies, the top of a two block crop going with it without dropping anything + const die = (block: BlockRef, params: CropParams) => { + const { x, y, z } = block; + if (params.upper && ctx.world.get_block(x, y + 1, z) === params.upper.block) { + ctx.world.set_block(x, y + 1, z, "bworld:air"); + } + ctx.world.set_block(x, y, z, DEAD_BUSH); + }; + // the stage for how many days it has grown: evenly spread, fully grown on the last day const stage_for = (params: CropParams, days: number) => { const stages = params.stages + (params.upper?.stages ?? 0); @@ -457,6 +469,10 @@ function register_farming(ctx: ServerContext) { block.data = { days: days_of(block, params) }; }, on_new_day(block, params) { + if (params.seasons && !params.seasons.includes(block.season)) { + die(block, params); + return; + } const days = days_of(block, params); if (days >= params.days) return; const watered = params.water === "always" || is_watered(block.x, block.y - 1, block.z); diff --git a/mods/farmersdelight/blocks/cabbages.json b/mods/farmersdelight/blocks/cabbages.json index 57e8b56..427ca80 100644 --- a/mods/farmersdelight/blocks/cabbages.json +++ b/mods/farmersdelight/blocks/cabbages.json @@ -69,7 +69,11 @@ "bworld:crop": { "days": 12, "stages": 8, - "fertilizer": "bworld:bone_meal" + "fertilizer": "bworld:bone_meal", + "seasons": [ + "spring", + "autumn" + ] } } } diff --git a/mods/farmersdelight/blocks/onions.json b/mods/farmersdelight/blocks/onions.json index 3404821..0e41578 100644 --- a/mods/farmersdelight/blocks/onions.json +++ b/mods/farmersdelight/blocks/onions.json @@ -49,7 +49,11 @@ "bworld:crop": { "days": 4, "stages": 4, - "fertilizer": "bworld:bone_meal" + "fertilizer": "bworld:bone_meal", + "seasons": [ + "spring", + "summer" + ] } } } diff --git a/mods/farmersdelight/blocks/rice.json b/mods/farmersdelight/blocks/rice.json index b7ade91..b5f6985 100644 --- a/mods/farmersdelight/blocks/rice.json +++ b/mods/farmersdelight/blocks/rice.json @@ -68,7 +68,12 @@ "stages": 4, "supporting_age": 4 }, - "leaves": "bworld:water" + "leaves": "bworld:water", + "seasons": [ + "spring", + "summer", + "autumn" + ] } } } diff --git a/mods/farmersdelight/blocks/tomatoes.json b/mods/farmersdelight/blocks/tomatoes.json index bf0f5a0..3d7722c 100644 --- a/mods/farmersdelight/blocks/tomatoes.json +++ b/mods/farmersdelight/blocks/tomatoes.json @@ -71,7 +71,10 @@ "stages": 8, "fertilizer": "bworld:bone_meal", "regrow_days": 4, - "harvest": "farmersdelight:harvest/tomatoes" + "harvest": "farmersdelight:harvest/tomatoes", + "seasons": [ + "summer" + ] } } } diff --git a/tests/farming_test.ts b/tests/farming_test.ts index b99eb1f..78b5736 100644 --- a/tests/farming_test.ts +++ b/tests/farming_test.ts @@ -188,6 +188,9 @@ Deno.test("potatoes and carrots are planted on farmland, and eaten anywhere else Deno.test("tomatoes take 11 days, are picked by hand, and grow back in 4", async () => { const t = await setup(); + // they only grow in summer, which starts on day 50 + t.game.set_time(49 * CYCLE_TICKS); + t.game.tick(); const ages = grow(t, 1, "farmersdelight:tomatoes", 11); assertEquals(ages.at(-1), 7); assertEquals(ages.at(-2), 6); @@ -240,3 +243,35 @@ Deno.test("rice is planted in water, grows panicles on top without watering, and assertEquals(t.game.world.get_block_id(1, 100, 0), "bworld:water"); assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:air"); }); + +Deno.test("a crop dies into a dead bush the first morning out of its seasons", async () => { + const t = await setup(); + // carrots grow in spring and autumn. the last morning of spring they grow + t.game.set_time(47 * CYCLE_TICKS); + t.game.tick(); + assertEquals(grow(t, 0, "bworld:carrots", 1), [1]); + // and the first of summer they die, dropping nothing + t.game.set_block_state(0, 100, 0, 1); + t.next_morning(); + assertEquals(t.game.world.day, 49); + assertEquals(t.game.world.get_block_id(0, 101, 0), "bworld:dead_bush"); + assertEquals(t.game.item_entities(), []); + // still standing on its farmland + assertEquals(t.game.world.get_block_id(0, 100, 0), "bworld:hoed_dirt"); +}); + +Deno.test("a two block crop dies whole, its top going without dropping anything", async () => { + const t = await setup(); + t.game.set_block(1, 99, 0, "bworld:dirt"); + t.game.set_block(1, 100, 0, "farmersdelight:rice"); + for (let day = 0; day < 6; day++) t.next_morning(); + assertEquals(t.game.world.get_block_id(1, 101, 0), "farmersdelight:rice_panicles"); + + // rice grows in autumn, dies in winter, winter starts on day 148 + t.game.set_time(147 * CYCLE_TICKS - 1); + t.game.tick(); + t.game.tick(); + assertEquals(t.game.world.get_block_id(1, 100, 0), "bworld:dead_bush"); + assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:air"); + assertEquals(t.game.item_entities(), []); +});