BlockType.cpp 6.3 KB

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