CraftingStorage.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "CraftingStorage.h"
  2. BasicShapedCrafter::BasicShapedCrafter( int width, int height, Inventory* zInventory )
  3. : zInventory( zInventory ),
  4. width( width ),
  5. height( height )
  6. {
  7. for( int i = 0; i < width * height; i++ )
  8. {
  9. ItemSlot* slot = new ItemSlot( 1, std::numeric_limits<int>::max(), std::numeric_limits<int>::max(), INSIDE, INSIDE, 0 );
  10. zInventory->addSlot( slot );
  11. craftingInput.add( slot );
  12. }
  13. }
  14. bool BasicShapedCrafter::isAllAvailable( Framework::RCArray<ItemFilter>& filters, int width, int height )
  15. {
  16. for( int x = 0; x <= this->width - width; x++ )
  17. {
  18. for( int y = 0; y <= this->height - height; y++ )
  19. {
  20. for( int w = 0; w < width; w++ )
  21. {
  22. for( int h = 0; h < height; h++ )
  23. {
  24. ItemFilter* f = filters.z( w * width + h );
  25. ItemSlot* s = craftingInput.get( w * 2 + h );
  26. const Item* item = 0;
  27. if( s && s->zStack() )
  28. item = s->zStack()->zItem();
  29. if( (item && !f) || (!item && f) ) return 0;
  30. if( item && f && !f->matchItem( item ) )
  31. return 0;
  32. }
  33. }
  34. }
  35. }
  36. return 1;
  37. }
  38. bool BasicShapedCrafter::hasFreeSpace( const Item* zItem, int amount )
  39. {
  40. ItemStack* stack = new ItemStack( (Item*)zItem, amount );
  41. int addable = zInventory->numberOfAddableItems( stack, NO_DIRECTION );
  42. stack->release();
  43. return addable >= amount;
  44. }
  45. bool BasicShapedCrafter::consume( Framework::RCArray<ItemFilter>& filters, int width, int height )
  46. {
  47. for( ItemSlot* slot : craftingInput )
  48. {
  49. if( slot && slot->zStack() )
  50. {
  51. ItemStack* stack = zInventory->takeItemsOut( slot, 1, INSIDE );
  52. if( stack )
  53. stack->release();
  54. }
  55. }
  56. for( int x = 0; x < width; x++ )
  57. {
  58. for( int y = 0; y < height; y++ )
  59. {
  60. ItemFilter* f = filters.z( x * width + y );
  61. if( f )
  62. {
  63. ItemSlot* target = craftingInput.get( x * this->width + y );
  64. Framework::Array< ItemSlot*> tmp;
  65. tmp.add( target );
  66. zInventory->localTransaction( 0, &tmp, f, 1, NO_DIRECTION, INSIDE );
  67. }
  68. }
  69. }
  70. return 1;
  71. }
  72. void BasicShapedCrafter::addCraftingResult( ItemStack* stack )
  73. {
  74. zInventory->addItems( stack, NO_DIRECTION );
  75. }