Example #1
0
//------------------------- CreatePathBFS --------------------------------
//
//  uses BFS to find a path between the start and target cells.
//  Stores the path as a series of node indexes in m_Path.
//------------------------------------------------------------------------
void BattleMap::createPathBFS()
{
	//set current algorithm
	m_CurrentAlgorithm = AT_BFS;

	//clear any existing path
	m_Path.clear();
	m_SubTree.clear();

	//create and start a timer
	PrecisionTimer timer; timer.Start();

	//do the search
	Graph_SearchBFS<NavGraph> BFS(*m_pGraph, m_iSourceCell, m_iTargetCell);

	//record the time taken  
	m_dTimeTaken = timer.TimeElapsed();

	//now grab the path (if one has been found)
	if (BFS.Found())
	{
		m_Path = BFS.GetPathToTarget();
	}

	m_SubTree = BFS.GetSearchTree();

	m_dCostToTarget = 0.0;
}
//--------------------------- CreatePathAStar ---------------------------
//------------------------------------------------------------------------
void Pathfinder::CreatePathAStar()
{
  //set current algorithm
  m_CurrentAlgorithm = search_astar;
      
  //create and start a timer
  PrecisionTimer timer; timer.Start();
  
  //create a couple of typedefs so the code will sit comfortably on the page   
  typedef Graph_SearchAStar<NavGraph, Heuristic_Euclid> AStarSearch;

  //create an instance of the A* search using the Euclidean heuristic
  AStarSearch AStar(*m_pGraph, m_iSourceCell, m_iTargetCell);
  

  //record the time taken  
  m_dTimeTaken = timer.TimeElapsed();

  m_Path = AStar.GetPathToTarget();

  m_SubTree = AStar.GetSPT();

  m_dCostToTarget = AStar.GetCostToTarget();

}
Example #3
0
//-------------------------- CreatePathDijkstra --------------------------
//
//  creates a path from m_iSourceCell to m_iTargetCell using Dijkstra's algorithm
//------------------------------------------------------------------------
void BattleMap::createPathDijkstra()
{
	//set current algorithm
	m_CurrentAlgorithm = AT_DIJKSTRA;

	//create and start a timer
	PrecisionTimer timer; timer.Start();

	Graph_SearchDijkstra<NavGraph> djk(*m_pGraph, m_iSourceCell, m_iTargetCell);

	//record the time taken  
	m_dTimeTaken = timer.TimeElapsed();

	m_Path = djk.GetPathToTarget();

	m_SubTree = djk.GetSPT();

	m_dCostToTarget = djk.GetCostToTarget();
}
Example #4
0
//-------------------------------- WinMain -------------------------------
//
//	The entry point of the windows program
//------------------------------------------------------------------------
int WINAPI WinMain (HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPSTR     szCmdLine, 
					int       iCmdShow)
{
	//handle to our window
	HWND						hWnd;

	//our window class structure
	WNDCLASSEX     winclass;

	// first fill in the window class stucture
	winclass.cbSize        = sizeof(WNDCLASSEX);
	winclass.style         = CS_HREDRAW | CS_VREDRAW;
	winclass.lpfnWndProc   = WindowProc;
	winclass.cbClsExtra    = 0;
	winclass.cbWndExtra    = 0;
	winclass.hInstance     = hInstance;
	winclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	winclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
	winclass.hbrBackground = NULL;
	winclass.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU1);
	winclass.lpszClassName = g_szWindowClassName;
	winclass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	//register the window class
	if (!RegisterClassEx(&winclass))
	{
		MessageBox(NULL, "Registration Failed!", "Error", 0);

		//exit the application
		return 0;
	}

	//create the window and assign its ID to hwnd    
	hWnd = CreateWindowEx (NULL,                 // extended style
		g_szWindowClassName,  // window class name
		g_szApplicationName,  // window caption
		WS_OVERLAPPED | WS_VISIBLE | WS_CAPTION | WS_SYSMENU,
		GetSystemMetrics(SM_CXSCREEN)/2 - constWindowWidth/2,
		GetSystemMetrics(SM_CYSCREEN)/2 - constWindowHeight/2,                    
		constWindowWidth,     // initial x size
		constWindowHeight,    // initial y size
		NULL,                 // parent window handle
		NULL,                 // window menu handle
		hInstance,            // program instance handle
		NULL);                // creation parameters

	//make sure the window creation has gone OK
	if(!hWnd)
	{
		MessageBox(NULL, "CreateWindowEx Failed!", "Error!", 0);
	}


	//make the window visible
	ShowWindow (hWnd, iCmdShow);
	UpdateWindow (hWnd);

	// Enter the message loop
	bool bDone = false;

	//create a timer
	PrecisionTimer timer;

	timer.SmoothUpdatesOn();

	//start the timer
	timer.Start();

	MSG msg;

	while(!bDone)
	{		
		while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) 
		{
			if( msg.message == WM_QUIT) 
			{
				//stop loop if it's a quit message
				bDone = true;
			} 

			else 
			{
				TranslateMessage( &msg );
				DispatchMessage( &msg );
			}
		}

		if (msg.message != WM_QUIT )
		{
			//update
			g_GameWorld->Update(timer.TimeElapsed());

			//render
			RedrawWindow(hWnd, false);

			Sleep(2);
		}

	}//end while




	delete g_GameWorld;

	UnregisterClass( g_szWindowClassName, winclass.hInstance );

	return msg.wParam;
}