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