]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mslsub.c
Get rid of msl_launch_mindam()
[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_hit(struct plnstr *pp, int hardtarget, int type, int news_item,
58         int snews_item, char *what, coord x, coord y, int victim)
59 {
60     int hit;
61     struct shpstr ship;
62     struct sctstr sect;
63     int sublaunch = 0;
64     struct plchrstr *pcp = plchr + pp->pln_type;
65     int hitchance;
66     char *from;
67     int dam;
68
69     mpr(pp->pln_own, "Preparing to launch %s at %s %s %s%s\n",
70         prplane(pp),
71         cname(victim),
72         what,
73         (type == EF_SHIP || type == EF_PLANE) ? "in " : "",
74         xyas(x, y, pp->pln_own));
75     mpr(pp->pln_own, "\tLaunching from ");
76     if (pp->pln_ship >= 0) {
77         getship(pp->pln_ship, &ship);
78         mpr(pp->pln_own, "%s in ", prship(&ship));
79         if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
80             sublaunch = 1;
81             from = "in hatch";
82         } else
83             from = "on deck";
84         mpr(pp->pln_own, "%s\n",
85             xyas(ship.shp_x, ship.shp_y, pp->pln_own));
86     } else {
87         if (pp->pln_harden > 0) {
88             mpr(pp->pln_own, "missile silo at ");
89             from = "in silo";
90         } else
91             from = "on launch pad";
92         mpr(pp->pln_own, "%s\n", xyas(pp->pln_x, pp->pln_y, pp->pln_own));
93     }
94
95     if (chance((0.05 + (100 - pp->pln_effic) / 100.0)
96                * (1 - techfact(pp->pln_tech, 1.0)))) {
97         mpr(pp->pln_own, "KABOOOOM!  Missile explodes %s!\n", from);
98         if (chance(0.33)) {
99             dam = pln_damage(pp, 'p', 1) / 2;
100             if (pp->pln_ship >= 0) {
101                 shipdamage(&ship, dam);
102                 putship(ship.shp_uid, &ship);
103             } else {
104                 pr("Explosion damages %s %d%%",
105                    xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
106                 getsect(pp->pln_x, pp->pln_y, &sect);
107                 sectdamage(&sect, dam);
108                 putsect(&sect);
109             }
110         }
111         return 0;
112     }
113
114     CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
115     pp->pln_flags |= PLN_LAUNCHED;
116     putplane(pp->pln_uid, pp);
117     mpr(pp->pln_own, "\tSHWOOOOOSH!  Missile launched!\n");
118
119     if (pcp->pl_flags & P_T)
120         mpr(victim, "Incoming %s missile sighted at %s...\n",
121             sublaunch ? "sub-launched" : cname(pp->pln_own),
122             xyas(x, y, victim));
123
124     if ((pcp->pl_flags & P_T && !(pcp->pl_flags & P_MAR))) {
125         if (msl_intercept(x, y, pp->pln_own, pcp->pl_def,
126                           sublaunch, P_N, P_O)) {
127             return 0;
128         }
129     }
130     if (pcp->pl_flags & P_MAR) {
131         if (shp_missile_defense(x, y, pp->pln_own, pcp->pl_def)) {
132             return 0;
133         }
134     }
135
136     if (nuk_on_plane(pp) >= 0) {
137         mpr(pp->pln_own, "\tArming nuclear warheads...\n");
138         hit = 1;
139     } else {
140         hitchance = pln_hitchance(pp, hardtarget, type);
141         hit = (roll(100) <= hitchance);
142         mpr(pp->pln_own, "\t%d%% hitchance...%s\n", hitchance,
143             hit ? "HIT!" : "miss");
144     }
145
146     if (pcp->pl_flags & P_T)
147         mpr(victim, "...Incoming %s missile %s\n",
148             sublaunch ? "" : cname(pp->pln_own),
149             hit ? "HIT!\n" : "missed\n");
150     if (hit && news_item) {
151         if (sublaunch)
152             nreport(victim, snews_item, 0, 1);
153         else
154             nreport(pp->pln_own, news_item, victim, 1);
155     }
156     return hit;
157 }
158
159 void
160 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
161         int wantflags, int nowantflags, int mission)
162 {
163     struct plchrstr *pcp;
164     struct plnstr plane;
165     struct plist *irv;
166     struct nstr_item ni;
167
168     emp_initque(list);
169     snxtitem_all(&ni, EF_PLANE);
170     while (nxtitem(&ni, &plane)) {
171         if (!plane.pln_own)
172             continue;
173
174         pcp = &plchr[(int)plane.pln_type];
175         if (!(pcp->pl_flags & P_M))
176             continue;
177         if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
178             continue;
179         if (nowantflags && pcp->pl_flags & nowantflags)
180             continue;
181         if (mission && plane.pln_mission != mission)
182             continue;
183         if (mission &&
184             plane.pln_radius < mapdist(x, y, plane.pln_opx, plane.pln_opy))
185             continue;
186         if (getrel(getnatp(plane.pln_own), victim) >= NEUTRAL)
187             continue;
188         /* missiles go one way, so we can use all the range */
189         if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
190             continue;
191         if (plane.pln_mobil <= 0)
192             continue;
193         if (plane.pln_effic < 100)
194             continue;
195         if (!pln_airbase_ok(&plane, 1, 0))
196             continue;
197         /* got a valid interceptor */
198         irv = malloc(sizeof(*irv));
199         irv->load = 0;
200         irv->pcp = &plchr[(int)plane.pln_type];
201         irv->plane = plane;
202         emp_insque(&irv->queue, list);
203     }
204 }
205
206 int
207 msl_intercept(coord x, coord y, natid bombown, int hardtarget,
208               int sublaunch, int wantflags, int nowantflags)
209 {
210     struct plnstr *pp;
211     struct plchrstr *pcp;
212     struct sctstr sect;
213     struct emp_qelem *irvlist;
214     struct emp_qelem foo;
215     struct emp_qelem *intlist;
216     struct emp_qelem intfoo;
217     struct emp_qelem *qp;
218     struct emp_qelem *next;
219     struct plist *ip;
220     int icount = 0;
221     short destroyed;
222     char *att_name;
223     char *def_name;
224     int news_item;
225     char *who = sublaunch ? "" : cname(bombown);
226
227     getsect(x, y, &sect);
228     if (wantflags == P_O && !nowantflags) {
229         att_name = "satellite";
230         def_name = "a-sat missile";
231         news_item = N_SAT_KILL;
232         CANT_HAPPEN(sublaunch);
233         mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
234             cname(bombown), xyas(x, y, sect.sct_own));
235     } else if (wantflags == P_N && nowantflags == P_O) {
236         att_name = "warhead";
237         def_name = "abm";
238         news_item = sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP;
239     } else {
240         att_name = "elephant";
241         def_name = "tomato";    /* heh -KHS */
242         news_item = N_NUKE_STOP;
243     }
244     irvlist = &foo;
245
246     /* get all hostile abms in range */
247     msl_sel(irvlist, x, y, bombown, wantflags, nowantflags, 0);
248     intlist = &intfoo;
249     emp_initque(intlist);
250     /* First choose interceptors belonging to the target sector */
251     /* only allow two defense missiles per missile attack */
252     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
253         next = qp->q_forw;
254         ip = (struct plist *)qp;
255         pp = &ip->plane;
256         if (pp->pln_own != sect.sct_own)
257             continue;
258         pcp = ip->pcp;
259         if (mission_pln_equip(ip, NULL, 'i') < 0) {
260             emp_remque(qp);
261             free(qp);
262             continue;
263         }
264         /* got one interceptor, delete from irv_list and
265          * add to  int_list.
266          */
267         emp_remque(qp);
268         emp_insque(qp, intlist);
269         putplane(pp->pln_uid, pp);
270         icount++;
271     }
272     /* only allow two defense missiles per missile attack */
273     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
274         next = qp->q_forw;
275         ip = (struct plist *)qp;
276         pp = &ip->plane;
277         pcp = ip->pcp;
278         if (mission_pln_equip(ip, NULL, 'i') < 0) {
279             emp_remque(qp);
280             free(qp);
281             continue;
282         }
283         /* got one interceptor, delete from irv_list and
284          * add to  int_list.
285          */
286         emp_remque(qp);
287         emp_insque(qp, intlist);
288         putplane(pp->pln_uid, pp);
289         icount++;
290     }
291     /* Now, clean out the queue */
292     while (!QEMPTY(irvlist)) {
293         qp = irvlist->q_forw;
294         emp_remque(qp);
295         free(qp);
296     }
297     if (icount == 0) {
298         mpr(sect.sct_own, "No %ss launched to intercept.\n", def_name);
299         return 0;
300     }
301
302     /* attempt to destroy incoming missile */
303
304     destroyed = 0;
305     while (!destroyed && !QEMPTY(intlist)) {
306         qp = intlist->q_forw;
307         ip = (struct plist *)qp;
308         pp = &ip->plane;
309         pcp = ip->pcp;
310
311         mpr(bombown, "%s %s launched in defense!\n",
312             cname(pp->pln_own), def_name);
313         if (sect.sct_own == pp->pln_own) {
314             mpr(sect.sct_own, "%s launched to intercept %s %s!\n",
315                 def_name, who, att_name);
316         } else {
317             mpr(sect.sct_own,
318                 "%s launched an %s to intercept the %s %s!\n",
319                 cname(pp->pln_own), def_name, who, att_name);
320             mpr(pp->pln_own,
321                 "%s launched to intercept %s %s arcing towards %s territory!\n",
322                 def_name, who, att_name, cname(sect.sct_own));
323         }
324
325         if (msl_hit(pp, hardtarget, EF_PLANE, news_item, news_item,
326                     att_name, x, y, bombown)) {
327             mpr(bombown, "%s destroyed by %s %s!\n",
328                 att_name, cname(pp->pln_own), def_name);
329             mpr(sect.sct_own, "%s %s intercepted!\n", who, att_name);
330             if (sect.sct_own != pp->pln_own)
331                 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
332             destroyed = 1;
333         }
334         /* zap the missile */
335         pp->pln_effic = 0;
336         putplane(pp->pln_uid, pp);
337         emp_remque(qp);
338         free(qp);
339     }
340     /* Clean out what is left in the list */
341     while (!QEMPTY(intlist)) {
342         qp = intlist->q_forw;
343         emp_remque(qp);
344         free(qp);
345     }
346     if (destroyed)
347         return 1;
348     if (icount) {
349         mpr(bombown, "%s made it through %s defenses!\n",
350             att_name, def_name);
351         mpr(sect.sct_own, "%s made it through %s defenses!\n",
352             att_name, def_name);
353     }
354     return 0;
355 }