]> git.pond.sub.org Git - empserver/blob - src/lib/subs/fortdef.c
Update copyright notice
[empserver] / src / lib / subs / fortdef.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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  *  fortdef.c: Fort defends an area.
29  *
30  *  Known contributors to this file:
31  *
32  */
33
34 /*
35  * The base routines can also be used for general purposes.
36  * Noisy tells whther to send teles, print things, etc.
37  * Defending tells whether they are being defensive, or offensive.
38  */
39
40 #include <config.h>
41
42 #include "file.h"
43 #include "nat.h"
44 #include "news.h"
45 #include "optlist.h"
46 #include "prototypes.h"
47 #include "sect.h"
48 #include "ship.h"
49
50 #define NOISY   1
51
52 static int sb(natid, natid, struct sctstr *, coord, coord, int, int);
53
54 /*
55  * See if any nearby ships will open up on the attacker
56  * Return damage done to attacker, if any.
57  * Subtracts shells used for firing.  Responding ships
58  * require military, shells, and guns.
59  */
60 int
61 shipdef(natid att, natid own, coord x, coord y)
62 {
63     return sd(att, own, x, y, NOISY, 1, 1);
64 }
65
66 int
67 sd(natid att, natid own, coord x, coord y, int noisy, int defending,
68    int usesubs)
69 {
70     int range;
71     double eff;
72     struct shpstr ship;
73     struct nstr_item ni;
74     int dam, rel, rel2;
75
76     if (own == 0)
77         return 0;
78     if (att == own)
79         return 0;
80     eff = 1.0;
81     snxtitem_dist(&ni, EF_SHIP, x, y, 8);
82     while (nxtitem(&ni, &ship) && eff > 0.30) {
83         if (ship.shp_own == att)
84             continue;
85         if (ship.shp_own == 0)
86             continue;
87
88         rel = getrel(getnatp(ship.shp_own), own);
89         rel2 = getrel(getnatp(ship.shp_own), att);
90         if ((ship.shp_own != own) && ((rel != ALLIED) || (rel2 != AT_WAR)))
91             continue;
92         if ((mchr[(int)ship.shp_type].m_flags & M_SUB) && !usesubs)
93             continue;
94         range = roundrange(shp_fire_range(&ship));
95         if (range < ni.curdist)
96             continue;
97         dam = shp_fire(&ship);
98         putship(ship.shp_uid, &ship);
99         if (dam < 0)
100             continue;
101         if (defending)
102             nreport(ship.shp_own, N_FIRE_BACK, att, 1);
103         else
104             nreport(ship.shp_own, N_FIRE_S_ATTACK, att, 1);
105         eff *= (1.0 - (0.01 * dam));
106         if (noisy) {
107             pr_beep();
108             pr("Incoming shells do %d damage!\n", dam);
109         }
110         if (noisy || (ship.shp_own != own)) {
111             if (ship.shp_own == own)
112                 wu(0, own, "%s fired on %s at %s doing %d damage.\n",
113                    prship(&ship), cname(att), xyas(x, y, own), dam);
114             else {
115                 if (defending)
116                     wu(0, ship.shp_own,
117                        "%s fired on %s at %s in defense of %s, doing %d damage.\n",
118                        prship(&ship), cname(att), xyas(x, y, ship.shp_own),
119                        cname(own), dam);
120                 else
121                     wu(0, ship.shp_own,
122                        "%s supported %s attacks against %s at %s, doing %d damage.\n",
123                        prship(&ship), cname(own), cname(att),
124                        xyas(x, y, ship.shp_own), dam);
125             }
126         }
127     }
128     return (int)(100 - eff * 100);
129 }
130
131 int
132 dd(natid att, natid def_own, coord ax, coord ay, int noisy, int defending)
133 {
134     int dam, rel, rel2;
135     struct sctstr firing;
136     struct nstr_sect ns;
137
138     if (opt_NO_FORT_FIRE)       /* Forts can't fire! */
139         return 0;
140     if (def_own == 0)
141         return 0;
142     if (att == def_own)
143         return 0;
144     dam = 0;
145     snxtsct_dist(&ns, ax, ay, 8);
146     while (nxtsct(&ns, &firing) && dam < 80) {
147         if (firing.sct_own == att)
148             continue;
149         if (firing.sct_own == 0)
150             continue;
151         rel = getrel(getnatp(firing.sct_own), def_own);
152         rel2 = getrel(getnatp(firing.sct_own), att);
153         if (firing.sct_own != def_own && (rel != ALLIED || rel2 != AT_WAR))
154             continue;
155         /* XXX defdef damage is additive, but ship or land unit damage isn't */
156         dam += sb(att, def_own, &firing, ax, ay, noisy, defending);
157     }
158     return dam;
159 }
160
161 /* Shoot back
162  *
163  * See if the sector being fired at will defend itself.
164  */
165 static int
166 sb(natid att, natid def, struct sctstr *sp, coord tx, coord ty, int noisy,
167    int defending)
168 {
169     int damage;
170     natid own;
171     int range, range2;
172
173     own = sp->sct_own;
174     if (own == 0)
175         return 0;
176     if (att == own)
177         return 0;
178     range = roundrange(fortrange(sp));
179     range2 = mapdist(sp->sct_x, sp->sct_y, tx, ty);
180     if (range < range2)
181         return 0;
182     damage = fort_fire(sp);
183     putsect(sp);
184     if (damage < 0)
185         return 0;
186     if (sp->sct_own != def)
187         wu(0, sp->sct_own,
188            "%s fired on %s in %s in defense of %s, doing %d damage!\n",
189            xyas(sp->sct_x, sp->sct_y, sp->sct_own), cname(att),
190            xyas(tx, ty, sp->sct_own), cname(def), damage);
191     if (defending)
192         nreport(sp->sct_own, N_FIRE_BACK, att, 1);
193     else
194         nreport(sp->sct_own, N_FIRE_F_ATTACK, att, 1);
195     if (noisy)
196         pr("Incoming shell! %d damage done.\007\n", damage);
197     return damage;
198 }