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