]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mslsub.c
Check pln_airbase_ok() in msl_sel()
[empserver] / src / lib / subs / mslsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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 "optlist.h"
46 #include "path.h"
47 #include "plane.h"
48 #include "player.h"
49 #include "prototypes.h"
50 #include "queue.h"
51 #include "sect.h"
52 #include "ship.h"
53 #include "xy.h"
54
55 int
56 msl_equip(struct plnstr *pp, char mission)
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, mission);
64 }
65
66 int
67 msl_hit(struct plnstr *pp, int hardtarget, int type, int news_item,
68         int snews_item, 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     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             dam = pln_damage(pp, pp->pln_x, pp->pln_y,
110                              'p', &dummyi, 1) / 2;
111             if (dam) {
112                 if (pp->pln_ship >= 0) {
113                     shipdamage(&ship, dam);
114                     putship(ship.shp_uid, &ship);
115                 } else {
116                     pr("Explosion damages %s %d%%",
117                        xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
118                     getsect(pp->pln_x, pp->pln_y, &sect);
119                     sectdamage(&sect, dam);
120                     putsect(&sect);
121                 }
122             }
123         }
124         return 0;
125     }
126
127     mpr(pp->pln_own, "\tSHWOOOOOSH!  Missile launched!\n");
128
129     if (pp->pln_nuketype != -1)
130         mpr(pp->pln_own, "\tArming nuclear warheads...\n");
131
132     if (pcp->pl_flags & P_T)
133         mpr(victim, "Incoming %s missile sighted at %s...\n",
134             sublaunch ? "sub-launched" : cname(pp->pln_own),
135             xyas(x, y, victim));
136
137     if (opt_PINPOINTMISSILE == 0 ||
138         (pcp->pl_flags & P_T && !(pcp->pl_flags & P_MAR))) {
139         if (msl_intercept(x, y, pp->pln_own, pcp->pl_def,
140                           sublaunch, P_N, P_O)) {
141             return 0;
142         }
143     }
144     if (pcp->pl_flags & P_MAR) {
145         if (shp_missile_defense(x, y, pp->pln_own, pcp->pl_def)) {
146             return 0;
147         }
148     }
149
150     if (pp->pln_nuketype != -1)
151         hitchance = 100;
152
153     mpr(pp->pln_own, "\t%d%% hitchance...", hitchance);
154     hit = (roll(100) <= hitchance);
155
156     mpr(pp->pln_own, hit ? "HIT!\n" : "miss\n");
157     if (pcp->pl_flags & P_T)
158         mpr(victim, "...Incoming %s missile %s\n",
159             sublaunch ? "" : cname(pp->pln_own),
160             hit ? "HIT!\n" : "missed\n");
161     if (hit && news_item) {
162         if (sublaunch)
163             nreport(victim, snews_item, 0, 1);
164         else
165             nreport(pp->pln_own, news_item, victim, 1);
166     }
167     return hit;
168 }
169
170 void
171 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
172         int wantflags, int nowantflags, int mission)
173 {
174     struct plchrstr *pcp;
175     struct plnstr plane;
176     struct plist *irv;
177     struct nstr_item ni;
178
179     emp_initque(list);
180     snxtitem_all(&ni, EF_PLANE);
181     while (nxtitem(&ni, &plane)) {
182         if (!plane.pln_own)
183             continue;
184
185         pcp = &plchr[(int)plane.pln_type];
186         if (!(pcp->pl_flags & P_M))
187             continue;
188         if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
189             continue;
190         if (nowantflags && pcp->pl_flags & nowantflags)
191             continue;
192         if (mission && plane.pln_mission != mission)
193             continue;
194         if (getrel(getnatp(plane.pln_own), victim) >= NEUTRAL)
195             continue;
196         /* missiles go one way, so we can use all the range */
197         if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
198             continue;
199         if (plane.pln_mobil <= 0)
200             continue;
201         if (plane.pln_effic < 100)
202             continue;
203         if (!pln_airbase_ok(&plane, 1, 0))
204             continue;
205         /* got a valid interceptor */
206         irv = malloc(sizeof(*irv));
207         irv->bombs = 0;
208         irv->misc = 0;
209         irv->pcp = &plchr[(int)plane.pln_type];
210         irv->plane = plane;
211         emp_insque(&irv->queue, list);
212     }
213 }
214
215 int
216 msl_intercept(coord x, coord y, natid bombown, int hardtarget,
217               int sublaunch, int wantflags, int nowantflags)
218 {
219     struct plnstr *pp;
220     struct plchrstr *pcp;
221     struct sctstr sect;
222     struct emp_qelem *irvlist;
223     struct emp_qelem foo;
224     struct emp_qelem *intlist;
225     struct emp_qelem intfoo;
226     struct emp_qelem *qp;
227     struct emp_qelem *next;
228     struct plist *ip;
229     int icount = 0;
230     short destroyed = 0;
231     char *att_name;
232     char *def_name;
233     int news_item;
234     char *who = sublaunch ? "" : cname(bombown);
235
236     getsect(x, y, &sect);
237     if (wantflags == P_O && !nowantflags) {
238         att_name = "satellite";
239         def_name = "a-sat missile";
240         news_item = N_SAT_KILL;
241         mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
242             sublaunch ? "someone" : cname(bombown),
243             xyas(x, y, sect.sct_own));
244     } else if (wantflags == P_N && nowantflags == P_O) {
245         att_name = "warhead";
246         def_name = "abm";
247         news_item = sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP;
248     } else {
249         att_name = "elephant";
250         def_name = "tomato";    /* heh -KHS */
251         news_item = N_NUKE_STOP;
252     }
253     irvlist = &foo;
254
255     /* get all hostile abms in range */
256     msl_sel(irvlist, x, y, bombown, wantflags, nowantflags, 0);
257     intlist = &intfoo;
258     emp_initque(intlist);
259     /* First choose interceptors belonging to the target sector */
260     /* only allow two defense missiles per missile attack */
261     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
262         next = qp->q_forw;
263         ip = (struct plist *)qp;
264         pp = &ip->plane;
265         if (pp->pln_own != sect.sct_own)
266             continue;
267         pcp = ip->pcp;
268         if (mission_pln_equip(ip, 0, 0, 'i') < 0) {
269             emp_remque(qp);
270             free(qp);
271             continue;
272         }
273         /* got one interceptor, delete from irv_list and
274          * add to  int_list.
275          */
276         emp_remque(qp);
277         emp_insque(qp, intlist);
278         putplane(pp->pln_uid, pp);
279         icount++;
280     }
281     /* only allow two defense missiles per missile attack */
282     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
283         next = qp->q_forw;
284         ip = (struct plist *)qp;
285         pp = &ip->plane;
286         pcp = ip->pcp;
287         if (mission_pln_equip(ip, 0, 0, 'i') < 0) {
288             emp_remque(qp);
289             free(qp);
290             continue;
291         }
292         /* got one interceptor, delete from irv_list and
293          * add to  int_list.
294          */
295         emp_remque(qp);
296         emp_insque(qp, intlist);
297         putplane(pp->pln_uid, pp);
298         icount++;
299     }
300     /* Now, clean out the queue */
301     while (!QEMPTY(irvlist)) {
302         qp = irvlist->q_forw;
303         emp_remque(qp);
304         free(qp);
305     }
306     if (icount == 0) {
307         mpr(sect.sct_own, "No %ss launched to intercept.\n", def_name);
308         return destroyed;
309     }
310
311     /* attempt to destroy incoming missile */
312
313     while (!QEMPTY(intlist)) {
314         qp = intlist->q_forw;
315         ip = (struct plist *)qp;
316         pp = &ip->plane;
317         pcp = ip->pcp;
318
319         mpr(bombown, "%s %s launched in defense!\n",
320             cname(pp->pln_own), def_name);
321         if (sect.sct_own == pp->pln_own) {
322             mpr(sect.sct_own, "%s launched to intercept %s %s!\n",
323                 def_name, who, att_name);
324         } else {
325             mpr(sect.sct_own,
326                 "%s launched an %s to intercept the %s %s!\n",
327                 cname(pp->pln_own), def_name, who, att_name);
328             mpr(pp->pln_own,
329                 "%s launched to intercept %s %s arcing towards %s territory!\n",
330                 def_name, who, att_name, cname(sect.sct_own));
331         }
332
333         if (!destroyed &&
334             msl_hit(pp, hardtarget, EF_PLANE, news_item, news_item,
335                     att_name, x, y, bombown)) {
336             mpr(bombown, "%s destroyed by %s %s!\n",
337                 att_name, cname(pp->pln_own), def_name);
338             mpr(sect.sct_own, "%s %s intercepted!\n", who, att_name);
339             if (sect.sct_own != pp->pln_own)
340                 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
341             destroyed = 1;
342         }
343         /* zap the missile */
344         pp->pln_effic = 0;
345         putplane(pp->pln_uid, pp);
346         emp_remque(qp);
347         free(qp);
348         if (destroyed)
349             break;
350     }
351     /* Clean out what is left in the list */
352     while (!QEMPTY(intlist)) {
353         qp = intlist->q_forw;
354         emp_remque(qp);
355         free(qp);
356     }
357     if (destroyed)
358         return destroyed;
359     if (icount) {
360         mpr(bombown, "%s made it through %s defenses!\n",
361             att_name, def_name);
362         mpr(sect.sct_own, "%s made it through %s defenses!\n",
363             att_name, def_name);
364     }
365     return destroyed;
366 }
367
368 /* Keep launching missiles on list until mindam damage has been done */
369 int
370 msl_launch_mindam(struct emp_qelem *list, coord x, coord y, int hardtarget,
371                   int type, int mindam, char *whatp, int victim,
372                   int mission)
373 {
374     struct emp_qelem *qp;
375     struct emp_qelem *next;
376     struct plist *plp;
377     int newdam, dam = 0;
378     int nukedam = 0;
379     int news_item;
380     int snews_item;
381
382     if (type == EF_SHIP) {
383         news_item = N_SHP_MISS;
384         snews_item = N_SHP_SMISS;
385     } else if (type == EF_LAND) {
386         news_item = N_LND_MISS;
387         snews_item = N_LND_SMISS;
388     } else {
389         news_item = N_SCT_MISS;
390         snews_item = N_SCT_SMISS;
391     }
392
393     for (qp = list->q_back; qp != list && dam < mindam && !nukedam;
394          qp = next) {
395         next = qp->q_back;
396         plp = (struct plist *)qp;
397
398         if (mission_pln_equip(plp, 0, 0, 'p') >= 0) {
399             if (msl_hit(&plp->plane,
400                         hardtarget, type, news_item, snews_item,
401                         whatp, x, y, victim)) {
402                 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
403                 if (nukedam) {
404                     if (mission == MI_INTERDICT && type == EF_SECTOR)
405                         dam += nukedam;
406                 } else
407                     dam += newdam;
408             } else {
409                 /* Missiles that miss have to hit somewhere! */
410                 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
411                 collateral_damage(x, y, newdam);
412             }
413             plp->plane.pln_effic = 0;
414             putplane(plp->plane.pln_uid, &plp->plane);
415             emp_remque(qp);
416             free(qp);
417         }
418     }
419     return dam;
420 }