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