BlockType.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include "BlockType.h"
  2. #include "Block.h"
  3. #include "Dimension.h"
  4. #include "Game.h"
  5. #include "MultiblockStructure.h"
  6. using namespace Framework;
  7. BlockType::BlockType()
  8. : ReferenceCounter(),
  9. id(0),
  10. model(0),
  11. initialMaxHP(0),
  12. needsClientInstance(true),
  13. lightSource(false),
  14. name(),
  15. needModelSubscription(false),
  16. initialMapColor(0),
  17. defaultBlock(0),
  18. groupNames(),
  19. hardness(1.f),
  20. damagableByHand(0)
  21. {}
  22. BlockType::~BlockType()
  23. {
  24. if (model) model->release();
  25. if (defaultBlock) defaultBlock->release();
  26. }
  27. void BlockType::loadSuperBlock(
  28. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  29. {
  30. zBlock->loadInventory(zReader);
  31. zReader->lese((char*)&zBlock->transparent, 1);
  32. zReader->lese((char*)&zBlock->passable, 1);
  33. zReader->lese((char*)&zBlock->hp, 4);
  34. zReader->lese((char*)&zBlock->maxHP, 4);
  35. zReader->lese((char*)&zBlock->hardness, 4);
  36. zReader->lese((char*)&zBlock->speedModifier, 4);
  37. zReader->lese((char*)&zBlock->mapColor, 4);
  38. zReader->lese((char*)&zBlock->interactable, 1);
  39. int strCount;
  40. zReader->lese((char*)&strCount, 4);
  41. for (int i = 0; i < strCount; i++)
  42. {
  43. __int64 id;
  44. zReader->lese((char*)&id, 8);
  45. MultiblockStructure* str
  46. = Game::INSTANCE->zDimension(dimensionId)->zStructureById(id);
  47. if (str)
  48. {
  49. zBlock->structures.add(
  50. dynamic_cast<MultiblockStructure*>(str->getThis()));
  51. }
  52. }
  53. }
  54. void BlockType::saveSuperBlock(
  55. Block* zBlock, Framework::StreamWriter* zWriter) const
  56. {
  57. zBlock->saveInventory(zWriter);
  58. zWriter->schreibe((char*)&zBlock->transparent, 1);
  59. zWriter->schreibe((char*)&zBlock->passable, 1);
  60. zWriter->schreibe((char*)&zBlock->hp, 4);
  61. zWriter->schreibe((char*)&zBlock->maxHP, 4);
  62. zWriter->schreibe((char*)&zBlock->hardness, 4);
  63. zWriter->schreibe((char*)&zBlock->speedModifier, 4);
  64. zWriter->schreibe((char*)&zBlock->mapColor, 4);
  65. zWriter->schreibe((char*)&zBlock->interactable, 1);
  66. int strCount = zBlock->structures.getEintragAnzahl();
  67. zWriter->schreibe((char*)&strCount, 4);
  68. for (MultiblockStructure* structure : zBlock->structures)
  69. {
  70. __int64 id = structure->getStructureId();
  71. zWriter->schreibe((char*)&id, 8);
  72. }
  73. }
  74. void BlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  75. {
  76. if (zItem)
  77. {
  78. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  79. if (item)
  80. {
  81. zBlock->transparent = item->transparent;
  82. zBlock->passable = item->passable;
  83. zBlock->hardness = item->hardness;
  84. zBlock->speedModifier = item->speedModifier;
  85. zBlock->interactable = item->interactable;
  86. }
  87. }
  88. zBlock->mapColor = initialMapColor;
  89. }
  90. void BlockType::createSuperItem(Block* zBlock, Item* zItem) const
  91. {
  92. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  93. if (item)
  94. {
  95. item->transparent = zBlock->transparent;
  96. item->passable = zBlock->passable;
  97. item->hardness = zBlock->hardness;
  98. item->speedModifier = zBlock->speedModifier;
  99. item->interactable = zBlock->interactable;
  100. }
  101. }
  102. bool BlockType::initialize(Game* zGame)
  103. {
  104. for (DropConfig* config : dropConfigs)
  105. {
  106. config->initialize();
  107. }
  108. return true;
  109. }
  110. BlockType* BlockType::initializeDefault()
  111. {
  112. if (!defaultBlock)
  113. {
  114. defaultBlock = createBlockAt({0, 0, 0}, 0, 0);
  115. }
  116. return this;
  117. }
  118. void BlockType::addDropConfig(DropConfig* config)
  119. {
  120. dropConfigs.add(config);
  121. }
  122. const Framework::RCArray<DropConfig>& BlockType::getDropConfigs() const
  123. {
  124. return dropConfigs;
  125. }
  126. const Block* BlockType::zDefault() const
  127. {
  128. return defaultBlock;
  129. }
  130. void BlockType::writeTypeInfo(StreamWriter* zWriter) const
  131. {
  132. int id = getId();
  133. zWriter->schreibe((char*)&id, 4);
  134. bool inst = doesNeedClientInstance();
  135. zWriter->schreibe((char*)&inst, 1);
  136. bool sub = doesNeedModelSubscription();
  137. zWriter->schreibe((char*)&sub, 1);
  138. bool fluid = isFluid();
  139. zWriter->schreibe((char*)&fluid, 1);
  140. if (fluid)
  141. {
  142. char flowDist = getFlowDistance();
  143. zWriter->schreibe(&flowDist, 1);
  144. }
  145. int maxHp = getInitialMaxHP();
  146. zWriter->schreibe((char*)&maxHp, 4);
  147. zModel()->writeTo(zWriter);
  148. }
  149. Framework::XML::Element* BlockType::getTargetUIML() const
  150. {
  151. return new Framework::XML::Element(
  152. Framework::Text(
  153. "<targetInfo><text id=\"type\" width=\"auto\" height=\"auto\">")
  154. + name + "</text></targetInfo>");
  155. }
  156. Block* BlockType::loadBlock(Framework::Vec3<int> position,
  157. Framework::StreamReader* zReader,
  158. int dimensionId) const
  159. {
  160. Block* result = createBlockAt(position, dimensionId, 0);
  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. void BlockType::addInteractionConfig(InteractionConfig* config)
  281. {
  282. interactionConfigs.add(config);
  283. }
  284. const Framework::RCArray<InteractionConfig>&
  285. BlockType::getInteractionConfigs() const
  286. {
  287. return interactionConfigs;
  288. }
  289. int BlockType::getTypeId(const char* name)
  290. {
  291. Text n = name;
  292. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  293. {
  294. if (Game::INSTANCE->zBlockType(i)
  295. && n.istGleich(Game::INSTANCE->zBlockType(i)->getName()))
  296. return Game::INSTANCE->zBlockType(i)->getId();
  297. }
  298. return 0;
  299. }
  300. Framework::Text BlockType::getTypeName(int id)
  301. {
  302. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  303. {
  304. if (Game::INSTANCE->zBlockType(i)
  305. && Game::INSTANCE->zBlockType(i)->getId() == id)
  306. return Game::INSTANCE->zBlockType(i)->getName();
  307. }
  308. return 0;
  309. }
  310. const Block* getDefaultBlock(Either<Block*, int> b)
  311. {
  312. if (b.isA())
  313. return b;
  314. else
  315. return Game::INSTANCE->zBlockType(b)->zDefault();
  316. }