]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mslsub.c
872d72cae721dee03aa2b05c08c580eab78561cd
[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_equip(struct plnstr *pp, char mission)
58 {
59     struct plist pl;
60
61     memset(&pl, 0, sizeof(struct plist));
62     pl.pcp = plchr + pp->pln_type;
63     pl.plane = *pp;
64     return mission_pln_equip(&pl, NULL, mission);
65 }
66
67 int
68 msl_hit(struct plnstr *pp, int hardtarget, int type, int news_item,
69         int snews_item, char *what, coord x, coord y, int victim)
70 {
71     int hit;
72     struct shpstr ship;
73     struct sctstr sect;
74     int sublaunch = 0;
75     struct plchrstr *pcp = plchr + pp->pln_type;
76     int hitchance;
77     char *from;
78     int dam, dummyi;
79
80     mpr(pp->pln_own, "Preparing to launch %s at %s %s %s%s\n",
81         prplane(pp),
82         cname(victim),
83         what,
84         (type == EF_SHIP || type == EF_PLANE) ? "in " : "",
85         xyas(x, y, pp->pln_own));
86     mpr(pp->pln_own, "\tLaunching from ");
87     if (pp->pln_ship >= 0) {
88         getship(pp->pln_ship, &ship);
89         mpr(pp->pln_own, "%s in ", prship(&ship));
90         if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
91             sublaunch = 1;
92             from = "in hatch";
93         } else
94             from = "on deck";
95         mpr(pp->pln_own, "%s\n",
96             xyas(ship.shp_x, ship.shp_y, pp->pln_own));
97     } else {
98         if (pp->pln_harden > 0) {
99             mpr(pp->pln_own, "missile silo at ");
100             from = "in silo";
101         } else
102             from = "on launch pad";
103         mpr(pp->pln_own, "%s\n", xyas(pp->pln_x, pp->pln_y, pp->pln_own));
104     }
105
106     if (chance((0.05 + (100 - pp->pln_effic) / 100.0)
107                * (1 - techfact(pp->pln_tech, 1.0)))) {
108         mpr(pp->pln_own, "KABOOOOM!  Missile explodes %s!\n", from);
109         if (chance(0.33)) {
110             dam = pln_damage(pp, pp->pln_x, pp->pln_y,
111                              'p', &dummyi, 1) / 2;
112             if (dam) {
113                 if (pp->pln_ship >= 0) {
114                     shipdamage(&ship, dam);
115                     putship(ship.shp_uid, &ship);
116                 } else {
117                     pr("Explosion damages %s %d%%",
118                        xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
119                     getsect(pp->pln_x, pp->pln_y, &sect);
120                     sectdamage(&sect, dam);
121                     putsect(&sect);
122                 }
123             }
124         }
125         return 0;
126     }
127
128     CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
129     pp->pln_flags |= PLN_LAUNCHED;
130     putplane(pp->pln_uid, pp);
131     mpr(pp->pln_own, "\tSHWOOOOOSH!  Missile launched!\n");
132
133     if (pcp->pl_flags & P_T)
134         mpr(victim, "Incoming %s missile sighted at %s...\n",
135             sublaunch ? "sub-launched" : cname(pp->pln_own),
136             xyas(x, y, victim));
137
138     if ((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 (nuk_on_plane(pp) >= 0) {
151         mpr(pp->pln_own, "\tArming nuclear warheads...\n");
152         hit = 1;
153     } else {
154         hitchance = pln_hitchance(pp, hardtarget, type);
155         hit = (roll(100) <= hitchance);
156         mpr(pp->pln_own, "\t%d%% hitchance...%s\n", hitchance,
157             hit ? "HIT!" : "miss");
158     }
159
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 (mission &&
198             plane.pln_radius < mapdist(x, y, plane.pln_opx, plane.pln_opy))
199             continue;
200         if (getrel(getnatp(plane.pln_own), victim) >= NEUTRAL)
201             continue;
202         /* missiles go one way, so we can use all the range */
203         if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
204             continue;
205         if (plane.pln_mobil <= 0)
206             continue;
207         if (plane.pln_effic < 100)
208             continue;
209         if (!pln_airbase_ok(&plane, 1, 0))
210             continue;
211         /* got a valid interceptor */
212         irv = malloc(sizeof(*irv));
213         irv->load = 0;
214         irv->pcp = &plchr[(int)plane.pln_type];
215         irv->plane = plane;
216         emp_insque(&irv->queue, list);
217     }
218 }
219
220 int
221 msl_intercept(coord x, coord y, natid bombown, int hardtarget,
222               int sublaunch, int wantflags, int nowantflags)
223 {
224     struct plnstr *pp;
225     struct plchrstr *pcp;
226     struct sctstr sect;
227     struct emp_qelem *irvlist;
228     struct emp_qelem foo;
229     struct emp_qelem *intlist;
230     struct emp_qelem intfoo;
231     struct emp_qelem *qp;
232     struct emp_qelem *next;
233     struct plist *ip;
234     int icount = 0;
235     short destroyed = 0;
236     char *att_name;
237     char *def_name;
238     int news_item;
239     char *who = sublaunch ? "" : 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         mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
247             sublaunch ? "someone" : cname(bombown),
248             xyas(x, y, sect.sct_own));
249     } else if (wantflags == P_N && nowantflags == P_O) {
250         att_name = "warhead";
251         def_name = "abm";
252         news_item = sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP;
253     } else {
254         att_name = "elephant";
255         def_name = "tomato";    /* heh -KHS */
256         news_item = N_NUKE_STOP;
257     }
258     irvlist = &foo;
259
260     /* get all hostile abms in range */
261     msl_sel(irvlist, x, y, bombown, wantflags, nowantflags, 0);
262     intlist = &intfoo;
263     emp_initque(intlist);
264     /* First choose interceptors belonging to the target sector */
265     /* only allow two defense missiles per missile attack */
266     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
267         next = qp->q_forw;
268         ip = (struct plist *)qp;
269         pp = &ip->plane;
270         if (pp->pln_own != sect.sct_own)
271             continue;
272         pcp = ip->pcp;
273         if (mission_pln_equip(ip, NULL, 'i') < 0) {
274             emp_remque(qp);
275             free(qp);
276             continue;
277         }
278         /* got one interceptor, delete from irv_list and
279          * add to  int_list.
280          */
281         emp_remque(qp);
282         emp_insque(qp, intlist);
283         putplane(pp->pln_uid, pp);
284         icount++;
285     }
286     /* only allow two defense missiles per missile attack */
287     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
288         next = qp->q_forw;
289         ip = (struct plist *)qp;
290         pp = &ip->plane;
291         pcp = ip->pcp;
292         if (mission_pln_equip(ip, NULL, 'i') < 0) {
293             emp_remque(qp);
294             free(qp);
295             continue;
296         }
297         /* got one interceptor, delete from irv_list and
298          * add to  int_list.
299          */
300         emp_remque(qp);
301         emp_insque(qp, intlist);
302         putplane(pp->pln_uid, pp);
303         icount++;
304     }
305     /* Now, clean out the queue */
306     while (!QEMPTY(irvlist)) {
307         qp = irvlist->q_forw;
308         emp_remque(qp);
309         free(qp);
310     }
311     if (icount == 0) {
312         mpr(sect.sct_own, "No %ss launched to intercept.\n", def_name);
313         return destroyed;
314     }
315
316     /* attempt to destroy incoming missile */
317
318     while (!QEMPTY(intlist)) {
319         qp = intlist->q_forw;
320         ip = (struct plist *)qp;
321         pp = &ip->plane;
322         pcp = ip->pcp;
323
324         mpr(bombown, "%s %s launched in defense!\n",
325             cname(pp->pln_own), def_name);
326         if (sect.sct_own == pp->pln_own) {
327             mpr(sect.sct_own, "%s launched to intercept %s %s!\n",
328                 def_name, who, att_name);
329         } else {
330             mpr(sect.sct_own,
331                 "%s launched an %s to intercept the %s %s!\n",
332                 cname(pp->pln_own), def_name, who, att_name);
333             mpr(pp->pln_own,
334                 "%s launched to intercept %s %s arcing towards %s territory!\n",
335                 def_name, who, att_name, cname(sect.sct_own));
336         }
337
338         if (!destroyed &&
339             msl_hit(pp, hardtarget, EF_PLANE, news_item, news_item,
340                     att_name, x, y, bombown)) {
341             mpr(bombown, "%s destroyed by %s %s!\n",
342                 att_name, cname(pp->pln_own), def_name);
343             mpr(sect.sct_own, "%s %s intercepted!\n", who, att_name);
344             if (sect.sct_own != pp->pln_own)
345                 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
346             destroyed = 1;
347         }
348         /* zap the missile */
349         pp->pln_effic = 0;
350         putplane(pp->pln_uid, pp);
351         emp_remque(qp);
352         free(qp);
353         if (destroyed)
354             break;
355     }
356     /* Clean out what is left in the list */
357     while (!QEMPTY(intlist)) {
358         qp = intlist->q_forw;
359         emp_remque(qp);
360         free(qp);
361     }
362     if (destroyed)
363         return destroyed;
364     if (icount) {
365         mpr(bombown, "%s made it through %s defenses!\n",
366             att_name, def_name);
367         mpr(sect.sct_own, "%s made it through %s defenses!\n",
368             att_name, def_name);
369     }
370     return destroyed;
371 }
372
373 /* Keep launching missiles on list until mindam damage has been done */
374 int
375 msl_launch_mindam(struct emp_qelem *list, coord x, coord y, int hardtarget,
376                   int type, int mindam, char *whatp, int victim,
377                   int mission)
378 {
379     struct emp_qelem *qp;
380     struct emp_qelem *next;
381     struct plist *plp;
382     int newdam, dam = 0;
383     int nukedam = 0;
384     int news_item;
385     int snews_item;
386
387     if (type == EF_SHIP) {
388         news_item = N_SHP_MISS;
389         snews_item = N_SHP_SMISS;
390     } else if (type == EF_LAND) {
391         news_item = N_LND_MISS;
392         snews_item = N_LND_SMISS;
393     } else {
394         news_item = N_SCT_MISS;
395         snews_item = N_SCT_SMISS;
396     }
397
398     for (qp = list->q_back; qp != list && dam < mindam && !nukedam;
399          qp = next) {
400         next = qp->q_back;
401         plp = (struct plist *)qp;
402
403         if (mission_pln_equip(plp, NULL, 'p') >= 0) {
404             if (msl_hit(&plp->plane,
405                         hardtarget, type, news_item, snews_item,
406                         whatp, x, y, victim)) {
407                 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
408                 if (nukedam) {
409                     if (mission == MI_INTERDICT && type == EF_SECTOR)
410                         dam += nukedam;
411                 } else
412                     dam += newdam;
413 #if 0
414             /*
415              * FIXME want collateral damage on miss (which can't
416              * happen for nuclear war heads), but we get here too when
417              * launch fails or missile is intercepted
418              */
419             } else {
420                 /* Missiles that miss have to hit somewhere! */
421                 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
422                 collateral_damage(x, y, newdam);
423 #endif
424             }
425             plp->plane.pln_effic = 0;
426             putplane(plp->plane.pln_uid, &plp->plane);
427             emp_remque(qp);
428             free(qp);
429         }
430     }
431     return dam;
432 }