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