Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Calculer le chemin le moins cher vers la base ennemie
- void calculRush(STATE* s, char p, CELL* target) {
- // Initialiser la grille
- for(int j = 0; j < G_HEIGHT; j++) {
- for(int i = 0; i < G_WIDTH; i++) {
- CELL* c = &s->cell[j][i];
- c->flag = 0;
- c->parcouru = -1;
- c->dist = -1;
- }}
- // Adversaire
- char op = 1 - p;
- // Initialiser la première cellule
- CELL* queue[144];
- int nbQueue = 0;
- queue[nbQueue++] = target;
- // Initialiser la racine
- target->parcouru = target->guard ? 30 : 10; // Coût de la cellule
- target->dist = 0; // Distance de la cellule
- target->flag = 1; // Distance de la cellule
- // Tant qu'il reste une cellule à traiter
- while(nbQueue) {
- // Liste des cellules voisines
- CELL* nextQueue[144];
- int nbNextQueue = 0;
- // Pour chaque cellule chercher les voisines
- for(int n = 0; n < nbQueue; n++) {
- CELL* c0 = queue[n];
- //p("[%d %d]%d ", c0->x, c0->y, c0->active);
- for(char d = 0; d < NB_DIR; d++) {
- CELL* c1 = c0->next[d];
- if(c1 == NULL) continue;
- // Tester les cellules bloquées (mes constructions)
- if(c1->flag == 0) {
- // Calculer les coûts
- if(c1->owner == p && c1->type == T_TOWER) c1->parcouru = INF_MAX;
- else if(c1->owner == p && c1->type == T_MINE) c1->parcouru = INF_MAX;
- else {
- c1->flag = 1;
- c1->parcouru = c0->parcouru + calculCost(c0, c1, op);
- c1->dist = c0->dist + 1;
- //if(c1->owner == p && c1->active) continue;
- nextQueue[nbNextQueue++] = c1;
- }
- //p("(%d %d)%d ", c1->x, c1->y, c1->active);
- }
- }
- }
- // Recopier la liste
- memmove(&queue, &nextQueue, nbNextQueue * sizeof(CELL*));
- nbQueue = nbNextQueue;
- }
- #if M5_MINIMIZE
- //---------- Minimiser les valeurs ---------- (passe complémentaire)
- // Initialiser la première cellule
- //if(myFlag) showParcours(s);
- nbQueue = 0;
- queue[nbQueue++] = target;
- // Tant qu'il reste une cellule à traiter
- while(nbQueue) {
- // Liste des cellules voisines
- CELL* nextQueue[144];
- int nbNextQueue = 0;
- // Pour chaque cellule chercher les voisines
- for(int n = 0; n < nbQueue; n++) {
- CELL* c0 = queue[n];
- for(char d = 0; d < NB_DIR; d++) {
- CELL* c1 = c0->next[d];
- if(c1 == NULL) continue;
- if(c1->parcouru == INF_MAX) continue;
- if(c1->flag == 1) {
- // Minimiser les coûts
- for(char d2 = 0; d2 < NB_DIR; d2++) {
- CELL* c2 = c1->next[d2];
- if(c2 == NULL) continue;
- //if(c2->dist >= c1->dist) continue;
- int cost = calculCost(c2, c1, op);
- if(c2->parcouru + cost < c1->parcouru) {
- //if(myFlag) p("[%d %d]%d (%d %d)%d cost:%d -> ", c1->x, c1->y, c1->parcouru, c2->x, c2->y, c2->parcouru, cost);
- c1->parcouru = c2->parcouru + cost;
- //if(myFlag) p("%d\n", c1->parcouru);
- }}
- c1->flag = 2;
- nextQueue[nbNextQueue++] = c1;
- }}}
- // Recopier la liste
- memmove(&queue, &nextQueue, nbNextQueue * sizeof(CELL*));
- nbQueue = nbNextQueue;
- }
- //if(myFlag) showParcours(s);
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement