// initialise un groupe avec tous les bytes présents dans le jeu void initialiseBytes(Groupe *g){ int i, cpt; cpt = 0; for (i = 0 ; i < PP_GetNumUnits(MY_COALITION) && cpt < MAX_UNITS ; i++){ if (PP_Unit_GetType(PP_GetUnitAt(MY_COALITION, i)) == BYTE){ g->membres[cpt] = PP_GetUnitAt(MY_COALITION, i); cpt++; } } g->nbMembres = cpt; }
// 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; }
int main () { PP_Open(); /* open the Prog&Play API */ PP_Unit u = PP_GetUnitAt(MY_COALITION, 0); PP_Pos p = PP_Unit_GetPosition(u); p.x += 927; p.y -= 513; PP_Unit_ActionOnPosition(u, MOVE, p); /* close the Prog&Play API */ PP_Close(); return 0; }
// déplacer toutes les unités à une coordonnée précise int main (){ int i; // compteur de boucle PP_Unit u; // unité courante // définition de al cible PP_Pos pos; pos.x = 256.0; pos.y = 1024.0; // ouverture du jeu PP_Open(); // parcourir toutes les unités i = 0; while (i < PP_GetNumUnits(MY_COALITION)){ // récupérer l'unité courante u = PP_GetUnitAt(MY_COALITION, i); // donner l'ordre de déplacement PP_Unit_ActionOnPosition(u, MOVE, pos); i++; } // fermer le jeu PP_Close(); }
// 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; } }
int main (){ PP_Open(); while (!PP_IsGameOver()){ // parcours de toutes mes unités for (int i = 0 ; i < PP_GetNumUnits(MY_COALITION) ; i++){ // récupération de l'unité courante PP_Unit u = PP_GetUnitAt(MY_COALITION, i); // display command params if (PP_Unit_GetNumPdgCmds(u) > 0){ //std::cout << "code : " << PP_Unit_PdgCmd_GetCode(u, 0) << std::endl; //std::cout << "nbParams : " << PP_Unit_PdgCmd_GetNumParams(u, 0) << std::endl; std::cout << PP_Unit_PdgCmd_GetParam(u, 0, 0) << std::endl; } } } /* // définition de la position cible PP_Pos p; p.x = 100.0; p.y = 100.0; // ouverture du jeu std::cout << "PP_Open" << std::flush; if (PP_Open() == -1) std::cout << " " << PP_GetError(); else std::cout << std::endl; // std::cout << "PP_Close" << std::flush; // if (PP_Close() == -1) std::cout << " " << PP_GetError(); // else std::cout << std::endl; std::cout << "PP_IsGameOver : " << std::flush; int tmp; if ((tmp = PP_IsGameOver()) == -1) std::cout << PP_GetError(); else std::cout << tmp << std::endl; std::cout << "PP_GetMapSize : " << std::flush; p = PP_GetMapSize(); if (p.x == -1.0) std::cout << PP_GetError(); else std::cout << "(" << p.x << ", " << p.y << ")" << std::endl; std::cout << "PP_GetStartPosition : " << std::flush; p = PP_GetStartPosition(); if (p.x == -1.0) std::cout << PP_GetError(); else std::cout << "(" << p.x << ", " << p.y << ")" << std::endl; std::cout << "PP_GetNumSpecialAreas : " << std::flush; int size = PP_GetNumSpecialAreas(); if (size == -1) std::cout << PP_GetError(); else{ std::cout << size << std::endl; for (int i = 0 ; i < size ; i++){ std::cout << " PP_GetSpecialAreaPosition de " << i << " : " << std::flush; p = PP_GetSpecialAreaPosition(i); if (p.x == -1.0) std::cout << PP_GetError(); else std::cout << "(" << p.x << ", " << p.y << ")" << std::endl; } } for (int i = 0 ; i < 3 ; i++){ std::cout << "Coalition " << i << " : " << std::endl; std::cout << " PP_GetNumUnits : " << std::flush; size = PP_GetNumUnits((PP_Coalition)i); if (size == -1) std::cout << PP_GetError(); else{ std::cout << size << std::endl; for (int j = 0 ; j < size ; j++){ std::cout << " PP_GetUnitAt " << j << " : " << std::flush; PP_Unit u = PP_GetUnitAt((PP_Coalition)i, j); if (u == -1) std::cout << PP_GetError(); else{ std::cout << u << std::endl; std::cout << " PP_Unit_GetCoalition : " << std::flush; if ((tmp = PP_Unit_GetCoalition(u)) == -1) std::cout << PP_GetError(); else std::cout << tmp << std::endl; std::cout << " PP_Unit_GetType : " << std::flush; if ((tmp = PP_Unit_GetType(u)) == -1) std::cout << PP_GetError(); else std::cout << tmp << std::endl; std::cout << " PP_Unit_GetPosition : " << std::flush; p = PP_Unit_GetPosition(u); if (p.x == -1.0) std::cout << PP_GetError(); else std::cout << "(" << p.x << ", " << p.y << ")" << std::endl; std::cout << " PP_Unit_GetHealth : " << std::flush; if ((p.x = PP_Unit_GetHealth(u)) == -1.0) std::cout << PP_GetError(); else std::cout << p.x << std::endl; std::cout << " PP_Unit_GetMaxHealth : " << std::flush; if ((p.x = PP_Unit_GetMaxHealth(u)) == -1.0) std::cout << PP_GetError(); else std::cout << p.x << std::endl; std::cout << " PP_Unit_GetGroup : " << std::flush; if ((tmp = PP_Unit_GetGroup(u)) == -1) std::cout << PP_GetError(); else std::cout << tmp << std::endl; std::cout << " PP_Unit_SetGroup " << std::flush; if (PP_Unit_SetGroup(u, 0) == -1) std::cout << " " << PP_GetError(); else std::cout << std::endl; std::cout << " PP_Unit_GetNumPdgCmds " << std::flush; int ret = PP_Unit_GetNumPdgCmds(u); if (ret == -1) std::cout << " " << PP_GetError(); else{ std::cout << ret << std::endl; for (int i = 0 ; i < ret ; i++){ std::cout << "code : " << PP_Unit_PdgCmd_GetCode(u, i) << "|"; for (int j = 0 ; j < PP_Unit_PdgCmd_GetNumParams(u, i) ; j++){ std::cout << " " << PP_Unit_PdgCmd_GetParam(u, i, j); } } std::cout << std::endl; } std::cout << " PP_GetUnitAt 1 : " << std::flush; PP_Unit u1 = PP_GetUnitAt(MY_COALITION, 1); if (u1 == -1) std::cout << PP_GetError(); else{ std::cout << " PP_Unit_ActionOnUnit " << std::flush; if (PP_Unit_ActionOnUnit(u, GUARD, u1) == -1) std::cout << " " << PP_GetError(); else std::cout << std::endl; } std::cout << " PP_Unit_ActionOnPosition " << std::flush; p.x = 50.0; p.y = 50.0; if (PP_Unit_ActionOnPosition(u, MOVE, p) == -1) std::cout << " " << PP_GetError(); else std::cout << std::endl; //std::cout << " PP_Unit_UntargetedAction " << std::flush; //if (PP_Unit_UntargetedAction(u, FIRE_STATE, 0.0) == -1) std::cout << " " << PP_GetError(); //else std::cout << std::endl; } } } } std::cout << "PP_Close" << std::endl; PP_Close();*/ }
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; }