Modificator.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Modificator.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "Modificator.h"
  11. #include "Functions.h"
  12. #include "CMapGenerator.h"
  13. #include "RmgMap.h"
  14. #include "../CStopWatch.h"
  15. #include "../mapping/CMap.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. Modificator::Modificator(Zone & zone, RmgMap & map, CMapGenerator & generator) : zone(zone), map(map), generator(generator)
  18. {
  19. }
  20. void Modificator::setName(const std::string & n)
  21. {
  22. name = n;
  23. }
  24. const std::string & Modificator::getName() const
  25. {
  26. return name;
  27. }
  28. bool Modificator::isReady()
  29. {
  30. Lock lock(mx, boost::try_to_lock_t{});
  31. if (!lock.owns_lock())
  32. {
  33. return false;
  34. }
  35. else
  36. {
  37. //Check prerequisites
  38. for (auto it = preceeders.begin(); it != preceeders.end();)
  39. {
  40. if ((*it)->isFinished()) //OK
  41. {
  42. it = preceeders.erase(it);
  43. }
  44. else if (!(*it)->isReady())
  45. {
  46. return false;
  47. }
  48. else
  49. {
  50. ++it;
  51. }
  52. }
  53. return !finished;
  54. }
  55. }
  56. bool Modificator::isFinished()
  57. {
  58. Lock lock(mx, boost::try_to_lock_t{});
  59. if (!lock.owns_lock())
  60. {
  61. return false;
  62. }
  63. else
  64. {
  65. return finished;
  66. }
  67. }
  68. void Modificator::run()
  69. {
  70. Lock lock(mx);
  71. if(!finished)
  72. {
  73. logGlobal->info("Modificator zone %d - %s - started", zone.getId(), getName());
  74. CStopWatch processTime;
  75. try
  76. {
  77. process();
  78. }
  79. catch(rmgException &e)
  80. {
  81. logGlobal->error("Modificator %s, exception: %s", getName(), e.what());
  82. }
  83. #ifdef RMG_DUMP
  84. dump();
  85. #endif
  86. finished = true;
  87. logGlobal->info("Modificator zone %d - %s - done (%d ms)", zone.getId(), getName(), processTime.getDiff());
  88. }
  89. }
  90. void Modificator::dependency(Modificator * modificator)
  91. {
  92. if(modificator && modificator != this)
  93. {
  94. if(std::find(preceeders.begin(), preceeders.end(), modificator) == preceeders.end())
  95. preceeders.push_back(modificator);
  96. }
  97. }
  98. void Modificator::postfunction(Modificator * modificator)
  99. {
  100. if(modificator && modificator != this)
  101. {
  102. if(std::find(modificator->preceeders.begin(), modificator->preceeders.end(), this) == modificator->preceeders.end())
  103. modificator->preceeders.push_back(this);
  104. }
  105. }
  106. void Modificator::dump()
  107. {
  108. std::ofstream out(boost::to_string(boost::format("seed_%d_modzone_%d_%s.txt") % generator.getRandomSeed() % zone.getId() % getName()));
  109. auto & mapInstance = map.map();
  110. int levels = mapInstance.levels();
  111. int width = mapInstance.width;
  112. int height = mapInstance.height;
  113. for(int z = 0; z < levels; z++)
  114. {
  115. for(int j=0; j<height; j++)
  116. {
  117. for(int i=0; i<width; i++)
  118. {
  119. out << dump(int3(i, j, z));
  120. }
  121. out << std::endl;
  122. }
  123. out << std::endl;
  124. }
  125. out << std::endl;
  126. }
  127. char Modificator::dump(const int3 & t)
  128. {
  129. if(zone.freePaths().contains(t))
  130. return '.'; //free path
  131. if(zone.areaPossible().contains(t))
  132. return ' '; //possible
  133. if(zone.areaUsed().contains(t))
  134. return 'U'; //used
  135. if(zone.area().contains(t))
  136. {
  137. if(map.shouldBeBlocked(t))
  138. return '#'; //obstacle
  139. else
  140. return '^'; //visitable points?
  141. }
  142. return '?';
  143. }
  144. VCMI_LIB_NAMESPACE_END