World.cpp 11 KB

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