Advertisement
Guest User

Untitled

a guest
May 26th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. // Calculer le chemin le moins cher vers la base ennemie
  2.  
  3. void calculRush(STATE* s, char p, CELL* target) {
  4.  
  5. // Initialiser la grille
  6. for(int j = 0; j < G_HEIGHT; j++) {
  7. for(int i = 0; i < G_WIDTH; i++) {
  8. CELL* c = &s->cell[j][i];
  9. c->flag = 0;
  10. c->parcouru = -1;
  11. c->dist = -1;
  12. }}
  13.  
  14. // Adversaire
  15. char op = 1 - p;
  16.  
  17. // Initialiser la première cellule
  18. CELL* queue[144];
  19. int nbQueue = 0;
  20. queue[nbQueue++] = target;
  21.  
  22. // Initialiser la racine
  23. target->parcouru = target->guard ? 30 : 10; // Coût de la cellule
  24.  
  25. target->dist = 0; // Distance de la cellule
  26. target->flag = 1; // Distance de la cellule
  27. // Tant qu'il reste une cellule à traiter
  28. while(nbQueue) {
  29.  
  30. // Liste des cellules voisines
  31. CELL* nextQueue[144];
  32. int nbNextQueue = 0;
  33.  
  34. // Pour chaque cellule chercher les voisines
  35. for(int n = 0; n < nbQueue; n++) {
  36.  
  37. CELL* c0 = queue[n];
  38. //p("[%d %d]%d ", c0->x, c0->y, c0->active);
  39. for(char d = 0; d < NB_DIR; d++) {
  40. CELL* c1 = c0->next[d];
  41. if(c1 == NULL) continue;
  42. // Tester les cellules bloquées (mes constructions)
  43. if(c1->flag == 0) {
  44. // Calculer les coûts
  45. if(c1->owner == p && c1->type == T_TOWER) c1->parcouru = INF_MAX;
  46. else if(c1->owner == p && c1->type == T_MINE) c1->parcouru = INF_MAX;
  47. else {
  48. c1->flag = 1;
  49. c1->parcouru = c0->parcouru + calculCost(c0, c1, op);
  50. c1->dist = c0->dist + 1;
  51. //if(c1->owner == p && c1->active) continue;
  52. nextQueue[nbNextQueue++] = c1;
  53. }
  54. //p("(%d %d)%d ", c1->x, c1->y, c1->active);
  55. }
  56. }
  57. }
  58.  
  59. // Recopier la liste
  60. memmove(&queue, &nextQueue, nbNextQueue * sizeof(CELL*));
  61. nbQueue = nbNextQueue;
  62. }
  63.  
  64. #if M5_MINIMIZE
  65. //---------- Minimiser les valeurs ---------- (passe complémentaire)
  66. // Initialiser la première cellule
  67. //if(myFlag) showParcours(s);
  68. nbQueue = 0;
  69. queue[nbQueue++] = target;
  70. // Tant qu'il reste une cellule à traiter
  71. while(nbQueue) {
  72. // Liste des cellules voisines
  73. CELL* nextQueue[144];
  74. int nbNextQueue = 0;
  75. // Pour chaque cellule chercher les voisines
  76. for(int n = 0; n < nbQueue; n++) {
  77. CELL* c0 = queue[n];
  78. for(char d = 0; d < NB_DIR; d++) {
  79. CELL* c1 = c0->next[d];
  80. if(c1 == NULL) continue;
  81. if(c1->parcouru == INF_MAX) continue;
  82. if(c1->flag == 1) {
  83. // Minimiser les coûts
  84. for(char d2 = 0; d2 < NB_DIR; d2++) {
  85. CELL* c2 = c1->next[d2];
  86. if(c2 == NULL) continue;
  87. //if(c2->dist >= c1->dist) continue;
  88. int cost = calculCost(c2, c1, op);
  89. if(c2->parcouru + cost < c1->parcouru) {
  90. //if(myFlag) p("[%d %d]%d (%d %d)%d cost:%d -> ", c1->x, c1->y, c1->parcouru, c2->x, c2->y, c2->parcouru, cost);
  91. c1->parcouru = c2->parcouru + cost;
  92. //if(myFlag) p("%d\n", c1->parcouru);
  93. }}
  94. c1->flag = 2;
  95. nextQueue[nbNextQueue++] = c1;
  96. }}}
  97. // Recopier la liste
  98. memmove(&queue, &nextQueue, nbNextQueue * sizeof(CELL*));
  99. nbQueue = nbNextQueue;
  100. }
  101. //if(myFlag) showParcours(s);
  102. #endif
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement