home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / games / table / stlcsrsm / cel2.c next >
C/C++ Source or Header  |  1986-12-07  |  33KB  |  1,611 lines

  1. #include <stdio.h>
  2. #include <osbind.h>
  3. #include <obdefs.h>
  4.  
  5. /* Celestial Caesarism  - Sept., 1986 - Jack B. Hardy */
  6.  
  7. int contrl[12],intin[128],ptsin[128],intout[128],ptsout[128],
  8.     numships[9][20];
  9. int handle,planets,explored,score,drive,
  10.     port_state,hi_mask=0xF00,lo_mask=0x00FF,ch_enable=7,
  11.     wr_command=128,rd_command=0,volume=1,
  12.     cha_lo=0,cha_hi=1,chb_lo=2,chb_hi=3,cha_vol=8,chb_vol=9;
  13. short contrl_grid[9][20],area_kn[9][20],a_grid[9][20],
  14.       industry[9][20],atari_xy[2];
  15. short numplayers,bit_player,turn_num,maxturns,vis_ships,limit_pro,atari;
  16. long f_length=3600;
  17. char player[4][16],buffer[3600],
  18.      message1[]="Show Area Known and Controlled by Fleet Admiral ",
  19.      a_orders[]="Orders From Fleet Admiral ",
  20.      alert0[]="[3][ Celestial Caesarism | Requires Medium Resolution | ]\
  21. [ RESTART ]",
  22.      path[]="A:\*.GAM\0",io_file[]="CELESTIA.GAM\0";
  23. struct record{
  24.     int eta;
  25.     int owner;
  26.     int shipsent;
  27.     int xs;
  28.     int ys;
  29.     int xf;
  30.     int yf;
  31.     };
  32. struct record orders[300];
  33. extern double giaccess();
  34.  
  35. main()
  36. {
  37.     int dum,dum2,rez;
  38.  
  39.     appl_init();
  40.     handle=graf_handle(&dum,&dum2,&dum,&dum2);
  41.     init_vwk();
  42.     color_reg(1,1000,900,0);
  43.     color_reg(0,100,100,300);
  44.     v_hide_c(handle);
  45.     v_clrwk(handle);
  46.     rez=Getrez();
  47.     if (rez!=1)
  48.         end_prg();
  49.     else
  50.         start_prg();
  51.     color_reg(0,1000,1000,1000);
  52.     color_reg(1,0,0,0);
  53.     v_clsvwk(handle);
  54.     appl_exit();
  55. }
  56.  
  57. /* Open work station */
  58.  
  59. init_vwk()
  60. {
  61.     int i,l_intin[11],l_intout[57];
  62.  
  63.     for (i=0; i<10;l_intin[i++]=1);
  64.     l_intin[10]=2;
  65.     i=v_opnvwk(l_intin,&handle,l_intout);
  66.  
  67.     Giaccess(volume,cha_vol+wr_command);
  68.     Giaccess(volume,chb_vol+wr_command);
  69.     port_state=Giaccess(port_state,ch_enable+rd_command);
  70.     Giaccess(60,ch_enable+wr_command);
  71.     Giaccess(port_state,ch_enable+wr_command);
  72. }
  73.  
  74. /* Change color register */
  75.  
  76. color_reg(reg,r,g,b)
  77. int reg,r,g,b;
  78. {
  79.     int rgb_in[3];
  80.  
  81.     rgb_in[0]=r;
  82.     rgb_in[1]=g;
  83.     rgb_in[2]=b;
  84.     vs_color(handle,reg,rgb_in);
  85. }
  86.  
  87. /* End program if not medium resolution */
  88.  
  89. end_prg()
  90. {
  91.     v_show_c(handle,0);
  92.     form_alert(1,alert0);
  93. }
  94.  
  95. /* Delay loop */
  96.  
  97. wait(time)
  98. long time;
  99. {
  100.     long length=0L;
  101.  
  102.     while(length<time) ++length;
  103. }
  104.  
  105. /* The main program loop */
  106.  
  107. start_prg()
  108. {
  109.     int i,x,y,stat,tot_ships,tot_planets,a_ct,save_stat,load_stat,but=0;
  110.     short t=0,quit=0;
  111.     char message[70];
  112.  
  113.     save_stat= -1;
  114.     atari= -1;
  115.     title();
  116.     load_stat=load_game();
  117.     if(load_stat == -1)
  118.     {
  119.         get_info();
  120.         load_grid();
  121.     }
  122.     color_reg(0,0,0,300);
  123.     while(turn_num<maxturns)
  124.     {
  125.         for(i=0; i<=numplayers; i++)
  126.         {
  127.             v_hide_c(handle);
  128.             v_clrwk(handle);
  129.             draw_grid();
  130.             draw_but(10,0,629,14);
  131.             strcpy(message,message1);
  132.             strcat(message,player[i]);
  133.             graf_mouse(3,0);
  134.             v_show_c(handle,0);
  135.             a_ct=30;
  136.             but=0;
  137.             while(but==0)
  138.             {
  139.                 text1(message,t);
  140.                 vq_mouse(handle,&stat,&x,&y);
  141.                 if(y<14) but=stat;
  142.                 wait(1000L);
  143.                 t=t^1;
  144.                 a_ct--;
  145.                 if((atari==i)&&(a_ct==0)) but=1;
  146.             }
  147.             if(atari==i) a_play(i); /* Let Atari play automatically */
  148.             else     /* Else collect information from human player */
  149.             {
  150.                 v_hide_c(handle);
  151.                 vsl_color(handle,WHITE);
  152.                 draw_but(10,0,629,14);
  153.                 vsl_color(handle,BLACK);
  154.                 display_grid(i);
  155.                 vs_curaddress(handle,1,9);
  156.   printf("      Status Report For Fleet Admiral %s.               ",player[i]);
  157.                 count(&tot_ships,&tot_planets);
  158.                 vs_curaddress(handle,2,2);
  159.   printf("Total Ships = %3d ~~ Total Planets = %2d ~~ ",tot_ships,tot_planets);
  160.   printf("Known Area = %3d  ~~ Score = %5d",explored,score);
  161.                 if(tot_ships==0) /* If player lost all ships skip turn */
  162.                 {
  163.                     vs_curaddress(handle,23,30);
  164.                     puts("\007");
  165.                     puts("No Ships Available!!");
  166.                     wait(400000);
  167.                     break;
  168.                 }
  169.                 draw_l_box(1,1,10,166,630,176);
  170.                 find_orders();
  171.                 v_rvoff(handle);
  172.             }
  173.         }
  174.         v_hide_c(handle);
  175.         v_clrwk(handle);
  176.         draw_grid();
  177.         draw_but(10,0,629,14);
  178.         vst_effects(handle,0);
  179. v_gtext(handle,128,10,"Movement and Battle Phase - Press Button to Begin");
  180.         vs_curaddress(handle,24,36);
  181.         turn_num++;
  182.         printf("Turn # %d",turn_num);
  183.         but=0;
  184.         while(but==0) vq_mouse(handle,&but,&x,&y);
  185.         erase_line();
  186.         move_players();
  187.         pack_orders();
  188.         add_industry();
  189.         if(turn_num%5==0) show_status();
  190.         vs_curaddress(handle,24,9);
  191.         v_rvon(handle);
  192.         puts("Press Left Button to Continue - Right Button to Save/Quit Game");
  193.         v_rvoff(handle);
  194.         but=0;
  195.         while(but==0) vq_mouse(handle,&but,&x,&y);
  196.         if(turn_num%5==0)
  197.         {
  198.         color_reg(0,0,0,300);
  199.         color_reg(1,1000,900,0);
  200.         }
  201.         if(but==2)
  202.         {
  203.             save_stat=save_game();
  204.             if(save_stat>-1) turn_num=maxturns;
  205.         }
  206.     }
  207.     if(save_stat== -1) final_show();
  208. }
  209.  
  210. /* Count players Ships / Planets */
  211.  
  212. count(tot_ships,tot_planets)
  213. int *tot_ships,*tot_planets;
  214. {
  215.     int row,column,ships,planets;
  216.  
  217.     explored=0;
  218.     score=0;
  219.     ships=0;
  220.     planets=0;
  221.  
  222.     for(row=0; row<=8; row++)
  223.         for(column=0; column<=19; column++)
  224.             if((area_kn[row][column]&bit_player)==bit_player)
  225.             {
  226.                 explored++;
  227.                 score+=50;
  228.                 if((contrl_grid[row][column]&bit_player)==bit_player)
  229.                 {
  230.                     score+=numships[row][column]*10;
  231.                     ships+=numships[row][column];
  232.                     if((contrl_grid[row][column]&16)==16)
  233.                     {
  234.                         score+=500;
  235.                         planets++;
  236.                     }
  237.                 }
  238.             }
  239.     *tot_ships=ships;
  240.     *tot_planets=planets;
  241. }
  242.  
  243. /* Display message in form specified */
  244.  
  245. text1(message,t)
  246. char message[70];
  247. short int t;
  248. {
  249.     vst_effects(handle,t);
  250.     vst_color(handle,GREEN);
  251.     v_gtext(handle,80,10,message);
  252. }
  253.  
  254. /* Display title screen */
  255.  
  256. title()
  257. {
  258.     int i;
  259.  
  260.     vst_effects(handle,16);
  261.     char_height(24);
  262.     vst_color(handle,BLACK);
  263.     v_gtext(handle,156,56,"Celestial Caesarism");
  264.     vst_effects(handle,0);
  265.     vst_color(handle,RED);
  266.     char_height(12);
  267.     for(i=432; i>=216; i-=8) v_gtext(handle,i,80,"by Jack Hardy ");
  268.     vst_color(handle,BLACK);
  269.     char_height(6);
  270.     for(i=0; i<=280; i+=4) v_gtext(handle,i,96," \275 1986");
  271. }
  272.  
  273. /* Get player names - etc. */
  274.  
  275. get_info()
  276. {
  277.     char number,yn,ch,name[140];
  278.     int i,in,fill,xy[2];
  279.  
  280.     while(yn!='Y' && yn!='y')
  281.     {
  282.         vst_color(handle,GREEN);
  283.         vs_curaddress(handle,14,1);
  284.         v_eeos(handle);
  285.         v_gtext(handle,208,110," How many players?  (1 - 4)  ");
  286.         vsin_mode(handle,4,1);
  287.         number='0';
  288.         while(number<'1' || number>'4') vrq_string(handle,1,0,xy,&number);
  289.         vs_curaddress(handle,14,55);
  290.         printf("%c",number);
  291.         vst_color(handle,BLACK);
  292.         numplayers=number-49;
  293.         vs_curaddress(handle,16,1);
  294.         v_rvon(handle);
  295.         puts(" Please limit name to 15 characters ");
  296.         v_rvoff(handle);
  297.         for (i=0; i<=numplayers; i++)
  298.         {
  299.             in=0;
  300.             for(fill=0; fill<=139; fill++) name[fill]='\000';
  301.             printf("Enter name of player #%d => ",i+1);
  302.             while((vrq_string(handle,1,0,xy,&ch))!=0)
  303.             {
  304.                 if(ch=='\010')
  305.                 {
  306.                     in--;
  307.                     vs_curaddress(handle,17+i,28+in);
  308.                     v_eeol(handle);
  309.                 }
  310.                 else if(ch=='\177')
  311.                 {
  312.                     in=0;
  313.                     for(fill=0; fill<=139; fill++) name[fill]='\000';
  314.                     vs_curaddress(handle,17+i,28);
  315.                     v_eeol(handle);
  316.                 }
  317.                 else if((ch>'\37')&&(ch<'\173'))
  318.                 {
  319.                     putchar(ch);
  320.                     name[in]=ch;
  321.                     in++;
  322.                 }
  323.             }
  324.             puts("\007");
  325.             name[15]='\000';
  326.             strcpy(player[i],name);
  327.         }
  328.         if(numplayers<3) /* Let atari player if less than 4 humans */
  329.         {
  330.             if(numplayers==0) add_atari(); /* Atari plays if only one human */
  331.             else
  332.             {
  333.                 printf("Do you want Atari ST to play? [Y/N] ");
  334.                 yn=yes_no();
  335.                 if((yn=='y') || (yn=='Y')) add_atari();
  336.             }
  337.         }
  338.         maxturns=0;
  339.         while(maxturns<10 || maxturns>25) /* Set maximun number of turns */
  340.         {
  341.             vs_curaddress(handle,16,1);
  342.             v_eeos(handle);
  343.             puts("Press space bar to terminate input.");
  344.             printf("How many turns would you like to play? [10-25] ");
  345.             scanf("%2d",&maxturns);
  346.             putchar('\n');
  347.         }
  348.         vis_ships=0;
  349.         printf("Show enemy ships? [Y/N] ");
  350.         yn=yes_no();
  351.         if((yn=='y') || (yn=='Y')) vis_ships=1;
  352.         limit_pro=0;
  353.         printf("Limit neutral planet ship production? [Y/N] ");
  354.         yn=yes_no();
  355.         if((yn=='y') || (yn=='Y')) limit_pro=1;
  356.         vs_curaddress(handle,16,1);
  357.         v_eeos(handle);
  358.         for(i=0; i<=numplayers; i++)
  359.             printf("Player #%d is %s.\n",i+1,player[i]);
  360.         printf("The number of turns = %d.\n",maxturns);
  361.         printf("See enemy ships = ");
  362.         if(vis_ships==1) puts("YES ");
  363.         else puts("NO ");
  364.         printf("Limit neutral planet ship production = ");
  365.         if(limit_pro==1) puts("YES ");
  366.         else puts("NO ");
  367.         printf("\nAll entries correct? [Y/N] ");
  368.         yn=yes_no();
  369.     }
  370. }
  371.  
  372. /* Change size of printed characters */
  373.  
  374. char_height(height)
  375. int height;
  376. {
  377. int char_width,char_height,cell_width,cell_height;
  378.  
  379. vst_height(handle,height,&char_width,&char_height,&cell_width,&cell_height);
  380. }
  381.  
  382. /* Increase players and let Atari enter name */
  383.  
  384. add_atari()
  385. {
  386.     int i;
  387.     static char at_string[]="Atari ST\0";
  388.  
  389.     numplayers++;
  390.     atari=numplayers;
  391.     strcpy(player[numplayers],at_string);
  392.     printf("Enter name of player #%d => ",numplayers+1);
  393.     for(i=0;i<=7;i++)
  394.     {
  395.         printf("%c",at_string[i]);
  396.         wait(8000L);
  397.     }
  398.     putchar('\n');
  399.     wait(270000L);
  400. }
  401.  
  402. /* Wait for Y or N keys to be pressed */
  403.  
  404. yes_no()
  405. {
  406.     int xy[2];
  407.     char yn='x';
  408.  
  409.     while((yn!='Y') && (yn!='y') && (yn!='N') && (yn!='n'))
  410.         vrq_string(handle,1,0,xy,&yn);
  411.     printf("%c\n",yn);
  412.     return(yn);
  413. }
  414.  
  415. /* Draw grid to screen  */
  416.  
  417. draw_grid()
  418. {
  419.     int pxyarray[4],x,y,i;
  420.     x=0;
  421.     y=16;
  422.  
  423.     fill_area();
  424.     vsl_color(handle,BLACK);
  425.     pxyarray[0]=x;
  426.     pxyarray[2]=x+639;
  427.     for (i=y; i<y+145; i=i+16) /* draw lines across */
  428.     {
  429.         pxyarray[1]=i;
  430.         pxyarray[3]=i;
  431.         v_pline(handle,2,pxyarray);
  432.     }
  433.  
  434.     pxyarray[1]=y;     /* draw lines down */
  435.     pxyarray[3]=y+144;
  436.     for (i=x; i<x+640; i=i+32)
  437.     {
  438.         pxyarray[0]=i;
  439.         pxyarray[2]=i;
  440.         v_pline(handle,2,pxyarray);
  441.     }
  442. draw_line();
  443. }
  444.  
  445. /* Draw line down right edge of screen */
  446.  
  447. draw_line()
  448. {
  449.     int pxyarray[4];
  450.  
  451.     pxyarray[0]=639;
  452.     pxyarray[1]=16;
  453.     pxyarray[2]=639;
  454.     pxyarray[3]=160;
  455.     v_pline(handle,2,pxyarray);
  456. }
  457.  
  458. /* Draw and fill area with color red */
  459.  
  460. fill_area()
  461. {
  462.     vsf_style(handle,3);
  463.     draw_l_box(3,RED,0,16,639,160);
  464. }
  465.  
  466. /* Make square transparent or solid */
  467.  
  468. shade_area(x,y,mode)
  469. int x,y,mode;
  470. {
  471.     v_hide_c(handle);
  472.     vsf_style(handle,1);
  473.     vswr_mode(handle,mode);
  474.     draw_l_box(2,BLACK,x*32+1,y*16+17,x*32+31,y*16+31);
  475.     vswr_mode(handle,1);
  476.     v_show_c(handle);
  477. }
  478.  
  479. /* Draw button in area, size, and color passed */
  480.  
  481. draw_l_box(index,color,x,y,x2,y2)
  482. int index,color,x,y,x2,y2;
  483. {
  484.     int pxy[4];
  485.  
  486.     vsf_interior(handle,index);
  487.     vsf_color(handle,color);
  488.     pxy[0]=x;
  489.     pxy[1]=y;
  490.     pxy[2]=x2;
  491.     pxy[3]=y2;
  492.     v_bar(handle,pxy);
  493. }
  494.  
  495. /* Set up initial board with planets and player positions */
  496.  
  497. load_grid()
  498. {
  499.     int i,row,column,player;
  500.  
  501.     player=1;
  502.     for(i=0; i<=numplayers; i++)
  503.     {
  504.         selec_rc(&row,&column);
  505.         contrl_grid[row][column]=player+16;
  506.         area_kn[row][column]=player;
  507.         numships[row][column]=60;   /* Add ships if player at edge */
  508.         if(column<9) numships[row][column]+=36-(column*4);
  509.         if(column>10) numships[row][column]+=4*column-40;
  510.         if(row<4) numships[row][column]+=16-(row*4);
  511.         if(row>4) numships[row][column]+=4*row-16;
  512.         industry[row][column]=15;
  513.         player*=2;
  514.         if(atari==i)
  515.         {
  516.             atari_xy[0]=column;
  517.             atari_xy[1]=row;
  518.             a_grid[row][column]=1;
  519.         }
  520.     }
  521.     planets=(Random() & 15)+30;  /* Number of neutral planets 30-45 */
  522.     for(i=0; i<=planets; i++)
  523.     {
  524.         selec_rc(&row,&column);
  525.         contrl_grid[row][column]=16;
  526.         industry[row][column]=(Random() & 3)+3;
  527.         numships[row][column]=industry[row][column];
  528.     }
  529. }
  530.  
  531. /* Choose row and column from grid */
  532.  
  533. selec_rc(row,column)
  534. int *row,*column;
  535. {
  536.     int num1,num2;
  537.  
  538.     do       /* Choose random number within grid */
  539.     {
  540.         num1=9;
  541.         while (num1>8) num1=Random() & 15;
  542.         num2=20;
  543.         while(num2>19) num2=Random() & 31;
  544.     }
  545.     while(contrl_grid[num1][num2]>0);  /* If occupied, choose another */
  546.  
  547.     *row=num1;
  548.     *column=num2;
  549. }
  550.  
  551. /* Draw box for button */
  552.  
  553. draw_but(x,y,x2,y2)
  554. int x,y,x2,y2;
  555. {
  556.     int xyarray[4];
  557.  
  558.     xyarray[0]=x;
  559.     xyarray[1]=y;
  560.     xyarray[2]=x2;
  561.     xyarray[3]=y2;
  562.     v_rbox(handle,xyarray);
  563. }
  564.  
  565. /* Show player area known and controlled */
  566.  
  567. display_grid(player)
  568. int player;
  569. {
  570.     int row,column,pxy[4],x,y,color;
  571.  
  572.     bit_player=num_player(player);
  573.     for(row=0; row<=8; row++)   /* player control checked by bits */
  574.         for(column=0; column<=19; column++)
  575.             if((area_kn[row][column]&bit_player)==bit_player)
  576.             {
  577.                 vsf_interior(handle,1);
  578.                 vsf_color(handle,WHITE);
  579.                 pxy[0]=column*32+1;
  580.                 pxy[1]=row*16+17;
  581.                 pxy[2]=column*32+31;
  582.                 pxy[3]=row*16+30;
  583.                 v_bar(handle,pxy);
  584.                 x=column*32+15;
  585.                 y=row*16+23;
  586.                 if((contrl_grid[row][column]&bit_player)==bit_player)
  587.                 {   /* Show planets and ships */
  588.                     vst_color(handle,GREEN);
  589.                     if(numships[row][column] != 0)
  590.                         v_gtext(handle,x-3,y+3,"\177");
  591.                     if((contrl_grid[row][column] & 16)==16)
  592.                             draw_planet(x,y,GREEN);
  593.                 }
  594.                 else if((contrl_grid[row][column] & 31)!=0)
  595.                 {   /* If enemy, show in red - neutral planet yellow */
  596.                     vst_color(handle,RED);
  597.                     if((numships[row][column]>0)&&(vis_ships>0))
  598.                         v_gtext(handle,x-3,y+3,"\177");
  599.                     if((contrl_grid[row][column]&16)==16)
  600.                     {
  601.                         color=1;
  602.                         if((contrl_grid[row][column] & 15)!=0)
  603.                             color=RED;
  604.                         draw_planet(x,y,color);
  605.                     }
  606.                 }
  607.             }
  608.     vst_color(handle,GREEN);
  609.     draw_line();
  610. }
  611.  
  612. /* Find bit that controls each player */
  613.  
  614. num_player(player)
  615. int player;
  616. {
  617.     int i=0,num=1;
  618.  
  619.     while(i!=player)
  620.     {
  621.         num*=2;
  622.         i++;
  623.     }
  624.     return(num);
  625. }
  626.  
  627. /* Draw planet in color passed to procedure */
  628.  
  629. draw_planet(x,y,color)
  630. int x,y,color;
  631. {
  632.     vsf_color(handle,color);
  633.     vsf_interior(handle,2);
  634.     vsf_style(handle,14);
  635.     v_circle(handle,x,y,8);
  636.     v_contourfill(handle,x,y,color);
  637. }
  638.  
  639. /* Accept player input from mouse */
  640.  
  641. find_orders()
  642. {
  643.     int x,y,ox,oy,i,stat,but,numsh,fleet,empty,end=0;
  644.     int lt_rt,up_dn,moves,equal_moves,total_moves;
  645.  
  646.     empty=find_start();
  647.     but=0;
  648.     while(but!=2)
  649.     {
  650.         v_rvon(handle);
  651.         vs_curaddress(handle,22,24);
  652.         puts(" ARMADA = ?    ");
  653.         vs_curaddress(handle,22,45);
  654.         puts(" DESTINATION ");
  655.         vs_curaddress(handle,22,64);
  656.         puts(" ETA = ?   ");
  657.         numsh=0;
  658.         while(numsh==0)
  659.         {
  660.             vs_curaddress(handle,24,22);
  661.             v_rvoff(handle);
  662.             puts("- Push Right Mouse Button To End Turn -");
  663.             graf_mouse(7,0);  /* Change mouse to crosshairs */
  664.             v_show_c(handle,0);
  665.             but=0;
  666.             while(but==0)
  667.             {
  668.                 v_rvoff(handle);
  669.                 flash_text(10,"ORIGIN");
  670.                 v_rvon(handle);
  671.                 flash_text(10,"ORIGIN");
  672.                 vq_mouse(handle,&stat,&x,&y);
  673.                 if(y>16 && y<160) but=stat;
  674.                 if(stat==2)
  675.                 {
  676.                     but=2;
  677.                     vs_curaddress(handle,24,16);
  678.            puts("\007 Continue - Left Button / End Turn - Right Button ");
  679.                     while(but!=0) vq_mouse(handle,&but,&x,&y);
  680.                     while(but==0) vq_mouse(handle,&but,&x,&y);
  681.                     erase_line();
  682.                     wait(1000L);
  683.                     if(but==1) but=0;
  684.                     if(but==2) end=2;
  685.                 }
  686.             }
  687.             if(end==2) break;
  688.             erase_line();
  689.             x/=32;y-=16;y/=16;ox=x;oy=y;
  690.             if((contrl_grid[y][x] & bit_player)!=bit_player)
  691.             {
  692.                 vs_curaddress(handle,24,21);
  693.                 puts("\007 That Sector Is Not Controlled By You!!");
  694.                 wait(150000);
  695.                 erase_line();
  696.             }
  697.             else
  698.             {
  699.                 shade_area(x,y,2);
  700.                 if(industry[y][x]>0)
  701.                 {
  702.                     vs_curaddress(handle,24,22);
  703.             printf("Planet Manufactures %d Ships Per Turn.",industry[y][x]);
  704.                 }
  705.                 if(numships[y][x]>0)
  706.                 {
  707.                     numsh=numships[y][x];
  708.                     vs_curaddress(handle,22,34);
  709.                     v_rvon(handle);
  710.                     printf(" %d",numsh);
  711.                 }
  712.                 else
  713.                 {
  714.                     v_rvoff(handle);
  715.                     vs_curaddress(handle,24,22);
  716.                 puts("\007 No Ships Available In This Sector!!      ");
  717.                     wait(150000);
  718.                     erase_line();
  719.                     shade_area(x,y,3);
  720.                     if(x==19) draw_line();
  721.                 }
  722.             }
  723.         }
  724.         if(end==2) break;
  725.         while(but!=0) vq_mouse(handle,&but,&x,&y);
  726.         while(but==0)
  727.         {
  728.             v_rvoff(handle);
  729.             flash_text(45,"DESTINATION");
  730.             v_rvon(handle);
  731.             flash_text(45,"DESTINATION");
  732.             vq_mouse(handle,&stat,&x,&y);
  733.             if(y>16 && y<160) but=stat;
  734.         }
  735.         erase_line();
  736.         x/=32;y-=16;y/=16;
  737.         if(x==ox && y==oy)
  738.         {
  739.             shade_area(x,y,3);
  740.             if(x==19) draw_line();
  741.         }
  742.         else
  743.         {
  744.             lt_rt=0;
  745.             up_dn=0;
  746.             shade_area(x,y,2);
  747.             lt_rt=(x<ox) ? ox-x : x-ox;
  748.             up_dn=(y<oy) ? oy-y : y-oy;
  749.             moves=(up_dn<lt_rt) ? lt_rt-up_dn : up_dn-lt_rt;
  750.             equal_moves=(up_dn<lt_rt) ? lt_rt-moves : up_dn-moves;
  751.             total_moves=equal_moves+moves;
  752.             if(total_moves>1) total_moves/=2;
  753.             v_rvon(handle);
  754.             vs_curaddress(handle,22,70);
  755.             printf(" %d",total_moves);
  756.             fleet=send_ships(numsh);
  757.             vs_curaddress(handle,23,1);
  758.             v_hide_c(handle);
  759.             v_eeos(handle);
  760.             if(fleet>0)
  761.             {
  762.                 numships[oy][ox]-=fleet;
  763.                 orders[empty].eta=total_moves;
  764.                 orders[empty].owner=bit_player;
  765.                 orders[empty].shipsent=fleet;
  766.                 orders[empty].xs=x;
  767.                 orders[empty].ys=y;
  768.                 orders[empty].xf=ox;
  769.                 orders[empty].yf=oy;
  770.                 empty++;
  771.             }
  772.         }
  773.         shade_area(x,y,3);
  774.         shade_area(ox,oy,3);
  775.         if((x==19)||(ox==19)) draw_line();
  776.     }
  777. }
  778.  
  779. /* Erase text line from bottom of screen */
  780.  
  781. erase_line()
  782. {
  783.     vs_curaddress(handle,24,10);
  784.     v_rvoff(handle);
  785.     printf("                                                         ");
  786. }
  787.  
  788. /* Display text in inverse or standard */
  789.  
  790. flash_text(x,text)
  791. int x;
  792. char text[12];
  793. {
  794.     vs_curaddress(handle,22,x);
  795.     printf(" %s ",text);
  796.     wait(6000L);
  797. }
  798.  
  799. /* Find number of ships to send */
  800.  
  801. send_ships(total)
  802. int total;
  803. {
  804.     int num=0,send=0,button=0,stat,x,y;
  805.  
  806.     graf_mouse(3,0);
  807.     while(send==0)
  808.     {
  809.         v_hide_c(handle);
  810.         draw_l_box(0,RED,204,180,356,194);
  811.         v_rvoff(handle);
  812.         vs_curaddress(handle,24,28);
  813.         printf("Send %3d Ship(s)",num);
  814.         draw_l_box(0,RED,384,180,402,194);
  815.         v_gtext(handle,390,190,"\001");
  816.         draw_l_box(0,RED,408,180,426,194);
  817.         v_gtext(handle,414,190,"\002");
  818.         v_show_c(handle,0);
  819.         while(button!=1)
  820.         {
  821.             vq_mouse(handle,&stat,&x,&y);
  822.             if(y>180 && y<194)
  823.                 button=stat;
  824.         }
  825.         v_hide_c(handle);
  826.         if(x>204 && x<356)
  827.         {
  828.             send=1;
  829.             draw_l_box(1,1,205,181,355,193);
  830.             v_rvon(handle);
  831.             vs_curaddress(handle,24,28);
  832.             printf("Send %3d Ship(s)",num);
  833.             v_rvoff(handle);
  834.             v_show_c(handle,0);
  835.             while(button==1)
  836.                 vq_mouse(handle,&button,&x,&y);
  837.         }
  838.         if(x>384 && x<402)
  839.         {
  840.             draw_l_box(1,1,385,181,401,193);
  841.             v_gtext(handle,390,190,"\001");
  842.             v_show_c(handle,0);
  843.             while(button==1)
  844.             {
  845.                 num+=1;
  846.                 if(num>total) num=total;
  847.                 vs_curaddress(handle,24,33);
  848.                 printf("%3d",num);
  849.                 wait(5000L);
  850.                 vq_mouse(handle,&button,&x,&y);
  851.             }
  852.         }
  853.         if(x>408 && x<426)
  854.         {
  855.             draw_l_box(1,1,409,181,425,193);
  856.             v_gtext(handle,414,190,"\002");
  857.             v_show_c(handle,0);
  858.             while(button==1)
  859.             {
  860.                 num-=1;
  861.                 if(num<0) num=0;
  862.                 vs_curaddress(handle,24,33);
  863.                 printf("%3d",num);
  864.                 wait(5000L);
  865.                 vq_mouse(handle,&button,&x,&y);
  866.             }
  867.         }
  868.         v_show_c(handle,0);
  869.         button=0;
  870.     }
  871.     return(num);
  872. }
  873.  
  874. /* Find empty spot in structured array to store orders */
  875.  
  876. find_start()
  877. {
  878.     int i=0;
  879.  
  880.     while(orders[i].eta != 0) i++;
  881.     return(i);
  882. }
  883.  
  884. /* Read orders and execute */
  885.  
  886. move_players()
  887. {
  888.     int i=0,x,y,results,planet,num;
  889.     short temp;
  890.     char message[60];
  891.  
  892.     while(orders[i].eta != 0)
  893.     {
  894.         orders[i].eta--;
  895.         if(orders[i].eta==0)
  896.         {
  897.             num=find_admiral(orders[i].owner);
  898.             strcpy(message,a_orders);
  899.             strcat(message,player[num]);
  900.             vs_curaddress(handle,1,1);
  901.             puts("\007");
  902.             vst_color(handle,WHITE);
  903.             v_gtext(handle,40,10,"000000000000000000000000000000000000000000");
  904.             v_gtext(handle,320,10,"00000000000000000000000000000000000");
  905.             vst_color(handle,RED);
  906.             v_gtext(handle,136,10,message);
  907.             x=orders[i].xs;
  908.             y=orders[i].ys;
  909.             temp=(area_kn[y][x]&15);
  910.             temp=(temp | orders[i].owner);
  911.             area_kn[y][x]=temp;
  912.             shade_area(x,y,2);
  913.             if(numships[y][x] != 0) /* If other ships occupy grid - fight */
  914.             {
  915.                 results=battle(x,y,i);
  916.                 vs_curaddress(handle,24,23);
  917.                 switch(results)
  918.                 {
  919.                     case 0:
  920.                         printf("**  Attacker Was Defeated!!  **");
  921.                         break;
  922.                     case 1:
  923.                         printf("    Attacker Was Suscessful!!");
  924.                         numships[y][x]=orders[i].shipsent;
  925.                         planet=(contrl_grid[y][x]&16);
  926.                         contrl_grid[y][x]=orders[i].owner+planet;
  927.                         break;
  928.                     case 2:
  929.                 printf("Area reinforced with %d ship(s).",orders[i].shipsent);
  930.                         numships[y][x]+=orders[i].shipsent;
  931.                         break;
  932.                     case 3:
  933.                         printf(" ~~  Attacker Retreated!!  ~~");
  934.                         orders[i].eta=(Random() & 3)+1;
  935.                         orders[i].xs=orders[i].xf;
  936.                         orders[i].ys=orders[i].yf;
  937.                         orders[i].xf=Random()&15;
  938.                         orders[i].yf=Random()&7;
  939.                         break;
  940.                 }
  941.             }
  942.             else
  943.             {
  944.                 vs_curaddress(handle,24,27);
  945.                 printf(" ^^^ %d Ship(s) Arriving. ^^^",orders[i].shipsent);
  946.                 planet=(contrl_grid[y][x]&16);
  947.                 contrl_grid[y][x]=orders[i].owner+planet;
  948.                 numships[y][x]=orders[i].shipsent;
  949.             }
  950.             wait(250000);
  951.             shade_area(x,y,3);
  952.             if(x==19) draw_line();
  953.             erase_line();
  954.             vs_curaddress(handle,22,1);
  955.             v_hide_c(handle);
  956.             v_eeos(handle);
  957.             v_show_c(handle);
  958.         }
  959.         i++;
  960.     }
  961. }
  962.  
  963. /* Execute battle and return results */
  964.  
  965. battle(x,y,ord)
  966. int x,y,ord;
  967. {
  968.     int num,dshots,ashots,achance,dchance,attships,player,maxshots;
  969.     int quit=1,i,mx,my,hit,won_lost,button,stop=0,shields=1,dshields=0;
  970.  
  971.     if((contrl_grid[y][x]&orders[ord].owner)==orders[ord].owner)
  972.         won_lost=2;
  973.     else
  974.     {
  975.         draw_l_box(1,1,2,166,637,176);
  976.         achance=(Random()&31)+69;
  977.         dchance=(Random()&31)+69;
  978.         v_rvon(handle);
  979.         vs_curaddress(handle,22,2);
  980.         attships=orders[ord].shipsent;
  981.         printf("Attacker has %3d ships -%3d%% accuracy --",attships,achance);
  982.         vs_curaddress(handle,22,43);
  983.      printf("Defender has %3d ships -%3d%% accuracy",numships[y][x],dchance);
  984.         vs_curaddress(handle,24,12);
  985.         printf(" Press left button to attack - right button to retreat! ");
  986.         player=find_admiral(orders[ord].owner);
  987.         if(player==atari)
  988.         {
  989.             wait(30000L);
  990.             if((numships[y][x]/attships)>2) quit=0;
  991.         }
  992.         else
  993.         {
  994.             while(button!=0) vq_mouse(handle,&button,&mx,&my);
  995.             while(button==0) vq_mouse(handle,&button,&mx,&my);
  996.             if(button==2) quit=0;
  997.             while(button!=0) vq_mouse(handle,&button,&mx,&my);
  998.         }
  999.         erase_line();
  1000.         while(orders[ord].shipsent>0 && numships[y][x]>0 && quit>0)
  1001.         {
  1002.             v_rvon(handle);
  1003.             dshots=numships[y][x];
  1004.             if((contrl_grid[y][x]&15)!=0)
  1005.             {
  1006.                 dshots+=numships[y][x]/2;
  1007.                 if((contrl_grid[y][x]&16)==16)
  1008.                 {
  1009.                     dshots+=numships[y][x]/2;
  1010.                     shields=2;
  1011.                     dshields=1;
  1012.                 }
  1013.             }
  1014.             ashots=orders[ord].shipsent;
  1015.             maxshots=(ashots>dshots) ? ashots : dshots;
  1016.             for(i=1; i<=maxshots; i++)
  1017.             {
  1018.                 if(ashots>0)
  1019.                 {
  1020.                     ashots--;
  1021.                     fire(170);
  1022.                     hit=odds(achance);
  1023.                     if(hit==1)
  1024.                     {
  1025.                         if(dshields==0)
  1026.                         {
  1027.                             numships[y][x]--;
  1028.                             vs_curaddress(handle,22,55);
  1029.                             printf("%4d",numships[y][x]);
  1030.                             dshields=shields;
  1031.                         }
  1032.                         hit_sound();
  1033.                         dshields--;
  1034.                     }
  1035.                     sound_off();
  1036.                     if(numships[y][x]==0)
  1037.                     {
  1038.                         stop=1;
  1039.                         break;
  1040.                     }
  1041.                 }
  1042.                 if(dshots>0)
  1043.                 {
  1044.                     dshots--;
  1045.                     fire(480);
  1046.                     hit=odds(dchance);
  1047.                     if(hit==1)
  1048.                     {
  1049.                         orders[ord].shipsent--;
  1050.                         vs_curaddress(handle,22,14);
  1051.                         printf("%4d",orders[ord].shipsent);
  1052.                         hit_sound();
  1053.                     }
  1054.                     sound_off();
  1055.                     if(orders[ord].shipsent==0)
  1056.                     {
  1057.                         stop=1;
  1058.                         break;
  1059.                      }
  1060.                 }
  1061.                 vq_mouse(handle,&button,&mx,&my);
  1062.                 if(button!=0)
  1063.                 {
  1064.                     vs_curaddress(handle,24,12);
  1065.                     v_rvoff(handle);
  1066.             printf("Left button to continue attack - Right button to retreat");
  1067.                     while(button!=0) vq_mouse(handle,&button,&mx,&my);
  1068.                     while(button==0) vq_mouse(handle,&button,&mx,&my);
  1069.                     erase_line();
  1070.                     v_rvon(handle);
  1071.                     if(button==2) quit=0;
  1072.                 }
  1073.                 if(quit==0) break;
  1074.                 if(stop==1) break;
  1075.             }
  1076.         }
  1077.         won_lost=(orders[ord].shipsent>numships[y][x])? 1 : 0;
  1078.     }
  1079.     if(quit==0)
  1080.     {
  1081.         won_lost=3;
  1082.         if(orders[ord].shipsent>10)
  1083.             orders[ord].shipsent-=orders[ord].shipsent/10;
  1084.     }
  1085.     v_rvoff(handle);
  1086.     return(won_lost);
  1087. }
  1088.  
  1089. /* Move orders in array to fill empty spaces */
  1090.  
  1091. pack_orders()
  1092. {
  1093.     int i,start,empty;
  1094.  
  1095.     empty=find_start();
  1096.     start=empty;
  1097.     for(i=start; i<=300; i++)
  1098.         if(orders[i].eta != 0)
  1099.         {
  1100.             orders[empty].eta=orders[i].eta;
  1101.             orders[i].eta=0;
  1102.             orders[empty].owner=orders[i].owner;
  1103.             orders[empty].shipsent=orders[i].shipsent;
  1104.             orders[empty].xs=orders[i].xs;
  1105.             orders[empty].ys=orders[i].ys;
  1106.             orders[empty].xf=orders[i].xf;
  1107.             orders[empty].yf=orders[i].yf;
  1108.             empty++;
  1109.         }
  1110. }
  1111.  
  1112. /* Draw expanding red box and create sound */
  1113.  
  1114. fire(side)
  1115. int side;
  1116. {
  1117.     char note_hi,note_lo;
  1118.     int i,tone1=202,tone2=304;
  1119.  
  1120.     volume=10;
  1121.  
  1122.     Giaccess(volume,cha_vol+wr_command);
  1123.     Giaccess(volume,chb_vol+wr_command);
  1124.     for(i=4; i<=152; i+=4)
  1125.     {
  1126.         draw_l_box(1,RED,side-i,182,side+i,196);
  1127.         tone1-=4;
  1128.         note_hi=(tone1&hi_mask)>>8;
  1129.         note_lo=(tone1&lo_mask);
  1130.         Giaccess(note_lo,cha_lo+wr_command);
  1131.         Giaccess(note_hi,cha_hi+wr_command);
  1132.         tone2-=4;
  1133.         note_hi=(tone2&hi_mask)>>8;
  1134.         note_lo=(tone2&lo_mask);
  1135.         Giaccess(note_lo,chb_lo+wr_command);
  1136.         Giaccess(note_hi,chb_hi+wr_command);
  1137.         wait(100L);
  1138.     }
  1139.     draw_l_box(0,0,side-i,182,side+i,196);
  1140. }
  1141.  
  1142. /* Calulate chance of miss or hit */
  1143.  
  1144. odds(chance)
  1145. int chance;
  1146. {
  1147.     int shot,results;
  1148.  
  1149.     do
  1150.         shot=Random() & 127;
  1151.     while(shot>100);
  1152.  
  1153.     results=(shot>chance)? 0:1;
  1154.     return(results);
  1155. }
  1156.  
  1157. /* Build ships for each planet */
  1158.  
  1159. add_industry()
  1160. {
  1161.     int r,c;
  1162.  
  1163.     for(r=0; r<=8; r++)
  1164.         for(c=0; c<=19; c++)
  1165.         {
  1166.             numships[r][c]+=industry[r][c];
  1167.         if((limit_pro==1)&&((contrl_grid[r][c]&15)==0)&&(numships[r][c]>30))
  1168.                 numships[r][c]=30;
  1169.         }
  1170. }
  1171.  
  1172. /* Find player number */
  1173.  
  1174. find_admiral(i)
  1175. int i;
  1176. {
  1177.     int results;
  1178.     results=i;
  1179.  
  1180.     if(i<4) results-=1;
  1181.     else results/=2;
  1182.     if(results==4) results-=1;
  1183.  
  1184.     return(results);
  1185. }
  1186.  
  1187. /* BANG */
  1188.  
  1189. hit_sound()
  1190. {
  1191.     char note_hi,note_lo;
  1192.     int tone1,tone2;
  1193.  
  1194.     volume=15;
  1195.  
  1196.     Giaccess(volume,cha_vol+wr_command);
  1197.     Giaccess(volume,chb_vol+wr_command);
  1198.     for(tone1=1280; tone1<=2580; tone1+=10)
  1199.     {
  1200.         tone2=tone1+640;
  1201.         note_hi=(tone1&hi_mask)>>8;
  1202.         note_lo=(tone1&lo_mask);
  1203.         Giaccess(note_lo,cha_lo+wr_command);
  1204.         Giaccess(note_hi,cha_hi+wr_command);
  1205.         note_hi=(tone2&hi_mask)>>8;
  1206.         note_lo=(tone2&lo_mask);
  1207.         Giaccess(note_lo,chb_lo+wr_command);
  1208.         Giaccess(note_hi,chb_hi+wr_command);
  1209.         wait(120L);
  1210.     }
  1211. }
  1212.  
  1213. /* Show status of all players each 5 turns */
  1214.  
  1215. show_status()
  1216. {
  1217.     int i,but,x,y,tot_ships,tot_planets,admiral=1;
  1218.  
  1219.     v_hide_c(handle);
  1220.     color_reg(1,0,0,0);
  1221.     color_reg(0,1000,1000,1000);
  1222.     v_clrwk(handle);
  1223.     v_rvon(handle);
  1224.     vs_curaddress(handle,1,32);
  1225.     printf(" Turn # %d ",turn_num);
  1226.     if(turn_num==maxturns) puts("  GAME OVER! ");
  1227.     for(i=0; i<=numplayers; i++)
  1228.     {
  1229.         v_rvon(handle);
  1230.         vs_curaddress(handle,i*5+3,15);
  1231.         printf("~~~~~~~ Status of Fleet Admiral %s ~~~~~~~",player[i]);
  1232.         v_rvoff(handle);
  1233.         bit_player=admiral;
  1234.         admiral*=2;
  1235.         count(&tot_ships,&tot_planets);
  1236.         vs_curaddress(handle,i*5+4,30);
  1237.         printf("Total Ships = %d",tot_ships);
  1238.         vs_curaddress(handle,i*5+5,30);
  1239.         printf("Total Planets = %d",tot_planets);
  1240.         vs_curaddress(handle,i*5+6,30);
  1241.         printf("Known Area = %d",explored);
  1242.         vs_curaddress(handle,i*5+7,30);
  1243.         printf("Score = %d",score);
  1244.     }
  1245. }
  1246.  
  1247. /* Kill sound */
  1248.  
  1249. sound_off()
  1250. {
  1251.     Giaccess(0,cha_vol+wr_command);
  1252.     Giaccess(0,chb_vol+wr_command);
  1253. }
  1254.  
  1255. /* Show final grid for all players */
  1256.  
  1257. final_show()
  1258. {
  1259.     int i,tot_ships,tot_planets,but,x,y;
  1260.  
  1261.     v_hide_c(handle);
  1262.     vst_color(handle,GREEN);
  1263.     for(i=0; i<=numplayers; i++)
  1264.     {
  1265.         v_clrwk(handle);
  1266.         draw_grid();
  1267.         display_grid(i);
  1268.         vs_curaddress(handle,1,9);
  1269.         printf("  Final Status Report For Fleet Admiral %s. ",player[i]);
  1270.         count(&tot_ships,&tot_planets);
  1271.         vs_curaddress(handle,2,2);
  1272.   printf("Total Ships = %3d ~~ Total Planets = %2d ~~ ",tot_ships,tot_planets);
  1273.         printf("Known Area = %3d  ~~ Score = %5d",explored,score);
  1274.         vs_curaddress(handle,24,26);
  1275.         v_rvon(handle);
  1276.         puts("  Press Button to Continue  ");
  1277.         v_rvoff(handle);
  1278.         but=0;
  1279.         while(but==0) vq_mouse(handle,&but,&x,&y);
  1280.     }
  1281.     v_show_c(handle,0);
  1282. }
  1283.  
  1284. /* Atari's turn to play */
  1285.  
  1286. a_play(player) /*  Check for Atari control */
  1287. int player;
  1288. {
  1289.     int r,c,p;
  1290.  
  1291.     bit_player=num_player(player);
  1292.     p=bit_player;
  1293.  
  1294.     for(r=0;r<=8;r++)
  1295.         for(c=0;c<=19;c++)
  1296.             if(((contrl_grid[r][c]&p)==p)&&(numships[r][c]>0)) search(r,c);
  1297. }
  1298.  
  1299. /* Search for unexplored territory and planets to attack */
  1300.  
  1301. search(r,c)  /* Four search patterns for orders */
  1302. int r,c;
  1303. {
  1304.     int x,y,pattern,flag=0;
  1305.  
  1306.     pattern=Random()&3;
  1307.     switch(pattern)  /* Primary orders search and attack */
  1308.     {
  1309.     case 0:
  1310.         for(y=r-3;y<=r+3;y++)
  1311.             for(x=c-3;x<=c+3;x++)
  1312.                 if((x>-1)&&(x<20)&&(y>-1)&&(y<9)&&(numships[r][c]>0))
  1313.                     flag=primary_orders(r,c,y,x);
  1314.         break;
  1315.     case 1:
  1316.         for(x=c-3;x<=c+3;x++)
  1317.             for(y=r-3;y<=r+3;y++)
  1318.                 if((x>-1)&&(x<20)&&(y>-1)&&(y<9)&&(numships[r][c]>0))
  1319.                     flag=primary_orders(r,c,y,x);
  1320.         break;
  1321.     case 2:
  1322.         for(y=r+3;y>=r-3;y--)
  1323.             for(x=c+3;x>=c-3;x--)
  1324.                 if((x>-1)&&(x<20)&&(y>-1)&&(y<9)&&(numships[r][c]>0))
  1325.                     flag=primary_orders(r,c,y,x);
  1326.         break;
  1327.     case 3:
  1328.         for(x=c+3;x>=c-3;x--)
  1329.             for(y=r+3;y>=r-3;y--)
  1330.                 if((x>-1)&&(x<20)&&(y>-1)&&(y<9)&&(numships[r][c]>0))
  1331.                     flag=primary_orders(r,c,y,x);
  1332.         break;
  1333.     }
  1334.     if(flag==0)
  1335.     {
  1336.     switch(pattern) /* Secondary orders re-inforce planet */
  1337.     {
  1338.     case 0:
  1339.         for(y=r-3;y<=r+3;y++)
  1340.             for(x=c-3;x<=c+3;x++)
  1341.                 if((x>-1)&&(x<20)&&(y>-1)&&(y<9)&&(numships[r][c]>0))
  1342.                     flag=secondary_orders(r,c,y,x);
  1343.         break;
  1344.     case 1:
  1345.         for(x=c-3;x<=c+3;x++)
  1346.             for(y=r-3;y<=r+3;y++)
  1347.                 if((x>-1)&&(x<20)&&(y>-1)&&(y<9)&&(numships[r][c]>0))
  1348.                     flag=secondary_orders(r,c,y,x);
  1349.         break;
  1350.     case 2:
  1351.         for(y=r+3;y>=r-3;y--)
  1352.             for(x=c+3;x>=c-3;x--)
  1353.                 if((x>-1)&&(x<20)&&(y>-1)&&(y<9)&&(numships[r][c]>0))
  1354.                     flag=secondary_orders(r,c,y,x);
  1355.         break;
  1356.     case 3:
  1357.         for(x=c+3;x>=c-3;x--)
  1358.             for(y=r+3;y>=r-3;y--)
  1359.                 if((x>-1)&&(x<20)&&(y>-1)&&(y<9)&&(numships[r][c]>0))
  1360.                     flag=secondary_orders(r,c,y,x);
  1361.         break;
  1362.     }
  1363.     }
  1364.     if((flag==0)&&((contrl_grid[r][c]&16)!=16)) go_home(r,c);
  1365. }
  1366.  
  1367. /* Explore and attack */
  1368.  
  1369. primary_orders(r,c,y,x)
  1370. int r,c,y,x;
  1371. {
  1372.     int flag=0;
  1373.  
  1374.     if((contrl_grid[r][c]&16)==16)
  1375.     {
  1376.         if(numships[r][c]>15)
  1377.         {
  1378.             if(a_grid[y][x]==0)
  1379.             {
  1380.                 if(turn_num==0) store_a_orders(r,c,y,x,5);
  1381.                 else store_a_orders(r,c,y,x,1);
  1382.                 a_grid[y][x]=1;
  1383.             }
  1384.             if((area_kn[y][x]&bit_player)==bit_player)
  1385.             {
  1386.                 if((contrl_grid[y][x]&31)==16)
  1387.                     s_a_fleet(r,c,y,x);
  1388.                 if(((contrl_grid[y][x]&31)>16)&&(numships[y][x]<10))
  1389.                     s_a_fleet(r,c,y,x);
  1390.             }
  1391.         }
  1392.         flag=1;
  1393.     }
  1394.     else if(a_grid[y][x]==0)
  1395.     {
  1396.         store_a_orders(r,c,y,x,1);
  1397.         a_grid[y][x]=1;
  1398.         flag=1;
  1399.     }
  1400.     return(flag);
  1401. }
  1402.  
  1403. /* Re-inforce planets */
  1404.  
  1405. secondary_orders(r,c,y,x)
  1406. int r,c,y,x;
  1407. {
  1408.     int ct,fleet,flag=0;
  1409.  
  1410.     ct=bit_player+16;
  1411.     if((contrl_grid[y][x]&ct)==ct)
  1412.     {
  1413.         if(((contrl_grid[r][c]&16)==16)&&(numships[r][c]>20))
  1414.         {
  1415.             fleet=numships[r][c]-15;
  1416.             store_a_orders(r,c,y,x,fleet);
  1417.         }
  1418.         else store_a_orders(r,c,y,x,numships[r][c]);
  1419.         flag=1;
  1420.     }
  1421.     return(flag);
  1422. }
  1423.  
  1424. /* Send a fleet to conquer */
  1425.  
  1426. s_a_fleet(r,c,y,x)
  1427. int r,c,y,x;
  1428. {
  1429.     int fleet;
  1430.  
  1431.     if(numships[r][c]>25)
  1432.     {
  1433.         fleet=numships[r][c]-12;
  1434.         if(fleet>(6*turn_num+6))
  1435.         {
  1436.             fleet=6*turn_num+6;
  1437.             store_a_orders(r,c,y,x,fleet);
  1438.         }
  1439.         else if((limit_pro==1)&&(turn_num>4)&&(fleet>35))
  1440.         {
  1441.             fleet=35;
  1442.             store_a_orders(r,c,y,x,fleet);
  1443.         }
  1444.     }
  1445. }
  1446.  
  1447. /* Unable to explore or conquer - go to home planet */
  1448.  
  1449. go_home(r,c)
  1450. int r,c;
  1451. {
  1452.     int x,y;
  1453.  
  1454.     x=atari_xy[0];
  1455.     y=atari_xy[1];
  1456.     store_a_orders(r,c,y,x,numships[r][c]);
  1457. }
  1458.  
  1459. /* Place Atari orders in array */
  1460.  
  1461. store_a_orders(r,c,y,x,fleet)
  1462. int r,c,y,x,fleet;
  1463. {
  1464.     int empty;
  1465.  
  1466.     empty=find_start();
  1467.     numships[r][c]-=fleet;
  1468.     orders[empty].eta=1;
  1469.     orders[empty].owner=bit_player;
  1470.     orders[empty].shipsent=fleet;
  1471.     orders[empty].xs=x;
  1472.     orders[empty].ys=y;
  1473.     orders[empty].xf=c;
  1474.     orders[empty].yf=r;
  1475. }
  1476.  
  1477. /* Save for a later date */
  1478.  
  1479. save_game()
  1480. {
  1481.     int button,wr_handle,i,j,ct;
  1482.  
  1483.     wr_handle= -1;
  1484.     for(i=0;i<=8;i++)
  1485.         for(j=0;j<=19;j++)
  1486.         {
  1487.             buffer[j+(i*20)]=contrl_grid[i][j];
  1488.             buffer[j+180+(i*20)]=area_kn[i][j];
  1489.             buffer[j+360+(i*20)]=numships[i][j];
  1490.             buffer[j+540+(i*20)]=industry[i][j];
  1491.             buffer[j+720+(i*20)]=a_grid[i][j];
  1492.         }
  1493.     for(i=0;i<=3;i++)
  1494.         for(j=0;j<=20;j++)
  1495.             buffer[j+900+(i*21)]=player[i][j];
  1496.     buffer[1000]=numplayers;
  1497.     buffer[1001]=turn_num;
  1498.     buffer[1002]=maxturns;
  1499.     buffer[1003]=vis_ships;
  1500.     buffer[1004]=limit_pro;
  1501.     buffer[1005]=atari;
  1502.     buffer[1006]=atari_xy[0];
  1503.     buffer[1007]=atari_xy[1];
  1504.     ct=find_start();
  1505.     buffer[1008]=ct;
  1506.     for(i=0;i<=ct;i++)
  1507.     {
  1508.         buffer[1010+(i*7)]=orders[i].eta;
  1509.         buffer[1011+(i*7)]=orders[i].owner;
  1510.         buffer[1012+(i*7)]=orders[i].shipsent;
  1511.         buffer[1013+(i*7)]=orders[i].xs;
  1512.         buffer[1014+(i*7)]=orders[i].ys;
  1513.         buffer[1015+(i*7)]=orders[i].xf;
  1514.         buffer[1016+(i*7)]=orders[i].yf;
  1515.     }
  1516.     v_show_c(handle,0);
  1517.     drive=Dgetdrv();
  1518.     path[0]=(drive+'A');
  1519.  
  1520.     fsel_input(path,io_file,&button);
  1521.     if(button!=0)
  1522.     {
  1523.         wr_handle=Fcreate(io_file,0);
  1524.         if(wr_handle> -1)
  1525.             Fwrite(wr_handle,f_length,buffer);
  1526.         Fclose(wr_handle);
  1527.     }
  1528.     v_hide_c(handle);
  1529.     return(wr_handle);
  1530. }
  1531.  
  1532. /* Restore old game */
  1533.  
  1534. load_game()
  1535. {
  1536.     int button,rd_handle,i,j,ct;
  1537.     char rn;
  1538.  
  1539.     rd_handle= -1;
  1540.     rn=question();
  1541.     if(rn=='R' || rn=='r')
  1542.     {
  1543.         v_show_c(handle,0);
  1544.         drive=Dgetdrv();
  1545.         path[0]=(drive+'A');
  1546.  
  1547.         fsel_input(path,io_file,&button);
  1548.         if(button!=0)
  1549.         {
  1550.             rd_handle=Fopen(io_file,0);
  1551.             if(rd_handle>-1)
  1552.                 Fread(rd_handle,f_length,buffer);
  1553.             Fclose(rd_handle);
  1554.             for(i=0;i<=8;i++)
  1555.                 for(j=0;j<=19;j++)
  1556.                 {
  1557.                     contrl_grid[i][j]=buffer[j+(i*20)];
  1558.                     area_kn[i][j]=buffer[j+180+(i*20)];
  1559.                     numships[i][j]=buffer[j+360+(i*20)];
  1560.                     industry[i][j]=buffer[j+540+(i*20)];
  1561.                     a_grid[i][j]=buffer[j+720+(i*20)];
  1562.                 }
  1563.             for(i=0;i<=3;i++)
  1564.                 for(j=0;j<=20;j++)
  1565.                     player[i][j]=buffer[j+900+(i*21)];
  1566.             numplayers=buffer[1000];
  1567.             turn_num=buffer[1001];
  1568.             maxturns=buffer[1002];
  1569.             vis_ships=buffer[1003];
  1570.             limit_pro=buffer[1004];
  1571.             atari=buffer[1005];
  1572.             atari_xy[0]=buffer[1006];
  1573.             atari_xy[1]=buffer[1007];
  1574.             ct=buffer[1008];
  1575.             for(i=0;i<=ct;i++)
  1576.             {
  1577.                 orders[i].eta=buffer[1010+(i*7)];
  1578.                 orders[i].owner=buffer[1011+(i*7)];
  1579.                 orders[i].shipsent=buffer[1012+(i*7)];
  1580.                 orders[i].xs=buffer[1013+(i*7)];
  1581.                 orders[i].ys=buffer[1014+(i*7)];
  1582.                 orders[i].xf=buffer[1015+(i*7)];
  1583.                 orders[i].yf=buffer[1016+(i*7)];
  1584.             }
  1585.         }
  1586.         v_hide_c(handle);
  1587.     }
  1588.     return(rd_handle);
  1589. }
  1590.  
  1591. /* New game or old */
  1592.  
  1593. question()
  1594. {
  1595.     char rn;
  1596.     int xy[2];
  1597.  
  1598.     vs_curaddress(handle,14,27);
  1599.     v_rvon(handle);
  1600.     printf("N");
  1601.     v_rvoff(handle);
  1602.     printf("ew game - ");
  1603.     v_rvon(handle);
  1604.     printf("R");
  1605.     v_rvoff(handle);
  1606.     printf("estore old game");
  1607.     while((rn!='r')&&(rn!='R')&&(rn!='n')&&(rn!='N'))
  1608.         vrq_string(handle,1,0,xy,&rn);
  1609.     return(rn);
  1610. }
  1611.