Exemplo n.º 1
0
Brain::ActionType Brain::Think(Agent &agent) {
	Brain::ActionType next_move;
	do {
		stayAlert(agent);
		meditate();
		//tellCurrentPath();
		if (priorize(agent)) {
			in_path = true;
			//tellCurrentPath();
		}
		if (!in_path) {
			int x, y, orientation;
			agent.GetCoord(y, x, orientation);
			pair<int, int> coords = pair<int, int>(x, y);
			translateToMoves(coords, orientation, calculateGoalAndPath(agent));
			//tellCurrentPath();
			in_path = true;
		}

		if (current_path.empty()) {
			in_path = false;
			next_move = Brain::ActionType::actIDLE;
		} else {
			next_move = current_path.front();
			current_path.pop();
		}
	} while (next_move == Brain::ActionType::actIDLE && !endgame);
	steps++;
	return next_move;
}
Exemplo n.º 2
0
deque<pair<int, int> > Brain::calculateGoalAndPath(Agent& agent) {
	int x, y, orientation;
	pair<bool, bool> solved;
	getGoal(agent);
	deque<pair<int, int> > path;
	agent.GetCoord(y, x, orientation);
	//cout << "-----------------------------" << endl;
	//yellWhatImDoing(agent.getCoord());
	Astar astar_alg = Astar(agent.mapa_entorno_, agent.mapa_objetos_, pair<int, int>(x, y), current_goal);
	solved = astar_alg.solve();
	if (solved.second) {
		this->current_goal = astar_alg.getGoal();
	}
	if (!solved.first) {
		//cout << "ERROR: no se me ocurre una forma de descubrir más mapa!" << endl;
		cout << "PASOS: " << steps << endl;
		if (map_oriented == false) {
			cout << "ERROR, mapa no orientado" << endl;
			exit(0);
		}
		cout << "MAP ORIENTATION: " << this->map_orientation << endl;
		agent.cropAndStoreSolutionMap();
		agent.rotateSolution(this->map_orientation);
		agent.isSolved();
		this->endgame = true;
		//exit(0);
	}
	//yellWhatImDoing(agent.getCoord());
	//cout << "-----------------------------" << endl;
	path = deque<pair<int, int> >(astar_alg.getSolution());
	//astar_alg.printRoute();
	return path;
}
Exemplo n.º 3
0
void Brain::stayAlert(Agent& agent) {
	int x, y, orientation;
	agent.GetCoord(y, x, orientation);
	pair<int, int> coords = pair<int, int>(x, y);
	for (int i = 1; i < SIGHT_LENGTH; i++) {
		//Check important_surface
		pair<int, int> item_coords = translateSensorToCoords(orientation, i, coords);
		checkSurface(agent.whatISeeThere(i).first, coords, item_coords);
		checkSurface(agent.whatISeeThere(i).second, coords, item_coords);
	}
}
Exemplo n.º 4
0
bool Brain::priorize(Agent& agent) {
	bool there_is_something_there = false;
	map<pair<int, int>, Memory>::iterator it = objectives.begin();
	if (!this->objectives.empty()) {
		int x, y, orientation;
		pair<bool, bool> solved;
		getGoal(agent);
		deque<pair<int, int> > path;
		agent.GetCoord(y, x, orientation);
		pair<int, int> coords = pair<int, int>(x, y);
		map<pair<int, int>, Memory>::iterator it = this->objectives.begin();

		this->current_goal = (*it).first;
		bool iHaveItTouchingMaNose = (current_path.empty() && !this->objectives.empty()
				&& (agent.whatISeeThere(1).first == (*it).second.getItem() || agent.whatISeeThere(1).second == (*it).second.getItem()));
		bool iAmOnIt = (current_path.empty() && current_goal.first == x && current_goal.second == y);

		if (iAmOnIt) {
			//cout << "I AM ON IT" << endl;
			iAmOnSomething((*it).second.getItem(), agent);
		} else if (iHaveItTouchingMaNose) {
			//cout << "I HAVE IT IN NOSE" << endl;
			iHaveSomethingAhead((*it).second.getItem(), agent);
		} else {
			//yellWhatImDoing(agent.getCoord());
			Astar astar_alg = Astar(agent.mapa_entorno_, agent.mapa_objetos_, coords, current_goal, true); //Cuidado, movimiento forzado
			solved = astar_alg.solve();
			if (solved.second) {
				this->current_goal = astar_alg.getGoal();
			}
			if (!solved.first) {
				cout << "ERROR: no se me ocurre una forma de encontrar lo que buscas, ni puedo encontrar más mapa!" << endl;
				cout << "PASOS: " << steps << endl;
				//exit(0);
			}
			while (!current_path.empty()) {
				current_path.pop();
			}
			path = deque<pair<int, int> >(astar_alg.getSolution());
			translateToMoves(coords, orientation, path);
		}
		there_is_something_there = true;
	}
	return there_is_something_there;
}
Exemplo n.º 5
0
void Brain::getGoal(Agent &agent) {
	bool goal_found = false;
	double current_distance = 1000000;
	int goal_x, goal_y;
	int x, y, orientacion;
	agent.GetCoord(x, y, orientacion);
	for (int i = 0; i < 200; i++) {
		for (int j = 0; j < 200; j++) {
			if (agent.mapa_entorno_[i][j] == '?') {
				double d = sqrt(pow(i - x, 2) + pow(j - y, 2));
				if (d < current_distance) {
					goal_found = true;
					goal_x = j;
					goal_y = i;
					current_distance = d;
				}
			}
		}
	}
	if (goal_found) {
		this->current_goal = make_pair(goal_x, goal_y);
	}
	//cout << "GOAL: " << current_goal.first << " " << current_goal.second << endl;
}