World.cpp 6.6 KB

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