Block.cpp 7.2 KB

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