Modificator.cpp 3.1 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 "StdInc.h"
  11. #include "Modificator.h"
  12. #include "../Functions.h"
  13. #include "../CMapGenerator.h"
  14. #include "../RmgMap.h"
  15. #include "../../CStopWatch.h"
  16. #include "../../mapping/CMap.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. Modificator::Modificator(Zone & zone, RmgMap & map, CMapGenerator & generator) : zone(zone), map(map), generator(generator)
  19. {
  20. mapProxy = map.getMapProxy();
  21. }
  22. void Modificator::setName(const std::string & n)
  23. {
  24. name = n;
  25. }
  26. const std::string & Modificator::getName() const
  27. {
  28. return name;
  29. }
  30. bool Modificator::isReady()
  31. {
  32. Lock lock(mx, boost::try_to_lock_t{});
  33. if (!lock.owns_lock())
  34. {
  35. return false;
  36. }
  37. else
  38. {
  39. //Check prerequisites
  40. for (auto it = preceeders.begin(); it != preceeders.end();)
  41. {
  42. if ((*it)->isFinished()) //OK
  43. {
  44. //This preceeder won't be checked in the future
  45. it = preceeders.erase(it);
  46. }
  47. else
  48. {
  49. return false;
  50. }
  51. }
  52. //If a job is finished, it should be already erased from a queue
  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. //TODO: use vstd::contains
  95. if(std::find(preceeders.begin(), preceeders.end(), modificator) == preceeders.end())
  96. preceeders.push_back(modificator);
  97. }
  98. }
  99. void Modificator::postfunction(Modificator * modificator)
  100. {
  101. if(modificator && modificator != this)
  102. {
  103. if(std::find(modificator->preceeders.begin(), modificator->preceeders.end(), this) == modificator->preceeders.end())
  104. modificator->preceeders.push_back(this);
  105. }
  106. }
  107. void Modificator::dump()
  108. {
  109. std::ofstream out(boost::str(boost::format("seed_%d_modzone_%d_%s.txt") % generator.getRandomSeed() % zone.getId() % getName()));
  110. int levels = map.levels();
  111. int width = map.width();
  112. int height = map.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