int CvSelectionGroupAI::AI_sumStrength(const CvPlot* pAttackedPlot, DomainTypes eDomainType, bool bCheckCanAttack, bool bCheckCanMove) const { CLLNode<IDInfo>* pUnitNode; CvUnit* pLoopUnit; int strSum = 0; pUnitNode = headUnitNode(); while (pUnitNode != NULL) { pLoopUnit = ::getUnit(pUnitNode->m_data); pUnitNode = nextUnitNode(pUnitNode); if (!pLoopUnit->isDead()) { bool bCanAttack = false; if (pLoopUnit->getDomainType() == DOMAIN_AIR) bCanAttack = pLoopUnit->canAirAttack(); else bCanAttack = pLoopUnit->canAttack(); if (!bCheckCanAttack || bCanAttack) { if (!bCheckCanMove || pLoopUnit->canMove()) if (!bCheckCanMove || pAttackedPlot == NULL || pLoopUnit->canMoveInto(pAttackedPlot, /*bAttack*/ true, /*bDeclareWar*/ true)) if (eDomainType == NO_DOMAIN || pLoopUnit->getDomainType() == eDomainType) strSum += pLoopUnit->currEffectiveStr(pAttackedPlot, pLoopUnit); } } } return strSum; }
CvUnit* CvSelectionGroupAI::AI_getBestGroupAttacker(const CvPlot* pPlot, bool bPotentialEnemy, int& iUnitOdds, bool bForce, bool bNoBlitz) const { CLLNode<IDInfo>* pUnitNode; CvUnit* pLoopUnit; CvUnit* pBestUnit; int iValue; int iBestValue; int iOdds; int iBestOdds; iBestValue = 0; iBestOdds = 0; pBestUnit = NULL; pUnitNode = headUnitNode(); bool bIsHuman = (pUnitNode != NULL) ? GET_PLAYER(::getUnit(pUnitNode->m_data)->getOwnerINLINE()).isHuman() : true; while (pUnitNode != NULL) { pLoopUnit = ::getUnit(pUnitNode->m_data); pUnitNode = nextUnitNode(pUnitNode); if (!pLoopUnit->isDead()) { bool bCanAttack = false; bCanAttack = pLoopUnit->canAttack(); if (bCanAttack && bNoBlitz && pLoopUnit->isBlitz() && pLoopUnit->isMadeAttack()) { bCanAttack = false; } if (bCanAttack) { if (bForce || pLoopUnit->canMove()) { if (bForce || pLoopUnit->canMoveInto(pPlot, /*bAttack*/ true, /*bDeclareWar*/ bPotentialEnemy)) { iOdds = pLoopUnit->AI_attackOdds(pPlot, bPotentialEnemy); iValue = iOdds; FAssertMsg(iValue > 0, "iValue is expected to be greater than 0"); // if non-human, prefer the last unit that has the best value (so as to avoid splitting the group) if (iValue > iBestValue || (!bIsHuman && iValue > 0 && iValue == iBestValue)) { iBestValue = iValue; iBestOdds = iOdds; pBestUnit = pLoopUnit; } } } } } } iUnitOdds = iBestOdds; return pBestUnit; }
// uses the same scale as CvPlayerAI::AI_getOurPlotStrength int CvSelectionGroupAI::AI_GroupPower(CvPlot* pPlot, bool bDefensiveBonuses) const { CLLNode<IDInfo>* pUnitNode; CvUnit* pLoopUnit; int iValue; iValue = 0; pUnitNode = headUnitNode(); while (pUnitNode != NULL) { pLoopUnit = ::getUnit(pUnitNode->m_data); pUnitNode = nextUnitNode(pUnitNode); if ((bDefensiveBonuses && pLoopUnit->canDefend()) || pLoopUnit->canAttack()) { if (!(pLoopUnit->isInvisible(getTeam(), false))) { if (pLoopUnit->atPlot(pPlot) || pLoopUnit->canMoveInto(pPlot) || pLoopUnit->canMoveInto(pPlot, /*bAttack*/ true)) { iValue += pLoopUnit->currEffectiveStr((bDefensiveBonuses ? pPlot : NULL), NULL); } } } } return iValue; }
void CvSelectionGroupAI::AI_seperateAI(UnitAITypes eUnitAI) { CLLNode<IDInfo>* pEntityNode; CvUnit* pLoopUnit; pEntityNode = headUnitNode(); while (pEntityNode != NULL) { pLoopUnit = ::getUnit(pEntityNode->m_data); pEntityNode = nextUnitNode(pEntityNode); if (pLoopUnit->AI_getUnitAIType() == eUnitAI) { pLoopUnit->joinGroup(NULL); // TAC - AI Assault Sea - koma13, jdog5000(BBAI) // Was potential crash in use of plot() if group emptied //if (plot()->getTeam() == getTeam()) if (pLoopUnit->plot()->getTeam() == getTeam()) // TAC - AI Assault Sea - koma13, jdog5000(BBAI) { pLoopUnit->getGroup()->pushMission(MISSION_SKIP); } } } }
void CvSelectionGroupAI::AI_setGroupflag(int newflag) { CLLNode<IDInfo>* pUnitNode = headUnitNode(); while (pUnitNode != NULL) { CvUnit* pLoopUnit = ::getUnit(pUnitNode->m_data); pUnitNode = nextUnitNode(pUnitNode); pLoopUnit->AI_setGroupflag(newflag); } }
CvUnit* CvSelectionGroupAI::AI_getBestGroupSacrifice(const CvPlot* pPlot, bool bPotentialEnemy, bool bForce, bool bNoBlitz) const { int iBestValue = 0; CvUnit* pBestUnit = NULL; CLLNode<IDInfo>* pUnitNode = headUnitNode(); while (pUnitNode != NULL) { CvUnit* pLoopUnit = ::getUnit(pUnitNode->m_data); pUnitNode = nextUnitNode(pUnitNode); if (!pLoopUnit->isDead()) { bool bCanAttack = false; if (pLoopUnit->getDomainType() == DOMAIN_AIR) { bCanAttack = pLoopUnit->canAirAttack(); } else { bCanAttack = pLoopUnit->canAttack(); if (bCanAttack && bNoBlitz && pLoopUnit->isBlitz() && pLoopUnit->isMadeAttack()) { bCanAttack = false; } } if (bCanAttack) { if (bForce || pLoopUnit->canMove()) { if (bForce || pLoopUnit->canMoveInto(pPlot, true)) { int iValue = pLoopUnit->AI_sacrificeValue(pPlot); FAssertMsg(iValue > 0, "iValue is expected to be greater than 0"); // we want to pick the last unit of highest value, so pick the last unit with a good value if (iValue >= iBestValue) { iBestValue = iValue; pBestUnit = pLoopUnit; } } } } } } return pBestUnit; }
// these separate function have been tweaked by K-Mod and bbai. void CvSelectionGroupAI::AI_separate() { CLLNode<IDInfo>* pEntityNode; CvUnit* pLoopUnit; pEntityNode = headUnitNode(); while (pEntityNode != NULL) { pLoopUnit = ::getUnit(pEntityNode->m_data); pEntityNode = nextUnitNode(pEntityNode); pLoopUnit->joinGroup(NULL); } }
void CvSelectionGroupAI::AI_groupBombard() { CLLNode<IDInfo>* pEntityNode = headUnitNode(); CvUnit* pLoopUnit = NULL; while (pEntityNode != NULL) { pLoopUnit = ::getUnit(pEntityNode->m_data); pEntityNode = nextUnitNode(pEntityNode); if (pLoopUnit->canBombard(plot())) { pLoopUnit->bombard(); } } }
void CvSelectionGroupAI::AI_separateNonAI(UnitAITypes eUnitAI) { CLLNode<IDInfo>* pEntityNode; CvUnit* pLoopUnit; pEntityNode = headUnitNode(); while (pEntityNode != NULL) { pLoopUnit = ::getUnit(pEntityNode->m_data); pEntityNode = nextUnitNode(pEntityNode); if (pLoopUnit->AI_getUnitAIType() != eUnitAI) { pLoopUnit->joinGroup(NULL); } } }
CvUnit* CvSelectionGroupAI::AI_ejectBestDefender(CvPlot* pDefendPlot) { CLLNode<IDInfo>* pEntityNode; CvUnit* pLoopUnit; pEntityNode = headUnitNode(); CvUnit* pBestUnit = NULL; int iBestUnitValue = 0; while (pEntityNode != NULL) { pLoopUnit = ::getUnit(pEntityNode->m_data); pEntityNode = nextUnitNode(pEntityNode); //if (!pLoopUnit->noDefensiveBonus()) // commented out by K-Mod. The noDefBonus thing is already taken into account. { int iValue = pLoopUnit->currEffectiveStr(pDefendPlot, NULL) * 100; if (pDefendPlot->isCity(true, getTeam())) { iValue *= 100 + pLoopUnit->cityDefenseModifier(); iValue /= 100; } iValue *= 100; iValue /= (100 + pLoopUnit->cityAttackModifier() + pLoopUnit->getExtraCityAttackPercent()); iValue /= 2 + pLoopUnit->getLevel(); if (iValue > iBestUnitValue) { iBestUnitValue = iValue; pBestUnit = pLoopUnit; } } } if (NULL != pBestUnit && getNumUnits() > 1) { pBestUnit->joinGroup(NULL); } return pBestUnit; }
bool CvSelectionGroupAI::AI_separateEmptyTransports() { bool bSeparated = false; CLLNode<IDInfo>* pEntityNode = headUnitNode(); while (pEntityNode != NULL) { CvUnit* pLoopUnit = ::getUnit(pEntityNode->m_data); pEntityNode = nextUnitNode(pEntityNode); if ((pLoopUnit->AI_getUnitAIType() == UNITAI_ASSAULT_SEA) && (pLoopUnit->getCargo() == 0)) { pLoopUnit->joinGroup(NULL); bSeparated = true; } } return bSeparated; }
bool CvSelectionGroupAI::AI_separateImpassable() { CvPlayerAI& kPlayer = GET_PLAYER(getOwner()); bool bSeparated = false; CLLNode<IDInfo>* pEntityNode = headUnitNode(); while (pEntityNode != NULL) { CvUnit* pLoopUnit = ::getUnit(pEntityNode->m_data); pEntityNode = nextUnitNode(pEntityNode); if (kPlayer.AI_unitImpassableCount(pLoopUnit->getUnitType()) > 0) { pLoopUnit->joinGroup(NULL); bSeparated = true; } } return bSeparated; }
void CvSelectionGroupAI::AI_separate() { CLLNode<IDInfo>* pEntityNode; CvUnit* pLoopUnit; pEntityNode = headUnitNode(); while (pEntityNode != NULL) { pLoopUnit = ::getUnit(pEntityNode->m_data); pEntityNode = nextUnitNode(pEntityNode); pLoopUnit->joinGroup(NULL); if (pLoopUnit->plot()->getTeam() == getTeam()) { pLoopUnit->getGroup()->pushMission(MISSION_SKIP); } } }
void CvSelectionGroupAI::AI_separateEmptyTransports() { CLLNode<IDInfo>* pEntityNode; CvUnit* pLoopUnit; pEntityNode = headUnitNode(); while (pEntityNode != NULL) { pLoopUnit = ::getUnit(pEntityNode->m_data); pEntityNode = nextUnitNode(pEntityNode); if ((pLoopUnit->AI_getUnitAIType() == UNITAI_ASSAULT_SEA) && (pLoopUnit->getCargo() == 0)) { pLoopUnit->joinGroup(NULL); if (pLoopUnit->plot()->getTeam() == getTeam()) { pLoopUnit->getGroup()->pushMission(MISSION_SKIP); } } } }
void CvSelectionGroupAI::AI_separateImpassable() { CLLNode<IDInfo>* pEntityNode; CvUnit* pLoopUnit; CvPlayerAI& kPlayer = GET_PLAYER(getOwner()); pEntityNode = headUnitNode(); while (pEntityNode != NULL) { pLoopUnit = ::getUnit(pEntityNode->m_data); pEntityNode = nextUnitNode(pEntityNode); if( (kPlayer.AI_unitImpassableCount(pLoopUnit->getUnitType()) > 0) ) { pLoopUnit->joinGroup(NULL); if (pLoopUnit->plot()->getTeam() == getTeam()) { pLoopUnit->getGroup()->pushMission(MISSION_SKIP); } } } }
void CvSelectionGroupAI::AI_seperateNonAI(UnitAITypes eUnitAI) { CLLNode<IDInfo>* pEntityNode; CvUnit* pLoopUnit; pEntityNode = headUnitNode(); int iCounter = 0; //Rhye (loop fix) while (pEntityNode != NULL) { iCounter++; //Rhye (fix) if (iCounter > 20) break; //Rhye (fix) pLoopUnit = ::getUnit(pEntityNode->m_data); pEntityNode = nextUnitNode(pEntityNode); if (pLoopUnit->AI_getUnitAIType() != eUnitAI) { pLoopUnit->joinGroup(NULL); if (pLoopUnit->plot()->getTeam() == getTeam()) { pLoopUnit->getGroup()->pushMission(MISSION_SKIP); } } } }
// // Approximate how many turns this group would take to reduce pCity's defense modifier to zero // int CvSelectionGroup::getBombardTurns(CvCity* pCity) { PROFILE_FUNC(); bool bHasBomber = (area()->getNumAIUnits(getOwner(),UNITAI_ATTACK_AIR) > 0); bool bIgnoreBuildingDefense = bHasBomber; int iTotalBombardRate = (bHasBomber ? 16 : 0); CLLNode<IDInfo>* pUnitNode = headUnitNode(); while (pUnitNode != NULL) { CvUnit* pLoopUnit = ::getUnit(pUnitNode->m_data); pUnitNode = nextUnitNode(pUnitNode); if( pLoopUnit->bombardRate() > 0 ) { iTotalBombardRate += pLoopUnit->bombardRate(); bIgnoreBuildingDefense = (bIgnoreBuildingDefense || pLoopUnit->ignoreBuildingDefense()); } } int iBombardTurns = pCity->getDefenseModifier(bIgnoreBuildingDefense); if( !bIgnoreBuildingDefense ) { iBombardTurns *= 100; iBombardTurns /= std::max(25, (100 - pCity->getBuildingBombardDefense())); } iBombardTurns /= std::max(8, iTotalBombardRate); // added Sephi if (iTotalBombardRate==0) { return -1; } return iBombardTurns; }
// Returns true if the group has become busy... bool CvSelectionGroupAI::AI_update() { CLLNode<IDInfo>* pEntityNode; CvUnit* pLoopUnit; bool bDead; bool bFollow; PROFILE("CvSelectionGroupAI::AI_update"); FAssert(getOwnerINLINE() != NO_PLAYER); if (!AI_isControlled()) { return false; } if (getNumUnits() == 0) { return false; } // K-Mod / BBAI if (getActivityType() == ACTIVITY_SLEEP && !isHuman() && !getHeadUnit()->isCargo()) { setForceUpdate(true); } // end if (isForceUpdate()) { doForceUpdate(); // K-Mod (based on old code) } //FAssert(!(GET_PLAYER(getOwnerINLINE()).isAutoMoves())); // (no longer true in K-Mod) int iTempHack = 0; // XXX bDead = false; bool bFailedAlreadyFighting = false; //while ((m_bGroupAttack && !bFailedAlreadyFighting) || readyToMove()) while ((AI_isGroupAttack() && !isBusy()) || readyToMove()) // K-Mod { iTempHack++; if (iTempHack > 100) { FAssertMsg(false, "unit stuck in a loop"); CvUnit* pHeadUnit = getHeadUnit(); if (NULL != pHeadUnit) { if (GC.getLogging()) { TCHAR szOut[1024]; CvWString szTempString; getUnitAIString(szTempString, pHeadUnit->AI_getUnitAIType()); sprintf(szOut, "Unit stuck in loop: %S(%S)[%d, %d] (%S)\n", pHeadUnit->getName().GetCString(), GET_PLAYER(pHeadUnit->getOwnerINLINE()).getName(), pHeadUnit->getX_INLINE(), pHeadUnit->getY_INLINE(), szTempString.GetCString()); gDLL->messageControlLog(szOut); } pHeadUnit->finishMoves(); } break; } // if we want to force the group to attack, force another attack if (AI_isGroupAttack()) { AI_cancelGroupAttack(); groupAttack(m_iGroupAttackX, m_iGroupAttackY, MOVE_DIRECT_ATTACK, bFailedAlreadyFighting); } // else pick AI action else { CvUnit* pHeadUnit = getHeadUnit(); //if (pHeadUnit == NULL || pHeadUnit->isDelayedDeath()) if (pHeadUnit == NULL || pHeadUnit->doDelayedDeath()) // K-Mod { break; } //resetPath(); if (pHeadUnit->AI_update()) { // AI_update returns true when we should abort the loop and wait until next slice FAssert(!pHeadUnit->isDelayedDeath()); break; } } if (doDelayedDeath()) { bDead = true; break; } // if no longer group attacking, and force separate is true, then bail, decide what to do after group is split up // (UnitAI of head unit may have changed) if (!AI_isGroupAttack() && AI_isForceSeparate()) { AI_separate(); // pointers could become invalid... //return true; return false; // K-Mod } } if (!bDead) { if (!isHuman()) { bFollow = false; // if we not group attacking, then check for follow action if (!AI_isGroupAttack()) { pEntityNode = headUnitNode(); // K-Mod note: I've rearranged a few things below, and added 'bFirst'. bool bFirst = true; while ((pEntityNode != NULL) && readyToMove(true)) { pLoopUnit = ::getUnit(pEntityNode->m_data); pEntityNode = nextUnitNode(pEntityNode); if (bFirst) resetPath(); if (pLoopUnit->canMove()) { if (pLoopUnit->AI_follow(bFirst)) { bFollow = true; bFirst = true; // let the next unit start fresh. } else bFirst = false; } } // K-Mod end } if (doDelayedDeath()) { bDead = true; } if (!bDead) { if (!bFollow && readyToMove(true)) { pushMission(MISSION_SKIP); } } /************************************************************************************************/ /* BETTER_BTS_AI_MOD 04/28/10 jdog5000 */ /* */ /* Unit AI */ /************************************************************************************************/ // AI should never put units to sleep, how does this ever happen? //FAssert( getHeadUnit()->isCargo() || getActivityType() != ACTIVITY_SLEEP ); /************************************************************************************************/ /* BETTER_BTS_AI_MOD END */ /************************************************************************************************/ } } if (bDead) { //return true; return false; // K-Mod } return (isBusy() || isCargoBusy()); }
bool CvSelectionGroupAI::AI_isFull() { CLLNode<IDInfo>* pUnitNode; CvUnit* pLoopUnit; if (getNumUnits() > 0) { UnitAITypes eUnitAI = getHeadUnitAI(); // do two passes, the first pass, we ignore units with speical cargo int iSpecialCargoCount = 0; int iCargoCount = 0; // first pass, count but ignore special cargo units pUnitNode = headUnitNode(); while (pUnitNode != NULL) { pLoopUnit = ::getUnit(pUnitNode->m_data); pUnitNode = nextUnitNode(pUnitNode); if (pLoopUnit->AI_getUnitAIType() == eUnitAI) { if (pLoopUnit->cargoSpace() > 0) { iCargoCount++; } if (pLoopUnit->specialCargo() != NO_SPECIALUNIT) { iSpecialCargoCount++; } else if (!(pLoopUnit->isFull())) { return false; } } } // if every unit in the group has special cargo, then check those, otherwise, consider ourselves full if (iSpecialCargoCount >= iCargoCount) { pUnitNode = headUnitNode(); while (pUnitNode != NULL) { pLoopUnit = ::getUnit(pUnitNode->m_data); pUnitNode = nextUnitNode(pUnitNode); if (pLoopUnit->AI_getUnitAIType() == eUnitAI) { if (!(pLoopUnit->isFull())) { return false; } } } } return true; } return false; }
CvUnit* CvSelectionGroupAI::AI_getBestGroupSacrifice(const CvPlot* pPlot, bool bPotentialEnemy, bool bForce, bool bNoBlitz) const { int iBestValue = 0; CvUnit* pBestUnit = NULL; CLLNode<IDInfo>* pUnitNode = headUnitNode(); while (pUnitNode != NULL) { CvUnit* pLoopUnit = ::getUnit(pUnitNode->m_data); pUnitNode = nextUnitNode(pUnitNode); if (!pLoopUnit->isDead()) { bool bCanAttack = false; if (pLoopUnit->getDomainType() == DOMAIN_AIR) { bCanAttack = pLoopUnit->canAirAttack(); } else { bCanAttack = pLoopUnit->canAttack(); if (bCanAttack && bNoBlitz && pLoopUnit->isBlitz() && pLoopUnit->isMadeAttack()) { bCanAttack = false; } } if (bCanAttack) { if (bForce || pLoopUnit->canMove()) { if (bForce || pLoopUnit->canMoveInto(pPlot, true)) { int iValue = pLoopUnit->AI_sacrificeValue(pPlot); FAssertMsg(iValue > 0, "iValue is expected to be greater than 0"); /*************************************************************************************************/ /** BETTER AI (Summons make good groupattack sacrifice units) Sephi **/ /** **/ /** **/ /*************************************************************************************************/ if (pLoopUnit->getDuration()>0) { iValue+=10000; } /*************************************************************************************************/ /** END **/ /*************************************************************************************************/ /*************************************************************************************************/ /** BETTER AI (Block some Units from attacking at low odds) Sephi **/ /** **/ /** **/ /*************************************************************************************************/ if (!GET_PLAYER(pLoopUnit->getOwnerINLINE()).isHuman()) { if (pLoopUnit->AI_getUnitAIType()==UNITAI_WARWIZARD) { iValue=1; } if (pLoopUnit->AI_getUnitAIType()==UNITAI_HERO) { iValue=1; } if (pLoopUnit->getLevel()>4) { iValue=1; } } /*************************************************************************************************/ /** END **/ /*************************************************************************************************/ // we want to pick the last unit of highest value, so pick the last unit with a good value if (iValue >= iBestValue) { iBestValue = iValue; pBestUnit = pLoopUnit; } } } } } } return pBestUnit; }
bool CvSelectionGroupAI::AI_tradeRoutes() { PROFILE_FUNC(); const IDInfo kEurope(getOwnerINLINE(), CvTradeRoute::EUROPE_CITY_ID); CvCity* pPlotCity = plot()->getPlotCity(); CvPlayerAI& kOwner = GET_PLAYER(getOwnerINLINE()); std::set<int>::iterator it; std::map<IDInfo, int> cityValues; std::vector<CvTradeRoute*> routes; std::vector<int> routeValues; std::vector<bool> yieldsDelivered(NUM_YIELD_TYPES, false); std::vector<bool> yieldsToUnload(NUM_YIELD_TYPES, false); std::vector<int> yieldsOnBoard(NUM_YIELD_TYPES, false); if (!isHuman() || (getAutomateType() == AUTOMATE_TRANSPORT_FULL)) { std::vector<CvTradeRoute*> aiRoutes; kOwner.getTradeRoutes(aiRoutes); for (uint i = 0; i < aiRoutes.size(); ++i) { CvTradeRoute* pRoute = aiRoutes[i]; // transport feeder - start - Nightinggale CvCity* pDestinationCity = ::getCity(pRoute->getDestinationCity()); if (pDestinationCity != NULL && pDestinationCity->isAutoImportStopped(pRoute->getYield())) { // ignore trade routes where destination is using feeder service and is full continue; } // transport feeder - end - Nightinggale // traderoute fix - start - Nightinggale if (isHuman() && pRoute->getDestinationCity().eOwner != getOwnerINLINE()) { // humans can't transport to allied cities with fully automated transports continue; } // traderoute fix - end - Nightinggale CvCity* pSourceCity = ::getCity(pRoute->getSourceCity()); CvArea* pSourceWaterArea = pSourceCity->waterArea(); if ((pSourceCity != NULL) && ((getDomainType() != DOMAIN_SEA) || (pSourceWaterArea != NULL))) { int iSourceArea = (getDomainType() == DOMAIN_SEA) ? pSourceWaterArea->getID() : pSourceCity->getArea(); if (getDomainType() == DOMAIN_SEA ? plot()->isAdjacentToArea(iSourceArea) : (iSourceArea == getArea())) { if ((getDomainType() == DOMAIN_SEA) || (pRoute->getDestinationCity() != kEurope)) { routes.push_back(pRoute); routeValues.push_back(0); yieldsDelivered[pRoute->getYield()] = true; if (pPlotCity != NULL && ::getCity(pRoute->getDestinationCity()) == pPlotCity) { yieldsToUnload[pRoute->getYield()] = true; } cityValues[pRoute->getSourceCity()] = 0; cityValues[pRoute->getDestinationCity()] = 0; } } } } } else { for (it = m_aTradeRoutes.begin(); it != m_aTradeRoutes.end(); ++it) { CvTradeRoute* pRoute = kOwner.getTradeRoute(*it); CvCity* pSourceCity = ::getCity(pRoute->getSourceCity()); if (pSourceCity != NULL) { CvArea* pSourceWaterArea = pSourceCity->waterArea(); if (getDomainType() != DOMAIN_SEA || pSourceWaterArea != NULL) { int iSourceArea = (getDomainType() == DOMAIN_SEA) ? pSourceWaterArea->getID() : pSourceCity->getArea(); if (getDomainType() == DOMAIN_SEA ? plot()->isAdjacentToArea(iSourceArea) : (iSourceArea == getArea())) { if ((getDomainType() == DOMAIN_SEA) || (pRoute->getDestinationCity() != kEurope)) { routes.push_back(pRoute); routeValues.push_back(0); yieldsDelivered[pRoute->getYield()] = true; if (pPlotCity != NULL && ::getCity(pRoute->getDestinationCity()) == pPlotCity) { yieldsToUnload[pRoute->getYield()] = true; } cityValues[pRoute->getSourceCity()] = 0; cityValues[pRoute->getDestinationCity()] = 0; } } else { FAssertMsg(false, "Unexpected : Unit can't run trade route it's assigned to"); } } } } } if ((pPlotCity != NULL) && hasCargo()) { std::vector<CvUnit*> units; //Unload everything which we should unload here, or can't unload anywhere... CLLNode<IDInfo>* pUnitNode = plot()->headUnitNode(); while (pUnitNode != NULL) { CvUnit* pLoopUnit = ::getUnit(pUnitNode->m_data); pUnitNode = plot()->nextUnitNode(pUnitNode); if (pLoopUnit != NULL) { YieldTypes eYield = pLoopUnit->getYield(); CvUnit* pTransport = pLoopUnit->getTransportUnit(); if ((eYield != NO_YIELD) && pTransport != NULL && (yieldsToUnload[eYield] || !(yieldsDelivered[eYield]))) { if (pTransport->getGroup() == this && pLoopUnit->canUnload()) { units.push_back(pLoopUnit); } } } } for (uint i = 0; i < units.size(); ++i) { units[i]->unload(); } } short aiYieldsLoaded[NUM_YIELD_TYPES]; AI_getYieldsLoaded(aiYieldsLoaded); bool bNoCargo = true; for (int i = 0; i < NUM_YIELD_TYPES; ++i) { if (aiYieldsLoaded[i] > 0) { bNoCargo = false; break; } } if (!bNoCargo) { //We need to iterate over every destination city and see if we can unload. for (uint i = 0; i < routes.size(); ++i) { CvCity* pDestinationCity = ::getCity(routes[i]->getDestinationCity()); if ((pDestinationCity == NULL) || (pDestinationCity != pPlotCity)) { int iRouteValue = kOwner.AI_transferYieldValue(routes[i]->getDestinationCity(), routes[i]->getYield(), aiYieldsLoaded[routes[i]->getYield()]); if (iRouteValue > 0) { cityValues[routes[i]->getDestinationCity()] += iRouteValue; routeValues[i] += iRouteValue; } } } } //We need to iterate over every source city, and see if there's anything which needs moving to the respective destination city. //We apply some bias to the city we are presently at, but not too much - sometimes empty runs need to be made... //Basically this looks at the entire NEXT trade run (source-city to dest-city), with some bias given towards //starting it from pPlotCity as sourceCity. //If we are carrying cargo, only count cities where we can unload. for (uint i = 0; i < routes.size(); ++i) { CvCity* pSourceCity = ::getCity(routes[i]->getSourceCity()); if ((pSourceCity != NULL) && (bNoCargo || (cityValues[routes[i]->getSourceCity()] > 0))) { CvCity* pDestinationCity = ::getCity(routes[i]->getDestinationCity()); YieldTypes eYield = routes[i]->getYield(); // transport feeder - start - Nightinggale //int iAmount = pSourceCity->getYieldStored(eYield) - pSourceCity->getMaintainLevel(eYield); int iAmount = pSourceCity->getYieldStored(eYield) - pSourceCity->getAutoMaintainThreshold(eYield); // transport feeder - end - Nightinggale if (iAmount > 0) { int iExportValue = kOwner.AI_transferYieldValue(routes[i]->getSourceCity(), routes[i]->getYield(), -iAmount); int iImportValue = kOwner.AI_transferYieldValue(routes[i]->getDestinationCity(), routes[i]->getYield(), iAmount); int iRouteValue = (iExportValue + iImportValue + 2 * std::min(iExportValue, iImportValue)) / 4; if (pSourceCity == pPlotCity) { cityValues[routes[i]->getDestinationCity()] += 2 * iRouteValue; } else { cityValues[routes[i]->getSourceCity()] += iRouteValue; } routeValues[i] = iRouteValue; } } } IDInfo kBestDestination(NO_PLAYER, -1); int iBestDestinationValue = 0; for (std::map<IDInfo, int>::iterator it = cityValues.begin(); it != cityValues.end(); ++it) { int iValue = it->second; if (iValue > 0) { CvCity* pCity = ::getCity(it->first); if (pCity != NULL) { FAssert(!atPlot(pCity->plot())); if (generatePath(plot(), pCity->plot(), MOVE_NO_ENEMY_TERRITORY, true)) { iValue /= 1 + kOwner.AI_plotTargetMissionAIs(pCity->plot(), MISSIONAI_TRANSPORT, this, 0); } else { iValue = 0; } } if (iValue > iBestDestinationValue) { iBestDestinationValue = iValue; kBestDestination = it->first; } } } if ((pPlotCity != NULL) && (kBestDestination.eOwner != NO_PLAYER)) { //We need to keep looping and recalculating //For example a city might have "101" of an item, we want to move the first 100 but not the 1. //But it could also have 200, in which case we might want 2 loads of 100... //But it could also have 200 of two resources, and we'd want to move 100 of each... ///TKs MEd int iTestCount = 0; while (!isFull()) { iTestCount++; if (iTestCount == 1000) { FAssert(iTestCount == 0); int iID = pPlotCity->getID(); int iIDplayer = GET_PLAYER(kBestDestination.eOwner).getID(); // break; } if (iTestCount == 1001) { break; } ///TKe int iBestRoute = -1; int iBestRouteValue = 0; //Now, for any trade routes which this group is assigned to, try to pick up cargo here. for (uint i = 0; i < routes.size(); ++i) { CvCity* pSourceCity = ::getCity(routes[i]->getSourceCity()); if ((pSourceCity != NULL) && (routes[i]->getDestinationCity() == kBestDestination)) { CvCity* pDestinationCity = ::getCity(routes[i]->getDestinationCity()); YieldTypes eYield = routes[i]->getYield(); if ((pPlotCity == pSourceCity)) { // transport feeder - start - Nightinggale //int iAmount = pSourceCity->getYieldStored(eYield) - pSourceCity->getMaintainLevel(eYield); int iAmount = pSourceCity->getYieldStored(eYield) - pSourceCity->getAutoMaintainThreshold(eYield); // transport feeder - end - Nightinggale if (iAmount > 0) { int iExportValue = kOwner.AI_transferYieldValue(routes[i]->getSourceCity(), routes[i]->getYield(), -iAmount); int iImportValue = kOwner.AI_transferYieldValue(routes[i]->getDestinationCity(), routes[i]->getYield(), iAmount); int iRouteValue = (iExportValue + iImportValue + 2 * std::min(iExportValue, iImportValue)) / 4; if (iRouteValue > iBestRouteValue) { iBestRouteValue = iRouteValue; iBestRoute = i; } } } } } if (iBestRouteValue > 0) { CLLNode<IDInfo>* pUnitNode = headUnitNode(); while (pUnitNode != NULL) { CvUnit* pLoopUnit = ::getUnit(pUnitNode->m_data); pUnitNode = nextUnitNode(pUnitNode); if (pLoopUnit != NULL) { if (pLoopUnit->canLoadYield(plot(), routes[iBestRoute]->getYield(), false)) { pLoopUnit->loadYield(routes[iBestRoute]->getYield(), false); break; } } } } else { break; } } //XXX fill hold. } if ((kBestDestination.eOwner == NO_PLAYER) && hasCargo()) { // Transport group is full and can't find any destination CvCity* pCity = kOwner.AI_findBestPort(); if (pCity != NULL && !atPlot(pCity->plot())) { kBestDestination = pCity->getIDInfo(); } } //As a final step, we could consider loading yields which would be useful as parts of delivery runs... if (kBestDestination != kEurope) { CvCity* pBestDestinationCity = ::getCity(kBestDestination); if (pBestDestinationCity != NULL) { FAssert(!atPlot(pBestDestinationCity->plot())); pushMission(MISSION_MOVE_TO, pBestDestinationCity->getX_INLINE(), pBestDestinationCity->getY_INLINE(), MOVE_NO_ENEMY_TERRITORY, false, false, MISSIONAI_TRANSPORT, pBestDestinationCity->plot()); if (atPlot(pBestDestinationCity->plot())) { //Unload any goods if required (we can always pick them back up if this is an i+e city). std::vector<CvUnit*> units; CLLNode<IDInfo>* pUnitNode = plot()->headUnitNode(); CvUnit* pLoopUnit; while (pUnitNode != NULL) { pLoopUnit = ::getUnit(pUnitNode->m_data); pUnitNode = plot()->nextUnitNode(pUnitNode); YieldTypes eYield = pLoopUnit->getYield(); if ((eYield != NO_YIELD) && pLoopUnit->isCargo()) { if (pLoopUnit->getTransportUnit()->getGroup() == this && pLoopUnit->canUnload()) { units.push_back(pLoopUnit); } } } for (uint i = 0; i < units.size(); ++i) { units[i]->unload(); } } return true; } } else { if (isHuman()) { getHeadUnit()->AI_setUnitAIState(UNITAI_STATE_SAIL); } } return false; }
CvUnit* CvSelectionGroupAI::AI_getBestGroupAttacker(const CvPlot* pPlot, bool bPotentialEnemy, int& iUnitOdds, bool bForce, bool bNoBlitz) const { CLLNode<IDInfo>* pUnitNode; CvUnit* pLoopUnit; CvUnit* pBestUnit; int iPossibleTargets; int iValue; int iBestValue; int iOdds; int iBestOdds; iBestValue = 0; iBestOdds = 0; pBestUnit = NULL; pUnitNode = headUnitNode(); bool bIsHuman = (pUnitNode != NULL) ? GET_PLAYER(::getUnit(pUnitNode->m_data)->getOwnerINLINE()).isHuman() : true; while (pUnitNode != NULL) { pLoopUnit = ::getUnit(pUnitNode->m_data); pUnitNode = nextUnitNode(pUnitNode); if (!pLoopUnit->isDead()) { bool bCanAttack = false; if (pLoopUnit->getDomainType() == DOMAIN_AIR) { bCanAttack = pLoopUnit->canAirAttack(); } else { bCanAttack = pLoopUnit->canAttack(); if (bCanAttack && bNoBlitz && pLoopUnit->isBlitz() && pLoopUnit->isMadeAttack()) { bCanAttack = false; } } if (bCanAttack) { if (bForce || pLoopUnit->canMove()) { if (bForce || pLoopUnit->canMoveInto(pPlot, /*bAttack*/ true, /*bDeclareWar*/ bPotentialEnemy)) { iOdds = pLoopUnit->AI_attackOdds(pPlot, bPotentialEnemy); iValue = iOdds; FAssertMsg(iValue > 0, "iValue is expected to be greater than 0"); if (pLoopUnit->collateralDamage() > 0) { iPossibleTargets = std::min((pPlot->getNumVisibleEnemyDefenders(pLoopUnit) - 1), pLoopUnit->collateralDamageMaxUnits()); if (iPossibleTargets > 0) { iValue *= (100 + ((pLoopUnit->collateralDamage() * iPossibleTargets) / 5)); iValue /= 100; } } // if non-human, prefer the last unit that has the best value (so as to avoid splitting the group) if (iValue > iBestValue || (!bIsHuman && iValue > 0 && iValue == iBestValue)) { iBestValue = iValue; iBestOdds = iOdds; pBestUnit = pLoopUnit; } } } } } } iUnitOdds = iBestOdds; return pBestUnit; }
// Returns true if the group has become busy... bool CvSelectionGroupAI::AI_update() { CLLNode<IDInfo>* pEntityNode; CvUnit* pLoopUnit; bool bDead; bool bFollow; PROFILE("CvSelectionGroupAI::AI_update"); FAssert(getOwnerINLINE() != NO_PLAYER); if (!AI_isControlled()) { return false; } if (getNumUnits() == 0) { return false; } if (isForceUpdate()) { clearMissionQueue(); // XXX ??? setActivityType(ACTIVITY_AWAKE); setForceUpdate(false); // if we are in the middle of attacking with a stack, cancel it AI_cancelGroupAttack(); } FAssert(!(GET_PLAYER(getOwnerINLINE()).isAutoMoves())); int iTempHack = 0; // XXX bDead = false; bool bFailedAlreadyFighting = false; while ((m_bGroupAttack && !bFailedAlreadyFighting) || readyToMove()) { iTempHack++; if (iTempHack > 100) { FAssert(false); CvUnit* pHeadUnit = getHeadUnit(); if (NULL != pHeadUnit) { if (GC.getLogging()) { TCHAR szOut[1024]; CvWString szTempString; getUnitAIString(szTempString, pHeadUnit->AI_getUnitAIType()); sprintf(szOut, "Unit stuck in loop: %S(%S)[%d, %d] (%S)\n", pHeadUnit->getName().GetCString(), GET_PLAYER(pHeadUnit->getOwnerINLINE()).getName(), pHeadUnit->getX_INLINE(), pHeadUnit->getY_INLINE(), szTempString.GetCString()); gDLL->messageControlLog(szOut); } pHeadUnit->finishMoves(); } break; } // if we want to force the group to attack, force another attack if (m_bGroupAttack) { m_bGroupAttack = false; groupAttack(m_iGroupAttackX, m_iGroupAttackY, MOVE_DIRECT_ATTACK, bFailedAlreadyFighting); } // else pick AI action else { CvUnit* pHeadUnit = getHeadUnit(); if (pHeadUnit == NULL || pHeadUnit->isDelayedDeath()) { break; } resetPath(); if (pHeadUnit->AI_update()) { // AI_update returns true when we should abort the loop and wait until next slice break; } } if (doDelayedDeath()) { bDead = true; break; } // if no longer group attacking, and force separate is true, then bail, decide what to do after group is split up // (UnitAI of head unit may have changed) if (!m_bGroupAttack && AI_isForceSeparate()) { AI_separate(); // pointers could become invalid... return true; } } if (!bDead) { if (!isHuman()) { bFollow = false; // if we not group attacking, then check for follow action if (!m_bGroupAttack) { pEntityNode = headUnitNode(); while ((pEntityNode != NULL) && readyToMove(true)) { pLoopUnit = ::getUnit(pEntityNode->m_data); pEntityNode = nextUnitNode(pEntityNode); if (pLoopUnit->canMove()) { resetPath(); if (pLoopUnit->AI_follow()) { bFollow = true; break; } } } } if (doDelayedDeath()) { bDead = true; } if (!bDead) { if (!bFollow && readyToMove(true)) { pushMission(MISSION_SKIP); } } } } if (bDead) { return true; } return (isBusy() || isCargoBusy()); }
CvUnit* CvSelectionGroupAI::AI_getBestGroupAttacker(const CvPlot* pPlot, bool bPotentialEnemy, int& iUnitOdds, bool bForce, bool bNoBlitz) const { PROFILE_FUNC(); int iBestValue = 0; int iBestOdds = 0; CvUnit* pBestUnit = NULL; CLLNode<IDInfo>* pUnitNode = headUnitNode(); bool bIsHuman = (pUnitNode != NULL) ? GET_PLAYER(::getUnit(pUnitNode->m_data)->getOwnerINLINE()).isHuman() : true; while (pUnitNode != NULL) { CvUnit* pLoopUnit = ::getUnit(pUnitNode->m_data); pUnitNode = nextUnitNode(pUnitNode); if (!pLoopUnit->isDead()) { bool bCanAttack = false; if (pLoopUnit->getDomainType() == DOMAIN_AIR) { bCanAttack = pLoopUnit->canAirAttack(); } else { bCanAttack = pLoopUnit->canAttack(); if (bCanAttack && bNoBlitz && pLoopUnit->isBlitz() && pLoopUnit->isMadeAttack()) { bCanAttack = false; } } if (bCanAttack) { if (bForce || pLoopUnit->canMove()) { if (bForce || pLoopUnit->canMoveInto(pPlot, /*bAttack*/ true, /*bDeclareWar*/ bPotentialEnemy)) { /************************************************************************************************/ /* BETTER_BTS_AI_MOD 02/21/10 jdog5000 */ /* */ /* Lead From Behind */ /************************************************************************************************/ // From Lead From Behind by UncutDragon if (GC.getLFBEnable() && GC.getLFBUseCombatOdds()) { //pLoopUnit->LFBgetBetterAttacker(&pBestUnit, pPlot, bPotentialEnemy, iBestOdds, iValue); pLoopUnit->LFBgetBetterAttacker(&pBestUnit, pPlot, bPotentialEnemy, iBestOdds, iBestValue); // K-Mod. } else { int iOdds = pLoopUnit->AI_attackOdds(pPlot, bPotentialEnemy); int iValue = iOdds; FAssertMsg(iValue > 0, "iValue is expected to be greater than 0"); if (pLoopUnit->collateralDamage() > 0) { int iPossibleTargets = std::min((pPlot->getNumVisibleEnemyDefenders(pLoopUnit) - 1), pLoopUnit->collateralDamageMaxUnits()); if (iPossibleTargets > 0) { iValue *= (100 + ((pLoopUnit->collateralDamage() * iPossibleTargets) / 5)); iValue /= 100; } } // if non-human, prefer the last unit that has the best value (so as to avoid splitting the group) if (iValue > iBestValue || (!bIsHuman && iValue > 0 && iValue == iBestValue)) { iBestValue = iValue; iBestOdds = iOdds; pBestUnit = pLoopUnit; } } /************************************************************************************************/ /* BETTER_BTS_AI_MOD END */ /************************************************************************************************/ } } } } } iUnitOdds = iBestOdds; return pBestUnit; }
// K-Mod. I've removed bCheckMove, and changed bCheckCanAttack to include checks for moves, and for hasAlreadyAttacked / blitz int CvSelectionGroupAI::AI_sumStrength(const CvPlot* pAttackedPlot, DomainTypes eDomainType, bool bCheckCanAttack) const { CLLNode<IDInfo>* pUnitNode; CvUnit* pLoopUnit; int strSum = 0; bool bDefenders = pAttackedPlot ? pAttackedPlot->isVisibleEnemyUnit(getHeadOwner()) : false; // K-Mod bool bCountCollateral = pAttackedPlot && pAttackedPlot != plot(); // K-Mod pUnitNode = headUnitNode(); int iBaseCollateral = bCountCollateral ? iBaseCollateral = estimateCollateralWeight(pAttackedPlot, pAttackedPlot->getTeam() == getTeam() ? NO_TEAM : pAttackedPlot->getTeam()) : 0; while (pUnitNode != NULL) { pLoopUnit = ::getUnit(pUnitNode->m_data); pUnitNode = nextUnitNode(pUnitNode); if (!pLoopUnit->isDead()) { // K-Mod. (original checks deleted.) if (bCheckCanAttack) { if (pLoopUnit->getDomainType() == DOMAIN_AIR) { if (!pLoopUnit->canAirAttack() || !pLoopUnit->canMove() || (pAttackedPlot && bDefenders && !pLoopUnit->canMoveInto(pAttackedPlot, true, true))) continue; // can't attack. } else { if (!pLoopUnit->canAttack() || !pLoopUnit->canMove() || (pAttackedPlot && bDefenders && !pLoopUnit->canMoveInto(pAttackedPlot, true, true)) || (!pLoopUnit->isBlitz() && pLoopUnit->isMadeAttack())) continue; // can't attack. } } // K-Mod end if (eDomainType == NO_DOMAIN || pLoopUnit->getDomainType() == eDomainType) { strSum += pLoopUnit->currEffectiveStr(pAttackedPlot, pLoopUnit); // K-Mod estimate the attack power of collateral units. (cf with calculation in AI_localAttackStrength) if (bCountCollateral && pLoopUnit->collateralDamage() > 0) { int iPossibleTargets = pLoopUnit->collateralDamageMaxUnits(); // If !bCheckCanAttack, then lets not assume pAttackPlot won't get more units on it. if (bCheckCanAttack && pAttackedPlot->isVisible(getTeam(), false)) iPossibleTargets = std::min(iPossibleTargets, pAttackedPlot->getNumVisibleEnemyDefenders(pLoopUnit) - 1); if (iPossibleTargets > 0) { // collateral damage is not trivial to calculate. This estimate is pretty rough. // (Note: collateralDamage() and iBaseCollateral both include factors of 100.) strSum += pLoopUnit->baseCombatStr() * iBaseCollateral * pLoopUnit->collateralDamage() * iPossibleTargets / 10000; } } // K-Mod end } } } return strSum; }