World.cpp 7.0 KB

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