World.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #include <Network.h>
  2. #include <Welt3D.h>
  3. #include <GraphicsApi.h>
  4. #include <iostream>
  5. #include "World.h"
  6. #include "Globals.h"
  7. #include "WorldUpdate.h"
  8. #include "Constants.h"
  9. #include "Registries.h"
  10. #include "Game.h"
  11. #include <AsynchronCall.h>
  12. using namespace Network;
  13. using namespace Framework;
  14. World* World::INSTANCE = 0;
  15. World::World(Bildschirm3D* zScreen, FactoryClient* client)
  16. : Thread(),
  17. client(client)
  18. {
  19. renderedWorld = new Welt3D();
  20. renderedWorld->addDiffuseLight(DiffuseLight{ Vec3<float>(0.5f, 0.5f, -1.f), Vec3<float>(1.f, 1.f, 1.f) });
  21. currentDimension = new Dimension();
  22. zScreenPtr = zScreen;
  23. kam = new PlayerKam(zScreen);
  24. kam->setWelt(renderedWorld);
  25. zScreen->addKamera(kam);
  26. firstMessage = 1;
  27. ownEntityId = -1;
  28. currentTarget = 0;
  29. start();
  30. }
  31. World::~World()
  32. {
  33. zScreenPtr->removeKamera(kam);
  34. currentDimension->release();
  35. if (currentTarget)
  36. currentTarget->release();
  37. client->release();
  38. }
  39. void World::update(bool background)
  40. {
  41. NetworkReader* serverMessageReader = 0;
  42. unsigned char type = 0;
  43. while (background ? serverMessageReader = client->getNextBackgroundMessage() : serverMessageReader = client->getNextForegroundMessage())
  44. {
  45. serverMessageReader->lese((char*)&type, 1);
  46. if (type == 2) // WORLD UPDATE
  47. {
  48. int id = 0;
  49. serverMessageReader->lese((char*)&id, 4);
  50. STATIC_REGISTRY(WorldUpdateType).zElement(id)->applyUpdateAndCheck(serverMessageReader);
  51. }
  52. if (type == 3) // API MESSAGE
  53. {
  54. int length;
  55. serverMessageReader->lese((char*)&length, 4);
  56. char* data = new char[length];
  57. serverMessageReader->lese(data, length);
  58. switch (data[0])
  59. {
  60. case 1: // dimension message
  61. {
  62. currentDimension->api(data + 1);
  63. break;
  64. }
  65. case 2: // gui message
  66. ((Game*)(Menu*)menuRegister->get("game"))->api(data + 1);
  67. break;
  68. case 3: // set target
  69. {
  70. switch (data[1])
  71. {
  72. case 0:
  73. setTarget(0);
  74. break;
  75. case 1:
  76. setTarget(zEntity(*(int*)(data + 2)));
  77. break;
  78. case 2:
  79. setTarget(zBlockAt(Vec3<int>(*(int*)(data + 2), *(int*)(data + 6), *(int*)(data + 10))));
  80. break;
  81. }
  82. break;
  83. }
  84. }
  85. delete[] data;
  86. // TODO: process messages
  87. }
  88. if (type == 4) // POSITION UPDATE
  89. {
  90. serverMessageReader->lese((char*)&ownEntityId, 4);
  91. kam->setEntityId(ownEntityId);
  92. Entity* p = zEntity(ownEntityId);
  93. if (p)
  94. {
  95. p->setPlayerControlled();
  96. }
  97. }
  98. client->endMessageReading(background);
  99. }
  100. client->endMessageReading(background);
  101. Entity* player = getCurrentPlayerEntity();
  102. if (player)
  103. {
  104. renderedWorld->lock();
  105. currentDimension->removeDistantChunks({ (int)player->getPos().x, (int)player->getPos().y });
  106. Punkt currentChunk = getChunkCenter((int)player->getX(), (int)player->getY());
  107. for (int x = 0; x <= CHUNK_VISIBILITY_RANGE; x++)
  108. {
  109. for (int y = 0; y <= CHUNK_VISIBILITY_RANGE; y++)
  110. {
  111. std::function<void(Punkt)> requestChunk = [this](Punkt center)
  112. {
  113. Chunk* zC = currentDimension->zChunk(center);
  114. if (!zC)
  115. {
  116. char msg[1];
  117. msg[0] = 0; // add observer and request chaunk data
  118. Punkt pos = center;
  119. subLock.lock();
  120. bool found = 0;
  121. for (Punkt p : subscriptions)
  122. {
  123. if (p == pos)
  124. {
  125. found = 1;
  126. break;
  127. }
  128. }
  129. if (!found)
  130. {
  131. client->chunkAPIRequest(pos, msg, 1);
  132. subscriptions.add(pos);
  133. }
  134. subLock.unlock();
  135. }
  136. };
  137. requestChunk(currentChunk + Punkt(x * CHUNK_SIZE, y * CHUNK_SIZE));
  138. if (y > 0)
  139. requestChunk(currentChunk + Punkt(x * CHUNK_SIZE, -y * CHUNK_SIZE));
  140. if (x > 0)
  141. {
  142. requestChunk(currentChunk + Punkt(-x * CHUNK_SIZE, y * CHUNK_SIZE));
  143. if (y > 0)
  144. requestChunk(currentChunk + Punkt(-x * CHUNK_SIZE, -y * CHUNK_SIZE));
  145. }
  146. }
  147. }
  148. renderedWorld->unlock();
  149. }
  150. }
  151. void World::setChunk(Chunk* chunk)
  152. {
  153. zScreenPtr->lock();
  154. renderedWorld->lock();
  155. currentDimension->setChunk(chunk, chunk->getCenter());
  156. renderedWorld->unlock();
  157. zScreenPtr->unlock();
  158. }
  159. void World::thread()
  160. {
  161. new AsynchronCall("World Update", [this]()
  162. {
  163. while (true)
  164. {
  165. zScreenPtr->lock();
  166. if (World::INSTANCE != this)
  167. {
  168. zScreenPtr->unlock();
  169. return;
  170. }
  171. zScreenPtr->unlock();
  172. update(0);
  173. Sleep(10);
  174. }
  175. });
  176. while (true)
  177. {
  178. zScreenPtr->lock();
  179. if (World::INSTANCE != this)
  180. {
  181. zScreenPtr->unlock();
  182. return;
  183. }
  184. zScreenPtr->unlock();
  185. update(1);
  186. Sleep(10);
  187. }
  188. }
  189. Block* World::zBlockAt(Framework::Vec3<int> location) const
  190. {
  191. return currentDimension->zBlock(location);
  192. return 0;
  193. }
  194. Block* World::getBlockAt(Framework::Vec3<int> location) const
  195. {
  196. return currentDimension->getBlock(location);
  197. return 0;
  198. }
  199. Dimension* World::zDimension() const
  200. {
  201. return currentDimension;
  202. }
  203. void World::setVisibility(Chunk* zChunk, bool visible)
  204. {
  205. renderedWorld->lock();
  206. if (visible)
  207. renderedWorld->addCollection(dynamic_cast<Framework::Model3DCollection*>(zChunk->getThis()));
  208. else
  209. renderedWorld->removeCollection(zChunk);
  210. renderedWorld->unlock();
  211. }
  212. void World::setVisibility(Entity* zEntity, bool visible)
  213. {
  214. renderedWorld->lock();
  215. if (visible)
  216. renderedWorld->addZeichnung(dynamic_cast<Framework::Model3D*>(zEntity->getThis()));
  217. else
  218. renderedWorld->removeZeichnung(zEntity);
  219. renderedWorld->unlock();
  220. }
  221. Framework::Punkt World::getChunkCenter(int x, int y) const
  222. {
  223. return Punkt(((x < 0 ? x + 1 : x) / CHUNK_SIZE) * CHUNK_SIZE + (x < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2, ((y < 0 ? y + 1 : y) / CHUNK_SIZE) * CHUNK_SIZE + (y < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2);
  224. }
  225. Entity* World::zEntity(int id) const
  226. {
  227. Entity* e = currentDimension->zEntity(id);
  228. if (e)
  229. return e;
  230. return 0;
  231. }
  232. Entity* World::getEntity(int id) const
  233. {
  234. Entity* e = currentDimension->getEntity(id);
  235. if (e)
  236. return e;
  237. return 0;
  238. }
  239. void World::removeEntity(int id)
  240. {
  241. currentDimension->removeEntity(id);
  242. }
  243. PlayerKam* World::zKamera() const
  244. {
  245. return kam;
  246. }
  247. int World::getCurrentPlayerId() const
  248. {
  249. return ownEntityId;
  250. }
  251. Entity* World::getCurrentPlayerEntity() const
  252. {
  253. return zEntity(ownEntityId);
  254. }
  255. void World::setTarget(Framework::Model3D* zTarget)
  256. {
  257. if (zTarget != currentTarget)
  258. {
  259. targetLock.lock();
  260. if (currentTarget)
  261. {
  262. currentTarget->setAmbientFactor(currentTarget->getAmbientFactor() - 0.2f);
  263. currentTarget->release();
  264. currentTarget = 0;
  265. }
  266. if (zTarget)
  267. {
  268. currentTarget = dynamic_cast<Framework::Model3D*>(zTarget->getThis());
  269. if (currentTarget)
  270. currentTarget->setAmbientFactor(currentTarget->getAmbientFactor() + 0.2f);
  271. }
  272. targetLock.unlock();
  273. }
  274. }
  275. void World::lockWorld()
  276. {
  277. renderedWorld->lock();
  278. }
  279. void World::unlockWorld()
  280. {
  281. renderedWorld->unlock();
  282. }
  283. void World::onChunkAdded(Punkt pos)
  284. {
  285. subLock.lock();
  286. int index = 0;
  287. for (Punkt p : subscriptions)
  288. {
  289. if (p == pos)
  290. {
  291. subscriptions.remove(index);
  292. break;
  293. }
  294. index++;
  295. }
  296. subLock.unlock();
  297. }
  298. Framework::Model3D* World::getCurrentTarget() const
  299. {
  300. while (targetLock.isLocked())
  301. Sleep(1);
  302. return currentTarget ? dynamic_cast<Framework::Model3D*>(currentTarget->getThis()) : 0;
  303. }
  304. Chunk* World::zChunk(Punkt center)
  305. {
  306. return currentDimension->zChunk(center);
  307. }
  308. FactoryClient* World::zClient() const
  309. {
  310. return client;
  311. }