home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume6 / conquer4 / part05 / npc.c next >
C/C++ Source or Header  |  1989-07-06  |  38KB  |  1,400 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. #include "header.h"
  14. #include "data.h"
  15. #include <ctype.h>
  16.  
  17. extern FILE *fnews;
  18.  
  19. int    stx, sty, endx, endy;    /* npc range of operations */
  20. extern    short country;
  21. extern    int **attr;         /*sector attactiveness*/
  22. extern    int    dissarray;    /* has nation lost its leader */
  23. int    peace;    /*is 8 if at peace, 12 if at war*/
  24.  
  25. void
  26. prtattr()
  27. {
  28. #ifdef DEBUG
  29.     int x,y;
  30. /*    FOR TESTING OF WHAT THE ATTRACTIVENESS ENDS UP LIKE  */
  31.     printf("Attractiveness for sectors around CAPITAL at %d %d veg alt des\n",curntn->capx,curntn->capy);
  32.     for(y=curntn->capy-3;y<curntn->capy+4;y++) {
  33.         printf("\n");
  34.         for(x=curntn->capx-3;x<curntn->capx+4;x++) if(ONMAP(x,y)) {
  35.             if( sct[x][y].altitude != WATER ) {
  36.                 printf("[%2d,%2d]   ",x,y);
  37.             }
  38.         }
  39.         printf("\n");
  40.         for(x=curntn->capx-3;x<curntn->capx+4;x++) if(ONMAP(x,y)) {
  41.             if( sct[x][y].altitude != WATER ) {
  42.                 printf("%5d %c%c%c "
  43.                     ,attr[x][y]
  44.                     ,sct[x][y].vegetation
  45.                     ,sct[x][y].altitude
  46.                     ,sct[x][y].designation);
  47.             } else    printf("0      ~  ");
  48.         }
  49.         printf("\n");
  50.     }
  51. #endif DEBUG
  52. }
  53.  
  54. /*newdip() diplomacy if unmet - ntn 1 is nation you are updating*/
  55. void
  56. newdip(ntn1,ntn2)
  57. int    ntn1,ntn2;
  58. {
  59.     if( ispc( ntn[ntn1].active ) ) {
  60.         if( ntn[ntn2].race==ORC ) 
  61.             ntn[ntn1].dstatus[ntn2]=HOSTILE;
  62.         else    ntn[ntn1].dstatus[ntn2]=NEUTRAL;
  63.         return;
  64.     }
  65.     if((ntn[ntn1].race==ORC)
  66.     ||( ntn[ntn2].race==ORC)) {
  67.         if(ntn[ntn1].dstatus[ntn2]==UNMET)
  68.             if((rand()%2==0)|| ispc(ntn[ntn1].active))
  69.                 ntn[ntn1].dstatus[ntn2]=HOSTILE;
  70.             else
  71.                 ntn[ntn1].dstatus[ntn2]=WAR;
  72.     } else if(ismonst(ntn[ntn2].active)) {
  73.         ntn[ntn1].dstatus[ntn2]=WAR;
  74.     } else if(ispc(ntn[ntn1].active)) {
  75.         if(ntn[ntn1].dstatus[ntn2]==UNMET)
  76.             ntn[ntn1].dstatus[ntn2]=NEUTRAL;
  77.     } else if(ntn[ntn1].race==ntn[ntn2].race){
  78.         if(rand()%2<1) ntn[ntn1].dstatus[ntn2]=FRIENDLY;
  79.         else ntn[ntn1].dstatus[ntn2]=NEUTRAL;
  80.     } else ntn[ntn1].dstatus[ntn2]=NEUTRAL;
  81. }
  82.  
  83. #ifdef MONSTER
  84. void
  85. monster()
  86. {
  87.     for(country=1;country<NTOTAL;country++) {
  88.         curntn = &ntn[country];
  89.         if( curntn->active==NPC_NOMAD ) do_nomad();
  90.         else if( curntn->active==NPC_PIRATE ) do_pirate();
  91.         else if( curntn->active==NPC_BARBARIAN ) do_barbarian();
  92.         else if( curntn->active==NPC_LIZARD ) do_lizard();
  93.     }
  94. }
  95.  
  96. void
  97. do_nomad()
  98. {
  99.     int    count;
  100.     short    armynum;
  101.     int    x, y;
  102.  
  103.     /*move nomads */
  104.     printf("updating nomad nation %d\n",country);
  105.     for(armynum=0;armynum<MAXARM;armynum++) if(P_ASOLD>0){
  106.         P_ASTAT=ATTACK;
  107.         P_AMOVE=(curntn->maxmove * *(unitmove+P_ATYPE%UTYPE))/10;
  108.         if(P_ATYPE<MINLEADER) {
  109.             P_ASOLD *= 102;
  110.             P_ASOLD /= 100;
  111.         }
  112.         count=0;
  113.         while( TRUE ) {
  114.             x=P_AXLOC+rand()%5-2;
  115.             y=P_AYLOC+rand()%5-2;
  116.  
  117.             if( count++ > 100 ) {
  118.                 P_ASOLD=0;
  119.                 break;
  120.             }
  121.             /* nomads cant stay in the same spot */
  122.             if(( x==P_AXLOC && y==P_AYLOC )
  123.             ||(!ONMAP(x,y)
  124.             ||(!is_habitable(x,y))) 
  125.             ||(!land_reachp(P_AXLOC,P_AYLOC,x,y,curntn->maxmove,country))) continue;
  126.  
  127.             P_AXLOC=x;
  128.             P_AYLOC=y;
  129.             /*if owned & unoccupied you take & people flee*/
  130.             if( ((sct[x][y].owner) == 0
  131.             || solds_in_sector( x, y, sct[x][y].owner) == 0 )
  132.             && (ntn[sct[x][y].owner].active!=NPC_NOMAD) ) {
  133.                 fprintf(fnews,"3.\tnomads capture sector %d,%d\n",x,y);
  134.                 if(sct[x][y].owner!=0) flee(x,y,1,FALSE);
  135.                 sct[x][y].owner=country;
  136.                 DEVASTATE(x,y);
  137.             }
  138.             break;
  139.         }
  140.     }
  141.     /* place a few new Nomad armies */
  142.     for(armynum=0;armynum<MAXARM;armynum++) if(P_ASOLD<=0){
  143.         if(rand()%4!=0) continue;
  144.         x=(rand()%(MAPX-8))+4;
  145.         y=(rand()%(MAPY-8))+4;
  146.         if(is_habitable(x,y)) {
  147.             P_AXLOC=x;
  148.             P_AYLOC=y;
  149.             P_ASOLD=100+100*(rand()%6);
  150.             P_ATYPE=A_LT_CAV;
  151.             P_ASTAT=ATTACK;
  152.         }
  153.     }
  154. }
  155.  
  156. void
  157. do_barbarian()
  158. {
  159.     short armynum;
  160.     int x, y;
  161.  
  162.     printf("updating barbarian nation %d\n",country);
  163.     for(armynum=0;armynum<MAXARM;armynum++) if(P_ASOLD>0){
  164.         P_ASTAT=ATTACK;
  165.         if(P_ATYPE<MINLEADER) {
  166.             P_ASOLD *= 102;
  167.             P_ASOLD /= 100;
  168.         }
  169.         P_AMOVE=(curntn->maxmove * *(unitmove+P_ATYPE%UTYPE))/10;
  170.         x=P_AXLOC+rand()%2-1;
  171.         y=P_AYLOC+rand()%2-1;
  172.         if(ONMAP(x,y)&&(is_habitable(x,y)) 
  173.         &&(land_reachp(P_AXLOC,P_AYLOC,x,y,P_AMOVE,country))){
  174.             P_AXLOC=x;
  175.             P_AYLOC=y;
  176.             /*if owned & unoccupied you take & people flee*/
  177.             if( ((sct[x][y].owner == 0)
  178.             || (solds_in_sector( x, y, sct[x][y].owner) == 0))
  179.             && (ntn[sct[x][y].owner].active != NPC_BARBARIAN)) {
  180.                 fprintf(fnews,"3.\tbarbarians capture sector %d,%d\n",x,y);
  181.                 if(P_ATYPE<MINLEADER) {
  182.                     if(sct[x][y].owner!=0) flee(x,y,1,FALSE);
  183.                     sct[x][y].owner=country;
  184.                 }
  185.                 DEVASTATE(x,y);
  186.             }
  187.         }
  188.     }
  189.     /* place a few new Barbarian armies */
  190.     for(armynum=0;armynum<MAXARM;armynum++) if(P_ASOLD<=0){
  191.         x=(rand()%(MAPX-8))+4;
  192.         y=(rand()%(MAPY-8))+4;
  193.         if((rand()%4!=0) 
  194.         ||( sct[x][y].altitude == PEAK)
  195.         ||( sct[x][y].altitude == WATER))
  196.             continue;
  197.         if(sct[x][y].owner==0 || sct[x][y].owner==country 
  198.         ||(sct[x][y].people< 50)) {
  199.             P_AXLOC=x;
  200.             P_AYLOC=y;
  201.             P_ASOLD=100+100*(rand()%3);
  202.             P_ATYPE=defaultunit(country);
  203.             P_ASTAT=ATTACK;
  204.         }
  205.     }
  206. }
  207.  
  208. void
  209. do_lizard()
  210. {
  211.     short armynum;
  212.  
  213.     printf("updating lizard nation %d\n",country);
  214.     for(armynum=0;armynum<MAXARM;armynum++) if(P_ASOLD>0){
  215.         P_ASOLD*=102;
  216.         P_ASOLD/=100;
  217.     }
  218. }
  219.  
  220. void
  221. do_pirate()
  222. {
  223.     short nvynum,shipsize;
  224.     int x, y, campx, campy;
  225.  
  226.     printf("updating pirate nation %d\n",country);
  227.  
  228.     /* if pirate fleet within 3 attack if outnumber any fleets */
  229.     /* automatically find their base first */
  230.     for(nvynum=0;nvynum<MAXNAVY;nvynum++) if(P_NWSHP!=0) {
  231.         int temp=TRUE;
  232.  
  233.         campx=P_NXLOC; campy=P_NYLOC;
  234.         for(x=P_NXLOC-PRTZONE;x<=P_NXLOC+PRTZONE;x++)
  235.         for(y=P_NYLOC-PRTZONE;y<=P_NYLOC+PRTZONE;y++) {
  236.             if((ONMAP(x,y)) && sct[x][y].designation==DBASECAMP ) {
  237.                 temp=FALSE;
  238.                 campx=x;
  239.                 campy=y;
  240.             }
  241.         }
  242.         if(temp==TRUE) {
  243.             fprintf(stderr,"Pirate fleet %d away from base\n",nvynum);
  244.         } else {
  245.             if (sct[campx][campy].designation!=DBASECAMP) {
  246.                 fprintf(stderr,"BASECAMP NOT FOUND!!!\n");
  247.             } else {
  248.                 P_NXLOC=campx;
  249.                 P_NYLOC=campy;
  250.             }
  251.         }
  252.     }
  253.         
  254.     for(nvynum=0;nvynum<MAXNAVY;nvynum++) if (P_NWSHP!=0) {
  255.         for(x=1;x<NTOTAL;x++) if(isntn(ntn[x].active))
  256.         for(y=0;y<MAXNAVY;y++)
  257.         if(ntn[x].nvy[y].warships!=0 || ntn[x].nvy[y].merchant!=0 
  258.           || ntn[x].nvy[y].galleys!=0) {
  259.             if((abs(ntn[x].nvy[y].xloc-P_NXLOC)<=PRTZONE)
  260.             &&(abs(ntn[x].nvy[y].yloc-P_NYLOC)<=PRTZONE)) {
  261.                 P_NXLOC= ntn[x].nvy[y].xloc;
  262.                 P_NYLOC= ntn[x].nvy[y].yloc;
  263.             }
  264.         }
  265.         if(rand()%15==0) {
  266.             /*randomly add one warship to pirate fleet*/
  267.             shipsize = rand()%(N_HEAVY-N_LIGHT+1);
  268.             (void) NADD_WAR(1);
  269.         }
  270.     }
  271. }
  272. #endif MONSTER
  273.  
  274. #ifdef NPC
  275. void
  276. n_redes(x,y,goldthresh,metalthresh,citythresh,hunger)
  277. int    x,y,goldthresh,metalthresh,citythresh;
  278. float    hunger;
  279. {
  280.     register struct s_sector    *sptr = &sct[x][y];
  281.  
  282.     if((sptr->designation == DCAPITOL)
  283.     ||(sptr->designation == DCITY)) return;
  284.  
  285.     /*large enough for a city now?*/
  286.     if(((sptr->people > (spread.civilians/CITYLIMIT))
  287.     ||((spread.civilians<30000)&&(sptr->people>1000)))
  288.     &&( hunger > P_EATRATE*1.5 )
  289.     &&( spread.incity+spread.incap < spread.civilians * CITYPERCENT / 100)
  290.     &&( spread.sectors > 10)
  291.     &&( sptr->tradegood == TG_none )){
  292.         sptr->designation=DTOWN;
  293.         spread.incity+=sptr->people;
  294.         spread.infarm-=sptr->people;
  295.     }
  296.  
  297.     /* large enough for city and not enough food*/
  298.     if((sptr->designation==DTOWN)
  299.     &&( hunger < P_EATRATE)
  300.     &&( tofood(sptr,sptr->owner) > citythresh )){
  301.         sptr->designation=DFARM;
  302.         spread.incity-=sptr->people;
  303.         spread.infarm+=sptr->people;
  304.     }
  305.  
  306.     if((sptr->designation==DTOWN)
  307.     &&( spread.incity+spread.incap > spread.civilians * CITYPERCENT / 66)){
  308.         sptr->designation=DFARM;
  309.         spread.incity-=sptr->people;
  310.         spread.infarm+=sptr->people;
  311.     }
  312.  
  313.     /*what if it is not a city*/
  314.     if((sptr->designation!=DTOWN)
  315.     &&(sptr->designation!=DCITY)
  316.     &&(sptr->designation!=DCAPITOL)){
  317.         if(( sptr->tradegood != TG_none )
  318.         &&( tg_ok( sptr->owner, sptr ))) {
  319.             if(( metalthresh+goldthresh > 8 )
  320.             ||(( sptr->metal < metalthresh )
  321.               &&( sptr->metal != 0 ))
  322.             ||(( sptr->jewels < goldthresh )
  323.               &&( sptr->jewels != 0 ))) {
  324.                 sptr->designation = DFARM;
  325.             } else    sptr->designation= *(tg_stype+sptr->tradegood);
  326.  
  327.             if(( sptr->metal < metalthresh )
  328.               &&( sptr->metal != 0 ))
  329.                 sptr->designation=DBLKSMITH;
  330.  
  331.             if(( sptr->designation== 'x' )
  332.             ||(( sptr->designation== DCITY )&&(sptr->people<1000)))
  333.                 sptr->designation=DFARM;
  334.         } else if( tofood(sptr,sptr->owner) >= 4 ){
  335.             sptr->designation=DFARM;
  336.         } else    sptr->designation=DSTOCKADE;
  337.     }
  338.     if(( sptr->designation==DFARM)
  339.     &&( hunger > P_EATRATE*1.5 )
  340.     &&(  tofood(sptr,sptr->owner) <= 6 )){
  341.         if(( rand()%2 == 0 )&&( curntn->mine_ability<30 ))
  342.             sptr->designation=DBLKSMITH;
  343.         else if(( rand()%2 == 0 )
  344.         &&( sptr->people<100 )
  345.         &&( curntn->spoilrate >15))
  346.             sptr->designation=DGRANARY;
  347.         else if(( rand()%2 == 0 )&&( curntn->popularity<50 ))
  348.             sptr->designation=DCHURCH;
  349.         else if( sptr->people>1000 )
  350.             sptr->designation=DTOWN;
  351.     }
  352. }
  353.  
  354. void
  355. redomil()
  356. {
  357.     short x,y,armynum,nvynum;
  358.     int i, free, done;
  359.     long militia=0l,ideal;
  360.     long diff=0l;
  361.     int ok;
  362.  
  363.     check();
  364.     /* check out any ship crews */
  365.     for(nvynum=1;nvynum<MAXNAVY;nvynum++) {
  366.         /* definite cheat -- add some random */
  367.         if((P_NMSHP!=0)||(P_NWSHP!=0)||(P_NGSHP != 0))
  368.             if(rand()%2==0) P_NCREW = SHIPCREW;
  369.     }
  370.     check();
  371.     for(armynum=1;armynum<MAXARM;armynum++) if(P_ASOLD>0){
  372.         /* move army back if too far out */
  373.         ok = 0;
  374.         for(x=P_AXLOC-3;x<=P_AXLOC+3;x++)
  375.             for(y=P_AYLOC-3;y<=P_AYLOC+3;y++)
  376.                 if((ONMAP(x,y))&&(sct[x][y].owner==country)) ok=1;
  377.         if(ok==0){
  378.             P_AXLOC=curntn->capx;
  379.             P_AYLOC=curntn->capy;
  380.         }
  381.  
  382.         /* count and verify militia */
  383.         if(P_ATYPE==A_MILITIA) {
  384.             /* eliminate invalid militia */
  385.             if(((sct[P_AXLOC][P_AYLOC].designation!=DTOWN)
  386.             &&(sct[P_AXLOC][P_AYLOC].designation!=DCAPITOL)
  387.             &&(sct[P_AXLOC][P_AYLOC].designation!=DCITY))
  388.             ||(sct[P_AXLOC][P_AYLOC].owner!=country)){
  389. #ifdef DEBUG
  390.                 printf("\teliminating %s army %d as %d %d is des:%c alt:%c own:%d\n",unittype[P_ATYPE],armynum,P_AXLOC,P_AYLOC,sct[P_AXLOC][P_AYLOC].designation,sct[P_AXLOC][P_AYLOC].altitude,sct[P_AXLOC][P_AYLOC].owner);
  391. #endif DEBUG
  392.                 if(sct[P_AXLOC][P_AYLOC].owner == country)
  393.                     sct[P_AXLOC][P_AYLOC].people+=P_ASOLD;
  394.                 else sct[curntn->capx][curntn->capy].people+=P_ASOLD;
  395.                 P_ASOLD=0;
  396.             } else militia+=P_ASOLD;
  397.         }
  398.  
  399.         /* set default status */
  400.         if(P_ASTAT<NUMSTATUS) switch(P_ASTAT) {
  401.         case MILITIA:
  402.         case SIEGED:
  403.         case TRADED:
  404.         case ONBOARD:
  405.         case GENERAL:
  406.             break;
  407.         default:
  408.             P_ASTAT=DEFEND;
  409.             break;
  410.         }
  411.     }
  412.     curntn->tmil -= militia;
  413.  
  414.     /*make sure enough men in army 0 -- garrison duty in capitol*/
  415.     armynum=0;
  416.     P_ASTAT=GARRISON;
  417.     P_ATYPE=defaultunit(country);
  418.     P_AXLOC=curntn->capx;
  419.     P_AYLOC=curntn->capy;
  420.  
  421.     /*Ideally P_ASOLD[0]*MILINCAP=tmil*peace/10*/
  422.     ideal = curntn->tmil * peace / (10L*MILINCAP);
  423.     if(curntn->tgold < 0) ideal/=2L;
  424. #ifdef DEBUG
  425.     if(peace==8)
  426.         printf("\t%s IS AT PEACE - garrison in cap is %ld, ideal is %ld\n",curntn->name,P_ASOLD,ideal);
  427.     else if(peace==12)
  428.         printf("\t%s IS AT WAR - garrison in cap is %d, ideal is %ld\n",curntn->name,P_ASOLD,ideal);
  429.     else printf("error - incap is %d ideal is %ld\n",P_ASOLD,ideal);
  430. #endif DEBUG
  431.  
  432.     /*MILRATIO ratio mil:civ for non player countries*/
  433.     /*MILINCAP ratio (mil in cap):mil for NPCs*/
  434.     check();
  435.  
  436.     if((P_ASOLD*10) < (9*ideal)){
  437.  
  438.     /*too few soldiers on garrison*/
  439.     /*diff is number to change mil in cap (>0)*/
  440.     if(curntn->tgold<0L) diff=0;
  441.     else diff = (long) min(ideal-P_ASOLD,(int) (curntn->metals / *(u_enmetal + (P_ATYPE%UTYPE))));
  442.  
  443.     diff=(long) min((int) diff, sct[curntn->capx][curntn->capy].people/2L);
  444.  
  445.     if(curntn->tgold<0L || curntn->metals<0L) if(diff > 0L) diff=0;
  446.  
  447. #ifdef DEBUG
  448.     printf("\tadding %d men to garrison (too few men on garrison)\n",diff);
  449. #endif DEBUG
  450.  
  451.     sct[curntn->capx][curntn->capy].people-=diff;
  452.     P_ASOLD+=diff;
  453.     curntn->tciv-=diff;
  454.     curntn->tmil+=diff;
  455.     if(magic(country,WARRIOR)==1)    /* take WARRIOR power into account */
  456.         curntn->tgold -= (diff * *(u_encost + (P_ATYPE%UTYPE))) / 2;
  457.     else curntn->tgold-=diff* *(u_encost + (P_ATYPE%UTYPE));
  458.     curntn->metals-=(diff* *(u_enmetal + (P_ATYPE%UTYPE)));
  459. #ifdef DEBUG
  460.     if(P_ASOLD < 0L) printf("error 2... P_ASOLD=%d <0\n",P_ASOLD);
  461. #endif DEBUG
  462.     }
  463.     /*else split garrison army if 1.25* needed number*/
  464.     else if(P_ASOLD *4L > 5L*ideal){
  465.         /*diff here is a negative number*/
  466.         diff=((4L*P_ASOLD)-(5L*ideal))/4L;
  467. #ifdef DEBUG
  468.         printf("\tsplit garrison of %d men\n",diff);
  469. #endif DEBUG
  470.         free=FALSE;
  471.         P_ASOLD-=diff;
  472. #ifdef DEBUG
  473.         if(P_ASOLD < 0) printf("error... subtracting %d from %d\n",diff,P_ASOLD);
  474. #endif DEBUG
  475.         curntn->tmil-=diff;
  476.         curntn->tciv+=diff;
  477.         sct[curntn->capx][curntn->capy].people+=diff;
  478.         /*I add back gold as armies get redone anyway*/
  479.         curntn->metals += (diff* *(u_enmetal + (P_ATYPE%UTYPE)));
  480.         if(magic(country,WARRIOR)==TRUE) { /* WARRIOR power */
  481.             curntn->tgold+=(diff* *(u_encost + (P_ATYPE%UTYPE))) / 2;
  482.         } else curntn->tgold+=diff* *(u_encost + (P_ATYPE%UTYPE));
  483.     }
  484. #ifdef DEBUG
  485.     else printf("\tno action - P_ASOLD (%d) ~= ideal (%d)\n",P_ASOLD,ideal);
  486.     printf("\tFinal Garrison Army %d (%s) type is %s men is %d\n",armynum,curntn->name,*(unittype+(P_ATYPE)),P_ASOLD);
  487. #endif DEBUG
  488.  
  489.     /*build ships and/or armies*/
  490.     done=FALSE;
  491.     ideal = curntn->tciv * peace / (10 * MILRATIO);
  492.     if(curntn->tgold<0) { ideal*=4; ideal/=5; }
  493. #ifdef DEBUG
  494.     printf("\t%s total military is %d -> ideal is %d\n",curntn->name,curntn->tmil,ideal);
  495. #endif DEBUG
  496.     check();
  497.  
  498.     /* find leader and place on RULE in capitol */
  499.     for(armynum=0;armynum<MAXARM;armynum++)
  500.         if (P_ATYPE==getleader(curntn->class)-1) {
  501.             P_ASTAT=RULE;
  502.             P_AXLOC=curntn->capx;
  503.             P_AYLOC=curntn->capy;
  504.             break;
  505.         }
  506.  
  507.     /* add to partial armies */
  508.     for(armynum=1;armynum<MAXARM;armynum++)
  509.     if((P_ASOLD>0)
  510.     &&( P_ATYPE!=A_MILITIA )
  511.     &&( P_ATYPE<MINLEADER )
  512.     &&( P_ASOLD < TAKESECTOR )
  513.     &&( curntn->tgold > 0 )
  514.     &&( fort_val(&sct[P_AXLOC][P_AYLOC]) > 0)
  515.     &&( sct[P_AXLOC][P_AYLOC].owner == country )) {
  516. #ifdef DEBUG
  517.         printf("\tadding %d men to weakened army %d\n",TAKESECTOR+20-P_ASOLD,armynum);
  518. #endif DEBUG
  519.         if(magic(country,WARRIOR)==TRUE) /* WARRIOR power */
  520.         curntn->tgold-=((TAKESECTOR+20-P_ASOLD)*
  521.             *(u_encost + (P_ATYPE%UTYPE))) / 2;
  522.         else curntn->tgold-=(TAKESECTOR+20-P_ASOLD)*
  523.             *(u_encost + (P_ATYPE%UTYPE));
  524.         curntn->tmil += TAKESECTOR+20-P_ASOLD;
  525.         P_ASOLD = TAKESECTOR+20;
  526.     }
  527.  
  528.     /*if < ideal build new army in the capitol - if possible*/
  529.     if(curntn->tmil < ((4*ideal)/5)) {
  530.         for(armynum=1;armynum<MAXARM;armynum++)
  531.         if((done==FALSE)&&(P_ASOLD==0)) {
  532.             done=TRUE;
  533.             P_ATYPE = defaultunit(country);
  534.             P_ASOLD = min ((int) (ideal-curntn->tmil), (int) (curntn->metals/ (*(u_enmetal + (P_ATYPE%UTYPE)))));
  535.  
  536.             P_ASOLD = min (P_ASOLD,sct[curntn->capx][curntn->capy].people/2);
  537.             P_ASOLD = min (P_ASOLD, (int) (curntn->tgold/ *(u_encost+(P_ATYPE%UTYPE))));
  538.             if(P_ASOLD>0){
  539. #ifdef DEBUG
  540.                 printf("\tnot enough soldiers - build new army %d with %d men\n",armynum,P_ASOLD);
  541. #endif DEBUG
  542.                 curntn->metals-=(P_ASOLD* *(u_enmetal + (P_ATYPE%UTYPE)));
  543.                 P_AXLOC= curntn->capx;
  544.                 P_AYLOC= curntn->capy;
  545.                 curntn->tmil += P_ASOLD;
  546.                 curntn->tciv -= P_ASOLD;
  547.                 if(magic(country,WARRIOR)==TRUE) /* WARRIOR power */
  548.                     curntn->tgold-=(P_ASOLD* *(u_encost + (P_ATYPE%UTYPE))) / 2;
  549.                 else curntn->tgold-=P_ASOLD* *(u_encost + (P_ATYPE%UTYPE));
  550.                 sct[P_AXLOC][P_AYLOC].people-=P_ASOLD;
  551.                 P_ASTAT= DEFEND;
  552.                 P_AMOVE=0;
  553.             }
  554.             else P_ASOLD=0;
  555.         }
  556.         check();
  557.     } else if(curntn->tmil > (6*ideal/5)){
  558.         check();
  559.         /*disband a pseudo-random army*/
  560.         done=FALSE;
  561.         diff=curntn->tmil-(6*ideal/5);
  562.         for(armynum=1;done==FALSE && armynum<MAXARM;armynum++){
  563.             if((P_ASOLD<=0)
  564.             ||(P_ATYPE==A_MILITIA)
  565.             ||(P_ATYPE>=MINLEADER)
  566.             ||(P_ASTAT==ONBOARD)
  567.             ||(P_ASTAT==TRADED)) continue;
  568.  
  569.             if((sct[P_AXLOC][P_AYLOC].owner==country)
  570.             &&((sct[P_AXLOC][P_AYLOC].jewels>4)
  571.               ||(sct[P_AXLOC][P_AYLOC].metal>4)
  572.               ||(ISCITY(sct[P_AXLOC][P_AYLOC].designation)))){
  573. #ifdef DEBUG
  574.                 printf("\ttoo many soldiers eliminate army %d (%d men)\n",armynum,P_ASOLD);
  575. #endif DEBUG
  576.                 diff-=P_ASOLD;
  577.                 sct[P_AXLOC][P_AYLOC].people+=P_ASOLD;
  578.                 curntn->tmil -= P_ASOLD;
  579.                 curntn->tciv += P_ASOLD;
  580.                 P_ASOLD=0;
  581.                 if(diff<=50) done=TRUE;
  582.             }
  583.         }
  584.     }
  585.     check();
  586. #ifdef DEBUG
  587.     printf("\twhew... new tmil is %d\n",curntn->tmil);
  588. #endif DEBUG
  589.  
  590.     /*resize armies */
  591.     for(armynum=1;armynum<MAXARM;armynum++) if(P_ATYPE < MINLEADER) {
  592.         /*maximum npc army is 3*TAKESECTOR or 3*tmil/MAXARM */
  593.         /* also let militia get big */
  594.         if((P_ASOLD>(2*TAKESECTOR))
  595.         &&(P_ATYPE!=A_MILITIA && P_ASTAT!=ONBOARD && P_ASTAT!=TRADED)) {
  596.             free=FALSE;
  597.             for(i=1;free==FALSE && i<MAXARM;i++){
  598.                 if(curntn->arm[i].sold==0){
  599.                     free=TRUE;
  600.                     P_ASOLD/=2;
  601. #ifdef DEBUG
  602.     printf("\tSplitting %ld troops from army %d forming %s army %d \n"
  603.         ,P_ASOLD,armynum,unittype[P_ATYPE],i);
  604. #endif DEBUG
  605.                     curntn->arm[i].sold  = P_ASOLD;
  606.                     curntn->arm[i].unittyp = P_ATYPE;
  607.                     curntn->arm[i].smove = P_AMOVE;
  608.                     curntn->arm[i].stat  = P_ASTAT;
  609.                     curntn->arm[i].xloc  = P_AXLOC;
  610.                     curntn->arm[i].yloc  = P_AYLOC;
  611.                 }
  612.             }
  613.         }
  614.     /*minimum npc army is TAKESECTOR, merge them*/
  615.         else if(P_ASOLD>0 && (P_ASOLD<TAKESECTOR || P_ASTAT==MILITIA)
  616.         && P_ASTAT!=ONBOARD && P_ASTAT!=TRADED) {
  617.             free=FALSE;
  618.             for(i=1;free==FALSE && i<MAXARM;i++){
  619.                 if((curntn->arm[i].sold>0)
  620.                 &&(curntn->arm[i].stat!=ONBOARD)
  621.                 &&(i!=armynum)     /* don't use same army */
  622.                         /* or it will be deleted */
  623.                 &&(curntn->arm[i].stat!=TRADED)
  624.                 &&(curntn->arm[i].xloc==P_AXLOC)
  625.                 &&(curntn->arm[i].yloc==P_AYLOC)
  626.                 &&(curntn->arm[i].unittyp==P_ATYPE)) {
  627.                     free=TRUE;
  628.                     curntn->arm[i].sold += P_ASOLD;
  629. #ifdef DEBUG
  630.     printf("\tMerge %ld men from army %d to make %ld troops in %s army %d \n"
  631.         ,P_ASOLD,armynum,curntn->arm[i].sold,unittype[P_ATYPE],i);
  632. #endif DEBUG
  633.                     P_ASOLD=0;
  634.                 }
  635.  
  636.             }
  637.         }
  638.     }
  639.     check();
  640.  
  641.     /* assure that a militia unit resides in each city */
  642.     if(curntn->tgold > 0)
  643.     for(x=stx;x<endx;x++) for(y=sty;y<endy;y++)
  644.     if((sct[x][y].owner==country)
  645.     &&((sct[x][y].designation==DTOWN)||(sct[x][y].designation==DCITY)||(sct[x][y].designation==DCAPITOL))){
  646.         free=FALSE;
  647.         for(armynum=0;armynum<MAXARM;armynum++){
  648.             if((P_ASOLD>0)
  649.             &&(P_AXLOC==x)&&(P_AYLOC==y)
  650.             &&(P_ATYPE==A_MILITIA)) {
  651.                 free=TRUE;
  652.                 break;
  653.             }
  654.         }
  655.         if(free==FALSE) {     /* draft new militia army */
  656.             for(armynum=0;armynum<MAXARM;armynum++) if(P_ASOLD==0){
  657.                 P_AXLOC=x;
  658.                 P_AYLOC=y;
  659.                 free=TRUE;
  660.                 break;
  661.             }
  662.         }
  663.         if((free==TRUE)) {
  664.             /* want to add ideal troops */
  665.             ideal = sct[x][y].people/MILINCITY - P_ASOLD;
  666.             ideal = min(ideal, 250);
  667.             if (P_ASOLD < 50)    /* make the militia at least 50 */
  668.                 ideal = max(ideal,50-P_ASOLD);
  669.             if (P_ASOLD + ideal < 50) /*don't let ideal bring it below 50*/
  670.                 continue;
  671.             if(ideal>0){
  672.             if(magic(country,WARRIOR)==TRUE){ /* WARRIOR power */
  673.                 curntn->tgold-=
  674.                     (ideal* *(u_encost+P_ATYPE))/2;
  675.             } else {
  676.                 curntn->tgold-=
  677.                     ideal* *(u_encost + P_ATYPE);
  678.             }
  679.             }
  680.             P_ASOLD+=ideal;
  681.             P_ATYPE=A_MILITIA;
  682.             P_ASTAT=MILITIA;
  683. #ifdef DEBUG
  684.         printf("\tadding %ld troops to %s army %d (now %ld men - populace %ld)\n",ideal,unittype[P_ATYPE],armynum,P_ASOLD,sct[x][y].people);
  685. #endif DEBUG
  686.         }
  687.     }
  688.     check();
  689.  
  690.       /* setup default units */
  691.       for(armynum=1;armynum<MAXARM;armynum++) 
  692.     if((P_ASOLD>0)&&(P_ATYPE!=A_MILITIA)&&(P_ATYPE<MINLEADER)) 
  693.         P_ATYPE=defaultunit(country);
  694. }
  695.  
  696. /* getdstatus() - do diplomacy for current nation */
  697. void
  698. getdstatus()
  699. {
  700.     int x,oldstat[NTOTAL];
  701.     int X,Y;
  702.     int svhostile,hostile;    /* chance nation will react hostile */
  703.     int friendly;        /* chance nation will react favorably */
  704.  
  705.     if(!isnpc(curntn->active)) return;
  706.  
  707.     if(( curntn->active==GOOD_6FREE ) 
  708.          ||( curntn->active==ISOLATIONIST )
  709.          ||( curntn->active==NEUTRAL_6FREE )
  710.          ||( curntn->active==EVIL_6FREE )) svhostile=5;
  711.     else if(( curntn->active==GOOD_4FREE ) 
  712.          ||( curntn->active==NEUTRAL_4FREE )
  713.          ||( curntn->active==EVIL_4FREE )) svhostile=10;
  714.     else if(( curntn->active==GOOD_2FREE ) 
  715.          ||( curntn->active==NEUTRAL_2FREE )
  716.          ||( curntn->active==EVIL_2FREE )) svhostile=20;
  717.     else if(( curntn->active==GOOD_0FREE ) 
  718.          ||( curntn->active==NEUTRAL_0FREE )
  719.          ||( curntn->active==EVIL_0FREE )) svhostile=35;
  720.     else svhostile=5;
  721.  
  722.     for(x=1;x<NTOTAL;x++) if( isntn(ntn[x].active) ){
  723.         hostile = svhostile;
  724.         if(npctype(curntn->active) != npctype(ntn[x].active)) 
  725.             hostile+=20;    /* not same allignment */
  726.         friendly = 60-hostile;
  727.              if( curntn->active==ISOLATIONIST ) friendly -= 20;
  728.         /* negate impact of above line on neutrals */
  729.         if(isneutral(ntn[x].active)) {
  730.             friendly-=10;
  731.             hostile-=10;
  732.         }
  733.         if(ntn[x].race==curntn->race){
  734.             friendly+=10;
  735.             hostile-=10;
  736.         }
  737.         if(isneutral(curntn->active)) {
  738.             friendly-=20;
  739.             hostile-=20;
  740.         }
  741.         /* if next to capitol, they dont like you */
  742.         for(X=curntn->capx-1;X<=curntn->capx+1;X++)
  743.         for(Y=curntn->capy-1;Y<=curntn->capy+1;Y++) if(ONMAP(X,Y)) {
  744.             if(sct[X][Y].owner == x) {
  745.                 friendly-=10;
  746.                 hostile +=10;
  747.             }
  748.         }
  749.  
  750.         if( friendly < 0 )    friendly=0;
  751.         if( hostile < 0 )    hostile=0;
  752.  
  753.         oldstat[x] = curntn->dstatus[x];
  754.  
  755.         /* break bad treaties */
  756.         if(curntn->dstatus[x] == TREATY) {
  757.             if(ntn[x].dstatus[country]>=WAR) 
  758.                 curntn->dstatus[x] = JIHAD;
  759.             continue;
  760.         }
  761.         
  762.         if((curntn->dstatus[x] == JIHAD)
  763.         ||(curntn->dstatus[x]==UNMET)
  764.         ||(ispc(curntn->active)))
  765.             continue;
  766.  
  767.         /*if 4* mil and 4* score then not like them*/
  768.         if((ntn[x].tmil>4*curntn->tmil)
  769.         &&(ntn[x].score>4*curntn->score)){
  770.             if(curntn->dstatus[x]<WAR){
  771.                 if(rand()%100<=hostile)
  772.                     curntn->dstatus[x]++;
  773.             }
  774.         }
  775.         /*adjust based on your status with them*/
  776.         if((curntn->dstatus[x]==WAR)
  777.         &&(ntn[x].dstatus[country]<WAR))
  778.             if(rand()%100<=friendly) curntn->dstatus[x]--;
  779.  
  780.         if((curntn->dstatus[x]<WAR)
  781.         &&(curntn->dstatus[x]>ALLIED)){
  782.             if(ntn[x].dstatus[country]>1+curntn->dstatus[x]){
  783.                 if(rand()%100<=hostile)
  784.                     curntn->dstatus[x]++;
  785.             } else if(ntn[x].dstatus[country]+1<curntn->dstatus[x]){
  786.                 if(rand()%100<=friendly)
  787.                     curntn->dstatus[x]--;
  788.             }
  789.         }
  790.         if(rand()%100<= hostile) {
  791.             if((curntn->dstatus[x]!=JIHAD)
  792.             &&(curntn->dstatus[x]!=TREATY))
  793.                 curntn->dstatus[x]++;
  794.         }
  795.         if((rand()%100<= friendly)
  796.         &&(curntn->dstatus[x]!=TREATY)
  797.         &&(curntn->dstatus[x]!=JIHAD)
  798.         &&(curntn->dstatus[x]!=WAR)) curntn->dstatus[x]--;
  799.     }
  800.  
  801.     for(x=1;x<NTOTAL;x++) if(isntn( ntn[x].active ) ){
  802.         if((rand()%5==0)
  803.         &&(ntn[x].dstatus[country]==WAR)
  804.         &&(curntn->dstatus[x]==WAR)) {
  805.             ntn[x].dstatus[country]=HOSTILE;
  806.             curntn->dstatus[x]=HOSTILE;
  807.             fprintf(fnews,"2.\tnation %s and %s announce ceasefire\n",curntn->name,ntn[x].name);
  808.             if( isnotpc(ntn[x].active) ) continue;
  809.             mailopen(x);
  810.             fprintf(fm,"nation %s and you negotiate a ceasefire\n",curntn->name);
  811.             mailclose();
  812.         } else if((oldstat[x]==WAR)&&(curntn->dstatus[x]==WAR)){
  813.             fprintf(fnews,"2.\tnation %s stays at war with %s\n",curntn->name,ntn[x].name);
  814.         } else if((oldstat[x]<WAR)&&(curntn->dstatus[x]==WAR)){
  815.             fprintf(fnews,"2.\tnation %s goes to war with %s\n",curntn->name,ntn[x].name);
  816.             if( isnotpc(ntn[x].active) ) continue;
  817.             mailopen(x);
  818.             fprintf(fm,"nation %s goes to war with you\n",curntn->name);
  819.             mailclose();
  820.         } else if((oldstat[x]!=JIHAD)&&(curntn->dstatus[x]==JIHAD)){
  821.             fprintf(fnews,"2.\tnation %s announces a jihad with %s\n",curntn->name,ntn[x].name);
  822.             if( isnotpc(ntn[x].active) ) continue;
  823.             mailopen(x);
  824.             fprintf(fm,"nation %s announces a jihad with you\n",curntn->name);
  825.             mailclose();
  826.         }
  827.     }
  828. }
  829.  
  830. void
  831. nationrun()
  832. {
  833.     int goldthresh,metalthresh,citythresh,useful;
  834.     int armynum,loop;
  835.     int x,y,i,p;
  836.     float    hunger;
  837.     long zz;
  838.     check();
  839.     prep(country,FALSE);
  840.  
  841.     for(x=0;x<MAPX;x++) for(y=0;y<MAPY;y++) attr[x][y]=0;
  842.  
  843.     /* is there an error*/
  844.     if((sct[curntn->capx][curntn->capy].owner==country)
  845.     &&(sct[curntn->capx][curntn->capy].designation!=DCAPITOL)){
  846.         sct[curntn->capx][curntn->capy].designation=DCAPITOL;
  847.     }
  848.  
  849.     if( ispc( curntn->active )) {
  850.         stx=sty=0;
  851.         endx=MAPX;
  852.         endy=MAPY;
  853.     } else {
  854.         if( curntn->capx > NPCTOOFAR )
  855.             stx=curntn->capx-NPCTOOFAR;
  856.         else    stx=0;
  857.         if( curntn->capy > NPCTOOFAR )
  858.             sty=curntn->capy-NPCTOOFAR;
  859.         else    sty=0;
  860.         if( curntn->capx + NPCTOOFAR < MAPX )
  861.             endx=curntn->capx+NPCTOOFAR;
  862.         else    endx=MAPX;
  863.         if( curntn->capy + NPCTOOFAR < MAPY )
  864.             endy=curntn->capy+NPCTOOFAR;
  865.         else    endy=MAPY;
  866.     }
  867.  
  868.     getdstatus();
  869.  
  870. #ifdef SPEW
  871.     for(x=1;x<NTOTAL;x++) if(isntn( ntn[x].active )) {
  872.         /* here is the bit which will occasionally send a randomly
  873.            generated message from a hostile NPC to a PC (25% chance) */
  874.         if((curntn->dstatus[x] >= HOSTILE) 
  875.         && (ispc(ntn[x].active))) 
  876.         if (rand()%4 == 0) {    /* send the message!! */
  877.             printf("Sent message to %s\n",ntn[x].name);
  878.             mailopen(x);
  879.             fprintf(fm,"Message to %s from %s (%s of year %d)\n\n"
  880.              ,ntn[x].name,curntn->name,PSEASON(TURN),YEAR(TURN));
  881.             makemess(rand()%5 +1,fm);
  882.             mailclose();
  883.         }
  884.     }
  885. #endif SPEW
  886.     /*move units */
  887.     /*are they at war with any normal countries*/
  888.     peace=0;
  889.     for(i=1;i<NTOTAL;i++) 
  890.     if(isntn(ntn[i].active)&&(curntn->dstatus[i]>peace)) {
  891.         peace=curntn->dstatus[i];
  892.         if( peace>= WAR) break;
  893.     }
  894.  
  895.     if(peace<WAR){
  896.         peace=8;
  897.         pceattr();
  898.     } else {
  899.     /*if war then attack &/or expand */
  900.         peace=12;
  901.         /*are they attacking or defending */
  902.         if(curntn->tmil==0) defattr();
  903.         else for(x=0;x<NTOTAL;x++) 
  904.         if(isntn( ntn[x].active ) && (curntn->dstatus[x]>HOSTILE)){
  905.             if(100*(curntn->tmil*(curntn->aplus+100))/((curntn->tmil*(curntn->aplus+100))+(ntn[x].tmil*(ntn[x].dplus+100)))>rand()%100){
  906.                 /*attacker*/
  907.                 for(armynum=1;armynum<MAXARM;armynum++)
  908.                     if((P_ASOLD>0)&&(P_ATYPE!=A_MILITIA)
  909.                     &&(P_ASTAT!=ONBOARD)&&(P_ASTAT!=TRADED)
  910.                     &&(P_ASTAT<NUMSTATUS)&&(P_ASTAT!=GENERAL))
  911.                         P_ASTAT=ATTACK;
  912.                 atkattr();
  913.             } else {    /*defender*/
  914.                 for(armynum=1;armynum<MAXARM;armynum++)
  915.                     if((P_ASOLD>0)&&(P_ATYPE!=A_MILITIA)
  916.                     &&(P_ASTAT!=ONBOARD)&&(P_ASTAT!=TRADED)
  917.                     &&(P_ASTAT<NUMSTATUS)&&(P_ASTAT!=GENERAL)){
  918.                         if(P_ASOLD<350) P_ASTAT=DEFEND;
  919.                         else P_ASTAT=ATTACK;
  920.                     }
  921.                 defattr();
  922.             }
  923.         }
  924.     }
  925.     check();
  926.  
  927.     /* move infantry then leader/monsters */
  928.     n_people(TRUE);            /* add to attr for people */
  929.  
  930.     if( country < 5 ) prtattr();
  931.  
  932.     loop=0;
  933.     for(armynum=1;armynum<MAXARM;armynum++)
  934.         if((P_ASOLD!=0)&&(P_ATYPE<MINLEADER)) loop+=armymove(armynum);
  935.     n_people(FALSE);        /* subtract to attr for people */
  936.     for(armynum=1;armynum<MAXARM;armynum++)
  937.         if((P_ASOLD!=0)&&(P_ATYPE>=MINLEADER)) loop+=armymove(armynum);
  938.  
  939.     /* NPC ACTIVE STATUS CHANGE */
  940.     if(isnpc(curntn->active)
  941.     &&(curntn->active != ISOLATIONIST)) {
  942.         if(isgood(curntn->active)) {
  943.             if(loop<=1)    curntn->active=GOOD_0FREE;
  944.             else if(loop>=6) curntn->active=GOOD_6FREE;
  945.             else if(loop>=4) curntn->active=GOOD_4FREE;
  946.             else    curntn->active=GOOD_2FREE;
  947.         } else if(isneutral(curntn->active)) {
  948.             if(loop<=1)    curntn->active=NEUTRAL_0FREE;
  949.             else if(loop>=6) curntn->active=NEUTRAL_6FREE;
  950.             else if(loop>=4) curntn->active=NEUTRAL_4FREE;
  951.             else    curntn->active=NEUTRAL_2FREE;
  952.         } else if(isevil(curntn->active)) {
  953.             if(loop<=1)    curntn->active=EVIL_0FREE;
  954.             else if(loop>=6) curntn->active=EVIL_6FREE;
  955.             else if(loop>=4) curntn->active=EVIL_4FREE;
  956.             else    curntn->active=EVIL_2FREE;
  957.         }
  958.     }
  959.  
  960.     if( curntn->tgold > curntn->tciv ) curntn->charity=10;
  961.     else curntn->charity=0;
  962.  
  963.     /* INTELLIGENT SECTOR REDESIGNATION */
  964.     /* note that only redesignate pc's if not designated yet */
  965.     goldthresh=4;
  966.     metalthresh=4;
  967.     citythresh=10;
  968.     hunger = 5.0;
  969.     for(loop=1;loop<5;loop++) {
  970. #ifdef DEBUG
  971.         printf("\tnpcredes(): country %s gold=%d metal=%d, city=%d hunger=%f\n",curntn->name,goldthresh,metalthresh,citythresh,hunger);
  972. #endif DEBUG
  973.  
  974.         useful=FALSE;
  975.         for(x=stx;x<endx;x++) for(y=sty;y<endy;y++)
  976.         if((sct[x][y].owner==country)
  977.         &&(is_habitable(x,y))
  978.         &&((isnotpc(curntn->active)) 
  979.           ||(sct[x][y].designation==DNODESIG))) {
  980.             n_redes(x,y,goldthresh,metalthresh,citythresh,hunger);
  981.             useful=TRUE;
  982.         }
  983.         if(useful==FALSE) break;
  984.  
  985.         spreadsheet(country);
  986.  
  987.         hunger = spread.food/((float)(spread.civilians+2*curntn->tmil));
  988.         if(hunger < P_EATRATE ) {
  989.             goldthresh++;
  990.             metalthresh++;
  991.             citythresh--;
  992.         } else if(hunger > 2*P_EATRATE) {
  993.             if( goldthresh==1 && metalthresh==1 ) break;
  994.             goldthresh-=2;
  995.             metalthresh-=2;
  996.             citythresh+=2;
  997.         } else {
  998.             if( goldthresh==1 && metalthresh==1 ) break;
  999.             if(rand()%2==0) goldthresh--;
  1000.             else metalthresh--;
  1001.             if(goldthresh==0) goldthresh=1;
  1002.             if(metalthresh==0) metalthresh=1;
  1003.             citythresh++;
  1004.         }
  1005.         if(goldthresh<=0) goldthresh=1;
  1006.         if(metalthresh<=0) metalthresh=1;
  1007.     }
  1008.  
  1009.     /*build forts in any cities*/
  1010.     if(isnotpc(curntn->active)) for(x=stx;x<endx;x++) for(y=sty;y<endy;y++)
  1011.         if((sct[x][y].owner==country)&&
  1012.             ((sct[x][y].designation==DTOWN)
  1013.             ||(sct[x][y].designation==DCITY)
  1014.             ||(sct[x][y].designation==DCAPITOL)
  1015.             ||(sct[x][y].designation==DFORT))
  1016.             &&(sct[x][y].fortress<10)
  1017.             &&(curntn->tgold>10000)
  1018.             &&(rand()%5==0)
  1019.             &&(sct[x][y].fortress<(sct[x][y].people%1000)))
  1020.                 sct[x][y].fortress++;
  1021.     check();
  1022.     /*redo mil*/
  1023.     if(isnotpc(curntn->active)) redomil();
  1024.     check();
  1025.  
  1026.     /*buy new powers and/or new weapons*/
  1027.     if(getmgkcost(M_MIL,country) < getmgkcost(M_CIV,country)){
  1028.         if(curntn->jewels > getmgkcost(M_MIL,country)) {
  1029.             curntn->jewels-=getmgkcost(M_MIL,country);
  1030.             if((zz=getmagic(M_MIL))!=0){
  1031.                 for(p=S_MIL;p<E_MIL;p++) if(powers[p]==zz){
  1032.                     fprintf(fnews,"1.\tnation %s gets combat power %s\n",curntn->name,pwrname[p]);
  1033.                     printf("\tnation %s gets combat power %s\n",curntn->name,pwrname[p]);
  1034.                     break;
  1035.                 }
  1036.                 exenewmgk(zz);
  1037.             } else if((zz=getmagic(M_MIL))!=0){
  1038.                 for(p=S_MIL;p<E_MIL;p++) if(powers[p]==zz){
  1039.                     fprintf(fnews,"1.\tnation %s gets combat power %s\n",curntn->name,pwrname[p]);
  1040.                     printf("\tnation %s gets combat power %s\n",curntn->name,pwrname[p]);
  1041.                     break;
  1042.                 }
  1043.                 exenewmgk(zz);
  1044.             }
  1045.             else    curntn->jewels+=getmgkcost(M_MIL,country);
  1046.         }
  1047.     } else {
  1048.         if(curntn->jewels > getmgkcost(M_CIV,country)) {
  1049.             curntn->jewels-=getmgkcost(M_CIV,country);
  1050.             if((zz=getmagic(M_CIV))!=0){
  1051.                 for(p=S_CIV;p<S_CIV+E_CIV;p++) if(powers[p]==zz){
  1052.                     fprintf(fnews,"1.\tnation %s gets civilian power %s\n",curntn->name,pwrname[p]);
  1053.                     printf("\tnation %s gets civilian power %s\n",curntn->name,pwrname[p]);
  1054.                     break;
  1055.                 }
  1056.                 exenewmgk(zz);
  1057.             }
  1058.             else if((zz=getmagic(M_CIV))!=0){
  1059.                 for(p=S_CIV;p<S_CIV+E_CIV;p++) if(powers[p]==zz){
  1060.                     fprintf(fnews,"1.\tnation %s gets civilian power %s\n",curntn->name,pwrname[p]);
  1061.                     printf("\tnation %s gets civilian power %s\n",curntn->name,pwrname[p]);
  1062.                     break;
  1063.                 }
  1064.                 exenewmgk(zz);
  1065.             }
  1066.             else    curntn->jewels+=getmgkcost(M_CIV,country);
  1067.         }
  1068.     }
  1069.  
  1070.     check();
  1071.     if(magic(country,VAMPIRE)!=TRUE) {
  1072.         i=0;
  1073.         if(magic(country,WARLORD)==TRUE) i=30;
  1074.         else if(magic(country,CAPTAIN)==TRUE) i=20;
  1075.         else if(magic(country,WARRIOR)==TRUE) i=10;
  1076.         x = max ( curntn->aplus-i, 10 ) / 10;
  1077.         x *= x;
  1078.         if( curntn->race==ORC) x*=2;
  1079.         /* SHOULD USE spread.metal but it didnt work right */
  1080.         if(rand()%2==0)
  1081.         if(curntn->metals >  3 * METALORE * curntn->tmil*x){
  1082.             curntn->aplus+=1;
  1083.             curntn->metals-=METALORE*curntn->tmil*x;
  1084.             printf("\tnation %s buys +1 percent attack\n",curntn->name);
  1085.         }
  1086.         x = max ( curntn->dplus-i, 10 ) / 10;
  1087.         x *= x;
  1088.         if( curntn->race==ORC) x*=2;
  1089.         if(curntn->metals >  3 * METALORE * curntn->tmil*x){
  1090.             curntn->dplus+=1;
  1091.             curntn->metals-=METALORE*curntn->tmil*x;
  1092.             printf("\tnation %s buys +1 percent defence\n",curntn->name);
  1093.         }
  1094.     }
  1095.     /* don't allow status ATTACK from own city */
  1096.     for(armynum=0;armynum<MAXARM;armynum++) {
  1097.         if (P_ASOLD<=0 || P_ASTAT!=ATTACK) continue;
  1098.         if ((sct[P_AXLOC][P_AYLOC].owner==country)
  1099.         && (fort_val(&sct[P_AXLOC][P_AYLOC]) > 0)) {
  1100.             if(rand()%2==0) P_ASTAT=DEFEND;
  1101.             else P_ASTAT=GARRISON;
  1102.         }
  1103.     }
  1104. }
  1105.  
  1106. /* dont allow npcs to trespass onto other nations land */
  1107. void
  1108. n_trespass()
  1109. {
  1110.     register int x,y;
  1111.     for(x=stx;x<endx;x++) for(y=sty;y<endy;y++)  {
  1112.         if((sct[x][y].owner != country )
  1113.         &&( sct[x][y].owner != 0 )
  1114.         &&( abs(x-curntn->capx)>2 )
  1115.         &&( abs(y-curntn->capy)>2 )
  1116.         &&( ntn[country].dstatus[sct[x][y].owner]<WAR)
  1117.         &&( ntn[sct[x][y].owner].dstatus[country]<WAR)
  1118.         &&( ntn[country].dstatus[sct[x][y].owner]>ALLIED))
  1119.             attr[x][y]=1;
  1120.     }
  1121. }
  1122.  
  1123. /* you are too far from capitol */
  1124. void
  1125. n_toofar()
  1126. {
  1127.     register int x,y;
  1128.     for(x=0;x<MAPX;x++) for(y=0;y<MAPY;y++) {
  1129.         if( x<stx || y<sty || x>=endx || y>=endy )
  1130.             attr[x][y]=1;
  1131.     }
  1132. }
  1133.  
  1134. /* take undefended land */
  1135. void
  1136. n_unowned()
  1137. {
  1138.     register int x,y;
  1139.  
  1140.     /* around capitol */
  1141.     for(x=curntn->capx-4;x<=curntn->capx+4;x++){
  1142.         for(y=curntn->capy-4;y<=curntn->capy+4;y++){
  1143.             if((ONMAP(x,y))&&(sct[x][y].owner==0)) {
  1144.                 attr[x][y] += 700;
  1145.             }
  1146.         }
  1147.     }
  1148.  
  1149.     for(x=stx;x<endx;x++) {
  1150.         for(y=sty;y<endy;y++) {
  1151.             /* add if metal high */
  1152.             if(sct[x][y].tradegood != TG_none) {
  1153.                 if(sct[x][y].metal != 0) attr[x][y]+=500;
  1154.                 else if(sct[x][y].jewels != 0) attr[x][y]+=500;
  1155.                 else attr[x][y]+=300;
  1156.             }
  1157.             /*add to attractiveness for unowned sectors*/
  1158.             if(sct[x][y].owner == 0) {
  1159.                 attr[x][y]+=300;
  1160.             } else    {
  1161.             if(ntn[sct[x][y].owner].active == NPC_NOMAD)
  1162.                 attr[x][y]+=100;
  1163.             }
  1164.             attr[x][y] += 50*tofood(&sct[x][y],country); 
  1165.             if(!is_habitable(x,y)) attr[x][y] /= 5;
  1166.         }
  1167.     }
  1168. }
  1169. void
  1170. n_defend(natn)
  1171. register short natn;
  1172. {
  1173.     int x,y;
  1174.  
  1175.     /* add 1/10th of their soldiers in sector */
  1176.     for(x=1;x<MAXARM;x++) if(ntn[natn].arm[x].sold > 0)
  1177.         if(sct[ntn[natn].arm[x].xloc][ntn[natn].arm[x].yloc].owner==country)
  1178.             attr[ntn[natn].arm[x].xloc][ntn[natn].arm[x].yloc] +=
  1179.                 ntn[natn].arm[x].sold/10;
  1180.  
  1181.     /*plus 80 if near your capitol */
  1182.     for(x=curntn->capx-1;x<=curntn->capy+1;x++){
  1183.         for(y=curntn->capy-1;y<=curntn->capy+1;y++){
  1184.             if(ONMAP(x,y)) attr[x][y]+=80;
  1185.         }
  1186.     }
  1187.  
  1188.     /*plus based on defensive value and population */
  1189.     for(x=stx;x<endx;x++) for(y=sty;y<endy;y++) {
  1190.         if(movecost[x][y]==1) attr[x][y] += 50;
  1191.         else if(movecost[x][y]<=3) attr[x][y] += 20;
  1192.         else if(movecost[x][y]<=5) attr[x][y] += 10;
  1193.  
  1194.          if(sct[x][y].owner==country){
  1195.             if(ISCITY(sct[x][y].designation))
  1196.                 attr[x][y] += 50;
  1197.             /* should spread 3000 points over country */
  1198.             attr[x][y]+=3000*sct[x][y].people/ntn[country].tciv;
  1199.         }
  1200.     }
  1201. }
  1202.  
  1203. void
  1204. n_attack(nation)
  1205. register short nation;
  1206. {
  1207.     register int x,y;
  1208.     int    armynum;
  1209.     long    solds;    /* solds within 1 of capitol or city */
  1210.  
  1211.     for(x=stx;x<endx;x++) for(y=sty;y<endy;y++){
  1212.         if((sct[x][y].owner==nation)&&
  1213.         ((sct[x][y].designation==DCITY)
  1214.         ||(sct[x][y].designation==DCAPITOL)
  1215.         ||(sct[x][y].designation==DTOWN))){
  1216.             solds=0;
  1217.             for(armynum=1;armynum<MAXARM;armynum++) 
  1218.                 if((ntn[country].arm[armynum].sold > 0)
  1219.                 &&(abs(AXLOC-x)<=1)
  1220.                 &&(abs(AYLOC-y)<=1)) solds+=ASOLD;
  1221.  
  1222.             if(solds_in_sector(x,y,nation)*2 < 3*solds)
  1223.                 attr[x][y]+=500;
  1224.         }
  1225.     }
  1226. }
  1227.  
  1228. /* +100 if undefended sectors of nation, +60 if not */
  1229. void
  1230. n_undefended( nation )
  1231. {
  1232.     register int x,y;
  1233.     for(x=stx;x<endx;x++) for(y=sty;y<endy;y++) if(sct[x][y].owner==nation){
  1234.         if(is_habitable(x,y) == 0) {
  1235.             attr[x][y] += 30;
  1236.         } else if(occ[x][y]==0) {
  1237.             attr[x][y]+=100;
  1238.         } else {
  1239.             attr[x][y]+=60;
  1240.         }
  1241.     }
  1242. }
  1243.  
  1244. /* add 1/2 of people in owned sectors for small armies */
  1245. void
  1246. n_people(doadd)
  1247. int doadd;    /* TRUE if adding, FALSE if subtracting */
  1248. {
  1249.     register int x,y;
  1250.     for(x=stx;x<endx;x++) for(y=sty;y<endy;y++)
  1251.     if(sct[x][y].owner==country){
  1252.         if(is_habitable(x,y)){
  1253.             if(doadd==TRUE) {
  1254.                 attr[x][y] += sct[x][y].people/4;
  1255.             } else    attr[x][y] -= sct[x][y].people/4;
  1256.         }
  1257.     }
  1258. }
  1259.  
  1260. /* +60 if between two capitols */
  1261. void
  1262. n_between(nation)
  1263. int nation;
  1264. {
  1265.     int x1,x2,y1,y2,x,y;
  1266.  
  1267.     /*plus if strategic blocking sector*/
  1268.  
  1269.     /*+60 if between the two capitol*/
  1270.     if (ntn[nation].capx < curntn->capx){
  1271.         x1=ntn[nation].capx;
  1272.         x2=curntn->capx;
  1273.     } else {
  1274.         x1=curntn->capx;
  1275.         x2=ntn[nation].capx;
  1276.     }
  1277.     if (ntn[nation].capy < curntn->capy){
  1278.         y1=ntn[nation].capy;
  1279.         y2=curntn->capy;
  1280.     }
  1281.     else {
  1282.         y1=curntn->capy;
  1283.         y2=ntn[nation].capy;
  1284.     }
  1285.  
  1286.     for(x=x1;x<=x2;x++) for(y=y1;y<=y2;y++) {
  1287.         if(ONMAP(x,y)) attr[x][y]+=60;
  1288.     }
  1289. }
  1290.  
  1291. /* if in jeopardy, move to survive
  1292.  *    if within two of cap add 1/5th of men
  1293.  *    if on cap and war and 2x your garrison go jihad and + 1/2 men
  1294.  */
  1295. void
  1296. n_survive()
  1297. {
  1298.     int nation,armynum;
  1299.     int capx,capy;
  1300.  
  1301.     capx=curntn->capx;
  1302.     capy=curntn->capy;
  1303.  
  1304.     if(sct[capx][capy].owner!=country){
  1305.         attr[capx][capy]=1000;
  1306.     }
  1307.  
  1308.     /*defend your capitol if occupied, +50 more if with their army*/
  1309.     for(nation=1;nation<NTOTAL;nation++)
  1310.     if((isntn(ntn[nation].active))
  1311.     &&((ntn[nation].dstatus[country]>=WAR)
  1312.       ||(curntn->dstatus[nation]>=WAR))){
  1313.         for(armynum=1;armynum<MAXARM;armynum++)
  1314.         if((ntn[nation].arm[armynum].sold > 0) 
  1315.         &&( ntn[nation].arm[armynum].xloc<=capx+2)
  1316.         &&( ntn[nation].arm[armynum].xloc>=capx-2)
  1317.         &&( ntn[nation].arm[armynum].yloc<=capy+2)
  1318.         &&( ntn[nation].arm[armynum].yloc>=capy-2)){
  1319.             if((ntn[nation].arm[armynum].xloc==capx)
  1320.             &&(ntn[nation].arm[armynum].yloc==capy)){
  1321.                 attr[capx][capy]+=2*ntn[nation].arm[armynum].sold;
  1322.             } else {
  1323.                 attr[ntn[nation].arm[armynum].xloc][ntn[nation].arm[armynum].yloc]+=ntn[nation].arm[armynum].sold;
  1324.             }
  1325.         }
  1326.     }
  1327. }
  1328.  
  1329. void
  1330. defattr()
  1331. {
  1332.     int nation;
  1333.  
  1334. #ifdef DEBUG
  1335.     printf("atkattr()\n");
  1336. #endif DEBUG
  1337.  
  1338.     n_unowned();
  1339.  
  1340.     for(nation=1;nation<NTOTAL;nation++)
  1341.     if((isntn(ntn[nation].active))&&(curntn->dstatus[nation]>=WAR)) {
  1342.         n_defend(nation);
  1343.         n_between(nation);
  1344.         n_undefended(nation);
  1345.     }
  1346.  
  1347.     n_trespass();
  1348.     n_toofar();
  1349.     n_survive();
  1350. }
  1351.  
  1352. /*calculate attractiveness of attacking sectors*/
  1353. void
  1354. atkattr()
  1355. {
  1356.     int nation;
  1357. #ifdef DEBUG
  1358.     printf("atkattr()\n");
  1359. #endif DEBUG
  1360.  
  1361.     n_unowned();
  1362.  
  1363.     /*adjust for each nation that you are at war with*/
  1364.     for(nation=1;nation<NTOTAL;nation++) if( isntn(ntn[nation].active) ){
  1365.         if(curntn->dstatus[nation]==WAR) {
  1366.             n_between(nation);
  1367.             n_undefended(nation);
  1368.             n_attack(nation);
  1369.         } else if(curntn->dstatus[nation]==JIHAD) {
  1370.             n_attack(nation);
  1371.             n_attack(nation);
  1372.             n_between(nation);
  1373.             n_undefended(nation);
  1374.             n_attack(nation);
  1375.             n_between(nation);
  1376.             n_undefended(nation);
  1377.             n_attack(nation);
  1378.         }
  1379.     }
  1380.  
  1381.     n_toofar();
  1382.     n_trespass();
  1383.     n_survive();
  1384. }
  1385.  
  1386. /*calculate attractiveness when at peace*/
  1387. void
  1388. pceattr()
  1389. {
  1390. #ifdef DEBUG
  1391.     printf("pceattr()\n");
  1392. #endif DEBUG
  1393.     n_unowned();
  1394.     n_unowned();
  1395.     n_trespass();
  1396.     n_toofar();
  1397.     n_survive();
  1398. }
  1399. #endif NPC
  1400.