home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume6 / gb / patch1 / build.c next >
C/C++ Source or Header  |  1989-07-06  |  8KB  |  290 lines

  1. /*
  2.  * Galactic Bloodshed (Robert Chansky, smq@ucscb.ucsc.EDU)
  3.  * build -- build a ship
  4.  */
  5.  
  6. #include "vars.h"
  7. #include "ships.h"
  8. #include "races.h"
  9. #include <curses.h>
  10. #include <setjmp.h>
  11. #include <signal.h>
  12. extern jmp_buf main_jenv;
  13. extern int Ignore_filelocks, God;
  14. int build_shdata,build_pdata,build_sectdata;
  15.  
  16.  
  17. build(APcount, argn,args)
  18. int APcount;
  19. int argn;
  20. char args[MAXARGS][COMMANDSIZE];
  21. {
  22. int j,i,x= -1 ,y= -1 ,shipno, planet_slot,mask;
  23. int picked=0;
  24. char shipc;
  25. planettype *planet;
  26. sectortype *sect;
  27. shiptype s,*dirship;
  28. int build_handler();
  29.  
  30.  
  31.  
  32. build_shdata = build_pdata = build_sectdata = NEUTRAL_FD;
  33. signal(SIGINT, build_handler);
  34.  
  35.  if (Dir.level==LEVEL_PLAN) {
  36.  } else if (Dir.level==LEVEL_SHIP) {
  37.     openshdata(&build_shdata);
  38.     if (!getship(build_shdata, &dirship, Dir.shipno)) {
  39.         printf("Illegal dir value.\n");
  40.         build_handler();
  41.     }
  42.     if (dirship->is_docked) {
  43.         if (dirship->whatdest==LEVEL_PLAN)
  44.             printf("This ship is landed.\n");
  45.         else
  46.             printf("There is already a ship docked here (#%d).\n",
  47.                     dirship->destshipno);
  48.         build_handler();
  49.     }
  50.  } else {
  51.     printf("You can't build anything here.\n");
  52.     build_handler();
  53.  }
  54.  
  55.  if (Dir.level==LEVEL_PLAN) {
  56.      if (!enufAP(Stars[Dir.snum]->AP, APcount))
  57.         build_handler();
  58.  } else if (dirship->whatorbits==LEVEL_UNIV) {
  59.      if (!enufAP(Sdata.AP, APcount))
  60.         build_handler();
  61.  } else
  62.      if (!enufAP(Stars[dirship->storbits]->AP, APcount))
  63.         build_handler();
  64.  
  65.   do {
  66.    tty_on();
  67.    printf("What ship type (? for help):");
  68.    shipc=getchr(); putchr('\n');
  69.    tty_off();
  70.    if (shipc=='?') {
  71.      printf("ltr              ship  carg armr dstcap guns  mass fuelcap mintech cost ^crew\n");
  72.      for (i=0; i<NUMSTYPES; i++)
  73.     if (Race->tech >= Shipdata[i][ABIL_TECH] || God)
  74.       if ((i==STYPE_POD && Race->Thing) || i!=STYPE_POD)
  75.         printf(" %c %18s %5d%5d%7d%5d%6d%8d%8d%5d%6d\n",Shipltrs[i],
  76.           Shipnames[i],Shipdata[i][ABIL_CARGO],
  77.           Shipdata[i][ABIL_ARMOR],
  78.           Shipdata[i][ABIL_DESTCAP],
  79.           Shipdata[i][ABIL_GUNS],Shipdata[i][ABIL_MASS],
  80.           Shipdata[i][ABIL_FUELCAP],Shipdata[i][ABIL_TECH],
  81.           Shipdata[i][ABIL_COST],Shipdata[i][ABIL_MAXCREW] );
  82.    } else {
  83.     i=0;
  84.     while ((Shipltrs[i]!=shipc) && (i<NUMSTYPES)) i++;
  85.     picked = 1;
  86.     if (i<0 || i>=NUMSTYPES || (i==STYPE_POD && !Race->Thing) ) {
  87.     printf("Illegal ship letter.\n");
  88.     picked = 0;
  89.     } else if (Race->tech < Shipdata[i][ABIL_TECH]) {
  90.     printf("You don't have enough technology to build one of those.\n");
  91.     if (!God)
  92.         picked = 0;
  93.     } else if (i==STYPE_MIRROR && Dir.level==LEVEL_PLAN) {
  94.     printf("You can't build one of those on a planet.\n");
  95.     picked = 0;
  96.     }
  97.    }
  98.   } while (!picked);
  99.  
  100.   if (Dir.level==LEVEL_PLAN) {
  101.       openpdata(&build_pdata);
  102.       getplanet(build_pdata,&planet,Stars[Dir.snum]->planetpos[Dir.pnum]);
  103.  
  104.         /* see if the ship can be made on the planet */
  105.     j = 0;
  106.     while (planet->shipnums[j++]) ;
  107.      if (j<=MAXPSHIPS) {
  108.         planet_slot = j-1;
  109.      } else {
  110.         printf("too many ships are already here.\n");
  111.         free(planet);
  112.         build_handler();
  113.      }
  114.      if (planet->info[Playernum-1].resource < Shipdata[i][ABIL_COST]) {
  115.     printf("not enough resources in stock.\n");
  116.     free(planet);
  117.     build_handler();
  118.      }
  119.    } else {
  120.      if (dirship->resource < Shipdata[i][ABIL_COST]) {
  121.     printf("not enough resources on this ship.\n");
  122.     build_handler();
  123.      }
  124.    }
  125.  
  126.      if (Dir.level==LEVEL_PLAN)
  127.     do {
  128.  
  129.             printf("Build %s on planet %s,", Shipnames[i],
  130.                          Stars[Dir.snum]->pnames[Dir.pnum] );
  131.        GetMapSector(planet, &x, &y);
  132.  
  133.        opensectdata(&build_sectdata);
  134.        getsector(build_sectdata, §, planet->sectormappos+(y*planet->Maxx+x)*sizeof(sectortype) );
  135.        close(build_sectdata);
  136.        free(sect);
  137.  
  138.           } while ( sect->owner != Playernum );
  139.  
  140.         /* keep people from fucking up the data files */
  141.     mask = sigblock(SIGINT | SIGQUIT | SIGSTOP);
  142.  
  143.  
  144.      Bzero(s);
  145.  
  146.     s.type = i;
  147.     s.xpos = (float)x;
  148.     s.ypos = (float)y;
  149.     /*s.augmented = 0;*/
  150.     if (s.type==STYPE_POD)
  151.         s.speed = 1;
  152.     else
  153.         s.speed = 2;    /* starting speed default */
  154.     s.owner = Playernum;
  155.     /*for (l=0; l < NUMABILS-3; l++)
  156.         s.abils[l]=Shipdata[i][l];*/
  157.     s.mass = (float)Shipdata[i][ABIL_MASS];
  158.     s.fuel = s.destruct = s.resource = 0;
  159.     s.is_docked = 1;
  160.     if (Dir.level==LEVEL_PLAN) {
  161.             /* docked on the planet */
  162.         s.whatorbits = LEVEL_PLAN;
  163.         s.whatdest = LEVEL_PLAN;
  164.         s.deststar = Dir.snum;
  165.         s.destpnum = Dir.pnum;
  166.     } else {
  167.         s.whatdest = LEVEL_SHIP;
  168.         s.destshipno = Dir.shipno;
  169.         s.whatorbits = dirship->whatorbits;
  170.     }
  171.     s.storbits = Dir.snum;
  172.     s.pnumorbits = Dir.pnum;
  173.     s.rad = 0;
  174.     if (Shipdata[s.type][ABIL_MAXCREW]==0)
  175.         s.damage = 0;
  176.     else
  177.         s.damage = 50;
  178.  
  179.     if (has_switch(&s)) {
  180.         s.orders.object.on = 0;
  181.     }
  182.     switch (s.type) {
  183.         case OTYPE_VN:
  184.         s.orders.object.number = 1;    /* one of them */
  185.         s.orders.object.number2 = 1;    /* we have an assignment */
  186.         s.orders.object.on = 1;
  187.         break;
  188.         case STYPE_MINE:
  189.         s.orders.object.number = 100;    /* trigger radius */
  190.         printf("Trigger radius set to 100.\n");
  191.         break;
  192.         case OTYPE_TRANSDEV:
  193.         printf("Receive OFF.  Change with order.\n");
  194.         break;
  195.         case OTYPE_TERRA:
  196.         printf("Processor OFF.\n");
  197.         break;
  198.         default:
  199.         break;
  200.     }
  201.  
  202.  
  203.         /* deduct resource cost */
  204.     if (Dir.level==LEVEL_PLAN)
  205.         planet->info[Playernum-1].resource -= Shipdata[i][ABIL_COST];
  206.     else
  207.         dirship->resource -= Shipdata[i][ABIL_COST];
  208.  
  209.       /* load new ship with fuel */
  210.     if (Dir.level==LEVEL_PLAN) {
  211.       if (planet->info[Playernum-1].fuel >= Shipdata[i][ABIL_FUELCAP]) {
  212.         planet->info[Playernum-1].fuel-=Shipdata[i][ABIL_FUELCAP];
  213.         s.fuel = (float)Shipdata[i][ABIL_FUELCAP];
  214.         s.mass += s.fuel*MASS_FUEL;
  215.         printf("%s Loaded with %.2f fuel.\n",Shipnames[s.type],s.fuel);
  216.       } else {
  217.         s.fuel = 0;
  218.         printf("No fuel available!\n");
  219.       }
  220.       /* put crew in ship */
  221.       opensectdata(&build_sectdata);
  222.       getsector(build_sectdata,§,planet->sectormappos+(y*planet->Maxx+x)*sizeof(sectortype));
  223.       if (sect->popn > Shipdata[i][ABIL_MAXCREW]) {
  224.         sect->popn -= Shipdata[i][ABIL_MAXCREW];
  225.         s.popn = Shipdata[i][ABIL_MAXCREW];
  226.         s.mass += (float)s.popn*Race->mass;
  227.         putsector(build_sectdata,sect,planet->sectormappos+(y*planet->Maxx+x)*sizeof(sectortype));
  228.         printf("Loaded with %d crew members.\n",Shipdata[i][ABIL_MAXCREW]);
  229.       } else {
  230.         s.popn = 1;
  231.         printf("Only one crewmember available.\n");
  232.       }
  233.       close(build_sectdata);
  234.       free(sect);
  235.  
  236.     }
  237.  
  238.         /* check for dead ships in the data files */
  239.     if (Dir.level==LEVEL_SHIP)
  240.         Ignore_filelocks = 1;
  241.     else
  242.         openshdata(&build_shdata);
  243.     if ( (shipno = getdeadship(build_shdata)) == -1) {
  244.             /* otherwise recycle topmost dead ship in data file */
  245.             /* no dead ships to recycle */
  246.         shipno = Numships(build_shdata) + 1;
  247.     }
  248.  
  249.     printf("Ship #%d is %s %.0f,%.0f.\n",shipno,
  250.         Dir.level==LEVEL_SHIP ? "at" : "on sector", s.xpos,s.ypos);
  251.  
  252.     putship(build_shdata,&s,shipno);
  253.  
  254.         /* Insert ship into planet's orbit (actually landed) */
  255.     if (Dir.level==LEVEL_PLAN) {
  256.         planet->shipnums[planet_slot] = shipno;
  257.         planet->numships++;
  258.         putplanet(build_pdata,planet,Stars[Dir.snum]->planetpos[Dir.pnum]);
  259.         close(build_pdata);
  260.             free(planet);
  261.     } else {
  262.         dirship->is_docked = 1;
  263.         dirship->whatdest = LEVEL_SHIP;
  264.         dirship->destshipno = shipno;
  265.         putship(build_shdata, dirship, Dir.shipno);
  266.         free(dirship);
  267.     }
  268.     close(build_shdata);
  269.  
  270.     Ignore_filelocks = 0;
  271.     if (Dir.level==LEVEL_SHIP && dirship->whatorbits==LEVEL_UNIV)
  272.         deductAPs(APcount, 0, 1);
  273.     else
  274.         deductAPs(APcount, Dir.snum, 0);
  275.  
  276.     sigsetmask(mask);
  277.  
  278. }
  279.  
  280.  
  281.  
  282. build_handler()
  283. {
  284.  close(build_shdata );
  285.  close(build_pdata );
  286.  close(build_sectdata );
  287.  Ignore_filelocks = 0;
  288.  longjmp(main_jenv,1);
  289. }
  290.