home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / antipolix-2.0 / game.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-22  |  8.2 KB  |  401 lines

  1. #include "creation.h"
  2. #include "messages.h"
  3. #include "game.h"
  4.  
  5. #define IsSame(c1,c2) (c1.p==c2.p && c1.x==c2.x && c1.y==c2.y)
  6.  
  7.  
  8. int CalculePrix(int Type, int Caract, Parameters *params)
  9. {
  10.   int Prix=0;
  11.   switch(Type)
  12.     {
  13.     case FORTERESSE:
  14.       Prix+=params->COST_TELEPORT;
  15.       break;
  16.     case ARMEE:
  17.       Prix+=params->COST_ARMIE;
  18.       break;
  19.     case HOVERCRAFT:
  20.       Prix+=params->COST_HOVERCRAFT;
  21.       break;
  22.     case CASE_VIDE:
  23.       return 0;
  24.       break;
  25.     }
  26.   switch(Caract)
  27.     {
  28.     case INVISIBLE:
  29.       Prix+=params->COST_INVISIBILITY;
  30.       break;
  31.     case TELEPORT:
  32.       Prix+=params->COST_TELEPORT;
  33.       break;
  34.     }
  35.  
  36.   return Prix;
  37. }
  38.  
  39.  
  40. int max3(int a, int b, int c)
  41. {
  42.   if (a>b && a>c)
  43.     return a;
  44.   else if (b>a && b>c)
  45.     return b;
  46.   else
  47.     return c;
  48. }
  49.  
  50.  
  51. int IsVisuel(Case c1, Case c2, int d)
  52. {
  53.   return (max3(abs(c1.p-c2.p), abs(c1.x-c2.x), abs(c1.y-c2.y))<=d);
  54. }
  55.  
  56. int EqualCase(Case mycase, int p, int x, int y)
  57. {
  58.   return (mycase.p==p && mycase.x==x && mycase.y==y);
  59. }
  60.  
  61. int IsGood(Univers *U, int p, int x, int y, int Couleur, int Type, int Caract)
  62. {
  63.   return ((U->P[p].Case[x][y].Couleur==Couleur) &&
  64.       (U->P[p].Case[x][y].Type==Type || Type==ANY) &&
  65.       (U->P[p].Case[x][y].Caract==Caract || Caract==ANY));
  66. }
  67.  
  68.  
  69. int FindPieceArea(Univers *U, Case c, int type, int caract, int couleur, int field, Case *t)
  70. {
  71.   int p, x, y, i=0;
  72.  
  73.   for (p=c.p-field; p<=c.p+field; p++)
  74.     if (p>=0 && p<U->NbPlateaux)
  75.       for (x=c.x-field; x<=c.x+field; x++)
  76.     if (x>=0 && x<U->P[p].Taille)
  77.       for (y=c.y-field; y<=c.y+field; y++)
  78.         if (y>=0 && y<U->P[p].Taille)
  79.           if (IsGood(U, p, x, y, couleur, type, caract))
  80.         {
  81.           t[i].p=p;
  82.           t[i].x=x;
  83.           t[i].y=y;
  84.           i++;
  85.         }
  86.   return i;
  87. }
  88.           
  89. void InitTn(Case *tc, int nc, int *tn1, int n1, int *tn2, int n2, Case *t1, Case *t2)
  90. {
  91.   int i, j;
  92.  
  93.   for (i=0; i<n1; i++)
  94.     {
  95.       for (j=0; j<nc && !IsSame(t1[i], tc[j]); j++);
  96.       tn1[i]=j;
  97.     }
  98.  
  99.   for (i=0; i<n2; i++)
  100.     {
  101.       for (j=0; j<nc && !IsSame(t2[i], tc[j]); j++);
  102.       tn2[i]=j;
  103.     }
  104. }
  105.  
  106. void InitTi(int *ti, int nc, int n1, int n2)
  107. {
  108.   int i;
  109.  
  110.   for (i=0; i<nc; i++)
  111.     ti[i]=0;
  112.   ti[n1]=1;
  113.   ti[n2]=1;
  114. }
  115.  
  116. int SearchTeleport(Univers *U, int couleur, Case *tc)
  117. /* cherche tous les teleporteurs de l'univers, et remplit le tableau "tc" avec leurs coordonnees */
  118. {
  119.   int p, x, y, i=0;
  120.  
  121.   for (p=0; p<U->NbPlateaux; p++)
  122.     for (x=0; x<U->P[0].Taille; x++)
  123.       for (y=0; y<U->P[0].Taille; y++)
  124.     if (IsGood(U, p, x, y, couleur, ANY, TELEPORT))
  125.       {
  126.         tc[i].p=p;
  127.         tc[i].x=x;
  128.         tc[i].y=y;
  129.         i++;
  130.       }
  131.   return i;
  132. }
  133.     
  134. int GetAWay(int *ti, Case *tc, int nc, int n1, int n2, Parameters *params)
  135. {
  136.   if (IsVisuel(tc[n1], tc[n2], params->TELEPORT_FIELD))
  137.     return 1;
  138.   else
  139.     {
  140.       int i;
  141.       for (i=0; i<nc; i++)
  142.     if (!ti[i] && IsVisuel(tc[n1], tc[i], params->TELEPORT_FIELD))
  143.       {
  144.         ti[i]=1;
  145.         if (GetAWay(ti, tc, nc, i, n2, params))
  146.           return 1;
  147.         ti[i]=0;
  148.       }
  149.     }
  150.   return 0;
  151. }
  152.  
  153. int IsNear(Univers *U, Case c1, Case c2, int Type, Parameters *params)
  154. /* verifie que la case c2 est a distance de deplacement de la case c1, pour la piece dont 
  155.    on donne les caracteristiques */
  156. {
  157.   int Field;
  158.  
  159.   switch(Type)
  160.     {
  161.     case ARMEE:
  162.       Field=params->MOV_ARMIE;
  163.       break;
  164.     case HOVERCRAFT:
  165.       Field=params->MOV_HOVERCRAFT;
  166.       break;
  167.     default:  /* ----- FORTERESSE ----- */
  168.       return 0;
  169.       break;
  170.     }
  171.   if (IsVisuel(c1, c2, Field))
  172.     return 1;
  173.  
  174.   {
  175.     Case t1[27], t2[27], tc[100];
  176.     int tn1[27], tn2[27], ti[100];
  177.     int n1, n2, nc, i, j;
  178.     
  179.     nc=SearchTeleport(U, U->P[c1.p].Case[c1.x][c1.y].Couleur, tc);
  180.     n1=FindPieceArea(U, c1, ANY, TELEPORT, U->P[c1.p].Case[c1.x][c1.y].Couleur, 1, t1);
  181.     n2=FindPieceArea(U, c2, ANY, TELEPORT, U->P[c1.p].Case[c1.x][c1.y].Couleur, Field, t2);
  182.     InitTn(tc, nc, tn1, n1, tn2, n2, t1, t2);
  183.  
  184.     for (i=0; i<n1; i++)
  185.       for (j=0; j<n2; j++)
  186.     if (IsVisuel(t1[i], t2[j], params->TELEPORT_FIELD))
  187.       return 1;
  188.  
  189.     for (i=0; i<n1; i++)
  190.       for (j=0; j<n2; j++)
  191.     {
  192.       InitTi(ti, nc, tn1[i], tn2[j]);
  193.       if (GetAWay(ti, tc, nc, tn1[i], tn2[i], params))
  194.         return 1;
  195.     }
  196.     
  197.     return 0;
  198.   }
  199. }
  200.  
  201.  
  202. int IsUnique(OneMovement *Moves, int f1, int f2, int p, int x, int y)
  203. {
  204.   int i, j;
  205.   for (i=f1; i<NB_MOVES; i++)
  206.     {
  207.       j=((i==f1)?f2:0);
  208.       for (; j<Moves[i].nbcases; j++)
  209.     if (EqualCase(Moves[i].cases[j], p, x, y))
  210.       return 0;
  211.     }
  212.   return 1;
  213. }
  214.  
  215.  
  216. int IsWhom(Univers *U, int caseZ, int caseX, int caseY, int Joueur)
  217. {
  218.   if (U->P[caseZ].Case[caseX][caseY].Couleur==Joueur)
  219.     return MYARMIE;
  220.   else if (U->P[caseZ].Case[caseX][caseY].Couleur>=0)
  221.     return ENNEMY;
  222.   else
  223.     return CASE_VIDE;
  224. }
  225.  
  226.  
  227. int Place(Univers *U, int caseZ, int caseX, int caseY, int Joueur,
  228.               int Type, int Caract, int *Pecule, Parameters *params)
  229. {
  230. /* renvoie 1 si la piece a bien ete placee, 0 sinon */
  231.   int MemoPecule;
  232.   if (U->P[caseZ].Case[caseX][caseY].Couleur==CASE_VIDE)
  233.     {
  234.       MemoPecule=*Pecule;
  235.       *Pecule-=CalculePrix(Type, Caract, params);
  236.       if (*Pecule>=0)
  237.     {
  238.       if (Type!=CASE_VIDE)
  239.         U->P[caseZ].Case[caseX][caseY].Couleur=Joueur;
  240.       else
  241.         U->P[caseZ].Case[caseX][caseY].Couleur=CASE_VIDE;
  242.       U->P[caseZ].Case[caseX][caseY].Type=Type;
  243.       U->P[caseZ].Case[caseX][caseY].Caract=Caract;
  244.       return 1;
  245.     }
  246.       else
  247.     {
  248.       *Pecule=MemoPecule;
  249.       return 0;
  250.     }
  251.     }
  252.   else if (U->P[caseZ].Case[caseX][caseY].Couleur==Joueur)
  253.     {
  254.       MemoPecule=*Pecule;
  255.       *Pecule+=CalculePrix(U->P[caseZ].Case[caseX][caseY].Type,
  256.                U->P[caseZ].Case[caseX][caseY].Caract, params);
  257.       *Pecule-=CalculePrix(Type, Caract, params);
  258.       if (*Pecule>=0)
  259.     {
  260.       if (Type==CASE_VIDE)
  261.         U->P[caseZ].Case[caseX][caseY].Couleur=CASE_VIDE;
  262.       U->P[caseZ].Case[caseX][caseY].Type=Type;
  263.       U->P[caseZ].Case[caseX][caseY].Caract=Caract;
  264.       return 1;
  265.     }
  266.       else
  267.     {
  268.       *Pecule=MemoPecule;
  269.       return 0;
  270.     }
  271.     }
  272.   else
  273.     return 0;
  274. }
  275.  
  276. int DefenseTotal(Univers *U, int p, int x, int y, Parameters *params)
  277. {
  278.   int p1, p2, x1, x2, y1, y2, i, j, k, Taille=U->P[0].Taille;
  279.   int joueur=U->P[p].Case[x][y].Couleur, tot=0;
  280.  
  281.   if (p==0)
  282.     {p1=0; p2=1;}
  283.   else if (p==U->NbPlateaux-1)
  284.     {p1=U->NbPlateaux-2; p2=U->NbPlateaux-1;}
  285.   else
  286.     {p1=p-1; p2=p+1;}
  287.   
  288.   if (x==0)
  289.     {x1=0; x2=1;}
  290.   else if (x==Taille-1)
  291.     {x1=Taille-2; x2=Taille-1;}
  292.   else
  293.     {x1=x-1; x2=x+1;}
  294.   
  295.   if (y==0)
  296.     {y1=0; y2=1;}
  297.   else if (y==Taille-1)
  298.     {y1=Taille-2; y2=Taille-1;}
  299.   else
  300.     {y1=y-1; y2=y+1;}
  301.   
  302.   for (i=p1; i<=p2; i++)
  303.     for (j=x1; j<=x2; j++)
  304.       for (k=y1; k<=y2; k++)
  305.     if (U->P[i].Case[j][k].Couleur==joueur)
  306.       switch(U->P[i].Case[j][k].Type)
  307.         {
  308.         case ARMEE:
  309.           tot+=params->DEF_ARMIE;
  310.           break;
  311.         case HOVERCRAFT:
  312.           tot+=params->DEF_HOVERCRAFT;
  313.           break;
  314.         case FORTERESSE:
  315.           tot+=params->DEF_FORTRESS;
  316.           break;
  317.         }
  318.   return tot;
  319. }
  320.  
  321.  
  322. int IsLocalVoisin(Univers *U, int p, int x, int y, int joueur)
  323. {
  324.   int p1, p2, x1, x2, y1, y2, i, j, k, Taille=U->P[0].Taille;
  325.   
  326.   if (p==0)
  327.     {p1=0; p2=1;}
  328.   else if (p==U->NbPlateaux-1)
  329.     {p1=U->NbPlateaux-2; p2=U->NbPlateaux-1;}
  330.   else
  331.     {p1=p-1; p2=p+1;}
  332.   if (p2>=U->NbPlateaux)
  333.     p2=U->NbPlateaux-1;
  334.   if (p1<0)
  335.     p1=0;
  336.   
  337.   if (x==0)
  338.     {x1=0; x2=1;}
  339.   else if (x==Taille-1)
  340.     {x1=Taille-2; x2=Taille-1;}
  341.   else
  342.     {x1=x-1; x2=x+1;}
  343.   
  344.   if (y==0)
  345.     {y1=0; y2=1;}
  346.   else if (y==Taille-1)
  347.     {y1=Taille-2; y2=Taille-1;}
  348.   else
  349.     {y1=y-1; y2=y+1;}
  350.  
  351.   for (i=p1; i<=p2; i++)
  352.     for (j=x1; j<=x2; j++)
  353.       for (k=y1; k<=y2; k++)
  354.     if (U->P[i].Case[j][k].Couleur==joueur)
  355.       return 1;
  356.   return 0;
  357. }
  358.  
  359.  
  360. int IsVoisin(Univers *U, int j1, int j2)
  361. {
  362.   int i, j, k;
  363.   for (i=0; i<U->NbPlateaux; i++)
  364.     for (j=0; j<U->P[i].Taille; j++)
  365.       for (k=0; k<U->P[i].Taille; k++)
  366.     {
  367.       if (U->P[i].Case[j][k].Couleur!=j1)
  368.         continue;
  369.       if (IsLocalVoisin(U, i, j, k, j2))
  370.         return 1;
  371.     }
  372.   return 0;
  373. }
  374.   
  375.  
  376. void ErasePlayer(Univers *U, int joueur)
  377. {
  378.   int p, x, y;
  379.   for (p=0; p<U->NbPlateaux; p++)
  380.     for (x=0; x<U->P[0].Taille; x++)
  381.       for (y=0; y<U->P[0].Taille; y++)
  382.     if (U->P[p].Case[x][y].Couleur==joueur)
  383.       {
  384.         U->P[p].Case[x][y].Couleur=CASE_VIDE;
  385.         U->P[p].Case[x][y].Type=CASE_VIDE;
  386.       }
  387. }
  388.  
  389.  
  390. void ChangeToPlayer(Univers *U, int Killed, int Alive)
  391. {
  392.   int p, x, y;
  393.   for (p=0; p<U->NbPlateaux; p++)
  394.     for (x=0; x<U->P[0].Taille; x++)
  395.       for (y=0; y<U->P[0].Taille; y++)
  396.     if (U->P[p].Case[x][y].Couleur==Killed)
  397.       U->P[p].Case[x][y].Couleur=Alive;
  398. }
  399.  
  400.  
  401.