WorldLoader.cpp 4.1 KB

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