forked from quixadhal/conquer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcombat.c
1377 lines (1277 loc) · 39.4 KB
/
combat.c
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 <math.h>
#include "header.h"
#include "data.h"
#define MGKNUM 32 /* number of units possible in battle */
#define ATKR 2
#define DFND 1
#define NTRL 0
#define WIMP 3
extern FILE *fpmsg;
extern FILE *fnews;
extern short country;
short retreatside; /* ATKR, DFND, or none (0) */
short retreatx; /* retreat x square */
short retreaty; /* retreat y square */
int unit[MGKNUM]; /*armynum*/
int owner[MGKNUM]; /*owner*/
int side[MGKNUM]; /*see definitions->1=units 2=unit*/
long troops[MGKNUM]; /*starting troops in army */
int xspot,yspot; /*location of battles*/
int anation; /*nation attacking in this fight*/
int dnation; /*one nation defending in this fight*/
int count=0; /*number of armies or navies in sector*/
/* indicators of naval or army combat */
#define COMBAT_X 0
#define COMBAT_A 1
#define COMBAT_N 2
#define FOUGHT_A 4
#define FOUGHT_N 8
/************************************************************************/
/* COMBAT() run all combat on the map */
/* for each sector, determine if armies in with attack mode */
/************************************************************************/
void
combat()
{
register int i,j;
char **fought; /* SET: if already fought in sctr */
int temp,ctry;
int initialized=FALSE; /* TRUE if arrays initialized */
short armynum,nvynum;
int valid;
struct s_nation *nptr;
struct army *aptr;
fought = (char **) m2alloc(MAPX,MAPY,sizeof(char));
printf("Run Combat Routines\n");
fprintf(fnews,"4\tBATTLE SUMMARY STATISTICS\n");
/*for each nation, if in attack mode run a check*/
/* no sectors have been fought in yet */
for(i=0;i<MAPX;i++) for(j=0;j<MAPY;j++) fought[i][j]=COMBAT_X;
for(ctry=NTOTAL-1;ctry>0;ctry--) if(isactive(ntn[ctry].active)) {
nptr = &ntn[ctry];
/*army combat*/
for(j=0;j<MAXARM;j++) {
aptr = &nptr->arm[j];
if((aptr->sold>0)
&&(aptr->stat>=ATTACK)
&&(aptr->stat<=SORTIE||aptr->stat>=NUMSTATUS)
&&(!(fought[aptr->xloc][aptr->yloc]&COMBAT_A))){
/* someone can initiate combat in xspot,yspot */
xspot=aptr->xloc;
yspot=aptr->yloc;
fought[xspot][yspot]|=COMBAT_A;
/*initialize matrix*/
if( !initialized ) {
for(temp=0;temp<MGKNUM;temp++){
unit[temp]=owner[temp]=(-1);
side[temp]=NTRL;
troops[temp]=0;
}
initialized=TRUE;
}
/*check all armies in sector and add to matrix*/
count=0;
valid=FALSE;
/*is valid,set matrix*/
for(country=0;country<NTOTAL;country++)
if(isactive(ntn[country].active))
for(armynum=0;armynum<MAXARM;armynum++)
if((ASOLD>0)
&&(ASTAT!=SCOUT)
&&(AXLOC==xspot)
&&(AYLOC==yspot)
&&(count<MGKNUM)) {
if((country!=ctry)
&&(nptr->dstatus[country]>HOSTILE)) {
valid=TRUE;
if( sct[xspot][yspot].owner==ctry ) {
dnation=ctry;
anation=country;
} else if(( rand()%2==0 )
||( sct[xspot][yspot].owner==country )){
anation=ctry;
dnation=country;
} else {
dnation=ctry;
anation=country;
}
}
unit[count]=armynum;
owner[count]=country;
count++;
}
if(valid==TRUE) {
fight();
initialized=FALSE;
}
}
}
/*navy combat*/
for(j=0;j<MAXNAVY;j++)
if((nptr->nvy[j].warships!=0)
&&(!(fought[nptr->nvy[j].xloc][nptr->nvy[j].yloc]&COMBAT_N))) {
xspot=nptr->nvy[j].xloc;
yspot=nptr->nvy[j].yloc;
fought[xspot][yspot]|=COMBAT_N;
/*initialize matrix*/
if( !initialized ){
for(temp=0;temp<MGKNUM;temp++){
unit[temp]= owner[temp]=(-1);
side[temp]=NTRL;
troops[temp]=0;
}
initialized=TRUE;
}
/*check all fleets in 2 sector range and add to matrix*/
count=0;
valid=FALSE;
/*is valid,set matrix*/
for(country=0;country<NTOTAL;country++)
if(isactive(ntn[country].active))
for(nvynum=0;nvynum<MAXNAVY;nvynum++)
if((NWSHP+NMSHP+NGSHP!=0)
&&(((NXLOC==xspot) && (NYLOC==yspot)) ||
(sct[NXLOC][NYLOC].altitude==WATER
&&(abs(NXLOC-xspot)<=2)
&&(abs(NYLOC-yspot)<=2)))
&&(count<MGKNUM)) {
fought[NXLOC][NYLOC]|=COMBAT_N;
if((country!=ctry)
&&(nptr->dstatus[country]>HOSTILE)){
valid=TRUE;
anation=ctry;
dnation=country;
}
unit[count]=nvynum;
owner[count]=country;
count++;
}
if(valid==TRUE) {
navalcbt();
initialized=FALSE;
}
}
}
free(fought);
printf("\nall army and navy attacks completed\n");
}
/* macro for owner, accounts for runaway indicator */
#define UOWNER(x) ((owner[(x)]<(-1))?(-owner[(x)]-1):(owner[(x)]))
/************************************************************************/
/* FIGHT() - fight an individual battle given the three */
/* matricies global to this module */
/************************************************************************/
void
fight()
{
int roll,strength,fortdam=FALSE;
int odds; /* odds (asold/dsold) times 100 */
int done;
int i,j,k;
long asold=0,dsold=0; /*a's and d's total soldiers*/
float astr=0,dstr=0; /*a's and d's relative strength*/
long Aloss,Dloss; /*a's and d's total losses*/
int PAloss,PDloss; /*percent a and d loss*/
long loss;
int abonus=0,dbonus=0; /* bonus aggregate */
long vampire=0; /* # non vamps deaded */
short nvamps=0; /* number of vampire armies */
/* determine who is attacker & who is on defenders side?*/
for(j=0;j<count;j++) if(owner[j]!=(-1)){
if(owner[j]==anation) side[j]=ATKR;
else if(owner[j]==dnation) side[j]=DFND;
else if(ntn[anation].dstatus[owner[j]]==JIHAD) side[j]=DFND;
else if(ntn[owner[j]].dstatus[anation]==JIHAD) side[j]=DFND;
else if(ntn[anation].dstatus[owner[j]]==WAR) side[j]=DFND;
else if(ntn[owner[j]].dstatus[anation]==WAR) side[j]=DFND;
else if((ntn[owner[j]].dstatus[anation]==TREATY)&&(ntn[owner[j]].dstatus[dnation]>HOSTILE)) side[j]=ATKR;
else if((ntn[owner[j]].dstatus[anation]==ALLIED)&&(ntn[owner[j]].dstatus[dnation]>HOSTILE)) side[j]=ATKR;
}
/*calculate number of troops and assign statuses */
asold=0;
dsold=0;
for(i=0;i<count;i++) if(owner[i]>(-1)) {
/* record troops for all units in sector */
troops[i]=ntn[owner[i]].arm[unit[i]].sold;
if(((ntn[owner[i]].arm[unit[i]].unittyp == A_MERCENARY)
||(ntn[owner[i]].arm[unit[i]].unittyp == A_ORC)
||(ntn[owner[i]].arm[unit[i]].unittyp == A_GOBLIN))
&&( ntn[owner[i]].arm[unit[i]].stat < NUMSTATUS )
&&( rand()%100<15 )) {
if( ispc(ntn[owner[i]].active)) {
if (mailopen( owner[i] )!=(-1)) {
fprintf(fm,"Message to %s from Conquer\n\n",ntn[owner[i]].name);
fprintf(fm," Your %s Army %d Refuses to Fight\n",
unittype[ntn[owner[i]].arm[unit[i]].unittyp],
unit[i]);
mailclose(owner[i]);
}
}
retreatside = side[i];
fdxyretreat();
if((retreatx==xspot)&&(retreaty==yspot)){
/* move to capitol & kill 30% */
ntn[owner[i]].arm[unit[i]].xloc=ntn[owner[i]].capx;
ntn[owner[i]].arm[unit[i]].yloc=ntn[owner[i]].capy;
ntn[owner[i]].arm[unit[i]].sold*=7;
ntn[owner[i]].arm[unit[i]].sold/=10;
} else {
/* retreat normally and kill 20% */
ntn[owner[i]].arm[unit[i]].sold*=8;
ntn[owner[i]].arm[unit[i]].sold/=10;
retreat( i );
}
owner[i]=(-1-owner[i]);
continue;
}
else if(side[i]==ATKR){
if ((ntn[owner[i]].arm[unit[i]].stat >= ATTACK)
&&(ntn[owner[i]].arm[unit[i]].stat <= SORTIE
||ntn[owner[i]].arm[unit[i]].stat >= NUMSTATUS)) {
asold += ntn[owner[i]].arm[unit[i]].sold;
} else {
side[i]=NTRL;
}
/* sortie 20% bonus in odds */
if(ntn[owner[i]].arm[unit[i]].stat==SORTIE)
asold += ntn[owner[i]].arm[unit[i]].sold/5;
} else if(side[i]==DFND){
if(ntn[owner[i]].arm[unit[i]].stat!=RULE) {
dsold += ntn[owner[i]].arm[unit[i]].sold;
}
}
if((magic(owner[i],VAMPIRE)==TRUE) &&
(ntn[owner[i]].arm[unit[i]].unittyp==A_ZOMBIE)) nvamps++;
}
if(asold<=0) {
printf("\nCombat aborted due to lack of attackers.\n");
return;
}
if( asold > dsold*100) odds=10000;
else if( dsold > asold*100 ) odds=1;
else odds = (asold*100)/dsold;
/* mercenaries/orcs/goblins might run away */
for(i=0;i<count;i++) if(owner[i]>(-1)) {
if(((( odds > 200 )&&(side[i]==DFND))
||(( odds < 100 )&&(side[i]==ATKR)))
&&((ntn[owner[i]].arm[unit[i]].unittyp == A_MERCENARY)
||(ntn[owner[i]].arm[unit[i]].unittyp == A_ORC)
||(ntn[owner[i]].arm[unit[i]].unittyp == A_GOBLIN))
&&(ntn[owner[i]].arm[unit[i]].stat < NUMSTATUS)
&&( rand()%100<30 )) {
if( ispc(ntn[owner[i]].active)) {
if(mailopen( owner[i] )!=(-1)) {
fprintf(fm,"Message to %s from Conquer\n\n",ntn[owner[i]].name);
fprintf(fm," Your %s Army %d Runs Away\n",
unittype[ntn[owner[i]].arm[unit[i]].unittyp],
unit[i]);
mailclose(owner[i]);
}
}
retreatside = side[i];
if( side[i] == ATKR ) asold-= troops[i];
if( side[i] == DFND ) dsold-= troops[i];
fdxyretreat();
if((retreatx==xspot)&&(retreaty==yspot)){
/* move to capitol & kill 75% */
ntn[owner[i]].arm[unit[i]].xloc=ntn[owner[i]].capx;
ntn[owner[i]].arm[unit[i]].yloc=ntn[owner[i]].capy;
ntn[owner[i]].arm[unit[i]].sold/=4;
} else {
/* retreat normally and kill 50% */
ntn[owner[i]].arm[unit[i]].sold/=2;
retreat( i );
}
owner[i]=(-1-owner[i]);
continue;
}
}
retreatside=0;
if( asold<=0 ) {
printf("Exit from battle due to lack of attackers\n");
return;
}
/* CALCULATE AVERAGE COMBAT BONUS */
abonus=0;
dbonus=0;
for(i=0;i<count;i++) if(owner[i]>(-1)) {
if(side[i]==ATKR)
abonus += cbonus(i)*troops[i];
else if(side[i]==DFND && ntn[owner[i]].arm[unit[i]].stat!=RULE)
dbonus += cbonus(i)*troops[i];
}
/*archer bonus if not in fort vs knights/cavalry*/
j=0;
k=0;
for(i=0;i<count;i++) if(owner[i]>(-1))
if(ISCITY(sct[xspot][yspot].designation)){
if((ntn[owner[i]].arm[unit[i]].unittyp == A_CAVALRY)
||(ntn[owner[i]].arm[unit[i]].unittyp == A_KNIGHT))
if(side[i]==ATKR) j+=troops[i];
else if(side[i]==DFND) k+=troops[i];
}
for(i=0;i<count;i++) if(owner[i]>(-1)) {
if(j>0) abonus += (15 * j * troops[i]) / asold;
if(k>0 && dsold>0) dbonus += (15 * k * troops[i]) / dsold;
}
abonus/=asold;
if (dsold>0) dbonus/=dsold;
/*CALCULATED BONUSES TO WHOLE COMBAT*/
for(i=0;i<count;i++) if(owner[i]>(-1)) {
if(fort_val(&sct[xspot][yspot]) != 0){
/*Catapults add +1%/20 men defending castle (max +10%)*/
if((ntn[owner[i]].arm[unit[i]].unittyp == A_CATAPULT)
&&(side[i]==DFND))
dbonus += max((troops[i]/20),10);
/*Catapults add +1%/40 men attacking castle (max +10%)*/
else if((ntn[owner[i]].arm[unit[i]].unittyp == A_CATAPULT)
&&(side[i]==ATKR)) {
strength = max((troops[i]/40),10);
abonus += strength;
/* possible damage 20% chance */
if(rand()%100<2*strength) {
fortdam=TRUE;
sct[xspot][yspot].fortress--;
if(sct[xspot][yspot].fortress == 0)
sct[xspot][yspot].designation = DRUIN;
}
}
/*Siege_engines add +1%/20 men when attacking fortress*/
else if((ntn[owner[i]].arm[unit[i]].unittyp == A_SIEGE)
&&(side[i]==ATKR)) {
strength = max((troops[i]/20),30);
abonus += strength;
/* possible damage 15% chance */
if(rand()%100<strength/2) {
fortdam=TRUE;
sct[xspot][yspot].fortress--;
if(sct[xspot][yspot].fortress == 0)
sct[xspot][yspot].designation = DRUIN;
}
}
} else {
/*Catapults add +1%/40 men normal combat (max +10%)*/
if(ntn[owner[i]].arm[unit[i]].unittyp == A_CATAPULT)
abonus+=max((troops[i]/40),10);
}
}
/*RUN COMBAT */
/*FIRST GIVE RANDOM ROLL FROM 0 to 100 */
/*WITH A PROBABILITY BELL CURVE */
/* high roll favors attacker [ 5 d21 - 5 ] */
roll = 0;
for(i=0;i<5;i++) {
roll += rand()%21+1;
}
roll -= 5;
/*find relative strength of troops*/
astr = asold * (100 + abonus);
dstr = dsold * (100 + dbonus);
/*Recalculate odds based on quality of troops*/
if( astr > dstr*100) odds=10000;
else if( dstr > astr*100 ) odds=1;
else odds = (astr*100)/dstr;
/* calculate loss for an even battle */
PDloss = MAXLOSS * roll / 100;
PAloss = MAXLOSS * (100 - roll) / 100;
/* adjust for odds */
if( odds == 1 ) {
PDloss=0;
PAloss=200;
} else if( odds == 10000 ) {
PAloss=0;
PDloss=200;
} else if(odds > 100) {
PDloss += (odds / 12 - 8); /* 8.33% for higher odds */
PAloss -= (odds / 16 - 6); /* 6.25% for lower odds */
if(PAloss<(100-roll)/20)
PAloss=(100-roll)/20; /* can't get too small */
} else {
PAloss += ( 800 / odds - 8); /* 8% for higher odds */
PDloss -= ( 600 / odds - 6); /* 6% for lower odds */
if(PDloss<roll/20)
PDloss = roll/20; /* can't get too small */
}
if( fort_val(&sct[xspot][yspot]) > 0 ){
PDloss *= 120;
PAloss *= 120;
PDloss /= 100;
PAloss /= 100;
}
retreatside = 0;
if((PDloss > 2* PAloss)
&&(odds>150)
&&(((PDloss>=50)&&(rand()%4==0))
||(rand()%8))) retreatside=DFND;
if((PAloss > 2* PDloss)
&&(odds<150)
&&(((PAloss>=50)&&(rand()%2==0))
||(rand()%6))) retreatside=ATKR;
if(retreatside!=0) {
fdxyretreat();
/* no legal retreat route */
if((retreatside!=0) && (retreatx== xspot)
&& (retreaty== yspot)){
if(retreatside==ATKR) PAloss+=15;
else if(retreatside==DFND) PDloss+=15;
#ifdef DEBUG
printf("side %d (%d %d) can't retreat...+15%% loss\n",retreatside,retreatx,retreaty);
#endif /* DEBUG */
retreatside = 0;
}
#ifdef DEBUG
else printf("retreat side %d to %d %d\n",retreatside,retreatx,retreaty);
#endif /* DEBUG */
}
if(PAloss>100) PAloss = 100;
if(PDloss>100) PDloss = 100;
Aloss = Dloss = 0;
for(i=0;i<count;i++) if(owner[i]>(-1)){
if(side[i]==ATKR){
if( ntn[owner[i]].arm[unit[i]].unittyp >= MINLEADER) {
if((rand()%100) < PAloss){ /* kill it */
for(j=0;j<MAXARM;j++)
if(ntn[owner[i]].arm[j].stat==unit[i]+NUMSTATUS)
ntn[owner[i]].arm[j].stat=ATTACK;
Aloss += troops[i];
ntn[owner[i]].arm[unit[i]].sold=0;
}
} else {
loss=(troops[i]*PAloss)/100;
/*archers/catapults on sortie take 1/4 damage*/
if((ntn[owner[i]].arm[unit[i]].stat==SORTIE)
&&(fort_val(&sct[xspot][yspot]) > 0)
&&(sct[xspot][yspot].owner==country)
&&((ntn[owner[i]].arm[unit[i]].unittyp==A_ARCHER)
||(ntn[owner[i]].arm[unit[i]].unittyp==A_CATAPULT)))
loss /= 4;
/*army can't have less than 25 men in it*/
if(troops[i]-loss<25)
loss=troops[i];
if( loss>troops[i] ) {
printf("I AM VERY CONFUSED - PLEASE HELP... combat.c\n");
}
Aloss+=loss;
ntn[owner[i]].arm[unit[i]].sold-=loss;
if((ntn[owner[i]].arm[unit[i]].unittyp==A_MILITIA)&&(retreatside==ATKR)) {
sct[ntn[owner[i]].arm[unit[i]].xloc][ntn[owner[i]].arm[unit[i]].yloc].people += ntn[owner[i]].arm[unit[i]].sold;
ntn[owner[i]].arm[unit[i]].sold=0;
}
}
} else if(side[i]==DFND){
if( ntn[owner[i]].arm[unit[i]].unittyp >= MINLEADER) {
if((ntn[owner[i]].arm[unit[i]].stat!=RULE
||PDloss>=80)&&((rand()%100) < PDloss)){ /* kill it */
for(j=0;j<MAXARM;j++)
if(ntn[owner[i]].arm[j].stat==unit[i]+NUMSTATUS)
ntn[owner[i]].arm[j].stat=ATTACK;
Dloss +=troops[i];
ntn[owner[i]].arm[unit[i]].sold=0;
}
} else {
loss=(troops[i]*PDloss)/100;
/*destroy army if < 25 men*/
if(troops[i]-loss<25)
loss=troops[i];
Dloss+=loss;
ntn[owner[i]].arm[unit[i]].sold-=loss;
if((ntn[owner[i]].arm[unit[i]].unittyp==A_MILITIA)&&(retreatside==DFND)) {
sct[ntn[owner[i]].arm[unit[i]].xloc][ntn[owner[i]].arm[unit[i]].yloc].people += ntn[owner[i]].arm[unit[i]].sold;
ntn[owner[i]].arm[unit[i]].sold=0;
}
}
}
/* non-vampire troops are sucked in by vampires */
if((nvamps>0)&&(magic(owner[i],VAMPIRE)==FALSE)
&&(ntn[owner[i]].arm[unit[i]].unittyp!=A_ZOMBIE)
&&(ntn[owner[i]].arm[unit[i]].unittyp<MINLEADER))
vampire+= loss / 3;
}
/* use k variable to hold length */
#ifdef HIDELOC
if( isntn( ntn[sct[xspot][yspot].owner].active )) {
fprintf(fnews,"4.\tBattle occurs in %s", ntn[sct[xspot][yspot].owner].name);
k = 27+strlen(ntn[sct[xspot][yspot].owner].name);
} else {
fprintf(fnews,"4.\tBattle on unowned land");
k = 30;
}
#else
fprintf(fnews,"4.\tBattle in %d,%d",xspot,yspot);
k = 25;
#endif
for(j=0;j<count;j++) if(UOWNER(j)>(-1)){
done=FALSE;
for(i=0;i<j;i++) if(UOWNER(j)==UOWNER(i)) done=TRUE;
if(done==FALSE) {
loss=NTRL;
for(i=j;(loss==NTRL||loss==WIMP) && i<count;i++)
if(UOWNER(i)==UOWNER(j)) {
if(owner[i]<(-1)) loss=WIMP;
else loss=side[i];
}
if(loss!=NTRL) {
k += 11 + strlen(ntn[UOWNER(j)].name);
if(loss==WIMP) k++;
if(k>79) {
k = 30;
fprintf(fnews,",\n4.\t ");
} else fprintf(fnews,", ");
if(loss==ATKR)
fprintf(fnews,"attacker %s",ntn[UOWNER(j)].name);
else if(loss==DFND)
fprintf(fnews,"defender %s",ntn[UOWNER(j)].name);
else if(loss==WIMP)
fprintf(fnews,"retreater %s",ntn[UOWNER(j)].name);
}
}
}
fprintf(fnews,"\n");
if(nvamps>0){
for(i=0;i<count;i++) if(owner[i]>(-1)){
if((magic(owner[i],VAMPIRE)==TRUE)
&&(ntn[owner[i]].arm[unit[i]].unittyp==A_ZOMBIE)
&&(ntn[owner[i]].arm[unit[i]].sold > 0))
ntn[owner[i]].arm[unit[i]].sold+=vampire/nvamps;
}
}
/*who is in the battle; but don't send to scared armies */
for(j=0;j<count;j++) if(owner[j]>(-1)){
done=FALSE;
/*first time your nation appears done=FALSE*/
for(i=0;i<j;i++) if(owner[j]==owner[i]) done=TRUE;
if((done==FALSE)&&(ispc(ntn[owner[j]].active))) {
loss=NTRL;
for(i=j;loss==NTRL && i<count;i++)
loss=side[i];
if (mailopen( owner[j] )==(-1)) continue;
fprintf(fm,"BATTLE SUMMARY for sector %d, %d\n",xspot,yspot);
fprintf(fm,"Battle occured during %s of Year %d\n",PSEASON(TURN),YEAR(TURN));
if(loss==ATKR)
fprintf(fm,"You are on the Attacking Side\n");
else if(loss==DFND)
fprintf(fm,"You are on the Defending Side\n");
else fprintf(fm,"You are Neutral\n");
/*detail all participants in battle*/
for(k=0;k<count;k++) if(owner[k]!=(-1)){
fprintf(fm," %s ",ntn[UOWNER(k)].name);
if(owner[k]<(-1))
fprintf(fm,"chickens out: ");
else if(side[k]==DFND
&& ntn[owner[k]].arm[unit[k]].stat!=RULE)
fprintf(fm,"defending: ");
else if(side[k]==ATKR)
fprintf(fm,"attacking: ");
else if(side[k]==NTRL
|| (side[k]==DFND
&& ntn[owner[k]].arm[unit[k]].stat==RULE))
fprintf(fm,"neutral: ");
else
fprintf(fm,"in limbo: ");
fprintf(fm,"army %d (%s, men %d, bonus=%d, loss=%d)",
unit[k],
unittype[ntn[UOWNER(k)].arm[unit[k]].unittyp%UTYPE],
troops[k],
cbonus(k),
troops[k]-ntn[UOWNER(k)].arm[unit[k]].sold);
if((ntn[UOWNER(k)].arm[unit[k]].unittyp >= MINLEADER)
&&( ntn[UOWNER(k)].arm[unit[k]].sold == 0))
fprintf(fm," (killed)\n");
else if((ntn[UOWNER(k)].arm[unit[k]].unittyp == A_MILITIA)
&&( ntn[UOWNER(k)].arm[unit[k]].sold == 0))
fprintf(fm," (disbanded)\n");
else fputc('\n',fm);
}
fprintf(fm,"attacking soldiers=%ld -> percent loss %d%%\n",asold,PAloss);
fprintf(fm,"defending soldiers=%ld -> percent loss %d%%\n",dsold,PDloss);
fprintf(fm,"ODDS=%d => adjusted to %d to 100; Die Roll is %d\n",odds*(100+dbonus)/(100+abonus),odds,roll);
fprintf(fm,"RESULT: Attackers lose %ld men, Defenders lose %ld men\n",Aloss, Dloss);
if(fortdam==TRUE) fprintf(fm,"Fortifications damaged during the attack\n");
if(retreatside==ATKR){
if(Aloss<asold)
fprintf(fm,"Additionally, All attackers retreat to %d %d\n",retreatx,retreaty);
} else if(retreatside==DFND){
if(Dloss<dsold)
fprintf(fm,"Additionally, All defenders retreat to %d %d\n",retreatx,retreaty);
}
mailclose(owner[j]);
}
}
retreat( -1 );
}
/************************************************************************/
/* CBONUS() - return combat bonuses for unit i */
/************************************************************************/
int
cbonus(num)
{
short armynum;
int armbonus;
armbonus=0;
armynum=unit[num];
country=UOWNER(num);
/*Racial combat bonus due to terrain (the faster you move the better)*/
armbonus+=5*(9-movecost[xspot][yspot]); /* this line always has */
/* the same result... must fix -- ADB */
if(((magic(country,DESTROYER)==1)
||(magic(country,DERVISH)==1))
&&((sct[xspot][yspot].vegetation==ICE)
||(sct[xspot][yspot].vegetation==DESERT)))
armbonus+=30;
if(ASTAT>=NUMSTATUS) armbonus+=20; /* army group */
if(side[num]==DFND){
if(sct[xspot][yspot].altitude==MOUNTAIN) armbonus+=20;
else if(sct[xspot][yspot].altitude==HILL) armbonus+=10;
if(sct[xspot][yspot].vegetation==JUNGLE) armbonus+=20;
else if(sct[xspot][yspot].vegetation==FOREST) armbonus+=15;
else if(sct[xspot][yspot].vegetation==WOOD) armbonus+=10;
if(ATYPE==A_MERCENARY) armbonus += MERCDEF;
else armbonus += ntn[UOWNER(num)].dplus;
if(ASTAT==MAGDEF) armbonus+=30;
else if(ASTAT==SORTIE) armbonus-=30;
else if(ASTAT==SIEGED) armbonus-=20;
if((sct[xspot][yspot].owner==country)
&&(ASTAT==GARRISON||ASTAT==MILITIA||ASTAT==SIEGED)){
if(ATYPE == A_ZOMBIE) /* don't utilize walls well */
armbonus += fort_val(&sct[xspot][yspot])/2;
else armbonus += fort_val(&sct[xspot][yspot]);
}
}
else if(side[num]==ATKR) {
if( (fort_val(&sct[xspot][yspot]) > 0)
&&( magic(country,SAPPER)==TRUE)) armbonus += 10;
if(ATYPE == A_MERCENARY) armbonus += MERCATT;
else armbonus += ntn[UOWNER(num)].aplus;
if(ASTAT==MAGATT) armbonus += 30;
if(ASTAT==SORTIE && (fort_val(&sct[xspot][yspot]) > 0)
&& sct[xspot][yspot].owner==country) {
armbonus += 10;
if((ATYPE==A_DRAGOON)||(ATYPE==A_LEGION)
||(ATYPE==A_PHALANX)) {
/* bonus for organization or riding cavalry */
armbonus += 5;
} else if ((ATYPE==A_LT_CAV)||(ATYPE==A_CAVALRY)) {
/* bonus for mounted sortie */
armbonus += 10;
} else if (avian(ATYPE)||ATYPE==A_ELEPHANT||ATYPE==A_KNIGHT) {
/* bonus for mounted or flying sortie */
armbonus += 15;
}
if ((ATYPE>=MINMONSTER)||(ATYPE<=MAXMONSTER)) {
/* bonus for monsters (scare factor) */
armbonus += 5;
}
}
}
/*army status is important*/
if(ASTAT==MARCH) armbonus-=40;
/*if a fortress*/
if(fort_val(&sct[xspot][yspot]) > 0){
/*Cavalry and Knights get -20%*/
if((ATYPE == A_CAVALRY) ||(ATYPE == A_KNIGHT)) armbonus -= 20;
/*Archers gain pluses*/
else if((ATYPE == A_ARCHER)&&(sct[xspot][yspot].owner==country))
armbonus += 15;
else if(ATYPE == A_ARCHER) armbonus += 5;
}
if(side[num]==ATKR) armbonus+= *(unitattack+(ATYPE%UTYPE));
else armbonus+= *(unitdefend+(ATYPE%UTYPE));
/*Phalanx and Legionaires need certain numbers of troops*/
if((ATYPE==A_PHALANX)||(ATYPE==A_LEGION)) {
if(ASOLD>1000){ armbonus+=20;
} else if(ASOLD>500) armbonus+=10;
}
return(armbonus);
}
void
fdxyretreat() /* finds retreat location */
{
int x,y,nation=(-1);
int xsctr= xspot;
int ysctr= yspot;
retreatx=xsctr;
retreaty=ysctr;
if((sct[xsctr][ysctr].designation==DTOWN)
||(sct[xsctr][ysctr].designation==DCAPITOL)
||(sct[xsctr][ysctr].designation==DCITY)){
retreatside=0;
return;
}
if(retreatside == ATKR) nation=anation;
else nation=dnation;
for(x= xsctr-1; x<=xsctr+1; x++)
for(y= ysctr-1; y<=ysctr+1; y++) if(ONMAP(x,y)){
if(tofood( &sct[x][y],
sct[x][y].owner == country ? country : 0)==0) continue;
if(((sct[x][y].owner == nation)
||(ntn[sct[x][y].owner].dstatus[nation] < NEUTRAL))
||(solds_in_sector( x, y, sct[x][y].owner) == 0)){
retreatx=x;
retreaty=y;
#ifdef DEBUG
printf("armies in %d %d retreat to %d %d\n",xsctr,ysctr,x,y);
#endif /* DEBUG */
return;
}
}
}
void
retreat(unitnum)
int unitnum; /* if -1 then normal, else retreat only unit ismerc */
{
int cnum;
if(retreatside == 0) return;
for(cnum=0;cnum<count;cnum++) if(owner[cnum]>(-1)){
if( unitnum != (-1) ) cnum=unitnum;
if(side[cnum] == retreatside){
if ((ntn[owner[cnum]].arm[unit[cnum]].unittyp==A_MARINES)||
(ntn[owner[cnum]].arm[unit[cnum]].unittyp==A_SAILOR)){
ntn[owner[cnum]].arm[unit[cnum]].sold *= 85;
ntn[owner[cnum]].arm[unit[cnum]].sold /= 100;
} else {
ntn[owner[cnum]].arm[unit[cnum]].xloc = retreatx;
ntn[owner[cnum]].arm[unit[cnum]].yloc = retreaty;
}
}
if( unitnum != (-1) ) return;
}
}
/*SUBROUTINE TO RUN NAVAL COMBAT ON ALL SHIPS */
/* quick define for easier reading */
#define QWAR 1
#define QGAL 2
#define QMER 3
/* just like fight, this takes array of owner,side,unit and calculates */
/* a random battle based on the strengths of the combatants. */
void
navalcbt()
{
int acrew=0,dcrew=0; /*a's and d's crew and soldier strength*/
int ahold=0,dhold=0; /*a's and d's warship strength*/
int awsunk=0,dwsunk=0; /*a's and d's warship losses for the round*/
int agsunk=0,dgsunk=0; /*a's and d's galley losses for the round*/
int amsunk=0,dmsunk=0; /*a's and d's merchent losses for the round*/
int awcapt=0,dwcapt=0; /*a's and d's warship captures for the round*/
int agcapt=0,dgcapt=0; /*a's and d's galley captures for the round*/
int amcapt=0,dmcapt=0; /*a's and d's merchant captures for the round*/
int akcrew=0,dkcrew=0; /*a's and d's crew losses for the round*/
char wnum[MGKNUM],gnum[MGKNUM],mnum[MGKNUM];
register int done,i,j,k;
int roll,odds,savecntry=country;
int PAloss, PDloss, Ploss, which, shipsize;
int thold, ghold, nvynum, armynum;
int dcptpct, acptpct, cptpct;
struct s_nation *saventn=curntn;
void show_ships(),capture();
printf("In Naval Combat....\n");
/* determine who is attacker & who is on defenders side?*/
for(j=0;j<count;j++) if(owner[j]!=(-1)){
if(owner[j]==anation) side[j]=ATKR;
else if(ntn[anation].dstatus[owner[j]]==JIHAD) side[j]=DFND;
else if(ntn[owner[j]].dstatus[anation]==JIHAD) side[j]=DFND;
else if(ntn[anation].dstatus[owner[j]]==WAR) side[j]=DFND;
else if(ntn[owner[j]].dstatus[anation]==WAR) side[j]=DFND;
else if((ntn[owner[j]].dstatus[anation]==TREATY)
&&(ntn[owner[j]].dstatus[dnation]>HOSTILE)) side[j]=ATKR;
else if((ntn[owner[j]].dstatus[anation]==ALLIED)
&&(ntn[owner[j]].dstatus[dnation]>HOSTILE)) side[j]=ATKR;
}
/* Loop through all competitors to determine
* relative combat strengths:
* men/unit strength
* crew on ship:
* warship crew 1
* galley crew 2
* merchant crew 4
* soldiers onboard:
* SAILOR/ARCHER 3/4
* MARINE 1/3
* others 4/3
*/
for(j=0;j<count;j++) if(owner[j]!=(-1)){
curntn= &ntn[owner[j]];
country= owner[j];
wnum[j]=SHIPS(ntn[country].nvy[unit[j]].warships,N_LIGHT)+
SHIPS(ntn[country].nvy[unit[j]].warships,N_MEDIUM)+
SHIPS(ntn[country].nvy[unit[j]].warships,N_HEAVY);
mnum[j]=SHIPS(ntn[country].nvy[unit[j]].merchant,N_LIGHT)+
SHIPS(ntn[country].nvy[unit[j]].merchant,N_MEDIUM)+
SHIPS(ntn[country].nvy[unit[j]].merchant,N_HEAVY);
gnum[j]=SHIPS(ntn[country].nvy[unit[j]].galleys,N_LIGHT)+
SHIPS(ntn[country].nvy[unit[j]].galleys,N_MEDIUM)+
SHIPS(ntn[country].nvy[unit[j]].galleys,N_HEAVY);
if(side[j]==DFND) {
if((k=fltwhold(unit[j]))>0) {
dhold += k;
if (magic(country,SAILOR)==TRUE) {
dcrew += 5*k*curntn->nvy[unit[j]].crew/4;
} else dcrew += k*curntn->nvy[unit[j]].crew;
}
if((k=fltmhold(unit[j]))>0) {
if (magic(country,SAILOR)==TRUE) {
dcrew += 5*k*curntn->nvy[unit[j]].crew/16;
} else dcrew += k*curntn->nvy[unit[j]].crew/4;
}
if((k=fltghold(unit[j]))>0) {
if (magic(country,SAILOR)==TRUE) {
dcrew += 5*k*curntn->nvy[unit[j]].crew/8;
} else dcrew += k*curntn->nvy[unit[j]].crew/2;
if(curntn->nvy[unit[j]].armynum!=MAXARM) {
k = curntn->nvy[unit[j]].armynum;
switch (curntn->arm[k].unittyp) {
case A_ARCHER:
case A_SAILOR:
dcrew += 3*curntn->arm[k].sold/2;
break;
case A_MARINES:
dcrew += 3*curntn->arm[k].sold;
break;
default:
dcrew += 3*curntn->arm[k].sold/4;
break;
}
}
}
} else if(side[j]==ATKR) {
if((k=fltwhold(unit[j]))>0) {
ahold += k;
if (magic(country,SAILOR)==TRUE) {
acrew += 5*k*curntn->nvy[unit[j]].crew/4;
} else acrew += k*curntn->nvy[unit[j]].crew;
}
if((k=fltmhold(unit[j]))>0) {
if (magic(country,SAILOR)==TRUE) {
acrew += 5*k*curntn->nvy[unit[j]].crew/16;
} else acrew += k*curntn->nvy[unit[j]].crew/4;
}
if((k=fltghold(unit[j]))>0) {
if (magic(country,SAILOR)==TRUE) {
acrew += 5*k*curntn->nvy[unit[j]].crew/8;
} else acrew += k*curntn->nvy[unit[j]].crew/2;
if(curntn->nvy[unit[j]].armynum!=MAXARM) {
k = curntn->nvy[unit[j]].armynum;
switch (curntn->arm[k].unittyp) {
case A_ARCHER:
case A_SAILOR:
acrew += 3*curntn->arm[k].sold/2;
break;
case A_MARINES:
acrew += 3*curntn->arm[k].sold;
break;
default:
acrew += 3*curntn->arm[k].sold/4;
break;
}
}
}
}
}
/*find battle odds*/
if( acrew > dcrew*100 ) odds=10000;
else if ( dcrew > acrew*100 ) odds=1;
else odds = (acrew*100)/dcrew;
/* calculate capture percentages */
/*
* This formula produces:
* 0% capture for 1:100 odds
* 2% capture for 1:10 odds
* 15% capture for 1:1 odds
* 60% capture for 10:1 odds
* 100% capture for >60:1 odds
* with linear progression between each.
*/
if (odds>6000) {
dcptpct=0;
acptpct=100;
} else if (odds>1000) {
dcptpct= (6000-odds)/2500;
acptpct= (odds-1000)/125+60;
} else if (odds>100) {
dcptpct= (1000-odds)/69+2;
acptpct= (odds-100)/20+15;
} else if (odds>10) {
dcptpct= (100-odds)/2+15;
acptpct= (odds-10)/6.9+2;
} else if (odds>6) {
dcptpct= (10-odds)*14+60;
acptpct= (odds-6)/2;
} else {
dcptpct= 100;
acptpct= 0;
}
#ifdef DEBUG