home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume6 / gb / part03 / moveship.c < prev    next >
C/C++ Source or Header  |  1989-07-06  |  9KB  |  278 lines

  1. /*
  2.  * Galactic Bloodshed (Robert Chansky, smq@b)
  3.  *  moveship -- moves specified ship according to its orders.
  4.  *    also deducts fuel from the ship's stores.
  5.  */
  6.  
  7.  
  8. #include "vars.h"
  9. #include "ships.h"
  10. #include "shipdata.h"
  11. #include <math.h>
  12. extern char telegram_buf[AUTO_TELEG_SIZE];
  13.   /* amount to move for each dir level. I arrived on these #'s only after
  14.     hours of dilligent tweaking */
  15.   /* amount to move for each directory level  */
  16. float MoveConsts[] = { 3500.0, 300.0, 13.0 };
  17.   /* amnt to move for each ship speed level (ordered) */
  18. float SpeedConsts[] = { 0.0, 0.80, 1.0, 1.2, 1.3 };
  19.   /* amount of fuel it costs to move at speed level */
  20. float FuelConsts[] =  { 0.0, 0.75, 1.0, 1.3, 1.4 };
  21.  
  22. Moveship(shipno,s,ost,dst,opl,dpl,dsh/*,sdatamod,stomod,stdmod,plomod,pldmod*/)
  23. int shipno;
  24. shiptype *s,*dsh;
  25. startype *ost,*dst;
  26. planettype *opl,*dpl;
  27. /*bool *sdatamod,*stomod,*stdmod,*plomod,*pldmod;*/
  28. {
  29.  double heading,stardist,Ddist,dist,xdest,ydest,mfactor;
  30.  char buf[100];
  31.  float oldxpos,oldypos,fuse;
  32.  register int i;
  33.  bool move_err = 0;
  34.  
  35.  /*sdatamod = *stomod = *stdmod = *plomod = *pldmod = 0;*/
  36.  
  37.  printf("moving ship #%d\n",shipno);
  38.  
  39.  if (can_move(s) && !s->is_docked && !s->is_dead && s->whatdest!=LEVEL_UNIV) {
  40.  
  41.     /* subtract fuel from the ship */
  42.    fuse = FuelConsts[s->speed] * FUEL_USE;
  43.    if (s->type == STYPE_POD)
  44.     fuse *= 0.4;
  45.  
  46.    if (s->fuel < fuse) {
  47.       /* ship is out of fuel; do whatever it is to do */
  48.     if (!s->notified) {
  49.       s->notified = 1;
  50.       teleg_add("",telegram_buf);    /* clear telegram buffer */
  51.       sprintf(buf,"Telecomm from %s #%d\n\n",Shipnames[s->type],shipno);
  52.       teleg_add(buf,telegram_buf);
  53.       sprintf(buf,"%s #%d is out of fuel at ", Shipnames[s->type],shipno,
  54.                 prin_ship_orbits(s));
  55.       teleg_add(buf,telegram_buf);
  56.       teleg_send(TELEG_PLAYER_AUTO, s->owner, telegram_buf);
  57.     }
  58.     return;
  59.    }
  60.  
  61. /*******   move the ship towards dest ******/
  62.  
  63.    oldxpos = s->xpos;    /* used in case of inablility to insert into data */
  64.    oldypos = s->ypos;
  65.  
  66.    switch (s->whatdest) {
  67.       case LEVEL_STAR:
  68.      xdest = dst->xpos;
  69.      ydest = dst->ypos;
  70.      break;
  71.       case LEVEL_PLAN:
  72.     /* dpl only defined & referenced if dest is a planet */
  73.      xdest = dst->xpos + dpl->xpos;
  74.      ydest = dst->ypos + dpl->ypos;
  75.      break;
  76.       case LEVEL_SHIP:
  77.      xdest = dsh->xpos;
  78.      ydest = dsh->ypos;
  79.    }
  80.  
  81. /*  update new xpos,ypos */
  82.    heading = atan2( xdest-s->xpos, (ydest - s->ypos)==0.0?0.000001:(ydest-s->ypos) );
  83.    mfactor = SpeedConsts[s->speed] * MoveConsts[s->whatorbits] 
  84.         / (logscale((int)s->mass * 10) );
  85.    if (s->type == STYPE_POD)
  86.     mfactor *= 0.4;
  87.  
  88.     /* keep from ending up in the middle of the system */
  89.    Ddist = sqrt( Distsq(s->xpos, s->ypos, xdest, ydest));
  90.    if (s->whatdest==LEVEL_UNIV)
  91.        Ddist -= (SYSTEMSIZE - 1.0);
  92.    else if (s->whatdest==LEVEL_STAR)
  93.     Ddist -= PLORBITSIZE - 1.0;
  94.    else if (s->whatdest==LEVEL_SHIP && Ddist > SYSTEMSIZE) {
  95.      if (!s->notified) {
  96.     s->notified = 1;
  97.     teleg_add("",telegram_buf);
  98.        sprintf(buf,"Telecomm from ship #%d at %s\n\n",shipno, 
  99.                         prin_ship_orbits(s));
  100.        teleg_add(buf,telegram_buf);
  101.        sprintf(buf,"%s #%d cannot find destination ship #%d.\n", 
  102.                 Shipnames[s->type],shipno,s->destshipno);
  103.        teleg_add(buf,telegram_buf);
  104.        teleg_send(TELEG_PLAYER_AUTO, s->owner, telegram_buf);
  105.     return;
  106.      }
  107.    }
  108.  
  109.    if (Ddist <= 0.0)
  110.     Ddist = 0.0;
  111.  
  112.    else if (Ddist > DIST_TO_LAND) {
  113.        s->fuel -= fuse;
  114.        printf(" subtracting %f fuel \n",fuse);
  115.             /* subtract the fuel's mass */
  116.        s->mass -= fuse * MASS_FUEL;
  117.  
  118.         /* dont overshoot */
  119.        xdest = sin(heading) * mfactor;
  120.         if (abs(xdest) > Ddist) 
  121.         xdest = sgn(xdest) * Ddist;
  122.        ydest = cos(heading) * mfactor;
  123.         if (abs(ydest) > Ddist) 
  124.         ydest = sgn(ydest) * Ddist;
  125.        s->xpos += xdest;
  126.        s->ypos += ydest;
  127.    }
  128.  
  129. /*****  check if far enough away from object it's orbiting to break orbit *****/
  130.    if (s->whatorbits==LEVEL_PLAN) {
  131.      printf("dist from orbts planet is %lf\n",dist = sqrt( Distsq(s->xpos, s->ypos, 
  132.         ost->xpos+opl->xpos, ost->ypos+opl->ypos ) ) );
  133.      if (dist > PLORBITSIZE) {
  134.         s->whatorbits = LEVEL_STAR;
  135.                 /* insert ship to star data */
  136.         for (i=0; i<MAXSSHIPS && ost->shipnums[i]; i++) ;
  137.         if (i<MAXSSHIPS) {
  138.             ost->shipnums[i] = shipno;
  139.             ost->numships++;
  140.             /**stomod = 1;*/
  141.             printf("ship #%d now in system %s slot %d.\n",shipno,ost->name,i);
  142.                 /* delete ship from planet list */
  143.             for (i=0; i<MAXPSHIPS && opl->shipnums[i]!=shipno; i++) ;
  144.             if (i<MAXPSHIPS) {
  145.                        opl->shipnums[i]=0;
  146.                        opl->numships--;
  147.                        /*plomod = 1;*/
  148.                         printf("ship #%d departed from planet /%s/%s.\n",shipno, ost->name, ost->pnames[s->pnumorbits]);
  149.             } else {move_err=1;printf("WARNING#1.. ship %d not found,pos%d.\n",shipno,i);}
  150.         } else {
  151.            printf("WARNING#2..ship %d unable to insert.\n",shipno);
  152.            move_err = 1;
  153.            s->xpos = oldxpos;    /* cant insert, instead leave ship */
  154.            s->ypos = oldypos;    /*  where it was.. */
  155.         }
  156.      }
  157.    } else if (s->whatorbits==LEVEL_STAR) {
  158.      printf("star orbits dist is %lf\n",dist = sqrt( Distsq(s->xpos, 
  159.         s->ypos, ost->xpos, ost->ypos ) ) );
  160.      if (dist > SYSTEMSIZE) {
  161.         s->whatorbits = LEVEL_UNIV;
  162.          /* insert ship into universe data */
  163.         for (i=0; i<MAXUSHIPS && Sdata.shipnums[i]; i++) ;
  164.         if (i<MAXUSHIPS) {
  165.            Sdata.shipnums[i] = shipno;
  166.            Sdata.numships++;
  167.            /*sdatamod = 1;    /* alert client program */
  168.             printf("ship #%d departed from system %s.\n",shipno, ost->name);
  169.          /* delete ship from star data */
  170.            for (i=0; i<MAXSSHIPS && ost->shipnums[i]!=shipno; i++) ;
  171.            if (i<MAXSSHIPS) {
  172.                ost->shipnums[i]=0;
  173.                ost->numships--;
  174.                /*stomod = 1;*/
  175.            } else {move_err=1;printf("WARNING#3.. ship %d not found,pos %d.\n",shipno,i);}
  176.         } else {    /* leave ship back where it was.. */
  177.            printf("WARNING#4:cant insert into universe.\n");
  178.            move_err = 1;
  179.            s->xpos = oldxpos;
  180.            s->ypos = oldypos;
  181.         }
  182.      }
  183.    }
  184.  
  185. /*******   check for arriving at destination *******/
  186.    if ( s->whatorbits==LEVEL_UNIV && (s->whatdest==LEVEL_STAR || s->whatdest==LEVEL_PLAN) ) {
  187.         /* dist to star not planet */
  188.      stardist = (s->whatdest==LEVEL_PLAN) ? sqrt(Distsq(s->xpos,s->ypos,Stars[s->deststar]->xpos,Stars[s->deststar]->ypos)) : Ddist;
  189.      printf("dist from dest syst is %lf\n",stardist);
  190.      if (stardist <= SYSTEMSIZE) {
  191.        s->whatorbits = LEVEL_STAR;
  192.        s->storbits = s->deststar;
  193.        if (s->type == STYPE_POD)
  194.         s->notified = 1;    /* signal to explode */
  195.             /* clear orders if the ship is not headed to a planet in
  196.                this system */
  197.          /* insert the ship */
  198.        for (i=0; i<MAXSSHIPS && dst->shipnums[i]; i++) ;
  199.        if (i<MAXSSHIPS) {
  200.         dst->shipnums[i] = shipno;
  201.         dst->numships++;
  202.         /**stdmod = 1;*/
  203.          /* mark as explored by that player */
  204.         setbit(dst->explored, s->owner);
  205.         printf(" ship #%d arrived at system %s.\n",shipno,dst->name);
  206.            if (s->whatdest == LEVEL_STAR) {
  207.              s->whatdest = LEVEL_UNIV;
  208.           teleg_add("",telegram_buf);
  209.           sprintf(buf,"Telecomm from ship #%d\n\n",shipno);
  210.           teleg_add(buf,telegram_buf);
  211.           sprintf(buf,"%s #%d arrived at system %s.\n",
  212.                Shipnames[s->type], shipno, prin_ship_orbits(s));
  213.           teleg_add(buf,telegram_buf);
  214.           teleg_send(TELEG_PLAYER_AUTO, s->owner, telegram_buf);
  215.         }
  216.  
  217.            /* delete ship from Sdata */
  218.             for (i=0; i<MAXUSHIPS && Sdata.shipnums[i]!=shipno; i++) ;
  219.             if (i<MAXUSHIPS) {
  220.              Sdata.shipnums[i] = 0;
  221.              Sdata.numships--;
  222.              /**sdatamod = 1;*/
  223.             } else printf("can't delete ship #%d from univ.data\n",shipno);
  224.        } else {
  225.            printf("ship #%d cannot be inserted into star %d!\n",shipno,s->deststar);
  226.            move_err = 1;
  227.            s->xpos = oldxpos;
  228.            s->ypos = oldypos;
  229.        }
  230.      }
  231.    } else if ( (s->whatdest==LEVEL_PLAN) && (s->deststar==s->storbits)  &&
  232.         !(s->whatorbits==LEVEL_PLAN && s->pnumorbits==s->destpnum)) {
  233.             /* headed for a planet in this system &
  234.                we are not already there.. */
  235.      printf("dist from target planet is %lf\n",Ddist );
  236.      if (Ddist<=PLORBITSIZE) {
  237.        s->whatorbits = LEVEL_PLAN;
  238.        s->pnumorbits = s->destpnum;    
  239.         /* don't clear orders */
  240.       /* insert the ship */
  241.        for (i=0; dpl->shipnums[i] && i<MAXPSHIPS; i++) ;
  242.        if (i<MAXPSHIPS) {
  243.         dpl->shipnums[i] = shipno;
  244.         dpl->numships++;
  245.          /* mark as explored by that player */
  246.         dpl->info[s->owner-1].explored = 1;
  247.         teleg_add("",telegram_buf);
  248.         sprintf(buf,"Telecomm from ship #%d\n\n",shipno);
  249.         teleg_add(buf,telegram_buf);
  250.         sprintf(buf,"%s #%d arrived at planet %s.\n",Shipnames[s->type],
  251.                     shipno, prin_ship_orbits(s));
  252.         teleg_add(buf,telegram_buf);
  253.         teleg_send(TELEG_PLAYER_AUTO, s->owner, telegram_buf);
  254.         /**pldmod = 1;*/
  255.            printf("ship #%d arrived at planet /%s/%s,slot %d.\n",shipno, ost->name, ost->pnames[s->pnumorbits],i);
  256.            /* delete the ship from star */
  257.            for (i=0; ost->shipnums[i]!=shipno && i<MAXSSHIPS; i++) ;
  258.            if (i<MAXSSHIPS) {
  259.             ost->shipnums[i] = 0;
  260.             ost->numships--;
  261.             /**stomod = 1;*/
  262.            } else {
  263.             move_err=1;
  264.             printf("WARNING #5: can't delete from deststar!\n"); 
  265.             s->xpos = oldxpos;
  266.             s->ypos = oldypos;
  267.         }
  268.        } else {
  269.         printf(" WARNING #6! can't insert\n");
  270.         move_err = 1;
  271.         s->xpos = oldxpos;
  272.         s->ypos = oldypos;
  273.        }
  274.      }
  275.   }
  276.  }
  277. }
  278.