1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  damage.c: Damage stuff.
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  *     Steve McClure, 1997
32  *     Markus Armbruster, 2004-2012
33  */
34 
35 #include <config.h>
36 
37 #include "chance.h"
38 #include "damage.h"
39 #include "land.h"
40 #include "misc.h"
41 #include "nsc.h"
42 #include "nuke.h"
43 #include "optlist.h"
44 #include "plane.h"
45 #include "prototypes.h"
46 #include "ship.h"
47 
48 void
item_damage(int pct,short * item)49 item_damage(int pct, short *item)
50 {
51     int lose;
52     i_type i;
53 
54     for (i = I_NONE + 1; i <= I_MAX; ++i) {
55 	if (opt_SUPER_BARS && i == I_BAR)
56 	    continue;
57 	lose = roundavg((double)item[i] * pct * 0.01);
58 	if (i == I_CIVIL || i == I_MILIT || i == I_UW)
59 	    lose = ldround(people_damage * lose, 1);
60 	item[i] = item[i] >= lose ? item[i] - lose : 0;
61     }
62 }
63 
64 void
ship_damage(struct shpstr * sp,int dam)65 ship_damage(struct shpstr *sp, int dam)
66 {
67     if (dam <= 0)
68 	return;
69     if (dam > 100)
70 	dam = 100;
71 
72     mpr(sp->shp_own, "\t%s takes %d\n", prship(sp), dam);
73 
74     sp->shp_effic = damage((int)sp->shp_effic, dam);
75     if (sp->shp_mobil > 0)
76 	sp->shp_mobil = damage((int)sp->shp_mobil, dam);
77     item_damage(dam, sp->shp_item);
78 }
79 
80 void
shipdamage(struct shpstr * sp,int dam)81 shipdamage(struct shpstr *sp, int dam)
82 {
83     ship_damage(sp, (int)(dam / (1.0 + shp_armor(sp) / 100.0)));
84 }
85 
86 void
land_damage(struct lndstr * lp,int dam)87 land_damage(struct lndstr *lp, int dam)
88 {
89     if (dam <= 0)
90 	return;
91     if (dam > 100)
92 	dam = 100;
93 
94     mpr(lp->lnd_own, "\t%s takes %d\n", prland(lp), dam);
95     if (lchr[(int)lp->lnd_type].l_flags & L_SPY) {
96 	/* Spies die! */
97 	lp->lnd_effic = 0;
98     } else {
99 	lp->lnd_effic = damage((int)lp->lnd_effic, dam);
100 	if (lp->lnd_mobil > 0)
101 	    lp->lnd_mobil = damage((int)lp->lnd_mobil, dam);
102 	item_damage(dam, lp->lnd_item);
103     }
104 }
105 
106 void
landdamage(struct lndstr * lp,int dam)107 landdamage(struct lndstr *lp, int dam)
108 {
109     double damage_factor, m;
110 
111     m = land_mob_max;
112 
113     /* fortification reduces damage */
114     damage_factor = m / (m + lp->lnd_harden);
115 
116     /* vulnerable units take more damage */
117     damage_factor *= lnd_vul(lp) / 100.0;
118 
119     land_damage(lp, ldround(damage_factor * dam, 1));
120 }
121 
122 void
planedamage(struct plnstr * pp,int dam)123 planedamage(struct plnstr *pp, int dam)
124 {
125     if (dam <= 0)
126 	return;
127     if (dam > 100)
128 	dam = 100;
129 
130     mpr(pp->pln_own, "\t%s takes %d\n", prplane(pp), dam);
131     pp->pln_effic = damage((int)pp->pln_effic, dam);
132     if (pp->pln_mobil > 0)
133 	pp->pln_mobil = damage((int)pp->pln_mobil, dam);
134 }
135 
136 /*
137  * nukedamage() actually just calculates damage
138  * rather than inflicting it.
139  */
140 int
nukedamage(struct nchrstr * ncp,int range,int airburst)141 nukedamage(struct nchrstr *ncp, int range, int airburst)
142 {
143     int dam;
144     int rad;
145 
146     rad = ncp->n_blast;
147     if (airburst)
148 	rad = (int)(rad * 1.5);
149     if (rad < range)
150 	return 0;
151     if (airburst) {
152 	/* larger area, less center damage */
153 	dam = (int)((ncp->n_dam * 0.75) - (range * 20));
154     } else {
155 	/* smaller area, more center damage */
156 	dam = (int)(ncp->n_dam / (range + 1.0));
157     }
158     if (dam < 5)
159 	dam = 0;
160     return dam;
161 }
162 
163 int
damage(int amt,int pct)164 damage(int amt, int pct)
165 {
166     if (amt <= 0)
167 	return 0;
168     return amt - roundavg(amt * (pct / 100.0));
169 }
170 
171 /* asymptotic damage to commodities, efficiency, and sectors */
172 int
effdamage(int amt,int dam)173 effdamage(int amt, int dam)
174 {
175     return damage(amt, PERCENT_DAMAGE(dam));
176 }
177 
178 int
commdamage(int amt,int dam,i_type vtype)179 commdamage(int amt, int dam, i_type vtype)
180 {
181     int lost;
182 
183     if (vtype == I_BAR && opt_SUPER_BARS)
184 	return amt;
185 
186     lost = amt - effdamage(amt, dam);
187 
188     if (vtype == I_MILIT || vtype == I_CIVIL || vtype == I_UW)
189 	lost = ldround(people_damage * lost, 1);
190     return amt - lost;
191 }
192