BuildBoat.cpp 2.0 KB

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