Block.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. {}
  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::api(char* message)
  41. {
  42. switch (message[0])
  43. {
  44. case 0: // hp change
  45. {
  46. hp = *(float*)(message + 1);
  47. setDestroyedState(1 - hp / maxHP);
  48. Model3D* mdl = World::INSTANCE->getCurrentTarget();
  49. if (mdl == this)
  50. {
  51. if (partOfGround)
  52. {
  53. World::INSTANCE->zSelectedEffectModel()->setDestroyedState(
  54. 1 - hp / maxHP);
  55. }
  56. }
  57. if (mdl) mdl->release();
  58. break;
  59. }
  60. case 1: // model change
  61. {
  62. bool affectsGround = partOfGround;
  63. ByteArrayReader reader(message + 1, 10000, 0);
  64. ModelInfo info(&reader);
  65. setModelDaten(info.getModel());
  66. setModelTextur(info.getTexture());
  67. affectsGround |= info.getModelName().istGleich("cube")
  68. || info.getModelName().istGleich("grass");
  69. if (affectsGround)
  70. {
  71. Chunk* zChunk = World::INSTANCE->zChunk(
  72. World::INSTANCE->getChunkCenter(location.x, location.y));
  73. if (zChunk)
  74. {
  75. zChunk->setGroundChanged();
  76. }
  77. }
  78. break;
  79. }
  80. case 2: // update fluid fill state
  81. {
  82. short fluidAmount = *(short*)(message + 1);
  83. if (this->skelett)
  84. {
  85. float z = (float)fluidAmount / 1000.f;
  86. Bone* b = this->skelett->zRootBone()->zFirstChild();
  87. b->setPosition(Vec3<float>(0.f, 0.f, z - 1));
  88. b = b->zFirstSibling();
  89. b->setPosition(Vec3<float>(0.f, 0.f, z - 1));
  90. b = b->zFirstSibling();
  91. b->setPosition(Vec3<float>(0.f, 0.f, z - 1));
  92. b = b->zFirstSibling();
  93. b->setPosition(Vec3<float>(0.f, 0.f, z - 1));
  94. }
  95. }
  96. }
  97. }
  98. void Block::copyLightTo(Block* zB)
  99. {
  100. memcpy(zB->lightData, lightData, 6 * 6);
  101. memcpy(zB->sideVisible, sideVisible, 6);
  102. }
  103. void Block::setLightData(Direction dir, unsigned char* data)
  104. {
  105. memcpy(lightData + getDirectionIndex(dir) * 6, data, 6);
  106. if (data[0] | data[1] | data[2] | data[3] | data[4] | data[5])
  107. sideVisible[getDirectionIndex(dir)] = 1;
  108. else
  109. sideVisible[getDirectionIndex(dir)] = 0;
  110. Chunk* zC = World::INSTANCE->zChunk(
  111. World::INSTANCE->getChunkCenter(location.x, location.y));
  112. if (zC) zC->setLightChanged();
  113. }
  114. void Block::setPartOfGround(bool partOfGround)
  115. {
  116. this->partOfGround = partOfGround;
  117. }
  118. const unsigned char* Block::getLightData(Direction dir) const
  119. {
  120. return lightData + getDirectionIndex(dir) * 6;
  121. }
  122. __int64 Block::getMaxLight() const {
  123. unsigned char max[6];
  124. max[0] = max[1] = max[2] = max[3] = max[4] = max[5] = 0;
  125. for (int i = 0; i < 6; i++)
  126. {
  127. for (int j = 0; j < 6; j++)
  128. {
  129. if (lightData[i * 6 + j] > max[j]) max[j] = lightData[i * 6 + j];
  130. }
  131. }
  132. return ((__int64)max[0] << 24) | ((__int64)max[1] << 16)
  133. | ((__int64)max[2] << 8) | ((__int64)max[3] << 56)
  134. | ((__int64)max[4] << 48) | ((__int64)max[5] << 40);
  135. }
  136. bool Block::isVisible() const
  137. {
  138. return true;
  139. //return sideVisible[0] || sideVisible[1] || sideVisible[2] || 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. bool Block::isPartOfGround() const
  237. {
  238. return partOfGround;
  239. }