/
battle.c
4108 lines (3637 loc) · 122 KB
/
battle.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
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "battle.h"
#include "alchemy.h"
#include "guard.h"
#include "laws.h"
#include "monsters.h"
#include "move.h"
#include "study.h"
#include "spy.h"
#include "reports.h"
/* attributes includes */
#include "attributes/key.h"
#include "attributes/racename.h"
#include "attributes/otherfaction.h"
#include "spells/buildingcurse.h"
#include "spells/regioncurse.h"
#include "spells/unitcurse.h"
#include "kernel/ally.h"
#include "kernel/alliance.h"
#include "kernel/build.h"
#include "kernel/building.h"
#include "kernel/callbacks.h"
#include "kernel/config.h"
#include "kernel/curse.h"
#include "kernel/equipment.h"
#include "kernel/faction.h"
#include "kernel/group.h"
#include "kernel/item.h"
#include "kernel/messages.h"
#include "kernel/order.h"
#include "kernel/plane.h"
#include "kernel/race.h"
#include "kernel/region.h"
#include "kernel/ship.h"
#include "kernel/skill.h"
#include "kernel/terrain.h"
#include "kernel/unit.h"
#include "kernel/spell.h"
/* util includes */
#include "kernel/attrib.h"
#include "util/base36.h"
#include "util/language.h"
#include "util/lists.h"
#include "util/log.h"
#include "util/macros.h"
#include "util/message.h"
#include "util/parser.h"
#include "util/stats.h"
#include "util/rand.h"
#include "util/rng.h"
#include <strings.h>
#include <selist.h>
/* libc includes */
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stb_ds.h>
#define TACTICS_BONUS 1 /* when undefined, we have a tactics round. else this is the bonus tactics give */
#define TACTICS_MODIFIER 1 /* modifier for generals in the front/rear */
#define BASE_CHANCE 70 /* 70% Basis-Ueberlebenschance */
#define TDIFF_CHANGE 5 /* 5% hoeher pro Stufe */
#define DAMAGE_QUOTIENT 2 /* damage += skilldiff/DAMAGE_QUOTIENT */
#define DEBUG_SELECT /* should be disabled if select_enemy works */
typedef enum combatmagic {
DO_PRECOMBATSPELL,
DO_POSTCOMBATSPELL
} combatmagic_t;
#define MINSPELLRANGE 1
#define MAXSPELLRANGE 7
#define ROW_FACTOR 3 /* factor for combat row advancement rule */
#define EFFECT_PANIC_SPELL 25
#define TROLL_REGENERATION 0.10
/* Nach dem alten System: */
static int missile_range[2] = { FIGHT_ROW, BEHIND_ROW };
static int melee_range[2] = { FIGHT_ROW, FIGHT_ROW };
const troop no_troop = { 0, 0 };
#define FORMULA_ORIG 0
#define FORMULA_NEW 1
#define LOOT_MONSTERS (1<<0)
#define LOOT_SELF (1<<1) /* code is mutually exclusive with LOOT_OTHERS */
#define LOOT_OTHERS (1<<2)
#define LOOT_KEEPLOOT (1<<4)
#define DAMAGE_CRITICAL (1<<0)
#define DAMAGE_SKILL_BONUS (1<<4)
static int max_turns;
static int rule_damage;
static int rule_loot;
static double loot_divisor;
static int flee_chance_max_percent;
static int flee_chance_base;
static int flee_chance_skill_bonus;
static int skill_formula;
static int rule_cavalry_skill;
static int rule_population_damage;
static unsigned char rule_hero_speed;
static bool rule_anon_battle;
static bool rule_igjarjuk_curse;
static int rule_goblin_bonus;
static int rule_tactics_formula;
static int rule_nat_armor;
static int rule_cavalry_mode;
static int rule_vampire;
static const item_type *it_mistletoe;
/** initialize rules from configuration.
*/
static void init_rules(void)
{
it_mistletoe = it_find("mistletoe");
flee_chance_skill_bonus = config_get_int("rules.combat.flee_chance_bonus", 5);
flee_chance_base = config_get_int("rules.combat.flee_chance_base", 20);
flee_chance_max_percent = config_get_int("rules.combat.flee_chance_limit", 90);
rule_nat_armor = config_get_int("rules.combat.nat_armor", 0);
rule_tactics_formula = config_get_int("rules.tactics.formula", 0);
rule_goblin_bonus = config_get_int("rules.combat.goblinbonus", 10);
rule_hero_speed = (unsigned char)config_get_int("rules.heroes.combat_speed", 10);
rule_population_damage = config_get_int("rules.combat.populationdamage", 20);
rule_anon_battle = config_get_int("rules.stealth.anon_battle", 1) != 0;
rule_igjarjuk_curse = config_get_int("rules.combat.igjarjuk_curse", 0) != 0;
rule_cavalry_mode = config_get_int("rules.cavalry.mode", 1);
rule_cavalry_skill = config_get_int("rules.cavalry.skill", 2);
rule_vampire = config_get_int("rules.combat.demon_vampire", 0);
rule_loot = config_get_int("rules.combat.loot",
LOOT_MONSTERS | LOOT_OTHERS | LOOT_KEEPLOOT);
loot_divisor = config_get_flt("rules.items.loot_divisor", 1);
/* new formula to calculate to-hit-chance */
skill_formula = config_get_int("rules.combat.skill_formula",
FORMULA_ORIG);
/* maximum number of combat turns */
max_turns = config_get_int("rules.combat.turns", 5);
/* damage calculation */
if (config_get_int("rules.combat.critical", 1)) {
rule_damage |= DAMAGE_CRITICAL;
}
if (config_get_int("rules.combat.skill_bonus", 1)) {
rule_damage |= DAMAGE_SKILL_BONUS;
}
}
static int army_index(const side * s)
{
return s->index;
}
const char *sidename(const side * s)
{
#define SIDENAMEBUFLEN 256
static int bufno; /* STATIC_XCALL: used across calls */
static char sidename_buf[4][SIDENAMEBUFLEN]; /* STATIC_RESULT: used for return, not across calls */
bufno = bufno % 4;
str_strlcpy(sidename_buf[bufno], factionname(s->stealthfaction ? s->stealthfaction : s->faction), SIDENAMEBUFLEN);
return sidename_buf[bufno++];
}
static const char *sideabkz(side * s, bool truename)
{
static char sideabkz_buf[8]; /* STATIC_RESULT: used for return, not across calls */
const faction *f = (s->stealthfaction
&& !truename) ? s->stealthfaction : s->faction;
str_strlcpy(sideabkz_buf, itoa36(f->no), sizeof(sideabkz_buf));
return sideabkz_buf;
}
void battle_message_faction(battle * b, faction * f, struct message *m)
{
region *r = b->region;
assert(f);
if (f->battles == NULL || f->battles->r != r) {
struct bmsg *bm = (struct bmsg *)calloc(1, sizeof(struct bmsg));
assert(bm);
bm->next = f->battles;
f->battles = bm;
bm->r = r;
}
add_message(&f->battles->msgs, m);
}
void message_all(battle * b, message * m)
{
bfaction *bf;
for (bf = b->factions; bf; bf = bf->next) {
assert(bf->faction);
battle_message_faction(b, bf->faction, m);
}
}
static void fbattlerecord(battle * b, faction * f, const char *s)
{
message *m = msg_message("battle_msg", "string", s);
battle_message_faction(b, f, m);
msg_release(m);
}
/* being an enemy or a friend is (and must always be!) symmetrical */
#define enemy_i(as, di) (as->relations[di]&E_ENEMY)
#define friendly_i(as, di) (as->relations[di]&E_FRIEND)
#define enemy(as, ds) (as->relations[ds->index]&E_ENEMY)
#define friendly(as, ds) (as->relations[ds->index]&E_FRIEND)
static bool set_enemy(side * as, side * ds, bool attacking)
{
int i;
assert(as && ds);
for (i = 0; i != MAXSIDES; ++i) {
if (ds->enemies[i] == NULL)
ds->enemies[i] = as;
if (ds->enemies[i] == as)
break;
}
for (i = 0; i != MAXSIDES; ++i) {
if (as->enemies[i] == NULL)
as->enemies[i] = ds;
if (as->enemies[i] == ds)
break;
}
assert(i != MAXSIDES);
if (attacking)
as->relations[ds->index] |= E_ATTACKING;
if ((ds->relations[as->index] & E_ENEMY) == 0) {
/* enemy-relation are always symmetrical */
assert((as->relations[ds->index] & (E_ENEMY | E_FRIEND)) == 0);
ds->relations[as->index] |= E_ENEMY;
as->relations[ds->index] |= E_ENEMY;
return true;
}
return false;
}
static void set_friendly(side * as, side * ds)
{
assert((as->relations[ds->index] & E_ENEMY) == 0);
ds->relations[as->index] |= E_FRIEND;
as->relations[ds->index] |= E_FRIEND;
}
static bool alliedside(const side * s, const faction * f, int mode)
{
if (s->faction == f) {
return true;
}
if (s->group) {
return alliedgroup(s->faction, f, s->group, mode) != 0;
}
return alliedfaction(s->faction, f, mode) != 0;
}
static int dead_fighters(const fighter * df)
{
return df->unit->number - df->alive - df->run.number;
}
fighter *select_corpse(battle * b, fighter * af)
/* Waehlt eine Leiche aus, der af hilft. casualties ist die Anzahl der
* Toten auf allen Seiten (im Array). Wenn af == NULL, wird die
* Parteizugehoerigkeit ignoriert, und irgendeine Leiche genommen.
*
* Untote werden nicht ausgewaehlt (casualties, not dead) */
{
int si, maxcasualties = 0;
fighter *df;
for (si = 0; si != b->nsides; ++si) {
side *s = b->sides + si;
if (af == NULL || (!enemy_i(af->side, si) && alliedside(af->side, s->faction, HELP_FIGHT))) {
maxcasualties += s->casualties;
}
}
if (maxcasualties > 0) {
int di = (int)(rng_int() % maxcasualties);
side *s;
for (s = b->sides; s != b->sides + b->nsides; ++s) {
for (df = s->fighters; df; df = df->next) {
/* Geflohene haben auch 0 hp, duerfen hier aber nicht ausgewaehlt
* werden! */
int dead = dead_fighters(df);
const race *rc = u_race(df->unit);
/* Untote sinc f�r immer tot */
if (!undeadrace(rc)) {
if (af && !helping(af->side, df->side))
continue;
if (di < dead) {
return df;
}
di -= dead;
}
}
}
}
return NULL;
}
bool helping(const side * as, const side * ds)
{
if (as->faction == ds->faction)
return true;
return (!enemy(as, ds) && alliedside(as, ds->faction, HELP_FIGHT));
}
int statusrow(int status)
{
switch (status) {
case ST_AGGRO:
case ST_FIGHT:
return FIGHT_ROW;
case ST_BEHIND:
case ST_CHICKEN:
return BEHIND_ROW;
case ST_AVOID:
return AVOID_ROW;
case ST_FLEE:
return FLEE_ROW;
default:
assert(!"unknown combatrow");
}
return FIGHT_ROW;
}
static double hpflee(int status)
/* if hp drop below this percentage, run away */
{
switch (status) {
case ST_AGGRO:
return 0.0;
case ST_FIGHT:
case ST_BEHIND:
return 0.2;
case ST_CHICKEN:
case ST_AVOID:
return 0.9;
case ST_FLEE:
return 1.0;
default:
assert(!"unknown combatrow");
}
return 0.0;
}
static int get_row(const side * s, int row, const side * vs)
{
bool counted[MAXSIDES];
int enemyfront = 0;
int line, result;
int retreat = 0;
int size[NUMROWS];
battle *b = s->battle;
memset(counted, 0, sizeof(counted));
memset(size, 0, sizeof(size));
for (line = FIRST_ROW; line != NUMROWS; ++line) {
int si, sa_i;
/* how many enemies are there in this row? */
for (si = 0; s->enemies[si]; ++si) {
side *se = s->enemies[si];
if (se->size[line] > 0) {
enemyfront += se->size[line];
/* - s->nonblockers[line] (nicht, weil angreifer) */
}
}
for (sa_i = 0; sa_i != b->nsides; ++sa_i) {
side *sa = b->sides + sa_i;
/* count people that like me, but don't like my enemy */
if (friendly_i(s, sa_i) && enemy_i(vs, sa_i)) {
if (!counted[sa_i]) {
int i;
for (i = 0; i != NUMROWS; ++i) {
size[i] += sa->size[i] - sa->nonblockers[i];
}
counted[sa_i] = true;
}
}
}
if (enemyfront)
break;
}
if (enemyfront) {
int front = 0;
for (line = FIRST_ROW; line != NUMROWS; ++line) {
front += size[line];
if (!front || front < enemyfront / ROW_FACTOR)
++retreat;
else if (front)
break;
}
}
/* every entry in the size[] array means someone trying to defend us.
* 'retreat' is the number of rows falling.
*/
result = row - retreat;
if (result < FIRST_ROW) result = FIRST_ROW;
return result;
}
int get_unitrow(const fighter * af, const side * vs)
{
int row = statusrow(af->status);
if (vs == NULL) {
int i;
for (i = FIGHT_ROW; i != row; ++i) {
if (af->side->size[i]) {
break;
}
}
return FIGHT_ROW + (row - i);
}
return get_row(af->side, row, vs);
}
static void reportcasualties(battle * b, fighter * fig, int dead)
{
struct message *m;
region *r = NULL;
if (fig->alive == fig->unit->number)
return;
m = msg_message("casualties", "unit runto run alive fallen",
fig->unit, r, fig->run.number, fig->alive, dead);
message_all(b, m);
msg_release(m);
}
static bool
contest_classic(int skilldiff, const armor_type * ar, const armor_type * sh)
{
int p, vw = BASE_CHANCE - TDIFF_CHANGE * skilldiff;
double mod = 1.0;
if (ar != NULL)
mod *= (1 + ar->penalty);
if (sh != NULL)
mod *= (1 + sh->penalty);
vw = (int)(100.0 - ((100.0 - (double)vw) * mod));
do {
p = (int)(rng_int() % 100);
vw -= p;
} while (vw >= 0 && p >= 90);
return (vw <= 0);
}
/** new rule for Eressea 1.5
* \param skilldiff - the attack skill with every modifier applied
*/
static bool
contest_new(int skilldiff, const troop dt, const armor_type * ar,
const armor_type * sh)
{
double tohit = 0.5 + skilldiff * 0.1;
UNUSED_ARG(sh);
UNUSED_ARG(ar);
if (tohit < 0.5)
tohit = 0.5;
if (chance(tohit)) {
int defense = effskill(dt.fighter->unit, SK_STAMINA, dt.fighter->unit->region);
double tosave = defense * 0.05;
return !chance(tosave);
}
return false;
}
static bool
contest(int skdiff, const troop dt, const armor_type * ar,
const armor_type * sh)
{
if (skill_formula == FORMULA_ORIG) {
return contest_classic(skdiff, ar, sh);
}
else {
return contest_new(skdiff, dt, ar, sh);
}
}
static bool is_riding(const troop t)
{
if (t.fighter->building != NULL)
return false;
if (t.fighter->horses + t.fighter->elvenhorses > t.index)
return true;
return false;
}
static const weapon *preferred_weapon(const troop t, bool attacking)
{
const weapon *missile = t.fighter->person[t.index].missile;
const weapon *melee = t.fighter->person[t.index].melee;
if (!melee) {
return missile;
}
if (missile) {
if (attacking) {
if (missile && missile->attackskill > melee->attackskill) {
return missile;
}
}
else {
if (missile && missile->defenseskill > melee->defenseskill) {
return missile;
}
}
}
return melee;
}
const struct weapon *select_weapon(const troop t, bool attacking, bool ismissile)
/* select the primary weapon for this trooper */
{
const weapon *w = NULL;
if (attacking) {
if (ismissile) {
/* from the back rows, have to use your missile weapon */
w = t.fighter->person[t.index].missile;
}
}
else {
if (!ismissile) {
/* have to use your melee weapon if it's melee */
w = t.fighter->person[t.index].melee;
}
}
if (!w) {
w = preferred_weapon(t, attacking);
}
return w;
}
static bool i_canuse(const unit * u, const item_type * itype)
{
return rc_can_use(u_race(u), itype);
}
static int
weapon_skill(const weapon_type * wtype, const unit * u, bool attacking)
/* the 'pure' skill when using this weapon to attack or defend.
* only undiscriminate modifiers (not affected by troops or enemies)
* are taken into account, e.g. no horses, magic, etc. */
{
int skill;
const race * rc = u_race(u);
if (wtype == NULL) {
skill = effskill(u, SK_WEAPONLESS, NULL);
int def = attacking ? rc->at_default : rc->df_default;
if (skill <= 0) {
/* wenn kein waffenloser kampf, dann den rassen-defaultwert */
if (rc == get_race(RC_ORC)) {
int sword = effskill(u, SK_MELEE, NULL);
int spear = effskill(u, SK_SPEAR, NULL);
skill = ((sword > spear) ? sword : spear) - 3;
}
}
if (def > skill) skill = def;
if (attacking) {
skill += rc->at_bonus;
if (fval(u->region->terrain, SEA_REGION) && u->ship)
skill += u->ship->type->at_bonus;
}
else {
skill += rc->df_bonus;
if (fval(u->region->terrain, SEA_REGION) && u->ship)
skill += u->ship->type->df_bonus;
}
}
else {
/* changed: if we own a weapon, we have at least a skill of 0 */
if (!i_canuse(u, wtype->itype))
return -1;
skill = effskill(u, wtype->skill, NULL);
if (skill > 0) {
if (attacking) {
skill += rc->at_bonus;
}
else {
skill += rc->df_bonus;
}
}
if (attacking) {
skill += wtype->offmod;
}
else {
skill += wtype->defmod;
}
}
return skill;
}
static int CavalrySkill(void)
{
return rule_cavalry_skill;
}
#define BONUS_SKILL 1
#define BONUS_DAMAGE 2
static int CavalryBonus(const unit * u, troop enemy, int type)
{
if (rule_cavalry_mode == 0) {
/* old rule, Eressea 1.0 compat */
return (type == BONUS_SKILL) ? 2 : 0;
}
else {
/* new rule, chargers in Eressea 1.1 */
int skl = effskill(u, SK_RIDING, NULL);
/* only half against trolls */
if (skl > 0) {
if (type == BONUS_SKILL) {
int dmg = (skl < 8) ? skl : 8;
if (u_race(enemy.fighter->unit) == get_race(RC_TROLL)) {
dmg = dmg / 4;
}
else {
dmg = dmg / 2;
}
return dmg;
}
else {
skl = skl / 2;
if (skl > 4) skl = 4;
return skl;
}
}
}
return 0;
}
/**
* Effektiver Waffenskill waehrend des Kampfes.
*/
static int
weapon_effskill(troop t, troop enemy, const weapon * w,
bool attacking, bool missile)
{
fighter *tf = t.fighter;
unit *tu = t.fighter->unit;
/* Alle Modifier berechnen, die fig durch die Waffen bekommt. */
if (w && w->item.type) {
int skill = 0;
const weapon_type *wtype = resource2weapon(w->item.type->rtype);
if (attacking) {
skill = w->attackskill;
}
else {
skill = w->defenseskill;
}
if (wtype->modifiers != NULL) {
/* Pferdebonus, Lanzenbonus, usw. */
const race *rc = u_race(tu);
int m;
unsigned int flags =
WMF_SKILL | (attacking ? WMF_OFFENSIVE : WMF_DEFENSIVE);
if (is_riding(t))
flags |= WMF_RIDING;
else
flags |= WMF_WALKING;
if (is_riding(enemy))
flags |= WMF_AGAINST_RIDING;
else
flags |= WMF_AGAINST_WALKING;
for (m = 0; wtype->modifiers[m].value; ++m) {
if ((wtype->modifiers[m].flags & flags) == flags) {
int mask = wtype->modifiers[m].race_mask;
if ((mask == 0) || (mask & rc->mask_item)) {
skill += wtype->modifiers[m].value;
}
}
}
}
/* Burgenbonus, Pferdebonus */
if (is_riding(t) && (wtype == NULL || (fval(wtype, WTF_HORSEBONUS)
&& !fval(wtype, WTF_MISSILE)))) {
skill += CavalryBonus(tu, enemy, BONUS_SKILL);
}
if (t.index < tf->elvenhorses) {
/* Elfenpferde: Helfen dem Reiter, egal ob und welche Waffe. Das ist
* eleganter, und vor allem einfacher, sonst muss man noch ein
* WMF_ELVENHORSE einbauen. */
skill += 2;
}
if (skill > 0 && !attacking && missile) {
/*
* Wenn ich verteidige, und nicht direkt meinem Feind gegenueberstehe,
* halbiert sich mein Skill: (z.B. gegen Fernkaempfer. Nahkaempfer
* koennen mich eh nicht treffen)
*/
skill /= 2;
}
return skill;
}
/* no weapon: fight weaponless */
return weapon_skill(NULL, tu, attacking);
}
const armor_type *select_armor(troop t, bool shield)
{
unsigned int type = shield ? ATF_SHIELD : 0;
unit *u = t.fighter->unit;
const armor *a = t.fighter->armors;
int geschuetzt = 0;
/* some monsters should not use armor (dragons in chainmail? ha!) */
if (!(u_race(u)->battle_flags & BF_EQUIPMENT))
return NULL;
/* ... neither do werewolves */
if (fval(u, UFL_WERE)) {
return NULL;
}
for (; a; a = a->next) {
if ((a->atype->flags & ATF_SHIELD) == type) {
geschuetzt += a->count;
if (geschuetzt > t.index) {
/* unser Kandidat wird geschuetzt */
return a->atype;
}
}
}
return NULL;
}
/* Hier ist zu beachten, ob und wie sich Zauber und Artefakte, die
* Ruestungschutz geben, addieren.
* - Artefakt "trollbelt" gibt Ruestung +1
* - Zauber Rindenhaut gibt Ruestung +3
*/
static int trollbelts(const unit *u) {
const struct resource_type *belt = rt_find("trollbelt");
return belt ? i_get(u->items, belt->itype) : 0;
}
int select_magicarmor(troop t)
{
unit *u = t.fighter->unit;
int ma = 0;
if (trollbelts(u) > t.index) /* unser Kandidat wird geschuetzt */
ma += 1;
return ma;
}
int meffect_apply(struct meffect *me, int damage) {
assert(0 <= damage); /* damage sollte hier immer mindestens 0 sein */
/* jeder Schaden wird um effect% reduziert bis der Schild duration
* Trefferpunkte aufgefangen hat */
if (me->typ == SHIELD_REDUCE && me->effect <= 100) {
int hp = damage * me->effect / 100;
if (hp > me->duration) {
hp = me->duration;
}
damage -= hp;
me->duration -= hp;
}
/* gibt Ruestung +effect fuer duration Treffer */
else if (me->typ == SHIELD_ARMOR) {
damage -= me->effect;
if (damage < 0) damage = 0;
me->duration--;
}
return damage;
}
/* Sind side ds und Magier des meffect verbuendet? */
bool meffect_protection(battle * b, meffect * s, side * ds)
{
UNUSED_ARG(b);
if (!s->magician->alive)
return false;
if (s->duration <= 0)
return false;
if (enemy(s->magician->side, ds))
return false;
if (alliedside(s->magician->side, ds->faction, HELP_FIGHT))
return true;
return false;
}
/* Sind side as und Magier des meffect verfeindet? */
bool meffect_blocked(battle * b, meffect * s, side * as)
{
UNUSED_ARG(b);
if (!s->magician->alive)
return false;
if (s->duration <= 0)
return false;
if (enemy(s->magician->side, as))
return true;
return false;
}
/* rmfighter wird schon im PRAECOMBAT gebraucht, da gibt es noch keine
* troops */
void reduce_fighter(fighter * df, int i)
{
side *ds = df->side;
/* nicht mehr personen abziehen, als in der Einheit am Leben sind */
assert(df->alive >= i);
assert(df->alive <= df->unit->number);
/* erst ziehen wir die Anzahl der Personen von den Kaempfern in der
* Schlacht, dann von denen auf dieser Seite ab*/
df->side->alive -= i;
/* Dann die Kampfreihen aktualisieren */
ds->size[SUM_ROW] -= i;
ds->size[statusrow(df->status)] -= i;
/* Spezialwirkungen, z.B. Schattenritter */
if (u_race(df->unit)->battle_flags & BF_NOBLOCK) {
ds->nonblockers[SUM_ROW] -= i;
ds->nonblockers[statusrow(df->status)] -= i;
}
/* und die Einheit selbst aktualisieren */
df->alive -= i;
}
void flee_all(fighter *fig)
{
unit *u = fig->unit;
fig->run.hp = u->hp;
fig->run.number = u->number;
fig->side->flee += u->number;
setguard(u, false);
reduce_fighter(fig, u->number);
}
static void rmtroop(troop dt)
{
fighter *df = dt.fighter;
/* troop ist immer eine einzelne Person */
reduce_fighter(df, 1);
assert(dt.index >= 0 && dt.index < df->unit->number);
if (dt.index != df->alive - df->removed) {
df->person[dt.index] = df->person[df->alive - df->removed];
}
if (df->removed) {
df->person[df->alive - df->removed] = df->person[df->alive];
}
df->person[df->alive].hp = 0;
}
void remove_troop(troop dt)
{
fighter *df = dt.fighter;
struct person p = df->person[dt.index];
++df->removed;
++df->side->removed;
df->person[dt.index] = df->person[df->alive - df->removed];
df->person[df->alive - df->removed] = p;
}
void kill_troop(troop dt)
{
fighter *df = dt.fighter;
unit *du = df->unit;
rmtroop(dt);
if (!df->alive) {
char eqname[64];
const race *rc = u_race(du);
item *drops = item_spoil(rc, du->number - df->run.number);
if (drops != NULL) {
i_merge(&du->items, &drops);
}
sprintf(eqname, "spo_%s", rc->_name);
equip_unit_mask(du, eqname, EQUIP_ITEMS);
}
}
/** reduces the target's exp by an equivalent of n points learning
* 30 points = 1 week
*/
void drain_exp(struct unit *u, int n)
{
skill_t sk = (skill_t)(rng_int() % MAXSKILLS);
skill_t ssk;
/* TODO (enno): we can use u->skill_size to find a random skill */
ssk = sk;
while (get_level(u, sk) == 0) {
sk++;
if (sk == MAXSKILLS)
sk = 0;
if (sk == ssk) {
sk = NOSKILL;
break;
}
}
if (sk != NOSKILL) {
change_skill_days(u, sk, -n);
}
}
static void vampirism(troop at, int damage)
{
const unit *au = at.fighter->unit;
if (u_race(au) == get_race(RC_DAEMON)) {
if (rule_vampire > 0) {
int gain = damage / rule_vampire;
int chance = damage - rule_vampire * gain;
if (chance > 0 && (rng_int() % rule_vampire < chance))
++gain;
if (gain > 0) {
int maxhp = unit_max_hp(at.fighter->unit);
gain += at.fighter->person[at.index].hp;
if (maxhp > gain) maxhp = gain;
at.fighter->person[at.index].hp = maxhp;
}
}
}
}
static void ship_damage(int turn, unit *du) {
if (turn > 1) {
/* someone on the ship got damaged, damage the ship */
ship *sh = du->ship ? du->ship : leftship(du);
if (sh)
fset(sh, SF_DAMAGED);
}
}
#define MAXRACES 128
int natural_armor(unit * du)
{
const race *rc = u_race(du);
int an;
assert(rc);
an = rc_armor_bonus(rc);
if (an > 0) {
int sk = effskill(du, SK_STAMINA, NULL);
return rc->armor + sk / an;
}
return rc->armor;
}
static int rc_specialdamage(const unit *au, const unit *du, const struct weapon_type *wtype)
{
const race *ar = u_race(au);
int modifier = 0;
if (wtype != NULL) {
if (fval(u_race(du), RCF_DRAGON)) {
static int cache;
static const race *rc_halfling;
if (rc_changed(&cache)) {
rc_halfling = get_race(RC_HALFLING);
}
if (ar == rc_halfling) {
modifier += 5;
}
}
if (wtype->modifiers != NULL) {
int m;
for (m = 0; wtype->modifiers[m].value; ++m) {