]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mslsub.c
Clean up superfluous includes
[empserver] / src / lib / subs / mslsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  mslsub.c: Missile subroutine stuff
28  *
29  *  Known contributors to this file:
30  *     Ken Stevens, 1995
31  *     Steve McClure, 1996-2000
32  *     Markus Armbruster, 2004-2010
33  */
34
35 #include <config.h>
36
37 #include <stdlib.h>
38 #include "file.h"
39 #include "misc.h"
40 #include "nat.h"
41 #include "news.h"
42 #include "nsc.h"
43 #include "nuke.h"
44 #include "optlist.h"
45 #include "plane.h"
46 #include "prototypes.h"
47 #include "queue.h"
48 #include "sect.h"
49 #include "ship.h"
50 #include "xy.h"
51
52 int
53 msl_launch(struct plnstr *pp, int type, char *what, coord x, coord y,
54            natid victim, int *sublaunchp)
55 {
56     struct shpstr ship;
57     struct nukstr nuke;
58     struct sctstr sect;
59     int sublaunch = 0;
60     char *base, *in_or_at, *from;
61     int dam;
62
63     mpr(pp->pln_own, "Preparing to launch %s at %s %s %s%s\n",
64         prplane(pp),
65         cname(victim),
66         what,
67         type != EF_SECTOR ? "in " : "",
68         xyas(x, y, pp->pln_own));
69     if (pp->pln_ship >= 0) {
70         getship(pp->pln_ship, &ship);
71         base = prship(&ship);
72         in_or_at = " in ";
73         if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
74             sublaunch = 1;
75             from = "in hatch";
76         } else
77             from = "on deck";
78     } else {
79         if (pp->pln_harden > 0) {
80             base = "missile silo";
81             in_or_at = " at ";
82             from = "in silo";
83         } else {
84             base = in_or_at = "";
85             from = "on launch pad";
86         }
87     }
88     mpr(pp->pln_own, "\tLaunching from %s%s%s\n",
89         base, in_or_at, xyas(pp->pln_x, pp->pln_y, pp->pln_own));
90
91     CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
92     pp->pln_flags |= PLN_LAUNCHED;
93     putplane(pp->pln_uid, pp);
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 (getnuke(nuk_on_plane(pp), &nuke)) {
99             mpr(pp->pln_own, "%s lost!\n", prnuke(&nuke));
100             nuke.nuk_effic = 0;
101             putnuke(nuke.nuk_uid, &nuke);
102         }
103         if (chance(0.33)) {
104             dam = pln_damage(pp, 'p', 0) / 2;
105             if (pp->pln_ship >= 0) {
106                 shipdamage(&ship, dam);
107                 putship(ship.shp_uid, &ship);
108             } else {
109                 mpr(pp->pln_own, "Explosion damages %s %d%%\n",
110                     xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
111                 getsect(pp->pln_x, pp->pln_y, &sect);
112                 sectdamage(&sect, dam);
113                 putsect(&sect);
114             }
115         }
116         return -1;
117     }
118
119     mpr(pp->pln_own, "\tSHWOOOOOSH!  Missile launched!\n");
120
121     if (type != EF_PLANE)
122         mpr(victim, "Incoming %s missile sighted at %s...\n",
123             sublaunch ? "sub-launched" : cname(pp->pln_own),
124             xyas(x, y, victim));
125
126     if (type == EF_SECTOR || type == EF_LAND) {
127         if (msl_abm_intercept(pp, x, y, sublaunch))
128             return -1;
129     }
130     if (type == EF_SHIP) {
131         if (shp_missile_defense(x, y, pp->pln_own, pln_def(pp))) {
132             return -1;
133         }
134     }
135
136     if (sublaunchp)
137         *sublaunchp = sublaunch;
138     return 0;
139 }
140
141 int
142 msl_hit(struct plnstr *pp, int hardtarget, int type,
143         int news_item, int snews_item, int sublaunch, natid victim)
144 {
145     int hitchance, hit;
146
147     if (nuk_on_plane(pp) >= 0) {
148         mpr(pp->pln_own, "\tArming nuclear warheads...\n");
149         hit = 1;
150     } else {
151         hitchance = pln_hitchance(pp, hardtarget, type);
152         hit = (roll(100) <= hitchance);
153         mpr(pp->pln_own, "\t%d%% hitchance...%s\n", hitchance,
154             hit ? "HIT!" : "miss");
155     }
156
157     if (type != EF_PLANE)
158         mpr(victim, "...Incoming %s missile %s\n",
159             sublaunch ? "" : cname(pp->pln_own),
160             hit ? "HIT!\n" : "missed\n");
161     if (hit && news_item) {
162         if (sublaunch)
163             nreport(victim, snews_item, 0, 1);
164         else
165             nreport(pp->pln_own, news_item, victim, 1);
166     }
167     return hit;
168 }
169
170 void
171 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
172         int wantflags, int nowantflags, int mission)
173 {
174     struct plchrstr *pcp;
175     struct plnstr plane;
176     struct plist *irv;
177     struct nstr_item ni;
178
179     emp_initque(list);
180     snxtitem_all(&ni, EF_PLANE);
181     while (nxtitem(&ni, &plane)) {
182         if (!plane.pln_own)
183             continue;
184
185         pcp = &plchr[(int)plane.pln_type];
186         if (!(pcp->pl_flags & P_M))
187             continue;
188         if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
189             continue;
190         if (nowantflags && pcp->pl_flags & nowantflags)
191             continue;
192         if (mission && plane.pln_mission != mission)
193             continue;
194         if (mission &&
195             plane.pln_radius < mapdist(x, y, plane.pln_opx, plane.pln_opy))
196             continue;
197         if (relations_with(plane.pln_own, victim) >= NEUTRAL)
198             continue;
199         /* missiles go one way, so we can use all the range */
200         if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
201             continue;
202         if (plane.pln_mobil <= 0)
203             continue;
204         if (plane.pln_effic < 100)
205             continue;
206         if (opt_MARKET) {
207             if (ontradingblock(EF_PLANE, &plane))
208                 continue;
209         }
210         if (!pln_airbase_ok(&plane, 1, 0))
211             continue;
212         /* got a valid interceptor */
213         irv = malloc(sizeof(*irv));
214         irv->load = 0;
215         irv->pcp = &plchr[(int)plane.pln_type];
216         irv->plane = plane;
217         emp_insque(&irv->queue, list);
218     }
219 }
220
221 static int
222 msl_intercept(struct plnstr *msl, struct sctstr *sp, int sublaunch,
223               struct emp_qelem *irvlist, char *att_name, char *def_name,
224               int news_item)
225 {
226     struct plnstr *pp;
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;
234     char *who = sublaunch ? "" : cname(msl->pln_own);
235
236     intlist = &intfoo;
237     emp_initque(intlist);
238     /* First choose interceptors belonging to the target sector */
239     /* only allow two defense missiles per missile attack */
240     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
241         next = qp->q_forw;
242         ip = (struct plist *)qp;
243         pp = &ip->plane;
244         if (pp->pln_own != sp->sct_own)
245             continue;
246         if (mission_pln_equip(ip, NULL, 'i') < 0) {
247             emp_remque(qp);
248             free(qp);
249             continue;
250         }
251         /* got one interceptor, delete from irv_list and
252          * add to  int_list.
253          */
254         emp_remque(qp);
255         emp_insque(qp, intlist);
256         putplane(pp->pln_uid, pp);
257         icount++;
258     }
259     /* only allow two defense missiles per missile attack */
260     for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
261         next = qp->q_forw;
262         ip = (struct plist *)qp;
263         pp = &ip->plane;
264         if (mission_pln_equip(ip, NULL, 'i') < 0) {
265             emp_remque(qp);
266             free(qp);
267             continue;
268         }
269         /* got one interceptor, delete from irv_list and
270          * add to  int_list.
271          */
272         emp_remque(qp);
273         emp_insque(qp, intlist);
274         putplane(pp->pln_uid, pp);
275         icount++;
276     }
277     /* Now, clean out the queue */
278     while (!QEMPTY(irvlist)) {
279         qp = irvlist->q_forw;
280         emp_remque(qp);
281         free(qp);
282     }
283     if (icount == 0) {
284         mpr(sp->sct_own, "No %ss launched to intercept.\n", def_name);
285         return 0;
286     }
287
288     /* attempt to destroy incoming missile */
289
290     destroyed = 0;
291     while (!destroyed && !QEMPTY(intlist)) {
292         qp = intlist->q_forw;
293         ip = (struct plist *)qp;
294         pp = &ip->plane;
295
296         mpr(msl->pln_own, "%s %s launched in defense!\n",
297             cname(pp->pln_own), def_name);
298         if (sp->sct_own == pp->pln_own) {
299             mpr(sp->sct_own, "%s launched to intercept %s %s!\n",
300                 def_name, who, att_name);
301         } else {
302             mpr(sp->sct_own,
303                 "%s launched an %s to intercept the %s %s!\n",
304                 cname(pp->pln_own), def_name, who, att_name);
305             mpr(pp->pln_own,
306                 "%s launched to intercept %s %s arcing towards %s territory!\n",
307                 def_name, who, att_name, cname(sp->sct_own));
308         }
309
310         if (msl_launch(pp, EF_PLANE, att_name, sp->sct_x, sp->sct_y,
311                        msl->pln_own, NULL) >= 0
312             && msl_hit(pp, pln_def(msl), EF_PLANE, 0, 0, 0, msl->pln_own)) {
313             mpr(msl->pln_own, "%s destroyed by %s %s!\n",
314                 att_name, cname(pp->pln_own), def_name);
315             mpr(sp->sct_own, "%s %s intercepted!\n", who, att_name);
316             if (sp->sct_own != pp->pln_own)
317                 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
318             if (sublaunch)
319                 nreport(pp->pln_own, news_item, 0, 1);
320             else
321                 nreport(pp->pln_own, news_item, msl->pln_own, 1);
322             destroyed = 1;
323         }
324         /* zap the missile */
325         pp->pln_effic = 0;
326         putplane(pp->pln_uid, pp);
327         emp_remque(qp);
328         free(qp);
329     }
330     /* Clean out what is left in the list */
331     while (!QEMPTY(intlist)) {
332         qp = intlist->q_forw;
333         emp_remque(qp);
334         free(qp);
335     }
336     if (destroyed)
337         return 1;
338     if (icount) {
339         mpr(msl->pln_own, "%s made it through %s defenses!\n",
340             att_name, def_name);
341         mpr(sp->sct_own, "%s made it through %s defenses!\n",
342             att_name, def_name);
343     }
344     return 0;
345 }
346
347 int
348 msl_abm_intercept(struct plnstr *msl, coord x, coord y, int sublaunch)
349 {
350     struct sctstr sect;
351     struct emp_qelem irvlist;
352
353     getsect(x, y, &sect);
354     msl_sel(&irvlist, x, y, msl->pln_own, P_N, P_O, 0);
355     return msl_intercept(msl, &sect, sublaunch,
356                          &irvlist, "warhead", "abm",
357                          sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP);
358 }
359
360 int
361 msl_asat_intercept(struct plnstr *msl, coord x, coord y)
362 {
363     struct sctstr sect;
364     struct emp_qelem irvlist;
365
366     getsect(x, y, &sect);
367     mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
368         cname(msl->pln_own), xyas(x, y, sect.sct_own));
369     msl_sel(&irvlist, x, y, msl->pln_own, P_O, 0, 0);
370     return msl_intercept(msl, &sect, 0,
371                          &irvlist, "satellite", "a-sat missile",
372                          N_SAT_KILL);
373 }