WorldLoader.cpp 4.4 KB

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