Functions.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Functions.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 "Functions.h"
  12. #include "CMapGenerator.h"
  13. #include "RmgMap.h"
  14. #include "TileInfo.h"
  15. #include "RmgPath.h"
  16. #include "../TerrainHandler.h"
  17. #include "../mapping/CMap.h"
  18. #include "../mapObjectConstructors/AObjectTypeHandler.h"
  19. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  20. #include "../VCMI_Lib.h"
  21. #include <vstd/RNG.h>
  22. VCMI_LIB_NAMESPACE_BEGIN
  23. void replaceWithCurvedPath(rmg::Path & path, const Zone & zone, const int3 & src, bool onlyStraight /* = true */)
  24. {
  25. auto costFunction = rmg::Path::createCurvedCostFunction(zone.area()->getBorder());
  26. auto pathArea = zone.areaForRoads();
  27. rmg::Path curvedPath(pathArea);
  28. curvedPath.connect(zone.freePaths().get());
  29. curvedPath = curvedPath.search(src, onlyStraight, costFunction);
  30. if (curvedPath.valid())
  31. {
  32. path = curvedPath;
  33. }
  34. else
  35. {
  36. logGlobal->warn("Failed to create curved path to %s", src.toString());
  37. }
  38. }
  39. rmg::Tileset collectDistantTiles(const Zone& zone, int distance)
  40. {
  41. uint32_t distanceSq = distance * distance;
  42. auto subarea = zone.area()->getSubarea([&zone, distanceSq](const int3 & t)
  43. {
  44. return t.dist2dSQ(zone.getPos()) > distanceSq;
  45. });
  46. return subarea.getTiles();
  47. }
  48. int chooseRandomAppearance(vstd::RNG & generator, si32 ObjID, TerrainId terrain)
  49. {
  50. auto factories = VLC->objtypeh->knownSubObjects(ObjID);
  51. vstd::erase_if(factories, [ObjID, &terrain](si32 f)
  52. {
  53. //TODO: Use templates with lowest number of terrains (most specific)
  54. return VLC->objtypeh->getHandlerFor(ObjID, f)->getTemplates(terrain).empty();
  55. });
  56. return *RandomGeneratorUtil::nextItem(factories, generator);
  57. }
  58. VCMI_LIB_NAMESPACE_END