]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mslsub.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / subs / mslsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 "var.h"
39 #include "sect.h"
40 #include "ship.h"
41 #include "optlist.h"
42 #include "nuke.h"
43 #include "plane.h"
44 #include "land.h"
45 #include "news.h"
46 #include "item.h"
47 #include "xy.h"
48 #include "nsc.h"
49 #include "file.h"
50 #include "nat.h"
51 #include "path.h"
52 #include "mission.h"
53 #include "prototypes.h"
54
55 int
56 msl_equip(struct plnstr *pp)
57 {
58     struct plist pl;
59
60     bzero((s_char *)&pl, sizeof(struct plist));
61     pl.pcp = plchr + pp->pln_type;
62     bcopy((s_char *)pp, (s_char *)&pl.plane, sizeof(struct plnstr));
63     return mission_pln_equip(&pl, 0, 0, 'p');
64 }
65
66 int
67 msl_hit(struct plnstr *pp, int hardtarget, int type, int news_item,
68         int snews_item, s_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     s_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 cilo";
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             if (pp->pln_nuketype != (s_char)-1 && opt_NUKEFAILDETONATE) {
110                 pp->pln_flags &= ~PLN_AIRBURST;
111                 detonate(pp, pp->pln_x, pp->pln_y);
112             } else {
113                 dam = pln_damage(pp, pp->pln_x, pp->pln_y,
114                                  'p', &dummyi, 1) / 2;
115                 if (dam) {
116                     if (pp->pln_ship >= 0) {
117                         shipdamage(&ship, dam);
118                         putship(ship.shp_uid, &ship);
119                     } else {
120                         pr("Explosion damages %s %d%%",
121                            xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
122                         getsect(pp->pln_x, pp->pln_y, &sect);
123                         sectdamage(&sect, dam, 0);
124                         putsect(&sect);
125                     }
126                 }
127             }
128         }
129         return 0;
130     }
131
132     mpr(pp->pln_own, "\tSHWOOOOOSH!  Missile launched!\n");
133
134     if (pp->pln_nuketype != (s_char)-1)
135         mpr(pp->pln_own, "\tArming nuclear warheads...\n");
136
137     if (pcp->pl_flags & P_T)
138         mpr(victim, "Incoming %s missile...\n",
139             sublaunch ? (s_char *)"" : cname(pp->pln_own));
140
141     if (opt_PINPOINTMISSILE == 0 ||
142         (pcp->pl_flags & P_T && !(pcp->pl_flags & P_MAR))) {
143         if (msl_intercept(x, y, pp->pln_own, pcp->pl_def,
144                           sublaunch, P_N, P_O)) {
145             return 0;
146         }
147     }
148     if (pcp->pl_flags & P_MAR) {
149         if (shp_missile_defense(x, y, pp->pln_own, pcp->pl_def)) {
150             return 0;
151         }
152     }
153
154     if (pp->pln_nuketype != (s_char)-1)
155         hitchance = 100;
156
157     mpr(pp->pln_own, "\t%d%% hitchance...", hitchance);
158     hit = (roll(100) <= hitchance);
159
160     mpr(pp->pln_own, hit ? "HIT!\n" : "miss\n");
161     if (pcp->pl_flags & P_T)
162         mpr(victim, "...Incoming %s missile %s\n",
163             sublaunch ? (s_char *)"" : cname(pp->pln_own),
164             hit ? "HIT!\n" : "missed\n");
165     if (hit && news_item) {
166         if (sublaunch)
167             nreport(victim, snews_item, 0, 1);
168         else
169             nreport(pp->pln_own, news_item, victim, 1);
170     }
171     return hit;
172 }
173
174 void
175 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
176         int wantflags, int nowantflags, int mission)
177 {
178     register struct plchrstr *pcp;
179     struct plnstr plane;
180     struct plist *irv;
181     struct nstr_item ni;
182
183     emp_initque(list);
184     snxtitem_all(&ni, EF_PLANE);
185     while (nxtitem(&ni, (s_char *)&plane)) {
186         if (!plane.pln_own)
187             continue;
188
189         pcp = &plchr[(int)plane.pln_type];
190         if (!(pcp->pl_flags & P_M))
191             continue;
192         if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
193             continue;
194         if (nowantflags && pcp->pl_flags & nowantflags)
195             continue;
196         if (mission && plane.pln_mission != mission)
197             continue;
198         if (getrel(getnatp(plane.pln_own), victim) >= NEUTRAL)
199             continue;
200         /* missiles go one way, so we can use all the range */
201         if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
202             continue;
203         if (plane.pln_mobil <= (s_char)0)
204             continue;
205         if (plane.pln_effic < 100)
206             continue;
207         /* got a valid interceptor */
208         irv = (struct plist *)malloc(sizeof(*irv));
209         irv->state = P_OK;
210         irv->bombs = 0;
211         irv->misc = 0;
212         irv->pcp = &plchr[(int)plane.pln_type];
213         bcopy((s_char *)&plane, (s_char *)&irv->plane, sizeof(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     register struct plnstr *pp;
223     register 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     s_char *att_name;
235     s_char *def_name;
236     int news_item;
237     s_char what[512];
238     s_char *who = sublaunch ? (s_char *)"" : cname(bombown);
239
240     getsect(x, y, &sect);
241     if (wantflags == P_O && !nowantflags) {
242         att_name = "satellite";
243         def_name = "a-sat missile";
244         news_item = N_SAT_KILL;
245         if (sect.sct_own) {
246             mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
247                 sublaunch ? (s_char *)"someone" : cname(bombown), xyas(x,
248                                                                        y,
249                                                                        sect.
250                                                                        sct_own));
251         }
252     } else if (wantflags == P_N && nowantflags == P_O) {
253         att_name = "warhead";
254         def_name = "abm";
255         news_item = sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP;
256     } else {
257         att_name = "elephant";
258         def_name = "tomato";    /* heh -KHS */
259         news_item = N_NUKE_STOP;
260     }
261     sprintf(what, "%s %s", who, att_name);
262     irvlist = &foo;
263
264     /* get all hostile abms in range */
265     msl_sel(irvlist, x, y, bombown, wantflags, nowantflags, 0);
266     intlist = &intfoo;
267     emp_initque(intlist);
268     /* First choose interceptors belonging to the target sector */
269     /* only allow two defense missiles per missile attack */
270     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
271         next = qp->q_forw;
272         ip = (struct plist *)qp;
273         pp = &ip->plane;
274         if (pp->pln_own != sect.sct_own)
275             continue;
276         pcp = ip->pcp;
277         if (mission_pln_equip(ip, 0, 0, 'i') < 0) {
278             emp_remque(qp);
279             free((s_char *)qp);
280             continue;
281         }
282         /* got one interceptor, delete from irv_list and
283          * add to  int_list.
284          */
285         emp_remque(qp);
286         emp_insque(qp, intlist);
287         putplane(pp->pln_uid, pp);
288         icount++;
289     }
290     /* only allow two defense missiles per missile attack */
291     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
292         next = qp->q_forw;
293         ip = (struct plist *)qp;
294         pp = &ip->plane;
295         pcp = ip->pcp;
296         if (mission_pln_equip(ip, 0, 0, 'i') < 0) {
297             emp_remque(qp);
298             free((s_char *)qp);
299             continue;
300         }
301         /* got one interceptor, delete from irv_list and
302          * add to  int_list.
303          */
304         emp_remque(qp);
305         emp_insque(qp, intlist);
306         putplane(pp->pln_uid, pp);
307         icount++;
308     }
309     /* Now, clean out the queue */
310     while (!QEMPTY(irvlist)) {
311         qp = irvlist->q_forw;
312         emp_remque(qp);
313         free((s_char *)qp);
314     }
315     if (icount == 0) {
316         if (sect.sct_own != 0)
317             mpr(sect.sct_own, "No %ss launched to intercept.\n", def_name);
318         return (destroyed);
319     }
320
321     /* attempt to destroy incoming missile */
322
323     while (!QEMPTY(intlist)) {
324         qp = intlist->q_forw;
325         ip = (struct plist *)qp;
326         pp = &ip->plane;
327         pcp = ip->pcp;
328
329         mpr(bombown, "%s %s launched in defense!\n",
330             cname(pp->pln_own), def_name);
331         if (sect.sct_own == pp->pln_own) {
332             mpr(sect.sct_own, "%s launched to intercept %s %s!\n",
333                 def_name, who, att_name);
334         } else {
335             if (sect.sct_own)
336                 mpr(sect.sct_own,
337                     "%s launched an %s to intercept the %s %s!\n",
338                     cname(pp->pln_own), def_name, who, att_name);
339             mpr(pp->pln_own,
340                 "%s launched to intercept %s %s arcing towards %s territory!\n",
341                 def_name, who, att_name, cname(sect.sct_own));
342         }
343
344         if (!destroyed &&
345             msl_hit(pp, hardtarget, EF_PLANE, news_item, news_item, what,
346                     x, y, bombown)) {
347             mpr(bombown, "%s destroyed by %s %s!\n", att_name,
348                 cname(pp->pln_own), def_name);
349             if (sect.sct_own)
350                 mpr(sect.sct_own, "%s %s intercepted!\n", who, att_name);
351             if (sect.sct_own != pp->pln_own)
352                 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
353             destroyed = 1;
354         }
355         /* zap the missile */
356         makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
357         pp->pln_own = 0;
358         putplane(pp->pln_uid, pp);
359         emp_remque(qp);
360         free((s_char *)qp);
361         if (destroyed)
362             break;
363     }
364     /* Clean out what is left in the list */
365     while (!QEMPTY(intlist)) {
366         qp = intlist->q_forw;
367         emp_remque(qp);
368         free((s_char *)qp);
369     }
370     if (destroyed)
371         return (destroyed);
372     if (icount) {
373         mpr(bombown, "%s made it through %s defenses!\n", att_name,
374             def_name);
375         if (sect.sct_own)
376             mpr(sect.sct_own, "%s made it through %s defenses!\n",
377                 att_name, def_name);
378     }
379     return (destroyed);
380 }
381
382 /* Keep launching missiles on list until mindam damage has been done */
383 int
384 msl_launch_mindam(struct emp_qelem *list, coord x, coord y, int hardtarget,
385                   int type, int mindam, s_char *whatp, int victim,
386                   int mission)
387 {
388     struct emp_qelem *qp;
389     struct emp_qelem *next;
390     struct plist *plp;
391     int newdam, dam = 0;
392     int nukedam = 0;
393     int news_item;
394     int snews_item;
395
396     if (type == EF_SHIP) {
397         news_item = N_SHP_MISS;
398         snews_item = N_SHP_SMISS;
399     } else if (type == EF_LAND) {
400         news_item = N_LND_MISS;
401         snews_item = N_LND_SMISS;
402     } else {
403         news_item = N_SCT_MISS;
404         snews_item = N_SCT_SMISS;
405     }
406
407     for (qp = list->q_back; qp != list && dam < mindam && !nukedam;
408          qp = next) {
409         next = qp->q_back;
410         plp = (struct plist *)qp;
411
412         if (mission_pln_equip(plp, 0, 0, 'p') >= 0) {
413             if (msl_hit(&plp->plane,
414                         hardtarget, type, news_item, snews_item,
415                         whatp, x, y, victim)) {
416                 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
417                 if (nukedam) {
418                     if (mission == MI_INTERDICT && type == EF_SECTOR)
419                         dam += nukedam;
420                 } else
421                     dam += newdam;
422             } else {
423                 /* Missiles that miss have to hit somewhere! */
424                 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
425                 collateral_damage(x, y, newdam, 0);
426             }
427             makelost(EF_PLANE, plp->plane.pln_own, plp->plane.pln_uid,
428                      plp->plane.pln_x, plp->plane.pln_y);
429             plp->plane.pln_own = 0;
430             putplane(plp->plane.pln_uid, &plp->plane);
431             emp_remque(qp);
432             free(qp);
433         }
434     }
435     return dam;
436 }