BuildBoat.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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->o->id == other.shipyard->o->id;
  23. }
  24. TSubgoal BuildBoat::whatToDoToAchieve()
  25. {
  26. if(cb->getPlayerRelations(ai->playerID, shipyard->o->tempOwner) == PlayerRelations::ENEMIES)
  27. {
  28. return fh->chooseSolution(ai->ah->howToVisitObj(shipyard->o));
  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->o->tempOwner) == 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 %s located at %s, estimated boat position %s",
  56. shipyard->o->getObjectName(),
  57. shipyard->o->visitablePos().toString(),
  58. shipyard->bestLocation().toString());
  59. cb->buildBoat(shipyard);
  60. throw goalFulfilledException(sptr(*this));
  61. }
  62. std::string BuildBoat::name() const
  63. {
  64. return "BuildBoat";
  65. }
  66. std::string BuildBoat::completeMessage() const
  67. {
  68. return "Boat have been built at " + shipyard->o->visitablePos().toString();
  69. }