BlockType.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #include "BlockType.h"
  2. #include "Block.h"
  3. #include "Dimension.h"
  4. #include "Game.h"
  5. #include "MultiblockStructure.h"
  6. #include "UIMLBuilder.h"
  7. using namespace Framework;
  8. BlockType::BlockType()
  9. : ReferenceCounter(),
  10. id(0),
  11. model(0),
  12. initialMaxHP(0),
  13. needsClientInstance(true),
  14. lightSource(false),
  15. needModelSubscription(false),
  16. initialMapColor(0),
  17. hardness(1.f),
  18. damagableByHand(0),
  19. itemType(0),
  20. defaultFrontDirection(Direction::NORTH),
  21. defaultBlock(0)
  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->read((char*)&zBlock->transparent, 1);
  33. zReader->read((char*)&zBlock->passable, 1);
  34. zReader->read((char*)&zBlock->hp, 4);
  35. zReader->read((char*)&zBlock->maxHP, 4);
  36. zReader->read((char*)&zBlock->hardness, 4);
  37. zReader->read((char*)&zBlock->speedModifier, 4);
  38. zReader->read((char*)&zBlock->mapColor, 4);
  39. zReader->read((char*)&zBlock->interactable, 1);
  40. zReader->read((char*)&zBlock->frontDirection, 1);
  41. int strCount;
  42. zReader->read((char*)&strCount, 4);
  43. for (int i = 0; i < strCount; i++)
  44. {
  45. __int64 id;
  46. zReader->read((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->write((char*)&zBlock->transparent, 1);
  61. zWriter->write((char*)&zBlock->passable, 1);
  62. zWriter->write((char*)&zBlock->hp, 4);
  63. zWriter->write((char*)&zBlock->maxHP, 4);
  64. zWriter->write((char*)&zBlock->hardness, 4);
  65. zWriter->write((char*)&zBlock->speedModifier, 4);
  66. zWriter->write((char*)&zBlock->mapColor, 4);
  67. zWriter->write((char*)&zBlock->interactable, 1);
  68. zWriter->write((char*)&zBlock->frontDirection, 1);
  69. int strCount = zBlock->structures.getEntryCount();
  70. zWriter->write((char*)&strCount, 4);
  71. for (MultiblockStructure* structure : zBlock->structures)
  72. {
  73. __int64 id = structure->getStructureId();
  74. zWriter->write((char*)&id, 8);
  75. }
  76. }
  77. void BlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  78. {
  79. if (zItem)
  80. {
  81. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  82. if (item)
  83. {
  84. zBlock->transparent = item->transparent;
  85. zBlock->passable = item->passable;
  86. zBlock->hardness = item->hardness;
  87. zBlock->speedModifier = item->speedModifier;
  88. zBlock->interactable = item->interactable;
  89. }
  90. }
  91. zBlock->mapColor = initialMapColor;
  92. zBlock->frontDirection = defaultFrontDirection;
  93. }
  94. void BlockType::createSuperItem(Block* zBlock, Item* zItem) const
  95. {
  96. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  97. if (item)
  98. {
  99. item->transparent = zBlock->transparent;
  100. item->passable = zBlock->passable;
  101. item->hardness = zBlock->hardness;
  102. item->speedModifier = zBlock->speedModifier;
  103. item->interactable = zBlock->interactable;
  104. }
  105. }
  106. bool BlockType::initialize(Game* zGame)
  107. {
  108. for (DropConfig* config : dropConfigs)
  109. {
  110. config->initialize();
  111. }
  112. return true;
  113. }
  114. BlockType* BlockType::initializeDefault()
  115. {
  116. if (!defaultBlock)
  117. {
  118. defaultBlock = createBlockAt({0, 0, 0}, 0, 0);
  119. }
  120. return this;
  121. }
  122. void BlockType::addDropConfig(DropConfig* config)
  123. {
  124. dropConfigs.add(config);
  125. }
  126. const Framework::RCArray<DropConfig>& BlockType::getDropConfigs() const
  127. {
  128. return dropConfigs;
  129. }
  130. const Block* BlockType::zDefault() const
  131. {
  132. return defaultBlock;
  133. }
  134. ItemType* BlockType::createItemType()
  135. {
  136. ItemType* result = createItemTypeInternal();
  137. itemType = result;
  138. return result;
  139. }
  140. int BlockType::getItemTypeId() const
  141. {
  142. return itemType ? itemType->getId() : -1;
  143. }
  144. void BlockType::writeTypeInfo(StreamWriter* zWriter) const
  145. {
  146. int id = getId();
  147. zWriter->write((char*)&id, 4);
  148. bool inst = doesNeedClientInstance();
  149. zWriter->write((char*)&inst, 1);
  150. bool sub = doesNeedModelSubscription();
  151. zWriter->write((char*)&sub, 1);
  152. bool fluid = isFluid();
  153. zWriter->write((char*)&fluid, 1);
  154. if (fluid)
  155. {
  156. char flowDist = getFlowDistance();
  157. zWriter->write(&flowDist, 1);
  158. }
  159. zWriter->write((char*)&defaultFrontDirection, 1);
  160. int maxHp = getInitialMaxHP();
  161. zWriter->write((char*)&maxHp, 4);
  162. zModel()->writeTo(zWriter);
  163. }
  164. Framework::XML::Element* BlockType::getTargetUIML() const
  165. {
  166. return UIMLBuilder::createContainer()
  167. ->addChild(UIMLBuilder::createTextAuto(name)->setID("type")->build())
  168. ->build();
  169. }
  170. Block* BlockType::loadBlock(Framework::Vec3<int> position,
  171. Framework::StreamReader* zReader,
  172. int dimensionId) const
  173. {
  174. Block* result = createBlockAt(position, dimensionId, 0);
  175. loadSuperBlock(result, zReader, dimensionId);
  176. return result;
  177. }
  178. void BlockType::saveBlock(Block* zBlock, Framework::StreamWriter* zWriter) const
  179. {
  180. saveSuperBlock(zBlock, zWriter);
  181. }
  182. Item* BlockType::getItemFromBlock(Block* zBlock) const
  183. {
  184. Item* result = createItem();
  185. if (result)
  186. {
  187. createSuperItem(zBlock, result);
  188. }
  189. return result;
  190. }
  191. Block* BlockType::createBlockAt(
  192. Framework::Vec3<int> position, int dimensionId, Item* zUsedItem) const
  193. {
  194. Block* result = createBlock(position, dimensionId);
  195. createSuperBlock(result, zUsedItem);
  196. return result;
  197. }
  198. int BlockType::getId() const
  199. {
  200. return id;
  201. }
  202. bool BlockType::isFluid() const
  203. {
  204. return false;
  205. }
  206. unsigned char BlockType::getFlowDistance() const
  207. {
  208. return 0;
  209. }
  210. void BlockType::setTypeId(int id)
  211. {
  212. this->id = id;
  213. }
  214. void BlockType::setModel(ModelInfo* model)
  215. {
  216. this->model = model;
  217. }
  218. ModelInfo* BlockType::zModel() const
  219. {
  220. return model;
  221. }
  222. void BlockType::setInitialMaxHP(int initialMaxHP)
  223. {
  224. this->initialMaxHP = initialMaxHP;
  225. }
  226. int BlockType::getInitialMaxHP() const
  227. {
  228. return initialMaxHP;
  229. }
  230. void BlockType::setNeedsClientInstance(bool needsClientInstance)
  231. {
  232. this->needsClientInstance = needsClientInstance;
  233. }
  234. bool BlockType::doesNeedClientInstance() const
  235. {
  236. return needsClientInstance;
  237. }
  238. void BlockType::setLightSource(bool lightSource)
  239. {
  240. this->lightSource = lightSource;
  241. }
  242. bool BlockType::isLightSource() const
  243. {
  244. return lightSource;
  245. }
  246. void BlockType::setName(Framework::Text name)
  247. {
  248. this->name = name;
  249. }
  250. const char* BlockType::getName() const
  251. {
  252. return name;
  253. }
  254. void BlockType::setNeedModelSubscription(bool needModelSubscription)
  255. {
  256. this->needModelSubscription = needModelSubscription;
  257. }
  258. const bool BlockType::doesNeedModelSubscription() const
  259. {
  260. return needModelSubscription;
  261. }
  262. void BlockType::setMapColor(int mapColor)
  263. {
  264. this->initialMapColor = mapColor;
  265. }
  266. int BlockType::getMapColor() const
  267. {
  268. return initialMapColor;
  269. }
  270. void BlockType::setGroupNames(Framework::RCArray<Framework::Text> groupNames)
  271. {
  272. this->groupNames = groupNames;
  273. }
  274. const Framework::RCArray<Framework::Text>& BlockType::getGroupNames() const
  275. {
  276. return groupNames;
  277. }
  278. void BlockType::setHardness(float hardness)
  279. {
  280. this->hardness = hardness;
  281. }
  282. float BlockType::getHardness() const
  283. {
  284. return hardness;
  285. }
  286. void BlockType::setDamagableByHand(bool damagableByHand)
  287. {
  288. this->damagableByHand = damagableByHand;
  289. }
  290. bool BlockType::isDamagableByHand() const
  291. {
  292. return damagableByHand;
  293. }
  294. void BlockType::addInteractionConfig(InteractionConfig* config)
  295. {
  296. interactionConfigs.add(config);
  297. }
  298. const Framework::RCArray<InteractionConfig>&
  299. BlockType::getInteractionConfigs() const
  300. {
  301. return interactionConfigs;
  302. }
  303. void BlockType::setDefaultFrontDirection(Direction defaultFrontDirection)
  304. {
  305. this->defaultFrontDirection = defaultFrontDirection;
  306. }
  307. Direction BlockType::getDefaultFrontDirection() const
  308. {
  309. return defaultFrontDirection;
  310. }
  311. int BlockType::getTypeId(const char* name)
  312. {
  313. Text n = name;
  314. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  315. {
  316. if (Game::INSTANCE->zBlockType(i)
  317. && n.isEqual(Game::INSTANCE->zBlockType(i)->getName()))
  318. return Game::INSTANCE->zBlockType(i)->getId();
  319. }
  320. return 0;
  321. }
  322. Framework::Text BlockType::getTypeName(int id)
  323. {
  324. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  325. {
  326. if (Game::INSTANCE->zBlockType(i)
  327. && Game::INSTANCE->zBlockType(i)->getId() == id)
  328. return Game::INSTANCE->zBlockType(i)->getName();
  329. }
  330. return 0;
  331. }
  332. const Block* getDefaultBlock(Either<Block*, int> b)
  333. {
  334. if (b.isA())
  335. return b;
  336. else
  337. return Game::INSTANCE->zBlockType(b)->zDefault();
  338. }