Recipie.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "Recipie.h"
  2. Recipie::Recipie()
  3. : ReferenceCounter()
  4. {}
  5. void Recipie::addIngredient( ItemFilter* filter, int amount )
  6. {
  7. filters.add( filter );
  8. inputAmount.add( amount );
  9. }
  10. void Recipie::addOutput( Item* item, int amount )
  11. {
  12. outputs.add( item );
  13. outputAmount.add( amount );
  14. }
  15. bool Recipie::testApplicability( CraftingStorage* zStorage )
  16. {
  17. for( int i = 0; i < outputs.getEintragAnzahl(); i++ )
  18. {
  19. if( !zStorage->hasFreeSpace( outputs.z( i ), outputAmount.get( i ) ) )
  20. return 0;
  21. }
  22. return zStorage->isAllAvailable( filters, inputAmount );
  23. }
  24. void Recipie::apply( CraftingStorage* zStorage )
  25. {
  26. zStorage->consume( filters, inputAmount );
  27. for( int i = 0; i < outputs.getEintragAnzahl(); i++ )
  28. {
  29. ItemStack* stack = new ItemStack( outputs.z( i )->zItemType()->cloneItem( outputs.z( i ) ), outputAmount.get( i ) );
  30. zStorage->addCraftingResult( stack );
  31. stack->release();
  32. }
  33. }
  34. ShapedRecipie::ShapedRecipie( int width, int height )
  35. :ReferenceCounter(),
  36. width( width ),
  37. height( height ),
  38. output( 0 ),
  39. outputAmount( 0 )
  40. {
  41. for( int i = 0; i < width * height; i++ )
  42. filters.add( 0 );
  43. }
  44. ShapedRecipie::~ShapedRecipie()
  45. {
  46. if( output )
  47. output->release();
  48. }
  49. void ShapedRecipie::setIngredient( int x, int y, ItemFilter* filter )
  50. {
  51. filters.set( filter, width * x + y );
  52. }
  53. void ShapedRecipie::setOutput( Item* item, int amount )
  54. {
  55. if( output )
  56. output->release();
  57. output = item;
  58. outputAmount = amount;
  59. }
  60. bool ShapedRecipie::testApplicability( ShapedCraftingStorage* zStorage )
  61. {
  62. return zStorage->isAllAvailable( filters, width, height ) && zStorage->hasFreeSpace( output, outputAmount );
  63. }
  64. void ShapedRecipie::apply( ShapedCraftingStorage* zStorage )
  65. {
  66. zStorage->consume( filters, width, height );
  67. ItemStack* stack = new ItemStack( output->zItemType()->cloneItem( output ), outputAmount );
  68. zStorage->addCraftingResult( stack );
  69. stack->release();
  70. }