World.cpp 9.6 KB

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