home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 7 Games / 07-Games.zip / VTREK.ZIP / SUB2.C < prev    next >
C/C++ Source or Header  |  1989-12-26  |  7KB  |  320 lines

  1. /* sub2.c -- subroutines for visual star trek */
  2.  
  3. #include "vtrek.h"
  4.  
  5. /* klingon movement and firing */
  6. klingmove()
  7. {
  8.     int k, dx, dy, dam, kx, ky, nx, ny, i, j;
  9.     int minj, mind, d, sk;
  10.     static int dirx[9] = {1, 1, 0, -1, -1, -1, 0, 1, 0};
  11.     static int diry[9] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
  12.     double dist;
  13.     char str[40];
  14.  
  15.     for (k = 0; k < 3; k++) {
  16.         if (klingon[k].sh < 0)
  17.         continue;
  18.         kx = klingon[k].xs;
  19.         ky = klingon[k].ys;
  20.  
  21.         /* movement */
  22.         if (rnd(100) < 30 + skill * 5) {
  23.         for (i = 0; i < numkmove; i++) {
  24.             mind = 999;
  25.             minj = -1;
  26.             for (j = 0; j < 9; j++) {
  27.             nx = kx + dirx[j];
  28.             ny = ky + diry[j];
  29.             if (nx < 0 || nx > 7 || ny < 0 || ny > 7)
  30.                 continue;
  31.             if (quadrant[nx][ny] != EMPTY)
  32.                 continue;
  33.             dx = xsect - nx;
  34.             dy = ysect - ny;
  35.             d = dx * dx + dy * dy;
  36.             if (d < mind) {
  37.                 mind = d;
  38.                 minj = j;
  39.             }
  40.             }
  41.  
  42.             if (minj < 0)
  43.             break;
  44.  
  45.             nx = kx + dirx[minj];
  46.             ny = ky + diry[minj];
  47.             quadrant[kx][ky] = EMPTY;
  48.             quadrant[nx][ny] = KLINGON;
  49.             plt_srs(ELEMENT, nx, ny);
  50.             plt_srs(ELEMENT, kx, ky);
  51.             kx = nx;
  52.             ky = ny;
  53.         }
  54.         klingon[k].xs = kx;
  55.         klingon[k].ys = ky;
  56.         }
  57.  
  58.         /* fire disrupters */
  59.         dx = kx - xsect;
  60.         dy = ky - ysect;
  61.         dist = sqrt((double)(dx * dx) + (double)(dy * dy));
  62.         dam = (int)((rnd(100) + 100 + skill * 20) / dist);
  63.         if (damage[SRS] > 0)
  64.             sprintf(str, "Klingon at [%d,%d] hits with %d units.", kx+1,
  65.         ky+1, dam);
  66.         else
  67.         sprintf(str, "Klingon hits with %d units.", dam);
  68.         readout(ADDLINE, str);
  69.         if (condition == DOCKED)
  70.         readout(ADDLINE, "Starbase shields protect you!");
  71.         else {
  72.         if ((shields -= dam) < 0)
  73.             die();
  74.         sk = skill * 250;
  75.         if (shields <= 500 + sk)
  76.             fixdev(REL, RND, -dam / 2);
  77.         else if (shields < 1500 + sk)
  78.             fixdev(REL, RND, -(1500 + sk - shields) * dam / 2000);
  79.         }
  80.     }
  81.  
  82.     plt_stat(ELEMENT, SHIELDS);
  83. }
  84.  
  85. /* fire phasers */
  86. phasers()
  87. {
  88.     int dam, k, dx, dy, e, kx, ky;
  89.     char str[40];
  90.     double dist;
  91.  
  92.     if (damage[PHASER] < 0)
  93.         readout(ADDLINE, "Phasers are damaged.");
  94.     else {
  95.         for (k = 0; k < 3; k++) {
  96.         if (klingon[k].sh < 0)
  97.             continue;
  98.         kx = klingon[k].xs;
  99.         ky = klingon[k].ys;
  100.  
  101.         if (damage[SRS] > 0) {
  102.             sprintf(str, "Units aimed at [%d,%d] : ", kx + 1, ky + 1);
  103.             prompt(str);
  104.         }
  105.         else
  106.             prompt("Units aimed at [?,?] : ");
  107.         e = getnumb();
  108.         dx = kx - xsect;
  109.         dy = ky - ysect;
  110.         dist = sqrt((double)(dx * dx) + (double)(dy * dy));
  111.         dam = (int)(e / dist);
  112.  
  113.         if (energy < e)
  114.             readout(ADDLINE, "Insufficient energy for command.");
  115.         else {
  116.             energy -= e;
  117.             damkling(kx, ky, dam);
  118.             plt_stat(ELEMENT, ENERGY);
  119.         }
  120.         }
  121.     }
  122. }
  123.  
  124. /* set condition */
  125. setcondition()
  126. {
  127.     int dx, dy, i, oldcond;
  128.     int plotgal, plotsrs;
  129.  
  130.     oldcond = condition;
  131.  
  132.     if (galaxy[xquad][yquad].nkling)
  133.         condition = RED;
  134.     else if (shields < 100) {
  135.         condition = YELLOW;
  136.         if (oldcond != DOCKED)
  137.             readout(ADDLINE, "Captain, shields are dangerously low.");
  138.     }
  139.     else
  140.         condition = GREEN;
  141.  
  142.     if (galaxy[xquad][yquad].nbase) {
  143.         dx = xsect - base_xsect;
  144.         dy = ysect - base_ysect;
  145.         if (abs(dx) <= 1 && abs(dy) <= 1) {
  146.         condition = DOCKED;
  147.         if (oldcond != DOCKED) {
  148.             energy = 3000;
  149.             torps = 10;
  150.             shields = 0;
  151.             plotsrs = damage[SRS] <= 0;
  152.             plotgal = damage[COMPUTER] <= 0;
  153.             for (i = 0; i < 8; i++)
  154.                 damage[i] = 100;
  155.             readout(ADDLINE, "Shields lowered for docking.");
  156.             playership[0] = playership[2] = ' ';
  157.             plt_srs(ELEMENT, xsect, ysect);
  158.             plt_stat(INFO);
  159.             plt_dam(INFO);
  160.             if (plotsrs)
  161.             plt_srs(INFO);
  162.             if (plotgal)
  163.                 plt_gal(INFO);
  164.             oldcond = DOCKED;
  165.         }
  166.         }
  167.     }
  168.  
  169.     if (oldcond != condition)
  170.         plt_stat(ELEMENT, CONDITION);
  171. }
  172.  
  173. /* move torp to a sector and see if it hit */
  174. mvtorp(x, y)
  175. int x, y;
  176. {
  177.     int ch, status = ALIVE;
  178.     char str[40];
  179.  
  180.     ch = quadrant[x][y];
  181.     quadrant[x][y] = TBLAST;
  182.     plt_srs(ELEMENT, x, y);
  183.  
  184.     switch (ch) {
  185.     case EMPTY :
  186.         break;
  187.     case KLINGON :
  188.         damkling(x, y, AUTOKILL);
  189.         status = DEAD;
  190.         break;
  191.     case STARBASE :
  192.         readout(ADDLINE, "Starbase destroyed.");
  193.         galaxy[xquad][yquad].nbase--;
  194.         numbases--;
  195.         plt_num(INFO);
  196.         status = DEAD;
  197.         break;
  198.     case STAR :
  199.         sprintf(str, "Star at [%d,%d] has gone supernova.", x + 1, y + 1);
  200.         readout(ADDLINE, str);
  201.         galaxy[xquad][yquad].nstar--;
  202.         status = DEAD;
  203.         break;
  204.     }
  205.  
  206.     if (status == DEAD)
  207.         ch = EMPTY;
  208.     quadrant[x][y] = ch;
  209.     plt_srs(ELEMENT, x, y);
  210.  
  211.     return status;
  212. }
  213.  
  214. /* fire a torpedo */
  215. torpedo()
  216. {
  217.     int dir, i, ch, x, y;
  218.     int dx, dy;
  219.     double th;
  220.     static char dirstr[] = "DEWQAZXC";
  221.  
  222.     if (damage[TUBES] < 0)
  223.         readout(ADDLINE, "Photon torpedo tubes are damaged.");
  224.     else if (torps < 1)
  225.         readout(ADDLINE, "You're out of photon torpedos!");
  226.     else {
  227.         torps--;
  228.         plt_stat(ELEMENT, TORPS);
  229.         prompt("Direction ? ");
  230.         dir = getch();
  231.         if (isdigit(dir)) {
  232.         putch(dir);
  233.         dir -= '0';
  234.         while ((ch = getch()) != '\r') {
  235.             putch(ch);
  236.             if (isdigit(ch))
  237.             dir = dir * 10 + ch - '0';
  238.             else
  239.             break;
  240.         }
  241.         }
  242.         else {
  243.         ch = Toupper(dir);
  244.         dir = 0;
  245.         for (i = 0; i < 8; i++) {
  246.             if (ch == dirstr[i])
  247.             break;
  248.             dir += 45;
  249.         }
  250.         if (i >= 8) {
  251.             readout(ADDLINE, "Illegal direction.");
  252.             return;
  253.         }
  254.         }
  255.  
  256.         th = dir / 180.0 * PI;
  257.         if (dir >= 315 || dir <= 45 || (dir >= 135 && dir <= 225)) {
  258.         dx = (dir >= 315 || dir <= 45) ? 1 : -1;
  259.         for (x = xsect + dx; x >= 0 && x <= 7; x += dx) {
  260.             y = (int)(ysect - (x - xsect) * tan(th) + 0.5);
  261.             if (y < 0 || y > 7)
  262.             break;
  263.             if (mvtorp(x, y) == DEAD) {
  264.             break;
  265.             }
  266.         }
  267.         }
  268.         else {
  269.         th -= PI / 2.0;
  270.         dy = (dir >= 45 && dir <= 135) ? -1 : 1;
  271.         for (y = ysect + dy; y >= 0 && y <= 7; y += dy) {
  272.             x = (int)(xsect + (y - ysect) * tan(th) + 0.5);
  273.             if (x < 0 || x > 7)
  274.             break;
  275.             if (mvtorp(x, y) == DEAD)
  276.             break;
  277.         }
  278.         }
  279.     }
  280. }
  281.  
  282. /* repair devices somewhat */
  283. repdevices()
  284. {
  285.     int i;
  286.  
  287.     for (i = 0; i < 8; i++)
  288.         fixdev(REL, i, 5);
  289. }
  290.  
  291. /* out of star dates */
  292. timeout()
  293. {
  294.     char str[44];
  295.  
  296.     readout(CLEAR);
  297.     readout(ADDLINE, "You have run out of stardates and there");
  298.     sprintf(str, "are still %d Klingons left.  Some captain you", numkling);
  299.     readout(ADDLINE, str);
  300.     readout(ADDLINE, "are.");
  301.     die();
  302. }
  303.  
  304. /* out of energy */
  305. dead()
  306. {
  307.     readout(CLEAR);
  308.     readout(ADDLINE, "Your ship is dead in space.  Eventually,");
  309.     readout(ADDLINE, "Klingons show up and relieve you of your");
  310.     readout(ADDLINE, "task . . .");
  311.     die();
  312. }
  313.  
  314. /* reset terminal and exit program */
  315. vexit(status)
  316. {
  317.     termreset();
  318.     exit(status);
  319. }
  320.