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