World.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 - nightLength - transitionLength)
  276. / transitionLength)
  277. * 0.95f
  278. + 0.05f;
  279. }
  280. else
  281. {
  282. dayLightFactor = 1.f;
  283. }
  284. if (this->time < dayLength + transitionLength
  285. || this->time > dayLength + transitionLength + nightLength)
  286. {
  287. float currentDayTime
  288. = (float)(this->time < dayLength + transitionLength
  289. ? this->time + transitionLength
  290. : this->time - dayLength + transitionLength
  291. + nightLength);
  292. Vec3<float> sunPos = Vec3<float>(-200.f, 50.f, 0.f);
  293. sunPos.rotateY((
  294. float)((currentDayTime / (dayLength + transitionLength * 2)) * PI));
  295. sunPos.normalize();
  296. if (!renderedWorld->getDiffuseLightCount())
  297. {
  298. renderedWorld->addDiffuseLight(
  299. {-sunPos, Vec3<float>(1.0, 1.0, 1.0)});
  300. }
  301. else
  302. {
  303. renderedWorld->getDiffuseLight(0).direction = -sunPos;
  304. }
  305. }
  306. }
  307. void World::setChunk(Chunk* chunk)
  308. {
  309. zScreenPtr->lock();
  310. renderedWorld->lock();
  311. currentDimension->setChunk(chunk, chunk->getCenter());
  312. renderedWorld->unlock();
  313. zScreenPtr->unlock();
  314. }
  315. void World::thread()
  316. {
  317. new AsynchronCall("World Update", [this]() {
  318. while (client->isConnected())
  319. {
  320. zScreenPtr->lock();
  321. if (World::INSTANCE != this)
  322. {
  323. zScreenPtr->unlock();
  324. return;
  325. }
  326. zScreenPtr->unlock();
  327. update(0);
  328. Sleep(10);
  329. }
  330. std::cout << "foreground connection lost\n";
  331. });
  332. while (client->isConnected())
  333. {
  334. zScreenPtr->lock();
  335. if (World::INSTANCE != this)
  336. {
  337. zScreenPtr->unlock();
  338. return;
  339. }
  340. zScreenPtr->unlock();
  341. update(1);
  342. Sleep(10);
  343. }
  344. Sleep(1000);
  345. zScreenPtr->lock();
  346. std::cout << "background connection lost\n";
  347. menuRegister->get("game")->hide();
  348. menuRegister->get("serverSelection")->show();
  349. zScreenPtr->unlock();
  350. zScreenPtr->postAction([this]() {
  351. if (World::INSTANCE == this)
  352. {
  353. World::INSTANCE = 0;
  354. release();
  355. }
  356. });
  357. }
  358. Block* World::zBlockAt(Framework::Vec3<int> location) const
  359. {
  360. return currentDimension->zBlock(location);
  361. }
  362. Block* World::getBlockAt(Framework::Vec3<int> location) const
  363. {
  364. return currentDimension->getBlock(location);
  365. }
  366. Dimension* World::zDimension() const
  367. {
  368. return currentDimension;
  369. }
  370. void World::setVisibility(Entity* zEntity, bool visible)
  371. {
  372. renderedWorld->lock();
  373. if (visible)
  374. renderedWorld->addZeichnung(
  375. dynamic_cast<Framework::Model3D*>(zEntity->getThis()));
  376. else
  377. renderedWorld->removeZeichnung(zEntity);
  378. renderedWorld->unlock();
  379. }
  380. Framework::Punkt World::getChunkCenter(int x, int y) const
  381. {
  382. return Punkt(((x < 0 ? x + 1 : x) / CHUNK_SIZE) * CHUNK_SIZE
  383. + (x < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2,
  384. ((y < 0 ? y + 1 : y) / CHUNK_SIZE) * CHUNK_SIZE
  385. + (y < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2);
  386. }
  387. Entity* World::zEntity(int id) const
  388. {
  389. Entity* e = currentDimension->zEntity(id);
  390. if (e) return e;
  391. return 0;
  392. }
  393. Entity* World::getEntity(int id) const
  394. {
  395. Entity* e = currentDimension->getEntity(id);
  396. if (e) return e;
  397. return 0;
  398. }
  399. void World::removeEntity(int id)
  400. {
  401. currentDimension->removeEntity(id);
  402. }
  403. PlayerKam* World::zKamera() const
  404. {
  405. return kam;
  406. }
  407. int World::getCurrentPlayerId() const
  408. {
  409. return ownEntityId;
  410. }
  411. Entity* World::getCurrentPlayerEntity() const
  412. {
  413. return zEntity(ownEntityId);
  414. }
  415. void World::setTarget(Framework::Model3D* zTarget)
  416. {
  417. if (zTarget != currentTarget)
  418. {
  419. targetLock.lock();
  420. if (!zTarget)
  421. {
  422. selectionModel->setVisible(0);
  423. }
  424. else
  425. {
  426. selectionModel->setPosition(
  427. zTarget->getPos().x, zTarget->getPos().y, zTarget->getPos().z);
  428. Block* b = dynamic_cast<Block*>(zTarget);
  429. if (b && b->getPartOfModels())
  430. {
  431. selectionModel->setDestroyedState(b->getEffectPercentage());
  432. }
  433. else
  434. {
  435. selectionModel->setDestroyedState(0);
  436. }
  437. selectionModel->setVisible(1);
  438. }
  439. if (currentTarget)
  440. {
  441. currentTarget->setAmbientFactor(
  442. currentTarget->getAmbientFactor() - 0.2f);
  443. currentTarget->release();
  444. currentTarget = 0;
  445. }
  446. if (zTarget)
  447. {
  448. currentTarget
  449. = dynamic_cast<Framework::Model3D*>(zTarget->getThis());
  450. if (currentTarget)
  451. currentTarget->setAmbientFactor(
  452. currentTarget->getAmbientFactor() + 0.2f);
  453. }
  454. targetLock.unlock();
  455. }
  456. }
  457. void World::lockWorld()
  458. {
  459. renderedWorld->lock();
  460. }
  461. void World::unlockWorld()
  462. {
  463. renderedWorld->unlock();
  464. }
  465. void World::onChunkAdded(Punkt pos)
  466. {
  467. subLock.lock();
  468. int index = 0;
  469. for (Punkt p : subscriptions)
  470. {
  471. if (p == pos)
  472. {
  473. subscriptions.remove(index);
  474. break;
  475. }
  476. index++;
  477. }
  478. subLock.unlock();
  479. }
  480. Framework::Model3D* World::getCurrentTarget() const
  481. {
  482. while (targetLock.isLocked())
  483. Sleep(1);
  484. return currentTarget
  485. ? dynamic_cast<Framework::Model3D*>(currentTarget->getThis())
  486. : 0;
  487. }
  488. FactoryCraftModel* World::zSelectedEffectModel() const
  489. {
  490. return selectionModel;
  491. }
  492. Chunk* World::zChunk(Punkt center)
  493. {
  494. return currentDimension->zChunk(center);
  495. }
  496. FactoryClient* World::zClient() const
  497. {
  498. return client;
  499. }
  500. float World::getDayLightFactor() const
  501. {
  502. return dayLightFactor;
  503. }
  504. Framework::DXBuffer* World::zFallbackVertexLightBuffer()
  505. {
  506. return fallbackVertexLightBuffer;
  507. }