home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume6 / conquer4 / part11 / display.c next >
C/C++ Source or Header  |  1989-07-06  |  12KB  |  425 lines

  1. /*Print and io subroutines for interactive game*/
  2.  
  3. /*conquer : Copyright (c) 1988 by Ed Barlow.
  4.  *  I spent a long time writing this code & I hope that you respect this.
  5.  *  I give permission to alter the code, but not to copy or redistribute
  6.  *  it without my explicit permission.  If you alter the code,
  7.  *  please document changes and send me a copy, so all can have it.
  8.  *  This code, to the best of my knowledge works well,  but it is my first
  9.  *  'C' program and should be treated as such.  I disclaim any
  10.  *  responsibility for the codes actions (use at your own risk).  I guess
  11.  *  I am saying "Happy gaming", and am trying not to get sued in the process.
  12.  *                                                Ed
  13.  */
  14.  
  15. /*include files*/
  16. #include <ctype.h>
  17. #include "header.h"
  18. #include "data.h"
  19.  
  20. /*offset of upper left hand corner*/
  21. extern short xoffset;
  22. extern short yoffset;
  23. /*current cursor postion (relative to 00 in upper corner)*/
  24. /*    position is 2*x,y*/
  25. extern short xcurs;
  26. extern short ycurs;
  27. /*redraw map in this turn if redraw is a 1*/
  28. extern short redraw;
  29. /*display state SEE data.h FOR CURRENT VALUES OF THESE */
  30. extern short hilmode;
  31. extern short dismode;
  32. short movmode;
  33. /* nation id of owner*/
  34. extern short country;
  35.  
  36. static char *hasseen;
  37.  
  38. /* allocate space for the hasseen array based on the actual screen size */
  39. void
  40. init_hasseen()
  41. {
  42.     hasseen = (char *)malloc(((COLS-10)/2) * (LINES-5));
  43. #ifdef BSD
  44.     bzero(hasseen,((COLS-10)/2) * (LINES-5));
  45. #else
  46.     memset( hasseen, 0, ((COLS-10)/2) * (LINES-5));
  47. #endif
  48.     if (hasseen == (char *)NULL) {
  49.         errormsg("Cannot allocate memory.");
  50.         bye(FALSE);
  51.     }
  52. }
  53.  
  54. /*make a map*/
  55. void
  56. makemap()
  57. {
  58.     register int x,y;
  59.  
  60.     for(x=0;x<SCREEN_X_SIZE;x++) for(y=0;y<SCREEN_Y_SIZE;y++)
  61.     if( HAS_SEEN(x,y) ) {
  62.         highlight(x,y);
  63.         see(x,y);
  64.     }
  65.     move(ycurs,2*xcurs);
  66. }
  67.  
  68. void
  69. newdisplay()
  70. {
  71.     mvaddstr(LINES-4,0,"viewing options:  (d)esignation, (r)ace, (M)ove cost, (p)eople, (D)efense");
  72.     clrtoeol();
  73.     mvaddstr(LINES-3,0,"   (f)ood, (c)ontour, (v)egetation, (m)etal, (n)ation mark, (j)ewels, (i)tems");
  74.     clrtoeol();
  75.     mvaddstr(LINES-2,0,"highlight option: (o)wners, (a)rmy, (y)our Army, move (l)eft, (s)pecial,(x)=none");
  76.     clrtoeol();
  77.     mvaddstr(LINES-1,0,"toggle move mode: hit '/'");
  78.     clrtoeol();
  79.     standout();
  80.     mvaddstr(LINES-1,COLS-25,"what display?:");
  81.     standend();
  82.     refresh();
  83.     redraw=TRUE;
  84.     switch(getch()) {
  85.     case '/':
  86.         if( movmode==TRUE ) movmode=FALSE;
  87.         else movmode=TRUE;
  88.         break;
  89.     case 'f':
  90.         dismode=DI_FOOD;
  91.         break;
  92.     case 'v':    /* vegetation map*/
  93.         dismode=DI_VEGE;
  94.         break;
  95.     case 'd':    /* designations map*/
  96.         movmode=FALSE;
  97.         dismode=DI_DESI;
  98.         break;
  99.     case 'c':    /* contour map of world */
  100.         dismode=DI_CONT;
  101.         break;
  102.     case 'n':    /* nations map*/
  103.         dismode=DI_NATI;
  104.         break;
  105.     case 'r':    /* race map*/
  106.         dismode=DI_RACE;
  107.         break;
  108.     case 'M': /* move cost map */
  109.         dismode=DI_MOVE;
  110.         break;
  111.     case 'D':
  112.         dismode=DI_DEFE;
  113.         break;
  114.     case 'p':
  115.         dismode=DI_PEOP;
  116.         break;
  117.     case 'j':
  118.         dismode=DI_GOLD;
  119.         break;
  120.     case 'm':
  121.         dismode=DI_METAL;
  122.         break;
  123.     case 'i':
  124.         dismode=DI_ITEMS;
  125.         break;
  126.     case 'a':    /* armies hilighted map*/
  127.         prep(country,FALSE);
  128.         hilmode=HI_ARMY;
  129.         break;
  130.     case 'o':    /* owners hilighted map*/
  131.         hilmode=HI_OWN;
  132.         break;
  133.     case 's':    /* hilight tradegoods */
  134.         hilmode=HI_GOOD;
  135.         break;
  136.     case 'x':    /*no highlighting*/
  137.         hilmode=HI_NONE;
  138.         break;
  139.     case 'y':    /* your armies hilighted map*/
  140.         prep(country,FALSE);
  141.         hilmode=HI_YARM;
  142.         break;
  143.     case 'l':    /* your armies with moves left hilighted map*/
  144.         prep(country,FALSE);
  145.         hilmode=HI_MOVE;
  146.         break;
  147.     default:
  148.         beep();
  149.         redraw=FALSE;
  150.     }
  151.     makebottom();
  152. }
  153.  
  154. /*see what is in xy as per display mode*/
  155. /* also add move cost next to sector */
  156. void
  157. see(x,y)
  158. {
  159.     int armbonus;
  160.     if((x<0)||(y<0)||(x>=SCREEN_X_SIZE)||(y>=SCREEN_Y_SIZE)
  161.     ||((y+yoffset)>=MAPY)||((x+xoffset)>=MAPX)) return;
  162.     if(((y+yoffset)<MAPY)&&((x+xoffset)<MAPX)) {
  163.  
  164.         if((magic(sct[x+xoffset][y+yoffset].owner,THE_VOID)==TRUE)
  165.         &&((dismode==DI_DEFE)||(dismode==DI_GOLD)||(dismode==DI_METAL)
  166.             ||(dismode==DI_PEOP)||(dismode==DI_FOOD)||(dismode==DI_ITEMS))
  167.         &&(country!=sct[x+xoffset][y+yoffset].owner)
  168.         &&(magic(country,NINJA)!=TRUE)
  169.         &&(country!=0)) {
  170.             mvaddch(y,2*x,'?');
  171.         } else {
  172.             switch(dismode){
  173.             case DI_FOOD:    /*food */
  174.                 if(tofood( &sct[x+xoffset][y+yoffset],country)==0)
  175.                 mvaddch(y,2*x,sct[x+xoffset][y+yoffset].vegetation);
  176.                 else if (tofood( &sct[x+xoffset][y+yoffset],country)<10)
  177.                 mvprintw(y,2*x,"%d",tofood( &sct[x+xoffset][y+yoffset],country));
  178.                 else mvaddch(y,2*x,'+');
  179.                 break;
  180.             case DI_VEGE: /*vegetation*/
  181.                 mvaddch(y,2*x,sct[x+xoffset][y+yoffset].vegetation);
  182.                 break;
  183.             case DI_DESI: /*designation*/
  184.                 if(sct[x+xoffset][y+yoffset].owner==0){
  185.                     if(tofood( &sct[x+xoffset][y+yoffset],sct[x+xoffset][y+yoffset].owner)!=0) mvaddch(y,2*x,sct[x+xoffset][y+yoffset].altitude);
  186.                     else mvaddch(y,2*x,sct[x+xoffset][y+yoffset].vegetation);
  187.                 }
  188.                 else if((country==0)
  189.                 ||(sct[x+xoffset][y+yoffset].owner==country))
  190.                 mvaddch(y,2*x,sct[x+xoffset][y+yoffset].designation);
  191.                 else mvaddch(y,2*x,ntn[sct[x+xoffset][y+yoffset].owner].mark);
  192.                 break;
  193.             case DI_CONT: /*contour*/
  194.                 mvaddch(y,2*x,sct[x+xoffset][y+yoffset].altitude);
  195.                 break;
  196.             case DI_NATI: /*ownership*/
  197.                 if(sct[x+xoffset][y+yoffset].owner==0)
  198.                     mvaddch(y,2*x,sct[x+xoffset][y+yoffset].altitude);
  199.                 else mvaddch(y,2*x,ntn[sct[x+xoffset][y+yoffset].owner].mark);
  200.                 break;
  201.             case DI_RACE: /*race*/
  202.                 if(sct[x+xoffset][y+yoffset].owner==0)
  203.                     mvaddch(y,2*x,sct[x+xoffset][y+yoffset].altitude);
  204.                 else mvaddch(y,2*x,ntn[sct[x+xoffset][y+yoffset].owner].race);
  205.                 break;
  206.             case DI_MOVE:    /*movement cost map*/
  207.                 if(movecost[x+xoffset][y+yoffset]>=0) {
  208.                     if(movecost[x+xoffset][y+yoffset]>=10)
  209.                         mvaddch(y,2*x,'+');
  210.                     else mvprintw(y,2*x,"%d",movecost[x+xoffset][y+yoffset]);
  211.                 } else if(sct[x+xoffset][y+yoffset].altitude==WATER)
  212.                     mvaddch(y,2*x,WATER);
  213.                 else
  214.                     mvaddch(y,2*x,'X');
  215.                 break;
  216.             case DI_DEFE:   /*Defence*/
  217.                 if (sct[x+xoffset][y+yoffset].altitude==WATER)
  218.                     mvaddch(y,2*x,WATER);
  219.                 else if(movecost[x+xoffset][y+yoffset]<0)
  220.                     mvaddch(y,2*x,'X');
  221.                 else {
  222.  
  223.                     /*Racial combat bonus due to terrain (the faster you move the better)*/
  224.                     armbonus=0;
  225.                     armbonus+=5*(9-movecost[x+xoffset][y+yoffset]);
  226.  
  227.                     if(sct[x+xoffset][y+yoffset].altitude==MOUNTAIN)
  228.                         armbonus+=40;
  229.                     else if(sct[x+xoffset][y+yoffset].altitude==HILL)
  230.                         armbonus+=20;
  231.  
  232.                     if(sct[x+xoffset][y+yoffset].vegetation==JUNGLE)
  233.                         armbonus+=30;
  234.                     else if(sct[x+xoffset][y+yoffset].vegetation==FOREST)
  235.                         armbonus+=20;
  236.                     else if(sct[x+xoffset][y+yoffset].vegetation==WOOD)
  237.                         armbonus+=10;
  238.  
  239.                     armbonus+=fort_val(&sct[x+xoffset][y+yoffset]);
  240.  
  241.                     if(armbonus<200) mvprintw(y,2*x,"%d",armbonus/20);
  242.                     else mvaddch(y,2*x,'+');
  243.                 }
  244.                 break;
  245.             case DI_PEOP:   /*People*/
  246.                 if (sct[x+xoffset][y+yoffset].altitude==WATER)
  247.                     mvaddch(y,2*x,WATER);
  248.                 else if (sct[x+xoffset][y+yoffset].people>=9950)
  249.                     mvaddch(y,2*x,'X');
  250.                 else if (sct[x+xoffset][y+yoffset].people>=4950)
  251.                     mvaddch(y,2*x,'V');
  252.                 else if (sct[x+xoffset][y+yoffset].people>=950)
  253.                     mvaddch(y,2*x,'I');
  254.                 else
  255.                 mvprintw(y,2*x,"%d",(50+sct[x+xoffset][y+yoffset].people)/100);
  256.                 break;
  257.             case DI_GOLD:  /*Gold*/
  258.                 if (sct[x+xoffset][y+yoffset].altitude==WATER)
  259.                     mvaddch(y,2*x,WATER);
  260.                 else if(tofood( &sct[x+xoffset][y+yoffset],country)==0)
  261.                     mvaddch(y,2*x,'X');
  262.                 else if (tg_ok(country,&sct[x+xoffset][y+yoffset])){
  263.                     if (sct[x+xoffset][y+yoffset].jewels>=10)
  264.                         mvaddch(y,2*x,'+');
  265.                     else
  266.                         mvprintw(y,2*x,"%d",sct[x+xoffset][y+yoffset].jewels);
  267.                 } else mvprintw(y,2*x,"0");
  268.                 break;
  269.             case DI_METAL:  /*Metal*/
  270.                 if (sct[x+xoffset][y+yoffset].altitude==WATER)
  271.                     mvaddch(y,2*x,WATER);
  272.                 else if(tofood( &sct[x+xoffset][y+yoffset],country)==0)
  273.                     mvaddch(y,2*x,'X');
  274.                 else if (tg_ok(country,&sct[x+xoffset][y+yoffset])){
  275.                     if (sct[x+xoffset][y+yoffset].metal>=10)
  276.                         mvaddch(y,2*x,'+');
  277.                     else
  278.                         mvprintw(y,2*x,"%d",sct[x+xoffset][y+yoffset].metal);
  279.                 } else mvprintw(y,2*x,"0");
  280.                 break;
  281.             case DI_ITEMS:    /* designations needed for tradegoods */
  282.                 if (sct[x+xoffset][y+yoffset].altitude==WATER)
  283.                     mvaddch(y,2*x,WATER);
  284.                 else if(tofood( &sct[x+xoffset][y+yoffset],country)==0)
  285.                     mvaddch(y,2*x,'X');
  286.                 else if (sct[x+xoffset][y+yoffset].tradegood!=TG_none
  287.                 && (*(tg_stype+sct[x+xoffset][y+yoffset].tradegood)!='x')
  288.                 && tg_ok(country,&sct[x+xoffset][y+yoffset]))
  289.                     mvaddch(y,2*x,*(tg_stype+sct[x+xoffset][y+yoffset].tradegood));
  290.                 else mvaddch(y,2*x,'-');
  291.                 break;
  292.             default:
  293.                 break;
  294.             }
  295.         }
  296.     }
  297.     else mvaddch(y,2*x,' ');
  298.  
  299.     if( movmode==TRUE )
  300.     if(movecost[x+xoffset][y+yoffset]>=10) {
  301.         mvaddch(y,2*x+1,"+");
  302.     } else if(movecost[x+xoffset][y+yoffset]>=0) {
  303.         mvprintw(y,2*x+1,"%d",movecost[x+xoffset][y+yoffset]);
  304.     }
  305. }
  306.  
  307. /*highlight what is in xy as per highlight mode*/
  308. void
  309. highlight(x,y)
  310. {
  311.     int    armynum;
  312.     if((x<0)||(y<0)||(x>=SCREEN_X_SIZE)||(y>=SCREEN_Y_SIZE)
  313.     ||((y+yoffset)>=MAPY)||((x+xoffset)>=MAPX)) return;
  314.     standend();
  315.     switch(hilmode){
  316.     case HI_MOVE:    /* your armies w/ move left */
  317.         for(armynum=0;armynum<MAXARM;armynum++)
  318.             if(( P_ASOLD>0 )
  319.             &&( P_AMOVE>0 )
  320.             &&( P_AXLOC==(x+xoffset ))
  321.             &&( P_AYLOC==(y+yoffset ))) break;
  322.         if(armynum<MAXARM) standout();
  323.         break;
  324.     case HI_YARM:    /* your armies */
  325.         for(armynum=0;armynum<MAXARM;armynum++)
  326.             if(( P_ASOLD>0)
  327.             &&( P_AXLOC==x+xoffset)
  328.             &&( P_AYLOC==y+yoffset)) break;
  329.         if(armynum<MAXARM) standout();
  330.         break;
  331.     case HI_GOOD:    /* trade goods */
  332.         if(tg_ok( country, &sct[x+xoffset][y+yoffset])
  333.          &&(sct[x+xoffset][y+yoffset].tradegood != TG_none)
  334.          &&(magic(sct[x+xoffset][y+yoffset].owner,THE_VOID)!=TRUE
  335.             || country==0
  336.              || country==sct[x+xoffset][y+yoffset].owner
  337.             || magic(country,NINJA)==TRUE)
  338.          &&(sct[x+xoffset][y+yoffset].altitude!=WATER)) standout();
  339.         break;
  340.     case HI_OWN: /* ownership */
  341.         if(country==0) {
  342.             if(sct[x+xoffset][y+yoffset].owner!=0)
  343.                 standout();
  344.         } else if(sct[x+xoffset][y+yoffset].owner==country)
  345.             standout();
  346.         break;
  347.     case HI_ARMY: /* any armies */
  348.         if(occ[x+xoffset][y+yoffset]!=0) standout();
  349.         break;
  350.     default:
  351.         break;
  352.     }
  353. }
  354.  
  355. /* check if cursor is out of bounds*/
  356. void
  357. coffmap()
  358. {
  359.     if((xcurs<=0)||(ycurs<=0)||(xcurs>=SCREEN_X_SIZE-1)
  360.     ||((ycurs>=SCREEN_Y_SIZE-1))||((XREAL)>=MAPX)
  361.     ||((YREAL)>=MAPY)) offmap();
  362.  
  363.     if( redraw==TRUE) {
  364.         clear();
  365.         makemap();     /* update map */
  366.         makebottom();
  367.         redraw=FALSE;
  368.     }
  369.     move(ycurs,2*xcurs);
  370.     makeside(FALSE);  /*update side*/
  371.     move(ycurs,2*xcurs);
  372.     refresh();
  373. }
  374.  
  375. int
  376. canbeseen(x,y)
  377. int    x,y;
  378. {
  379.     if(!ONMAP(x,y)) return(FALSE);
  380.     return( (int) HAS_SEEN(x-xoffset,y-yoffset) );
  381. }
  382.  
  383. /** CANSEE() fills seen[SCREEN_X_SIZE][SCREEN_Y_SIZE] */
  384. void
  385. whatcansee()
  386. {
  387.     register int x,y;
  388.     int    i,j;
  389.     short    armynum,nvynum;
  390.  
  391.     if((magic(country,KNOWALL)==1)||(country==0)) {
  392.         for(x=0;x<SCREEN_X_SIZE;x++) for(y=0;y<SCREEN_Y_SIZE;y++)
  393.             HAS_SEEN(x,y)=TRUE;
  394.         return;
  395.     }
  396.     for(x=0;x<SCREEN_X_SIZE;x++) for(y=0;y<SCREEN_Y_SIZE;y++)
  397.         HAS_SEEN(x,y)=FALSE;
  398.  
  399.     for(x=(-LANDSEE);(x<SCREEN_X_SIZE+LANDSEE);x++)
  400.     for(y=(-LANDSEE);(y<SCREEN_Y_SIZE+LANDSEE);y++)
  401.     if((ONMAP(x+xoffset,y+yoffset))
  402.     &&(sct[x+xoffset][y+yoffset].owner==country)){
  403.         for(i=x-LANDSEE;i<=x+LANDSEE;i++)
  404.         for(j=y-LANDSEE;j<=y+LANDSEE;j++)
  405.         if(i>=0 && j>=0 && i<SCREEN_X_SIZE && j<SCREEN_Y_SIZE)
  406.             HAS_SEEN(i,j)=TRUE;
  407.     }
  408.  
  409.     for(nvynum=0;nvynum<MAXNAVY;nvynum++)
  410.         if((P_NMSHP!=0)||(P_NWSHP!=0)||(P_NGSHP!=0))
  411.         for(i=P_NXLOC-xoffset-NAVYSEE;i<=P_NXLOC-xoffset+NAVYSEE;i++)
  412.         for(j=P_NYLOC-yoffset-NAVYSEE;j<=P_NYLOC-yoffset+NAVYSEE;j++)
  413.         if(i>=0 && j>=0 && i<SCREEN_X_SIZE && j<SCREEN_Y_SIZE)
  414.             HAS_SEEN(i,j)=TRUE;
  415.  
  416.     for(armynum=0;armynum<MAXARM;armynum++)
  417.         if(P_ASOLD>0)
  418.         for(i=P_AXLOC-xoffset-ARMYSEE;i<=P_AXLOC-xoffset+ARMYSEE;i++)
  419.         for(j=P_AYLOC-yoffset-ARMYSEE;j<=P_AYLOC-yoffset+ARMYSEE;j++)
  420.         if(i>=0 && j>=0 && i<SCREEN_X_SIZE && j<SCREEN_Y_SIZE)
  421.             HAS_SEEN(i,j)=TRUE;
  422.  
  423.     return;
  424. }
  425.