Block.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. flowOptions(0),
  20. distanceToSource(0)
  21. {
  22. hp = (float)maxHP;
  23. memset(sideVisible, 0, 6);
  24. // Model3D::setAlpha(transparent);
  25. Model3D::setPosition(
  26. (Framework::Vec3<float>)pos + Framework::Vec3<float>{0.5f, 0.5f, 0.5f});
  27. setModelDaten(model);
  28. setModelTextur(texture);
  29. memset(lightData, 0, 6 * 6);
  30. }
  31. Block::~Block() {}
  32. void Block::beforeRender(
  33. GraphicsApi* api, Shader* zVertexShader, Shader* zPixelShader)
  34. {
  35. if (needRequestModelInfo)
  36. {
  37. needRequestModelInfo = 0;
  38. World::INSTANCE->zClient()->blockAPIRequest(location, "\0", 1);
  39. }
  40. FactoryCraftModel::beforeRender(api, zVertexShader, zPixelShader);
  41. }
  42. void Block::setFlow(char flowOptions, char distanceToSource) {
  43. this->flowOptions = flowOptions;
  44. this->distanceToSource = distanceToSource;
  45. }
  46. void Block::api(char* message)
  47. {
  48. switch (message[0])
  49. {
  50. case 0: // hp change
  51. {
  52. hp = *(float*)(message + 1);
  53. setDestroyedState(1 - hp / maxHP);
  54. Model3D* mdl = World::INSTANCE->getCurrentTarget();
  55. if (mdl == this)
  56. {
  57. if (partOfModel)
  58. {
  59. World::INSTANCE->zSelectedEffectModel()->setDestroyedState(
  60. 1 - hp / maxHP);
  61. }
  62. }
  63. if (mdl) mdl->release();
  64. break;
  65. }
  66. case 1: // model change
  67. {
  68. ByteArrayReader reader(message + 1, 10000, 0);
  69. ModelInfo info(&reader);
  70. setModelDaten(info.getModel());
  71. setModelTextur(info.getTexture());
  72. Chunk* zChunk = World::INSTANCE->zChunk(
  73. World::INSTANCE->getChunkCenter(location.x, location.y));
  74. if (zChunk)
  75. {
  76. zChunk->setModelChanged(partOfModel);
  77. }
  78. break;
  79. }
  80. case 2: // update fluid state
  81. {
  82. flowOptions = message[1];
  83. distanceToSource = message[2];
  84. Chunk* zChunk = World::INSTANCE->zChunk(
  85. World::INSTANCE->getChunkCenter(location.x, location.y));
  86. if (zChunk)
  87. {
  88. zChunk->setModelChanged(partOfModel);
  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. char Block::getFlowOptions() const
  241. {
  242. return flowOptions;
  243. }
  244. char Block::getDistanceToSource() const
  245. {
  246. return distanceToSource;
  247. }