World.cpp 7.3 KB

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