Exemple #1
0
// retourne vrai si toutes les unités du jeu sont mobiles
bool mobile(){
	int i;
	PP_PendingCommands cmd;
	for (i = 0 ; i < PP_GetNumUnits(MY_COALITION) ; i++){
		if (PP_Unit_GetPendingCommands(PP_GetUnitAt(MY_COALITION, i), &cmd) != -1)
			if (cmd.nbCmds == 0)
				return false;
	}
	return true;
}
Exemple #2
0
// retourne vrai si toutes les unités du jeu sont mobiles
bool mobile(Groupe g){
	int i;
	PP_PendingCommands cmd;
	for (i = 0 ; i < g.nbMembres ; i++){
		if (PP_Unit_GetPendingCommands(g.membres[i], &cmd) != -1){
			if (cmd.nbCmds == 0)
				return false;
		}
	}
	return true;
}
Exemple #3
0
bool enAttaque(PP_Unit source, PP_Unit cible){
	int i;
	PP_PendingCommands cmd;
	PP_Unit_GetPendingCommands(source, &cmd);
	for (i = 0 ; i < cmd.nbCmds ; i++)
		if (cmd.cmd[i].code == ATTACK)
			if (cmd.cmd[i].nbParams == 1)
				if (cmd.cmd[i].param[0] == cible)
					return true;
	return false;
}
Exemple #4
0
// détermine une cible à attaquer pour le groupe g
PP_Unit determinerCible (Groupe g){
	PP_Unit ennemis [MAX_UNITS];
	int occurrence [MAX_UNITS];
	PP_Unit e, tmp;
	PP_Pos bar;
	int i, j, k, cpt;
	int indice, max;
	float distance, dx, dy;
	PP_PendingCommands cmd;
	
	// initialisation
	cpt = 0;
	// enregistrer les cibles actuelles et comptabiliser les occurences
	for (i = 0 ; i < g.nbMembres ; i++){
		PP_Unit_GetPendingCommands(g.membres[i], &cmd);
		for (j = 0 ; j < cmd.nbCmds ; j++)
			if (cmd.cmd[j].code == ATTACK)
				if (cmd.cmd[j].nbParams == 1){
					// chercher si cet ennemi est déjà pris en compte
					indice = -1;
					e = cmd.cmd[j].param[0];
					for (k = 0 ; k < cpt && indice == -1 ; k++)
						if (ennemis[k] == e)
							indice = k;
					if (indice != -1)
						occurrence[indice]++;
					else{
						ennemis[cpt] = e;
						occurrence[cpt] = 1;
						cpt++;
					}
				}
	}
	if (cpt > 0){
		// identifier l'ennemi le plus référencé et le plus endommagé
		max = 0;
		indice = 0;
		for (i = 0 ; i < cpt ; i++){
			if (occurrence[i] > max){
				indice = i;
				max = occurrence[i];
			}
			else
				if (occurrence[i] == max)
					if (PP_Unit_GetHealth(ennemis[i]) < PP_Unit_GetHealth(ennemis[indice])){
						indice = i;
					}
		}
		return ennemis[indice];
	}
	else{
		// calcul du barycentre du groupe
		bar.x = 0;
		bar.y = 0;
		for (i = 0 ; i < g.nbMembres ; i++){
			bar.x = bar.x + PP_Unit_GetPosition(g.membres[i]).x;
			bar.y = bar.y + PP_Unit_GetPosition(g.membres[i]).y;
		}
		if (g.nbMembres != 0){
			bar.x = bar.x / g.nbMembres;
			bar.y = bar.y / g.nbMembres;
		}
		// rechercher l'ennemi le plus proche du groupe
		distance = PP_GetMapSize().x*PP_GetMapSize().x+PP_GetMapSize().y*PP_GetMapSize().y;
		for (i = 0 ; i < PP_GetNumUnits(ENEMY_COALITION) ; i++){
			tmp = PP_GetUnitAt(ENEMY_COALITION, i);
			dx = bar.x - PP_Unit_GetPosition(tmp).x;
			dy = bar.y - PP_Unit_GetPosition(tmp).y;
			if (dx*dx+dy*dy < distance){
				distance = dx*dx+dy*dy;
				e = tmp;
			}
		}
		return e;
	}
}
Exemple #5
0
int main (){
	int i, j, sens;
	PP_Pos pos;
	PP_Unit u, select, tmp;
	bool attaque;
	float distance, dx, dy;
	PP_PendingCommands cmd;
	
	// ouverture du jeu
	PP_Open();
	
	// determination de la direction de l'attaque
	if (PP_GetStartPosition().x < PP_GetMapSize().x/2)
		sens = 1;
	else
		sens = -1;
	
	// lancer la marche
	for (i = 0 ; i < PP_GetNumUnits(MY_COALITION) ; i++){
		pos = PP_Unit_GetPosition(PP_GetUnitAt(MY_COALITION, i));
		if (sens == -1)
			pos.x = 10;
		else
			pos.x = PP_GetMapSize().x - 10;
		PP_Unit_ActionOnPosition(PP_GetUnitAt(MY_COALITION, i), MOVE, pos);
	}
	// attendre le départ de l'armée
	while (!mobile())
		printf("Attente depart armee\n");
	
	// ordonner d'attaquer les bytes ennemis
	while (!PP_IsGameOver()){
		if (PP_GetNumUnits(ENEMY_COALITION) == 0){
			// faire une recherche aléatoire
			for (i = 0 ; i < PP_GetNumUnits(MY_COALITION) ; i++){
				u = PP_GetUnitAt(MY_COALITION, i);
				if (PP_Unit_GetPendingCommands(u, &cmd) != -1){
					if (cmd.nbCmds == 0){
						pos.x = rand() % (int)PP_GetMapSize().x + 1;
						pos.y = rand() % (int)PP_GetMapSize().y + 1;
						PP_Unit_ActionOnPosition(PP_GetUnitAt(MY_COALITION, i), MOVE, pos);
					}
				}
			}
		}
		else{
			// ordonner aux unités qui n'attaquent pas de cibler l'unité ennemie la plus proche d'elle
			for (i = 0 ; i < PP_GetNumUnits(MY_COALITION) ; i++){
				printf(" %d", i);
				u = PP_GetUnitAt(MY_COALITION, i);
				if (PP_Unit_GetPendingCommands(u, &cmd) != -1){
					attaque = false;
					if (cmd.nbCmds == 0)
						attaque = true;
					else if (cmd.nbCmds > 0)
						if (cmd.cmd[0].code != ATTACK)
							attaque = true;
					if (attaque){
						pos = PP_Unit_GetPosition(u);
						// calcul de la distance max pour cette carte
						distance = PP_GetMapSize().x*PP_GetMapSize().x+PP_GetMapSize().y*PP_GetMapSize().y;
						select = -1;
						for (j = 0 ; j < PP_GetNumUnits(ENEMY_COALITION) ; j++){
							tmp = PP_GetUnitAt(ENEMY_COALITION, j);
							dx = pos.x - PP_Unit_GetPosition(tmp).x;
							dy = pos.y - PP_Unit_GetPosition(tmp).y;
							if (dx*dx+dy*dy < distance){
								distance = dx*dx+dy*dy;
								select = tmp;
							}
						}
						if (select != -1)
							PP_Unit_ActionOnUnit(u, ATTACK, select);
					}
				}
				else{
					printf("%s", PP_GetError());
				}
			}
			printf("\n");
		}
	}
	// fermer le jeu
	PP_Close();
	return 0;
}