WorldLoader.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. if (!Game::INSTANCE->isChunkLoaded(x, y, next.dimensionId))
  66. {
  67. Datei* file = new Datei();
  68. Text filePath = Game::INSTANCE->getWorldDirectory() + "/dim/" + next.dimensionId + "/";
  69. filePath.appendHex(x);
  70. filePath += "_";
  71. filePath.appendHex(y);
  72. filePath += ".chunk";
  73. file->setDatei(filePath);
  74. if (file->open(Datei::Style::lesen))
  75. {
  76. Chunk* chunk = new Chunk(Framework::Punkt(x, y), next.dimensionId, file);
  77. Dimension* dim = Game::INSTANCE->zDimension(next.dimensionId);
  78. if (!dim)
  79. {
  80. dim = new Dimension(next.dimensionId);
  81. Game::INSTANCE->addDimension(dim);
  82. }
  83. dim->setChunk(chunk, Punkt(x, y));
  84. }
  85. file->close();
  86. file->release();
  87. }
  88. }
  89. }
  90. }
  91. }
  92. void WorldLoader::requestLoading(Area request)
  93. {
  94. cs.lock();
  95. requestQueue.add(request);
  96. cs.unlock();
  97. }
  98. void WorldLoader::exitAndWait()
  99. {
  100. exit = 1;
  101. warteAufThread(10000);
  102. ende();
  103. }
  104. bool WorldLoader::existsChunk(int x, int y, int dimension) const
  105. {
  106. Text filePath = Game::INSTANCE->getWorldDirectory() + "/dim/" + dimension + "/";
  107. filePath.appendHex(x);
  108. filePath += "_";
  109. filePath.appendHex(y);
  110. filePath += ".chunk";
  111. return DateiExistiert(filePath);
  112. }