Пример #1
0
void NPC::moveTo(float x, float y) {
	PathFinding pathFinder = PathFinding(_map, _id, _x, _y, x, y, 0.5);
	_path = pathFinder.find();
	if(_path.empty()) {
		_dstX = _x;
		_dstY = _y;
	}
	passNode();
}
Пример #2
0
int test3() {
    vector<string> board = {"....", ".A..", "..B.", "...."};
    PathFinding* pObj = new PathFinding();
    clock_t start = clock();
    int result = pObj->minTurns(board);
    clock_t end = clock();
    delete pObj;
    int expected = 2;
    if(result == expected) {
        cout << "Test Case 3: Passed! Time: " << static_cast<double>(end-start)/CLOCKS_PER_SEC << " seconds" << endl;
        return 0;
    } else {
        cout << "Test Case 3: Failed! Time: " << static_cast<double>(end-start)/CLOCKS_PER_SEC << " seconds" << endl;
        return 1;
    }
}
Пример #3
0
void SmartAIController::PathFind(Engine::Actor & actor, D3DXVECTOR3 newGoal)
{
	Engine::Vector3 nGoal= Engine::Vector3(newGoal.x, newGoal.y, newGoal.z);
	Engine::Vector3 cGoal= Engine::Vector3(currentGoal.x, currentGoal.y, currentGoal.z);
	
	if(positionToFollow.size()<=0 || (DistanceXYZ(nGoal, cGoal)>20.0f))
	{
		if( (DistanceXYZ(nGoal, cGoal)>20.0f))
			currentGoal=newGoal;

		//AI
		PathFinding playerPath;
		playerPath.SetLinks(Engine::WaypointsData::Manager().links);
		playerPath.SetPoints(Engine::WaypointsData::Manager().points);
		D3DXVECTOR3 currentPos= D3DXVECTOR3( actor.getActorPosition()->getx(), actor.getActorPosition()->gety(), actor.getActorPosition()->getz() );

		positionToFollow=playerPath.findShortestPathByAstar(currentGoal,currentPos);
	}

	Engine::Vector3 closestPositionToFollow= Vector3(positionToFollow[0].x,  positionToFollow[0].y,  positionToFollow[0].z);
	
	float distance=DistanceXZ(closestPositionToFollow, *actor.getActorPosition());

	if(distance<30.0f)
	{
		positionToFollow.erase(positionToFollow.begin() + 0);
	}
	else
	{
		Vector3 follow;
		follow.setx(positionToFollow[0].x - actor.getActorPosition()->getx());
		follow.sety(positionToFollow[0].y - actor.getActorPosition()->gety());
		follow.setz(positionToFollow[0].z - actor.getActorPosition()->getz());
	
		follow=follow.Normalize();

		D3DXVECTOR3 newFollow;
		newFollow= D3DXVECTOR3(follow.getx()* 20 *speed, 0, follow.getz()* 20 *speed);

		actor.TransLatePosition(newFollow);
	}
}
Пример #4
0
bool KawigiEdit_RunTest(int testNum, vector <string> p0, bool hasAnswer, int p1) {
    cout << "Test " << testNum << ": [" << "{";
    for (int i = 0; int(p0.size()) > i; ++i) {
        if (i > 0) {
            cout << ",";
        }
        cout << "\"" << p0[i] << "\"";
    }
    cout << "}";
    cout << "]" << endl;
    PathFinding *obj;
    int answer;
    obj = new PathFinding();
    clock_t startTime = clock();
    answer = obj->minTurns(p0);
    clock_t endTime = clock();
    delete obj;
    bool res;
    res = true;
    cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
    if (hasAnswer) {
        cout << "Desired answer:" << endl;
        cout << "\t" << p1 << endl;
    }
    cout << "Your answer:" << endl;
    cout << "\t" << answer << endl;
    if (hasAnswer) {
        res = answer == p1;
    }
    if (!res) {
        cout << "DOESN'T MATCH!!!!" << endl;
    } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
        cout << "FAIL the timeout" << endl;
        res = false;
    } else if (hasAnswer) {
        cout << "Match :-)" << endl;
    } else {
        cout << "OK, but is it right?" << endl;
    }
    cout << "" << endl;
    return res;
}
Пример #5
0
int main(int argc, char* argv[]) {
	for(int x = 0; x < 20; x++)
		cout << pfft.ScatterTerrain() << endl;

	g_game = new Game();

	g_game->Init("A* Pathfinding - Dieter", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 800, 0, false);
	while (g_game->isRunning()) {
		g_game->handleEvents();
		g_game->Update();
		g_game->Render();
	}
	g_game->Clean();
	return 0;
}
Пример #6
0
int main()
{
  Graph<> graph(GraphTypes::DIRECTED, GraphTypes::WEIGHTED, GraphTypes::NOCONTENT);
  Graph<> paths_greedy(graph.edgeType(), graph.edgeState(), GraphTypes::NOCONTENT);
  Graph<> paths_dynamic(graph.edgeType(), graph.edgeState(), GraphTypes::NOCONTENT);
  PathFinding<> lookup;

  typedef Exporter<> Export;

  /*
    exemple du cours, exemple n°1, page 83 (attention, celui la page 82 est différent)
  */
  graph.add_edge(1,2, 10);
  graph.add_edge(1,3, 3);
  graph.add_edge(1,5, 6);
  graph.add_edge(1,6, 2);

  graph.add_edge(3,2, 4);
  graph.add_edge(3,4, 1);
  graph.add_edge(3,5, 2);

  graph.add_edge(5,4, 3);

  graph.add_edge(6,2, 1);
  graph.add_edge(6,5, 1);

  try
    {
      lookup.bellman(graph, 1, GraphTypes::Algorithms::GREEDY);
      paths_greedy = lookup.resultGraph();

      lookup.bellman(graph, 1, GraphTypes::Algorithms::DYNAMIC);
      paths_dynamic = lookup.resultGraph();

      //Exports
      Export::ToGraphviz(graph, "bin/test_bellman.graph");
      Export::ToGraphviz(paths_greedy, "bin/greedy_bellman.graph");
      Export::ToGraphviz(paths_dynamic, "bin/dynamic_bellman.graph");

#ifdef _SYSTEM

      //compilation dot
      system("dot -Tpng bin/test_bellman.graph -o bin/test_bellman.png");
      system("dot -Tpng bin/greedy_bellman.graph -o bin/greedy_bellman.png");
      system("dot -Tpng bin/dynamic_bellman.graph -o bin/dynamic_bellman.png");

#endif

      //affichages
      std::cout << "Graph a été exporté dans le fichier test_bellman.graph" << std::endl;
      std::cout << "Bellman Glouton: Arbre des chemins a été exporté dans le fichier greedy_bellman.graph" << std::endl;
      std::cout << "Bellman Dynamique: Arbre des chemins a été exporté dans le fichier dynamic_bellman.graph" << std::endl;

      std::cout << std::endl << "dot -Tpng bin/test_bellman.graph -o bin/test_bellman.png" << std::endl;
      std::cout << "Graph a été compilé dans le fichier test_bellman.png" << std::endl;
      std::cout << std::endl << "dot -Tpng bin/paths_bellman.graph -o bin/paths_bellman.png" << std::endl;
      std::cout << "Bellman glouton: arbre des chemins a été compilé dans le fichier greedy_bellman.png" << std::endl;
      std::cout << "Bellman dynamique: arbre des chemins a été compilé dans le fichier dynamic_bellman.png" << std::endl;

    }

  catch(const GraphException::InvalidOperation & io)
    {
      std::cout << "Caught GraphException::InvalidOperation:" << std::endl << io.what() << std::endl;
    }

  catch(const GraphException::InvalidNodeID & in)
    {
      std::cout << "Caught GraphException::InvalidNodeID:" << std::endl << in.what() << std::endl;
    }

  catch(const GraphException::InvalidEdge & ie)
    {
      std::cout << "Caught GraphException::InvalidEdge:" << std::endl << ie.what() << std::endl;
    }

  catch(const GraphException::BasicGraphException & bge)
    {
      std::cout << "Caught GraphException::BasicGraphException:" << std::endl << bge.what() << std::endl;
    }

  catch(const std::exception & e)
    {
      std::cout << "Caught exception:" << std::endl << e.what() << std::endl;
    }

  catch(...)
    {
      std::cout << "Caught unexpected exception." << std::endl;
    }

  return 0;
}