BlockType.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. zBlock->transparent = item->transparent;
  98. zBlock->passable = item->passable;
  99. zBlock->hardness = item->hardness;
  100. zBlock->speedModifier = item->speedModifier;
  101. zBlock->zTool
  102. = item->toolId >= 0
  103. ? StaticRegistry<ItemType>::INSTANCE.zElement(item->toolId)
  104. : 0;
  105. zBlock->interactable = item->interactable;
  106. }
  107. }
  108. zBlock->mapColor = initialMapColor;
  109. }
  110. void BlockType::createSuperItem(Block* zBlock, Item* zItem) const
  111. {
  112. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  113. if (item)
  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. }
  123. Framework::Text BlockType::getTargetUIML() const
  124. {
  125. return Text("<targetInfo><text width=\"auto\" height=\"auto\">") + name
  126. + "</text></targetInfo>";
  127. }
  128. Block* BlockType::loadBlock(Framework::Vec3<int> position,
  129. Framework::StreamReader* zReader,
  130. int dimensionId) const
  131. {
  132. Block* result = createBlock(position, dimensionId);
  133. loadSuperBlock(result, zReader, dimensionId);
  134. return result;
  135. }
  136. void BlockType::saveBlock(Block* zBlock, Framework::StreamWriter* zWriter) const
  137. {
  138. saveSuperBlock(zBlock, zWriter);
  139. }
  140. Item* BlockType::getItemFromBlock(Block* zBlock) const
  141. {
  142. Item* result = createItem();
  143. if (result)
  144. {
  145. createSuperItem(zBlock, result);
  146. }
  147. return result;
  148. }
  149. Block* BlockType::createBlockAt(
  150. Framework::Vec3<int> position, int dimensionId, Item* zUsedItem) const
  151. {
  152. Block* result = createBlock(position, dimensionId);
  153. createSuperBlock(result, zUsedItem);
  154. return result;
  155. }
  156. bool BlockType::doesNeedClientInstance() const
  157. {
  158. return needsClientInstance;
  159. }
  160. const ModelInfo& BlockType::getModel() const
  161. {
  162. return model;
  163. }
  164. int BlockType::getId() const
  165. {
  166. return id;
  167. }
  168. const Block* BlockType::zDefault() const
  169. {
  170. return defaultBlock;
  171. }
  172. int BlockType::getInitialMaxHP() const
  173. {
  174. return initialMaxHP;
  175. }
  176. bool BlockType::isLightSource() const
  177. {
  178. return lightSource;
  179. }
  180. const Block* getDefaultBlock(Either<Block*, int> b)
  181. {
  182. if (b.isA())
  183. return b;
  184. else
  185. return StaticRegistry<BlockType>::INSTANCE.zElement(b)->zDefault();
  186. }
  187. BlockType* BlockType::initializeDefault()
  188. {
  189. defaultBlock = createBlockAt({0, 0, 0}, 0, 0);
  190. return this;
  191. }
  192. const char* BlockType::getName() const
  193. {
  194. return name;
  195. }
  196. const bool BlockType::doesNeedModelSubscription() const
  197. {
  198. return needModelSubscription;
  199. }
  200. void BlockType::writeTypeInfo(StreamWriter* zWriter) const
  201. {
  202. int id = getId();
  203. zWriter->schreibe((char*)&id, 4);
  204. bool inst = doesNeedClientInstance();
  205. zWriter->schreibe((char*)&inst, 1);
  206. bool sub = doesNeedModelSubscription();
  207. zWriter->schreibe((char*)&sub, 1);
  208. bool fluid = isFluid();
  209. zWriter->schreibe((char*)&fluid, 1);
  210. if (fluid)
  211. {
  212. char flowDist = getFlowDistance();
  213. zWriter->schreibe(&flowDist, 1);
  214. }
  215. int maxHp = getInitialMaxHP();
  216. zWriter->schreibe((char*)&maxHp, 4);
  217. getModel().writeTo(zWriter);
  218. }
  219. bool BlockType::isFluid() const
  220. {
  221. return false;
  222. }
  223. int BlockType::getTypeId(const char* name)
  224. {
  225. Text n = name;
  226. for (int i = 0; i < StaticRegistry<BlockType>::INSTANCE.getCount(); i++)
  227. {
  228. if (StaticRegistry<BlockType>::INSTANCE.zElement(i)
  229. && n.istGleich(
  230. StaticRegistry<BlockType>::INSTANCE.zElement(i)->getName()))
  231. return StaticRegistry<BlockType>::INSTANCE.zElement(i)->getId();
  232. }
  233. return 0;
  234. }
  235. Framework::Text BlockType::getTypeName(int id)
  236. {
  237. for (int i = 0; i < StaticRegistry<BlockType>::INSTANCE.getCount(); i++)
  238. {
  239. if (StaticRegistry<BlockType>::INSTANCE.zElement(i)
  240. && StaticRegistry<BlockType>::INSTANCE.zElement(i)->getId() == id)
  241. return StaticRegistry<BlockType>::INSTANCE.zElement(i)->getName();
  242. }
  243. return 0;
  244. }
  245. char BlockType::getFlowDistance() const
  246. {
  247. return 0;
  248. }