]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mslsub.c
Fix and enable collateral damage for missing missiles
[empserver] / src / lib / subs / mslsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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 sctstr sect;
63     int sublaunch = 0;
64     struct plchrstr *pcp = plchr + pp->pln_type;
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_SHIP || type == EF_PLANE) ? "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     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 (chance(0.33)) {
98             dam = pln_damage(pp, 'p', 1) / 2;
99             if (pp->pln_ship >= 0) {
100                 shipdamage(&ship, dam);
101                 putship(ship.shp_uid, &ship);
102             } else {
103                 pr("Explosion damages %s %d%%",
104                    xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
105                 getsect(pp->pln_x, pp->pln_y, &sect);
106                 sectdamage(&sect, dam);
107                 putsect(&sect);
108             }
109         }
110         return -1;
111     }
112
113     CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
114     pp->pln_flags |= PLN_LAUNCHED;
115     putplane(pp->pln_uid, pp);
116     mpr(pp->pln_own, "\tSHWOOOOOSH!  Missile launched!\n");
117
118     if (pcp->pl_flags & P_T)
119         mpr(victim, "Incoming %s missile sighted at %s...\n",
120             sublaunch ? "sub-launched" : cname(pp->pln_own),
121             xyas(x, y, victim));
122
123     if ((pcp->pl_flags & P_T && !(pcp->pl_flags & P_MAR))) {
124         if (msl_abm_intercept(pp, x, y, sublaunch))
125             return -1;
126     }
127     if (pcp->pl_flags & P_MAR) {
128         if (shp_missile_defense(x, y, pp->pln_own, pln_def(pp))) {
129             return -1;
130         }
131     }
132
133     if (sublaunchp)
134         *sublaunchp = sublaunch;
135     return 0;
136 }
137
138 int
139 msl_hit(struct plnstr *pp, int hardtarget, int type,
140         int news_item, int snews_item, int sublaunch, natid victim)
141 {
142     struct plchrstr *pcp = plchr + pp->pln_type;
143     int hitchance, hit;
144
145     if (nuk_on_plane(pp) >= 0) {
146         mpr(pp->pln_own, "\tArming nuclear warheads...\n");
147         hit = 1;
148     } else {
149         hitchance = pln_hitchance(pp, hardtarget, type);
150         hit = (roll(100) <= hitchance);
151         mpr(pp->pln_own, "\t%d%% hitchance...%s\n", hitchance,
152             hit ? "HIT!" : "miss");
153     }
154
155     if (pcp->pl_flags & P_T)
156         mpr(victim, "...Incoming %s missile %s\n",
157             sublaunch ? "" : cname(pp->pln_own),
158             hit ? "HIT!\n" : "missed\n");
159     if (hit && news_item) {
160         if (sublaunch)
161             nreport(victim, snews_item, 0, 1);
162         else
163             nreport(pp->pln_own, news_item, victim, 1);
164     }
165     return hit;
166 }
167
168 void
169 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
170         int wantflags, int nowantflags, int mission)
171 {
172     struct plchrstr *pcp;
173     struct plnstr plane;
174     struct plist *irv;
175     struct nstr_item ni;
176
177     emp_initque(list);
178     snxtitem_all(&ni, EF_PLANE);
179     while (nxtitem(&ni, &plane)) {
180         if (!plane.pln_own)
181             continue;
182
183         pcp = &plchr[(int)plane.pln_type];
184         if (!(pcp->pl_flags & P_M))
185             continue;
186         if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
187             continue;
188         if (nowantflags && pcp->pl_flags & nowantflags)
189             continue;
190         if (mission && plane.pln_mission != mission)
191             continue;
192         if (mission &&
193             plane.pln_radius < mapdist(x, y, plane.pln_opx, plane.pln_opy))
194             continue;
195         if (getrel(getnatp(plane.pln_own), victim) >= NEUTRAL)
196             continue;
197         /* missiles go one way, so we can use all the range */
198         if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
199             continue;
200         if (plane.pln_mobil <= 0)
201             continue;
202         if (plane.pln_effic < 100)
203             continue;
204         if (!pln_airbase_ok(&plane, 1, 0))
205             continue;
206         /* got a valid interceptor */
207         irv = malloc(sizeof(*irv));
208         irv->load = 0;
209         irv->pcp = &plchr[(int)plane.pln_type];
210         irv->plane = plane;
211         emp_insque(&irv->queue, list);
212     }
213 }
214
215 static int
216 msl_intercept(struct plnstr *msl, struct sctstr *sp, int sublaunch,
217               struct emp_qelem *irvlist, char *att_name, char *def_name,
218               int news_item)
219 {
220     struct plnstr *pp;
221     struct plchrstr *pcp;
222     struct emp_qelem *intlist;
223     struct emp_qelem intfoo;
224     struct emp_qelem *qp;
225     struct emp_qelem *next;
226     struct plist *ip;
227     int icount = 0;
228     short destroyed;
229     char *who = sublaunch ? "" : cname(msl->pln_own);
230
231     intlist = &intfoo;
232     emp_initque(intlist);
233     /* First choose interceptors belonging to the target sector */
234     /* only allow two defense missiles per missile attack */
235     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
236         next = qp->q_forw;
237         ip = (struct plist *)qp;
238         pp = &ip->plane;
239         if (pp->pln_own != sp->sct_own)
240             continue;
241         pcp = ip->pcp;
242         if (mission_pln_equip(ip, NULL, 'i') < 0) {
243             emp_remque(qp);
244             free(qp);
245             continue;
246         }
247         /* got one interceptor, delete from irv_list and
248          * add to  int_list.
249          */
250         emp_remque(qp);
251         emp_insque(qp, intlist);
252         putplane(pp->pln_uid, pp);
253         icount++;
254     }
255     /* only allow two defense missiles per missile attack */
256     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
257         next = qp->q_forw;
258         ip = (struct plist *)qp;
259         pp = &ip->plane;
260         pcp = ip->pcp;
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     /* Now, clean out the queue */
275     while (!QEMPTY(irvlist)) {
276         qp = irvlist->q_forw;
277         emp_remque(qp);
278         free(qp);
279     }
280     if (icount == 0) {
281         mpr(sp->sct_own, "No %ss launched to intercept.\n", def_name);
282         return 0;
283     }
284
285     /* attempt to destroy incoming missile */
286
287     destroyed = 0;
288     while (!destroyed && !QEMPTY(intlist)) {
289         qp = intlist->q_forw;
290         ip = (struct plist *)qp;
291         pp = &ip->plane;
292         pcp = ip->pcp;
293
294         mpr(msl->pln_own, "%s %s launched in defense!\n",
295             cname(pp->pln_own), def_name);
296         if (sp->sct_own == pp->pln_own) {
297             mpr(sp->sct_own, "%s launched to intercept %s %s!\n",
298                 def_name, who, att_name);
299         } else {
300             mpr(sp->sct_own,
301                 "%s launched an %s to intercept the %s %s!\n",
302                 cname(pp->pln_own), def_name, who, att_name);
303             mpr(pp->pln_own,
304                 "%s launched to intercept %s %s arcing towards %s territory!\n",
305                 def_name, who, att_name, cname(sp->sct_own));
306         }
307
308         if (msl_launch(pp, EF_PLANE, att_name, sp->sct_x, sp->sct_y,
309                        msl->pln_own, NULL) >= 0
310             && msl_hit(pp, pln_def(msl), EF_PLANE, 0, 0, 0, msl->pln_own)) {
311             mpr(msl->pln_own, "%s destroyed by %s %s!\n",
312                 att_name, cname(pp->pln_own), def_name);
313             mpr(sp->sct_own, "%s %s intercepted!\n", who, att_name);
314             if (sp->sct_own != pp->pln_own)
315                 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
316             if (sublaunch)
317                 nreport(pp->pln_own, news_item, 0, 1);
318             else
319                 nreport(pp->pln_own, news_item, msl->pln_own, 1);
320             destroyed = 1;
321         }
322         /* zap the missile */
323         pp->pln_effic = 0;
324         putplane(pp->pln_uid, pp);
325         emp_remque(qp);
326         free(qp);
327     }
328     /* Clean out what is left in the list */
329     while (!QEMPTY(intlist)) {
330         qp = intlist->q_forw;
331         emp_remque(qp);
332         free(qp);
333     }
334     if (destroyed)
335         return 1;
336     if (icount) {
337         mpr(msl->pln_own, "%s made it through %s defenses!\n",
338             att_name, def_name);
339         mpr(sp->sct_own, "%s made it through %s defenses!\n",
340             att_name, def_name);
341     }
342     return 0;
343 }
344
345 int
346 msl_abm_intercept(struct plnstr *msl, coord x, coord y, int sublaunch)
347 {
348     struct sctstr sect;
349     struct emp_qelem irvlist;
350
351     getsect(x, y, &sect);
352     msl_sel(&irvlist, x, y, msl->pln_own, P_N, P_O, 0);
353     return msl_intercept(msl, &sect, sublaunch,
354                          &irvlist, "warhead", "abm",
355                          sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP);
356 }
357
358 int
359 msl_asat_intercept(struct plnstr *msl, coord x, coord y)
360 {
361     struct sctstr sect;
362     struct emp_qelem irvlist;
363
364     getsect(x, y, &sect);
365     mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
366         cname(msl->pln_own), xyas(x, y, sect.sct_own));
367     msl_sel(&irvlist, x, y, msl->pln_own, P_O, 0, 0);
368     return msl_intercept(msl, &sect, 0,
369                          &irvlist, "satellite", "a-sat missile",
370                          N_SAT_KILL);
371 }