forked from vejeta/conquer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnpc.c
More file actions
1720 lines (1579 loc) · 46.4 KB
/
npc.c
File metadata and controls
1720 lines (1579 loc) · 46.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*conquer : Copyright (c) 1988 by Ed Barlow.
* I spent a long time writing this code & I hope that you respect this.
* I give permission to alter the code, but not to copy or redistribute
* it without my explicit permission. If you alter the code,
* please document changes and send me a copy, so all can have it.
* This code, to the best of my knowledge works well, but it is my first
* 'C' program and should be treated as such. I disclaim any
* responsibility for the codes actions (use at your own risk). I guess
* I am saying "Happy gaming", and am trying not to get sued in the process.
* Ed
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curses.h>
#include <ctype.h>
#include "header.h"
#include "data.h"
extern FILE *fnews;
int stx, sty, endx, endy; /* npc range of operations */
extern short country;
extern int **attr; /*sector attactiveness*/
extern int dissarray; /* has nation lost its leader */
int peace; /*is 8 if at peace, 12 if at war*/
static int Avg_food;
static int Avg_soldiers[NTOTAL];
static int Avg_tradegood;
/* macros that indicate what the country can see */
#ifdef NPC_SEE_SECTORS
#define SEE_SECTOR(x,y,country) TRUE
#else /* NPC_SEE_SECTORS */
#define SEE_SECTOR(x,y,country) \
( (magic(sct[x][y].owner,THE_VOID)!=TRUE) \
|| (magic(country,NINJA)==TRUE) )
#endif /* NPC_SEE_SECTORS */
#ifdef NPC_SEE_CITIES
#define SEE_CITIES(city_nation,country) TRUE
#else /* NPC_SEE_CITIES */
#define SEE_CITIES(city_nation,country) \
( (magic(city_nation,THE_VOID)!=TRUE) \
|| (magic(country,NINJA)==TRUE) )
#endif /* NPC_SEE_CITIES */
#ifdef NPC_COUNT_ARMIES
#define COUNT_ARMIES(army_nation,country) TRUE
#else /* NPC_COUNT_ARMIES */
#define COUNT_ARMIES(army_nation,country) \
( ( (magic(army_nation,THE_VOID)!=TRUE) \
&& (magic(army_nation,HIDDEN)!=TRUE) \
) || (magic(country,NINJA)==TRUE) )
#endif /* NPC_COUNT_ARMIES */
/* When a sector cannot be seen by an npc nation because of
* THE_VOID, a value must still be given to the
* sector, because there MIGHT be something there.
* The following unseen (UNS_) value represents this.
*/
#define UNS_CITY_VALUE 10 /* If there is an under-defended City
* the value is 500
*/
void
prtattr()
{
#ifdef DEBUG
int x,y;
/* FOR TESTING OF WHAT THE ATTRACTIVENESS ENDS UP LIKE */
printf("Attractiveness for sectors around CAPITAL at %d %d veg alt des\n",curntn->capx,curntn->capy);
for(y=(int)curntn->capy-3;y<(int)curntn->capy+4;y++) {
printf("\n");
for(x=(int)curntn->capx-3;x<(int)curntn->capx+4;x++) if(ONMAP(x,y)) {
if( sct[x][y].altitude != WATER ) {
printf("[%2d,%2d] ",x,y);
}
}
printf("\n");
for(x=(int)curntn->capx-3;x<(int)curntn->capx+4;x++) if(ONMAP(x,y)) {
if( sct[x][y].altitude != WATER ) {
printf("%5d %c%c%c "
,attr[x][y]
,sct[x][y].vegetation
,sct[x][y].altitude
,sct[x][y].designation);
} else printf("0 ~ ");
}
printf("\n");
}
#endif /* DEBUG */
}
/*newdip() diplomacy if unmet - ntn 1 is nation you are updating*/
void
newdip(ntn1,ntn2)
int ntn1,ntn2;
{
if( ispc( ntn[ntn1].active ) ) {
if( ntn[ntn2].race==ORC )
ntn[ntn1].dstatus[ntn2]=HOSTILE;
else ntn[ntn1].dstatus[ntn2]=NEUTRAL;
return;
}
if((ntn[ntn1].race==ORC)
||( ntn[ntn2].race==ORC)) {
if(ntn[ntn1].dstatus[ntn2]==UNMET)
if((rand()%2==0)|| ispc(ntn[ntn1].active))
ntn[ntn1].dstatus[ntn2]=HOSTILE;
else
ntn[ntn1].dstatus[ntn2]=WAR;
} else if(ismonst(ntn[ntn2].active)) {
ntn[ntn1].dstatus[ntn2]=WAR;
} else if(ispc(ntn[ntn1].active)) {
if(ntn[ntn1].dstatus[ntn2]==UNMET)
ntn[ntn1].dstatus[ntn2]=NEUTRAL;
} else if(ntn[ntn1].race==ntn[ntn2].race){
if(rand()%2<1) ntn[ntn1].dstatus[ntn2]=FRIENDLY;
else ntn[ntn1].dstatus[ntn2]=NEUTRAL;
} else ntn[ntn1].dstatus[ntn2]=NEUTRAL;
}
#ifdef MONSTER
void
monster()
{
for(country=1;country<NTOTAL;country++) {
curntn = &ntn[country];
if( curntn->active==NPC_NOMAD ) do_nomad();
else if( curntn->active==NPC_PIRATE ) do_pirate();
else if( curntn->active==NPC_SAVAGE ) do_savage();
else if( curntn->active==NPC_LIZARD ) do_lizard(); /* update.c */
}
#ifdef MORE_MONST
{
int neededtroops;
int actualtroops;
int savages,nomads;
int i,x,y;
int nomad_space=1, savage_space=1;
for(i=1;i<NTOTAL;i++)
switch(ntn[i].active)
{
case NPC_SAVAGE:
savages=i;
break;
case NPC_NOMAD:
nomads=i;
break;
}
/* neededarmies is an estimete of
* the desired military strength of savages
* and nomads. The proportions are based on
* npc.c routines. yeuch.
*/
neededtroops= ((NUMSECTS)/MONSTER)*( /* number of armies */
(5.0/12)*450 + /* nomads */
(1.0/4)*250 ); /* savages */
actualtroops=0;
for(i=0;i<MAXARM;i++)
{
if (ntn[nomads].arm[i].sold > 0)
actualtroops += ntn[nomads].arm[i].sold;
if (ntn[savages].arm[i].sold > 0)
actualtroops += ntn[savages].arm[i].sold;
}
#ifdef DEBUG
printf("monsters: need %d have %d\n",neededtroops,actualtroops);
#endif /* DEBUG */
neededtroops= neededtroops-actualtroops;
while(neededtroops>0&& (nomad_space||savage_space))
{
#ifdef DEBUG
printf("\t need %d monster troops\n",neededtroops);
#endif /* */
if ( (rand()%8)<5 && nomad_space)
{
while(!is_habitable((x=(rand()%(MAPX-8))+4),
(y=(rand()%(MAPY-8))+4)))
;
for(i=0;i<MAXARM;i++)
if ( ntn[nomads].arm[i].sold <=0 )
goto found_free_nomad_army;
#ifdef DEBUG
printf("\t\t Out of nomad space\n");
#endif
nomad_space=0;
continue;
found_free_nomad_army:
ntn[nomads].arm[i].xloc=x;
ntn[nomads].arm[i].yloc=y;
ntn[nomads].arm[i].sold=100+100*(rand()%6);
ntn[nomads].arm[i].unittyp=A_LT_CAV;
ntn[nomads].arm[i].stat=ATTACK;
neededtroops -= ntn[nomads].arm[i].sold;
#ifdef DEBUG
printf("\t\tAdding nomad army %d size %d at (%d,%d)\n",i,
ntn[nomads].arm[i].sold,x,y);
#endif
}
else
{
do {
x=(rand()%(MAPX-8))+4;
y=(rand()%(MAPY-8))+4;
} while ( sct[x][y].altitude == PEAK ||
sct[x][y].altitude == WATER ||
( sct[x][y].owner !=0 &&
sct[x][y].owner != savages &&
sct[x][y].people >= 50 ) );
for(i=0;i<MAXARM;i++)
if ( ntn[savages].arm[i].sold <=0 )
goto found_free_savage_army;
#ifdef DEBUG
printf("\t\t Out of savage space\n");
#endif
savage_space=0;
continue;
found_free_savage_army:
ntn[savages].arm[i].xloc=x;
ntn[savages].arm[i].yloc=y;
ntn[savages].arm[i].sold=100+100*(rand()%3);
ntn[savages].arm[i].unittyp=defaultunit(savages);
ntn[savages].arm[i].stat=ATTACK;
neededtroops -= ntn[savages].arm[i].sold;
#ifdef DEBUG
printf("\t\tAdding savage army %d size %d at (%d,%d)\n",i,
ntn[savages].arm[i].sold,x,y);
#endif
}
}
}
#endif /* MORE_MONST */
}
void
do_nomad()
{
int count;
short armynum;
int x, y;
/*move nomads */
printf("updating nomad (nation %d)\n",country);
for(armynum=0;armynum<MAXARM;armynum++) if(P_ASOLD>0){
P_ASTAT=ATTACK;
P_AMOVE=(curntn->maxmove * *(unitmove+P_ATYPE%UTYPE))/10;
if(P_ATYPE<MINLEADER) {
P_ASOLD *= 102;
P_ASOLD /= 100;
}
count=0;
while( TRUE ) {
x=(int)P_AXLOC+rand()%3-1;
y=(int)P_AYLOC+rand()%3-1;
if( count++ > 100 ) {
P_ASOLD=0;
break;
}
/* nomads cannot stay in the same spot */
if(( x==P_AXLOC && y==P_AYLOC )
||(!ONMAP(x,y)
||(!is_habitable(x,y)))
||(!land_reachp((int)P_AXLOC,(int)P_AYLOC,
x,y,curntn->maxmove,country))) continue;
P_AXLOC=x;
P_AYLOC=y;
/*if owned & unoccupied you take & people flee*/
if( ((sct[x][y].owner) == 0
|| solds_in_sector( x, y, sct[x][y].owner) == 0 )
&& (ntn[sct[x][y].owner].active!=NPC_NOMAD) ) {
fprintf(fnews,"3:\tnomads capture sector %d,%d\n",x,y);
if(sct[x][y].owner!=0) flee(x,y,1,FALSE);
sct[x][y].owner=country;
DEVASTATE(x,y);
}
break;
}
}
}
void
do_savage()
{
short armynum;
int x, y;
printf("updating savage (nation %d)\n",country);
for(armynum=0;armynum<MAXARM;armynum++) if(P_ASOLD>0){
P_ASTAT=ATTACK;
if(P_ATYPE<MINLEADER) {
P_ASOLD *= 102;
P_ASOLD /= 100;
}
P_AMOVE=(curntn->maxmove * *(unitmove+P_ATYPE%UTYPE))/10;
x=(int)P_AXLOC+rand()%3-1;
y=(int)P_AYLOC+rand()%3-1;
if(ONMAP(x,y)&&(is_habitable(x,y))
&&(land_reachp((int)P_AXLOC,(int)P_AYLOC,x,y,P_AMOVE,country))){
P_AXLOC=x;
P_AYLOC=y;
/*if owned & unoccupied you take & people flee*/
if( ((sct[x][y].owner == 0)
|| (solds_in_sector( x, y, sct[x][y].owner) == 0))
&& (ntn[sct[x][y].owner].active != NPC_SAVAGE)) {
fprintf(fnews,"3:\tsavages capture sector %d,%d\n",x,y);
if(P_ATYPE<MINLEADER) {
if(sct[x][y].owner!=0) flee(x,y,1,FALSE);
sct[x][y].owner=country;
}
DEVASTATE(x,y);
}
}
}
}
void
do_pirate()
{
short nvynum,shipsize;
int x, y, campx, campy;
printf("updating pirate (nation %d)\n",country);
/* if pirate fleet within 3 attack if outnumber any fleets */
/* automatically find their base first */
for(nvynum=0;nvynum<MAXNAVY;nvynum++) if(P_NWSHP!=0) {
int temp=TRUE;
campx=P_NXLOC; campy=P_NYLOC;
for(x=P_NXLOC-PRTZONE;x<=P_NXLOC+PRTZONE;x++)
for(y=P_NYLOC-PRTZONE;y<=P_NYLOC+PRTZONE;y++) {
if((ONMAP(x,y)) && sct[x][y].designation==DBASECAMP ) {
temp=FALSE;
campx=x;
campy=y;
}
}
if(temp==TRUE) {
fprintf(stderr,"Pirate fleet %d away from base\n",nvynum);
} else {
if (sct[campx][campy].designation!=DBASECAMP) {
fprintf(stderr,"BASECAMP NOT FOUND!!!\n");
} else {
P_NXLOC=campx;
P_NYLOC=campy;
}
}
}
for(nvynum=0;nvynum<MAXNAVY;nvynum++) if (P_NWSHP!=0) {
for(x=1;x<NTOTAL;x++) if(isntn(ntn[x].active))
for(y=0;y<MAXNAVY;y++)
if(ntn[x].nvy[y].warships!=0 || ntn[x].nvy[y].merchant!=0
|| ntn[x].nvy[y].galleys!=0) {
if((abs((int)ntn[x].nvy[y].xloc-(int)P_NXLOC)<=PRTZONE)
&&(abs((int)ntn[x].nvy[y].yloc-(int)P_NYLOC)<=PRTZONE)) {
P_NXLOC= ntn[x].nvy[y].xloc;
P_NYLOC= ntn[x].nvy[y].yloc;
}
}
#ifdef MORE_MONST
if(rand()%15==0) {
/*randomly add one warship to pirate fleet*/
shipsize = rand()%(N_HEAVY-N_LIGHT+1);
(void) NADD_WAR(1);
}
#endif /* MORE_MONST */
}
}
#endif /* MONSTER */
#ifdef NPC
void
n_redes(x,y,goldthresh,metalthresh,citythresh,hunger)
int x,y,goldthresh,metalthresh,citythresh;
float hunger;
{
register struct s_sector *sptr = &sct[x][y];
if((sptr->designation == DCAPITOL)
||(sptr->designation == DCITY)) return;
/*large enough for a city now?*/
if(((sptr->people > (spread.civilians/CITYLIMIT))
||((spread.civilians<30000)&&(sptr->people>1000)))
&&( hunger > P_EATRATE*1.5 )
&&( spread.incity+spread.incap < spread.civilians * CITYPERCENT / 100)
&&( spread.sectors > 10)
&&( sptr->tradegood == TG_none )){
sptr->designation=DTOWN;
spread.incity+=sptr->people;
spread.infarm-=sptr->people;
}
/* large enough for city and not enough food*/
if((sptr->designation==DTOWN)
&&( hunger < P_EATRATE)
&&( tofood(sptr,sptr->owner) > citythresh )){
sptr->designation=DFARM;
spread.incity-=sptr->people;
spread.infarm+=sptr->people;
}
if((sptr->designation==DTOWN)
&&( spread.incity+spread.incap > spread.civilians * CITYPERCENT / 66)){
sptr->designation=DFARM;
spread.incity-=sptr->people;
spread.infarm+=sptr->people;
}
/*what if it is not a city*/
if((sptr->designation!=DTOWN)
&&(sptr->designation!=DCITY)
&&(sptr->designation!=DCAPITOL)){
if(( sptr->tradegood != TG_none )
&&( tg_ok( sptr->owner, sptr ))) {
if(( metalthresh+goldthresh > 8 )
||(( sptr->metal < metalthresh )
&&( sptr->metal != 0 ))
||(( sptr->jewels < goldthresh )
&&( sptr->jewels != 0 ))) {
sptr->designation = DFARM;
} else sptr->designation= *(tg_stype+sptr->tradegood);
if(( sptr->metal < metalthresh )
&&( sptr->metal != 0 ))
sptr->designation=DBLKSMITH;
if(( sptr->designation== 'x' )
||(( sptr->designation== DCITY )&&(sptr->people<1000)))
sptr->designation=DFARM;
} else if( tofood(sptr,sptr->owner) >= 4 ){
sptr->designation=DFARM;
} else sptr->designation=DSTOCKADE;
}
if(( sptr->designation==DFARM)
&&( hunger > P_EATRATE*1.5 )
&&( tofood(sptr,sptr->owner) <= 6 )){
if(( rand()%2 == 0 )&&( curntn->mine_ability<30 ))
sptr->designation=DBLKSMITH;
else if(( rand()%2 == 0 )
&&( sptr->people<100 )
&&( curntn->spoilrate >15))
sptr->designation=DGRANARY;
else if(( rand()%2 == 0 )&&( curntn->popularity<50 ))
sptr->designation=DCHURCH;
else if( sptr->people>1000 )
sptr->designation=DTOWN;
}
}
void
redomil()
{
short x,y,armynum,nvynum;
int i, free, done;
long ideal;
long diff=0l;
int ok;
check();
/* check out any ship crews */
for(nvynum=1;nvynum<MAXNAVY;nvynum++) {
/* definite cheat -- add some random */
if((P_NMSHP!=0)||(P_NWSHP!=0)||(P_NGSHP != 0))
if(rand()%2==0) P_NCREW = SHIPCREW;
}
check();
curntn->tmil = 0L;
for(armynum=1;armynum<MAXARM;armynum++) if(P_ASOLD>0){
/* move army back if too far out */
ok = 0;
if (P_ATYPE < MINLEADER && P_ASOLD!=A_MILITIA) {
curntn->tmil += P_ASOLD;
}
for(x=(int)P_AXLOC-3;x<=(int)P_AXLOC+3;x++)
for(y=(int)P_AYLOC-3;y<=(int)P_AYLOC+3;y++)
if((ONMAP(x,y))&&(sct[x][y].owner==country)) ok=1;
if(ok==0){
P_AXLOC=curntn->capx;
P_AYLOC=curntn->capy;
}
/* verify militia */
if(P_ATYPE==A_MILITIA) {
/* eliminate invalid militia */
if(((sct[P_AXLOC][P_AYLOC].designation!=DTOWN)
&&(sct[P_AXLOC][P_AYLOC].designation!=DCAPITOL)
&&(sct[P_AXLOC][P_AYLOC].designation!=DCITY))
||(sct[P_AXLOC][P_AYLOC].owner!=country)){
#ifdef DEBUG
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);
#endif /* DEBUG */
if(sct[P_AXLOC][P_AYLOC].owner == country)
sct[P_AXLOC][P_AYLOC].people+=P_ASOLD;
else sct[curntn->capx][curntn->capy].people+=P_ASOLD;
P_ASOLD=0;
}
}
/* set default status */
if(P_ASTAT<NUMSTATUS) switch(P_ASTAT) {
case MILITIA:
case SIEGED:
case TRADED:
case ONBOARD:
case GENERAL:
case SCOUT:
break;
default:
P_ASTAT=DEFEND;
break;
}
}
/*make sure enough men in army 0 -- garrison duty in capitol*/
armynum=0;
/* find lowest army of non-leader type */
while (armynum < MAXARM && (P_ATYPE>=MINLEADER && P_ASOLD>0)) {
armynum++;
}
/* oh well... wipe out army zero; c'est la vie */
if (armynum == MAXARM) armynum = 0;
P_ATYPE=defaultunit(country);
P_ASTAT=GARRISON;
P_AXLOC=curntn->capx;
P_AYLOC=curntn->capy;
/*Ideally P_ASOLD[0]*MILINCAP=tmil*peace/10*/
ideal = curntn->tmil * peace / (10L*MILINCAP);
if(curntn->tgold < 0) ideal/=2L;
#ifdef DEBUG
if(peace==8)
printf("\t%s IS AT PEACE - garrison in cap is %ld, ideal is %ld\n",curntn->name,P_ASOLD,ideal);
else if(peace==12)
printf("\t%s IS AT WAR - garrison in cap is %d, ideal is %ld\n",curntn->name,P_ASOLD,ideal);
else printf("error - incap is %d ideal is %ld\n",P_ASOLD,ideal);
#endif /* DEBUG */
/*MILRATIO ratio mil:civ for non player countries*/
/*MILINCAP ratio (mil in cap):mil for NPCs*/
check();
if((P_ASOLD*10) < (9*ideal)){
/*too few soldiers on garrison*/
/*diff is number to change mil in cap (>0)*/
if (*(u_enmetal + (P_ATYPE%UTYPE)) > 0)
diff = (long) min(ideal-P_ASOLD,(int) (curntn->metals / *(u_enmetal + (P_ATYPE%UTYPE))));
diff=(long) min((int) diff, sct[curntn->capx][curntn->capy].people/2L);
if ((curntn->tgold<0L || curntn->metals<0L) && (diff > 0L)) diff=0L;
if (sct[curntn->capx][curntn->capy].owner != country) diff=0L;
#ifdef DEBUG
printf("\tadding %d men to garrison (too few men on garrison)\n",diff);
#endif /* DEBUG */
sct[curntn->capx][curntn->capy].people-=diff;
P_ASOLD+=diff;
curntn->tciv-=diff;
curntn->tmil+=diff;
if(magic(country,WARRIOR)==1) /* take WARRIOR power into account */
curntn->tgold -= (diff * *(u_encost + (P_ATYPE%UTYPE))) / 2;
else curntn->tgold-=diff* *(u_encost + (P_ATYPE%UTYPE));
curntn->metals-=(diff* *(u_enmetal + (P_ATYPE%UTYPE)));
#ifdef DEBUG
if(P_ASOLD < 0L) printf("error 2... P_ASOLD=%d <0\n",P_ASOLD);
#endif /* DEBUG */
}
/*else split garrison army if 1.25* needed number*/
else if(P_ASOLD *4L > 5L*ideal){
/*diff here is a negative number*/
diff=((4L*P_ASOLD)-(5L*ideal))/4L;
#ifdef DEBUG
printf("\tsplit garrison of %d men\n",diff);
#endif /* DEBUG */
free=FALSE;
P_ASOLD-=diff;
#ifdef DEBUG
if(P_ASOLD < 0) printf("error... subtracting %d from %d\n",diff,P_ASOLD);
#endif /* DEBUG */
curntn->tmil-=diff;
curntn->tciv+=diff;
sct[curntn->capx][curntn->capy].people+=diff;
/*I add back gold as armies get redone anyway*/
curntn->metals += (diff* *(u_enmetal + (P_ATYPE%UTYPE)));
if(magic(country,WARRIOR)==TRUE) { /* WARRIOR power */
curntn->tgold+=(diff* *(u_encost + (P_ATYPE%UTYPE))) / 2;
} else curntn->tgold+=diff* *(u_encost + (P_ATYPE%UTYPE));
}
#ifdef DEBUG
else printf("\tno action - P_ASOLD (%d) ~= ideal (%d)\n",P_ASOLD,ideal);
printf("\tFinal Garrison Army %d (%s) type is %s men is %d\n",armynum,curntn->name,*(unittype+(P_ATYPE)),P_ASOLD);
#endif /* DEBUG */
/*build ships and/or armies*/
done=FALSE;
ideal = curntn->tciv * peace / (10 * MILRATIO);
if(curntn->tgold<0) { ideal*=4; ideal/=5; }
#ifdef DEBUG
printf("\t%s total military is %d -> ideal is %d\n",curntn->name,curntn->tmil,ideal);
#endif /* DEBUG */
check();
/* find leader and place on RULE in capitol */
for(armynum=0;armynum<MAXARM;armynum++)
if (P_ATYPE==getleader(curntn->class)-1) {
P_ASTAT=RULE;
P_AXLOC=curntn->capx;
P_AYLOC=curntn->capy;
break;
}
/* add to partial armies */
for(armynum=1;armynum<MAXARM;armynum++)
if((P_ASOLD>0)
&&( P_ATYPE!=A_MILITIA )
&&( P_ATYPE<MINLEADER )
&&( P_ASOLD < TAKESECTOR )
&&( curntn->tgold > 0 )
&&(curntn->metals >= (TAKESECTOR+20-P_ASOLD)* *(u_enmetal + (P_ATYPE%UTYPE)))
&&( fort_val(&sct[P_AXLOC][P_AYLOC]) > 0)
&&( sct[P_AXLOC][P_AYLOC].owner == country )) {
#ifdef DEBUG
printf("\tadding %d men to weakened army %d\n",TAKESECTOR+20-P_ASOLD,armynum);
#endif /* DEBUG */
if(magic(country,WARRIOR)==TRUE) /* WARRIOR power */
curntn->tgold-=((TAKESECTOR+20-P_ASOLD)*
*(u_encost + (P_ATYPE%UTYPE))) / 2;
else curntn->tgold-=(TAKESECTOR+20-P_ASOLD)*
*(u_encost + (P_ATYPE%UTYPE));
curntn->tmil += TAKESECTOR+20-P_ASOLD;
curntn->metals -= ((TAKESECTOR+20-P_ASOLD)* *(u_enmetal + (P_ATYPE%UTYPE)));
P_ASOLD = TAKESECTOR+20;
}
/*if < ideal build new army in the capitol - if possible*/
if(curntn->tmil < ((4*ideal)/5)) {
for(armynum=1;armynum<MAXARM;armynum++)
if((done==FALSE)&&(P_ASOLD==0)) {
done=TRUE;
P_ATYPE = defaultunit(country);
P_ASOLD = min ((int) (ideal-curntn->tmil), (int) (curntn->metals/ (*(u_enmetal + (P_ATYPE%UTYPE)))));
P_ASOLD = min (P_ASOLD,sct[curntn->capx][curntn->capy].people/2);
P_ASOLD = min (P_ASOLD, (int) (curntn->tgold/ *(u_encost+(P_ATYPE%UTYPE))));
if(P_ASOLD>0){
#ifdef DEBUG
printf("\tnot enough soldiers - build new army %d with %d men\n",armynum,P_ASOLD);
#endif /* DEBUG */
curntn->metals-=(P_ASOLD* *(u_enmetal + (P_ATYPE%UTYPE)));
P_AXLOC= curntn->capx;
P_AYLOC= curntn->capy;
curntn->tmil += P_ASOLD;
curntn->tciv -= P_ASOLD;
if(magic(country,WARRIOR)==TRUE) /* WARRIOR power */
curntn->tgold-=(P_ASOLD* *(u_encost + (P_ATYPE%UTYPE))) / 2;
else curntn->tgold-=P_ASOLD* *(u_encost + (P_ATYPE%UTYPE));
sct[P_AXLOC][P_AYLOC].people-=P_ASOLD;
P_ASTAT= DEFEND;
P_AMOVE=0;
}
else P_ASOLD=0;
}
check();
} else if(curntn->tmil > (6*ideal/5)){
check();
/*disband a pseudo-random army*/
done=FALSE;
diff=curntn->tmil-(6*ideal/5);
for(armynum=1;done==FALSE && armynum<MAXARM;armynum++){
if((P_ASOLD<=0)
||(P_ATYPE==A_ZOMBIE)
||(P_ATYPE==A_MILITIA)
||(P_ATYPE>=MINLEADER)
||(P_ASTAT==ONBOARD)
||(P_ASTAT==TRADED)) continue;
if((sct[P_AXLOC][P_AYLOC].owner==country)
&&((sct[P_AXLOC][P_AYLOC].jewels>4)
||(sct[P_AXLOC][P_AYLOC].metal>4)
||(ISCITY(sct[P_AXLOC][P_AYLOC].designation)))){
#ifdef DEBUG
printf("\ttoo many soldiers eliminate army %d (%d men)\n",armynum,P_ASOLD);
#endif /* DEBUG */
diff-=P_ASOLD;
sct[P_AXLOC][P_AYLOC].people+=P_ASOLD;
curntn->tmil -= P_ASOLD;
curntn->tciv += P_ASOLD;
P_ASOLD=0;
if(diff<=50) done=TRUE;
}
}
}
check();
#ifdef DEBUG
printf("\twhew... new tmil is %d\n",curntn->tmil);
#endif /* DEBUG */
/*resize armies */
for(armynum=1;armynum<MAXARM;armynum++) if(P_ATYPE < MINLEADER) {
/*maximum npc army is 3*TAKESECTOR or 3*tmil/MAXARM */
/* also let militia get big */
if((P_ASOLD>(2*TAKESECTOR))
&&(P_ATYPE!=A_MILITIA && P_ASTAT!=ONBOARD && P_ASTAT!=TRADED)) {
free=FALSE;
for(i=1;free==FALSE && i<MAXARM;i++){
if(curntn->arm[i].sold==0){
free=TRUE;
P_ASOLD/=2;
#ifdef DEBUG
printf("\tSplitting %ld troops from army %d forming %s army %d \n"
,P_ASOLD,armynum,unittype[P_ATYPE],i);
#endif /* DEBUG */
curntn->arm[i].sold = P_ASOLD;
curntn->arm[i].unittyp = P_ATYPE;
curntn->arm[i].smove = P_AMOVE;
curntn->arm[i].stat = P_ASTAT;
curntn->arm[i].xloc = P_AXLOC;
curntn->arm[i].yloc = P_AYLOC;
}
}
}
/*minimum npc army is TAKESECTOR, merge them*/
else if(P_ASOLD>0 && (P_ASOLD<TAKESECTOR || P_ASTAT==MILITIA)
&& P_ASTAT!=ONBOARD && P_ASTAT!=TRADED) {
free=FALSE;
for(i=1;free==FALSE && i<MAXARM;i++){
if((curntn->arm[i].sold>0)
&&(curntn->arm[i].stat!=ONBOARD)
&&(i!=armynum) /* don't use same army */
/* or it will be deleted */
&&(curntn->arm[i].stat!=TRADED)
&&(curntn->arm[i].xloc==P_AXLOC)
&&(curntn->arm[i].yloc==P_AYLOC)
&&(curntn->arm[i].unittyp==P_ATYPE)) {
free=TRUE;
curntn->arm[i].sold += P_ASOLD;
#ifdef DEBUG
printf("\tMerge %ld men from army %d to make %ld troops in %s army %d \n"
,P_ASOLD,armynum,curntn->arm[i].sold,unittype[P_ATYPE],i);
#endif /* DEBUG */
P_ASOLD=0;
}
}
}
}
check();
/* assure that a militia unit resides in each city */
if(curntn->tgold > 0)
for(x=stx;x<endx;x++) for(y=sty;y<endy;y++)
if((sct[x][y].owner==country)
&&((sct[x][y].designation==DTOWN)||(sct[x][y].designation==DCITY)||(sct[x][y].designation==DCAPITOL))){
free=FALSE;
for(armynum=0;armynum<MAXARM;armynum++){
if((P_ASOLD>0)
&&(P_AXLOC==x)&&(P_AYLOC==y)
&&(P_ATYPE==A_MILITIA)) {
free=TRUE;
break;
}
}
if(free==FALSE) { /* draft new militia army */
for(armynum=0;armynum<MAXARM;armynum++) if(P_ASOLD==0){
P_AXLOC=x;
P_AYLOC=y;
P_ATYPE=A_MILITIA;
free=TRUE;
break;
}
}
if((free==TRUE)) {
/* want to have ideal troops */
ideal = sct[x][y].people/MILINCITY;
if(ideal < 50) /* make the militia at least 50 */
ideal = 50;
if(ideal>0){
if(magic(country,WARRIOR)==TRUE){ /* WARRIOR power */
curntn->tgold-=
((ideal-P_ASOLD)* *(u_encost+P_ATYPE))/2;
} else {
curntn->tgold-=
(ideal-P_ASOLD)* *(u_encost + P_ATYPE);
}
}
#ifdef DEBUG
printf("\tnow in sector %d,%d\n",x,y);
printf("\tadding %ld troops to %s army %d (now %ld men - populace %ld)\n",ideal-P_ASOLD,unittype[P_ATYPE%UTYPE],armynum,ideal,sct[x][y].people);
#endif /* DEBUG */
P_ASOLD=ideal;
P_ASTAT=MILITIA;
}
}
check();
#ifdef DEBUG
printf("\tnow setting all units to default type of %s\n",unittype[defaultunit(country)]);
#endif /*DEBUG*/
/* setup default units */
for(armynum=1;armynum<MAXARM;armynum++)
if((P_ASOLD>0)&&(P_ATYPE!=A_MILITIA)&&(P_ATYPE<MINLEADER))
P_ATYPE=defaultunit(country);
}
/* getdstatus() - do diplomacy for current nation */
void
getdstatus()
{
int x,oldstat[NTOTAL];
int X,Y;
int svhostile,hostile; /* chance nation will react hostile */
int friendly; /* chance nation will react favorably */
if(!isnpc(curntn->active)) return;
if(( curntn->active==GOOD_6FREE )
||( curntn->active==ISOLATIONIST )
||( curntn->active==NEUTRAL_6FREE )
||( curntn->active==EVIL_6FREE )) svhostile=5;
else if(( curntn->active==GOOD_4FREE )
||( curntn->active==NEUTRAL_4FREE )
||( curntn->active==EVIL_4FREE )) svhostile=10;
else if(( curntn->active==GOOD_2FREE )
||( curntn->active==NEUTRAL_2FREE )
||( curntn->active==EVIL_2FREE )) svhostile=20;
else if(( curntn->active==GOOD_0FREE )
||( curntn->active==NEUTRAL_0FREE )
||( curntn->active==EVIL_0FREE )) svhostile=35;
else svhostile=5;
for(x=1;x<NTOTAL;x++) if( isntn(ntn[x].active) ){
hostile = svhostile;
if(npctype(curntn->active) != npctype(ntn[x].active))
hostile+=20; /* not same alignment */
friendly = 60-hostile;
if( curntn->active==ISOLATIONIST ) friendly -= 20;
/* negate impact of above line on neutrals */
if(isneutral(ntn[x].active)) {
friendly-=10;
hostile-=10;
}
if(ntn[x].race==curntn->race){
friendly+=10;
hostile-=10;
}
if(isneutral(curntn->active)) {
friendly-=20;
hostile-=20;
}
/* if next to capitol, they dont like you */
for(X=(int)curntn->capx-1;X<=(int)curntn->capx+1;X++)
for(Y=(int)curntn->capy-1;Y<=(int)curntn->capy+1;Y++)
if(ONMAP(X,Y)) {
if(sct[X][Y].owner == x) {
friendly-=10;
hostile +=10;
}
}
if( friendly < 0 ) friendly=0;
if( hostile < 0 ) hostile=0;
oldstat[x] = curntn->dstatus[x];
/* break bad treaties */
if(curntn->dstatus[x] == TREATY) {
if(ntn[x].dstatus[country]>=WAR)
curntn->dstatus[x] = JIHAD;
continue;
}
if((curntn->dstatus[x] == JIHAD)
||(curntn->dstatus[x]==UNMET)
||(ispc(curntn->active)))
continue;
/*if 4* mil and 4* score then not like them*/
if((ntn[x].tmil>4*curntn->tmil)
&&(ntn[x].score>4*curntn->score)){
if(curntn->dstatus[x]<WAR){
if(rand()%100<=hostile)
curntn->dstatus[x]++;
}
}
/*adjust based on your status with them*/
if((curntn->dstatus[x]==WAR)
&&(ntn[x].dstatus[country]<WAR))
if(rand()%100<=friendly) curntn->dstatus[x]--;
if((curntn->dstatus[x]<WAR)
&&(curntn->dstatus[x]>ALLIED)){
if(ntn[x].dstatus[country]>1+curntn->dstatus[x]){
if(rand()%100<=hostile)
curntn->dstatus[x]++;
} else if(ntn[x].dstatus[country]+1<curntn->dstatus[x]){
if(rand()%100<=friendly)
curntn->dstatus[x]--;
}
}
if(rand()%100<= hostile) {
if((curntn->dstatus[x]!=JIHAD)
&&(curntn->dstatus[x]!=TREATY))
curntn->dstatus[x]++;
}
if((rand()%100<= friendly)
&&(curntn->dstatus[x]!=TREATY)
&&(curntn->dstatus[x]!=JIHAD)
&&(curntn->dstatus[x]!=WAR)) curntn->dstatus[x]--;
}
for(x=1;x<NTOTAL;x++) if(isntn( ntn[x].active ) ){
if((rand()%5==0)
&&(ntn[x].dstatus[country]==WAR)
&&(curntn->dstatus[x]==WAR)) {
ntn[x].dstatus[country]=HOSTILE;
curntn->dstatus[x]=HOSTILE;
fprintf(fnews,"2.\tnation %s and %s announce ceasefire\n",curntn->name,ntn[x].name);
if( isnotpc(ntn[x].active) ) continue;
if (mailopen(x)!=(-1)) {
fprintf(fm,"Message from Conquer\n\n");
fprintf(fm,"Nation %s and you negotiate a ceasefire\n",curntn->name);
mailclose(x);
}
} else if((oldstat[x]==WAR)&&(curntn->dstatus[x]==WAR)){
fprintf(fnews,"2.\tnation %s stays at war with %s\n",curntn->name,ntn[x].name);
} else if((oldstat[x]<WAR)&&(curntn->dstatus[x]==WAR)){
fprintf(fnews,"2.\tnation %s goes to war with %s\n",curntn->name,ntn[x].name);
if( isnotpc(ntn[x].active) ) continue;
if (mailopen(x)!=(-1)) {
fprintf(fm,"Message from Conquer\n\n");
fprintf(fm,"Nation %s goes to war with you\n",curntn->name);
mailclose(x);
}
} else if((oldstat[x]!=JIHAD)&&(curntn->dstatus[x]==JIHAD)){
fprintf(fnews,"2.\tnation %s announces a jihad with %s\n",curntn->name,ntn[x].name);
if( isnotpc(ntn[x].active) ) continue;
if(mailopen(x)!=(-1)) {
fprintf(fm,"Message from Conquer\n\n");
fprintf(fm,"nation %s announces a jihad with you\n",curntn->name);
mailclose(x);
}
}
}
}
/* Find the average world food value per sector
* and the average tradegood value per sector.
* This is used for unseen sectors and unseen
* armies.
*/
static void
find_avg_sector ()
{
int armynum, i, nation, repeat, total_sectors, total_food = 0;
struct s_sector *sptr; /* used to speed up this function */
register int x,y;
register long total_tg = 0;
register long useable_land = 0;
for(x=0;x<MAPX;x++) for(y=0;y<MAPY;y++) {
sptr = &sct[x][y];
if(( sptr->altitude!=WATER )&&( sptr->altitude!=PEAK )) {
useable_land++;
total_food += tofood(sptr,country);
if(sptr->tradegood != TG_none) {
if(sptr->metal != 0) total_tg +=500;
else if(sptr->jewels != 0) total_tg +=500;
else total_tg +=300;
}
}
}
if (useable_land>0) {