World.cpp 16 KB

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