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