]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mslsub.c
(msl_hit): Tell victim coordinates of incoming missile. Closes
[empserver] / src / lib / subs / mslsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
36 #include "queue.h"
37 #include "player.h"
38 #include "var.h"
39 #include "sect.h"
40 #include "ship.h"
41 #include "optlist.h"
42 #include "nuke.h"
43 #include "plane.h"
44 #include "land.h"
45 #include "news.h"
46 #include "item.h"
47 #include "xy.h"
48 #include "nsc.h"
49 #include "file.h"
50 #include "nat.h"
51 #include "path.h"
52 #include "mission.h"
53 #include "prototypes.h"
54
55 int
56 msl_equip(struct plnstr *pp)
57 {
58     struct plist pl;
59
60     memset(&pl, 0, sizeof(struct plist));
61     pl.pcp = plchr + pp->pln_type;
62     pl.plane = *pp;
63     return mission_pln_equip(&pl, 0, 0, 'p');
64 }
65
66 int
67 msl_hit(struct plnstr *pp, int hardtarget, int type, int news_item,
68         int snews_item, s_char *what, coord x, coord y, int victim)
69 {
70     int hit;
71     struct shpstr ship;
72     struct sctstr sect;
73     int sublaunch = 0;
74     struct plchrstr *pcp = plchr + pp->pln_type;
75     int hitchance = pln_hitchance(pp, hardtarget, type);
76     s_char *from;
77     int dam, dummyi;
78
79     mpr(pp->pln_own, "Preparing to launch %s at %s %s %s%s\n",
80         prplane(pp),
81         cname(victim),
82         what,
83         (type == EF_SHIP || type == EF_PLANE) ? "in " : "",
84         xyas(x, y, pp->pln_own));
85     mpr(pp->pln_own, "\tLaunching from ");
86     if (pp->pln_ship >= 0) {
87         getship(pp->pln_ship, &ship);
88         mpr(pp->pln_own, "%s in ", prship(&ship));
89         if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
90             sublaunch = 1;
91             from = "in hatch";
92         } else
93             from = "on deck";
94         mpr(pp->pln_own, "%s\n",
95             xyas(ship.shp_x, ship.shp_y, pp->pln_own));
96     } else {
97         if (pp->pln_harden > 0) {
98             mpr(pp->pln_own, "missile silo at ");
99             from = "in silo";
100         } else
101             from = "on launch pad";
102         mpr(pp->pln_own, "%s\n", xyas(pp->pln_x, pp->pln_y, pp->pln_own));
103     }
104
105     if (chance((0.05 + (100 - pp->pln_effic) / 100.0)
106                * (1 - techfact(pp->pln_tech, 1.0)))) {
107         mpr(pp->pln_own, "KABOOOOM!  Missile explodes %s!\n", from);
108         if (chance(0.33)) {
109             if (pp->pln_nuketype != (s_char)-1 && opt_NUKEFAILDETONATE) {
110                 pp->pln_flags &= ~PLN_AIRBURST;
111                 detonate(pp, pp->pln_x, pp->pln_y);
112             } else {
113                 dam = pln_damage(pp, pp->pln_x, pp->pln_y,
114                                  'p', &dummyi, 1) / 2;
115                 if (dam) {
116                     if (pp->pln_ship >= 0) {
117                         shipdamage(&ship, dam);
118                         putship(ship.shp_uid, &ship);
119                     } else {
120                         pr("Explosion damages %s %d%%",
121                            xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
122                         getsect(pp->pln_x, pp->pln_y, &sect);
123                         sectdamage(&sect, dam, 0);
124                         putsect(&sect);
125                     }
126                 }
127             }
128         }
129         return 0;
130     }
131
132     mpr(pp->pln_own, "\tSHWOOOOOSH!  Missile launched!\n");
133
134     if (pp->pln_nuketype != (s_char)-1)
135         mpr(pp->pln_own, "\tArming nuclear warheads...\n");
136
137     if (pcp->pl_flags & P_T)
138         mpr(victim, "Incoming %s missile sighted at %s...\n",
139             sublaunch ? "sub-launched" : cname(pp->pln_own),
140             xyas(x, y, victim));
141
142     if (opt_PINPOINTMISSILE == 0 ||
143         (pcp->pl_flags & P_T && !(pcp->pl_flags & P_MAR))) {
144         if (msl_intercept(x, y, pp->pln_own, pcp->pl_def,
145                           sublaunch, P_N, P_O)) {
146             return 0;
147         }
148     }
149     if (pcp->pl_flags & P_MAR) {
150         if (shp_missile_defense(x, y, pp->pln_own, pcp->pl_def)) {
151             return 0;
152         }
153     }
154
155     if (pp->pln_nuketype != (s_char)-1)
156         hitchance = 100;
157
158     mpr(pp->pln_own, "\t%d%% hitchance...", hitchance);
159     hit = (roll(100) <= hitchance);
160
161     mpr(pp->pln_own, hit ? "HIT!\n" : "miss\n");
162     if (pcp->pl_flags & P_T)
163         mpr(victim, "...Incoming %s missile %s\n",
164             sublaunch ? (s_char *)"" : cname(pp->pln_own),
165             hit ? "HIT!\n" : "missed\n");
166     if (hit && news_item) {
167         if (sublaunch)
168             nreport(victim, snews_item, 0, 1);
169         else
170             nreport(pp->pln_own, news_item, victim, 1);
171     }
172     return hit;
173 }
174
175 void
176 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
177         int wantflags, int nowantflags, int mission)
178 {
179     register struct plchrstr *pcp;
180     struct plnstr plane;
181     struct plist *irv;
182     struct nstr_item ni;
183
184     emp_initque(list);
185     snxtitem_all(&ni, EF_PLANE);
186     while (nxtitem(&ni, (s_char *)&plane)) {
187         if (!plane.pln_own)
188             continue;
189
190         pcp = &plchr[(int)plane.pln_type];
191         if (!(pcp->pl_flags & P_M))
192             continue;
193         if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
194             continue;
195         if (nowantflags && pcp->pl_flags & nowantflags)
196             continue;
197         if (mission && plane.pln_mission != mission)
198             continue;
199         if (getrel(getnatp(plane.pln_own), victim) >= NEUTRAL)
200             continue;
201         /* missiles go one way, so we can use all the range */
202         if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
203             continue;
204         if (plane.pln_mobil <= (s_char)0)
205             continue;
206         if (plane.pln_effic < 100)
207             continue;
208         /* got a valid interceptor */
209         irv = (struct plist *)malloc(sizeof(*irv));
210         irv->state = P_OK;
211         irv->bombs = 0;
212         irv->misc = 0;
213         irv->pcp = &plchr[(int)plane.pln_type];
214         irv->plane = plane;
215         emp_insque(&irv->queue, list);
216     }
217 }
218
219 int
220 msl_intercept(coord x, coord y, natid bombown, int hardtarget,
221               int sublaunch, int wantflags, int nowantflags)
222 {
223     register struct plnstr *pp;
224     register struct plchrstr *pcp;
225     struct sctstr sect;
226     struct emp_qelem *irvlist;
227     struct emp_qelem foo;
228     struct emp_qelem *intlist;
229     struct emp_qelem intfoo;
230     struct emp_qelem *qp;
231     struct emp_qelem *next;
232     struct plist *ip;
233     int icount = 0;
234     short destroyed = 0;
235     s_char *att_name;
236     s_char *def_name;
237     int news_item;
238     s_char what[512];
239     s_char *who = sublaunch ? (s_char *)"" : cname(bombown);
240
241     getsect(x, y, &sect);
242     if (wantflags == P_O && !nowantflags) {
243         att_name = "satellite";
244         def_name = "a-sat missile";
245         news_item = N_SAT_KILL;
246         if (sect.sct_own) {
247             mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
248                 sublaunch ? (s_char *)"someone" : cname(bombown), xyas(x,
249                                                                        y,
250                                                                        sect.
251                                                                        sct_own));
252         }
253     } else if (wantflags == P_N && nowantflags == P_O) {
254         att_name = "warhead";
255         def_name = "abm";
256         news_item = sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP;
257     } else {
258         att_name = "elephant";
259         def_name = "tomato";    /* heh -KHS */
260         news_item = N_NUKE_STOP;
261     }
262     sprintf(what, "%s %s", who, att_name);
263     irvlist = &foo;
264
265     /* get all hostile abms in range */
266     msl_sel(irvlist, x, y, bombown, wantflags, nowantflags, 0);
267     intlist = &intfoo;
268     emp_initque(intlist);
269     /* First choose interceptors belonging to the target sector */
270     /* only allow two defense missiles per missile attack */
271     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
272         next = qp->q_forw;
273         ip = (struct plist *)qp;
274         pp = &ip->plane;
275         if (pp->pln_own != sect.sct_own)
276             continue;
277         pcp = ip->pcp;
278         if (mission_pln_equip(ip, 0, 0, 'i') < 0) {
279             emp_remque(qp);
280             free((s_char *)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     /* only allow two defense missiles per missile attack */
292     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
293         next = qp->q_forw;
294         ip = (struct plist *)qp;
295         pp = &ip->plane;
296         pcp = ip->pcp;
297         if (mission_pln_equip(ip, 0, 0, 'i') < 0) {
298             emp_remque(qp);
299             free((s_char *)qp);
300             continue;
301         }
302         /* got one interceptor, delete from irv_list and
303          * add to  int_list.
304          */
305         emp_remque(qp);
306         emp_insque(qp, intlist);
307         putplane(pp->pln_uid, pp);
308         icount++;
309     }
310     /* Now, clean out the queue */
311     while (!QEMPTY(irvlist)) {
312         qp = irvlist->q_forw;
313         emp_remque(qp);
314         free((s_char *)qp);
315     }
316     if (icount == 0) {
317         if (sect.sct_own != 0)
318             mpr(sect.sct_own, "No %ss launched to intercept.\n", def_name);
319         return (destroyed);
320     }
321
322     /* attempt to destroy incoming missile */
323
324     while (!QEMPTY(intlist)) {
325         qp = intlist->q_forw;
326         ip = (struct plist *)qp;
327         pp = &ip->plane;
328         pcp = ip->pcp;
329
330         mpr(bombown, "%s %s launched in defense!\n",
331             cname(pp->pln_own), def_name);
332         if (sect.sct_own == pp->pln_own) {
333             mpr(sect.sct_own, "%s launched to intercept %s %s!\n",
334                 def_name, who, att_name);
335         } else {
336             if (sect.sct_own)
337                 mpr(sect.sct_own,
338                     "%s launched an %s to intercept the %s %s!\n",
339                     cname(pp->pln_own), def_name, who, att_name);
340             mpr(pp->pln_own,
341                 "%s launched to intercept %s %s arcing towards %s territory!\n",
342                 def_name, who, att_name, cname(sect.sct_own));
343         }
344
345         if (!destroyed &&
346             msl_hit(pp, hardtarget, EF_PLANE, news_item, news_item, what,
347                     x, y, bombown)) {
348             mpr(bombown, "%s destroyed by %s %s!\n", att_name,
349                 cname(pp->pln_own), def_name);
350             if (sect.sct_own)
351                 mpr(sect.sct_own, "%s %s intercepted!\n", who, att_name);
352             if (sect.sct_own != pp->pln_own)
353                 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
354             destroyed = 1;
355         }
356         /* zap the missile */
357         makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
358         pp->pln_own = 0;
359         putplane(pp->pln_uid, pp);
360         emp_remque(qp);
361         free((s_char *)qp);
362         if (destroyed)
363             break;
364     }
365     /* Clean out what is left in the list */
366     while (!QEMPTY(intlist)) {
367         qp = intlist->q_forw;
368         emp_remque(qp);
369         free((s_char *)qp);
370     }
371     if (destroyed)
372         return (destroyed);
373     if (icount) {
374         mpr(bombown, "%s made it through %s defenses!\n", att_name,
375             def_name);
376         if (sect.sct_own)
377             mpr(sect.sct_own, "%s made it through %s defenses!\n",
378                 att_name, def_name);
379     }
380     return (destroyed);
381 }
382
383 /* Keep launching missiles on list until mindam damage has been done */
384 int
385 msl_launch_mindam(struct emp_qelem *list, coord x, coord y, int hardtarget,
386                   int type, int mindam, s_char *whatp, int victim,
387                   int mission)
388 {
389     struct emp_qelem *qp;
390     struct emp_qelem *next;
391     struct plist *plp;
392     int newdam, dam = 0;
393     int nukedam = 0;
394     int news_item;
395     int snews_item;
396
397     if (type == EF_SHIP) {
398         news_item = N_SHP_MISS;
399         snews_item = N_SHP_SMISS;
400     } else if (type == EF_LAND) {
401         news_item = N_LND_MISS;
402         snews_item = N_LND_SMISS;
403     } else {
404         news_item = N_SCT_MISS;
405         snews_item = N_SCT_SMISS;
406     }
407
408     for (qp = list->q_back; qp != list && dam < mindam && !nukedam;
409          qp = next) {
410         next = qp->q_back;
411         plp = (struct plist *)qp;
412
413         if (mission_pln_equip(plp, 0, 0, 'p') >= 0) {
414             if (msl_hit(&plp->plane,
415                         hardtarget, type, news_item, snews_item,
416                         whatp, x, y, victim)) {
417                 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
418                 if (nukedam) {
419                     if (mission == MI_INTERDICT && type == EF_SECTOR)
420                         dam += nukedam;
421                 } else
422                     dam += newdam;
423             } else {
424                 /* Missiles that miss have to hit somewhere! */
425                 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
426                 collateral_damage(x, y, newdam, 0);
427             }
428             makelost(EF_PLANE, plp->plane.pln_own, plp->plane.pln_uid,
429                      plp->plane.pln_x, plp->plane.pln_y);
430             plp->plane.pln_own = 0;
431             putplane(plp->plane.pln_uid, &plp->plane);
432             emp_remque(qp);
433             free(qp);
434         }
435     }
436     return dam;
437 }