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