BuildBoat.cpp 2.1 KB

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