WorldLoader.cpp 4.4 KB

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