Block.cpp 6.8 KB

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