]> git.pond.sub.org Git - empserver/blob - src/lib/subs/damage.c
License upgrade to GPL version 3 or later
[empserver] / src / lib / subs / damage.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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  */
33
34 #include <config.h>
35
36 #include "damage.h"
37 #include "land.h"
38 #include "misc.h"
39 #include "nsc.h"
40 #include "nuke.h"
41 #include "optlist.h"
42 #include "plane.h"
43 #include "prototypes.h"
44 #include "sect.h"
45 #include "ship.h"
46
47 void
48 item_damage(int pct, short *item)
49 {
50     int lose;
51     i_type i;
52
53     for (i = I_NONE + 1; i <= I_MAX; ++i) {
54         if (opt_SUPER_BARS && i == I_BAR)
55             continue;
56         lose = roundavg((double)item[i] * pct * 0.01);
57         if (i == I_CIVIL || i == I_MILIT || i == I_UW)
58             lose = ldround(people_damage * lose, 1);
59         item[i] = item[i] >= lose ? item[i] - lose : 0;
60     }
61 }
62
63 void
64 ship_damage(struct shpstr *sp, int dam)
65 {
66     if (dam <= 0)
67         return;
68     if (dam > 100)
69         dam = 100;
70
71     mpr(sp->shp_own, "\t%s takes %d\n", prship(sp), dam);
72
73     sp->shp_effic = damage((int)sp->shp_effic, dam);
74     if (sp->shp_mobil > 0)
75         sp->shp_mobil = damage((int)sp->shp_mobil, dam);
76     item_damage(dam, sp->shp_item);
77 }
78
79 void
80 shipdamage(struct shpstr *sp, int dam)
81 {
82     ship_damage(sp, (int)(dam / (1.0 + shp_armor(sp) / 100.0)));
83 }
84
85 void
86 land_damage(struct lndstr *lp, int dam)
87 {
88     if (dam <= 0)
89         return;
90     if (dam > 100)
91         dam = 100;
92
93     mpr(lp->lnd_own, "\t%s takes %d\n", prland(lp), dam);
94     if (lchr[(int)lp->lnd_type].l_flags & L_SPY) {
95         /* Spies die! */
96         lp->lnd_effic = 0;
97     } else {
98         lp->lnd_effic = damage((int)lp->lnd_effic, dam);
99         if (lp->lnd_mobil > 0)
100             lp->lnd_mobil = damage((int)lp->lnd_mobil, dam);
101         item_damage(dam, lp->lnd_item);
102     }
103 }
104
105 void
106 landdamage(struct lndstr *lp, int dam)
107 {
108     double damage_factor, m;
109
110     m = land_mob_max;
111
112     /* fortification reduces damage */
113     damage_factor = m / (m + lp->lnd_harden);
114
115     /* vulnerable units take more damage */
116     damage_factor *= lnd_vul(lp) / 100.0;
117
118     land_damage(lp, ldround(damage_factor * dam, 1));
119 }
120
121 void
122 planedamage(struct plnstr *pp, int dam)
123 {
124     if (dam <= 0)
125         return;
126     if (dam > 100)
127         dam = 100;
128
129     mpr(pp->pln_own, "\t%s takes %d\n", prplane(pp), dam);
130     pp->pln_effic = damage((int)pp->pln_effic, dam);
131     if (pp->pln_mobil > 0)
132         pp->pln_mobil = damage((int)pp->pln_mobil, dam);
133 }
134
135 /*
136  * nukedamage() actually just calculates damage
137  * rather than inflicting it.
138  */
139 int
140 nukedamage(struct nchrstr *ncp, int range, int airburst)
141 {
142     int dam;
143     int rad;
144
145     rad = ncp->n_blast;
146     if (airburst)
147         rad = (int)(rad * 1.5);
148     if (rad < range)
149         return 0;
150     if (airburst) {
151         /* larger area, less center damage */
152         dam = (int)((ncp->n_dam * 0.75) - (range * 20));
153     } else {
154         /* smaller area, more center damage */
155         dam = (int)(ncp->n_dam / (range + 1.0));
156     }
157     if (dam < 5)
158         dam = 0;
159     return dam;
160 }
161
162 int
163 damage(int amt, int pct)
164 {
165     int tmp;
166     int lost;
167
168     if (amt <= 0)
169         return 0;
170     tmp = amt * pct;
171     lost = tmp / 100;
172     if (random() % 100 < tmp % 100)
173         lost++;
174     return amt - lost;
175 }
176
177 /* asymptotic damage to commodities, efficiency, and sectors */
178 int
179 effdamage(int amt, int dam)
180 {
181     return damage(amt, PERCENT_DAMAGE(dam));
182 }
183
184 int
185 commdamage(int amt, int dam, i_type vtype)
186 {
187     int lost;
188
189     if (vtype == I_BAR && opt_SUPER_BARS)
190         return amt;
191
192     lost = amt - effdamage(amt, dam);
193
194     if (vtype == I_MILIT || vtype == I_CIVIL || vtype == I_UW)
195         lost = ldround(people_damage * lost, 1);
196     return amt - lost;
197 }