//Function used to tie together whole program and allow us to use a single function to invoke an A Star search.
void PathFind(bool &goalReached, deque <unique_ptr < SCoords > > &openList, deque <unique_ptr < SCoords > > &closedList, SCoords mapEnd, SCoords &north, SCoords &east, SCoords &south, SCoords &west
	, int mapArray[10][10], IModel* mapSquares[10][10], IMesh* squareMesh, int newCost, int existingCost, string visitedNodeSkin, string openListNodeSkin, bool &validNode)
{
	unique_ptr<SCoords> currentNode(new SCoords);
	if (!goalReached || openList.empty())
	{
		currentNode.reset(new SCoords);//Reset the current node every loop because we move current node onto closed list at the end of every loop.
		currentNode = move(openList.front());//We then set current node to the first value on open list and pop the value from open list.
		openList.pop_front();
		mapSquares[currentNode->y][currentNode->x]->SetSkin(visitedNodeSkin);
		if (CheckFinish(currentNode.get(), mapEnd.x, mapEnd.y))//Check if the end goal has been reached. If it has then we exit the loop after dispalying an end message.
		{
			openList.push_back(move(currentNode));//We then push the final node back onto open list so we can move through it's parent chain.
			goalReached = true;
			return;
		}
		//If the current node isn't the end goal then we generate new nodes.
		else
		{
			GenerateNodes(north, east, south, west, currentNode.get());
			//Then we check if the nodes have already been visited by checking openlist, if they haven't then we insert them. 
			CalculatePath(currentNode.get(), mapArray, newCost, existingCost, north, mapEnd, openList, closedList, validNode);
			//If the node is a valid node,i.e. the node isn't a wall then a skin is applied to the square to indicate that it is a viable node.
			if (validNode)
			{
				mapSquares[north.y][north.x]->SetSkin(openListNodeSkin);
			}
			validNode = false;
			CalculatePath(currentNode.get(), mapArray, newCost, existingCost, east, mapEnd, openList, closedList, validNode);
			if (validNode)
			{
				mapSquares[east.y][east.x]->SetSkin(openListNodeSkin);
			}
			validNode = false;
			CalculatePath(currentNode.get(), mapArray, newCost, existingCost, south, mapEnd, openList, closedList, validNode);
			if (validNode)
			{
				mapSquares[south.y][south.x]->SetSkin(openListNodeSkin);
			}
			validNode = false;
			CalculatePath(currentNode.get(), mapArray, newCost, existingCost, west, mapEnd, openList, closedList, validNode);
			if (validNode)
			{
				mapSquares[west.y][west.x]->SetSkin(openListNodeSkin);
			}
			validNode = false;
			closedList.push_back(move(currentNode));
		}
		//The list is then sorted based on score from lowest to highest.
		sort(openList.begin(), openList.end(), CompareCoords);
	}
}
Пример #2
0
bool PathFinder::UpdateMap(vector<vector<bool>>& tileMap, const Vector2 & start, const Vector2 & end)
{
	AStar::Update(tileMap);

	if (CalculatePath(start, end))
		return true;

	return false;
}
Пример #3
0
int	PathPlanner :: GetPathToCover(vector3df startPos, vector3df viewerPos, std::vector<vector3df> *path)
{
	// Make sure the path is empty:
	path->clear();

	AStarPathToCoverGoal* goal = new AStarPathToCoverGoal(viewerPos, navGraph);
	int pathLength = CalculatePath(startPos, path, goal);
	delete goal;

	return pathLength;
}
Пример #4
0
    void State::InitializeState(const Player& player)
    {
        const Room& room = player.GetRoom();

        for (size_t i = 0; i < room.GetTileCount(); ++i)
        {
            const ITile* tile = room.GetTile(i);
            switch (tile->GetTileType())
            {
            case TileType::Treasure:
                {
                    auto pTreasure = static_cast<const TileTreasure*>(tile);
                    if(!pTreasure->isEmpty())
                    {
                        StateNode* node = new StateNode(pTreasure);
                        CalculatePath(node, tile);
                        listNodes.push_back(*node);
                    }
                    break;
                }
            case TileType::Monster:
                {
                   auto pMonster = static_cast<const TileMonster*>(tile);
                   if(!pMonster->isEmpty())
                   {
                        StateNode* node = new StateNode(pMonster);
                        CalculatePath(node, tile);
                        listNodes.push_back(*node);
                   }
                    break;
                }
            case TileType::Floor:
            case TileType::Wall:

            default:
                break;

            }
        }
    }
Пример #5
0
int PathPlanner :: GetPathToPos(vector3df startPos, vector3df endPos, std::vector<vector3df> *path)
{
	// Make sure the path is empty:
	path->clear();

	// The Path vector is aranged so that the exact endPos is the first element and
	// the startPos is the last element.  Characters using the path will pop
	// elements from the back of the vector so the final element popped will be the exact
	// endPos.  We push it onto the path now so that it will be the first element.
	path->push_back(endPos);

	AStarPathToPositionGoal* goal = new AStarPathToPositionGoal(endPos, navGraph);
	
	int pathLength = CalculatePath(startPos, path, goal);
	delete goal;

	return pathLength;
}
Пример #6
0
void YARPViterbi::ShowPath()
{
  CalculatePath();
  for (int i=0; i<index; i++)
    {
      if (path(0,i)==-1)
	{
	  printf("*");
	}
      else
	{
	  printf("%d",path(0,i));
	}
      if (K>=10)
	{
	  printf(" ");
	}
    }
  printf(" costs %g\n", GetCost());
}
Пример #7
0
	virtual void Resume( )
	{
		CalculatePath( );
		MoveNext( );
	}
Пример #8
0
bool ProcessBattleStepAI(iBattleEngine& engine)
{
	bool bRes = false;
	const sint32 INV_CVAL = -65536;
	_LOG(_T(">> Begin of Battle AI <<\r\n"));

	iBattleUnit* pCurUnit = engine.TurnSeq().CurUnit();

	if (iBattleUnit_CreatGroup* pCreatUnit = DynamicCast<iBattleUnit_CreatGroup*>(pCurUnit)) {
		iBattleGroup* pGroup = pCreatUnit->GetCreatGroup();

		// Shoot list
		if (pGroup->ShootListCount()) {
	#ifdef DEBUG_BATTLE_AI
			_LOG(iFormat(_T("Shot targets found:%d\r\n"),pGroup->ShootListCount()).CStr());
			for (uint32 ll=0; ll<pGroup->ShootListCount(); ++ll) 
				_LOG(iFormat(_T("%d,%d\r\n"),pGroup->GetShootEntry(ll)->m_pos.x,pGroup->GetShootEntry(ll)->m_pos.y).CStr());
	#endif // DEBUG_BATTLE_AI

			// select appropriate target
			sint32 cval = INV_CVAL;
			sint32 penalty = 0;
			iPoint mCell;
			for (uint32 xx=0; xx<pGroup->ShootListCount(); ++xx) {
				const iBattleGroup::iShootEntry* se = pGroup->GetShootEntry(xx);
				iBattleGroup* pTarget = engine.FindGroup(se->m_pos);
				check(pTarget);
				sint32 val = CalcShootValue(pGroup, se, pTarget);
				if ( cval == INV_CVAL || cval < val) {
					cval = val;
					mCell = se->m_pos;
					penalty = se->m_penalty;
				}
			}
			engine.Shot(mCell, penalty);
			bRes = true;
		} else if (pGroup->MeleeListCount()) {
	#ifdef DEBUG_BATTLE_AI
			_LOG(iFormat(_T("Melee targets found:%d\r\n"),pGroup->MeleeListCount()).CStr());
			for (uint32 ll=0; ll<pGroup->MeleeListCount(); ++ll) 
				_LOG(iFormat(_T("%d,%d\r\n"),pGroup->GetMeleeEntry(ll)->m_pos.x,pGroup->GetMeleeEntry(ll)->m_pos.y).CStr());
	#endif // DEBUG_BATTLE_AI

			// select appropriate target and direction
			sint32 cval = INV_CVAL;
			iPoint mCell;
			uint16 mDir = 0xffff;
			for (uint32 xx=0; xx<pGroup->MeleeListCount(); ++xx) {
				const iBattleGroup::iMeleeEntry* me = pGroup->GetMeleeEntry(xx);
				iBattleGroup* pTarget = engine.FindGroup(me->m_pos);
				check(pTarget);
				sint32 val = CalcMeleeValue(pGroup, me, pTarget);
				for (uint16 dd=0; dd<12; ++dd) {
					if (me->m_cfg & (1<<dd)) {
						sint32 dval = val;
						iPoint mp;
						uint8 aor;
						pGroup->GetMeleePosition(pTarget, me->m_pos, dd, mp, aor);
						dval -= (sint32)pGroup->DistMap().GetAt(mp.x,mp.y).dirs[aor].val;
						if ( cval == INV_CVAL || cval < dval) {
							cval = dval;
							mCell = me->m_pos;
							mDir = dd;
						}
					}
				}
			}
			check( mDir != 0xffff );
			engine.Melee(mCell, mDir);
			bRes = true;
		} else if (pGroup->PotTargetsCount()) {
			// first try to wait
			if (pCurUnit->CanWait()) {
				_LOG(_T("Found potential melee targets. Wait.\r\n"));
				engine.Wait();
			} else {
				#ifdef DEBUG_BATTLE_AI
						_LOG(iFormat(_T("Potential melee targets found:%d\r\n"),pGroup->PotTargetsCount()).CStr());
						for (uint32 ll=0; ll<pGroup->PotTargetsCount(); ++ll) 
							_LOG(iFormat(_T("%d,%d\r\n"),pGroup->GetPotTargetEntry(ll)->m_pos.x,pGroup->GetPotTargetEntry(ll)->m_pos.y).CStr());
				#endif // DEBUG_BATTLE_AI

				// select appropriate target and direction
				sint32 cval = INV_CVAL;
				iPoint mCell;
				uint16 mDir;
				uint8 mOrient;
				iPoint mPos;
				for (uint32 xx=0; xx<pGroup->PotTargetsCount(); ++xx) {
					const iBattleGroup::iMeleeEntry* me = pGroup->GetPotTargetEntry(xx);
					iBattleGroup* pTarget = engine.FindGroup(me->m_pos);
					check(pTarget);
					sint32 val = CalcMeleeValue(pGroup, me, pTarget);
					for (uint16 dd=0; dd<12; ++dd) {
						if (me->m_cfg & (1<<dd)) {
							sint32 dval = val;
							iPoint mp;
							uint8 aor;
							pGroup->GetMeleePosition(pTarget, me->m_pos, dd, mp, aor);
							dval -= (sint32)pGroup->DistMap().GetAt(mp.x,mp.y).dirs[aor].val;
							if ( cval == INV_CVAL || cval < dval) {
								cval = dval;
								mCell = me->m_pos;
								mDir = dd;
								mOrient = aor;
								mPos = mp;
							}
						}
					}
				}

				CalculatePath(pGroup, mPos, mOrient);
				_LOG(iFormat(_T("Group at (%d,%d) move to (%d,%d)\r\n"),pGroup->Pos().x,pGroup->Pos().y,mPos.x,mPos.y).CStr());
				engine.Move(mPos, mOrient);
				bRes = true;
			}
		} else {
			if (pCurUnit->CanWait()) {
				_LOG(_T("Nothing to do. Wait.\r\n"));
				engine.Wait();
			} else {
				_LOG(_T("Nothing to do. Skip the turn.\r\n"));
				bRes = engine.Defend();
			}
		}
	} else if (iBattleUnit_Catapult* pCatapult = DynamicCast<iBattleUnit_Catapult*>(pCurUnit)) {
		engine.CatapultShot(engine.CastleFort()->GetTarget());
		bRes = true;
	} else if (iBattleUnit_Turret* pTurret = DynamicCast<iBattleUnit_Turret*>(pCurUnit)) {
		iBattleGroup* pTarget = SelectTurretTarget(engine);
		check(pTarget);
		engine.TurretShot(pTarget);
		bRes = true;
	} else {
		check(0);
	}

	_LOG(_T(">> End of Battle AI <<\r\n"));
	return bRes;
}