Block.cpp 6.7 KB

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