Modificator.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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, std::try_to_lock);
  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, std::try_to_lock);
  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->trace("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->trace("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. // TODO: Refactor to lock zone area only once
  110. std::ofstream out(boost::str(boost::format("seed_%d_modzone_%d_%s.txt") % generator.getRandomSeed() % zone.getId() % getName()));
  111. int levels = map.levels();
  112. int width = map.width();
  113. int height = map.height();
  114. for(int z = 0; z < levels; z++)
  115. {
  116. for(int j=0; j<height; j++)
  117. {
  118. for(int i=0; i<width; i++)
  119. {
  120. out << dump(int3(i, j, z));
  121. }
  122. out << std::endl;
  123. }
  124. out << std::endl;
  125. }
  126. out << std::endl;
  127. }
  128. char Modificator::dump(const int3 & t)
  129. {
  130. if(zone.freePaths()->contains(t))
  131. return '.'; //free path
  132. if(zone.areaPossible()->contains(t))
  133. return ' '; //possible
  134. if(zone.areaUsed()->contains(t))
  135. return 'U'; //used
  136. if(zone.area()->contains(t))
  137. {
  138. if(map.shouldBeBlocked(t))
  139. return '#'; //obstacle
  140. else
  141. return '^'; //visitable points?
  142. }
  143. return '?';
  144. }
  145. VCMI_LIB_NAMESPACE_END