BlockType.cpp 7.3 KB

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