BlockType.cpp 8.9 KB

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