BlockType.cpp 5.2 KB

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