-
Notifications
You must be signed in to change notification settings - Fork 6
feat(block): make furnaces smelt, burn fuel and show progress #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xRookieFight
wants to merge
2
commits into
dev
Choose a base branch
from
feat/furnace
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| module block | ||
|
|
||
| import server.world | ||
|
|
||
| // FurnaceVariant is which of the three cooking blocks a furnace is. They | ||
| // differ only in how fast they cook and in what they will accept, which the | ||
| // session decides from here rather than from the block name. | ||
| pub enum FurnaceVariant { | ||
| furnace | ||
| blast_furnace | ||
| smoker | ||
| } | ||
|
|
||
| // furnace_cook_ticks is how long this variant takes to cook one item. | ||
| pub fn (v FurnaceVariant) cook_ticks() int { | ||
| return if v == .furnace { furnace_cook_ticks } else { furnace_cook_ticks / 2 } | ||
| } | ||
|
|
||
| pub const furnace_cook_ticks = 200 | ||
|
|
||
| // furnace_slot_input, _fuel and _output are the three slots a furnace shows, | ||
| // in the order the client addresses them. | ||
| pub const furnace_slot_input = 0 | ||
| pub const furnace_slot_fuel = 1 | ||
| pub const furnace_slot_output = 2 | ||
| pub const furnace_slot_count = 3 | ||
|
|
||
| const furnace_names = { | ||
| 'minecraft:furnace': FurnaceVariant.furnace | ||
| 'minecraft:blast_furnace': FurnaceVariant.blast_furnace | ||
| 'minecraft:smoker': FurnaceVariant.smoker | ||
| } | ||
|
|
||
| // furnace_variant returns which cooking block an identifier names, lit or not. | ||
| pub fn furnace_variant(identifier string) ?FurnaceVariant { | ||
| name := identifier.replace('minecraft:lit_', 'minecraft:') | ||
| return furnace_names[name] or { return none } | ||
| } | ||
|
|
||
| // furnace_lit_ids and furnace_unlit_ids map a furnace between its two block | ||
| // states without the caller having to know which way it faces. They are built | ||
| // once from the same names and directions the container blocks are registered | ||
| // with, so the two can never drift apart. | ||
| pub const furnace_lit_ids = build_furnace_ids(false) | ||
| pub const furnace_unlit_ids = build_furnace_ids(true) | ||
|
|
||
| fn build_furnace_ids(from_lit bool) map[int]int { | ||
| mut out := map[int]int{} | ||
| for name, _ in furnace_names { | ||
| lit_name := name.replace('minecraft:', 'minecraft:lit_') | ||
| for direction in cardinal_directions { | ||
| unlit := furnace_state_id(name, direction) | ||
| lit := furnace_state_id(lit_name, direction) | ||
| if from_lit { | ||
| out[lit] = unlit | ||
| } else { | ||
| out[unlit] = lit | ||
| } | ||
| } | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| fn furnace_state_id(name string, direction string) int { | ||
| return world.new_block_with_states(name, [ | ||
| world.BlockState{ | ||
| key: 'minecraft:cardinal_direction' | ||
| kind: world.state_kind_string | ||
| string_val: direction | ||
| }, | ||
| ]).network_id | ||
| } | ||
|
|
||
| // lit_furnace_id is the burning form of a furnace block, keeping the way it | ||
| // faces. It returns none for anything that is not an unlit furnace. | ||
| pub fn lit_furnace_id(block_id int) ?int { | ||
| return furnace_lit_ids[block_id] or { return none } | ||
| } | ||
|
|
||
| // unlit_furnace_id is the reverse. | ||
| pub fn unlit_furnace_id(block_id int) ?int { | ||
| return furnace_unlit_ids[block_id] or { return none } | ||
| } | ||
|
|
||
| // is_lit_furnace reports whether a furnace block is currently burning. | ||
| pub fn is_lit_furnace(block_id int) bool { | ||
| return block_id in furnace_unlit_ids | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| module block | ||
|
|
||
| fn test_every_cooking_block_is_a_furnace() { | ||
| assert furnace_variant('minecraft:furnace')? == .furnace | ||
| assert furnace_variant('minecraft:blast_furnace')? == .blast_furnace | ||
| assert furnace_variant('minecraft:smoker')? == .smoker | ||
| // The burning form is the same block as far as behaviour goes. | ||
| assert furnace_variant('minecraft:lit_furnace')? == .furnace | ||
| assert furnace_variant('minecraft:lit_smoker')? == .smoker | ||
| if _ := furnace_variant('minecraft:chest') { | ||
| assert false, 'a chest reported itself as a furnace' | ||
| } | ||
| } | ||
|
|
||
| fn test_the_specialised_blocks_cook_twice_as_fast() { | ||
| assert FurnaceVariant.furnace.cook_ticks() == furnace_cook_ticks | ||
| assert FurnaceVariant.blast_furnace.cook_ticks() == furnace_cook_ticks / 2 | ||
| assert FurnaceVariant.smoker.cook_ticks() == furnace_cook_ticks / 2 | ||
| } | ||
|
|
||
| fn test_lighting_a_furnace_keeps_the_way_it_faces() { | ||
| for direction in cardinal_directions { | ||
| unlit := furnace_state_id('minecraft:furnace', direction) | ||
| lit := furnace_state_id('minecraft:lit_furnace', direction) | ||
| assert lit_furnace_id(unlit)? == lit | ||
| assert unlit_furnace_id(lit)? == unlit | ||
| assert is_lit_furnace(lit) | ||
| assert !is_lit_furnace(unlit) | ||
| } | ||
| } | ||
|
|
||
| fn test_a_block_that_is_not_a_furnace_has_no_lit_form() { | ||
| if _ := lit_furnace_id(0) { | ||
| assert false, 'block 0 reported a lit form' | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| module item | ||
|
|
||
| // smelt_ticks is how long a furnace takes to cook one item. A blast furnace or | ||
| // a smoker halves it, which is what furnace_speed is for. | ||
| pub const smelt_ticks = 200 | ||
|
|
||
| // SmeltingRecipe is one input turning into one output, and what cooking it is | ||
| // worth once the result is taken out. | ||
| pub struct SmeltingRecipe { | ||
| pub: | ||
| input string | ||
| output string | ||
| experience f32 | ||
| } | ||
|
|
||
| // smelting_recipe returns what an item smelts into, or none when it does not | ||
| // smelt at all. | ||
| pub fn smelting_recipe(input string) ?SmeltingRecipe { | ||
| result, experience := smelting_result(input) or { return none } | ||
| return SmeltingRecipe{ | ||
| input: input | ||
| output: result | ||
| experience: experience | ||
| } | ||
| } | ||
|
|
||
| fn smelting_result(input string) ?(string, f32) { | ||
| return match input { | ||
| 'minecraft:raw_iron', 'minecraft:iron_ore', 'minecraft:deepslate_iron_ore' { | ||
| 'minecraft:iron_ingot', f32(0.7) | ||
| } | ||
| 'minecraft:raw_gold', 'minecraft:gold_ore', 'minecraft:deepslate_gold_ore' { | ||
| 'minecraft:gold_ingot', f32(1.0) | ||
| } | ||
| 'minecraft:raw_copper', 'minecraft:copper_ore', 'minecraft:deepslate_copper_ore' { | ||
| 'minecraft:copper_ingot', f32(0.7) | ||
| } | ||
| 'minecraft:ancient_debris' { | ||
| 'minecraft:netherite_scrap', f32(2.0) | ||
| } | ||
| 'minecraft:coal_ore', 'minecraft:deepslate_coal_ore' { | ||
| 'minecraft:coal', f32(0.1) | ||
| } | ||
| 'minecraft:diamond_ore', 'minecraft:deepslate_diamond_ore' { | ||
| 'minecraft:diamond', f32(1.0) | ||
| } | ||
| 'minecraft:emerald_ore', 'minecraft:deepslate_emerald_ore' { | ||
| 'minecraft:emerald', f32(1.0) | ||
| } | ||
| 'minecraft:lapis_ore', 'minecraft:deepslate_lapis_ore' { | ||
| 'minecraft:lapis_lazuli', f32(0.2) | ||
| } | ||
| 'minecraft:redstone_ore', 'minecraft:deepslate_redstone_ore' { | ||
| 'minecraft:redstone', f32(0.7) | ||
| } | ||
| 'minecraft:nether_quartz_ore' { | ||
| 'minecraft:quartz', f32(0.2) | ||
| } | ||
| 'minecraft:sand', 'minecraft:red_sand' { | ||
| 'minecraft:glass', f32(0.1) | ||
| } | ||
| 'minecraft:cobblestone' { | ||
| 'minecraft:stone', f32(0.1) | ||
| } | ||
| 'minecraft:stone' { | ||
| 'minecraft:smooth_stone', f32(0.1) | ||
| } | ||
| 'minecraft:clay_ball' { | ||
| 'minecraft:brick', f32(0.3) | ||
| } | ||
| 'minecraft:netherrack' { | ||
| 'minecraft:netherbrick', f32(0.1) | ||
| } | ||
| 'minecraft:cactus' { | ||
| 'minecraft:green_dye', f32(1.0) | ||
| } | ||
| 'minecraft:kelp' { | ||
| 'minecraft:dried_kelp', f32(0.1) | ||
| } | ||
| 'minecraft:porkchop' { | ||
| 'minecraft:cooked_porkchop', f32(0.35) | ||
| } | ||
| 'minecraft:beef' { | ||
| 'minecraft:cooked_beef', f32(0.35) | ||
| } | ||
| 'minecraft:chicken' { | ||
| 'minecraft:cooked_chicken', f32(0.35) | ||
| } | ||
| 'minecraft:mutton' { | ||
| 'minecraft:cooked_mutton', f32(0.35) | ||
| } | ||
| 'minecraft:rabbit' { | ||
| 'minecraft:cooked_rabbit', f32(0.35) | ||
| } | ||
| 'minecraft:cod' { | ||
| 'minecraft:cooked_cod', f32(0.35) | ||
| } | ||
| 'minecraft:salmon' { | ||
| 'minecraft:cooked_salmon', f32(0.35) | ||
| } | ||
| 'minecraft:potato' { | ||
| 'minecraft:baked_potato', f32(0.35) | ||
| } | ||
| else { | ||
| none | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // fuel_burn_ticks is how long an item keeps a furnace lit, or none when it | ||
| // does not burn. | ||
| pub fn fuel_burn_ticks(name string) ?int { | ||
| if name.ends_with('_planks') || name.ends_with('_log') || name.ends_with('_wood') | ||
| || name.ends_with('_slab') || name.ends_with('_sapling') { | ||
| return 300 | ||
| } | ||
| return match name { | ||
| 'minecraft:lava_bucket' { 20000 } | ||
| 'minecraft:coal_block' { 16000 } | ||
| 'minecraft:blaze_rod' { 2400 } | ||
| 'minecraft:coal', 'minecraft:charcoal' { 1600 } | ||
| 'minecraft:dried_kelp_block' { 4000 } | ||
| 'minecraft:stick' { 100 } | ||
| else { none } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| module item | ||
|
|
||
| fn test_ores_smelt_into_ingots() { | ||
| recipe := smelting_recipe('minecraft:raw_iron') or { | ||
| assert false, 'raw iron does not smelt' | ||
| return | ||
| } | ||
| assert recipe.output == 'minecraft:iron_ingot' | ||
| assert recipe.experience > 0 | ||
| } | ||
|
|
||
| fn test_food_cooks() { | ||
| for raw, cooked in { | ||
| 'minecraft:porkchop': 'minecraft:cooked_porkchop' | ||
| 'minecraft:beef': 'minecraft:cooked_beef' | ||
| 'minecraft:potato': 'minecraft:baked_potato' | ||
| } { | ||
| recipe := smelting_recipe(raw) or { | ||
| assert false, '${raw} does not cook' | ||
| continue | ||
| } | ||
| assert recipe.output == cooked | ||
| } | ||
| } | ||
|
|
||
| fn test_most_things_do_not_smelt() { | ||
| if _ := smelting_recipe('minecraft:diamond') { | ||
| assert false, 'a diamond smelted into something' | ||
| } | ||
| if _ := smelting_recipe('minecraft:stick') { | ||
| assert false, 'a stick smelted into something' | ||
| } | ||
| } | ||
|
|
||
| fn test_fuel_burns_for_its_own_time() { | ||
| assert fuel_burn_ticks('minecraft:coal')? == 1600 | ||
| assert fuel_burn_ticks('minecraft:coal_block')? == 16000 | ||
| assert fuel_burn_ticks('minecraft:lava_bucket')? == 20000 | ||
| assert fuel_burn_ticks('minecraft:stick')? == 100 | ||
| // Anything wooden burns for the same short while. | ||
| assert fuel_burn_ticks('minecraft:oak_planks')? == 300 | ||
| assert fuel_burn_ticks('minecraft:spruce_log')? == 300 | ||
| } | ||
|
|
||
| fn test_things_that_do_not_burn() { | ||
| if _ := fuel_burn_ticks('minecraft:stone') { | ||
| assert false, 'stone burned as fuel' | ||
| } | ||
| if _ := fuel_burn_ticks('minecraft:iron_ingot') { | ||
| assert false, 'an iron ingot burned as fuel' | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not every slab is burnable (stone for example)