World.cpp 16 KB

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