diff --git a/AI/VCAI/Fuzzy.cpp b/AI/VCAI/Fuzzy.cpp index c93071ec1..6f572c521 100644 --- a/AI/VCAI/Fuzzy.cpp +++ b/AI/VCAI/Fuzzy.cpp @@ -306,21 +306,18 @@ Goals::TSubgoal FuzzyHelper::chooseSolution (Goals::TGoalVec vec) if (vec.empty()) //no possibilities found return sptr(Goals::Invalid()); - typedef std::pair goalValue; - std::vector values; - for (auto g : vec) { - values.push_back (std::make_pair(g, g->accept(this))); + g->setpriority (g->accept(this)); } - auto compareGoals = [&](const goalValue & lhs, const goalValue & rhs) -> bool + auto compareGoals = [&](Goals::TSubgoal & lhs, const Goals::TSubgoal & rhs) -> bool { - return lhs.second < rhs.second; + return lhs->priority < rhs->priority; }; - boost::sort (values, compareGoals); - return values.back().first; + boost::sort (vec, compareGoals); + return vec.back(); } float FuzzyHelper::evaluate (Goals::Explore & g) @@ -404,14 +401,12 @@ float FuzzyHelper::evaluate (Goals::VisitTile & g) if (!g.hero) return 0; - float output = 0; - cb->setSelection (g.hero.h); int distance = cb->getDistance(g.tile); //at this point we already assume tile is reachable float missionImportance = 0; if (vstd::contains(ai->lockedHeroes, g.hero)) - missionImportance = ai->lockedHeroes[g.hero]->importanceWhenLocked(); + missionImportance = ai->lockedHeroes[g.hero]->priority; float strengthRatio = 100; //we are much stronger than enemy ui64 danger = evaluateDanger (g.tile, g.hero.h); @@ -427,13 +422,13 @@ float FuzzyHelper::evaluate (Goals::VisitTile & g) vt.movement->setInput(g.hero->movement); engine.process (VISIT_TILE); - output = vt.value->output().defuzzify(); + g.priority = vt.value->output().defuzzify(); } catch (fl::FuzzyException & fe) { logAi->errorStream() << "evaluate VisitTile " << fe.name() << ": " << fe.message(); } - return output; + return g.priority; } float FuzzyHelper::evaluate (Goals::VisitHero & g) diff --git a/AI/VCAI/Goals.cpp b/AI/VCAI/Goals.cpp index 27b9f98ec..2f81908b2 100644 --- a/AI/VCAI/Goals.cpp +++ b/AI/VCAI/Goals.cpp @@ -242,10 +242,6 @@ TSubgoal FindObj::whatToDoToAchieve() else return sptr (Goals::Explore()); } -float FindObj::importanceWhenLocked() const -{ - return 1; //we will probably fins it anyway, someday -} std::string GetObj::completeMessage() const { @@ -271,11 +267,6 @@ TSubgoal GetObj::whatToDoToAchieve() return sptr (Goals::ClearWayTo(pos).sethero(hero)); } -float GetObj::importanceWhenLocked() const -{ - return 3; -} - bool GetObj::fulfillsMe (TSubgoal goal) { if (goal->goalType == Goals::VISIT_TILE) @@ -313,11 +304,6 @@ TSubgoal VisitHero::whatToDoToAchieve() return sptr (Goals::Invalid()); } -float VisitHero::importanceWhenLocked() const -{ - return 4; -} - bool VisitHero::fulfillsMe (TSubgoal goal) { if (goal->goalType == Goals::VISIT_TILE && cb->getObj(ObjectInstanceID(objid))->visitablePos() == goal->tile) @@ -334,11 +320,6 @@ TSubgoal GetArtOfType::whatToDoToAchieve() return sptr (Goals::Invalid()); } -float GetArtOfType::importanceWhenLocked() const -{ - return 2; -} - TSubgoal ClearWayTo::whatToDoToAchieve() { assert(cb->isInTheMap(tile)); //set tile @@ -402,11 +383,6 @@ TGoalVec ClearWayTo::getAllPossibleSubgoals() return ret; } -float ClearWayTo::importanceWhenLocked() const -{ - return 5; -} - std::string Explore::completeMessage() const { return "Hero " + hero.get()->name + " completed exploration"; @@ -494,11 +470,6 @@ TGoalVec Explore::getAllPossibleSubgoals() return ret; }; -float Explore::importanceWhenLocked() const -{ - return 1; //exploration is natural and lowpriority process -} - TSubgoal RecruitHero::whatToDoToAchieve() { const CGTownInstance *t = ai->findTownWithTavern(); @@ -536,11 +507,6 @@ TSubgoal VisitTile::whatToDoToAchieve() return ret; } -float VisitTile::importanceWhenLocked() const -{ - return 5; //depends on a distance, but we should really reach the tile once it was selected -} - TGoalVec VisitTile::getAllPossibleSubgoals() { TGoalVec ret; @@ -582,11 +548,6 @@ TSubgoal DigAtTile::whatToDoToAchieve() return sptr (Goals::VisitTile(tile)); } -float DigAtTile::importanceWhenLocked() const -{ - return 20; //do not! interrupt tile digging -} - TSubgoal BuildThis::whatToDoToAchieve() { //TODO check res @@ -595,11 +556,6 @@ TSubgoal BuildThis::whatToDoToAchieve() return iAmElementar(); } -float BuildThis::importanceWhenLocked() const -{ - return 5; -} - TSubgoal CollectRes::whatToDoToAchieve() { std::vector markets; @@ -667,11 +623,6 @@ TSubgoal CollectRes::whatToDoToAchieve() return sptr (setisElementar(true)); //all the conditions for trade are met } -float CollectRes::importanceWhenLocked() const -{ - return 2; -} - TSubgoal GatherTroops::whatToDoToAchieve() { std::vector dwellings; @@ -727,11 +678,6 @@ TSubgoal GatherTroops::whatToDoToAchieve() //TODO: exchange troops between heroes } -float GatherTroops::importanceWhenLocked() const -{ - return 2; -} - TSubgoal Conquer::whatToDoToAchieve() { return fh->chooseSolution (getAllPossibleSubgoals()); @@ -776,21 +722,11 @@ TGoalVec Conquer::getAllPossibleSubgoals() return ret; } -float Conquer::importanceWhenLocked() const -{ - return 10; //defeating opponent is hig priority, always -} - TSubgoal Build::whatToDoToAchieve() { return iAmElementar(); } -float Build::importanceWhenLocked() const -{ - return 1; -} - TSubgoal Invalid::whatToDoToAchieve() { return iAmElementar(); @@ -887,11 +823,6 @@ TGoalVec GatherArmy::getAllPossibleSubgoals() return ret; } -float GatherArmy::importanceWhenLocked() const -{ - return 2.5; -} - //TSubgoal AbstractGoal::whatToDoToAchieve() //{ // logAi->debugStream() << boost::format("Decomposing goal of type %s") % name(); diff --git a/AI/VCAI/Goals.h b/AI/VCAI/Goals.h index 717ae9132..c022562a8 100644 --- a/AI/VCAI/Goals.h +++ b/AI/VCAI/Goals.h @@ -67,7 +67,7 @@ class AbstractGoal public: bool isElementar; VSETTER(bool, isElementar) bool isAbstract; VSETTER(bool, isAbstract) - int priority; VSETTER(bool, priority) + float priority; VSETTER(float, priority) int value; VSETTER(int, value) int resID; VSETTER(int, resID) int objid; VSETTER(int, objid) @@ -104,8 +104,6 @@ public: virtual TGoalVec getAllPossibleSubgoals() = 0; virtual TSubgoal whatToDoToAchieve() = 0; - virtual float importanceWhenLocked() const {return -1e10;}; //how much would it cost to interrupt the goal - //probably could use some sophisticated fuzzy evalluation for it as well ///Visitor pattern //TODO: make accept work for shared_ptr... somehow @@ -146,7 +144,7 @@ public: OSETTER(bool, isElementar) OSETTER(bool, isAbstract) - OSETTER(bool, priority) + OSETTER(float, priority) OSETTER(int, value) OSETTER(int, resID) OSETTER(int, objid) @@ -158,6 +156,7 @@ public: void accept (VCAI * ai) override; float accept (FuzzyHelper * f) override; + //float importanceWhenLocked() const override; CGoal * clone() const override { @@ -182,157 +181,141 @@ TSubgoal sptr(const AbstractGoal & tmp); class Invalid : public CGoal { public: - Invalid() : CGoal (Goals::INVALID){}; + Invalid() : CGoal (Goals::INVALID) {priority = -1e10;}; TGoalVec getAllPossibleSubgoals() override {return TGoalVec();}; TSubgoal whatToDoToAchieve() override; - //float importanceWhenLocked() const override; }; class Win : public CGoal { public: - Win() : CGoal (Goals::WIN){}; + Win() : CGoal (Goals::WIN) {priority = 100;}; TGoalVec getAllPossibleSubgoals() override {return TGoalVec();}; TSubgoal whatToDoToAchieve() override; - //float importanceWhenLocked() const override; //can't be locked, doesn't make much sense anyway }; class NotLose : public CGoal { public: - NotLose() : CGoal (Goals::DO_NOT_LOSE){}; + NotLose() : CGoal (Goals::DO_NOT_LOSE) {priority = 100;}; TGoalVec getAllPossibleSubgoals() override {return TGoalVec();}; TSubgoal whatToDoToAchieve() override; - float importanceWhenLocked() const override; }; class Conquer : public CGoal { public: - Conquer() : CGoal (Goals::CONQUER){}; + Conquer() : CGoal (Goals::CONQUER) {priority = 10;}; TGoalVec getAllPossibleSubgoals() override; TSubgoal whatToDoToAchieve() override; - float importanceWhenLocked() const override; }; class Build : public CGoal { public: - Build() : CGoal (Goals::BUILD){}; + Build() : CGoal (Goals::BUILD) {priority = 1;}; TGoalVec getAllPossibleSubgoals() override {return TGoalVec();}; TSubgoal whatToDoToAchieve() override; - float importanceWhenLocked() const override; }; class Explore : public CGoal { public: - Explore() : CGoal (Goals::EXPLORE){}; + Explore() : CGoal (Goals::EXPLORE){priority = 1;}; Explore(HeroPtr h) : CGoal (Goals::EXPLORE){hero = h;}; TGoalVec getAllPossibleSubgoals() override; TSubgoal whatToDoToAchieve() override; std::string completeMessage() const override; - float importanceWhenLocked() const override; }; class GatherArmy : public CGoal { private: GatherArmy() : CGoal (Goals::GATHER_ARMY){}; public: - GatherArmy(int val) : CGoal (Goals::GATHER_ARMY){value = val;}; + GatherArmy(int val) : CGoal (Goals::GATHER_ARMY){value = val; priority = 2.5;}; TGoalVec getAllPossibleSubgoals() override; TSubgoal whatToDoToAchieve() override; std::string completeMessage() const override; - float importanceWhenLocked() const override; }; class BoostHero : public CGoal { public: - BoostHero() : CGoal (Goals::INVALID){}; //TODO + BoostHero() : CGoal (Goals::INVALID){priority = -1e10;}; //TODO TGoalVec getAllPossibleSubgoals() override {return TGoalVec();}; TSubgoal whatToDoToAchieve() override; - float importanceWhenLocked() const override; }; class RecruitHero : public CGoal { public: - RecruitHero() : CGoal (Goals::RECRUIT_HERO){}; + RecruitHero() : CGoal (Goals::RECRUIT_HERO){priority = 1;}; TGoalVec getAllPossibleSubgoals() override {return TGoalVec();}; TSubgoal whatToDoToAchieve() override; - //float importanceWhenLocked() const override; }; class BuildThis : public CGoal { private: BuildThis() : CGoal (Goals::BUILD_STRUCTURE){}; public: - BuildThis(BuildingID Bid, const CGTownInstance *tid) : CGoal (Goals::BUILD_STRUCTURE) {bid = Bid; town = tid;}; - BuildThis(BuildingID Bid) : CGoal (Goals::BUILD_STRUCTURE) {bid = Bid;}; + BuildThis(BuildingID Bid, const CGTownInstance *tid) : CGoal (Goals::BUILD_STRUCTURE) {bid = Bid; town = tid; priority = 5;}; + BuildThis(BuildingID Bid) : CGoal (Goals::BUILD_STRUCTURE) {bid = Bid; priority = 5;}; TGoalVec getAllPossibleSubgoals() override {return TGoalVec();}; TSubgoal whatToDoToAchieve() override; - float importanceWhenLocked() const override; }; class CollectRes : public CGoal { private: CollectRes() : CGoal (Goals::COLLECT_RES){}; public: - CollectRes(int rid, int val) : CGoal (Goals::COLLECT_RES) {resID = rid; value = val;}; + CollectRes(int rid, int val) : CGoal (Goals::COLLECT_RES) {resID = rid; value = val; priority = 2;}; TGoalVec getAllPossibleSubgoals() override {return TGoalVec();}; TSubgoal whatToDoToAchieve() override; - float importanceWhenLocked() const override; }; class GatherTroops : public CGoal { private: - GatherTroops() : CGoal (Goals::GATHER_TROOPS){}; + GatherTroops() : CGoal (Goals::GATHER_TROOPS){priority = 2;}; public: - GatherTroops(int type, int val) : CGoal (Goals::GATHER_TROOPS){objid = type; value = val;}; + GatherTroops(int type, int val) : CGoal (Goals::GATHER_TROOPS){objid = type; value = val; priority = 2;}; TGoalVec getAllPossibleSubgoals() override {return TGoalVec();}; TSubgoal whatToDoToAchieve() override; - float importanceWhenLocked() const override; }; class GetObj : public CGoal { private: GetObj() {}; // empty constructor not allowed public: - GetObj(int Objid) : CGoal(Goals::GET_OBJ) {objid = Objid;}; + GetObj(int Objid) : CGoal(Goals::GET_OBJ) {objid = Objid; priority = 3;}; TGoalVec getAllPossibleSubgoals() override {return TGoalVec();}; TSubgoal whatToDoToAchieve() override; bool operator== (GetObj &g) {return g.objid == objid;} bool fulfillsMe (TSubgoal goal) override; std::string completeMessage() const override; - float importanceWhenLocked() const override; }; class FindObj : public CGoal { private: FindObj() {}; // empty constructor not allowed public: - FindObj(int ID) : CGoal(Goals::FIND_OBJ) {objid = ID;}; - FindObj(int ID, int subID) : CGoal(Goals::FIND_OBJ) {objid = ID; resID = subID;}; + FindObj(int ID) : CGoal(Goals::FIND_OBJ) {objid = ID; priority = 1;}; + FindObj(int ID, int subID) : CGoal(Goals::FIND_OBJ) {objid = ID; resID = subID; priority = 1;}; TGoalVec getAllPossibleSubgoals() override {return TGoalVec();}; TSubgoal whatToDoToAchieve() override; - float importanceWhenLocked() const override; }; class VisitHero : public CGoal { private: VisitHero() : CGoal (Goals::VISIT_HERO){}; public: - VisitHero(int hid) : CGoal (Goals::VISIT_HERO){objid = hid;}; + VisitHero(int hid) : CGoal (Goals::VISIT_HERO){objid = hid; priority = 4;}; TGoalVec getAllPossibleSubgoals() override {return TGoalVec();}; TSubgoal whatToDoToAchieve() override; bool operator== (VisitHero &g) {return g.objid == objid;} bool fulfillsMe (TSubgoal goal) override; std::string completeMessage() const override; - float importanceWhenLocked() const override; }; class GetArtOfType : public CGoal { private: GetArtOfType() : CGoal (Goals::GET_ART_TYPE){}; public: - GetArtOfType(int type) : CGoal (Goals::GET_ART_TYPE){aid = type;}; + GetArtOfType(int type) : CGoal (Goals::GET_ART_TYPE){aid = type; priority = 2;}; TGoalVec getAllPossibleSubgoals() override {return TGoalVec();}; TSubgoal whatToDoToAchieve() override; - float importanceWhenLocked() const override; }; class VisitTile : public CGoal //tile, in conjunction with hero elementar; assumes tile is reachable @@ -340,21 +323,19 @@ class VisitTile : public CGoal private: VisitTile() {}; // empty constructor not allowed public: - VisitTile(int3 Tile) : CGoal (Goals::VISIT_TILE) {tile = Tile;}; + VisitTile(int3 Tile) : CGoal (Goals::VISIT_TILE) {tile = Tile; priority = 5;}; TGoalVec getAllPossibleSubgoals() override; TSubgoal whatToDoToAchieve() override; bool operator== (VisitTile &g) {return g.tile == tile;} std::string completeMessage() const override; - float importanceWhenLocked() const override; }; class ClearWayTo : public CGoal { public: - ClearWayTo(int3 Tile) : CGoal (Goals::CLEAR_WAY_TO) {tile = Tile;}; + ClearWayTo(int3 Tile) : CGoal (Goals::CLEAR_WAY_TO) {tile = Tile; priority = 5;}; TGoalVec getAllPossibleSubgoals() override; TSubgoal whatToDoToAchieve() override; bool operator== (ClearWayTo &g) {return g.tile == tile;} - float importanceWhenLocked() const override; }; class DigAtTile : public CGoal //elementar with hero on tile @@ -362,11 +343,10 @@ class DigAtTile : public CGoal private: DigAtTile() : CGoal (Goals::DIG_AT_TILE){}; public: - DigAtTile(int3 Tile) : CGoal (Goals::DIG_AT_TILE) {tile = Tile;}; + DigAtTile(int3 Tile) : CGoal (Goals::DIG_AT_TILE) {tile = Tile; priority = 20;}; TGoalVec getAllPossibleSubgoals() override {return TGoalVec();}; TSubgoal whatToDoToAchieve() override; bool operator== (DigAtTile &g) {return g.tile == tile;} - float importanceWhenLocked() const override; }; class CIssueCommand : public CGoal @@ -374,10 +354,9 @@ class CIssueCommand : public CGoal std::function command; public: - CIssueCommand(std::function _command): CGoal(ISSUE_COMMAND), command(_command) {} + CIssueCommand(std::function _command): CGoal(ISSUE_COMMAND), command(_command) {priority = 1e10;}; TGoalVec getAllPossibleSubgoals() override {return TGoalVec();}; TSubgoal whatToDoToAchieve() override; - //float importanceWhenLocked() const override; //unsupported yet, but shoudl be highest }; }