]> git.pond.sub.org Git - empserver/blob - src/lib/common/sectdamage.c
ae387cb8599b90d13337fc98bd72578a854c0047
[empserver] / src / lib / common / sectdamage.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  *  sectdamage.c: Damage a sector
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Steve McClure, 1996
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 "xy.h"
43 #include "nsc.h"
44 #include "file.h"
45 #include "nat.h"
46 #include "optlist.h"
47 #include "damage.h"
48 #include "common.h"
49 #include "gen.h"
50 #include "subs.h"
51 #include "combat.h"
52
53 int
54 sect_damage(struct sctstr *sp, int dam, struct emp_qelem *list)
55 {
56     int eff;
57
58     if (dam <= 0)
59         return 0;
60     if (dam > 100)
61         dam = 100;
62
63     sp->sct_effic = damage(sp->sct_effic, dam);
64     sp->sct_avail = damage(sp->sct_avail, dam);
65     sp->sct_road = damage(sp->sct_road, dam);
66     sp->sct_rail = damage(sp->sct_rail, dam);
67     sp->sct_defense = damage(sp->sct_defense, dam);
68     if (!opt_DEFENSE_INFRA)
69         sp->sct_defense = sp->sct_effic;
70
71     eff = dam;
72
73     if (sp->sct_mobil > 0)
74         sp->sct_mobil = damage(sp->sct_mobil, dam);
75     item_damage(dam, sp->sct_item);
76     if (opt_EASY_BRIDGES == 0) {
77         if (sp->sct_effic < SCT_MINEFF && sp->sct_type == SCT_BHEAD)
78             bridgefall(sp, list);
79     } else {
80         if (sp->sct_effic < SCT_MINEFF && sp->sct_type == SCT_BSPAN)
81             knockdown(sp, list);
82     }
83     putsect(sp);
84     return eff;
85 }
86
87 int
88 sectdamage(struct sctstr *sp, int dam, struct emp_qelem *list)
89 {
90     struct nstr_item ni;
91     struct lndstr land;
92     struct plnstr plane;
93     double real_dam;
94     int eff;
95
96     /* Some sectors are harder/easier to kill..   */
97     /* Average sector has a dstr of 1, so adjust  */
98     /* the damage accordingly. Makes forts a pain */
99
100 /*      real_dam = (double)dam * (1.0/((((double)(dchr[sp->sct_type].d_dstr - 1))*(sp->sct_effic/100.0)) + 1.0));*/
101     real_dam = (double)dam *(1.0 / sector_strength(sp));
102     dam = ldround(real_dam, 1);
103
104     eff = sect_damage(sp, PERCENT_DAMAGE(dam), list);
105
106     /* Damage all the land units in the sector */
107     /* Units don't take full damage */
108     dam = ldround(DPERCENT_DAMAGE(dam * unit_damage), 1);
109     if (dam <= 0)
110         return eff;
111
112     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
113     while (nxtitem(&ni, &land)) {
114         if (!land.lnd_own)
115             continue;
116         landdamage(&land, dam);
117         putland(land.lnd_uid, &land);
118     }
119
120     dam = dam / 7;
121     if (dam <= 0)
122         return eff;
123     snxtitem_xy(&ni, EF_PLANE, sp->sct_x, sp->sct_y);
124     while (nxtitem(&ni, &plane)) {
125         if (!plane.pln_own)
126             continue;
127         if (plane.pln_flags & PLN_LAUNCHED)
128             continue;
129         if (plane.pln_ship >= 0)
130             continue;
131         /* Is this plane flying in this list? */
132         if (ac_isflying(&plane, list))
133             continue;
134         planedamage(&plane, dam);
135         putplane(plane.pln_uid, &plane);
136     }
137     return eff;
138 }