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