World.cpp 16 KB

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