Functions.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 "../GameLibrary.h"
  21. #include <vstd/RNG.h>
  22. VCMI_LIB_NAMESPACE_BEGIN
  23. void replaceWithCurvedPath(rmg::Path & path, Zone & zone, const int3 & src, bool onlyStraight /* = true */)
  24. {
  25. // Get random control points from within the zone
  26. auto & rng = zone.getRand();
  27. const auto & tiles = zone.area()->getTilesVector();
  28. if(tiles.size() < 2)
  29. {
  30. logGlobal->warn("Zone too small for curved path");
  31. return;
  32. }
  33. // Pick two random control points (Q1, Q2) from zone tiles
  34. int3 control1 = tiles[rng.nextInt(0, static_cast<int>(tiles.size()) - 1)];
  35. int3 control2 = tiles[rng.nextInt(0, static_cast<int>(tiles.size()) - 1)];
  36. // Create Bezier cost function: Q0=start, Q1=control1, Q2=control2, Q3=src
  37. auto costFunction = rmg::Path::createBezierCostFunction(src, control1, control2, zone.getPos());
  38. auto pathArea = zone.areaForRoads();
  39. rmg::Path curvedPath(pathArea);
  40. curvedPath.connect(zone.freePaths().get());
  41. curvedPath = curvedPath.search(src, onlyStraight, costFunction);
  42. if (curvedPath.valid())
  43. {
  44. path = curvedPath;
  45. }
  46. else
  47. {
  48. logGlobal->warn("Failed to create curved path to %s", src.toString());
  49. }
  50. }
  51. rmg::Tileset collectDistantTiles(const Zone& zone, int distance)
  52. {
  53. uint32_t distanceSq = distance * distance;
  54. auto subarea = zone.area()->getSubarea([&zone, distanceSq](const int3 & t)
  55. {
  56. return t.dist2dSQ(zone.getPos()) > distanceSq;
  57. });
  58. return subarea.getTiles();
  59. }
  60. int chooseRandomAppearance(vstd::RNG & generator, si32 ObjID, TerrainId terrain)
  61. {
  62. auto factories = LIBRARY->objtypeh->knownSubObjects(ObjID);
  63. vstd::erase_if(factories, [ObjID, &terrain](si32 f)
  64. {
  65. //TODO: Use templates with lowest number of terrains (most specific)
  66. return LIBRARY->objtypeh->getHandlerFor(ObjID, f)->getTemplates(terrain).empty();
  67. });
  68. return *RandomGeneratorUtil::nextItem(factories, generator);
  69. }
  70. VCMI_LIB_NAMESPACE_END