]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mslsub.c
Cleanup #includes of (mostly a long time) unused header files.
[empserver] / src / lib / subs / mslsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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             if (pp->pln_nuketype != (s_char)-1 && opt_NUKEFAILDETONATE) {
107                 pp->pln_flags &= ~PLN_AIRBURST;
108                 detonate(pp, pp->pln_x, pp->pln_y);
109             } else {
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, 0);
121                         putsect(&sect);
122                     }
123                 }
124             }
125         }
126         return 0;
127     }
128
129     mpr(pp->pln_own, "\tSHWOOOOOSH!  Missile launched!\n");
130
131     if (pp->pln_nuketype != (s_char)-1)
132         mpr(pp->pln_own, "\tArming nuclear warheads...\n");
133
134     if (pcp->pl_flags & P_T)
135         mpr(victim, "Incoming %s missile sighted at %s...\n",
136             sublaunch ? "sub-launched" : cname(pp->pln_own),
137             xyas(x, y, victim));
138
139     if (opt_PINPOINTMISSILE == 0 ||
140         (pcp->pl_flags & P_T && !(pcp->pl_flags & P_MAR))) {
141         if (msl_intercept(x, y, pp->pln_own, pcp->pl_def,
142                           sublaunch, P_N, P_O)) {
143             return 0;
144         }
145     }
146     if (pcp->pl_flags & P_MAR) {
147         if (shp_missile_defense(x, y, pp->pln_own, pcp->pl_def)) {
148             return 0;
149         }
150     }
151
152     if (pp->pln_nuketype != (s_char)-1)
153         hitchance = 100;
154
155     mpr(pp->pln_own, "\t%d%% hitchance...", hitchance);
156     hit = (roll(100) <= hitchance);
157
158     mpr(pp->pln_own, hit ? "HIT!\n" : "miss\n");
159     if (pcp->pl_flags & P_T)
160         mpr(victim, "...Incoming %s missile %s\n",
161             sublaunch ? (s_char *)"" : cname(pp->pln_own),
162             hit ? "HIT!\n" : "missed\n");
163     if (hit && news_item) {
164         if (sublaunch)
165             nreport(victim, snews_item, 0, 1);
166         else
167             nreport(pp->pln_own, news_item, victim, 1);
168     }
169     return hit;
170 }
171
172 void
173 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
174         int wantflags, int nowantflags, int mission)
175 {
176     struct plchrstr *pcp;
177     struct plnstr plane;
178     struct plist *irv;
179     struct nstr_item ni;
180
181     emp_initque(list);
182     snxtitem_all(&ni, EF_PLANE);
183     while (nxtitem(&ni, (s_char *)&plane)) {
184         if (!plane.pln_own)
185             continue;
186
187         pcp = &plchr[(int)plane.pln_type];
188         if (!(pcp->pl_flags & P_M))
189             continue;
190         if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
191             continue;
192         if (nowantflags && pcp->pl_flags & nowantflags)
193             continue;
194         if (mission && plane.pln_mission != mission)
195             continue;
196         if (getrel(getnatp(plane.pln_own), victim) >= NEUTRAL)
197             continue;
198         /* missiles go one way, so we can use all the range */
199         if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
200             continue;
201         if (plane.pln_mobil <= (s_char)0)
202             continue;
203         if (plane.pln_effic < 100)
204             continue;
205         /* got a valid interceptor */
206         irv = (struct plist *)malloc(sizeof(*irv));
207         irv->state = P_OK;
208         irv->bombs = 0;
209         irv->misc = 0;
210         irv->pcp = &plchr[(int)plane.pln_type];
211         irv->plane = plane;
212         emp_insque(&irv->queue, list);
213     }
214 }
215
216 int
217 msl_intercept(coord x, coord y, natid bombown, int hardtarget,
218               int sublaunch, int wantflags, int nowantflags)
219 {
220     struct plnstr *pp;
221     struct plchrstr *pcp;
222     struct sctstr sect;
223     struct emp_qelem *irvlist;
224     struct emp_qelem foo;
225     struct emp_qelem *intlist;
226     struct emp_qelem intfoo;
227     struct emp_qelem *qp;
228     struct emp_qelem *next;
229     struct plist *ip;
230     int icount = 0;
231     short destroyed = 0;
232     s_char *att_name;
233     s_char *def_name;
234     int news_item;
235     s_char *who = sublaunch ? (s_char *)"" : cname(bombown);
236
237     getsect(x, y, &sect);
238     if (wantflags == P_O && !nowantflags) {
239         att_name = "satellite";
240         def_name = "a-sat missile";
241         news_item = N_SAT_KILL;
242         if (sect.sct_own) {
243             mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
244                 sublaunch ? (s_char *)"someone" : cname(bombown), xyas(x,
245                                                                        y,
246                                                                        sect.
247                                                                        sct_own));
248         }
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, 0, 0, 'i') < 0) {
274             emp_remque(qp);
275             free((s_char *)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, 0, 0, 'i') < 0) {
293             emp_remque(qp);
294             free((s_char *)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((s_char *)qp);
310     }
311     if (icount == 0) {
312         if (sect.sct_own != 0)
313             mpr(sect.sct_own, "No %ss launched to intercept.\n", def_name);
314         return (destroyed);
315     }
316
317     /* attempt to destroy incoming missile */
318
319     while (!QEMPTY(intlist)) {
320         qp = intlist->q_forw;
321         ip = (struct plist *)qp;
322         pp = &ip->plane;
323         pcp = ip->pcp;
324
325         mpr(bombown, "%s %s launched in defense!\n",
326             cname(pp->pln_own), def_name);
327         if (sect.sct_own == pp->pln_own) {
328             mpr(sect.sct_own, "%s launched to intercept %s %s!\n",
329                 def_name, who, att_name);
330         } else {
331             if (sect.sct_own)
332                 mpr(sect.sct_own,
333                     "%s launched an %s to intercept the %s %s!\n",
334                     cname(pp->pln_own), def_name, who, att_name);
335             mpr(pp->pln_own,
336                 "%s launched to intercept %s %s arcing towards %s territory!\n",
337                 def_name, who, att_name, cname(sect.sct_own));
338         }
339
340         if (!destroyed &&
341             msl_hit(pp, hardtarget, EF_PLANE, news_item, news_item,
342                     att_name, x, y, bombown)) {
343             mpr(bombown, "%s destroyed by %s %s!\n", att_name,
344                 cname(pp->pln_own), def_name);
345             if (sect.sct_own)
346                 mpr(sect.sct_own, "%s %s intercepted!\n", who, att_name);
347             if (sect.sct_own != pp->pln_own)
348                 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
349             destroyed = 1;
350         }
351         /* zap the missile */
352         makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
353         pp->pln_own = 0;
354         putplane(pp->pln_uid, pp);
355         emp_remque(qp);
356         free((s_char *)qp);
357         if (destroyed)
358             break;
359     }
360     /* Clean out what is left in the list */
361     while (!QEMPTY(intlist)) {
362         qp = intlist->q_forw;
363         emp_remque(qp);
364         free((s_char *)qp);
365     }
366     if (destroyed)
367         return (destroyed);
368     if (icount) {
369         mpr(bombown, "%s made it through %s defenses!\n", att_name,
370             def_name);
371         if (sect.sct_own)
372             mpr(sect.sct_own, "%s made it through %s defenses!\n",
373                 att_name, def_name);
374     }
375     return (destroyed);
376 }
377
378 /* Keep launching missiles on list until mindam damage has been done */
379 int
380 msl_launch_mindam(struct emp_qelem *list, coord x, coord y, int hardtarget,
381                   int type, int mindam, s_char *whatp, int victim,
382                   int mission)
383 {
384     struct emp_qelem *qp;
385     struct emp_qelem *next;
386     struct plist *plp;
387     int newdam, dam = 0;
388     int nukedam = 0;
389     int news_item;
390     int snews_item;
391
392     if (type == EF_SHIP) {
393         news_item = N_SHP_MISS;
394         snews_item = N_SHP_SMISS;
395     } else if (type == EF_LAND) {
396         news_item = N_LND_MISS;
397         snews_item = N_LND_SMISS;
398     } else {
399         news_item = N_SCT_MISS;
400         snews_item = N_SCT_SMISS;
401     }
402
403     for (qp = list->q_back; qp != list && dam < mindam && !nukedam;
404          qp = next) {
405         next = qp->q_back;
406         plp = (struct plist *)qp;
407
408         if (mission_pln_equip(plp, 0, 0, 'p') >= 0) {
409             if (msl_hit(&plp->plane,
410                         hardtarget, type, news_item, snews_item,
411                         whatp, x, y, victim)) {
412                 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
413                 if (nukedam) {
414                     if (mission == MI_INTERDICT && type == EF_SECTOR)
415                         dam += nukedam;
416                 } else
417                     dam += newdam;
418             } else {
419                 /* Missiles that miss have to hit somewhere! */
420                 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
421                 collateral_damage(x, y, newdam, 0);
422             }
423             makelost(EF_PLANE, plp->plane.pln_own, plp->plane.pln_uid,
424                      plp->plane.pln_x, plp->plane.pln_y);
425             plp->plane.pln_own = 0;
426             putplane(plp->plane.pln_uid, &plp->plane);
427             emp_remque(qp);
428             free(qp);
429         }
430     }
431     return dam;
432 }