World.cpp 10 KB

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