]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mslsub.c
Remove some redundant casts.
[empserver] / src / lib / subs / mslsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
36 #include "queue.h"
37 #include "player.h"
38 #include "sect.h"
39 #include "ship.h"
40 #include "optlist.h"
41 #include "plane.h"
42 #include "land.h"
43 #include "news.h"
44 #include "xy.h"
45 #include "nsc.h"
46 #include "file.h"
47 #include "nat.h"
48 #include "path.h"
49 #include "mission.h"
50 #include "prototypes.h"
51
52 int
53 msl_equip(struct plnstr *pp)
54 {
55     struct plist pl;
56
57     memset(&pl, 0, sizeof(struct plist));
58     pl.pcp = plchr + pp->pln_type;
59     pl.plane = *pp;
60     return mission_pln_equip(&pl, 0, 0, 'p');
61 }
62
63 int
64 msl_hit(struct plnstr *pp, int hardtarget, int type, int news_item,
65         int snews_item, s_char *what, coord x, coord y, int victim)
66 {
67     int hit;
68     struct shpstr ship;
69     struct sctstr sect;
70     int sublaunch = 0;
71     struct plchrstr *pcp = plchr + pp->pln_type;
72     int hitchance = pln_hitchance(pp, hardtarget, type);
73     s_char *from;
74     int dam, dummyi;
75
76     mpr(pp->pln_own, "Preparing to launch %s at %s %s %s%s\n",
77         prplane(pp),
78         cname(victim),
79         what,
80         (type == EF_SHIP || type == EF_PLANE) ? "in " : "",
81         xyas(x, y, pp->pln_own));
82     mpr(pp->pln_own, "\tLaunching from ");
83     if (pp->pln_ship >= 0) {
84         getship(pp->pln_ship, &ship);
85         mpr(pp->pln_own, "%s in ", prship(&ship));
86         if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
87             sublaunch = 1;
88             from = "in hatch";
89         } else
90             from = "on deck";
91         mpr(pp->pln_own, "%s\n",
92             xyas(ship.shp_x, ship.shp_y, pp->pln_own));
93     } else {
94         if (pp->pln_harden > 0) {
95             mpr(pp->pln_own, "missile silo at ");
96             from = "in silo";
97         } else
98             from = "on launch pad";
99         mpr(pp->pln_own, "%s\n", xyas(pp->pln_x, pp->pln_y, pp->pln_own));
100     }
101
102     if (chance((0.05 + (100 - pp->pln_effic) / 100.0)
103                * (1 - techfact(pp->pln_tech, 1.0)))) {
104         mpr(pp->pln_own, "KABOOOOM!  Missile explodes %s!\n", from);
105         if (chance(0.33)) {
106             dam = pln_damage(pp, pp->pln_x, pp->pln_y,
107                              'p', &dummyi, 1) / 2;
108             if (dam) {
109                 if (pp->pln_ship >= 0) {
110                     shipdamage(&ship, dam);
111                     putship(ship.shp_uid, &ship);
112                 } else {
113                     pr("Explosion damages %s %d%%",
114                        xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
115                     getsect(pp->pln_x, pp->pln_y, &sect);
116                     sectdamage(&sect, dam, 0);
117                     putsect(&sect);
118                 }
119             }
120         }
121         return 0;
122     }
123
124     mpr(pp->pln_own, "\tSHWOOOOOSH!  Missile launched!\n");
125
126     if (pp->pln_nuketype != -1)
127         mpr(pp->pln_own, "\tArming nuclear warheads...\n");
128
129     if (pcp->pl_flags & P_T)
130         mpr(victim, "Incoming %s missile sighted at %s...\n",
131             sublaunch ? "sub-launched" : cname(pp->pln_own),
132             xyas(x, y, victim));
133
134     if (opt_PINPOINTMISSILE == 0 ||
135         (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 (pp->pln_nuketype != -1)
148         hitchance = 100;
149
150     mpr(pp->pln_own, "\t%d%% hitchance...", hitchance);
151     hit = (roll(100) <= hitchance);
152
153     mpr(pp->pln_own, hit ? "HIT!\n" : "miss\n");
154     if (pcp->pl_flags & P_T)
155         mpr(victim, "...Incoming %s missile %s\n",
156             sublaunch ? (s_char *)"" : cname(pp->pln_own),
157             hit ? "HIT!\n" : "missed\n");
158     if (hit && news_item) {
159         if (sublaunch)
160             nreport(victim, snews_item, 0, 1);
161         else
162             nreport(pp->pln_own, news_item, victim, 1);
163     }
164     return hit;
165 }
166
167 void
168 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
169         int wantflags, int nowantflags, int mission)
170 {
171     struct plchrstr *pcp;
172     struct plnstr plane;
173     struct plist *irv;
174     struct nstr_item ni;
175
176     emp_initque(list);
177     snxtitem_all(&ni, EF_PLANE);
178     while (nxtitem(&ni, (s_char *)&plane)) {
179         if (!plane.pln_own)
180             continue;
181
182         pcp = &plchr[(int)plane.pln_type];
183         if (!(pcp->pl_flags & P_M))
184             continue;
185         if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
186             continue;
187         if (nowantflags && pcp->pl_flags & nowantflags)
188             continue;
189         if (mission && plane.pln_mission != mission)
190             continue;
191         if (getrel(getnatp(plane.pln_own), victim) >= NEUTRAL)
192             continue;
193         /* missiles go one way, so we can use all the range */
194         if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
195             continue;
196         if (plane.pln_mobil <= 0)
197             continue;
198         if (plane.pln_effic < 100)
199             continue;
200         /* got a valid interceptor */
201         irv = (struct plist *)malloc(sizeof(*irv));
202         irv->state = P_OK;
203         irv->bombs = 0;
204         irv->misc = 0;
205         irv->pcp = &plchr[(int)plane.pln_type];
206         irv->plane = plane;
207         emp_insque(&irv->queue, list);
208     }
209 }
210
211 int
212 msl_intercept(coord x, coord y, natid bombown, int hardtarget,
213               int sublaunch, int wantflags, int nowantflags)
214 {
215     struct plnstr *pp;
216     struct plchrstr *pcp;
217     struct sctstr sect;
218     struct emp_qelem *irvlist;
219     struct emp_qelem foo;
220     struct emp_qelem *intlist;
221     struct emp_qelem intfoo;
222     struct emp_qelem *qp;
223     struct emp_qelem *next;
224     struct plist *ip;
225     int icount = 0;
226     short destroyed = 0;
227     s_char *att_name;
228     s_char *def_name;
229     int news_item;
230     s_char *who = sublaunch ? (s_char *)"" : cname(bombown);
231
232     getsect(x, y, &sect);
233     if (wantflags == P_O && !nowantflags) {
234         att_name = "satellite";
235         def_name = "a-sat missile";
236         news_item = N_SAT_KILL;
237         if (sect.sct_own) {
238             mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
239                 sublaunch ? (s_char *)"someone" : cname(bombown), xyas(x,
240                                                                        y,
241                                                                        sect.
242                                                                        sct_own));
243         }
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((s_char *)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((s_char *)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((s_char *)qp);
305     }
306     if (icount == 0) {
307         if (sect.sct_own != 0)
308             mpr(sect.sct_own, "No %ss launched to intercept.\n", def_name);
309         return (destroyed);
310     }
311
312     /* attempt to destroy incoming missile */
313
314     while (!QEMPTY(intlist)) {
315         qp = intlist->q_forw;
316         ip = (struct plist *)qp;
317         pp = &ip->plane;
318         pcp = ip->pcp;
319
320         mpr(bombown, "%s %s launched in defense!\n",
321             cname(pp->pln_own), def_name);
322         if (sect.sct_own == pp->pln_own) {
323             mpr(sect.sct_own, "%s launched to intercept %s %s!\n",
324                 def_name, who, att_name);
325         } else {
326             if (sect.sct_own)
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", att_name,
339                 cname(pp->pln_own), def_name);
340             if (sect.sct_own)
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         makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
348         pp->pln_own = 0;
349         putplane(pp->pln_uid, pp);
350         emp_remque(qp);
351         free((s_char *)qp);
352         if (destroyed)
353             break;
354     }
355     /* Clean out what is left in the list */
356     while (!QEMPTY(intlist)) {
357         qp = intlist->q_forw;
358         emp_remque(qp);
359         free((s_char *)qp);
360     }
361     if (destroyed)
362         return (destroyed);
363     if (icount) {
364         mpr(bombown, "%s made it through %s defenses!\n", att_name,
365             def_name);
366         if (sect.sct_own)
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, s_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, 0, 0, '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             } else {
414                 /* Missiles that miss have to hit somewhere! */
415                 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
416                 collateral_damage(x, y, newdam, 0);
417             }
418             makelost(EF_PLANE, plp->plane.pln_own, plp->plane.pln_uid,
419                      plp->plane.pln_x, plp->plane.pln_y);
420             plp->plane.pln_own = 0;
421             putplane(plp->plane.pln_uid, &plp->plane);
422             emp_remque(qp);
423             free(qp);
424         }
425     }
426     return dam;
427 }