Complete basic crafting functionality

This commit is contained in:
Andrew Miner
2014-12-24 19:17:37 -08:00
parent 3ea6a845c7
commit 1a84ce2e28
20 changed files with 427 additions and 90 deletions
+79
View File
@@ -0,0 +1,79 @@
###
# Crafting Guide - crafting_plan.test.coffee
#
# Copyright (c) 2014 by Redwood Labs
# All rights reserved.
###
RecipeCatalog = require '../src/scripts/models/recipe_catalog'
CraftingPlan = require '../src/scripts/models/crafting_plan'
########################################################################################################################
catalog = plan = null
########################################################################################################################
describe 'CraftingPlan', ->
beforeEach ->
catalog = new RecipeCatalog
catalog.loadBookData {
version: 1
mod_name: 'Minecraft'
mod_version: '1.7.10'
recipes: [
{ input:'Oak Log', output:[[4, 'Oak Plank']] }
{ input:[[2, 'Oak Plank']], output:[[4, 'Stick']] }
{ input:[[4, 'Oak Plank']], output:'Crafting Table' }
{ input:[[8, 'Cobblestone']], tools:'Crafting Table', output:'Furnace' }
{ input:['Iron Ore', '{furnace fuel}'], tools:'Furnace', output:'Iron Ingot' }
{ input:[[2, 'Iron Ingot'], 'Stick'], tools:'Crafting Table', output:'Iron Sword' }
]
}
plan = new CraftingPlan catalog
describe 'craft', ->
describe 'under the simplest conditions', ->
it 'can craft a single step recipe', ->
plan.craft 'Oak Plank'
plan.need.toList().should.eql ['Oak Log']
plan.result.toList().should.eql [[4, 'Oak Plank']]
it 'can craft a multi-step recipe', ->
plan.craft 'Crafting Table'
plan.need.toList().should.eql ['Oak Log']
plan.result.toList().should.eql ['Crafting Table']
it 'can craft a multi-step recipe using tools', ->
plan.craft 'Furnace'
plan.need.toList().should.eql [[8, 'Cobblestone']]
plan.result.toList().should.eql ['Furnace']
it 'can craft a multi-step recipe re-using tools', ->
plan.craft 'Iron Sword'
plan.need.toList().should.eql [[2, 'Iron Ore'], 'Oak Log', [2, '{furnace fuel}']]
plan.result.toList().should.eql ['Iron Sword', [2, 'Oak Plank'], [3, 'Stick']]
describe 'with building tools', ->
it 'can craft a multi-step recipe using tools', ->
plan.includingTools = true
plan.craft 'Furnace'
plan.need.toList().should.eql [[8, 'Cobblestone'], 'Oak Log']
plan.result.toList().should.eql ['Crafting Table', 'Furnace']
it 'can craft a multi-step recipe re-using tools', ->
plan.includingTools = true
plan.craft 'Iron Sword'
plan.need.toList().should.eql [
[8, 'Cobblestone'], [2, 'Iron Ore'], [2, 'Oak Log'], [2, '{furnace fuel}'],
]
plan.result.toList().should.eql [
'Crafting Table', 'Furnace', 'Iron Sword', [2, 'Oak Plank'], [3, 'Stick']
]
describe 'using existing inventory', ->
+47 -12
View File
@@ -35,17 +35,42 @@ describe 'Inventory', ->
it 'can augment quantity of existing items', ->
inventory.add 'wool', 2
inventory._items.wool.quantity.should.equal 6
inventory.toList().should.eql ['boat', [20, 'string'], [6, 'wool']]
it 'can add zero quantity', ->
inventory.add 'wool', 0
inventory._items.wool.quantity.should.equal 4
inventory.toList().should.eql ['boat', [20, 'string'], [4, 'wool']]
it 'emits the proper events', ->
events = new EventRecorder inventory
inventory.add 'iron ingot', 10
events.names.should.eql [Event.add, Event.change]
describe 'addInventory', ->
it 'can add to an empty inventory', ->
newInventory = new Inventory
newInventory.addInventory inventory
newInventory._names.should.eql ['boat', 'string', 'wool']
it 'can add a mix of new and existing items', ->
newInventory = new Inventory
newInventory.add 'string', 2
newInventory.addInventory inventory
newInventory.toList().should.eql ['boat', [22, 'string'], [4, 'wool']]
describe 'clone', ->
it 'creates an empty inventory from an empty inventory', ->
a = new Inventory
b = a.clone()
b._names.should.eql []
it 'faithfully copies an existing inventory', ->
copy = inventory.clone()
copy._names.should.eql ['boat', 'string', 'wool']
(item.quantity for name, item of copy._items).should.eql [1, 20, 4]
describe 'each', ->
it 'works with an empty inventory', ->
@@ -57,7 +82,7 @@ describe 'Inventory', ->
it 'works when items have only been added', ->
result = []
inventory.each (item)-> result.push item.name
result.should.eql ['wool', 'string', 'boat']
result.should.eql ['boat', 'string', 'wool']
it 'works when items have been augmented', ->
inventory.add 'iron ingot'
@@ -66,15 +91,7 @@ describe 'Inventory', ->
result = []
inventory.each (item)-> result.push item.name
result.should.eql ['wool', 'string', 'boat', 'iron ingot']
it 'moves items to the end when removed and re-added', ->
inventory.remove 'wool', 4
inventory.add 'wool'
result = []
inventory.each (item)-> result.push item.name
result.should.eql ['string', 'boat', 'wool']
result.should.eql ['boat', 'iron ingot', 'string', 'wool']
describe 'hasAtLeast', ->
@@ -91,6 +108,24 @@ describe 'Inventory', ->
inventory.hasAtLeast('wool', 4).should.be.true
inventory.hasAtLeast('wool', 5).should.be.false
describe 'pop', ->
it 'returns null for an empty inventory', ->
inventory = new Inventory
result = inventory.pop()
expect(result).to.be.null
it 'completely removes the last item', ->
result = inventory.pop()
result.name.should.equal 'wool'
result.quantity.should.equal 4
inventory.toList().should.eql ['boat', [20, 'string']]
it 'triggers the right events', ->
events = new EventRecorder inventory
result = inventory.pop()
events.names.should.eql [Event.remove, Event.change]
describe 'remove', ->
it 'throws when the item is absent', ->
+2 -7
View File
@@ -32,15 +32,10 @@ describe 'InventoryParser', ->
it 'can parse multiple mixed-type items', ->
result = parser.parse '4 wool\n10 string\nboat\n\n'
result._items.wool.quantity.should.equal 4
result._items.string.quantity.should.equal 10
result._items.boat.quantity.should.equal 1
result._names.should.eql ['wool', 'string', 'boat']
result.toList().should.eql ['boat', [10, 'string'], [4, 'wool']]
it 're-uses the given inventory object', ->
inventory = new Inventory
inventory.add 'string', 8
result = parser.parse '4 wool', inventory
inventory._items.string.quantity.should.equal 8
inventory._items.wool.quantity.should.equal 4
inventory._names.should.eql ['string', 'wool']
result.toList().should.eql [[8, 'string'], [4, 'wool']]
+1
View File
@@ -24,6 +24,7 @@ global.logger = new Logger level:Logger.TRACE
mocha.setup 'bdd'
require './crafting_plan.test'
require './inventory.test'
require './inventory_parser.test'
require './recipe_book_parser.test'