]> git.pond.sub.org Git - empserver/blob - src/lib/common/damage.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / common / damage.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 "misc.h"
36 #include "var.h"
37 #include "sect.h"
38 #include "ship.h"
39 #include "land.h"
40 #include "plane.h"
41 #include "nuke.h"
42 #include "xy.h"
43 #include "nsc.h"
44 #include <fcntl.h>
45 #include "file.h"
46 #include "optlist.h"
47 #include "damage.h"
48 #include "common.h"
49 #include "gen.h"
50 #include "subs.h"
51
52 void
53 ship_damage(struct shpstr *sp, int dam)
54 {
55
56     if (dam <= 0)
57         return;
58     if (dam > 100)
59         dam = 100;
60
61     mpr(sp->shp_own, "\t%s takes %d\n", prship(sp), dam);
62
63     sp->shp_effic = damage((int)sp->shp_effic, dam);
64     if (sp->shp_mobil > 0)
65         sp->shp_mobil = damage((int)sp->shp_mobil, dam);
66     if (opt_FUEL && sp->shp_fuel)
67         sp->shp_fuel = damage((int)sp->shp_fuel, dam);
68     sp->shp_nv = vl_damage(dam, sp->shp_vtype, sp->shp_vamt,
69                            (int)sp->shp_nv);
70 }
71
72 void
73 shipdamage(struct shpstr *sp, int dam)
74 {
75     ship_damage(sp, (int)(dam / (1.0 + sp->shp_armor / 100.0)));
76 }
77
78 void
79 land_damage(struct lndstr *lp, int dam)
80 {
81     if (dam <= 0)
82         return;
83     if (dam > 100)
84         dam = 100;
85
86     mpr(lp->lnd_own, "\t%s takes %d\n", prland(lp), dam);
87     if (lchr[(int)lp->lnd_type].l_flags & L_SPY) {
88         /* Spies die! */
89         lp->lnd_effic = 0;
90     } else {
91         lp->lnd_effic = damage((int)lp->lnd_effic, dam);
92         if (lp->lnd_mobil > 0)
93             lp->lnd_mobil = damage((int)lp->lnd_mobil, dam);
94         if (opt_FUEL && lp->lnd_fuel)
95             lp->lnd_fuel = damage((int)lp->lnd_fuel, dam);
96         lp->lnd_nv = vl_damage(dam, lp->lnd_vtype, lp->lnd_vamt,
97                                (int)lp->lnd_nv);
98     }
99 }
100
101 void
102 landdamage(struct lndstr *lp, int dam)
103 {
104     double damage_factor, m;
105     extern int land_mob_max;
106
107     m = (double)land_mob_max;
108
109     /* fortification reduces damage */
110     damage_factor = m / (m + ((double)lp->lnd_harden));
111     if (damage_factor == 0.0)
112         damage_factor = 1.0;
113
114     /* vulnerable units take more damage */
115     damage_factor *= lp->lnd_vul / 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(register int amt, int pct)
163 {
164     register int tmp;
165     register 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(register int amt, int dam)
179 {
180     return damage(amt, PERCENT_DAMAGE(dam));
181 }
182
183 int
184 commdamage(register int amt, int dam, int vtype)
185 {
186     extern double people_damage;
187     int lost;
188
189     if (vtype == V_BAR && opt_SUPER_BARS)
190         return amt;
191
192     lost = amt - effdamage(amt, dam);
193
194     if (vtype == V_MILIT || vtype == V_CIVIL || vtype == V_UW)
195         lost = ldround(people_damage * lost, 1);
196     return amt - lost;
197 }