IBF_Result IBFactDef_HasValidPath::Test(const vector<IBObject*>& aUserData) { void* pOwner = m_pPlanner->GetOwner(); ASSERT(pOwner != NULL); ASSERT(aUserData.size() == GetDegree()); BLBot* pBot = static_cast<BLBot*>(pOwner); Path* pPath = reinterpret_cast<IBPath*>(aUserData[0]); Vector2* pStart = reinterpret_cast<IBVector2*>(aUserData[1]); Vector2* pTarget = reinterpret_cast<IBVector2*>(aUserData[2]); IBInt* pDist = reinterpret_cast<IBInt*>(aUserData[3]); if (pPath == NULL || pStart == NULL || pTarget == NULL || pDist == NULL) return IBF_UNKNOW; if (!pPath->IsValid()) return IBF_FAIL; if (pPath->GetStart() != *pStart) return IBF_FAIL; if (pBot->GetWorld().GetGrid().Distance(pPath->GetTarget(), *pTarget) > pDist->GetValue()) return IBF_FAIL; return (pBot->GetWorld().TestPath(*pPath) ? IBF_OK : IBF_FAIL); }
//makes the bots moves for the turn void AntWar::MakeMoves() { NavDijkstra m_oNavDijkstra(m_oWorld.GetGrid()); NavAStar m_oNavAStar(m_oWorld.GetGrid()); int iExplMin = 5; float fExplCoeff = 0.1f; int iAntCount = m_oWorld.GetAntCount(); int iExpl = max(min(iExplMin, iAntCount), (int)(iAntCount * fExplCoeff)); int iProtect = 0;//max<int>(0, min<int>(m_oWorld.GetMinDistCount(), (int)m_oWorld.GetAntCount() - iExpl)); int iGuard = max<int>(0, iAntCount - (iExpl + iProtect)); DistAntMap::const_reverse_iterator begin = m_oWorld.GetAntByDist().rbegin(); DistAntMap::const_reverse_iterator end = m_oWorld.GetAntByDist().rend(); DistAntMap::const_reverse_iterator it; // Default Explore / Guard / Protect int i; for (i=0, it=begin ; it != end ; ++it, ++i) { Ant* pAnt = it->second; if (pAnt->GetPlayer() > 0) continue; Vector2 loc = pAnt->GetLocation(); if (i<iExpl) pAnt->SetRole(Explore); if (i>=iExpl && i<iExpl+iGuard) pAnt->SetRole(Guard); if (i>=iExpl+iGuard) pAnt->SetRole(Protect); } // Attack for (uint i=0 ; i<m_oWorld.GetAntCount() ; ++i) { Ant& oAnt = m_oWorld.GetAnt(i); if (oAnt.GetPlayer() == 0) oAnt.TestAnts(m_oWorld); } for (uint i=0 ; i<m_oWorld.GetAntCount() ; ++i) { Ant& oAnt = m_oWorld.GetAnt(i); if (oAnt.GetPlayer() == 0) oAnt.ReachAllies(m_oWorld); } // Loot vector<Vector2> aLootLoc; aLootLoc.reserve(m_oWorld.GetEnemyHills().size() + m_oWorld.GetFoods().size()); for (uint i=0 ; i<m_oWorld.GetEnemyHills().size() ; ++i) { aLootLoc.push_back(m_oWorld.GetEnemyHills()[i]); } for (uint i=0 ; i<m_oWorld.GetFoods().size() ; ++i) { aLootLoc.push_back(m_oWorld.GetFoods()[i]); } if (aLootLoc.size()) { vector<Vector2> aLootAnt; for (uint i=0 ; i<m_oWorld.GetAntCount() ; ++i) { Ant& oAnt = m_oWorld.GetAnt(i); if (oAnt.GetPlayer() > 0) continue; if (oAnt.GetRole() == Attack) continue; aLootAnt.push_back(oAnt.GetLocation()); } m_oNavDijkstra.Explore(aLootLoc, aLootAnt, 0, m_oWorld.GetTurn()); #ifdef MYDEBUG //m_oNavDijkstra.PrintDebug(); #endif typedef map<Vector2, set<Path>> AllPathMap; typedef pair<Vector2, set<Path>> AllPathPair; AllPathMap aAllPath; for (uint i=0 ; i<aLootAnt.size() ; ++i) { Path oPath; if (m_oNavDijkstra.GetPath(aLootAnt[i], oPath)) { Vector2 start = oPath.GetStart(); AllPathMap::iterator it = aAllPath.find(start); if (it == aAllPath.end()) { pair<AllPathMap::iterator, bool> res = aAllPath.insert(AllPathPair(start, set<Path>())); it = res.first; } it->second.insert(oPath); } } for (AllPathMap::const_iterator it = aAllPath.begin() ; it != aAllPath.end() ; ++it) { const Path& oPath = *(it->second.begin()); Ant& oAnt = m_oWorld.GetAnt(oPath.GetTarget()); ASSERT(oAnt.GetPlayer() == 0); if (oAnt.GetPath().GetLength() == 0) { oAnt.SetPath(oPath.GetInverse()); oAnt.SetRole(Loot); } } } // Compute Path for (i=0 ; i<(int)m_oWorld.GetAntCount() ; ++i) { Ant& oAnt = m_oWorld.GetAnt(i); if (oAnt.GetPlayer() > 0) continue; if (oAnt.GetRole() == Explore) { if (m_oNavDijkstra.FindNearest(oAnt.GetLocation(), NavDijkstra::Undiscovered, oAnt.GetPath(), m_oWorld.GetTurn()) == false) oAnt.SetRole(Guard); } if (oAnt.GetRole() == Guard) { if (m_oNavDijkstra.FindNearest(oAnt.GetLocation(), NavDijkstra::Unvisible, oAnt.GetPath(), m_oWorld.GetTurn()) == false) oAnt.SetRole(Protect); } if (oAnt.GetRole() == Attack || oAnt.GetRole() == Help) { m_oNavAStar.FindPath(oAnt.GetLocation(), oAnt.GetFirstEnemy(), m_oWorld.GetAttackRadiusSq(), oAnt.GetPath(), m_oWorld.GetTurn()); } if (oAnt.GetRole() == Flee) { if (m_oNavAStar.FindPath(oAnt.GetLocation(), m_oWorld.GetFriendHills()[0], 0, oAnt.GetPath(), m_oWorld.GetTurn()) == false) { // ? } } //if (oAnt.GetRole() == Protect) //{ // if (m_oNavDijkstra.FindNearest(oAnt.GetLocation(), m_oWorld.GetBestDistCase(), oAnt.GetPath(), m_oWorld.GetTurn()) == false) // { // } //} if (oAnt.GetPath().GetLength() == 0 && m_oWorld.GetGrid().GetCase(oAnt.GetLocation()).GetAntInfluence() <= 0) { m_oNavDijkstra.FindNearest(oAnt.GetLocation(), NavDijkstra::Safe, oAnt.GetPath(), m_oWorld.GetTurn()); } } // Exec Move for (it=begin, i=0 ; it != m_oWorld.GetAntByDist().rend(); ++it, ++i) { Ant* pAnt = it->second; if (pAnt->GetPlayer() == 0 && pAnt->GetPath().GetLength() > 0) { EDirection dir; Vector2 target = pAnt->GetPath()[0]; if (m_oWorld.IsEmpty(target)) { dir = m_oWorld.GetDirection(pAnt->GetLocation(), target); ExecMove(pAnt->GetLocation(), dir); } } } };