BlockType.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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(Block* defaultBlock,
  9. ModelInfo* model,
  10. bool needsClientInstance,
  11. int initialMaxHP,
  12. bool lightSource,
  13. Framework::Text name,
  14. bool needModelSubscription,
  15. int initialMapColor,
  16. Framework::RCArray<Framework::Text> groupNames,
  17. float hardness)
  18. : ReferenceCounter(),
  19. id(id),
  20. model(model),
  21. initialMaxHP(initialMaxHP),
  22. needsClientInstance(needsClientInstance),
  23. lightSource(lightSource),
  24. name(name),
  25. needModelSubscription(needModelSubscription),
  26. initialMapColor(initialMapColor),
  27. defaultBlock(defaultBlock),
  28. groupNames(groupNames),
  29. hardness(hardness)
  30. {}
  31. BlockType::~BlockType()
  32. {
  33. if (defaultBlock) defaultBlock->release();
  34. }
  35. void BlockType::loadSuperBlock(
  36. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  37. {
  38. zBlock->loadInventory(zReader);
  39. zReader->lese((char*)&zBlock->transparent, 1);
  40. zReader->lese((char*)&zBlock->passable, 1);
  41. zReader->lese((char*)&zBlock->hp, 4);
  42. zReader->lese((char*)&zBlock->maxHP, 4);
  43. zReader->lese((char*)&zBlock->hardness, 4);
  44. zReader->lese((char*)&zBlock->speedModifier, 4);
  45. zReader->lese((char*)&zBlock->mapColor, 4);
  46. zReader->lese((char*)&zBlock->interactable, 1);
  47. int strCount;
  48. zReader->lese((char*)&strCount, 4);
  49. for (int i = 0; i < strCount; i++)
  50. {
  51. __int64 id;
  52. zReader->lese((char*)&id, 8);
  53. MultiblockStructure* str
  54. = Game::INSTANCE->zDimension(dimensionId)->zStructureById(id);
  55. if (str)
  56. {
  57. zBlock->structures.add(
  58. dynamic_cast<MultiblockStructure*>(str->getThis()));
  59. }
  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. zWriter->schreibe((char*)&zBlock->mapColor, 4);
  73. zWriter->schreibe((char*)&zBlock->interactable, 1);
  74. int strCount = zBlock->structures.getEintragAnzahl();
  75. zWriter->schreibe((char*)&strCount, 4);
  76. for (MultiblockStructure* structure : zBlock->structures)
  77. {
  78. __int64 id = structure->getStructureId();
  79. zWriter->schreibe((char*)&id, 8);
  80. }
  81. }
  82. void BlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  83. {
  84. if (zItem)
  85. {
  86. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  87. if (item)
  88. {
  89. zBlock->transparent = item->transparent;
  90. zBlock->passable = item->passable;
  91. zBlock->hardness = item->hardness;
  92. zBlock->speedModifier = item->speedModifier;
  93. zBlock->interactable = item->interactable;
  94. }
  95. }
  96. zBlock->mapColor = initialMapColor;
  97. }
  98. void BlockType::createSuperItem(Block* zBlock, Item* zItem) const
  99. {
  100. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  101. if (item)
  102. {
  103. item->transparent = zBlock->transparent;
  104. item->passable = zBlock->passable;
  105. item->hardness = zBlock->hardness;
  106. item->speedModifier = zBlock->speedModifier;
  107. item->interactable = zBlock->interactable;
  108. }
  109. }
  110. bool BlockType::initialize(Game* zGame)
  111. {
  112. return true;
  113. }
  114. Framework::Text BlockType::getTargetUIML() const
  115. {
  116. return Text("<targetInfo><text width=\"auto\" height=\"auto\">") + name
  117. + "</text></targetInfo>";
  118. }
  119. Block* BlockType::loadBlock(Framework::Vec3<int> position,
  120. Framework::StreamReader* zReader,
  121. int dimensionId) const
  122. {
  123. Block* result = createBlock(position, dimensionId);
  124. loadSuperBlock(result, zReader, dimensionId);
  125. return result;
  126. }
  127. void BlockType::saveBlock(Block* zBlock, Framework::StreamWriter* zWriter) const
  128. {
  129. saveSuperBlock(zBlock, zWriter);
  130. }
  131. Item* BlockType::getItemFromBlock(Block* zBlock) const
  132. {
  133. Item* result = createItem();
  134. if (result)
  135. {
  136. createSuperItem(zBlock, result);
  137. }
  138. return result;
  139. }
  140. Block* BlockType::createBlockAt(
  141. Framework::Vec3<int> position, int dimensionId, Item* zUsedItem) const
  142. {
  143. Block* result = createBlock(position, dimensionId);
  144. createSuperBlock(result, zUsedItem);
  145. return result;
  146. }
  147. bool BlockType::doesNeedClientInstance() const
  148. {
  149. return needsClientInstance;
  150. }
  151. ModelInfo* BlockType::zModel() const
  152. {
  153. return model;
  154. }
  155. int BlockType::getMapColor() const
  156. {
  157. return initialMapColor;
  158. }
  159. const Framework::RCArray<Framework::Text>& BlockType::getGroupNames() const
  160. {
  161. return groupNames;
  162. }
  163. float BlockType::getHardness() const
  164. {
  165. return hardness;
  166. }
  167. int BlockType::getId() const
  168. {
  169. return id;
  170. }
  171. const Block* BlockType::zDefault() const
  172. {
  173. return defaultBlock;
  174. }
  175. int BlockType::getInitialMaxHP() const
  176. {
  177. return initialMaxHP;
  178. }
  179. bool BlockType::isLightSource() const
  180. {
  181. return lightSource;
  182. }
  183. const Block* getDefaultBlock(Either<Block*, int> b)
  184. {
  185. if (b.isA())
  186. return b;
  187. else
  188. return Game::INSTANCE->zBlockType(b)->zDefault();
  189. }
  190. BlockType* BlockType::initializeDefault()
  191. {
  192. if (!defaultBlock)
  193. {
  194. defaultBlock = createBlockAt({0, 0, 0}, 0, 0);
  195. }
  196. return this;
  197. }
  198. const char* BlockType::getName() const
  199. {
  200. return name;
  201. }
  202. const bool BlockType::doesNeedModelSubscription() const
  203. {
  204. return needModelSubscription;
  205. }
  206. void BlockType::writeTypeInfo(StreamWriter* zWriter) const
  207. {
  208. int id = getId();
  209. zWriter->schreibe((char*)&id, 4);
  210. bool inst = doesNeedClientInstance();
  211. zWriter->schreibe((char*)&inst, 1);
  212. bool sub = doesNeedModelSubscription();
  213. zWriter->schreibe((char*)&sub, 1);
  214. bool fluid = isFluid();
  215. zWriter->schreibe((char*)&fluid, 1);
  216. if (fluid)
  217. {
  218. char flowDist = getFlowDistance();
  219. zWriter->schreibe(&flowDist, 1);
  220. }
  221. int maxHp = getInitialMaxHP();
  222. zWriter->schreibe((char*)&maxHp, 4);
  223. zModel()->writeTo(zWriter);
  224. }
  225. bool BlockType::isFluid() const
  226. {
  227. return false;
  228. }
  229. int BlockType::getTypeId(const char* name)
  230. {
  231. Text n = name;
  232. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  233. {
  234. if (Game::INSTANCE->zBlockType(i)
  235. && n.istGleich(Game::INSTANCE->zBlockType(i)->getName()))
  236. return Game::INSTANCE->zBlockType(i)->getId();
  237. }
  238. return 0;
  239. }
  240. Framework::Text BlockType::getTypeName(int id)
  241. {
  242. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  243. {
  244. if (Game::INSTANCE->zBlockType(i)
  245. && Game::INSTANCE->zBlockType(i)->getId() == id)
  246. return Game::INSTANCE->zBlockType(i)->getName();
  247. }
  248. return 0;
  249. }
  250. unsigned char BlockType::getFlowDistance() const
  251. {
  252. return 0;
  253. }
  254. void BlockType::setTypeId(int id)
  255. {
  256. this->id = id;
  257. }