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