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