BlockType.cpp 7.5 KB

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