BlockType.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "BlockType.h"
  2. #include "ItemType.h"
  3. #include "BasicBlocks.h"
  4. #include "Block.h"
  5. #include "MultiblockStructure.h"
  6. #include "Game.h"
  7. using namespace Framework;
  8. BlockType::BlockType(int id, Block* defaultBlock,
  9. ModelInfo model, bool needsClientInstance, int initialMaxHP, bool lightSource)
  10. : ReferenceCounter(),
  11. id(id),
  12. model(model),
  13. initialMaxHP(initialMaxHP),
  14. needsClientInstance(needsClientInstance),
  15. lightSource(lightSource),
  16. defaultBlock(defaultBlock)
  17. {
  18. StaticRegistry<BlockType>::INSTANCE.registerT(this, id);
  19. }
  20. BlockType::~BlockType()
  21. {
  22. if (defaultBlock)
  23. defaultBlock->release();
  24. }
  25. void BlockType::loadSuperBlock(Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  26. {
  27. zBlock->loadInventory(zReader);
  28. zReader->lese((char*)&zBlock->transparent, 1);
  29. zReader->lese((char*)&zBlock->passable, 1);
  30. zReader->lese((char*)&zBlock->hp, 4);
  31. zReader->lese((char*)&zBlock->maxHP, 4);
  32. zReader->lese((char*)&zBlock->hardness, 4);
  33. zReader->lese((char*)&zBlock->speedModifier, 4);
  34. int effectiveToolId;
  35. zReader->lese((char*)&effectiveToolId, 4);
  36. if (effectiveToolId >= 0)
  37. zBlock->zTool = StaticRegistry<ItemType>::INSTANCE.zElement(effectiveToolId);
  38. else
  39. zBlock->zTool = 0;
  40. zReader->lese((char*)&zBlock->interactable, 1);
  41. int strCount;
  42. zReader->lese((char*)&strCount, 4);
  43. for (int i = 0; i < strCount; i++)
  44. {
  45. __int64 id;
  46. zReader->lese((char*)&id, 8);
  47. MultiblockStructure* str = Game::INSTANCE->zDimension(dimensionId)->zStructureById(id);
  48. zBlock->structures.add(dynamic_cast<MultiblockStructure*>(str->getThis()));
  49. }
  50. }
  51. void BlockType::saveSuperBlock(Block* zBlock, Framework::StreamWriter* zWriter) const
  52. {
  53. zBlock->saveInventory(zWriter);
  54. zWriter->schreibe((char*)&zBlock->transparent, 1);
  55. zWriter->schreibe((char*)&zBlock->passable, 1);
  56. zWriter->schreibe((char*)&zBlock->hp, 4);
  57. zWriter->schreibe((char*)&zBlock->maxHP, 4);
  58. zWriter->schreibe((char*)&zBlock->hardness, 4);
  59. zWriter->schreibe((char*)&zBlock->speedModifier, 4);
  60. int effectiveToolId = zBlock->zTool ? zBlock->zTool->getId() : -1;
  61. zWriter->schreibe((char*)&effectiveToolId, 4);
  62. zWriter->schreibe((char*)&zBlock->interactable, 1);
  63. int strCount = zBlock->structures.getEintragAnzahl();
  64. zWriter->schreibe((char*)&strCount, 4);
  65. for (MultiblockStructure* structure : zBlock->structures)
  66. {
  67. __int64 id = structure->getStructureId();
  68. zWriter->schreibe((char*)&id, 8);
  69. }
  70. }
  71. void BlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  72. {
  73. if (zItem)
  74. {
  75. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  76. if (!item)
  77. {
  78. throw "BlockType::createSuperBlock was called with an item witch was not an instance of BasicBlockItem";
  79. }
  80. zBlock->transparent = item->transparent;
  81. zBlock->passable = item->passable;
  82. zBlock->hardness = item->hardness;
  83. zBlock->speedModifier = item->speedModifier;
  84. zBlock->zTool = item->toolId >= 0 ? StaticRegistry<ItemType>::INSTANCE.zElement(item->toolId) : 0;
  85. zBlock->interactable = item->interactable;
  86. }
  87. }
  88. void BlockType::createSuperItem(Block* zBlock, Item* zItem) const
  89. {
  90. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  91. if (!item)
  92. {
  93. throw "BlockType::createSuperItem was called with an item witch was not an instance of BasicBlockItem";
  94. }
  95. item->transparent = zBlock->transparent;
  96. item->passable = zBlock->passable;
  97. item->hardness = zBlock->hardness;
  98. item->speedModifier = zBlock->speedModifier;
  99. item->toolId = zBlock->zTool ? zBlock->zTool->getId() : -1;
  100. item->interactable = zBlock->interactable;
  101. }
  102. Block* BlockType::loadBlock(Framework::Vec3<int> position, Framework::StreamReader* zReader, int dimensionId) const
  103. {
  104. Block* result = createBlock(position);
  105. loadSuperBlock(result, zReader, dimensionId);
  106. return result;
  107. }
  108. void BlockType::saveBlock(Block* zBlock, Framework::StreamWriter* zWriter) const
  109. {
  110. saveSuperBlock(zBlock, zWriter);
  111. }
  112. Item* BlockType::getItemFromBlock(Block* zBlock) const
  113. {
  114. Item* result = createItem();
  115. createSuperItem(zBlock, result);
  116. return result;
  117. }
  118. Block* BlockType::createBlockAt(Framework::Vec3<int> position, Item* zUsedItem) const
  119. {
  120. Block* result = createBlock(position);
  121. createSuperBlock(result, zUsedItem);
  122. return result;
  123. }
  124. bool BlockType::doesNeedClientInstance() const
  125. {
  126. return needsClientInstance;
  127. }
  128. const ModelInfo& BlockType::getModel() const
  129. {
  130. return model;
  131. }
  132. int BlockType::getId() const
  133. {
  134. return id;
  135. }
  136. const Block* BlockType::zDefault() const
  137. {
  138. return defaultBlock;
  139. }
  140. int BlockType::getInitialMaxHP() const
  141. {
  142. return initialMaxHP;
  143. }
  144. bool BlockType::isLightSource() const
  145. {
  146. return lightSource;
  147. }
  148. const Block* getDefaultBlock(Either<Block*, int> b)
  149. {
  150. if (b.isA())
  151. return b;
  152. else
  153. return StaticRegistry<BlockType>::INSTANCE.zElement(b)->zDefault();
  154. }