WorldLoader.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <Punkt.h>
  2. #include <Text.h>
  3. #include "WorldLoader.h"
  4. #include "Game.h"
  5. #include "AddChunkUpdate.h"
  6. using namespace Framework;
  7. WorldLoader::WorldLoader()
  8. : Thread(),
  9. exit(0)
  10. {
  11. setName("World Loader");
  12. Datei d;
  13. d.setDatei(Game::INSTANCE->getWorldDirectory() + "/dim");
  14. RCArray<Text>* names = d.getDateiListe();
  15. if (names)
  16. {
  17. for (Text* name : *names)
  18. {
  19. Datei entities;
  20. entities.setDatei(Game::INSTANCE->getWorldDirectory() + "/dim/" + Text(name->getText()) + "/entities");
  21. if (entities.open(Datei::Style::lesen))
  22. {
  23. Dimension* dim = new Dimension(*name);
  24. while (!entities.istEnde())
  25. {
  26. int type = 0;
  27. entities.lese((char*)&type, 4);
  28. dim->addEntity(StaticRegistry<EntityType>::INSTANCE.zElement(type)->loadEntity(&entities));
  29. }
  30. Game::INSTANCE->addDimension(dim);
  31. }
  32. }
  33. names->release();
  34. }
  35. start();
  36. }
  37. WorldLoader::~WorldLoader()
  38. {}
  39. void WorldLoader::thread()
  40. {
  41. while (!exit)
  42. {
  43. cs.lock();
  44. Area next;
  45. bool hasNext = 0;
  46. if (requestQueue.getEintragAnzahl() > 0)
  47. {
  48. next = requestQueue.get(0);
  49. requestQueue.remove(0);
  50. hasNext = 1;
  51. }
  52. cs.unlock();
  53. if (!hasNext)
  54. {
  55. Sleep(1000);
  56. continue;
  57. }
  58. Punkt start = Game::INSTANCE->getChunkCenter(next.startX, next.startY);
  59. Punkt end = Game::INSTANCE->getChunkCenter(next.endX, next.endY);
  60. int xDir = start.x > end.x ? -1 : 1;
  61. int yDir = start.y > end.y ? -1 : 1;
  62. for (int x = start.x; xDir < 0 ? x >= end.x : x <= end.x; x += CHUNK_SIZE * xDir)
  63. {
  64. for (int y = start.y; yDir < 0 ? y >= end.y : y <= end.y; y += CHUNK_SIZE * yDir)
  65. {
  66. if (!Game::INSTANCE->isChunkLoaded(x, y, next.dimensionId))
  67. {
  68. Datei* file = new Datei();
  69. Text filePath = Game::INSTANCE->getWorldDirectory() + "/dim/" + next.dimensionId + "/";
  70. filePath.appendHex(x);
  71. filePath += "_";
  72. filePath.appendHex(y);
  73. filePath += ".chunk";
  74. file->setDatei(filePath);
  75. if (file->open(Datei::Style::lesen))
  76. {
  77. Chunk* chunk = new Chunk(Framework::Punkt(x, y), next.dimensionId, file);
  78. Game::INSTANCE->requestWorldUpdate(new AddChunkUpdate(chunk));
  79. }
  80. file->close();
  81. file->release();
  82. }
  83. }
  84. }
  85. }
  86. }
  87. void WorldLoader::requestLoading(Area request)
  88. {
  89. cs.lock();
  90. requestQueue.add(request);
  91. cs.unlock();
  92. }
  93. void WorldLoader::exitAndWait()
  94. {
  95. exit = 1;
  96. warteAufThread(10000);
  97. ende();
  98. }
  99. bool WorldLoader::existsChunk(int x, int y, int dimension) const
  100. {
  101. Text filePath = Game::INSTANCE->getWorldDirectory() + "/dim/" + dimension + "/";
  102. filePath.appendHex(x);
  103. filePath += "_";
  104. filePath.appendHex(y);
  105. filePath += ".chunk";
  106. return DateiExistiert(filePath);
  107. }