home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume2 / conquest / part05 / io.c < prev   
C/C++ Source or Header  |  1987-10-26  |  11KB  |  389 lines

  1. /*io.c*/
  2. /*Print and io subroutines for game*/
  3.  
  4. /*conquest is copyrighted 1986 by Ed Barlow.
  5.  *  I spent a long time writing this code & I hope that you respect this.  
  6.  *  I give permission to alter the code, but not to copy or redistribute
  7.  *  it without my explicit permission.  If you alter the code, 
  8.  *  please document changes and send me a copy, so all can have it.  
  9.  *  This code, to the best of my knowledge works well,  but it is my first
  10.  *  'C' program and should be treated as such.  I disclaim any
  11.  *  responsibility for the codes actions (use at your own risk).  I guess
  12.  *  I am saying "Happy gaming", and am trying not to get sued in the process.
  13.  *                                                Ed
  14.  */
  15.  
  16. /*include files*/
  17. #include <ctype.h>
  18. #include "header.h"
  19.  
  20. /*Declarations*/
  21. extern struct s_sector sct[MAPX][MAPY];
  22. extern struct nation ntn[NTOTAL];   /* player nation stats */
  23. /*is sector occupied by an army?*/
  24. extern short occ[MAPX][MAPY];
  25.  
  26. /*offset of upper left hand corner*/
  27. extern short xoffset;
  28. extern short yoffset;
  29. /*current cursor postion (relative to 00 in upper corner)*/
  30. /*    position is 2*x,y*/
  31. extern short xcurs;
  32. extern short ycurs;
  33. /*redraw map in this turn if redraw is a 1*/
  34. extern short redraw;
  35. /*display state*/
  36. extern short hilmode;   /*highlight modes: 0=owned sectors, 1= armies, 2=none*/
  37. extern short dismode;   /*display mode: 1=vegitation, 2=desig, 3=contour*/
  38. /*         4=armies/navies, 5=commodities, 6=fertility, 7=movement*/
  39. /* nation id of owner*/
  40. extern short country;
  41.  
  42. /*print a sector.altitude map subroutine*/
  43. printele()
  44. {
  45.     register int X, Y;
  46.     printf("doing print of altitude\n");
  47.     for(Y=0;Y<MAPY;Y++) {
  48.         for(X=0;X<MAPX;X++) putc(sct[X][Y].altitude,stdout);
  49.         putc('\n',stdout);
  50.     }
  51. }
  52.  
  53. pr_ntns()
  54. {
  55.     register int X, Y;
  56.     printf("doing print of nations\n");
  57.     for(Y=0;Y<MAPY;Y++) {
  58.         for(X=0;X<MAPX;X++) {
  59.             if(sct[X][Y].owner==0)
  60.                 putc(sct[X][Y].altitude,stdout);
  61.             else putc(ntn[sct[X][Y].owner].mark,stdout);
  62.         }
  63.         putc('\n',stdout);
  64.     }
  65. }
  66.  
  67. /*print all data--trashes/creates datafile in the process*/
  68. writedata()
  69. {
  70.     int fd;
  71.     printf("\ndoing write of data\n");
  72.     if((fd = creat(DATAFILE,0666))==-1) {
  73.         printf("cant open data.  check permissions\n");
  74.         return;
  75.     }
  76.     write(fd,sct,sizeof(sct));
  77.     write(fd,ntn,sizeof(ntn));
  78.     close(fd);
  79. }
  80.  
  81. readdata()
  82. {
  83.     int fd;
  84.     int n_read;
  85.     /*read in existing nation army and navy data*/
  86.     /*check if file openable*/
  87.     printf("reading data file\n");
  88.     fd=open(DATAFILE,0);
  89.     if((n_read=read(fd,sct,sizeof(sct)))==0) printf("EOF\n");
  90.     else if(n_read==-1) printf("error reading sector data (sct)\n");
  91.     if(n_read!=sizeof(sct)) {
  92.         printf("wrong data format\n");
  93.         exit(1);
  94.     }
  95.     if((n_read=read(fd,ntn,sizeof(ntn)))==0) printf("EOF\n");
  96.     else if(n_read==-1) printf("error reading nation data (ntn)\n");
  97.     if(n_read!=sizeof(ntn)) {
  98.         printf("wrong data format\n");
  99.         exit(1);
  100.     }
  101.     close(fd);
  102. }
  103.  
  104. /*print a map subroutine*/
  105. printveg()
  106. {
  107.     register int X, Y;
  108.     printf("doing print of vegitation\n");
  109.     for(Y=0;Y<MAPY;Y++) {
  110.         for(X=0;X<MAPX;X++) putc(sct[X][Y].vegitation,stdout);
  111.         putc('\n',stdout);
  112.     }
  113. }
  114.  
  115. /*make a map*/
  116. makemap()
  117. {
  118.     register int x,y;
  119.     register int i,j;
  120.     short armynum,nvynum;
  121.  
  122.     /*can you see all?*/
  123.     if((magic(country,KNOWALL)==1)||(country==0)) {
  124.         for(x=0;x<(COLS-21)/2;x++) {
  125.             for(y=0;y<(LINES-4);y++) {
  126.                 highlight(x,y);
  127.                 see(x,y);
  128.             }
  129.         }
  130.         if((hilmode==1)||(hilmode==4)||(hilmode==3)) {
  131.             for(armynum=0;armynum<MAXARM;armynum++) 
  132.             if((ASOLD>0)&&((AMOVE>0)||(hilmode!=4))){
  133.                 standout();
  134.                 see(AXLOC-xoffset,AYLOC-yoffset);
  135.             }
  136.         }
  137.     }
  138.     /*see as appropriate?*/
  139.     else {
  140.         for(x=0;x<(COLS-21)/2;x++) for(y=0;y<(LINES-4);y++) {
  141.             if(sct[x+xoffset][y+yoffset].owner==country){
  142.                 for(i=x-LANDSEE;i<=x+LANDSEE;i++){
  143.                     for(j=y-LANDSEE;j<=y+LANDSEE;j++) {
  144.                         highlight(i,j);
  145.                         see(i,j);
  146.                     }
  147.                 }
  148.             }
  149.         }
  150.         for(nvynum=0;nvynum<MAXNAVY;nvynum++) if(NMER+NWAR>0){
  151.             for(i=NXLOC-xoffset-NAVYSEE;i<=NXLOC-xoffset+NAVYSEE;i++) for(j=NYLOC-yoffset-NAVYSEE;j<=NYLOC-yoffset+NAVYSEE;j++){
  152.                 highlight(i,j);
  153.                 see(i,j);
  154.             }
  155.         }
  156.         for(armynum=0;armynum<MAXARM;armynum++) if(ASOLD>0){
  157.             for(i=AXLOC-xoffset-ARMYSEE;i<=AXLOC-xoffset+ARMYSEE;i++) for(j=AYLOC-yoffset-ARMYSEE;j<=AYLOC-yoffset+ARMYSEE;j++) {
  158.                 highlight(i,j);
  159.                 see(i,j);
  160.             }
  161.         }
  162.         /*optimal method of highlighting your armies*/
  163.         if((hilmode==3)||(hilmode==4)) {
  164.             for(armynum=0;armynum<MAXARM;armynum++) 
  165.             if((ASOLD>0)&&((AMOVE>0)||(hilmode==3))){
  166.                 standout();
  167.                 see(AXLOC-xoffset,AYLOC-yoffset);
  168.             }
  169.             for(nvynum=0;nvynum<MAXNAVY;nvynum++) 
  170.             if((NWAR+NMER>0)&&((NMOVE>0)||(hilmode==3))){
  171.                 standout();
  172.                 see(NXLOC-xoffset,NYLOC-yoffset);
  173.             }
  174.         }
  175.     }
  176.     move(ycurs,2*xcurs);
  177. }
  178.  
  179. newdisplay()
  180. {
  181.     mvaddstr(LINES-4,0,"viewing options:  (d)esignation, (r)ace, (m)ove cost, (p)eople, (D)efense");
  182.     clrtoeol();
  183.         mvaddstr(LINES-3,0,"                  (c)ontour, (v)egitation, (i)ron, (n)ation mark, (g)old");
  184.     clrtoeol();
  185.     mvaddstr(LINES-2,0,"highlight option: (o)wners, (a)rmy, (y)our Army, (M)ove left, (x)=none");
  186.     clrtoeol();
  187.     standout();
  188.     mvaddstr(LINES-1,0,"what display?:");
  189.     clrtoeol();
  190.     move(LINES-1,16);
  191.     standend();
  192.     refresh();
  193.     redraw=TRUE;
  194.     switch(getch()) {
  195.     case 'v':    /* vegetation map*/
  196.         dismode=1;
  197.         break;
  198.     case 'd':    /* designations map*/
  199.         dismode=2;
  200.         break;
  201.     case 'c':    /* contour map of world */
  202.         dismode=3;
  203.         break;
  204.     case 'n':    /* nations map*/
  205.         dismode=4;
  206.         break;
  207.     case 'r':    /* race map*/
  208.         dismode=5;
  209.         break;
  210.     case 'm':    /* move cost map*/
  211.         dismode=7;
  212.         break;
  213.     case 'D':
  214.         dismode=8;
  215.         break;
  216.     case 'p':
  217.         dismode=9;
  218.         break;
  219.     case 'g':
  220.         dismode=10;
  221.         break;
  222.     case 'i':
  223.         dismode=11;
  224.         break;
  225.     case 'a':    /* armies hilighted map*/
  226.         prep();
  227.         hilmode=1;
  228.         break;
  229.     case 'o':    /* owners hilighted map*/
  230.         hilmode=0;
  231.         break;
  232.     case 'x':    /*no highlighting*/
  233.         hilmode=2;
  234.         break;
  235.     case 'y':    /* your armies hilighted map*/
  236.         prep();
  237.         hilmode=3;
  238.         break;
  239.     case 'M':    /* your armies with moves left hilighted map*/
  240.         prep();
  241.         hilmode=4;
  242.         break;
  243.     default:
  244.         beep();
  245.         redraw=FALSE;
  246.     }
  247.     makebottom();
  248. }
  249.  
  250. /*see what is in xy as per display mode*/
  251. see(x,y)
  252. {
  253.     int armbonus;
  254.     if((x<0)||(y<0)||(x>COLS-21)||(y>=LINES-4)) return;
  255.     if(((y+yoffset)<MAPY)&&((x+xoffset)<MAPX)) {
  256.  
  257.         if((dismode>7)&&(country!=sct[x+xoffset][y+yoffset].owner)&&(magic(sct[x+xoffset][y+yoffset].owner,THE_VOID)==1)&&(country!=0)) {
  258.             standout();
  259.             mvaddch(y,2*x,' ');
  260.             standend();
  261.         }
  262.         else {
  263.             switch(dismode){
  264.             case 1: /*vegitation*/
  265.                 mvaddch(y,2*x,sct[x+xoffset][y+yoffset].vegitation);
  266.                 break;
  267.             case 2: /*designation*/
  268.                 if(sct[x+xoffset][y+yoffset].owner==0){
  269.                     if(isdigit(sct[x+xoffset][y+yoffset].vegitation)!=0) mvaddch(y,2*x,sct[x+xoffset][y+yoffset].altitude);
  270.                     else mvaddch(y,2*x,sct[x+xoffset][y+yoffset].vegitation);
  271.                 }
  272.                 else if((country==0)||(sct[x+xoffset][y+yoffset].owner==country)) mvaddch(y,2*x,sct[x+xoffset][y+yoffset].designation);
  273.                 else mvaddch(y,2*x,ntn[sct[x+xoffset][y+yoffset].owner].mark);
  274.                 break;
  275.             case 3: /*contour*/
  276.                 mvaddch(y,2*x,sct[x+xoffset][y+yoffset].altitude);
  277.                 break;
  278.             case 4: /*ownership*/
  279.                 if(sct[x+xoffset][y+yoffset].owner==0)
  280.                     mvaddch(y,2*x,sct[x+xoffset][y+yoffset].altitude);
  281.                 else mvaddch(y,2*x,ntn[sct[x+xoffset][y+yoffset].owner].mark);
  282.                 break;
  283.             case 5: /*race*/
  284.                 if(sct[x+xoffset][y+yoffset].owner==0)
  285.                     mvaddch(y,2*x,sct[x+xoffset][y+yoffset].altitude);
  286.                 else mvaddch(y,2*x,ntn[sct[x+xoffset][y+yoffset].owner].race);
  287.                 break;
  288.             case 7:    /*movement cost map*/
  289.                 if(movecost[x+xoffset][y+yoffset]>=0) mvprintw(y,2*x,"%d",movecost[x+xoffset][y+yoffset]);
  290.                 else if(sct[x+xoffset][y+yoffset].altitude==WATER)
  291.                     mvaddch(y,2*x,'~');
  292.                 else
  293.                     mvaddch(y,2*x,'X');
  294.                 break;
  295.             case 8:   /*Defence*/
  296.                 if (sct[x+xoffset][y+yoffset].altitude==WATER)
  297.                     mvaddch(y,2*x,'~');
  298.                 else if (movecost[x+xoffset][y+yoffset]<0)
  299.                     mvaddch(y,2*x,'*');
  300.                 else {
  301.  
  302.                     /*Racial combat bonus due to terrain (the faster you move the better)*/
  303.                     armbonus=0;
  304.                     armbonus+=5*(9-movecost[x+xoffset][y+yoffset]);
  305.  
  306.                     if(sct[x+xoffset][y+yoffset].altitude==MOUNTAIN) armbonus+=40;
  307.                     else if(sct[x+xoffset][y+yoffset].altitude==HILL) armbonus+=20;
  308.  
  309.                     if(sct[x+xoffset][y+yoffset].vegitation==JUNGLE)
  310.                         armbonus+=30;
  311.                     else if(sct[x+xoffset][y+yoffset].vegitation==FORREST)
  312.                         armbonus+=20;
  313.                     else if(sct[x+xoffset][y+yoffset].vegitation==WOOD)
  314.                         armbonus+=10;
  315.  
  316.                     if((sct[x+xoffset][y+yoffset].designation==DCASTLE)||(sct[x+xoffset][y+yoffset].designation==DCITY)||(sct[x+xoffset][y+yoffset].designation==DCAPITOL))
  317.                         armbonus+=8*sct[x+xoffset][y+yoffset].fortress;
  318.  
  319.                     mvprintw(y,2*x,"%d",armbonus/20);
  320.                 }
  321.                 break;
  322.             case 9:   /*People*/
  323.                 if (sct[x+xoffset][y+yoffset].altitude==WATER)
  324.                     mvaddch(y,2*x,'~');
  325.                 else if (sct[x+xoffset][y+yoffset].people>=1000)
  326.                     mvaddch(y,2*x,'+');
  327.                 else if (sct[x+xoffset][y+yoffset].people>=450)
  328.                     mvaddch(y,2*x,'>');
  329.                 else if (sct[x+xoffset][y+yoffset].people==0)
  330.                     mvaddch(y,2*x,'0');
  331.                 else
  332.                     mvprintw(y,2*x,"%d",1+sct[x+xoffset][y+yoffset].people/50);
  333.                 break;
  334.             case 10:  /*Gold*/
  335.                 if (sct[x+xoffset][y+yoffset].altitude==WATER)
  336.                     mvaddch(y,2*x,'~');
  337.                 else if(isdigit(sct[x+xoffset][y+yoffset].vegitation)==0) 
  338.                     mvaddch(y,2*x,'X');
  339.                 else if((sct[x+xoffset][y+yoffset].owner!=0)&&(sct[x+xoffset][y+yoffset].owner!=country))
  340.                     mvaddch(y,2*x,'?');
  341.                 else if(sct[x+xoffset][y+yoffset].gold>=10)
  342.                     mvaddch(y,2*x,'+');
  343.                 else
  344.                     mvprintw(y,2*x,"%d",sct[x+xoffset][y+yoffset].gold);
  345.                 break;
  346.             case 11:  /*Iron*/
  347.                 if (sct[x+xoffset][y+yoffset].altitude==WATER)
  348.                     mvaddch(y,2*x,'~');
  349.                 else if(isdigit(sct[x+xoffset][y+yoffset].vegitation)==0) 
  350.                     mvaddch(y,2*x,'X');
  351.                 else if((sct[x+xoffset][y+yoffset].owner!=0)&&(sct[x+xoffset][y+yoffset].owner!=country))
  352.                     mvaddch(y,2*x,'?');
  353.                 else if (sct[x+xoffset][y+yoffset].iron>=10)
  354.                     mvaddch(y,2*x,'+');
  355.                 else
  356.                     mvprintw(y,2*x,"%d",sct[x+xoffset][y+yoffset].iron);
  357.                 break;
  358.             default:
  359.                 break;
  360.             }
  361.         }
  362.     }
  363.     else mvaddch(y,2*x,' ');
  364.     standend();
  365. }
  366.  
  367. /*highlight what is in xy as per highlight mode*/
  368. highlight(x,y)
  369. {
  370.     if((x<0)||(y<0)||(x>COLS-21)||(y>=LINES-4)) return;
  371.     if(((y+yoffset)<MAPY)&&((x+xoffset)<MAPX)) {
  372.         switch(hilmode){
  373.         case 0: /*ownership*/
  374.             if(country==0) {
  375.                 if(sct[x+xoffset][y+yoffset].owner>0)
  376.                     standout();
  377.             }
  378.             else if(sct[x+xoffset][y+yoffset].owner==country)
  379.                 standout();
  380.             break;
  381.         case 1: /*army map*/
  382.             if(occ[x+xoffset][y+yoffset]!=0) standout();
  383.             break;
  384.         default:
  385.             break;
  386.         }
  387.     }
  388. }
  389.