World.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. });
  197. while (client->isConnected())
  198. {
  199. zScreenPtr->lock();
  200. if (World::INSTANCE != this)
  201. {
  202. zScreenPtr->unlock();
  203. return;
  204. }
  205. zScreenPtr->unlock();
  206. update(1);
  207. Sleep(10);
  208. }
  209. }
  210. Block* World::zBlockAt(Framework::Vec3<int> location) const
  211. {
  212. return currentDimension->zBlock(location);
  213. return 0;
  214. }
  215. Block* World::getBlockAt(Framework::Vec3<int> location) const
  216. {
  217. return currentDimension->getBlock(location);
  218. return 0;
  219. }
  220. Dimension* World::zDimension() const
  221. {
  222. return currentDimension;
  223. }
  224. void World::setVisibility(Chunk* zChunk, bool visible)
  225. {
  226. renderedWorld->lock();
  227. if (visible)
  228. renderedWorld->addCollection(
  229. dynamic_cast<Framework::Model3DCollection*>(zChunk->getThis()));
  230. else
  231. renderedWorld->removeCollection(zChunk);
  232. renderedWorld->unlock();
  233. }
  234. void World::setVisibility(Entity* zEntity, bool visible)
  235. {
  236. renderedWorld->lock();
  237. if (visible)
  238. renderedWorld->addZeichnung(
  239. dynamic_cast<Framework::Model3D*>(zEntity->getThis()));
  240. else
  241. renderedWorld->removeZeichnung(zEntity);
  242. renderedWorld->unlock();
  243. }
  244. Framework::Punkt World::getChunkCenter(int x, int y) const
  245. {
  246. return Punkt(((x < 0 ? x + 1 : x) / CHUNK_SIZE) * CHUNK_SIZE
  247. + (x < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2,
  248. ((y < 0 ? y + 1 : y) / CHUNK_SIZE) * CHUNK_SIZE
  249. + (y < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2);
  250. }
  251. Entity* World::zEntity(int id) const
  252. {
  253. Entity* e = currentDimension->zEntity(id);
  254. if (e) return e;
  255. return 0;
  256. }
  257. Entity* World::getEntity(int id) const
  258. {
  259. Entity* e = currentDimension->getEntity(id);
  260. if (e) return e;
  261. return 0;
  262. }
  263. void World::removeEntity(int id)
  264. {
  265. currentDimension->removeEntity(id);
  266. }
  267. PlayerKam* World::zKamera() const
  268. {
  269. return kam;
  270. }
  271. int World::getCurrentPlayerId() const
  272. {
  273. return ownEntityId;
  274. }
  275. Entity* World::getCurrentPlayerEntity() const
  276. {
  277. return zEntity(ownEntityId);
  278. }
  279. void World::setTarget(Framework::Model3D* zTarget)
  280. {
  281. if (zTarget != currentTarget)
  282. {
  283. targetLock.lock();
  284. if (currentTarget)
  285. {
  286. currentTarget->setAmbientFactor(
  287. currentTarget->getAmbientFactor() - 0.2f);
  288. currentTarget->release();
  289. currentTarget = 0;
  290. }
  291. if (zTarget)
  292. {
  293. currentTarget
  294. = dynamic_cast<Framework::Model3D*>(zTarget->getThis());
  295. if (currentTarget)
  296. currentTarget->setAmbientFactor(
  297. currentTarget->getAmbientFactor() + 0.2f);
  298. }
  299. targetLock.unlock();
  300. }
  301. }
  302. void World::lockWorld()
  303. {
  304. renderedWorld->lock();
  305. }
  306. void World::unlockWorld()
  307. {
  308. renderedWorld->unlock();
  309. }
  310. void World::onChunkAdded(Punkt pos)
  311. {
  312. subLock.lock();
  313. int index = 0;
  314. for (Punkt p : subscriptions)
  315. {
  316. if (p == pos)
  317. {
  318. subscriptions.remove(index);
  319. break;
  320. }
  321. index++;
  322. }
  323. subLock.unlock();
  324. }
  325. Framework::Model3D* World::getCurrentTarget() const
  326. {
  327. while (targetLock.isLocked())
  328. Sleep(1);
  329. return currentTarget
  330. ? dynamic_cast<Framework::Model3D*>(currentTarget->getThis())
  331. : 0;
  332. }
  333. Chunk* World::zChunk(Punkt center)
  334. {
  335. return currentDimension->zChunk(center);
  336. }
  337. FactoryClient* World::zClient() const
  338. {
  339. return client;
  340. }