Modificator.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. mapProxy = map.getMapProxy();
  20. }
  21. void Modificator::setName(const std::string & n)
  22. {
  23. name = n;
  24. }
  25. const std::string & Modificator::getName() const
  26. {
  27. return name;
  28. }
  29. bool Modificator::isReady()
  30. {
  31. Lock lock(mx, boost::try_to_lock_t{});
  32. if (!lock.owns_lock())
  33. {
  34. return false;
  35. }
  36. else
  37. {
  38. //Check prerequisites
  39. for (auto it = preceeders.begin(); it != preceeders.end();)
  40. {
  41. if ((*it)->isFinished()) //OK
  42. {
  43. //This preceeder won't be checked in the future
  44. it = preceeders.erase(it);
  45. }
  46. else
  47. {
  48. return false;
  49. }
  50. }
  51. //If a job is finished, it should be already erased from a queue
  52. return !finished;
  53. }
  54. }
  55. bool Modificator::isFinished()
  56. {
  57. Lock lock(mx, boost::try_to_lock_t{});
  58. if (!lock.owns_lock())
  59. {
  60. return false;
  61. }
  62. else
  63. {
  64. return finished;
  65. }
  66. }
  67. void Modificator::run()
  68. {
  69. Lock lock(mx);
  70. if(!finished)
  71. {
  72. logGlobal->info("Modificator zone %d - %s - started", zone.getId(), getName());
  73. CStopWatch processTime;
  74. try
  75. {
  76. process();
  77. }
  78. catch(rmgException &e)
  79. {
  80. logGlobal->error("Modificator %s, exception: %s", getName(), e.what());
  81. }
  82. #ifdef RMG_DUMP
  83. dump();
  84. #endif
  85. finished = true;
  86. logGlobal->info("Modificator zone %d - %s - done (%d ms)", zone.getId(), getName(), processTime.getDiff());
  87. }
  88. }
  89. void Modificator::dependency(Modificator * modificator)
  90. {
  91. if(modificator && modificator != this)
  92. {
  93. //TODO: use vstd::contains
  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. int levels = map.levels();
  110. int width = map.width();
  111. int height = map.height();
  112. for(int z = 0; z < levels; z++)
  113. {
  114. for(int j=0; j<height; j++)
  115. {
  116. for(int i=0; i<width; i++)
  117. {
  118. out << dump(int3(i, j, z));
  119. }
  120. out << std::endl;
  121. }
  122. out << std::endl;
  123. }
  124. out << std::endl;
  125. }
  126. char Modificator::dump(const int3 & t)
  127. {
  128. if(zone.freePaths().contains(t))
  129. return '.'; //free path
  130. if(zone.areaPossible().contains(t))
  131. return ' '; //possible
  132. if(zone.areaUsed().contains(t))
  133. return 'U'; //used
  134. if(zone.area().contains(t))
  135. {
  136. if(map.shouldBeBlocked(t))
  137. return '#'; //obstacle
  138. else
  139. return '^'; //visitable points?
  140. }
  141. return '?';
  142. }
  143. VCMI_LIB_NAMESPACE_END