BlockType.cpp 6.1 KB

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