]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mslsub.c
Limit nukes to strategic missions
[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 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;
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, 'p', 1) / 2;
111             if (pp->pln_ship >= 0) {
112                 shipdamage(&ship, dam);
113                 putship(ship.shp_uid, &ship);
114             } else {
115                 pr("Explosion damages %s %d%%",
116                    xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
117                 getsect(pp->pln_x, pp->pln_y, &sect);
118                 sectdamage(&sect, dam);
119                 putsect(&sect);
120             }
121         }
122         return 0;
123     }
124
125     CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
126     pp->pln_flags |= PLN_LAUNCHED;
127     putplane(pp->pln_uid, pp);
128     mpr(pp->pln_own, "\tSHWOOOOOSH!  Missile launched!\n");
129
130     if (pcp->pl_flags & P_T)
131         mpr(victim, "Incoming %s missile sighted at %s...\n",
132             sublaunch ? "sub-launched" : cname(pp->pln_own),
133             xyas(x, y, victim));
134
135     if ((pcp->pl_flags & P_T && !(pcp->pl_flags & P_MAR))) {
136         if (msl_intercept(x, y, pp->pln_own, pcp->pl_def,
137                           sublaunch, P_N, P_O)) {
138             return 0;
139         }
140     }
141     if (pcp->pl_flags & P_MAR) {
142         if (shp_missile_defense(x, y, pp->pln_own, pcp->pl_def)) {
143             return 0;
144         }
145     }
146
147     if (nuk_on_plane(pp) >= 0) {
148         mpr(pp->pln_own, "\tArming nuclear warheads...\n");
149         hit = 1;
150     } else {
151         hitchance = pln_hitchance(pp, hardtarget, type);
152         hit = (roll(100) <= hitchance);
153         mpr(pp->pln_own, "\t%d%% hitchance...%s\n", hitchance,
154             hit ? "HIT!" : "miss");
155     }
156
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 (mission &&
195             plane.pln_radius < mapdist(x, y, plane.pln_opx, plane.pln_opy))
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->load = 0;
211         irv->pcp = &plchr[(int)plane.pln_type];
212         irv->plane = plane;
213         emp_insque(&irv->queue, list);
214     }
215 }
216
217 int
218 msl_intercept(coord x, coord y, natid bombown, int hardtarget,
219               int sublaunch, int wantflags, int nowantflags)
220 {
221     struct plnstr *pp;
222     struct plchrstr *pcp;
223     struct sctstr sect;
224     struct emp_qelem *irvlist;
225     struct emp_qelem foo;
226     struct emp_qelem *intlist;
227     struct emp_qelem intfoo;
228     struct emp_qelem *qp;
229     struct emp_qelem *next;
230     struct plist *ip;
231     int icount = 0;
232     short destroyed = 0;
233     char *att_name;
234     char *def_name;
235     int news_item;
236     char *who = sublaunch ? "" : cname(bombown);
237
238     getsect(x, y, &sect);
239     if (wantflags == P_O && !nowantflags) {
240         att_name = "satellite";
241         def_name = "a-sat missile";
242         news_item = N_SAT_KILL;
243         mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
244             sublaunch ? "someone" : cname(bombown),
245             xyas(x, y, sect.sct_own));
246     } else if (wantflags == P_N && nowantflags == P_O) {
247         att_name = "warhead";
248         def_name = "abm";
249         news_item = sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP;
250     } else {
251         att_name = "elephant";
252         def_name = "tomato";    /* heh -KHS */
253         news_item = N_NUKE_STOP;
254     }
255     irvlist = &foo;
256
257     /* get all hostile abms in range */
258     msl_sel(irvlist, x, y, bombown, wantflags, nowantflags, 0);
259     intlist = &intfoo;
260     emp_initque(intlist);
261     /* First choose interceptors belonging to the target sector */
262     /* only allow two defense missiles per missile attack */
263     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
264         next = qp->q_forw;
265         ip = (struct plist *)qp;
266         pp = &ip->plane;
267         if (pp->pln_own != sect.sct_own)
268             continue;
269         pcp = ip->pcp;
270         if (mission_pln_equip(ip, NULL, 'i') < 0) {
271             emp_remque(qp);
272             free(qp);
273             continue;
274         }
275         /* got one interceptor, delete from irv_list and
276          * add to  int_list.
277          */
278         emp_remque(qp);
279         emp_insque(qp, intlist);
280         putplane(pp->pln_uid, pp);
281         icount++;
282     }
283     /* only allow two defense missiles per missile attack */
284     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
285         next = qp->q_forw;
286         ip = (struct plist *)qp;
287         pp = &ip->plane;
288         pcp = ip->pcp;
289         if (mission_pln_equip(ip, NULL, 'i') < 0) {
290             emp_remque(qp);
291             free(qp);
292             continue;
293         }
294         /* got one interceptor, delete from irv_list and
295          * add to  int_list.
296          */
297         emp_remque(qp);
298         emp_insque(qp, intlist);
299         putplane(pp->pln_uid, pp);
300         icount++;
301     }
302     /* Now, clean out the queue */
303     while (!QEMPTY(irvlist)) {
304         qp = irvlist->q_forw;
305         emp_remque(qp);
306         free(qp);
307     }
308     if (icount == 0) {
309         mpr(sect.sct_own, "No %ss launched to intercept.\n", def_name);
310         return destroyed;
311     }
312
313     /* attempt to destroy incoming missile */
314
315     while (!QEMPTY(intlist)) {
316         qp = intlist->q_forw;
317         ip = (struct plist *)qp;
318         pp = &ip->plane;
319         pcp = ip->pcp;
320
321         mpr(bombown, "%s %s launched in defense!\n",
322             cname(pp->pln_own), def_name);
323         if (sect.sct_own == pp->pln_own) {
324             mpr(sect.sct_own, "%s launched to intercept %s %s!\n",
325                 def_name, who, att_name);
326         } else {
327             mpr(sect.sct_own,
328                 "%s launched an %s to intercept the %s %s!\n",
329                 cname(pp->pln_own), def_name, who, att_name);
330             mpr(pp->pln_own,
331                 "%s launched to intercept %s %s arcing towards %s territory!\n",
332                 def_name, who, att_name, cname(sect.sct_own));
333         }
334
335         if (!destroyed &&
336             msl_hit(pp, hardtarget, EF_PLANE, news_item, news_item,
337                     att_name, x, y, bombown)) {
338             mpr(bombown, "%s destroyed by %s %s!\n",
339                 att_name, cname(pp->pln_own), def_name);
340             mpr(sect.sct_own, "%s %s intercepted!\n", who, att_name);
341             if (sect.sct_own != pp->pln_own)
342                 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
343             destroyed = 1;
344         }
345         /* zap the missile */
346         pp->pln_effic = 0;
347         putplane(pp->pln_uid, pp);
348         emp_remque(qp);
349         free(qp);
350         if (destroyed)
351             break;
352     }
353     /* Clean out what is left in the list */
354     while (!QEMPTY(intlist)) {
355         qp = intlist->q_forw;
356         emp_remque(qp);
357         free(qp);
358     }
359     if (destroyed)
360         return destroyed;
361     if (icount) {
362         mpr(bombown, "%s made it through %s defenses!\n",
363             att_name, def_name);
364         mpr(sect.sct_own, "%s made it through %s defenses!\n",
365             att_name, def_name);
366     }
367     return destroyed;
368 }
369
370 /* Keep launching missiles on list until mindam damage has been done */
371 int
372 msl_launch_mindam(struct emp_qelem *list, coord x, coord y, int hardtarget,
373                   int type, int mindam, char *whatp, int victim)
374 {
375     struct emp_qelem *qp;
376     struct emp_qelem *next;
377     struct plist *plp;
378     int newdam, dam = 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;
394          qp = next) {
395         next = qp->q_back;
396         plp = (struct plist *)qp;
397
398         if (mission_pln_equip(plp, NULL, '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, 'p', 1);
403                 dam += newdam;
404 #if 0
405             /*
406              * FIXME want collateral damage on miss, but we get here
407              * too when launch fails or missile is intercepted
408              */
409             } else {
410                 /* Missiles that miss have to hit somewhere! */
411                 newdam = pln_damage(&plp->plane, 'p', 0);
412                 collateral_damage(x, y, newdam);
413 #endif
414             }
415             plp->plane.pln_effic = 0;
416             putplane(plp->plane.pln_uid, &plp->plane);
417             emp_remque(qp);
418             free(qp);
419         }
420     }
421     return dam;
422 }