home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume2 / conquest / part02 / combat.c next >
C/C++ Source or Header  |  1987-10-26  |  19KB  |  629 lines

  1. /*conquest is copyrighted 1986 by Ed Barlow.
  2.  *  I spent a long time writing this code & I hope that you respect this.  
  3.  *  I give permission to alter the code, but not to copy or redistribute
  4.  *  it without my explicit permission.  If you alter the code, 
  5.  *  please document changes and send me a copy, so all can have it.  
  6.  *  This code, to the best of my knowledge works well,  but it is my first
  7.  *  'C' program and should be treated as such.  I disclaim any
  8.  *  responsibility for the codes actions (use at your own risk).  I guess
  9.  *  I am saying "Happy gaming", and am trying not to get sued in the process.
  10.  *                                                Ed
  11.  */
  12. #include "header.h"
  13.  
  14. #define ATKR 2
  15. #define DFND 1
  16.  
  17. /*is sector occupied, if MAXNTN+1 2+ armies occupy*/
  18. extern short occ[MAPX][MAPY];
  19. extern FILE *fnews;
  20. extern short country;
  21.  
  22. int unit[32];        /*armynum*/
  23. int owner[32];        /*owner*/
  24. int side[32];        /*see definitions->1=units 2=unit*/
  25. int anation;        /*nation attacking in this fight*/
  26. int dnation;        /*one nation defending in this fight*/
  27.  
  28. /*RESOLVE COMBAT RESULTS */
  29. /*attacker wants a high roll and defender wants low roll on both dice*/
  30. /*       0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9*/
  31.  
  32. int Cbt6_1[]={
  33.     30,20,15,15,15,15,15,15,15,10,10,10,5,5,5,0,0,0,0,0,0
  34. };
  35. int Cbt5_1[]={
  36.     40,30,20,20,20,20,15,15,15,10,10,10,5,5,5,0,0,0,0,0,0
  37. };
  38. int Cbt4_1[]={
  39.     40,35,30,25,25,20,20,15,15,15,10,10,10,5,5,5,0,0,0,0,0
  40. };
  41. int Cbt3_1[]={
  42.     55,45,35,30,25,25,20,20,20,15,15,10,10,10,5,5,5,0,0,0,0
  43. };
  44. int Cbt2_1[]={
  45.     60,50,35,35,35,35,25,25,20,20,15,15,10,10,10,10,10,5,5,5,0
  46. };
  47. int Cbt3_2[]={
  48.     65,55,35,35,35,35,30,30,25,25,20,20,15,15,15,15,15,10,10,10,0
  49. };
  50. int Cbt5_4[]={
  51.     65,55,45,45,45,45,35,35,30,30,30,30,25,25,25,25,25,15,10,10,10
  52. };
  53. int Cbt1_1[]={
  54.     85,65,55,55,55,55,45,45,35,35,35,35,25,25,25,25,25,15,10,10,10
  55. };
  56. int Cbt4_5[]={
  57.     100,75,65,65,65,65,55,55,45,45,45,45,35,35,35,35,35,25,20,15,10
  58. };
  59. int Cbt2_3[]={
  60.     100,85,75,75,65,65,55,55,45,45,45,45,40,40,35,35,35,35,25,20,10
  61. };
  62. int Cbt1_2[]={
  63.     100,100,95,90,80,80,75,75,65,60,60,60,45,50,45,45,45,35,30,25,10
  64. };
  65. int Cbt1_3[]={
  66.     110,110,100,100,90,90,85,85,80,80,70,70,65,65,55,50,45,40,30,25,10
  67. };
  68. int Cbt1_4[]={
  69.     110,110,100,100,90,90,90,85,80,80,70,70,65,65,55,50,45,40,30,25,10
  70. };
  71. int Cbt1_5[]={
  72.     120,110,110,100,100,90,90,90,80,80,70,70,65,65,55,50,45,40,30,25,10
  73. };
  74. int Cbt1_6[]={
  75.     130,120,110,110,110,100,100,90,90,90,80,80,80,70,65,50,45,40,30,25,10
  76. };
  77.  
  78. /*run all combat on map
  79.  *  procedure is to find each sector with armies in attack mode and
  80.  *  then search around them
  81.  */
  82. combat()
  83. {
  84.     register int i,j;
  85.     /*if fought is 1 then do not fight a second battle in sector*/
  86.     short fought[MAPX][MAPY];
  87.     int temp;
  88.     short armynum,nvynum;
  89.     int valid;
  90.     int count=0;
  91.  
  92.     printf("RUN COMBAT SUBROUTINES");
  93.     fprintf(fnews,"4\tBATTLE SUMMARY STATISTICS\n");
  94.     /*for each nation, if in attack mode run a check*/
  95.  
  96.     for(i=0;i<MAPX;i++) for(j=0;j<MAPY;j++) fought[i][j]=0;
  97.  
  98.     for(anation=0;anation<NTOTAL;anation++) if(ntn[anation].active>0) {
  99.  
  100.         /*army combat*/
  101.         for(j=0;j<MAXARM;j++) if((ntn[anation].arm[j].stat==ATTACK)&&(ntn[anation].arm[j].sold>0)&&(fought[ntn[anation].arm[j].xloc][ntn[anation].arm[j].yloc]==0)){
  102.  
  103.             fought[ntn[anation].arm[j].xloc][ntn[anation].arm[j].yloc]=1;
  104.             /*initialize matrix*/
  105.             for(temp=0;temp<32;temp++){
  106.                 unit[temp]=(-1);
  107.                 owner[temp]=(-1);
  108.                 side[temp]=(-1);
  109.             }
  110.  
  111.             /*check all armies in area and add to matrix*/
  112.             count=0;
  113.             valid=0;
  114.             /*is valid,set matrix*/
  115.             for(country=0;country<NTOTAL;country++) if(ntn[country].active!=0) for(armynum=0;armynum<MAXARM;armynum++) if((ASOLD>0)&&(ASTAT!=SCOUT)&&(AXLOC==ntn[anation].arm[j].xloc)&&(AYLOC==ntn[anation].arm[j].yloc)&&(count<32)) {
  116.  
  117.                 if((country!=anation)&&(ntn[anation].dstatus[country]>HOSTILE)) {
  118.                     valid=1;
  119.                     dnation=country;
  120.                 }
  121.                 unit[count]=armynum;
  122.                 owner[count]=country;
  123.                 count++;
  124.             }
  125.  
  126.             if(valid==1) fight();
  127.         }
  128.  
  129.         /*navy combat*/
  130.         for(j=0;j<MAXNAVY;j++) 
  131.         if((ntn[anation].nvy[j].warships>0)&&(fought[ntn[anation].nvy[j].xloc][ntn[anation].nvy[j].yloc]==0)&&(sct[ntn[anation].arm[j].xloc][ntn[anation].arm[j].yloc].altitude==WATER)){
  132.  
  133.          fought[ntn[anation].nvy[j].xloc][ntn[anation].nvy[j].yloc]=1;
  134.  
  135.             /*initialize matrix*/
  136.             for(temp=0;temp<32;temp++){
  137.                 unit[temp]=(-1);
  138.                 owner[temp]=(-1);
  139.                 side[temp]=(-1);
  140.             }
  141.  
  142.             /*check all fleets in 1 sector range and add to matrix*/
  143.             count=0;
  144.             valid=0;
  145.             /*is valid,set matrix*/
  146.             for(country=0;country<NTOTAL;country++) if(ntn[country].active!=0) for(nvynum=0;nvynum<MAXNAVY;nvynum++) if((NWAR+NMER>0)&&(abs(NXLOC-ntn[anation].nvy[j].xloc)<=1)&&(abs(NYLOC-ntn[anation].nvy[j].yloc)<=1)&&(count<32)) {
  147.                  fought[ntn[country].nvy[nvynum].xloc][ntn[country].nvy[nvynum].yloc]=1;
  148.                 if((country!=anation)&&(ntn[anation].dstatus[country]>HOSTILE)){
  149.                     valid=1;
  150.                     dnation=country;
  151.                 }
  152.                 unit[count]=armynum;
  153.                 owner[count]=country;
  154.                 count++;
  155.             }
  156.             if(valid==1) navalcbt();
  157.         }
  158.     }
  159.     printf("\nall army and navy attacks completed");
  160. }
  161.  
  162. /*taking the three matrices, run a combat*/
  163. fight()
  164. {
  165.     FILE *fpmsg, *fopen();
  166.     int droll,aroll;
  167.     int odds;         /*odds total times 100*/
  168.     int done;
  169.     int i,j,k;
  170.     int asold=0,dsold=0;    /*a's and d's total soldiers*/
  171.     int Aloss=0,Dloss=0;    /*a's and d's total losses*/
  172.     int PAloss,PDloss;    /*percent a and d loss*/
  173.     int loss;
  174.     int abonus=0,dbonus=0;         /*bonus aggregate*/
  175.     short vampire=0;        /*# non vamps deaded */
  176.     short nvamps=0;            /*number of vampire armies*/
  177.  
  178.     /* determine who is attacker & who is on defenders side?*/
  179.     for(j=0;j<32;j++) if(owner[j]!=(-1)){
  180.         if(owner[j]==anation) side[j]=ATKR;
  181.         else if(owner[j]==dnation) side[j]=DFND;
  182.         else if(ntn[anation].dstatus[owner[j]]==JIHAD) side[j]=DFND;
  183.         else if(ntn[owner[j]].dstatus[anation]==JIHAD) side[j]=DFND;
  184.         else if(ntn[anation].dstatus[owner[j]]==WAR)   side[j]=DFND;
  185.         else if(ntn[owner[j]].dstatus[anation]==WAR)   side[j]=DFND;
  186.         else if((ntn[owner[j]].dstatus[anation]==CONFEDERACY)&&(ntn[owner[j]].dstatus[dnation]>HOSTILE)) side[j]=ATKR;
  187.         else if((ntn[owner[j]].dstatus[anation]==ALLIED)&&(ntn[owner[j]].dstatus[dnation]>HOSTILE)) side[j]=ATKR;
  188.     }
  189.  
  190.     /*calculate number of troops */
  191.     asold=0;
  192.     dsold=0;
  193.     for(i=0;i<32;i++) if(owner[i]!=(-1)) {
  194.         if(side[i]==ATKR)
  195.             asold += ntn[owner[i]].arm[unit[i]].sold;
  196.         else if(side[i]==DFND)
  197.             dsold += ntn[owner[i]].arm[unit[i]].sold;
  198.         if(magic(owner[i],VAMPIRE)==1) nvamps++;
  199.     }
  200.  
  201.     if((dsold<=0)||(asold<=0)) {
  202.         printf("ERROR CONDITION -- ABORTING THIS COMBAT!!!!!!\n");
  203.         return;
  204.     }
  205.     odds = (asold*100)/dsold;
  206.  
  207.     /* CALCULATE WEIGHTED AVERAGE BONUS*/
  208.     abonus=0;
  209.     dbonus=0;
  210.     for(i=0;i<32;i++) if(owner[i]!=(-1)) {
  211.         if(side[i]==ATKR)
  212.             abonus += cbonus(i)*ntn[owner[i]].arm[unit[i]].sold;
  213.         else if(side[i]==DFND)
  214.             dbonus += cbonus(i)*ntn[owner[i]].arm[unit[i]].sold;
  215.     }
  216.  
  217.     abonus/=asold;
  218.     dbonus/=dsold;
  219.  
  220.     /*RUN COMBAT */
  221.     /*DICE RESULTS MAY BE FROM 0 TO 200*/
  222.     /*attacker wants a high roll and defender wants low roll on both dice*/
  223.     aroll = rand()%100 + 50 + abonus - dbonus;
  224.     droll = rand()%100 + 50 + abonus - dbonus;
  225.  
  226.     if(aroll<0) aroll=0;
  227.     if(aroll>199) aroll=199;
  228.     if(droll<0) droll=0;
  229.     if(droll>199) droll=199;
  230.  
  231.     /*odds bonus*/
  232.     if(odds>1500) {
  233.         PAloss = (Cbt6_1[aroll/10])/4;
  234.         PDloss = Cbt1_6[20-droll/10]+30;
  235.     }
  236.     if(odds>600) {
  237.         PAloss = Cbt6_1[aroll/10];
  238.         PDloss = Cbt1_6[20-droll/10];
  239.     }
  240.     else if(odds>500){
  241.         PAloss = Cbt5_1[aroll/10];
  242.         PDloss = Cbt1_5[20-droll/10];
  243.     }
  244.     else if(odds>400){
  245.         PAloss = Cbt4_1[aroll/10];
  246.         PDloss = Cbt1_4[20-droll/10];
  247.     }
  248.     else if(odds>300) {
  249.         PAloss = Cbt3_1[aroll/10];
  250.         PDloss = Cbt1_3[20-droll/10];
  251.     }
  252.     else if(odds>200) {
  253.         PAloss = Cbt2_1[aroll/10];
  254.         PDloss = Cbt1_2[20-droll/10];
  255.     }
  256.     else if(odds>150) {
  257.         PAloss = Cbt3_2[aroll/10];
  258.         PDloss = Cbt2_3[20-droll/10];
  259.     }
  260.     else if(odds>125) {
  261.         PAloss = Cbt5_4[aroll/10];
  262.         PDloss = Cbt4_5[20-droll/10];
  263.     }
  264.     else if(odds>100){
  265.         PAloss = Cbt1_1[aroll/10];
  266.         PDloss = Cbt1_1[20-droll/10];
  267.     }
  268.     else if(odds>75) {
  269.         PAloss = Cbt4_5[aroll/10];
  270.         PDloss = Cbt5_4[20-droll/10];
  271.     }
  272.     else if(odds>50) {
  273.         PAloss = Cbt1_2[aroll/10];
  274.         PDloss = Cbt2_1[20-droll/10];
  275.     }
  276.     else if(odds>33) {
  277.         PAloss = Cbt1_3[aroll/10];
  278.         PDloss = Cbt3_1[20-droll/10];
  279.     }
  280.     else if(odds>15) {
  281.         PAloss = Cbt1_6[aroll/10];
  282.         PDloss = Cbt6_1[20-droll/10];
  283.     }
  284.     else {
  285.         PAloss = 120;
  286.         PDloss = 0;
  287.     }
  288.  
  289.     if ((fpmsg=fopen(MSGFILE,"a+"))==NULL) {
  290.         printf("\n\tERROR OPENING %s",MSGFILE);
  291.         exit(1);
  292.     }
  293.  
  294.     /*NOTE LOSSES ARE ADJUSTED BY CBONUS*/
  295.     for(i=0;i<32;i++) if(owner[i]!=(-1)){
  296.         if(side[i]==ATKR){
  297.             loss=(ntn[owner[i]].arm[unit[i]].sold * PAloss * 1.2)/(100+2*ntn[owner[i]].aplus);
  298.             if(loss>ntn[owner[i]].arm[unit[i]].sold )
  299.                 loss=ntn[owner[i]].arm[unit[i]].sold;
  300.             /*army can't have less than 25 men in it*/
  301.             if(ntn[owner[i]].arm[unit[i]].sold-loss<25)
  302.                 loss=ntn[owner[i]].arm[unit[i]].sold;
  303.             Aloss+=loss;
  304.             ntn[owner[i]].arm[unit[i]].sold-=loss;
  305.  
  306.         }
  307.         else if(side[i]==DFND){
  308.             loss=(ntn[owner[i]].arm[unit[i]].sold * PDloss * 1.2)/(100+2*ntn[owner[i]].dplus);
  309.             if(loss>ntn[owner[i]].arm[unit[i]].sold )
  310.                 loss=ntn[owner[i]].arm[unit[i]].sold;
  311.             /*destroy army if < 25 men*/
  312.             if(ntn[owner[i]].arm[unit[i]].sold-loss<25)
  313.                 loss=ntn[owner[i]].arm[unit[i]].sold;
  314.             Dloss+=loss;
  315.             ntn[owner[i]].arm[unit[i]].sold-=loss;
  316.         }
  317.         if((nvamps>0)&&(magic(owner[i],VAMPIRE)==1)) vampire+=loss/2;
  318.     }
  319.  
  320.     fprintf(fnews,"4.\tBattle in %d,%d",ntn[owner[0]].arm[unit[0]].xloc, ntn[owner[0]].arm[unit[0]].yloc);
  321.     for(j=0;j<32;j++) if(owner[j]!=(-1)){
  322.         done=0;
  323.         for(i=0;i<j;i++) if(owner[j]==owner[i]) done=1;
  324.         if(done==0) {
  325.         if(side[i]==DFND) 
  326.         fprintf(fnews,",attacker %s",ntn[owner[j]].name);
  327.         else if(side[i]==ATKR) 
  328.         fprintf(fnews,",defender %s",ntn[owner[j]].name);
  329.         }
  330.     }
  331.     fprintf(fnews,"\n");
  332.     if(nvamps>0){
  333.         for(i=0;i<32;i++) if(owner[i]!=(-1)){
  334.             if(magic(owner[i],VAMPIRE)==1)
  335.             ntn[owner[i]].arm[unit[i]].sold+=vampire/nvamps;
  336.         }
  337.     }
  338.  
  339.     /*who is in the battle*/
  340.     for(j=0;j<32;j++) if(owner[j]!=(-1)){
  341.         done=0;
  342.  
  343.         /*first time your nation appears done=0*/
  344.         for(i=0;i<j;i++) if(owner[j]==owner[i]) done=1;
  345.  
  346.         if((done==0)&&(ntn[owner[j]].active==1)) {
  347.  
  348.             fprintf(fpmsg,"%s BATTLE SUMMARY for sector %d, %d\n",ntn[owner[j]].name,ntn[owner[0]].arm[unit[0]].xloc, ntn[owner[0]].arm[unit[0]].yloc);
  349.  
  350.             if(side[j]==ATKR) fprintf(fpmsg,"%s You are on the Attacking Side\n",ntn[owner[j]].name);
  351.             else fprintf(fpmsg,"%s You are on the Defending Side\n",ntn[owner[j]].name);
  352.  
  353.             /*detail all participants in battle*/
  354.             for(k=0;k<32;k++) if(owner[k]!=(-1)){
  355.                 if(side[k]==DFND) fprintf(fpmsg,"%s\t %s is defending with army %d \n",ntn[owner[j]].name, ntn[owner[k]].name,unit[k]);
  356.                 else fprintf(fpmsg,"%s\t %s is attacking with army %d \n",ntn[owner[j]].name,ntn[owner[k]].name, unit[k]);
  357.             }
  358.  
  359.             fprintf(fpmsg,"%s attacking soldiers=%d\tmodified roll=%d\n",ntn[owner[j]].name,asold,aroll);
  360.             fprintf(fpmsg,"%s defending soldiers=%d\tmodified roll=%d\n",ntn[owner[j]].name,dsold,droll);
  361.             fprintf(fpmsg,"%s ODDS are %d to 100\n",ntn[owner[j]].name,odds);
  362.             fprintf(fpmsg,"%s RESULT: Attackers lose %d men, Defenders lose %d men\n",ntn[owner[j]].name,Aloss, Dloss);
  363.             fprintf(fpmsg,"%s\n","END");
  364.         }
  365.     }
  366.     fclose(fpmsg);
  367. }
  368.  
  369. /*Subroutine to determine combat bonuses for unit i in matrix*/
  370. cbonus(num)
  371. {
  372.     short armynum;
  373.     int armbonus;
  374.  
  375.     armbonus=0;
  376.     armynum=unit[num];
  377.     country=owner[num];
  378.  
  379.     /*Racial combat bonus due to terrain (the faster you move the better)*/
  380.     armbonus+=5*(9-movecost[AXLOC][AYLOC]);
  381.  
  382.     if((magic(country,MI_MONST)==1)&&(unit[num]==0)) armbonus+=20;
  383.     if((magic(country,AV_MONST)==1)&&(unit[num]==0)) armbonus+=20;
  384.     if((magic(country,MA_MONST)==1)&&(unit[num]==0)) armbonus+=20;
  385.  
  386.     if(((magic(country,DESTROYER)==1)||(magic(country,DERVISH)==1))&&((sct[AXLOC][AYLOC].vegitation==ICE)||(sct[AXLOC][AYLOC].vegitation==DESERT)))
  387.         armbonus+=30;
  388.  
  389.     if(side[num]==DFND){
  390.  
  391.         if(sct[AXLOC][AYLOC].altitude==MOUNTAIN) armbonus+=20;
  392.         else if(sct[AXLOC][AYLOC].altitude==HILL) armbonus+=10;
  393.  
  394.         if(sct[AXLOC][AYLOC].vegitation==JUNGLE) armbonus+=20;
  395.         else if(sct[AXLOC][AYLOC].vegitation==FORREST) armbonus+=15;
  396.         else if(sct[AXLOC][AYLOC].vegitation==WOOD) armbonus+=10;
  397.  
  398.         armbonus += ntn[owner[num]].dplus;
  399.  
  400.         if(sct[AXLOC][AYLOC].designation==DCASTLE)
  401.             armbonus+=5*sct[AXLOC][AYLOC].fortress;
  402.         else if((ASTAT==GARRISON)&&(sct[AXLOC][AYLOC].designation==DCITY)){
  403.             if(magic(country,ARCHER)==1) armbonus+=30;
  404.             if(magic(country,ARCHITECT)==1){
  405.                 armbonus+=10+16*sct[AXLOC][AYLOC].fortress;
  406.             }
  407.             else armbonus+=10+8*sct[AXLOC][AYLOC].fortress;
  408.         }
  409.         else if((ASTAT==GARRISON)&&(sct[AXLOC][AYLOC].designation==DCAPITOL)){
  410.             if(magic(country,ARCHER)==1) armbonus+=30;
  411.             if(magic(country,ARCHITECT)==1){
  412.                 armbonus+=20+20*sct[AXLOC][AYLOC].fortress;
  413.             }
  414.             else armbonus+=20+10*sct[AXLOC][AYLOC].fortress;
  415.         }
  416.  
  417.     }
  418.     else if(side[num]==ATKR) armbonus += ntn[owner[num]].aplus;
  419.  
  420.     /*army status is important*/
  421.     if(ASTAT==MARCH) armbonus-=40;
  422.  
  423.     return(armbonus);
  424. }
  425.  
  426. prep()
  427. {
  428.     short armynum,nvynum;
  429.     int save,i,j;
  430.     /*set occ to 0*/
  431.     for(i=0;i<MAPX;i++) for(j=0;j<MAPX;j++) occ[i][j]=0;
  432.     save=country;
  433.     /*set occ to country of occupant army*/
  434.     for(country=0;country<NTOTAL;country++)
  435.         if(ntn[country].active!=0) {
  436.             for(armynum=0;armynum<MAXARM;armynum++){
  437.                 if((ASOLD>0)&&(ASTAT!=SCOUT)){
  438.                     if((occ[AXLOC][AYLOC]==0)||(occ[AXLOC][AYLOC]==country))
  439.                         occ[AXLOC][AYLOC]=country;
  440.                     else occ[AXLOC][AYLOC]=MAXNTN+5;
  441.                 }
  442.             }
  443.             for(nvynum=0;nvynum<MAXNAVY;nvynum++){
  444.                 if(NWAR+NMER>0){
  445.                     if((occ[NXLOC][NYLOC]==0)||(occ[NXLOC][NYLOC]==country))
  446.                         occ[NXLOC][NYLOC]=country;
  447.                     else occ[NXLOC][NYLOC]=MAXNTN+5;
  448.                 }
  449.             }
  450.         }
  451.     country=save;
  452. }
  453.  
  454. /*SUBROUTINE TO RUN NAVAL COMBAT ON ALL SHIPS */
  455. /*just like fight, this takes array of owner,side,unit and calculate*/
  456. navalcbt()
  457. {
  458.     FILE *fpmsg, *fopen();
  459.     int done=0;
  460.     int temp1,temp2;
  461.     int aship=0,dship=0;    /*a's and d's total war ships*/
  462.     int asunk=0,dsunk=0;    /*a's and d's losses for the round*/
  463.     int taloss=0,tdloss=0;    /*total a's and d's total losses*/
  464.     register int k,i,j;
  465.  
  466.     /* determine who is attacker & who is on defenders side?*/
  467.     for(j=0;j<32;j++) if(owner[j]!=(-1)){
  468.         if(owner[j]==anation) side[j]=ATKR;
  469.         else if(ntn[anation].dstatus[owner[j]]==JIHAD) side[j]=DFND;
  470.         else if(ntn[owner[j]].dstatus[anation]==JIHAD) side[j]=DFND;
  471.         else if(ntn[anation].dstatus[owner[j]]==WAR)   side[j]=DFND;
  472.         else if(ntn[owner[j]].dstatus[anation]==WAR)   side[j]=DFND;
  473.         else if((ntn[owner[j]].dstatus[anation]==CONFEDERACY)&&(ntn[owner[j]].dstatus[dnation]>HOSTILE)) side[j]=ATKR;
  474.         else if((ntn[owner[j]].dstatus[anation]==ALLIED)&&(ntn[owner[j]].dstatus[dnation]>HOSTILE)) side[j]=ATKR;
  475.     }
  476.  
  477.     /*RUN COMBAT; loop until done*/
  478.     /*determine relative strengths--does anybody try to flee*/
  479.  
  480.     while(done==0){
  481.         /*calculate number of ships on a side*/
  482.         for(j=0;j<32;j++) if(owner[j]!=(-1)){
  483.             if(side[j]==DFND) {
  484.                 dship+=ntn[owner[j]].nvy[unit[j]].warships;
  485.             }
  486.             else if(side[j]==ATKR) {
  487.                 aship+=ntn[owner[j]].nvy[unit[j]].warships;
  488.             }
  489.         }
  490.  
  491.         /*no bonus currently included in this combat*/
  492.  
  493.         /*each warship can do damage 10%; once all warships sunk then all*/
  494.         /*sunk are are captured merchant*/
  495.         for(i=0;i<32;i++)
  496.             if((owner[i]!=(-1))&&(ntn[owner[i]].nvy[unit[i]].warships>0)){
  497.                 for(j=0;j<ntn[owner[i]].nvy[unit[i]].warships;j++)
  498.                     if(side[j]==ATKR) if(rand()%10==0) asunk++;
  499.                     else if(rand()%10==0) dsunk++;
  500.             }
  501.  
  502.         fprintf(fnews,"4.\tNaval Battle in %d,%d",ntn[owner[0]].arm[unit[0]].xloc, ntn[owner[0]].arm[unit[0]].yloc);
  503.         for(j=0;j<32;j++) if(owner[j]!=(-1)){
  504.             done=0;
  505.             for(i=0;i<j;i++) if(owner[j]==owner[i]) done=1;
  506.             if(done==0) {
  507.             if(side[i]==DFND) 
  508.             fprintf(fnews,",attacker %s",ntn[owner[j]].name);
  509.             else if(side[i]==ATKR) 
  510.             fprintf(fnews,",defender %s",ntn[owner[j]].name);
  511.             }
  512.         }
  513.         fprintf(fnews,"\n");
  514.  
  515.         fprintf(fnews,"\n4.\tthis round attackers lose %d of %d warships (remainder prizes)",asunk,aship);
  516.         fprintf(fnews,"\n4.\tthis round defenders lose %d of %d warships (remainder prizes)",dsunk,dship);
  517.  
  518.         taloss+=asunk;
  519.         tdloss+=dsunk;
  520.  
  521.         if ((fpmsg=fopen(MSGFILE,"a+"))==NULL) {
  522.             fprintf(fnews,"\n4.\tERROR OPENING %s",MSGFILE);
  523.             exit(1);
  524.         }
  525.  
  526.         for(i=0;i<32;i++) if(owner[i]!=(-1)){
  527.             if((asunk>0)&&(side[i]==ATKR)){
  528.                 if(asunk>ntn[owner[i]].nvy[unit[i]].warships) {
  529.                     asunk-=ntn[owner[i]].nvy[unit[i]].warships;
  530.                     ntn[owner[i]].nvy[unit[i]].warships=0;
  531.                 }
  532.                 else {
  533.                     ntn[owner[i]].nvy[unit[i]].warships-=asunk;
  534.                     asunk=0;
  535.                 }
  536.             }
  537.             else if((dsunk>0)&&(side[i]==DFND)){
  538.                 if(dsunk>ntn[owner[i]].nvy[unit[i]].warships) {
  539.                     dsunk-=ntn[owner[i]].nvy[unit[i]].warships;
  540.                     ntn[owner[i]].nvy[unit[i]].warships=0;
  541.                 }
  542.                 else {
  543.                     ntn[owner[i]].nvy[unit[i]].warships-=dsunk;
  544.                     dsunk=0;
  545.                 }
  546.             }
  547.         }
  548.  
  549.         /*prizes*/
  550.         if(dsunk+asunk>0) {
  551.  
  552.             temp1=asunk;
  553.             temp2=dsunk;
  554.             for(i=0;i<32;i++) if(owner[i]!=(-1)){
  555.                 if((dsunk>0)&&(side[i]==ATKR)){
  556.                     ntn[owner[i]].nvy[unit[i]].merchant+=dsunk;
  557.                     dsunk=0;
  558.                     if(rand()%2==0) done=1;
  559.                 }
  560.                 if((asunk>0)&&(side[i]==DFND)){
  561.                     ntn[owner[i]].nvy[unit[i]].merchant+=asunk;
  562.                     asunk=0;
  563.                     if(rand()%2==0) done=1;
  564.                 }
  565.             }
  566.             asunk=temp1;
  567.             dsunk=temp2;
  568.  
  569.             /*remove prizes?*/
  570.             for(i=0;i<32;i++) if(owner[i]!=(-1)){
  571.                 if((asunk>0)&&(side[i]==ATKR)){
  572.                     if(asunk>aship) {
  573.                         asunk-=ntn[owner[i]].nvy[unit[i]].merchant;
  574.                         ntn[owner[i]].nvy[unit[i]].merchant=0;
  575.                     }
  576.                     else {
  577.                         ntn[owner[i]].nvy[unit[i]].merchant-=asunk;
  578.                         asunk=0;
  579.                     }
  580.                 }
  581.                 else if((dsunk>0)&&(side[i]==DFND)){
  582.                     if(dsunk>dship) {
  583.                         dsunk-=ntn[owner[i]].nvy[unit[i]].merchant;
  584.                         ntn[owner[i]].nvy[unit[i]].merchant=0;
  585.                     }
  586.                     else {
  587.                         ntn[owner[i]].nvy[unit[i]].merchant-=dsunk;
  588.                         dsunk=0;
  589.                     }
  590.                 }
  591.             }
  592.         }
  593.  
  594.         /*will round continue; does one side wish to withdraw*/
  595.         if(rand()%3==0) done=1;
  596.         else if((taloss>1.5*tdloss)||(tdloss>1.5*taloss)){
  597.             if(rand()%3==0) done=1;
  598.         }
  599.     }
  600.  
  601.     /*mail results; who is in the battle*/
  602.     for(j=0;j<32;j++) if(owner[j]!=(-1)){
  603.         done=0;
  604.  
  605.         /*first time your nation appears done=0*/
  606.         for(i=0;i<j;i++) if(owner[j]==owner[i]) done=1;
  607.  
  608.         if((done==0)&&(ntn[owner[j]].active==1)) {
  609.  
  610.             fprintf(fpmsg,"%s NAVAL BATTLE SUMMARY for sector %d, %d\n",ntn[owner[j]].name,ntn[owner[0]].nvy[unit[0]].xloc, ntn[owner[0]].nvy[unit[0]].yloc);
  611.  
  612.             if(side[j]==ATKR) fprintf(fpmsg,"%s You are on the Attacking Side\n",ntn[owner[j]].name);
  613.             else fprintf(fpmsg,"%s You are on the Defending Side\n",ntn[owner[j]].name);
  614.  
  615.             /*detail all participants in battle*/
  616.             for(k=0;k<32;k++)
  617.                 if((owner[k]!=owner[j]&&(owner[k]!=(-1)))){
  618.                     if(side[k]==DFND) fprintf(fpmsg,"%s\t %s is defender with navy %d\n",ntn[owner[j]].name, ntn[owner[k]].name,unit[k]);
  619.                     else fprintf(fpmsg,"%s\t %s is attacker with navy %d\n",ntn[owner[j]].name,ntn[owner[k]].name, unit[k]);
  620.                 }
  621.  
  622.             fprintf(fpmsg,"%s RESULT: Attackers lose %d ships, Defenders lose %d ships\n",ntn[owner[j]].name, taloss,tdloss);
  623.             fprintf(fpmsg,"END\n");
  624.         }
  625.     }
  626.  
  627.     fclose(fpmsg);
  628. }
  629.