BlockType.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include "BlockType.h"
  2. #include "BasicBlocks.h"
  3. #include "Block.h"
  4. #include "Dimension.h"
  5. #include "Game.h"
  6. #include "ItemType.h"
  7. #include "MultiblockStructure.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. for (DropConfig* config : dropConfigs)
  106. {
  107. config->initialize();
  108. }
  109. return true;
  110. }
  111. BlockType* BlockType::initializeDefault()
  112. {
  113. if (!defaultBlock)
  114. {
  115. defaultBlock = createBlockAt({0, 0, 0}, 0, 0);
  116. }
  117. return this;
  118. }
  119. void BlockType::addDropConfig(DropConfig* config)
  120. {
  121. dropConfigs.add(config);
  122. }
  123. const Framework::RCArray<DropConfig>& BlockType::getDropConfigs() const
  124. {
  125. return dropConfigs;
  126. }
  127. const Block* BlockType::zDefault() const
  128. {
  129. return defaultBlock;
  130. }
  131. void BlockType::writeTypeInfo(StreamWriter* zWriter) const
  132. {
  133. int id = getId();
  134. zWriter->schreibe((char*)&id, 4);
  135. bool inst = doesNeedClientInstance();
  136. zWriter->schreibe((char*)&inst, 1);
  137. bool sub = doesNeedModelSubscription();
  138. zWriter->schreibe((char*)&sub, 1);
  139. bool fluid = isFluid();
  140. zWriter->schreibe((char*)&fluid, 1);
  141. if (fluid)
  142. {
  143. char flowDist = getFlowDistance();
  144. zWriter->schreibe(&flowDist, 1);
  145. }
  146. int maxHp = getInitialMaxHP();
  147. zWriter->schreibe((char*)&maxHp, 4);
  148. zModel()->writeTo(zWriter);
  149. }
  150. Framework::Text BlockType::getTargetUIML() const
  151. {
  152. return Text("<targetInfo><text width=\"auto\" height=\"auto\">") + name
  153. + "</text></targetInfo>";
  154. }
  155. Block* BlockType::loadBlock(Framework::Vec3<int> position,
  156. Framework::StreamReader* zReader,
  157. int dimensionId) const
  158. {
  159. Block* result = createBlock(position, dimensionId);
  160. loadSuperBlock(result, zReader, dimensionId);
  161. return result;
  162. }
  163. void BlockType::saveBlock(Block* zBlock, Framework::StreamWriter* zWriter) const
  164. {
  165. saveSuperBlock(zBlock, zWriter);
  166. }
  167. Item* BlockType::getItemFromBlock(Block* zBlock) const
  168. {
  169. Item* result = createItem();
  170. if (result)
  171. {
  172. createSuperItem(zBlock, result);
  173. }
  174. return result;
  175. }
  176. Block* BlockType::createBlockAt(
  177. Framework::Vec3<int> position, int dimensionId, Item* zUsedItem) const
  178. {
  179. Block* result = createBlock(position, dimensionId);
  180. createSuperBlock(result, zUsedItem);
  181. return result;
  182. }
  183. int BlockType::getId() const
  184. {
  185. return id;
  186. }
  187. bool BlockType::isFluid() const
  188. {
  189. return false;
  190. }
  191. unsigned char BlockType::getFlowDistance() const
  192. {
  193. return 0;
  194. }
  195. void BlockType::setTypeId(int id)
  196. {
  197. this->id = id;
  198. }
  199. void BlockType::setModel(ModelInfo* model)
  200. {
  201. this->model = model;
  202. }
  203. ModelInfo* BlockType::zModel() const
  204. {
  205. return model;
  206. }
  207. void BlockType::setInitialMaxHP(int initialMaxHP)
  208. {
  209. this->initialMaxHP = initialMaxHP;
  210. }
  211. int BlockType::getInitialMaxHP() const
  212. {
  213. return initialMaxHP;
  214. }
  215. void BlockType::setNeedsClientInstance(bool needsClientInstance)
  216. {
  217. this->needsClientInstance = needsClientInstance;
  218. }
  219. bool BlockType::doesNeedClientInstance() const
  220. {
  221. return needsClientInstance;
  222. }
  223. void BlockType::setLightSource(bool lightSource)
  224. {
  225. this->lightSource = lightSource;
  226. }
  227. bool BlockType::isLightSource() const
  228. {
  229. return lightSource;
  230. }
  231. void BlockType::setName(Framework::Text name)
  232. {
  233. this->name = name;
  234. }
  235. const char* BlockType::getName() const
  236. {
  237. return name;
  238. }
  239. void BlockType::setNeedModelSubscription(bool needModelSubscription)
  240. {
  241. this->needModelSubscription = needModelSubscription;
  242. }
  243. const bool BlockType::doesNeedModelSubscription() const
  244. {
  245. return needModelSubscription;
  246. }
  247. void BlockType::setMapColor(int mapColor)
  248. {
  249. this->initialMapColor = mapColor;
  250. }
  251. int BlockType::getMapColor() const
  252. {
  253. return initialMapColor;
  254. }
  255. void BlockType::setGroupNames(Framework::RCArray<Framework::Text> groupNames)
  256. {
  257. this->groupNames = groupNames;
  258. }
  259. const Framework::RCArray<Framework::Text>& BlockType::getGroupNames() const
  260. {
  261. return groupNames;
  262. }
  263. void BlockType::setHardness(float hardness)
  264. {
  265. this->hardness = hardness;
  266. }
  267. float BlockType::getHardness() const
  268. {
  269. return hardness;
  270. }
  271. int BlockType::getTypeId(const char* name)
  272. {
  273. Text n = name;
  274. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  275. {
  276. if (Game::INSTANCE->zBlockType(i)
  277. && n.istGleich(Game::INSTANCE->zBlockType(i)->getName()))
  278. return Game::INSTANCE->zBlockType(i)->getId();
  279. }
  280. return 0;
  281. }
  282. Framework::Text BlockType::getTypeName(int id)
  283. {
  284. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  285. {
  286. if (Game::INSTANCE->zBlockType(i)
  287. && Game::INSTANCE->zBlockType(i)->getId() == id)
  288. return Game::INSTANCE->zBlockType(i)->getName();
  289. }
  290. return 0;
  291. }
  292. const Block* getDefaultBlock(Either<Block*, int> b)
  293. {
  294. if (b.isA())
  295. return b;
  296. else
  297. return Game::INSTANCE->zBlockType(b)->zDefault();
  298. }