BlockType.cpp 8.0 KB

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