Block.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "Block.h"
  2. #include <Shader.h>
  3. #include <Textur.h>
  4. #include "CustomDX11API.h"
  5. #include "Globals.h"
  6. Block::Block(const BlockType* zType,
  7. Framework::Vec3<int> pos,
  8. Model3DData* model,
  9. Model3DTextur* texture,
  10. int maxHP,
  11. bool transparent,
  12. bool needRequestModelInfo,
  13. float size)
  14. : FactoryCraftModel(),
  15. zType(zType),
  16. location(pos),
  17. maxHP((float)maxHP),
  18. needRequestModelInfo(needRequestModelInfo),
  19. partOfModel(0),
  20. flowOptions(0),
  21. distanceToSource(0)
  22. {
  23. hp = (float)maxHP;
  24. memset(sideVisible, 0, 6);
  25. // Model3D::setAlpha(transparent);
  26. Model3D::setPosition(
  27. (Framework::Vec3<float>)pos + Framework::Vec3<float>{0.5f, 0.5f, 0.5f});
  28. setModelDaten(model);
  29. setModelTextur(texture);
  30. memset(lightData, 0, 6 * 6);
  31. setSize(size);
  32. }
  33. Block::~Block() {}
  34. void Block::beforeRender(
  35. GraphicsApi* api, Shader* zVertexShader, Shader* zPixelShader)
  36. {
  37. if (needRequestModelInfo)
  38. {
  39. needRequestModelInfo = 0;
  40. World::INSTANCE->zClient()->blockAPIRequest(location, "\0", 1);
  41. }
  42. FactoryCraftModel::beforeRender(api, zVertexShader, zPixelShader);
  43. }
  44. void Block::setFlow(char flowOptions, char distanceToSource) {
  45. this->flowOptions = flowOptions;
  46. this->distanceToSource = distanceToSource;
  47. }
  48. void Block::api(char* message)
  49. {
  50. switch (message[0])
  51. {
  52. case 0: // hp change
  53. {
  54. hp = *(float*)(message + 1);
  55. setDestroyedState(1 - hp / maxHP);
  56. Model3D* mdl = World::INSTANCE->getCurrentTarget();
  57. if (mdl == this)
  58. {
  59. if (partOfModel)
  60. {
  61. World::INSTANCE->zSelectedEffectModel()->setDestroyedState(
  62. 1 - hp / maxHP);
  63. }
  64. }
  65. if (mdl) mdl->release();
  66. break;
  67. }
  68. case 1: // model change
  69. {
  70. ByteArrayReader reader(message + 1, 10000, 0);
  71. ModelInfo info(&reader);
  72. setModelDaten(info.getModel());
  73. setSize(info.getSize());
  74. setModelTextur(info.getTexture());
  75. Chunk* zChunk = World::INSTANCE->zChunk(
  76. World::INSTANCE->getChunkCenter(location.x, location.y));
  77. if (zChunk)
  78. {
  79. zChunk->setModelChanged(partOfModel);
  80. }
  81. break;
  82. }
  83. case 2: // update fluid state
  84. {
  85. flowOptions = message[1];
  86. distanceToSource = message[2];
  87. Chunk* zChunk = World::INSTANCE->zChunk(
  88. World::INSTANCE->getChunkCenter(location.x, location.y));
  89. if (zChunk)
  90. {
  91. zChunk->setModelChanged(partOfModel);
  92. }
  93. }
  94. }
  95. }
  96. void Block::copyLightTo(Block* zB)
  97. {
  98. memcpy(zB->lightData, lightData, 6 * 6);
  99. memcpy(zB->sideVisible, sideVisible, 6);
  100. }
  101. void Block::setLightData(Direction dir, unsigned char* data)
  102. {
  103. memcpy(lightData + getDirectionIndex(dir) * 6, data, 6);
  104. if (data[0] | data[1] | data[2] | data[3] | data[4] | data[5])
  105. sideVisible[getDirectionIndex(dir)] = 1;
  106. else
  107. sideVisible[getDirectionIndex(dir)] = 0;
  108. Chunk* zC = World::INSTANCE->zChunk(
  109. World::INSTANCE->getChunkCenter(location.x, location.y));
  110. if (zC) zC->setLightChanged(partOfModel);
  111. }
  112. void Block::setPartOfModel(int type, bool part)
  113. {
  114. if (part)
  115. this->partOfModel |= type;
  116. else
  117. this->partOfModel &= ~type;
  118. }
  119. const unsigned char* Block::getLightData(Direction dir) const
  120. {
  121. return lightData + getDirectionIndex(dir) * 6;
  122. }
  123. __int64 Block::getMaxLight() const
  124. {
  125. unsigned char max[6];
  126. max[0] = max[1] = max[2] = max[3] = max[4] = max[5] = 0;
  127. for (int i = 0; i < 6; i++)
  128. {
  129. for (int j = 0; j < 6; j++)
  130. {
  131. if (lightData[i * 6 + j] > max[j]) max[j] = lightData[i * 6 + j];
  132. }
  133. }
  134. return ((__int64)max[0] << 24) | ((__int64)max[1] << 16)
  135. | ((__int64)max[2] << 8) | ((__int64)max[3] << 56)
  136. | ((__int64)max[4] << 48) | ((__int64)max[5] << 40);
  137. }
  138. bool Block::isVisible() const
  139. {
  140. return true;
  141. // return sideVisible[0] || sideVisible[1] || sideVisible[2] ||
  142. // sideVisible[3]
  143. // || sideVisible[4] || sideVisible[5];
  144. }
  145. Vec3<int> Block::getLocation() const
  146. {
  147. return location;
  148. }
  149. const BlockType* Block::zBlockType() const
  150. {
  151. return zType;
  152. }
  153. Skeleton* Block::zSkeleton() const
  154. {
  155. return skelett;
  156. }
  157. Text Block::printLightInfo()
  158. {
  159. Text result = "NORTH[0;-1;0](";
  160. result += (int)lightData[0];
  161. result += ",";
  162. result += (int)lightData[1];
  163. result += ",";
  164. result += (int)lightData[2];
  165. result += ";";
  166. result += (int)lightData[3];
  167. result += ",";
  168. result += (int)lightData[4];
  169. result += ",";
  170. result += (int)lightData[5];
  171. result += ")\n";
  172. result += "EAST[1;0;0](";
  173. result += (int)lightData[6];
  174. result += ",";
  175. result += (int)lightData[7];
  176. result += ",";
  177. result += (int)lightData[8];
  178. result += ";";
  179. result += (int)lightData[9];
  180. result += ",";
  181. result += (int)lightData[10];
  182. result += ",";
  183. result += (int)lightData[11];
  184. result += ")\n";
  185. result += "SOUTH[0;1;0](";
  186. result += (int)lightData[12];
  187. result += ",";
  188. result += (int)lightData[13];
  189. result += ",";
  190. result += (int)lightData[14];
  191. result += ";";
  192. result += (int)lightData[15];
  193. result += ",";
  194. result += (int)lightData[16];
  195. result += ",";
  196. result += (int)lightData[17];
  197. result += ")\n";
  198. result += "WEST[-1;0;0](";
  199. result += (int)lightData[18];
  200. result += ",";
  201. result += (int)lightData[19];
  202. result += ",";
  203. result += (int)lightData[20];
  204. result += ";";
  205. result += (int)lightData[21];
  206. result += ",";
  207. result += (int)lightData[22];
  208. result += ",";
  209. result += (int)lightData[23];
  210. result += ")\n";
  211. result += "TOP[0;0;1](";
  212. result += (int)lightData[24];
  213. result += ",";
  214. result += (int)lightData[25];
  215. result += ",";
  216. result += (int)lightData[26];
  217. result += ";";
  218. result += (int)lightData[27];
  219. result += ",";
  220. result += (int)lightData[28];
  221. result += ",";
  222. result += (int)lightData[29];
  223. result += ")\n";
  224. result += "BOTTOM[0;0;-1](";
  225. result += (int)lightData[30];
  226. result += ",";
  227. result += (int)lightData[31];
  228. result += ",";
  229. result += (int)lightData[32];
  230. result += ";";
  231. result += (int)lightData[33];
  232. result += ",";
  233. result += (int)lightData[34];
  234. result += ",";
  235. result += (int)lightData[35];
  236. result += ")\n";
  237. return result;
  238. }
  239. int Block::getPartOfModels() const
  240. {
  241. return partOfModel;
  242. }
  243. char Block::getFlowOptions() const
  244. {
  245. return flowOptions;
  246. }
  247. char Block::getDistanceToSource() const
  248. {
  249. return distanceToSource;
  250. }