home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume6 / lander / screen.c < prev   
C/C++ Source or Header  |  1989-07-06  |  6KB  |  285 lines

  1. #include <signal.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <curses.h>
  5. #include "consts.h"
  6. #include "funcs.h"
  7.  
  8. #define ALT_ADD(screen, y, x, ch) \
  9.     wmove(screen, y, x); \
  10.     waddch(screen, ch);
  11. #define STAND_ADD(screen, y, x, ch) \
  12.     wmove(screen, y, x); \
  13.     wstandout(screen); \
  14.     waddch(screen, ch); \
  15.     wstandend(screen);
  16. #define STAND_ALT_ADD(screen, y, x, ch) \
  17.     wstandout(screen); \
  18.     ALT_ADD(screen, y, x, ch); \
  19.     wstandend(screen);
  20.  
  21. char *Template[] = {
  22. "                                                                            ",
  23. ".                                                                          /",
  24. "<                                                                        /v+",
  25. "<                                                                       /+++",
  26. "<                                                                     /v++++",
  27. "<                            /====.                                   >+++++",
  28. "<                            `v30v<                                  /++++++",
  29. "<                             >++++.                                 >++++++",
  30. "<                             >++++<                                /+++++++",
  31. "<                             >++++<                               /++++++++",
  32. "+.                            >+++++.                              `++++++++",
  33. "+<                           /++++++<                               `+++++++",
  34. "+<                          /+++++++<                                >++++++",
  35. "+<                          >+++++++<                          /v.   >++++++",
  36. "+<                          >+++++++<                         /+++.  >++++++",
  37. "+<                          >+++++++'                         >+++<  >++++++",
  38. "++====.                /====+++++++<                          >+++<  >++++++",
  39. "++v30v<            /vvv+v15v+++++++<          /v.           /v++++<  >++++++",
  40. "+++++++v.       /vv++++++++++++++++<        /v+++====.      >++++++==+++++++",
  41. "+++++++++=======+++++++++++++++++++<      /v+++++v20v+======+++++++50+++++++",
  42. "+++++++++vv10vvv++++++++++++++++++++======++++++++++++vv15vv++++++++++++++++",
  43. "++++++++++++++++++++++++++++++++++++vv25vv++++++++++++++++++++++++++++++++++"};
  44.  
  45. int PadScore[MAX_PADS] = {30, 30, 15, 20, 50, 10, 15, 25};
  46. int LastLegalY, LastLegalX;
  47.  
  48. #ifdef BSD
  49. typedef char chtype_port_t;
  50. #else
  51. typedef chtype chtype_port_t;
  52. #endif
  53.  
  54. static chtype_port_t LineMap[128] = {0};
  55. static int Old_Y, Old_X;
  56.  
  57. void Introduction();
  58. void dumpcore();
  59. void zap();
  60.  
  61. void InitialiseScreen(init_scr)
  62.  
  63. WINDOW **init_scr;
  64.  
  65.     {
  66.     register WINDOW *screen;
  67.  
  68.     signal(SIGINT, zap);
  69.     signal(SIGQUIT, dumpcore);
  70.     initscr();
  71.     noecho();
  72.     cbreak();
  73.     *init_scr = stdscr;
  74.     screen = *init_scr;
  75. #ifdef BSD
  76.     LineMap['\''] = '+';
  77.     LineMap['.'] = '+';
  78.     LineMap['|'] = '|';
  79.     LineMap['-'] = '-';
  80.     LineMap['/'] = '+';
  81.     LineMap['`'] = '+';
  82.     LineMap['+'] = '+';
  83.     LineMap['>'] = '+';
  84.     LineMap['<'] = '+';
  85.     LineMap['^'] = '+';
  86.     LineMap['v'] = '+';
  87.     LineMap[PAD] = '=';
  88. #else
  89.     LineMap['\''] = ACS_LRCORNER;
  90.     LineMap['.'] = ACS_URCORNER;
  91.     LineMap['|'] = ACS_VLINE;
  92.     LineMap['-'] = ACS_HLINE;
  93.     LineMap['/'] = ACS_ULCORNER;
  94.     LineMap['`'] = ACS_LLCORNER;
  95.     LineMap['+'] = ACS_PLUS;
  96.     LineMap['>'] = ACS_LTEE;
  97.     LineMap['<'] = ACS_RTEE;
  98.     LineMap['^'] = ACS_BTEE;
  99.     LineMap['v'] = ACS_TTEE;
  100.     LineMap[PAD] = ACS_HLINE;
  101. #endif
  102.     Introduction(screen);
  103.     }
  104.  
  105. void DrawScreen(screen)
  106.  
  107. WINDOW *screen;
  108.  
  109.     {
  110.     register int i, j, scr_i;
  111.     chtype_port_t map;
  112.     char *line, ch;
  113.  
  114.     werase(screen);
  115.     for (i = 0; i < SCR_Y; ++i)
  116.         {
  117.         line = Template[i];
  118.         scr_i = i + 1;
  119.         for (j = 0; j < SCR_X; ++j)
  120.             {
  121.             ch = *(line + j);
  122.             map = LineMap[ch];
  123.             if (map)
  124.                 if (ch == PAD)
  125.                     {
  126.                     STAND_ALT_ADD(screen, scr_i, j, map);
  127.                     }
  128.                 else
  129.                     {
  130.                     ALT_ADD(screen, scr_i, j, map);
  131.                     }
  132.             else
  133.                 if (ch == PAD)
  134.                     {
  135.                     STAND_ADD(screen, scr_i, j, ch);
  136.                     }
  137.                 else
  138.                     mvwaddch(screen, scr_i, j, ch);
  139.             }
  140.         }
  141.     wrefresh(screen);
  142.     LastLegalY = SCR_ADJ(0);
  143.     LastLegalX = 0;
  144.     Old_Y = Old_X = -1;
  145.     }
  146.  
  147. int MoveLander(screen, land_y, land_x)
  148.  
  149. WINDOW *screen;
  150. double land_y, land_x;
  151.  
  152.     {
  153.     int y, x, touchup = 0, screen_y, new_legal;
  154.     double y_real, x_real;
  155.     char ch;
  156.  
  157.     y_real = (ALTITUDE_INIT - land_y) / ALTITUDE_INIT * (double) SCR_Y;
  158.     x_real = land_x / LANDSCAPE_WIDTH * (double) SCR_X;
  159.     y = y_real + 0.5;
  160.     x = x_real + 0.5;
  161.     new_legal = LEGAL_YX(y, x);
  162.     if (y != Old_Y || x != Old_X)
  163.         {
  164.         if (LEGAL_YX(Old_Y, Old_X))
  165.             {
  166.             mvwaddch(screen, SCR_ADJ(Old_Y), Old_X, '.');
  167.             touchup = 1;
  168.             }
  169.         if (new_legal)
  170.             {
  171.             screen_y = SCR_ADJ(y);
  172. #ifdef BSD
  173.             ALT_ADD(screen, screen_y, x, '$');
  174. #else
  175.             ALT_ADD(screen, screen_y, x, ACS_TTEE);
  176. #endif
  177.             wmove(screen, screen_y, x);
  178.             LastLegalY = y;
  179.             LastLegalX = x;
  180.             touchup = 1;
  181.             }
  182.         }
  183.     if (touchup)
  184.         wrefresh(screen);
  185.     Old_Y = y;
  186.     Old_X = x;
  187.     if (new_legal)
  188.         {
  189.         ch = *(Template[y] + x);
  190.         if (ch == PAD)
  191.             return LANDED;
  192.         if (ch != ' ')
  193.             return CRASH;
  194.         }
  195.     else
  196.         return CRASH;
  197.     return FLYING;
  198.     }
  199.  
  200. static void zap()
  201.  
  202.     {
  203.     endwin();
  204.     fcntl(fileno(stdin), F_SETFL, 0);
  205.     exit(1);
  206.     }
  207.  
  208. static void dumpcore()
  209.  
  210.     {
  211.     fcntl(fileno(stdin), F_SETFL, 0);
  212.     endwin();
  213.     signal(SIGQUIT, SIG_DFL);
  214.     kill(getpid(), SIGQUIT);
  215.     pause();
  216.     }
  217.  
  218. #define L0 "Lunar Lander"
  219. #define L1 "The classic arcade game comes to Unix."
  220. #define L2 "Controls:"
  221. #define L3 "'0'-'9' - power level"
  222. #define L4 "'z' - set left retro rocket to power level"
  223. #define L5 "'x' - set vertical thrust to power level"
  224. #define L6 "'c' - set right retro to power level"
  225. #define L7 "--press space to start game--"
  226. #define L8 "Stacey Campbell at HCR, 1989"
  227. #define CENTRE(win, line, str) mvwaddstr(win, line, (COLS - sizeof(str)) / 2, \
  228.     str)
  229.  
  230. static void Introduction(screen)
  231.  
  232. WINDOW *screen;
  233.  
  234.     {
  235.     werase(screen);
  236.     CENTRE(screen, 2, L0);
  237.     CENTRE(screen, 5, L1);
  238.     CENTRE(screen, 9, L2);
  239.     CENTRE(screen, 11, L3);
  240.     CENTRE(screen, 12, L4);
  241.     CENTRE(screen, 13, L5);
  242.     CENTRE(screen, 14, L6);
  243.     CENTRE(screen, 18, L8);
  244.     CENTRE(screen, 20, L7);
  245. #ifdef BSD
  246.     wrefresh(screen);
  247. #endif
  248.     while (wgetch(screen) != ' ');
  249.     }
  250.  
  251. #ifdef BSD
  252.  
  253. int flash()
  254.  
  255.     {
  256.     putchar(7);
  257.     }
  258.  
  259. #ifndef FNDELAY
  260. #define FNDELAY O_NDELAY
  261. #endif
  262.  
  263. int nodelay(win, flag)
  264.  
  265. WINDOW *win;
  266. int flag;
  267.  
  268.     {
  269.     int res;
  270.  
  271.     res = fcntl(fileno(stdin), F_GETFL, 0);
  272.     if (flag)
  273.         {
  274.         res |=  FNDELAY;
  275.         fcntl(fileno(stdin), F_SETFL, res);
  276.         }
  277.     else
  278.         {
  279.         res &= ~FNDELAY;
  280.         fcntl(fileno(stdin), F_SETFL, res);
  281.         }
  282.     return 0;
  283.     }
  284. #endif
  285.