World.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. #include "World.h"
  2. #include <AsynchronCall.h>
  3. #include <DX12Buffer.h>
  4. #include <GraphicsApi.h>
  5. #include <iostream>
  6. #include <Network.h>
  7. #include <Welt3D.h>
  8. #include "Constants.h"
  9. #include "Game.h"
  10. #include "Globals.h"
  11. #include "Registries.h"
  12. #include "WorldUpdate.h"
  13. using namespace Network;
  14. using namespace Framework;
  15. World* World::INSTANCE = 0;
  16. World::World(Bildschirm3D* zScreen, FactoryClient* client)
  17. : Thread(),
  18. client(client)
  19. {
  20. renderedWorld = new Welt3D();
  21. renderedWorld->addDiffuseLight(DiffuseLight{
  22. Vec3<float>(0.5f, 0.5f, -1.f), Vec3<float>(1.f, 1.f, 1.f)});
  23. currentDimension = new Dimension();
  24. renderedWorld->addCollection(
  25. dynamic_cast<Model3DCollection*>(currentDimension->getThis()));
  26. zScreenPtr = zScreen;
  27. kam = new PlayerKam(zScreen);
  28. kam->setWelt(renderedWorld);
  29. zScreen->addKamera(kam);
  30. firstMessage = 1;
  31. ownEntityId = -1;
  32. currentTarget = 0;
  33. dayLightFactor = 1;
  34. time = 0;
  35. dayLength = 1000;
  36. transitionLength = 0;
  37. nightLength = 0;
  38. fallbackVertexLightBuffer
  39. = zScreen->zGraphicsApi()->createStructuredBuffer(16);
  40. char data[16];
  41. memset(data, 0, 16);
  42. *(int*)data = 0xFFFFFF00;
  43. fallbackVertexLightBuffer->setData(data);
  44. fallbackVertexLightBuffer->setLength(16);
  45. fallbackVertexLightBuffer->copieren();
  46. selectionModel = new FactoryCraftModel();
  47. selectionModel->setModelDaten(zScreen->zGraphicsApi()->getModel("cube"));
  48. selectionModel->setSize(1.005f);
  49. selectionModel->setVisible(0);
  50. selectionModel->setModelTextur(new Model3DTextur());
  51. for (int i = 0; i < 6; i++)
  52. {
  53. selectionModel->zTextur()->setPolygonTextur(i,
  54. zScreen->zGraphicsApi()->createOrGetTextur(
  55. "blocks.ltdb/selectedblock.p"));
  56. }
  57. selectionModel->setAlpha(1);
  58. selectionModel->setUseEffectAlpha(1);
  59. renderedWorld->addZeichnung(selectionModel);
  60. start();
  61. }
  62. World::~World()
  63. {
  64. zScreenPtr->removeKamera(kam);
  65. currentDimension->release();
  66. if (currentTarget) currentTarget->release();
  67. client->release();
  68. World::INSTANCE = 0;
  69. }
  70. void World::update(bool background)
  71. {
  72. NetworkReader* serverMessageReader = 0;
  73. unsigned char type = 0;
  74. while (background
  75. ? serverMessageReader = client->getNextBackgroundMessage()
  76. : serverMessageReader = client->getNextForegroundMessage())
  77. {
  78. serverMessageReader->lese((char*)&type, 1);
  79. if (type == 2) // WORLD UPDATE
  80. {
  81. int id = 0;
  82. serverMessageReader->lese((char*)&id, 4);
  83. STATIC_REGISTRY(WorldUpdateType)
  84. .zElement(id)
  85. ->applyUpdateAndCheck(serverMessageReader);
  86. }
  87. if (type == 3) // API MESSAGE
  88. {
  89. int length;
  90. serverMessageReader->lese((char*)&length, 4);
  91. char* data = new char[length];
  92. serverMessageReader->lese(data, length);
  93. switch (data[0])
  94. {
  95. case 1: // dimension message
  96. {
  97. int dimId = *(int*)(data + 1);
  98. if (dimId == currentDimension->getId())
  99. {
  100. currentDimension->api(data + 5);
  101. }
  102. break;
  103. }
  104. case 2: // gui message
  105. ((Game*)(Menu*)menuRegister->get("game"))->api(data + 1);
  106. break;
  107. case 3: // set target
  108. {
  109. switch (data[1])
  110. {
  111. case 0:
  112. setTarget(0);
  113. ((Game*)(Menu*)menuRegister->get("game"))
  114. ->setTargetUIML("");
  115. break;
  116. case 1:
  117. setTarget(zEntity(*(int*)(data + 2)));
  118. ((Game*)(Menu*)menuRegister->get("game"))
  119. ->setTargetUIML("");
  120. break;
  121. case 2:
  122. setTarget(zBlockAt(Vec3<int>(*(int*)(data + 2),
  123. *(int*)(data + 6),
  124. *(int*)(data + 10))));
  125. int side = *(int*)(data + 14);
  126. short len = *(short*)(data + 18);
  127. char* uiml = new char[len + 1];
  128. memcpy(uiml, data + 20, len);
  129. uiml[len] = 0;
  130. ((Game*)(Menu*)menuRegister->get("game"))
  131. ->setTargetUIML(uiml);
  132. delete[] uiml;
  133. break;
  134. }
  135. break;
  136. }
  137. case 4: // chat message
  138. {
  139. ((Game*)(Menu*)menuRegister->get("game"))
  140. ->zChat()
  141. ->addMessage(data + 1);
  142. break;
  143. }
  144. case 5: // chat options
  145. {
  146. ((Game*)(Menu*)menuRegister->get("game"))
  147. ->zChat()
  148. ->initOptions(data + 1);
  149. break;
  150. }
  151. case 6: // map data
  152. {
  153. ByteArrayReader reader(data + 1, length - 1, 0);
  154. ChunkMap* map = new ChunkMap(&reader);
  155. ((Game*)(Menu*)menuRegister->get("game"))
  156. ->zMap()
  157. ->addChunk(map);
  158. break;
  159. }
  160. case 7: // map player data
  161. {
  162. ((Game*)(Menu*)menuRegister->get("game"))
  163. ->zMap()
  164. ->updatePlayerData(data + 1);
  165. break;
  166. }
  167. case 8: // time sync
  168. {
  169. nightLength = *(double*)(data + 9);
  170. transitionLength = *(double*)(data + 17);
  171. dayLength = *(double*)(data + 25);
  172. time = *(double*)(data + 1);
  173. break;
  174. }
  175. }
  176. delete[] data;
  177. }
  178. if (type == 4) // POSITION UPDATE
  179. {
  180. int old = ownEntityId;
  181. serverMessageReader->lese((char*)&ownEntityId, 4);
  182. int dimensionId = 0;
  183. serverMessageReader->lese((char*)&dimensionId, 4);
  184. currentDimension->setId(dimensionId);
  185. kam->setEntityId(ownEntityId);
  186. Entity* p = zEntity(ownEntityId);
  187. if (p) p->setPlayerControlled();
  188. if (old != ownEntityId) client->sendPlayerAction("\5", 1);
  189. }
  190. client->endMessageReading(background);
  191. }
  192. client->endMessageReading(background);
  193. Entity* player = getCurrentPlayerEntity();
  194. if (player)
  195. {
  196. renderedWorld->lock();
  197. currentDimension->removeDistantChunks(
  198. {(int)player->getPos().x, (int)player->getPos().y});
  199. Punkt currentChunk
  200. = getChunkCenter((int)player->getX(), (int)player->getY());
  201. for (int x = 0; x <= CHUNK_VISIBILITY_RANGE; x++)
  202. {
  203. for (int y = 0; y <= CHUNK_VISIBILITY_RANGE; y++)
  204. {
  205. std::function<void(Punkt)> requestChunk = [this](Punkt center) {
  206. Chunk* zC = currentDimension->zChunk(center);
  207. if (!zC)
  208. {
  209. char msg[1];
  210. msg[0] = 0; // add observer and request chaunk data
  211. Punkt pos = center;
  212. subLock.lock();
  213. bool found = 0;
  214. for (Punkt p : subscriptions)
  215. {
  216. if (p == pos)
  217. {
  218. found = 1;
  219. break;
  220. }
  221. }
  222. if (!found)
  223. {
  224. client->chunkAPIRequest(pos, msg, 1);
  225. subscriptions.add(pos);
  226. }
  227. subLock.unlock();
  228. }
  229. };
  230. requestChunk(
  231. currentChunk + Punkt(x * CHUNK_SIZE, y * CHUNK_SIZE));
  232. if (y > 0)
  233. requestChunk(
  234. currentChunk + Punkt(x * CHUNK_SIZE, -y * CHUNK_SIZE));
  235. if (x > 0)
  236. {
  237. requestChunk(
  238. currentChunk + Punkt(-x * CHUNK_SIZE, y * CHUNK_SIZE));
  239. if (y > 0)
  240. requestChunk(currentChunk
  241. + Punkt(-x * CHUNK_SIZE, -y * CHUNK_SIZE));
  242. }
  243. }
  244. }
  245. renderedWorld->unlock();
  246. }
  247. }
  248. void World::onTick(double time)
  249. {
  250. selectionModel->tick(0.1);
  251. this->time += time;
  252. if (this->time >= dayLength + nightLength + transitionLength * 2)
  253. {
  254. this->time -= dayLength + nightLength + transitionLength * 2;
  255. }
  256. if (this->time > dayLength && this->time < dayLength + transitionLength)
  257. {
  258. dayLightFactor
  259. = 1.f
  260. - (float)((this->time - dayLength) / transitionLength) * 0.95f;
  261. }
  262. else if (this->time > dayLength + transitionLength
  263. && this->time < dayLength + transitionLength + nightLength)
  264. {
  265. // night
  266. dayLightFactor = 0.05f;
  267. if (renderedWorld->getDiffuseLightCount() > 0)
  268. {
  269. renderedWorld->removeDiffuseLight(0);
  270. }
  271. }
  272. else if (this->time > dayLength + transitionLength + nightLength)
  273. {
  274. dayLightFactor
  275. = (float)((this->time - dayLength) / transitionLength) * 0.95f
  276. + 0.05f;
  277. }
  278. else
  279. {
  280. dayLightFactor = 1.f;
  281. }
  282. if (this->time < dayLength + transitionLength
  283. || this->time > dayLength + transitionLength + nightLength)
  284. {
  285. float currentDayTime
  286. = (float)(this->time < dayLength + transitionLength
  287. ? this->time + transitionLength
  288. : this->time - dayLength + transitionLength
  289. + nightLength);
  290. Vec3<float> sunPos = Vec3<float>(-200.f, 50.f, 0.f);
  291. sunPos.rotateY((
  292. float)((currentDayTime / (dayLength + transitionLength * 2)) * PI));
  293. sunPos.normalize();
  294. if (!renderedWorld->getDiffuseLightCount())
  295. {
  296. renderedWorld->addDiffuseLight(
  297. {-sunPos, Vec3<float>(1.0, 1.0, 1.0)});
  298. }
  299. else
  300. {
  301. renderedWorld->getDiffuseLight(0).direction = -sunPos;
  302. }
  303. }
  304. }
  305. void World::setChunk(Chunk* chunk)
  306. {
  307. zScreenPtr->lock();
  308. renderedWorld->lock();
  309. currentDimension->setChunk(chunk, chunk->getCenter());
  310. renderedWorld->unlock();
  311. zScreenPtr->unlock();
  312. }
  313. void World::thread()
  314. {
  315. new AsynchronCall("World Update", [this]() {
  316. while (client->isConnected())
  317. {
  318. zScreenPtr->lock();
  319. if (World::INSTANCE != this)
  320. {
  321. zScreenPtr->unlock();
  322. return;
  323. }
  324. zScreenPtr->unlock();
  325. update(0);
  326. Sleep(10);
  327. }
  328. std::cout << "foreground connection lost\n";
  329. });
  330. while (client->isConnected())
  331. {
  332. zScreenPtr->lock();
  333. if (World::INSTANCE != this)
  334. {
  335. zScreenPtr->unlock();
  336. return;
  337. }
  338. zScreenPtr->unlock();
  339. update(1);
  340. Sleep(10);
  341. }
  342. Sleep(1000);
  343. zScreenPtr->lock();
  344. std::cout << "background connection lost\n";
  345. menuRegister->get("game")->hide();
  346. menuRegister->get("serverSelection")->show();
  347. release();
  348. zScreenPtr->unlock();
  349. if (World::INSTANCE == this)
  350. {
  351. World::INSTANCE = 0;
  352. }
  353. }
  354. Block* World::zBlockAt(Framework::Vec3<int> location) const
  355. {
  356. return currentDimension->zBlock(location);
  357. return 0;
  358. }
  359. Block* World::getBlockAt(Framework::Vec3<int> location) const
  360. {
  361. return currentDimension->getBlock(location);
  362. return 0;
  363. }
  364. Dimension* World::zDimension() const
  365. {
  366. return currentDimension;
  367. }
  368. void World::setVisibility(Entity* zEntity, bool visible)
  369. {
  370. renderedWorld->lock();
  371. if (visible)
  372. renderedWorld->addZeichnung(
  373. dynamic_cast<Framework::Model3D*>(zEntity->getThis()));
  374. else
  375. renderedWorld->removeZeichnung(zEntity);
  376. renderedWorld->unlock();
  377. }
  378. Framework::Punkt World::getChunkCenter(int x, int y) const
  379. {
  380. return Punkt(((x < 0 ? x + 1 : x) / CHUNK_SIZE) * CHUNK_SIZE
  381. + (x < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2,
  382. ((y < 0 ? y + 1 : y) / CHUNK_SIZE) * CHUNK_SIZE
  383. + (y < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2);
  384. }
  385. Entity* World::zEntity(int id) const
  386. {
  387. Entity* e = currentDimension->zEntity(id);
  388. if (e) return e;
  389. return 0;
  390. }
  391. Entity* World::getEntity(int id) const
  392. {
  393. Entity* e = currentDimension->getEntity(id);
  394. if (e) return e;
  395. return 0;
  396. }
  397. void World::removeEntity(int id)
  398. {
  399. currentDimension->removeEntity(id);
  400. }
  401. PlayerKam* World::zKamera() const
  402. {
  403. return kam;
  404. }
  405. int World::getCurrentPlayerId() const
  406. {
  407. return ownEntityId;
  408. }
  409. Entity* World::getCurrentPlayerEntity() const
  410. {
  411. return zEntity(ownEntityId);
  412. }
  413. void World::setTarget(Framework::Model3D* zTarget)
  414. {
  415. if (zTarget != currentTarget)
  416. {
  417. targetLock.lock();
  418. if (!zTarget)
  419. {
  420. selectionModel->setVisible(0);
  421. }
  422. else
  423. {
  424. selectionModel->setPosition(
  425. zTarget->getPos().x, zTarget->getPos().y, zTarget->getPos().z);
  426. Block* b = dynamic_cast<Block*>(zTarget);
  427. if (b && b->getPartOfModels())
  428. {
  429. selectionModel->setDestroyedState(b->getEffectPercentage());
  430. }
  431. else
  432. {
  433. selectionModel->setDestroyedState(0);
  434. }
  435. selectionModel->setVisible(1);
  436. }
  437. if (currentTarget)
  438. {
  439. currentTarget->setAmbientFactor(
  440. currentTarget->getAmbientFactor() - 0.2f);
  441. currentTarget->release();
  442. currentTarget = 0;
  443. }
  444. if (zTarget)
  445. {
  446. currentTarget
  447. = dynamic_cast<Framework::Model3D*>(zTarget->getThis());
  448. if (currentTarget)
  449. currentTarget->setAmbientFactor(
  450. currentTarget->getAmbientFactor() + 0.2f);
  451. }
  452. targetLock.unlock();
  453. }
  454. }
  455. void World::lockWorld()
  456. {
  457. renderedWorld->lock();
  458. }
  459. void World::unlockWorld()
  460. {
  461. renderedWorld->unlock();
  462. }
  463. void World::onChunkAdded(Punkt pos)
  464. {
  465. subLock.lock();
  466. int index = 0;
  467. for (Punkt p : subscriptions)
  468. {
  469. if (p == pos)
  470. {
  471. subscriptions.remove(index);
  472. break;
  473. }
  474. index++;
  475. }
  476. subLock.unlock();
  477. }
  478. Framework::Model3D* World::getCurrentTarget() const
  479. {
  480. while (targetLock.isLocked())
  481. Sleep(1);
  482. return currentTarget
  483. ? dynamic_cast<Framework::Model3D*>(currentTarget->getThis())
  484. : 0;
  485. }
  486. FactoryCraftModel* World::zSelectedEffectModel() const
  487. {
  488. return selectionModel;
  489. }
  490. Chunk* World::zChunk(Punkt center)
  491. {
  492. return currentDimension->zChunk(center);
  493. }
  494. FactoryClient* World::zClient() const
  495. {
  496. return client;
  497. }
  498. float World::getDayLightFactor() const
  499. {
  500. return dayLightFactor;
  501. }
  502. Framework::DXBuffer* World::zFallbackVertexLightBuffer()
  503. {
  504. return fallbackVertexLightBuffer;
  505. }