Improve handling of pass-through items in recipes
This commit is contained in:
@@ -24,6 +24,7 @@ module.exports = class CraftingPlan
|
|||||||
@_rawScores = {}
|
@_rawScores = {}
|
||||||
@_scores = {}
|
@_scores = {}
|
||||||
@_steps = steps
|
@_steps = steps
|
||||||
|
@_tools = null
|
||||||
@_want = want
|
@_want = want
|
||||||
|
|
||||||
@_numberSteps()
|
@_numberSteps()
|
||||||
@@ -37,19 +38,27 @@ module.exports = class CraftingPlan
|
|||||||
@_made = new SimpleInventory modPack:@_modPack
|
@_made = new SimpleInventory modPack:@_modPack
|
||||||
@_made.addInventory @_have
|
@_made.addInventory @_have
|
||||||
|
|
||||||
|
@_tools = new SimpleInventory modPack:@_modPack
|
||||||
|
|
||||||
for i in [@_steps.length-1..0] by -1
|
for i in [@_steps.length-1..0] by -1
|
||||||
step = @_steps[i]
|
step = @_steps[i]
|
||||||
step.multiplier = 0
|
step.multiplier = 0
|
||||||
|
recipe = step.recipe
|
||||||
|
|
||||||
for stack in step.recipe.output
|
for stack in step.recipe.output
|
||||||
if not stack?
|
|
||||||
throw new Error 'stack should not be null here'
|
|
||||||
|
|
||||||
qualifiedSlug = @_modPack.qualifySlug stack.itemSlug
|
qualifiedSlug = @_modPack.qualifySlug stack.itemSlug
|
||||||
while @_need.quantityOf(qualifiedSlug) > 0
|
|
||||||
@_executeStep step
|
if recipe.isPassThroughFor stack.itemSlug
|
||||||
|
continue if @_need.hasAtLeast qualifiedSlug, 1
|
||||||
|
continue if @_tools.hasAtLeast qualifiedSlug, 1
|
||||||
|
@_need.add qualifiedSlug, 1
|
||||||
|
@_tools.add qualifiedSlug, 1
|
||||||
|
else
|
||||||
|
while @_need.quantityOf(qualifiedSlug) > 0
|
||||||
|
@_executeStep step
|
||||||
|
|
||||||
@_made.addInventory @_want
|
@_made.addInventory @_want
|
||||||
|
@_made.addInventory @_tools
|
||||||
@_pruneEmptySteps()
|
@_pruneEmptySteps()
|
||||||
@_numberSteps()
|
@_numberSteps()
|
||||||
|
|
||||||
|
|||||||
@@ -63,6 +63,22 @@ describe 'crafting_plan.coffee', ->
|
|||||||
'1x 8 test__iron_ore,test__coal>.0. ... .1.>test__furnace>8 test__iron_ingot'
|
'1x 8 test__iron_ore,test__coal>.0. ... .1.>test__furnace>8 test__iron_ingot'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
it 'uses the correct amount of passthrough items', ->
|
||||||
|
plans = fixtures.makePlans [10, 'test__split_oak_wood'], [10, 'test__split_spruce_wood']
|
||||||
|
plan = plans[2]
|
||||||
|
plan.computeRequired()
|
||||||
|
|
||||||
|
plan.need.unparse().should.equal 'coal:8.iron_ore:6.oak_wood:5.spruce_wood'
|
||||||
|
plan.made.unparse().should.equal '4.iron_ingot:maul:2.oak_planks:10.split_oak_wood:10.split_spruce_wood:stick'
|
||||||
|
(s.toString() for s in plan.steps).should.eql [
|
||||||
|
'1x test__oak_wood>... .0. ...>>4 test__oak_planks'
|
||||||
|
'1x test__oak_planks>.0. .0. ...>>4 test__stick'
|
||||||
|
'1x 8 test__iron_ore,test__coal>.0. ... .1.>test__furnace>8 test__iron_ingot'
|
||||||
|
'1x test__iron_ingot,test__stick>001 001 ..1>test__crafting_table>test__maul'
|
||||||
|
'5x test__spruce_wood,test__maul>.1. .0. ...>>2 test__split_spruce_wood,test__maul'
|
||||||
|
'5x test__oak_wood,test__maul>.1. .0. ...>>2 test__split_oak_wood,test__maul'
|
||||||
|
]
|
||||||
|
|
||||||
it 'can compute multiple items with multiple plans', ->
|
it 'can compute multiple items with multiple plans', ->
|
||||||
plans = fixtures.makePlans [1, 'test__copper_block'], [1, 'test__iron_sword']
|
plans = fixtures.makePlans [1, 'test__copper_block'], [1, 'test__iron_sword']
|
||||||
plans.length.should.equal 4
|
plans.length.should.equal 4
|
||||||
|
|||||||
@@ -67,4 +67,6 @@ module.exports = class CraftingStep
|
|||||||
@_inventory = new SimpleInventory
|
@_inventory = new SimpleInventory
|
||||||
for stack in @_recipe.input
|
for stack in @_recipe.input
|
||||||
qualifiedSlug = @_modPack.qualifySlug stack.itemSlug
|
qualifiedSlug = @_modPack.qualifySlug stack.itemSlug
|
||||||
@_inventory.add qualifiedSlug, @multiplier * @_recipe.getQuantityRequired stack.itemSlug
|
required = @_recipe.getQuantityRequired(stack.itemSlug) - @_recipe.getQuantityProduced(stack.itemSlug)
|
||||||
|
required = if required > 0 then required * @multiplier else 1
|
||||||
|
@_inventory.add qualifiedSlug, required
|
||||||
|
|||||||
@@ -98,6 +98,12 @@ MOD_VERSION_FILE =
|
|||||||
input: Stick, Cobblestone
|
input: Stick, Cobblestone
|
||||||
pattern: .0. .1. ...
|
pattern: .0. .1. ...
|
||||||
|
|
||||||
|
item: Maul
|
||||||
|
recipe:
|
||||||
|
input: Iron Ingot, Stick
|
||||||
|
pattern: 001 001 ..1
|
||||||
|
tools: Crafting Table
|
||||||
|
|
||||||
item: Oak Planks
|
item: Oak Planks
|
||||||
recipe:
|
recipe:
|
||||||
input: Oak Wood
|
input: Oak Wood
|
||||||
@@ -107,12 +113,29 @@ MOD_VERSION_FILE =
|
|||||||
item: Oak Wood
|
item: Oak Wood
|
||||||
gatherable: yes
|
gatherable: yes
|
||||||
|
|
||||||
|
item: Spruce Wood
|
||||||
|
gatherable: yes
|
||||||
|
|
||||||
item: Stick
|
item: Stick
|
||||||
recipe:
|
recipe:
|
||||||
input: Oak Planks
|
input: Oak Planks
|
||||||
pattern: .0. .0. ...
|
pattern: .0. .0. ...
|
||||||
quantity: 4
|
quantity: 4
|
||||||
|
|
||||||
|
item: Split Oak Wood
|
||||||
|
recipe:
|
||||||
|
extras: Maul
|
||||||
|
input: Oak Wood, Maul
|
||||||
|
pattern: .1. .0. ...
|
||||||
|
quantity: 2
|
||||||
|
|
||||||
|
item: Split Spruce Wood
|
||||||
|
recipe:
|
||||||
|
extras: Maul
|
||||||
|
input: Spruce Wood, Maul
|
||||||
|
pattern: .1. .0. ...
|
||||||
|
quantity: 2
|
||||||
|
|
||||||
item: String
|
item: String
|
||||||
gatherable: yes
|
gatherable: yes
|
||||||
|
|
||||||
|
|||||||
@@ -111,16 +111,7 @@ module.exports = class Recipe extends BaseModel
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
isPassThroughFor: (itemSlug)->
|
isPassThroughFor: (itemSlug)->
|
||||||
amountCreated = 0
|
return @getQuantityProduced(itemSlug) is @getQuantityRequired(itemSlug)
|
||||||
for stack in @output
|
|
||||||
if stack.itemSlug.matches itemSlug
|
|
||||||
amountCreated += stack.quantity
|
|
||||||
|
|
||||||
for stack in @input
|
|
||||||
if stack.itemSlug.matches itemSlug
|
|
||||||
amountCreated -= stack.quantity
|
|
||||||
|
|
||||||
return amountCreated <= 0
|
|
||||||
|
|
||||||
produces: (itemSlug)->
|
produces: (itemSlug)->
|
||||||
if not @_produces?
|
if not @_produces?
|
||||||
|
|||||||
Reference in New Issue
Block a user