World.cpp 9.9 KB

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