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

  1. /*conquer : Copyright (c) 1988 by Ed Barlow.
  2.  *  I spent a long time writing this code & I hope that you respect this.
  3.  *  I give permission to alter the code, but not to copy or redistribute
  4.  *  it without my explicit permission.  If you alter the code,
  5.  *  please document changes and send me a copy, so all can have it.
  6.  *  This code, to the best of my knowledge works well,  but it is my first
  7.  *  'C' program and should be treated as such.  I disclaim any
  8.  *  responsibility for the codes actions (use at your own risk).  I guess
  9.  *  I am saying "Happy gaming", and am trying not to get sued in the process.
  10.  *                                                Ed
  11.  */
  12.  
  13. /*EXECUTE THE PROGRAM*/
  14. #include "header.h"
  15. #include "data.h"
  16. extern long startgold;
  17. extern short country;
  18. extern FILE *fexe;
  19. extern char fison[];
  20.  
  21. #ifdef CONQUER
  22. extern int roads_this_turn;
  23. extern int terror_adj;
  24. #endif
  25.  
  26. int
  27. execute(isupdate)
  28. int    isupdate;    /* 0 if not update, 1 if update */
  29. {
  30.     FILE *fp, *fopen();
  31.     int cmd,savectry;
  32.     char comment[20];
  33.     char temp[10];
  34.     long longvar,long2var;
  35.     int armynum;
  36.     short int x,y;
  37.     int execed=0;
  38.     int done=FALSE;
  39.     char line[80];
  40.  
  41.     /* initialize startgold */
  42.     if( isupdate==0) startgold = curntn->tgold;
  43.  
  44.     /* initialize i_people */
  45.     for(x=0;x<MAPX;x++)
  46.         for(y=0;y<MAPY;y++)
  47.             if(( sct[x][y].owner == country)&&
  48.             ((sct[x][y].designation == DTOWN)
  49.             ||( sct[x][y].designation == DCAPITOL)
  50.             ||( sct[x][y].designation == DCITY)))
  51. /* note: i_people is a short, so we must scale to allow for people >= 32K */
  52.                 sct[x][y].i_people = sct[x][y].people/256;
  53.             else
  54.                 sct[x][y].i_people = 0;
  55.  
  56.     /*open exefile file*/
  57.     sprintf(line,"%s%d",exefile,country);
  58.     if ((fp=fopen(line,"r"))==NULL) {
  59.         /*THIS MEANS THAT THE NATION HAS NOT MOVED YET*/
  60.         return(0);
  61.     }
  62.     savectry=country;
  63.  
  64.     /*read in file*/
  65.     if(fgets(line,80,fp)==NULL) done=TRUE;
  66.     while(done==FALSE) {
  67.         /*read and parse a new line*/
  68.         /*CODE IF YOU USE LONG VAR IS L_*/
  69.         if( line[0] == 'L' && line[1] == '_' ) {
  70.             sscanf(line,"%s %d %hd %ld %ld %hd %s",
  71.                 temp,&cmd,&country,&longvar,&long2var,&y,comment);
  72.         } else {
  73.             sscanf(line,"%s %d %hd %d %hd %hd %s",
  74.                 temp,&cmd,&country,&armynum,&x,&y,comment);
  75.         }
  76.         curntn = &ntn[country];
  77.  
  78.         execed=1;
  79.         switch(cmd){
  80.         case XASTAT:        /*Aadjstat*/
  81.             if(x>0)  P_ASTAT=x;
  82.             break;
  83.         case XAMEN:    /*Aadjmen*/
  84.             armynum= (int) longvar;
  85.             P_ASOLD= (int) long2var;
  86.             P_ATYPE= y;
  87.             break;
  88.         case XBRIBE:    /* nation has been bribed */
  89.             if(!isupdate) break;    /* only work on update */
  90.             ntn[y].tgold += longvar;
  91. #ifdef DEBUG
  92.     fprintf(stderr,"DEBUG: BRIBE BY %s of %s\n",ntn[country].name,ntn[y].name);
  93. #endif DEBUG
  94.             /* x represents chance of successful bribe */
  95.             if(npctype(curntn->active)==npctype(ntn[y].active))
  96.                 x = 50;
  97.             else if(isneutral(ntn[y].active)) x=30;
  98.             else if(npctype(ntn[y].active)==ISOLATIONIST) x=15;
  99.             else    x = 20;
  100.             if(curntn->race==ntn[y].race) x+= 20;
  101.             if( rand()%100 < x){
  102. #ifdef DEBUG
  103.     fprintf(stderr,"DEBUG: BRIBE IS SUCCESS\n");
  104. #endif DEBUG
  105.                 ntn[y].dstatus[country]--;
  106.             }
  107.             break;
  108.         case XALOC:    /*Aadjloc*/
  109.             P_AXLOC=x;
  110.             P_AYLOC=y;
  111.             break;
  112.         case MSETA:    /*Aadjmerc*/
  113. #ifdef CONQUER
  114.             mercgot+=armynum;
  115. #endif
  116. #ifdef ADMIN
  117.             MERCMEN-=armynum;
  118. #endif
  119.             break;
  120.         case MSETB:    /*Aadjdisb*/
  121.             /* only allow raising of merc bonus */
  122.             if (x>MERCATT)
  123.             MERCATT=(MERCMEN*MERCATT+armynum*x)/(MERCMEN+armynum);
  124.             if (y>MERCDEF)
  125.             MERCDEF=(MERCMEN*MERCDEF+armynum*y)/(MERCMEN+armynum);
  126.             MERCMEN+=armynum;
  127.             break;
  128.         case XNLOC: /*nadjloc*/
  129.             curntn->nvy[armynum].xloc=x;
  130.             curntn->nvy[armynum].yloc=y;
  131.             break;
  132.         case XNACREW:
  133.             curntn->nvy[armynum].crew=x;
  134.             curntn->nvy[armynum].armynum=y;
  135.             break;
  136.         case XNAMER: /*nadjmer*/
  137.             curntn->nvy[armynum].merchant=x;
  138.             break;
  139.         case XNAWAR: /*nadjwar*/
  140.             curntn->nvy[armynum].warships=x;
  141.             break;
  142.         case XNAGAL: /*nadjgal*/
  143.             curntn->nvy[armynum].galleys=x;
  144.             break;
  145.         case XNAHOLD: /*nadjhld*/
  146.             curntn->nvy[armynum].people=(unsigned char)y;
  147.             curntn->nvy[armynum].armynum=(unsigned char)x;
  148.             break;
  149.         case XECNAME:    /*Nadjname*/
  150.             strcpy(curntn->name,comment);
  151.             break;
  152.         case XECPAS:    /*Nadjpas*/
  153.             strncpy(curntn->passwd,comment,PASSLTH);
  154.             break;
  155.         case NPOP:    /* set various nation attributes */
  156. #ifdef CONQUER
  157.             terror_adj++;
  158. #endif CONQUER
  159.             curntn->popularity = armynum;
  160.             curntn->terror = x;
  161.             curntn->reputation = y;
  162.             break;
  163.         case NTAX:    /* set nations tax rate */
  164.             curntn->tax_rate = armynum;
  165.             curntn->active = x;
  166.             curntn->charity = y;
  167.             break;
  168.         case EDSPL:    /*Edecspl*/
  169.             curntn->spellpts-=armynum;
  170.             break;
  171.         case XSADES:    /*Sadjdes*/
  172.             if((sct[x][y].owner!=country)&&(country!=0)) {
  173.                 fprintf(stderr,"ERROR: <%s> redesignate sector %d,%d that is not owned\n",curntn->name,x,y);
  174.                 break;
  175.             }
  176.  
  177.             sct[x][y].designation=comment[0];
  178.             if(sct[x][y].designation==DCAPITOL){
  179.                 curntn->capx=x;
  180.                 curntn->capy=y;
  181.             }
  182. #ifdef CONQUER
  183.             if (sct[x][y].designation==DROAD)
  184.                 roads_this_turn++;
  185. #endif
  186.             break;
  187.         case XSACIV:    /*Sadjciv*/
  188.             /* if for some reason you dont own it, put people
  189.             back into your capitol */
  190.             if((sct[x][y].owner!=country)&&(country!=0)) {
  191.                 sct[curntn->capx][curntn->capy].people+=armynum;
  192.                 fprintf(stderr,"ERROR: <%s> told to put %d civilians in sector %d,%d not owned - placed in capitol\n",curntn->name,armynum,x,y);
  193.                 break;
  194.             }
  195.             sct[x][y].people=armynum;
  196.             break;
  197.         case XSIFORT:    /*Sincfort*/
  198.             sct[x][y].fortress++;
  199.             break;
  200.         case XNAGOLD:    /*Nadjgold:*/
  201.             curntn->tgold = longvar;
  202.             break;
  203.         case XAMOV:
  204.             P_AMOVE=x;
  205.             break;
  206.         case XNMOV:
  207.             curntn->nvy[armynum].smove=x;
  208.             break;
  209.         case XSAOWN:
  210.             /* if not own it, and if people there, problem */
  211.             if((sct[x][y].owner!=country)
  212.             &&( country!=0)
  213.             &&( sct[x][y].owner>0 )
  214.             &&( sct[x][y].people>0 )
  215.             &&( magic(country,SLAVER)==FALSE )
  216.             &&( ntn[sct[x][y].owner].race!=curntn->race)){
  217.                 sct[ntn[sct[x][y].owner].capx][ntn[sct[x][y].owner].capy].people+= sct[x][y].people;
  218.                 sct[x][y].people=0;
  219.                 fprintf(stderr,"ERROR: <%s> taking sector %d %d but civilians exist of other race - puting them in their capitol\n",curntn->name,x,y);
  220.             }
  221.             if(curntn->popularity<MAXTGVAL) curntn->popularity++;
  222.             sct[x][y].owner=country;
  223.             break;
  224.         case EDADJ:
  225.             curntn->dstatus[armynum]=x;
  226.             break;
  227.         case XNARGOLD:
  228.             curntn->jewels = longvar;
  229.             break;
  230.         case XNAMETAL:
  231.             curntn->metals = longvar;
  232.             break;
  233.         case INCAPLUS:
  234.             curntn->aplus++;
  235.             break;
  236.         case INCDPLUS:
  237.             curntn->dplus++;
  238.             break;
  239.         case DESTRY:
  240.             sct[ntn[armynum].capx][ntn[armynum].capy].owner=savectry;
  241.             country=armynum;
  242.             destroy(country);
  243.             country=savectry;
  244.             break;
  245.         case CHG_MGK:
  246.             curntn->powers|=long2var;
  247.             if(curntn->powers!=longvar){
  248.             printf("\nERROR ON MAGIC READ country=%d %ld != %ld (or of %ld)",country,longvar,curntn->powers,long2var);
  249.             getchar();
  250.             }
  251.             exenewmgk(long2var);
  252.             long2var=0;
  253.             break;
  254.         }
  255.         if(fgets(line,80,fp)==NULL) done=TRUE;
  256.     }
  257.     fclose(fp);
  258.     /*return 1 if it did something*/
  259.     country=savectry;
  260.     curntn = &ntn[country];
  261.     if(execed==1) return(1);
  262.     else return(0);
  263. }
  264. #ifdef CONQUER
  265. void
  266. hangup()
  267. {
  268.     if(country==0) writedata();
  269.     else {
  270.         fprintf(fexe,"L_NGOLD\t%d \t%d \t%ld \t0 \t0 \t%s\n",
  271.         XNAGOLD ,country,curntn->tgold,"null");
  272.         fprintf(fexe,"L_NMETAL\t%d \t%d \t%ld \t0 \t0 \t%s\n",
  273.         XNAMETAL ,country,curntn->metals,"null");
  274.         fprintf(fexe,"L_NJWLS\t%d \t%d \t%ld \t0 \t0 \t%s\n",
  275.         XNARGOLD ,country,curntn->jewels,"null");
  276.     }
  277.     /*close file*/
  278.     fclose(fexe);
  279.     /*send a message to God*/
  280.     mailopen( 0 );
  281.     fprintf(fm,"WARNING: Nation %s hungup on me.\n",curntn->name);
  282.     mailclose();
  283.  
  284.     /* remove the lock file */
  285.     unlink(fison);
  286.     /* exit program */
  287.     exit(FAIL);
  288. }
  289. #endif CONQUER
  290.