WorldLoader.cpp 4.6 KB

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