]> git.pond.sub.org Git - empserver/blob - src/lib/common/damage.c
Update copyright notice.
[empserver] / src / lib / common / damage.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  damage.c: Damage stuff.
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Steve McClure, 1997
33  */
34
35 #include <config.h>
36
37 #include <fcntl.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 "sect.h"
47 #include "ship.h"
48
49 void
50 item_damage(int pct, short *item)
51 {
52     int lose;
53     i_type i;
54
55     for (i = I_NONE + 1; i <= I_MAX; ++i) {
56         if (opt_SUPER_BARS && i == I_BAR)
57             continue;
58         lose = roundavg((double)item[i] * pct * 0.01);
59         if (i == I_CIVIL || i == I_MILIT || i == I_UW)
60             lose = ldround(people_damage * lose, 1);
61         item[i] = item[i] >= lose ? item[i] - lose : 0;
62     }
63 }
64
65 void
66 ship_damage(struct shpstr *sp, int dam)
67 {
68     if (dam <= 0)
69         return;
70     if (dam > 100)
71         dam = 100;
72
73     mpr(sp->shp_own, "\t%s takes %d\n", prship(sp), dam);
74
75     sp->shp_effic = damage((int)sp->shp_effic, dam);
76     if (sp->shp_mobil > 0)
77         sp->shp_mobil = damage((int)sp->shp_mobil, dam);
78     if (opt_FUEL && sp->shp_fuel)
79         sp->shp_fuel = damage((int)sp->shp_fuel, dam);
80     item_damage(dam, sp->shp_item);
81 }
82
83 void
84 shipdamage(struct shpstr *sp, int dam)
85 {
86     ship_damage(sp, (int)(dam / (1.0 + sp->shp_armor / 100.0)));
87 }
88
89 void
90 land_damage(struct lndstr *lp, int dam)
91 {
92     if (dam <= 0)
93         return;
94     if (dam > 100)
95         dam = 100;
96
97     mpr(lp->lnd_own, "\t%s takes %d\n", prland(lp), dam);
98     if (lchr[(int)lp->lnd_type].l_flags & L_SPY) {
99         /* Spies die! */
100         lp->lnd_effic = 0;
101     } else {
102         lp->lnd_effic = damage((int)lp->lnd_effic, dam);
103         if (lp->lnd_mobil > 0)
104             lp->lnd_mobil = damage((int)lp->lnd_mobil, dam);
105         if (opt_FUEL && lp->lnd_fuel)
106             lp->lnd_fuel = damage((int)lp->lnd_fuel, dam);
107         item_damage(dam, lp->lnd_item);
108     }
109 }
110
111 void
112 landdamage(struct lndstr *lp, int dam)
113 {
114     double damage_factor, m;
115
116     m = land_mob_max;
117
118     /* fortification reduces damage */
119     damage_factor = m / (m + lp->lnd_harden);
120
121     /* vulnerable units take more damage */
122     damage_factor *= lp->lnd_vul / 100.0;
123
124     land_damage(lp, ldround(damage_factor * dam, 1));
125 }
126
127 void
128 planedamage(struct plnstr *pp, int dam)
129 {
130     if (dam <= 0)
131         return;
132     if (dam > 100)
133         dam = 100;
134
135     mpr(pp->pln_own, "\t%s takes %d\n", prplane(pp), dam);
136     pp->pln_effic = damage((int)pp->pln_effic, dam);
137     if (pp->pln_mobil > 0)
138         pp->pln_mobil = damage((int)pp->pln_mobil, dam);
139 }
140
141 /*
142  * nukedamage() actually just calculates damage
143  * rather than inflicting it.
144  */
145 int
146 nukedamage(struct nchrstr *ncp, int range, int airburst)
147 {
148     int dam;
149     int rad;
150
151     rad = ncp->n_blast;
152     if (airburst)
153         rad = (int)(rad * 1.5);
154     if (rad < range)
155         return 0;
156     if (airburst) {
157         /* larger area, less center damage */
158         dam = (int)((ncp->n_dam * 0.75) - (range * 20));
159     } else {
160         /* smaller area, more center damage */
161         dam = (int)(ncp->n_dam / (range + 1.0));
162     }
163     if (dam < 5)
164         dam = 0;
165     return dam;
166 }
167
168 int
169 damage(int amt, int pct)
170 {
171     int tmp;
172     int lost;
173
174     if (amt <= 0)
175         return 0;
176     tmp = amt * pct;
177     lost = tmp / 100;
178     if (random() % 100 < tmp % 100)
179         lost++;
180     return amt - lost;
181 }
182
183 /* asymptotic damage to commodities, efficiency, and sectors */
184 int
185 effdamage(int amt, int dam)
186 {
187     return damage(amt, PERCENT_DAMAGE(dam));
188 }
189
190 int
191 commdamage(int amt, int dam, i_type vtype)
192 {
193     int lost;
194
195     if (vtype == I_BAR && opt_SUPER_BARS)
196         return amt;
197
198     lost = amt - effdamage(amt, dam);
199
200     if (vtype == I_MILIT || vtype == I_CIVIL || vtype == I_UW)
201         lost = ldround(people_damage * lost, 1);
202     return amt - lost;
203 }