World.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. int old = ownEntityId;
  91. serverMessageReader->lese((char*)&ownEntityId, 4);
  92. kam->setEntityId(ownEntityId);
  93. Entity* p = zEntity(ownEntityId);
  94. if (p)
  95. p->setPlayerControlled();
  96. if (old != ownEntityId)
  97. client->sendPlayerAction("\5", 1);
  98. }
  99. client->endMessageReading(background);
  100. }
  101. client->endMessageReading(background);
  102. Entity* player = getCurrentPlayerEntity();
  103. if (player)
  104. {
  105. renderedWorld->lock();
  106. currentDimension->removeDistantChunks({ (int)player->getPos().x, (int)player->getPos().y });
  107. Punkt currentChunk = getChunkCenter((int)player->getX(), (int)player->getY());
  108. for (int x = 0; x <= CHUNK_VISIBILITY_RANGE; x++)
  109. {
  110. for (int y = 0; y <= CHUNK_VISIBILITY_RANGE; y++)
  111. {
  112. std::function<void(Punkt)> requestChunk = [this](Punkt center)
  113. {
  114. Chunk* zC = currentDimension->zChunk(center);
  115. if (!zC)
  116. {
  117. char msg[1];
  118. msg[0] = 0; // add observer and request chaunk data
  119. Punkt pos = center;
  120. subLock.lock();
  121. bool found = 0;
  122. for (Punkt p : subscriptions)
  123. {
  124. if (p == pos)
  125. {
  126. found = 1;
  127. break;
  128. }
  129. }
  130. if (!found)
  131. {
  132. client->chunkAPIRequest(pos, msg, 1);
  133. subscriptions.add(pos);
  134. }
  135. subLock.unlock();
  136. }
  137. };
  138. requestChunk(currentChunk + Punkt(x * CHUNK_SIZE, y * CHUNK_SIZE));
  139. if (y > 0)
  140. requestChunk(currentChunk + Punkt(x * CHUNK_SIZE, -y * CHUNK_SIZE));
  141. if (x > 0)
  142. {
  143. requestChunk(currentChunk + Punkt(-x * CHUNK_SIZE, y * CHUNK_SIZE));
  144. if (y > 0)
  145. requestChunk(currentChunk + Punkt(-x * CHUNK_SIZE, -y * CHUNK_SIZE));
  146. }
  147. }
  148. }
  149. renderedWorld->unlock();
  150. }
  151. }
  152. void World::setChunk(Chunk* chunk)
  153. {
  154. zScreenPtr->lock();
  155. renderedWorld->lock();
  156. currentDimension->setChunk(chunk, chunk->getCenter());
  157. renderedWorld->unlock();
  158. zScreenPtr->unlock();
  159. }
  160. void World::thread()
  161. {
  162. new AsynchronCall("World Update", [this]()
  163. {
  164. while (true)
  165. {
  166. zScreenPtr->lock();
  167. if (World::INSTANCE != this)
  168. {
  169. zScreenPtr->unlock();
  170. return;
  171. }
  172. zScreenPtr->unlock();
  173. update(0);
  174. Sleep(10);
  175. }
  176. });
  177. while (true)
  178. {
  179. zScreenPtr->lock();
  180. if (World::INSTANCE != this)
  181. {
  182. zScreenPtr->unlock();
  183. return;
  184. }
  185. zScreenPtr->unlock();
  186. update(1);
  187. Sleep(10);
  188. }
  189. }
  190. Block* World::zBlockAt(Framework::Vec3<int> location) const
  191. {
  192. return currentDimension->zBlock(location);
  193. return 0;
  194. }
  195. Block* World::getBlockAt(Framework::Vec3<int> location) const
  196. {
  197. return currentDimension->getBlock(location);
  198. return 0;
  199. }
  200. Dimension* World::zDimension() const
  201. {
  202. return currentDimension;
  203. }
  204. void World::setVisibility(Chunk* zChunk, bool visible)
  205. {
  206. renderedWorld->lock();
  207. if (visible)
  208. renderedWorld->addCollection(dynamic_cast<Framework::Model3DCollection*>(zChunk->getThis()));
  209. else
  210. renderedWorld->removeCollection(zChunk);
  211. renderedWorld->unlock();
  212. }
  213. void World::setVisibility(Entity* zEntity, bool visible)
  214. {
  215. renderedWorld->lock();
  216. if (visible)
  217. renderedWorld->addZeichnung(dynamic_cast<Framework::Model3D*>(zEntity->getThis()));
  218. else
  219. renderedWorld->removeZeichnung(zEntity);
  220. renderedWorld->unlock();
  221. }
  222. Framework::Punkt World::getChunkCenter(int x, int y) const
  223. {
  224. 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);
  225. }
  226. Entity* World::zEntity(int id) const
  227. {
  228. Entity* e = currentDimension->zEntity(id);
  229. if (e)
  230. return e;
  231. return 0;
  232. }
  233. Entity* World::getEntity(int id) const
  234. {
  235. Entity* e = currentDimension->getEntity(id);
  236. if (e)
  237. return e;
  238. return 0;
  239. }
  240. void World::removeEntity(int id)
  241. {
  242. currentDimension->removeEntity(id);
  243. }
  244. PlayerKam* World::zKamera() const
  245. {
  246. return kam;
  247. }
  248. int World::getCurrentPlayerId() const
  249. {
  250. return ownEntityId;
  251. }
  252. Entity* World::getCurrentPlayerEntity() const
  253. {
  254. return zEntity(ownEntityId);
  255. }
  256. void World::setTarget(Framework::Model3D* zTarget)
  257. {
  258. if (zTarget != currentTarget)
  259. {
  260. targetLock.lock();
  261. if (currentTarget)
  262. {
  263. currentTarget->setAmbientFactor(currentTarget->getAmbientFactor() - 0.2f);
  264. currentTarget->release();
  265. currentTarget = 0;
  266. }
  267. if (zTarget)
  268. {
  269. currentTarget = dynamic_cast<Framework::Model3D*>(zTarget->getThis());
  270. if (currentTarget)
  271. currentTarget->setAmbientFactor(currentTarget->getAmbientFactor() + 0.2f);
  272. }
  273. targetLock.unlock();
  274. }
  275. }
  276. void World::lockWorld()
  277. {
  278. renderedWorld->lock();
  279. }
  280. void World::unlockWorld()
  281. {
  282. renderedWorld->unlock();
  283. }
  284. void World::onChunkAdded(Punkt pos)
  285. {
  286. subLock.lock();
  287. int index = 0;
  288. for (Punkt p : subscriptions)
  289. {
  290. if (p == pos)
  291. {
  292. subscriptions.remove(index);
  293. break;
  294. }
  295. index++;
  296. }
  297. subLock.unlock();
  298. }
  299. Framework::Model3D* World::getCurrentTarget() const
  300. {
  301. while (targetLock.isLocked())
  302. Sleep(1);
  303. return currentTarget ? dynamic_cast<Framework::Model3D*>(currentTarget->getThis()) : 0;
  304. }
  305. Chunk* World::zChunk(Punkt center)
  306. {
  307. return currentDimension->zChunk(center);
  308. }
  309. FactoryClient* World::zClient() const
  310. {
  311. return client;
  312. }