home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume3 / trek73 / part01 / src / cmds1.c < prev    next >
C/C++ Source or Header  |  1987-12-17  |  14KB  |  643 lines

  1. #ident "@(#) TREK73 $Header: cmds1.c,v 1.1 87/10/09 11:00:47 okamoto Exp $"
  2. /*
  3.  * $Source: /ccc/okamoto/src/trek/src/RCS/cmds1.c,v $
  4.  *
  5.  * $Header: cmds1.c,v 1.1 87/10/09 11:00:47 okamoto Exp $
  6.  *
  7.  * $Log:    cmds1.c,v $
  8.  * Revision 1.1  87/10/09  11:00:47  11:00:47  okamoto (Jeff Okamoto)
  9.  * Initial revision
  10.  * 
  11.  */
  12. /*
  13.  * TREK73: cmds1.c
  14.  *
  15.  * User Commands
  16.  *
  17.  * fire_phasers, fire_tubes, lock_phasers, lock_tubes,
  18.  * turn_phasers, turn_tubes, load_tubes, launch_probe,
  19.  * probe_control
  20.  *
  21.  */
  22.  
  23. #include "externs.h"
  24.  
  25. int
  26. fire_phasers(sp) 
  27. struct ship *sp;
  28. {
  29.     char    buf1[20];
  30.     char    buf2[20];
  31.     char    c;
  32.     int    typed[MAXPHASERS];
  33.     register int i;
  34.     register int k;
  35.  
  36.     for (i=0; i < MAXPHASERS; i++)
  37.         typed[i] = 0;
  38.     printf("   fire phasers [1-%d] ", sp->num_phasers);
  39.     (void) Gets(buf1, sizeof(buf1));
  40.     if (buf1[0] == NULL) {
  41.         printf("%s:  Belay that order!\n", captain);
  42.         return 0;
  43.     }
  44.     printf("   spread [%d-%d] ", MIN_PHASER_SPREAD,
  45.         MAX_PHASER_SPREAD);
  46.     (void) Gets(buf2, sizeof(buf2));
  47.     if (buf2[0] == NULL) {
  48.         printf("%s:  Belay that order!\n", captain);
  49.         return 0;
  50.     }
  51.     i = atoi(buf2);
  52.     if (i < MIN_PHASER_SPREAD || i > MAX_PHASER_SPREAD)
  53.         return 0;
  54.     sp->p_spread = i;
  55.     if (strcmp(buf1, "all") && strcmp(buf1, "ALL"))
  56.         for (i=0; c = buf1[i]; i++) {
  57.             k = c - '1';
  58.             if (k < 0 || k > sp->num_phasers - 1)
  59.                 continue;
  60.             typed[k]++;
  61.             if (sp->phasers[k].status & (P_DAMAGED | P_FIRING))
  62.                 continue;
  63.             sp->phasers[k].status |= P_FIRING;
  64.         }
  65.     else
  66.         for (i=0; i<sp->num_phasers; i++) {
  67.             typed[i]++;
  68.             if (sp->phasers[i].status & (P_DAMAGED | P_FIRING))
  69.                 continue;
  70.             sp->phasers[i].status |= P_FIRING;
  71.         }
  72.     check_p_damage(typed, sp, "fire");    /* Type if damaged */
  73.     check_p_turn(typed, sp, 1);
  74.     return 1;
  75. }
  76.  
  77.  
  78. int
  79. fire_tubes(sp)
  80. struct ship *sp;
  81. {
  82.     char    buf1[20];
  83.     char    c;
  84.     int    typed[MAXTUBES];
  85.     register int i;
  86.     register int j;
  87.     register int k;
  88.  
  89.     for (i=0; i < MAXTUBES; i++)
  90.         typed[i] = 0;
  91.     printf("   fire tubes [1-%d] ", sp->num_tubes);
  92.     (void) Gets(buf1, sizeof(buf1));
  93.     if (buf1[0] == NULL) {
  94.         printf("%s:  Belay that order!\n", captain);
  95.         return 0;
  96.     }
  97.     if (strcmp(buf1, "all") && strcmp(buf1, "ALL"))
  98.         for (i=0; c = buf1[i]; i++) {
  99.             k = c - '1';
  100.             if (k < 0 || k > sp->num_tubes - 1)
  101.                 continue;
  102.             typed[k]++;
  103.             if (sp->tubes[k].status & (T_DAMAGED | T_FIRING))
  104.                 continue;
  105.             sp->tubes[k].status |= T_FIRING;
  106.         }
  107.     else
  108.         for (i=0; i < sp->num_tubes; i++) {
  109.             typed[i]++;
  110.             if (sp->tubes[i].status & (T_DAMAGED | T_FIRING))
  111.                 continue;
  112.             sp->tubes[i].status |= T_FIRING;
  113.         }
  114.     check_t_damage(typed, sp, "fire");    /* Type if damaged */
  115.     check_t_turn(typed, sp, 1);
  116.     j = 0;
  117.     for (i=0; i < sp->num_tubes; i++) {
  118.         if ((typed[i] == 0) || (!(sp->tubes[i].status & T_FIRING)))
  119.             continue;
  120.         if (sp->tubes[i].load == 0) {
  121.             if (!j)
  122.                 printf("Computer: Tube(s) %d", i + 1);
  123.             else
  124.                 printf(", %d", i + 1);
  125.             j++;
  126.         }
  127.     }
  128.     if (j > 1)
  129.         puts(" are not loaded.");
  130.     else if (j == 1)
  131.         puts(" is not loaded.");
  132.     return 0;
  133. }
  134.  
  135.  
  136. int
  137. lock_phasers(sp)
  138. struct ship *sp;
  139. {
  140.     char    buf1[20];
  141.     char    buf2[20];
  142.     int    typed[MAXPHASERS];
  143.     char    c;
  144.     struct    ship *ep;
  145.     register int i;
  146.     register int k;
  147.  
  148.     for (i=0; i < MAXPHASERS; i++)
  149.         typed[i] = 0;
  150.     if (is_dead(sp, S_COMP)) {
  151.         printf("%s:  Impossible %s, our computer is dead.\n",
  152.             science, title);
  153.         return 0;
  154.     }
  155.     if (!syswork(sp, S_COMP)) {
  156.         printf("%s:  Our computer is temporarily buggy",
  157.             science);
  158.         return 0;
  159.     }
  160.     printf("   lock phasers [1-%d] ", sp->num_phasers);
  161.     (void) Gets(buf1, sizeof(buf1));
  162.     if (buf1[0] == NULL)
  163.         return 0;
  164.     printf("   onto [whom] ");
  165.     (void) Gets(buf2, sizeof(buf2));
  166.     if (buf2[0] == NULL)
  167.         return 0;
  168.     ep = ship_name(buf2);
  169.     if (ep == NULL)
  170.         return 0;
  171.     if (cantsee(ep)) {
  172.         printf("%s:  %s, unable to lock phasers onto %s.\n",
  173.             nav, title, ep->name);
  174.         return 0;
  175.     }
  176.     if (strcmp(buf1, "all") && strcmp(buf1, "ALL"))
  177.         for (i=0; c = buf1[i]; i++) {
  178.             k = c - '1';
  179.             if (k < 0 || k > sp->num_phasers - 1)
  180.                 continue;
  181.             typed[k]++;
  182.             if (sp->phasers[k].status & P_DAMAGED)
  183.                 continue;
  184.             sp->phasers[k].target = ep;
  185.         }
  186.     else
  187.         for (i=0; i < sp->num_phasers; i++) {
  188.             typed[i]++;
  189.             if (sp->phasers[i].status & P_DAMAGED)
  190.                 continue;
  191.             sp->phasers[i].target = ep;
  192.         }
  193.     check_p_damage(typed, sp, "lock");
  194.     check_p_turn(typed, sp, 0);
  195.     return 1;
  196. }
  197.  
  198.  
  199. int
  200. lock_tubes(sp)
  201. struct ship *sp;
  202. {
  203.     char    buf1[20];
  204.     char    buf2[20];
  205.     int    typed[MAXTUBES];
  206.     char    c;
  207.     struct    ship *ep;
  208.     register int i;
  209.     register int k;
  210.  
  211.     for (i=0; i < sp->num_tubes; i++)
  212.         typed[i] = 0;
  213.     if (is_dead(sp, S_COMP)) {
  214.         printf("%s:  Impossible %s, our computer is dead.\n", science, title);
  215.         return 0;
  216.     }
  217.     if (!syswork(sp, S_COMP)) {
  218.         printf("%s:  Our computer is temporarily buggy", science);
  219.         return 0;
  220.     }
  221.     printf("   lock tubes [1-%d] ", sp->num_tubes);
  222.     (void) Gets(buf1, sizeof(buf1));
  223.     if (buf1[0] == NULL)
  224.         return 0;
  225.     printf("   onto whom ");
  226.     (void) Gets(buf2, sizeof(buf2));
  227.     if (buf2[0] == NULL)
  228.         return 0;
  229.     ep = ship_name(buf2);
  230.     if (ep == NULL)
  231.         return 0;
  232.     if (cantsee(ep)) {
  233.         printf ("%s:  %s, unable to lock tubes onto %s.",
  234.             nav, title, ep->name);
  235.         return 0;
  236.     }
  237.     if (strcmp(buf1, "all") && strcmp(buf1, "ALL"))
  238.         for (i=0; c = buf1[i]; i++) {
  239.             k = c - '1';
  240.             if (k < 0 || k > sp->num_tubes - 1)
  241.                 continue;
  242.             typed[k]++;
  243.             if (sp->tubes[k].status & T_DAMAGED)
  244.                 continue;
  245.             sp->tubes[k].target = ep;
  246.         }
  247.     else
  248.         for (i=0; i < sp->num_tubes; i++) {
  249.             typed[i]++;
  250.             if (sp->tubes[i].status & T_DAMAGED)
  251.                 continue;
  252.             sp->tubes[i].target = ep;
  253.         }
  254.     check_t_damage(typed, sp, "lock");
  255.     check_t_turn(typed, sp, 0);
  256.     return 1;
  257. }
  258.  
  259.  
  260. int
  261. turn_phasers(sp)
  262. struct ship *sp;
  263. {
  264.     char    buf1[20];
  265.     char    buf2[20];
  266.     char    c;
  267.     int    typed[MAXPHASERS];
  268.     register int i;
  269.     register float j;
  270.     register int k;
  271.  
  272.     for (i=0; i < MAXPHASERS; i++)
  273.         typed[i] = 0;
  274.     printf("   turn phasers [1-%d] ", sp->num_phasers);
  275.     (void) Gets(buf1, sizeof(buf1));
  276.     if (buf1[0] == NULL)
  277.         return 0;
  278.     printf("   to [0-360] ");
  279.     (void) Gets(buf2, sizeof(buf2));
  280.     if (buf2[0] == NULL)
  281.         return 0;
  282.     j = atof(buf2);
  283.     if (j < 0.0 || j > 360.0)
  284.         return 0;
  285.     if (strcmp(buf1, "all") && strcmp(buf1, "ALL"))
  286.         for (i=0; c = buf1[i]; i++) {
  287.             k = c - '1';
  288.             if (k < 0 || k > sp->num_phasers - 1)
  289.                 continue;
  290.             typed[k]++;
  291.             if (sp->phasers[k].status & P_DAMAGED)
  292.                 continue;
  293.             sp->phasers[k].target = NULL;
  294.             sp->phasers[k].bearing = j;
  295.         }
  296.     else
  297.         for (i=0; i < sp->num_phasers; i++) {
  298.             typed[i]++;
  299.             if (sp->phasers[i].status & P_DAMAGED)
  300.                 continue;
  301.             sp->phasers[i].target = NULL;
  302.             sp->phasers[i].bearing = j;
  303.         }
  304.     check_p_damage(typed, sp, "turn");
  305.     check_p_turn(typed, sp, 0);
  306.     return 1;
  307. }
  308.  
  309.  
  310. int
  311. turn_tubes(sp)
  312. struct ship *sp;
  313. {
  314.     char    buf1[20];
  315.     char    buf2[20];
  316.     char    c;
  317.     int    typed[MAXTUBES];
  318.     register int i;
  319.     register float j;
  320.     register int k;
  321.  
  322.     for (i=0; i < MAXTUBES; i++)
  323.         typed[i] = 0;
  324.     printf("   turn tubes [1-%d] ", sp->num_tubes);
  325.     (void) Gets(buf1, sizeof(buf1));
  326.     if (buf1[0] == NULL)
  327.         return 0;
  328.     printf("   to [0-360] ");
  329.     (void) Gets(buf2, sizeof(buf2));
  330.     if (buf2[0] == NULL)
  331.         return 0;
  332.     j = atof(buf2);
  333.     if (j < 0.0 || j > 360.0)
  334.         return 0;
  335.     if (strcmp(buf1, "all") && strcmp(buf1, "ALL"))
  336.         for (i=0; c = buf1[i]; i++) {
  337.             k = c - '1';
  338.             if (k < 0 || k > sp->num_tubes - 1)
  339.                 continue;
  340.             typed[k]++;
  341.             if (sp->tubes[k].status & T_DAMAGED)
  342.                 continue;
  343.             sp->tubes[k].target = NULL;
  344.             sp->tubes[k].bearing = j;
  345.         }
  346.     else
  347.         for (i=0; i < sp->num_tubes; i++) {
  348.             typed[i]++;
  349.             if (sp->tubes[i].status & T_DAMAGED)
  350.                 continue;
  351.             sp->tubes[i].target = NULL;
  352.             sp->tubes[i].bearing = j;
  353.         }
  354.     check_t_damage(typed, sp, "turn");
  355.     check_t_turn(typed, sp, 0);
  356.     return 1;
  357. }
  358.  
  359.  
  360. int
  361. load_tubes(sp)
  362. struct ship *sp;
  363. {
  364.     char    buf1[20];
  365.     char    buf2[20];
  366.     char    c;
  367.     int    load;
  368.     struct    tube *tp;
  369.     int    typed[MAXTUBES];
  370.     register int i;
  371.     register float j;
  372.     register int k;
  373.  
  374.     for (i=0; i<MAXTUBES; i++)
  375.         typed[i] = 0;
  376.     load = 0;
  377.     printf("   [load or unload] ");
  378.     (void) Gets(buf1, sizeof(buf1));
  379.     if (buf1[0] == NULL)
  380.         return 0;
  381.     if (*buf1 == 'l' || *buf1 == 'L')
  382.         load++;
  383.     else if (*buf1 != 'u' && *buf1 != 'U')
  384.         return 0;
  385.     printf("   tubes [1-%d] ", sp->num_tubes);
  386.     (void) Gets(buf2, sizeof(buf2));
  387.     if (buf2[0] == NULL)
  388.         return 0;
  389.     if (strcmp(buf2, "all") && strcmp(buf2, "ALL"))
  390.         for (i=0; c = buf2[i]; i++) {
  391.             k = c - '1';
  392.             if (k < 0 || k > sp->num_tubes - 1)
  393.                 continue;
  394.             typed[k]++;
  395.         }
  396.     else
  397.         for (i=0; i < sp->num_tubes; i++)
  398.             typed[i]++;
  399.     for (i = 0; i < sp->num_tubes; i++) {
  400.         tp = &sp->tubes[i];
  401.         if (!typed[i] || tp->status & T_DAMAGED)
  402.             continue;
  403.         if (load) {
  404.             j = min(sp->energy, MAX_TUBE_CHARGE - tp->load);
  405.             if (j <= 0)
  406.                 continue;
  407.             sp->energy -= j;
  408.             sp->pods -= j;
  409.             tp->load += j;
  410.         } else {
  411.             j = tp->load;
  412.             if (j <= 0)
  413.                 continue;
  414.             sp->energy += j;
  415.             sp->pods += j;
  416.             tp->load = 0;
  417.         }
  418.     }
  419.     printf("%s: Tubes now ", engineer);
  420.     for (i=0; i < sp->num_tubes; i++) {
  421.         if (sp->tubes[i].status & T_DAMAGED)
  422.             printf(" -- ");
  423.         else
  424.             printf(" %-2d ", sp->tubes[i].load);
  425.     }
  426.     printf(" energy at %d/%d\n", (int)sp->energy, (int)sp->pods);
  427.     return 1;
  428. }
  429.  
  430.  
  431. int
  432. launch_probe(sp)
  433. struct ship *sp;
  434. {
  435.     char    buf1[20];
  436.     int    pods, delay, prox;
  437.     float    course;
  438.     struct    ship *target;
  439.     struct    list *lp;
  440.     struct    torpedo *pp;
  441.  
  442.     pods = delay = prox = 0;
  443.     course = 0.0;
  444.     target = NULL;
  445.     if (is_dead(sp, S_PROBE)) {
  446.         printf("%s:  Probe launcher destroyed!\n", engineer);
  447.         return 0;
  448.     }
  449.     if (!syswork(sp, S_PROBE)) {
  450.         printf("%s:  Probe launcher temporarily disabled, %s\n",
  451.             engineer, title);
  452.         return 0;
  453.     }
  454.     if (sp->energy < MIN_PROBE_CHARGE) {
  455.         printf("%s: We've not enough power, Captain.\n", engineer);
  456.         return 0;
  457.     }
  458.     printf("%s: %d pods available.\n", engineer, (int)sp->energy);
  459.     printf("%s: Number to launch [%d+] is ", captain, MIN_PROBE_CHARGE);
  460.     (void) Gets(buf1, sizeof(buf1));
  461.     if (buf1[0] == NULL)
  462.         goto bad_param;
  463.     pods = atoi(buf1);
  464.     if (pods < MIN_PROBE_CHARGE || pods > sp->energy)
  465.         goto bad_param;
  466.     printf("   set time delay to [0-%d] ", MAX_PROBE_DELAY);
  467.     (void) Gets(buf1, sizeof(buf1));
  468.     if (buf1[0] == NULL)
  469.         goto bad_param;
  470.     delay = atoi(buf1);
  471.     if (delay < 0 || delay > MAX_PROBE_DELAY)
  472.         goto bad_param;
  473.     printf("   set proximity delay to [%d+] ", MIN_PROBE_PROX);
  474.     (void) Gets(buf1, sizeof(buf1));
  475.     if (buf1[0] == NULL)
  476.         goto bad_param;
  477.     prox = atoi(buf1);
  478.     if (prox < MIN_PROBE_PROX)
  479.         goto bad_param;
  480.     printf("   launch towards [whom, if anyone] ");
  481.     (void) Gets(buf1, sizeof(buf1));
  482.     if (*buf1) {
  483.         target = ship_name(buf1);
  484.         if (target == NULL)
  485.             goto bad_param;
  486.         if (cantsee(target) || !syswork(sp, S_SENSOR)) {
  487.             printf("%s:  %s, unable to lock probe onto the %s.\n",
  488.                 helmsman, title, target->name);
  489.             return 0;
  490.         }
  491.     } else {
  492.         printf("   course [0-360] ");
  493.         (void) Gets(buf1, sizeof(buf1));
  494.         if (buf1[0] == NULL)
  495.             goto bad_param;
  496.         course = atof(buf1);
  497.         if (course < 0.0 || course > 360.0)
  498.             goto bad_param;
  499.         target = NULL;
  500.     }
  501.     /*
  502.      * add a new item to the list of items in space
  503.      */
  504.     lp = newitem(I_PROBE);
  505.     lp->data.tp = MKNODE(struct torpedo, *, 1);
  506.     pp = lp->data.tp;
  507.     if (pp == (struct torpedo *)NULL) {
  508.         fprintf(stderr, "launch_probe: malloc failed\n");
  509.         exit(2);
  510.     }
  511.     pp->from = sp;
  512.     pp->fuel = pods;
  513.     pp->timedelay = delay;
  514.     pp->speed = sp->warp;
  515.     pp->newspeed = 3.0;
  516.     pp->prox = prox;
  517.     pp->target = target;
  518.     pp->course = course;
  519.     pp->x = sp->x;
  520.     pp->y = sp->y;
  521.     pp->id = new_slot();
  522.     pp->type = TP_PROBE;
  523.     /*
  524.      * subtract the pods used
  525.      */
  526.     sp->pods -= pods;
  527.     sp->energy -= pods;
  528.     sp->probe_status = PR_LAUNCHING;
  529.     printf("%s: Probe %d away\n",engineer, pp->id);
  530.     return 1;
  531. bad_param:
  532.     printf("%s: Bad parameters, %s.\n", science, title);
  533.     return 0;
  534. }
  535.  
  536.  
  537. int
  538. probe_control(sp)
  539. struct ship *sp;
  540. {
  541.     register struct list *lp;
  542.     register int i;
  543.     register float j;
  544.     register struct torpedo *pp;
  545.     struct    torpedo *probes[20];
  546.     int    probenum;
  547.     struct    ship *ep;
  548.     int    pnum;
  549.     float    bear;
  550.     int    range;
  551.     char    buf1[20];
  552.     char    *bp;
  553.  
  554.     pnum = 0;
  555.     for (lp = &head; lp != tail; lp = lp->fwd) {
  556.         if (lp->type != I_PROBE)
  557.             continue;
  558.         pp = lp->data.tp;
  559.         if (pp->from != sp)
  560.             continue;
  561.         if (!pnum)
  562.     printf("\nprobe bearng range course time  prox units target\n");
  563.         if (pnum >= sizeof probes / sizeof probes[0]) {
  564.             printf("\n%s:  There are other probes out but\n",
  565.                 nav);
  566.             puts("   these are all we can control at one time.");
  567.         }
  568.         probes[pnum] = pp;
  569.         pnum++;
  570.         range = rangefind(sp->x, pp->x, sp->y, pp->y);
  571.         bear = bearing(sp->x, pp->x, sp->y, pp->y);
  572.         if (pp->target == NULL)
  573.             bp = "NONE";
  574.         else
  575.             bp = pp->target->name;
  576.         printf(" %2d    %4.1f %5d  %4.0f  %4.1f %5d  %3d  %s\n",
  577.             pp->id, bear, range, pp->course, pp->timedelay,
  578.             pp->prox, pp->fuel, bp);
  579.     }
  580.     if (!pnum) {
  581.         printf("%s: What probes?\n", nav);
  582.         return 0;
  583.     }
  584.     printf("%s:  Detonate all probes?\n", nav);
  585.     printf("%s:  [yes or no] ", captain);
  586.     (void) Gets(buf1, sizeof(buf1));
  587.     if (buf1[0] != NULL && (*buf1 == 'Y' || *buf1 == 'y')) {
  588.         printf("%s:  Aye, %s\n", nav, title);
  589.         for (i=0; i<pnum; i++)
  590.             probes[i]->timedelay = 0.0;
  591.         sp->probe_status = PR_DETONATE;
  592.         return 1;
  593.     }
  594.     printf("   control probe [#] ");
  595.     (void) Gets(buf1, sizeof(buf1));
  596.     if (buf1[0] == NULL)
  597.         return 0;
  598.     probenum = atoi(buf1);
  599.     for (i=0; i < pnum; i++)
  600.         if (probes[i]->id == probenum)
  601.             break;
  602.     if (i == pnum)
  603.         return 0;
  604.     probenum = i;
  605.     printf("%s:  Detonate it?\n", nav);
  606.     printf("%s:  [yes or no] ", captain);
  607.     (void) Gets(buf1, sizeof(buf1));
  608.     if (buf1[0] != NULL && (*buf1 == 'y' || *buf1 == 'Y')) {
  609.         probes[probenum]->timedelay = 0.;
  610.         sp->probe_status = PR_DETONATE;
  611.         return 1;
  612.     }
  613.     printf("   lock it onto [whom, if anyone] ");
  614.     (void) Gets(buf1, sizeof(buf1));
  615.     if (buf1[0] != NULL) {
  616.         ep = ship_name(buf1);
  617.         if (ep != NULL) {
  618.             sp->probe_status = PR_LOCK;
  619.             if (cansee(ep) && syswork(sp, S_SENSOR)) {
  620.                 probes[probenum]->target = ep;
  621.                 printf("%s: locking.\n", nav);
  622.                 return 1;
  623.             } else {
  624.                 printf("%s:  %s, unable to lock probe on the %s.\n",
  625.                     helmsman, title, ep->name);
  626.                 return 0;
  627.             }
  628.         }
  629.     }
  630.     printf("   set it to course [0-360] ");
  631.     (void) Gets(buf1, sizeof(buf1));
  632.     if (buf1[0] == NULL)
  633.         return 0;
  634.     sp->probe_status = PR_LOCK;
  635.     j = atof(buf1);
  636.     if (j < 0.0 || j > 360.0)
  637.         return 0;
  638.     probes[probenum]->course = j;
  639.     probes[probenum]->target = NULL;
  640.     printf("%s: setting in new course.\n", nav);
  641.     return 1;
  642. }
  643.