WorldLoader.cpp 2.9 KB

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