]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mslsub.c
Update copyright notice
[empserver] / src / lib / subs / mslsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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  *     Markus Armbruster, 2004-2009
34  */
35
36 #include <config.h>
37
38 #include <stdlib.h>
39 #include "file.h"
40 #include "land.h"
41 #include "misc.h"
42 #include "mission.h"
43 #include "nat.h"
44 #include "news.h"
45 #include "nsc.h"
46 #include "nuke.h"
47 #include "optlist.h"
48 #include "path.h"
49 #include "plane.h"
50 #include "player.h"
51 #include "prototypes.h"
52 #include "queue.h"
53 #include "sect.h"
54 #include "ship.h"
55 #include "xy.h"
56
57 int
58 msl_launch(struct plnstr *pp, int type, char *what, coord x, coord y,
59            natid victim, int *sublaunchp)
60 {
61     struct shpstr ship;
62     struct sctstr sect;
63     int sublaunch = 0;
64     char *from;
65     int dam;
66
67     mpr(pp->pln_own, "Preparing to launch %s at %s %s %s%s\n",
68         prplane(pp),
69         cname(victim),
70         what,
71         type != EF_SECTOR ? "in " : "",
72         xyas(x, y, pp->pln_own));
73     mpr(pp->pln_own, "\tLaunching from ");
74     if (pp->pln_ship >= 0) {
75         getship(pp->pln_ship, &ship);
76         mpr(pp->pln_own, "%s in ", prship(&ship));
77         if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
78             sublaunch = 1;
79             from = "in hatch";
80         } else
81             from = "on deck";
82         mpr(pp->pln_own, "%s\n",
83             xyas(ship.shp_x, ship.shp_y, pp->pln_own));
84     } else {
85         if (pp->pln_harden > 0) {
86             mpr(pp->pln_own, "missile silo at ");
87             from = "in silo";
88         } else
89             from = "on launch pad";
90         mpr(pp->pln_own, "%s\n", xyas(pp->pln_x, pp->pln_y, pp->pln_own));
91     }
92
93     if (chance((0.05 + (100 - pp->pln_effic) / 100.0)
94                * (1 - techfact(pp->pln_tech, 1.0)))) {
95         mpr(pp->pln_own, "KABOOOOM!  Missile explodes %s!\n", from);
96         if (chance(0.33)) {
97             dam = pln_damage(pp, 'p', 1) / 2;
98             if (pp->pln_ship >= 0) {
99                 shipdamage(&ship, dam);
100                 putship(ship.shp_uid, &ship);
101             } else {
102                 pr("Explosion damages %s %d%%\n",
103                    xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
104                 getsect(pp->pln_x, pp->pln_y, &sect);
105                 sectdamage(&sect, dam);
106                 putsect(&sect);
107             }
108         }
109         return -1;
110     }
111
112     CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
113     pp->pln_flags |= PLN_LAUNCHED;
114     putplane(pp->pln_uid, pp);
115     mpr(pp->pln_own, "\tSHWOOOOOSH!  Missile launched!\n");
116
117     if (type != EF_PLANE)
118         mpr(victim, "Incoming %s missile sighted at %s...\n",
119             sublaunch ? "sub-launched" : cname(pp->pln_own),
120             xyas(x, y, victim));
121
122     if (type == EF_SECTOR || type == EF_LAND) {
123         if (msl_abm_intercept(pp, x, y, sublaunch))
124             return -1;
125     }
126     if (type == EF_SHIP) {
127         if (shp_missile_defense(x, y, pp->pln_own, pln_def(pp))) {
128             return -1;
129         }
130     }
131
132     if (sublaunchp)
133         *sublaunchp = sublaunch;
134     return 0;
135 }
136
137 int
138 msl_hit(struct plnstr *pp, int hardtarget, int type,
139         int news_item, int snews_item, int sublaunch, natid victim)
140 {
141     int hitchance, hit;
142
143     if (nuk_on_plane(pp) >= 0) {
144         mpr(pp->pln_own, "\tArming nuclear warheads...\n");
145         hit = 1;
146     } else {
147         hitchance = pln_hitchance(pp, hardtarget, type);
148         hit = (roll(100) <= hitchance);
149         mpr(pp->pln_own, "\t%d%% hitchance...%s\n", hitchance,
150             hit ? "HIT!" : "miss");
151     }
152
153     if (type != EF_PLANE)
154         mpr(victim, "...Incoming %s missile %s\n",
155             sublaunch ? "" : cname(pp->pln_own),
156             hit ? "HIT!\n" : "missed\n");
157     if (hit && news_item) {
158         if (sublaunch)
159             nreport(victim, snews_item, 0, 1);
160         else
161             nreport(pp->pln_own, news_item, victim, 1);
162     }
163     return hit;
164 }
165
166 void
167 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
168         int wantflags, int nowantflags, int mission)
169 {
170     struct plchrstr *pcp;
171     struct plnstr plane;
172     struct plist *irv;
173     struct nstr_item ni;
174
175     emp_initque(list);
176     snxtitem_all(&ni, EF_PLANE);
177     while (nxtitem(&ni, &plane)) {
178         if (!plane.pln_own)
179             continue;
180
181         pcp = &plchr[(int)plane.pln_type];
182         if (!(pcp->pl_flags & P_M))
183             continue;
184         if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
185             continue;
186         if (nowantflags && pcp->pl_flags & nowantflags)
187             continue;
188         if (mission && plane.pln_mission != mission)
189             continue;
190         if (mission &&
191             plane.pln_radius < mapdist(x, y, plane.pln_opx, plane.pln_opy))
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         if (!pln_airbase_ok(&plane, 1, 0))
203             continue;
204         /* got a valid interceptor */
205         irv = malloc(sizeof(*irv));
206         irv->load = 0;
207         irv->pcp = &plchr[(int)plane.pln_type];
208         irv->plane = plane;
209         emp_insque(&irv->queue, list);
210     }
211 }
212
213 static int
214 msl_intercept(struct plnstr *msl, struct sctstr *sp, int sublaunch,
215               struct emp_qelem *irvlist, char *att_name, char *def_name,
216               int news_item)
217 {
218     struct plnstr *pp;
219     struct plchrstr *pcp;
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;
227     char *who = sublaunch ? "" : cname(msl->pln_own);
228
229     intlist = &intfoo;
230     emp_initque(intlist);
231     /* First choose interceptors belonging to the target sector */
232     /* only allow two defense missiles per missile attack */
233     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
234         next = qp->q_forw;
235         ip = (struct plist *)qp;
236         pp = &ip->plane;
237         if (pp->pln_own != sp->sct_own)
238             continue;
239         pcp = ip->pcp;
240         if (mission_pln_equip(ip, NULL, 'i') < 0) {
241             emp_remque(qp);
242             free(qp);
243             continue;
244         }
245         /* got one interceptor, delete from irv_list and
246          * add to  int_list.
247          */
248         emp_remque(qp);
249         emp_insque(qp, intlist);
250         putplane(pp->pln_uid, pp);
251         icount++;
252     }
253     /* only allow two defense missiles per missile attack */
254     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
255         next = qp->q_forw;
256         ip = (struct plist *)qp;
257         pp = &ip->plane;
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     /* Now, clean out the queue */
273     while (!QEMPTY(irvlist)) {
274         qp = irvlist->q_forw;
275         emp_remque(qp);
276         free(qp);
277     }
278     if (icount == 0) {
279         mpr(sp->sct_own, "No %ss launched to intercept.\n", def_name);
280         return 0;
281     }
282
283     /* attempt to destroy incoming missile */
284
285     destroyed = 0;
286     while (!destroyed && !QEMPTY(intlist)) {
287         qp = intlist->q_forw;
288         ip = (struct plist *)qp;
289         pp = &ip->plane;
290         pcp = ip->pcp;
291
292         mpr(msl->pln_own, "%s %s launched in defense!\n",
293             cname(pp->pln_own), def_name);
294         if (sp->sct_own == pp->pln_own) {
295             mpr(sp->sct_own, "%s launched to intercept %s %s!\n",
296                 def_name, who, att_name);
297         } else {
298             mpr(sp->sct_own,
299                 "%s launched an %s to intercept the %s %s!\n",
300                 cname(pp->pln_own), def_name, who, att_name);
301             mpr(pp->pln_own,
302                 "%s launched to intercept %s %s arcing towards %s territory!\n",
303                 def_name, who, att_name, cname(sp->sct_own));
304         }
305
306         if (msl_launch(pp, EF_PLANE, att_name, sp->sct_x, sp->sct_y,
307                        msl->pln_own, NULL) >= 0
308             && msl_hit(pp, pln_def(msl), EF_PLANE, 0, 0, 0, msl->pln_own)) {
309             mpr(msl->pln_own, "%s destroyed by %s %s!\n",
310                 att_name, cname(pp->pln_own), def_name);
311             mpr(sp->sct_own, "%s %s intercepted!\n", who, att_name);
312             if (sp->sct_own != pp->pln_own)
313                 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
314             if (sublaunch)
315                 nreport(pp->pln_own, news_item, 0, 1);
316             else
317                 nreport(pp->pln_own, news_item, msl->pln_own, 1);
318             destroyed = 1;
319         }
320         /* zap the missile */
321         pp->pln_effic = 0;
322         putplane(pp->pln_uid, pp);
323         emp_remque(qp);
324         free(qp);
325     }
326     /* Clean out what is left in the list */
327     while (!QEMPTY(intlist)) {
328         qp = intlist->q_forw;
329         emp_remque(qp);
330         free(qp);
331     }
332     if (destroyed)
333         return 1;
334     if (icount) {
335         mpr(msl->pln_own, "%s made it through %s defenses!\n",
336             att_name, def_name);
337         mpr(sp->sct_own, "%s made it through %s defenses!\n",
338             att_name, def_name);
339     }
340     return 0;
341 }
342
343 int
344 msl_abm_intercept(struct plnstr *msl, coord x, coord y, int sublaunch)
345 {
346     struct sctstr sect;
347     struct emp_qelem irvlist;
348
349     getsect(x, y, &sect);
350     msl_sel(&irvlist, x, y, msl->pln_own, P_N, P_O, 0);
351     return msl_intercept(msl, &sect, sublaunch,
352                          &irvlist, "warhead", "abm",
353                          sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP);
354 }
355
356 int
357 msl_asat_intercept(struct plnstr *msl, coord x, coord y)
358 {
359     struct sctstr sect;
360     struct emp_qelem irvlist;
361
362     getsect(x, y, &sect);
363     mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
364         cname(msl->pln_own), xyas(x, y, sect.sct_own));
365     msl_sel(&irvlist, x, y, msl->pln_own, P_O, 0, 0);
366     return msl_intercept(msl, &sect, 0,
367                          &irvlist, "satellite", "a-sat missile",
368                          N_SAT_KILL);
369 }