WorldLoader.cpp 4.5 KB

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