BlockType.cpp 6.1 KB

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