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

  1.  
  2. /*
  3.  * Galactic Bloodshed (Robert Chansky, smq@b)
  4.  *  load.c -- load/unload stuff
  5.  */
  6.  
  7. #include "vars.h"
  8. #include "ships.h"
  9. #include "races.h"
  10. #include <curses.h>
  11. #include <signal.h>
  12. #include <setjmp.h>
  13. extern jmp_buf main_jenv;
  14. extern int Ignore_filelocks;
  15. extern char telegram_buf[AUTO_TELEG_SIZE];
  16. int load_shdata,load_pdata,load_sectdata;
  17.  
  18.  
  19. load(APcount, argn,args)
  20. int APcount;
  21. int argn;
  22. char args[MAXARGS][COMMANDSIZE];
  23. {
  24. bool jett=0,sh=0;
  25. int first=1,proc,Mod=0,lolim,uplim,shipno,amt;
  26. char commod, buf[100],bufr[70],bufd[70],buff[70],bufc[70];
  27. shiptype *s,*s2;
  28. planettype *p;
  29. sectortype *sect;
  30. int load_handler();
  31.  
  32.     load_shdata = load_pdata = load_sectdata = NEUTRAL_FD;
  33.     signal(SIGINT, load_handler);
  34.  
  35.     if (argn==1) {
  36.       printf("ship #");
  37.       scanf("%d",&shipno);
  38.       getchr();
  39.     } else 
  40.     shipno = atoi(args[1] + ((args[1][0])=='#') ? 1 : 0);
  41.  
  42.     openshdata(&load_shdata);
  43.     if (!getship(load_shdata,&s,shipno)) {
  44.     load_handler();
  45.     }
  46.     if (s->owner!=Playernum || s->is_dead) {
  47.     DontOwnErr(shipno);
  48.     printf("(%d)\n",s->owner);
  49.     free(s);
  50.     load_handler();
  51.     }
  52.     if (s->rad) {
  53.     printf("%s #%d is irradiated and inactive.\n", Shipnames[s->type],shipno);
  54.     free(s);
  55.     load_handler();
  56.     }
  57.  
  58.  
  59.    do {
  60.  
  61.     proc = 0;
  62.  
  63.     if (s->type == OTYPE_TRANSDEV) {
  64.     Ignore_filelocks = 1;
  65.     if (!getship(load_shdata,&s2, s->orders.object.number)) {
  66.         printf("The hopper seems to be blocked.\n");
  67.         load_handler();
  68.     }
  69.     Ignore_filelocks = 0;
  70.     if (s2->is_dead) {
  71.         printf("The target device does not exist.\n");
  72.         load_handler();
  73.     }
  74.     if (s2->type!=OTYPE_TRANSDEV || !s2->orders.object.on) {
  75.         printf("The target device is not receiving.\n");
  76.         load_handler();
  77.     }
  78.     } else if (!s->is_docked) {
  79.     if (s->type==OTYPE_TRANSDEV) {
  80.         printf("It does not seem to work in zero-g environments.\n");
  81.         load_handler();
  82.     }
  83.     printf("%s #%d is not landed.\n",is_object(s)?"object":"ship",shipno);
  84.           tty_on();
  85.     printf("  jettison into open space (y/n)?");
  86.     jett = 1;
  87.           commod=getchr();
  88.           tty_off();
  89.           if (commod!='y')
  90.         load_handler();
  91.     } else {
  92.     if (s->whatdest==LEVEL_PLAN)
  93.         printf("Ship #%d at %.2f,%.2f\n", shipno,s->xpos, s->ypos);
  94.     else {
  95.         Ignore_filelocks = 1;
  96.         if (!getship(load_shdata, &s2, s->destshipno) || s2->is_dead ||
  97.             !(s2->is_docked && s2->whatdest==LEVEL_SHIP) ) {
  98.                 /* the ship it was docked with died or
  99.                    something. */
  100.             s->is_docked = 0;
  101.             s->whatdest = LEVEL_UNIV;
  102.             putship(load_shdata, &s, shipno);
  103.             printf("ship is not docked.\n");
  104.             load_handler();
  105.         }
  106.         Ignore_filelocks = 0;
  107.         printf("Ship #%d docked with %s #%d\n", shipno, 
  108.                 Shipnames[s2->type], s->destshipno);
  109.         sh = 1;
  110.     }
  111.     }
  112.  
  113.  
  114.     if (!jett && !sh) {
  115.         openpdata(&load_pdata);
  116.         getplanet(load_pdata,&p,Stars[Dir.snum]->planetpos[Dir.pnum]);
  117.     }
  118.  
  119.     if (first) {
  120.       if (s->whatorbits==LEVEL_UNIV)
  121.         if (!enufAP(Sdata.AP, APcount))
  122.         load_handler();
  123.       else 
  124.     if (!enufAP(Stars[s->storbits]->AP, APcount))
  125.         load_handler();
  126.     }
  127.  
  128.  
  129.    do {
  130.  
  131.     tty_on();
  132.     printf("%s #%d:(rfdc? q) >", Shipnames[s->type], shipno);
  133.     commod = getchr();
  134.     putchr('\n');
  135.     tty_off();
  136.  
  137.     if (!jett && !sh && commod=='c') {
  138.         opensectdata(&load_sectdata);
  139.         getsector(load_sectdata,§,p->sectormappos + ( (int)s->ypos*p->Maxx+(int)s->xpos) * sizeof(sectortype) );
  140.     }
  141.  
  142.     switch (commod) {
  143.     case 'c': lolim = - s->popn;
  144.           uplim = jett ? 0 : sh ? s2->popn :
  145.             MIN(sect->popn, 
  146.                  Shipdata[s->type][ABIL_MAXCREW] - s->popn);
  147.           proc = 1;
  148.           break;
  149.     case 'd': lolim = - s->destruct;
  150.           uplim = jett ? 0 : sh ? s2->destruct :
  151.             MIN(p->info[Playernum-1].destruct,
  152.                      Shipdata[s->type][ABIL_DESTCAP] - s->destruct);
  153.           proc = 1;
  154.           break;
  155.     case 'f': lolim = - s->fuel;
  156.           uplim = jett ? 0 : sh ? s2->fuel :
  157.             MIN(p->info[Playernum-1].fuel,
  158.                      Shipdata[s->type][ABIL_FUELCAP] - s->fuel);
  159.           proc = 1;
  160.           break;
  161.     case 'r': lolim = - s->resource;
  162.           uplim = jett ? 0 : sh ? s->resource :
  163.             MIN(p->info[Playernum-1].resource,
  164.                      Shipdata[s->type][ABIL_CARGO] - s->resource);
  165.           proc = 1;
  166.           break;
  167.     case '?': printf("\nship #%3d:\n",shipno);
  168.           printf("       res:%u\n       fuel:%.1f\n       crew:%u\n       dest:%u\n", s->xpos,s->ypos, s->resource, s->fuel, s->popn, s->destruct);
  169.           if (sh) {
  170.             printf("docked with %s #%d:\n",s->destshipno);
  171.             printf("       res:%u\n       fuel:%.1f\n       crew:%u\n       dest:%u\n", s2->xpos,s2->ypos, s2->resource, s2->fuel, s2->popn, s2->destruct);
  172.           }
  173.           proc = 0;
  174.           break;
  175.     case 'q':
  176.     case ' ':
  177.           load_handler();
  178.           break;
  179.     default:  printf("%c not valid.\n",commod);
  180.           proc = 0;
  181.           load_handler();
  182.       }
  183.      } while (!proc);
  184.  
  185.       tty_off();
  186.       amt = uplim + 1;
  187.       while (amt<lolim || amt>uplim) {
  188.       printf("      amount (%d to %d) ",lolim,uplim);
  189.       if (scanf("%d",&amt)!=1) 
  190.         amt=uplim+1;    /* force error */
  191.       getchr();
  192.       }
  193.     if (amt == 0)
  194.      load_handler();
  195.  
  196.     switch (commod) {
  197.     case 'c': if (!jett) {
  198.             if (sh) {
  199.                 s2->popn -= amt;
  200.                 s2->mass -= amt*Race->mass;
  201.             } else {
  202.                  if (sect->popn==0 && amt<0) {
  203.                 p->info[Playernum-1].numsectsowned++;
  204.                 sect->owner = Playernum;
  205.                 printf("sector %.0f,%.0f COLONIZED.\n",s->xpos,s->ypos);
  206.                  }
  207.                  sect->popn -= (us)amt;
  208.                  p->popn -= amt;
  209.                  if (sect->popn==0) {
  210.                 p->info[Playernum-1].numsectsowned--;
  211.                 sect->owner = 0;
  212.                 printf("sector %.0f,%.0f evacuated.\n",s->xpos,s->ypos);
  213.                  }
  214.             }
  215.           }
  216.           printf("crew complement of ship #%d is now %u.\n", 
  217.             shipno, s->popn+amt);
  218.           s->popn += amt;
  219.           s->mass += amt*Race->mass;
  220.           Mod = 1;
  221.           break;
  222.     case 'd': s->destruct += amt;
  223.           s->mass += amt*MASS_DESTRUCT;
  224.           if (!jett)
  225.               p->info[Playernum-1].destruct -= amt;
  226.           else if (sh) {
  227.             s2->destruct -= amt;
  228.             s2->mass -= amt*MASS_DESTRUCT;
  229.           }
  230.           Mod = 1;
  231.           break;
  232.     case 'f': s->fuel += amt;
  233.           s->mass += amt*MASS_FUEL;
  234.           if (!jett)
  235.               p->info[Playernum-1].fuel -= amt;
  236.           else if (sh) {
  237.             s2->fuel -= amt;
  238.             s2->mass -= amt*MASS_FUEL;
  239.           }
  240.           Mod = 1;
  241.           break;
  242.     case 'r': s->resource += amt;
  243.           s->mass += amt*MASS_RESOURCE;
  244.           if (!jett)
  245.               p->info[Playernum-1].resource -= amt;
  246.           else if (sh) {
  247.             s2->resource -= amt;
  248.             s2->mass -= amt*MASS_FUEL;
  249.           }
  250.           Mod = 1;
  251.           break;
  252.     case 'q':
  253.     case ' ':
  254.     case '?':
  255.     default:
  256.         break;
  257.     }
  258.  
  259.     putship(load_shdata,s,shipno);
  260.  
  261.     if (s->type==OTYPE_TRANSDEV || sh) {
  262.     /* send stuff to other transport device */
  263.     if (s->resource) {
  264.         printf("Zap! %d resources sent.\n", s->resource);
  265.         sprintf(bufr, "%d Resources\n",s->resource);
  266.         s2->mass += s->resource * MASS_RESOURCE;
  267.         s2->resource += s->resource;
  268.         s->mass -= s->resource * MASS_RESOURCE;
  269.         s->resource = 0;
  270.     } else
  271.         bufr[0] = '\0';
  272.     if (s->fuel) {
  273.         printf("Zap! %g fuel sent.\n", s->fuel);
  274.         sprintf(buff, "%g Fuel\n",s->fuel);
  275.         s2->fuel += s->fuel;
  276.         s2->mass += s->fuel * MASS_FUEL;
  277.         s->mass -= s->fuel * MASS_FUEL;
  278.         s->fuel = 0.0;
  279.     } else
  280.         buff[0] = '\0';
  281.     if (s->destruct) {
  282.         printf("Zap! %d destruct sent.\n", s->destruct);
  283.         sprintf(bufd, "%d Destruct\n",s->destruct);
  284.         s2->mass += s->destruct * MASS_DESTRUCT;
  285.         s2->destruct += s->destruct;
  286.         s->mass -= s->destruct * MASS_DESTRUCT;
  287.         s->destruct = 0;
  288.     } else
  289.         bufd[0] = '\0';
  290.     if (s->popn) {
  291.         printf("Zap! %d popn sent.\n", s->popn);
  292.         sprintf(bufc, "%d %s\n",s->destruct,Race->Thing?"tons of biomass" : "population");
  293.         s2->mass += s->popn * Race->mass;
  294.         s2->popn += s->popn;
  295.         s->mass -= s->popn * Race->mass;
  296.         s->popn = 0;
  297.     } else
  298.         bufc[0] = '\0';
  299.     putship(load_shdata,s2,s->orders.object.number);
  300.  
  301.       if (s2->owner!=s->owner) {
  302.     teleg_add("", telegram_buf);
  303.     teleg_add("BULLETIN!\n\n Audio-vibatory-physio-molecular transport device #",telegram_buf);
  304.     sprintf(buf,"%d just gave your ship #%d the following:\n", shipno, s->orders.object.number);
  305.     teleg_add(buf,telegram_buf);
  306.     teleg_add(bufr,telegram_buf);
  307.     teleg_add(bufd,telegram_buf);
  308.     teleg_add(buff,telegram_buf);
  309.     teleg_add(bufc,telegram_buf);
  310.     teleg_send(TELEG_PLAYER_AUTO, s2->owner, telegram_buf);
  311.       }
  312.     }
  313.  
  314.  
  315.     if (!jett && !sh) {
  316.  
  317.       if (commod=='c') {
  318.     putsector(load_sectdata,sect,p->sectormappos + ( (int)s->ypos*p->Maxx+(int)s->xpos) * sizeof(sectortype) );
  319.         close(load_sectdata);
  320.     free(sect);
  321.        }
  322.  
  323.        putplanet(load_pdata,p,Stars[Dir.snum]->planetpos[Dir.pnum]);
  324.        close(load_pdata);
  325.        free(p);
  326.      }
  327.  
  328.     if (Mod && first) {
  329.          first = 0;
  330.          if (s->whatorbits==LEVEL_UNIV)
  331.         deductAPs(APcount, 0, 1);    /* ded from sdata */
  332.          else 
  333.         deductAPs(APcount, s->storbits, 0);
  334.  
  335.     }
  336.  
  337.    } while (1);
  338.  
  339.  
  340.  
  341. }
  342.  
  343.  
  344.  
  345. load_handler()
  346. {
  347.  Ignore_filelocks = 0;
  348.  close(load_shdata);
  349.  close(load_pdata);
  350.  close(load_sectdata);
  351.  longjmp(main_jenv,1);
  352. }
  353.