BlockType.cpp 5.5 KB

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