BuildBoat.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * BuildBoat.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 "BuildBoat.h"
  12. #include "../VCAI.h"
  13. #include "../FuzzyHelper.h"
  14. #include "../AIhelper.h"
  15. #include "../../../lib/CPathfinder.h"
  16. extern boost::thread_specific_ptr<CCallback> cb;
  17. extern boost::thread_specific_ptr<VCAI> ai;
  18. extern FuzzyHelper * fh;
  19. using namespace Goals;
  20. bool BuildBoat::operator==(const BuildBoat & other) const
  21. {
  22. return shipyard == other.shipyard;
  23. }
  24. TSubgoal BuildBoat::whatToDoToAchieve()
  25. {
  26. if(cb->getPlayerRelations(ai->playerID, shipyard->getObject()->getOwner()) == PlayerRelations::ENEMIES)
  27. {
  28. return fh->chooseSolution(ai->ah->howToVisitObj(dynamic_cast<const CGObjectInstance*>(shipyard)));
  29. }
  30. if(shipyard->shipyardStatus() != IShipyard::GOOD)
  31. {
  32. throw cannotFulfillGoalException("Shipyard is busy.");
  33. }
  34. TResources boatCost;
  35. shipyard->getBoatCost(boatCost);
  36. return ai->ah->whatToDo(boatCost, this->iAmElementar());
  37. }
  38. void BuildBoat::accept(VCAI * ai)
  39. {
  40. TResources boatCost;
  41. shipyard->getBoatCost(boatCost);
  42. if(!cb->getResourceAmount().canAfford(boatCost))
  43. {
  44. throw cannotFulfillGoalException("Can not afford boat");
  45. }
  46. if(cb->getPlayerRelations(ai->playerID, shipyard->getObject()->getOwner()) == PlayerRelations::ENEMIES)
  47. {
  48. throw cannotFulfillGoalException("Can not build boat in enemy shipyard");
  49. }
  50. if(shipyard->shipyardStatus() != IShipyard::GOOD)
  51. {
  52. throw cannotFulfillGoalException("Shipyard is busy.");
  53. }
  54. logAi->trace(
  55. "Building boat at shipyard located at %s, estimated boat position %s",
  56. shipyard->getObject()->visitablePos().toString(),
  57. shipyard->bestLocation().toString());
  58. cb->buildBoat(shipyard);
  59. throw goalFulfilledException(sptr(*this));
  60. }
  61. std::string BuildBoat::name() const
  62. {
  63. return "BuildBoat";
  64. }
  65. std::string BuildBoat::completeMessage() const
  66. {
  67. return "Boat have been built at " + shipyard->getObject()->visitablePos().toString();
  68. }