]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mslsub.c
Disable damage to base when missile explodes on launch
[empserver] / src / lib / subs / mslsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  mslsub.c: Missile subroutine stuff
28  *
29  *  Known contributors to this file:
30  *     Ken Stevens, 1995
31  *     Steve McClure, 1996-2000
32  *     Markus Armbruster, 2004-2010
33  */
34
35 #include <config.h>
36
37 #include <stdlib.h>
38 #include "file.h"
39 #include "misc.h"
40 #include "nat.h"
41 #include "news.h"
42 #include "nsc.h"
43 #include "nuke.h"
44 #include "optlist.h"
45 #include "plane.h"
46 #include "prototypes.h"
47 #include "queue.h"
48 #include "sect.h"
49 #include "ship.h"
50 #include "xy.h"
51
52 int
53 msl_launch(struct plnstr *pp, int type, char *what, coord x, coord y,
54            natid victim, int *sublaunchp)
55 {
56     struct shpstr ship;
57     struct nukstr nuke;
58     int sublaunch = 0;
59     char *base, *in_or_at, *from;
60
61     mpr(pp->pln_own, "Preparing to launch %s at %s %s %s%s\n",
62         prplane(pp),
63         cname(victim),
64         what,
65         type != EF_SECTOR ? "in " : "",
66         xyas(x, y, pp->pln_own));
67     if (pp->pln_ship >= 0) {
68         getship(pp->pln_ship, &ship);
69         base = prship(&ship);
70         in_or_at = " in ";
71         if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
72             sublaunch = 1;
73             from = "in hatch";
74         } else
75             from = "on deck";
76     } else {
77         if (pp->pln_harden > 0) {
78             base = "missile silo";
79             in_or_at = " at ";
80             from = "in silo";
81         } else {
82             base = in_or_at = "";
83             from = "on launch pad";
84         }
85     }
86     mpr(pp->pln_own, "\tLaunching from %s%s%s\n",
87         base, in_or_at, xyas(pp->pln_x, pp->pln_y, pp->pln_own));
88
89     CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
90     pp->pln_flags |= PLN_LAUNCHED;
91     putplane(pp->pln_uid, pp);
92
93     if (chance((0.05 + (100 - pp->pln_effic) / 100.0)
94                * (1 - techfact(pp->pln_tech, 1.0)))) {
95         mpr(pp->pln_own, "KABOOOOM!  Missile explodes %s!\n", from);
96         if (getnuke(nuk_on_plane(pp), &nuke)) {
97             mpr(pp->pln_own, "%s lost!\n", prnuke(&nuke));
98             nuke.nuk_effic = 0;
99             putnuke(nuke.nuk_uid, &nuke);
100         }
101 #if 0
102         /*
103          * Disabled for now, because it breaks callers that call
104          * msl_launch() for each member of a list of planes, created
105          * by msl_sel() or perform_mission().  Damage to the base can
106          * damage other planes.  Any copies of them in the list become
107          * stale.  When msl_launch() modifies and writes back such a
108          * stale copy, the damage gets wiped out, triggering a seqno
109          * oops.
110          */
111         if (chance(0.33)) {
112             struct sctstr sect;
113             int dam;
114
115             dam = pln_damage(pp, 'p', 0) / 2;
116             if (pp->pln_ship >= 0) {
117                 shipdamage(&ship, dam);
118                 putship(ship.shp_uid, &ship);
119             } else {
120                 mpr(pp->pln_own, "Explosion damages %s %d%%\n",
121                     xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
122                 getsect(pp->pln_x, pp->pln_y, &sect);
123                 sectdamage(&sect, dam);
124                 putsect(&sect);
125             }
126         }
127 #endif
128         return -1;
129     }
130
131     mpr(pp->pln_own, "\tSHWOOOOOSH!  Missile launched!\n");
132
133     if (type != EF_PLANE)
134         mpr(victim, "Incoming %s missile sighted at %s...\n",
135             sublaunch ? "sub-launched" : cname(pp->pln_own),
136             xyas(x, y, victim));
137
138     if (type == EF_SECTOR || type == EF_LAND) {
139         if (msl_abm_intercept(pp, x, y, sublaunch))
140             return -1;
141     }
142     if (type == EF_SHIP) {
143         if (shp_missile_defense(x, y, pp->pln_own, pln_def(pp))) {
144             return -1;
145         }
146     }
147
148     if (sublaunchp)
149         *sublaunchp = sublaunch;
150     return 0;
151 }
152
153 int
154 msl_hit(struct plnstr *pp, int hardtarget, int type,
155         int news_item, int snews_item, int sublaunch, natid victim)
156 {
157     int hitchance, hit;
158
159     if (nuk_on_plane(pp) >= 0) {
160         mpr(pp->pln_own, "\tArming nuclear warheads...\n");
161         hit = 1;
162     } else {
163         hitchance = pln_hitchance(pp, hardtarget, type);
164         hit = (roll(100) <= hitchance);
165         mpr(pp->pln_own, "\t%d%% hitchance...%s\n", hitchance,
166             hit ? "HIT!" : "miss");
167     }
168
169     if (type != EF_PLANE)
170         mpr(victim, "...Incoming %s missile %s\n",
171             sublaunch ? "" : cname(pp->pln_own),
172             hit ? "HIT!\n" : "missed\n");
173     if (hit && news_item) {
174         if (sublaunch)
175             nreport(victim, snews_item, 0, 1);
176         else
177             nreport(pp->pln_own, news_item, victim, 1);
178     }
179     return hit;
180 }
181
182 void
183 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
184         int wantflags, int nowantflags, int mission)
185 {
186     struct plchrstr *pcp;
187     struct plnstr plane;
188     struct plist *irv;
189     struct nstr_item ni;
190
191     emp_initque(list);
192     snxtitem_all(&ni, EF_PLANE);
193     while (nxtitem(&ni, &plane)) {
194         if (!plane.pln_own)
195             continue;
196
197         pcp = &plchr[(int)plane.pln_type];
198         if (!(pcp->pl_flags & P_M))
199             continue;
200         if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
201             continue;
202         if (nowantflags && pcp->pl_flags & nowantflags)
203             continue;
204         if (mission && plane.pln_mission != mission)
205             continue;
206         if (mission &&
207             plane.pln_radius < mapdist(x, y, plane.pln_opx, plane.pln_opy))
208             continue;
209         if (relations_with(plane.pln_own, victim) >= NEUTRAL)
210             continue;
211         /* missiles go one way, so we can use all the range */
212         if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
213             continue;
214         if (plane.pln_mobil <= 0)
215             continue;
216         if (plane.pln_effic < 100)
217             continue;
218         if (opt_MARKET) {
219             if (ontradingblock(EF_PLANE, &plane))
220                 continue;
221         }
222         if (!pln_airbase_ok(&plane, 1, 0))
223             continue;
224         /* got a valid interceptor */
225         irv = malloc(sizeof(*irv));
226         irv->load = 0;
227         irv->pcp = &plchr[(int)plane.pln_type];
228         irv->plane = plane;
229         emp_insque(&irv->queue, list);
230     }
231 }
232
233 static int
234 msl_intercept(struct plnstr *msl, struct sctstr *sp, int sublaunch,
235               struct emp_qelem *irvlist, char *att_name, char *def_name,
236               int news_item)
237 {
238     struct plnstr *pp;
239     struct emp_qelem *intlist;
240     struct emp_qelem intfoo;
241     struct emp_qelem *qp;
242     struct emp_qelem *next;
243     struct plist *ip;
244     int icount = 0;
245     short destroyed;
246     char *who = sublaunch ? "" : cname(msl->pln_own);
247
248     intlist = &intfoo;
249     emp_initque(intlist);
250     /* First choose interceptors belonging to the target sector */
251     /* only allow two defense missiles per missile attack */
252     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
253         next = qp->q_forw;
254         ip = (struct plist *)qp;
255         pp = &ip->plane;
256         if (pp->pln_own != sp->sct_own)
257             continue;
258         if (mission_pln_equip(ip, NULL, 'i') < 0) {
259             emp_remque(qp);
260             free(qp);
261             continue;
262         }
263         /* got one interceptor, delete from irv_list and
264          * add to  int_list.
265          */
266         emp_remque(qp);
267         emp_insque(qp, intlist);
268         putplane(pp->pln_uid, pp);
269         icount++;
270     }
271     /* only allow two defense missiles per missile attack */
272     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
273         next = qp->q_forw;
274         ip = (struct plist *)qp;
275         pp = &ip->plane;
276         if (mission_pln_equip(ip, NULL, 'i') < 0) {
277             emp_remque(qp);
278             free(qp);
279             continue;
280         }
281         /* got one interceptor, delete from irv_list and
282          * add to  int_list.
283          */
284         emp_remque(qp);
285         emp_insque(qp, intlist);
286         putplane(pp->pln_uid, pp);
287         icount++;
288     }
289     /* Now, clean out the queue */
290     while (!QEMPTY(irvlist)) {
291         qp = irvlist->q_forw;
292         emp_remque(qp);
293         free(qp);
294     }
295     if (icount == 0) {
296         mpr(sp->sct_own, "No %ss launched to intercept.\n", def_name);
297         return 0;
298     }
299
300     /* attempt to destroy incoming missile */
301
302     destroyed = 0;
303     while (!destroyed && !QEMPTY(intlist)) {
304         qp = intlist->q_forw;
305         ip = (struct plist *)qp;
306         pp = &ip->plane;
307
308         mpr(msl->pln_own, "%s %s launched in defense!\n",
309             cname(pp->pln_own), def_name);
310         if (sp->sct_own == pp->pln_own) {
311             mpr(sp->sct_own, "%s launched to intercept %s %s!\n",
312                 def_name, who, att_name);
313         } else {
314             mpr(sp->sct_own,
315                 "%s launched an %s to intercept the %s %s!\n",
316                 cname(pp->pln_own), def_name, who, att_name);
317             mpr(pp->pln_own,
318                 "%s launched to intercept %s %s arcing towards %s territory!\n",
319                 def_name, who, att_name, cname(sp->sct_own));
320         }
321
322         if (msl_launch(pp, EF_PLANE, att_name, sp->sct_x, sp->sct_y,
323                        msl->pln_own, NULL) >= 0
324             && msl_hit(pp, pln_def(msl), EF_PLANE, 0, 0, 0, msl->pln_own)) {
325             mpr(msl->pln_own, "%s destroyed by %s %s!\n",
326                 att_name, cname(pp->pln_own), def_name);
327             mpr(sp->sct_own, "%s %s intercepted!\n", who, att_name);
328             if (sp->sct_own != pp->pln_own)
329                 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
330             if (sublaunch)
331                 nreport(pp->pln_own, news_item, 0, 1);
332             else
333                 nreport(pp->pln_own, news_item, msl->pln_own, 1);
334             destroyed = 1;
335         }
336         /* zap the missile */
337         pp->pln_effic = 0;
338         putplane(pp->pln_uid, pp);
339         emp_remque(qp);
340         free(qp);
341     }
342     /* Clean out what is left in the list */
343     while (!QEMPTY(intlist)) {
344         qp = intlist->q_forw;
345         emp_remque(qp);
346         free(qp);
347     }
348     if (destroyed)
349         return 1;
350     if (icount) {
351         mpr(msl->pln_own, "%s made it through %s defenses!\n",
352             att_name, def_name);
353         mpr(sp->sct_own, "%s made it through %s defenses!\n",
354             att_name, def_name);
355     }
356     return 0;
357 }
358
359 int
360 msl_abm_intercept(struct plnstr *msl, coord x, coord y, int sublaunch)
361 {
362     struct sctstr sect;
363     struct emp_qelem irvlist;
364
365     getsect(x, y, &sect);
366     msl_sel(&irvlist, x, y, msl->pln_own, P_N, P_O, 0);
367     return msl_intercept(msl, &sect, sublaunch,
368                          &irvlist, "warhead", "abm",
369                          sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP);
370 }
371
372 int
373 msl_asat_intercept(struct plnstr *msl, coord x, coord y)
374 {
375     struct sctstr sect;
376     struct emp_qelem irvlist;
377
378     getsect(x, y, &sect);
379     mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
380         cname(msl->pln_own), xyas(x, y, sect.sct_own));
381     msl_sel(&irvlist, x, y, msl->pln_own, P_O, 0, 0);
382     return msl_intercept(msl, &sect, 0,
383                          &irvlist, "satellite", "a-sat missile",
384                          N_SAT_KILL);
385 }