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
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.
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.
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
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.
28 * mslsub.c: Missile subroutine stuff
30 * Known contributors to this file:
32 * Steve McClure, 1996-2000
33 * Markus Armbruster, 2004-2009
51 #include "prototypes.h"
58 msl_launch(struct plnstr *pp, int type, char *what, coord x, coord y,
59 natid victim, int *sublaunchp)
68 mpr(pp->pln_own, "Preparing to launch %s at %s %s %s%s\n",
72 type != EF_SECTOR ? "in " : "",
73 xyas(x, y, pp->pln_own));
74 mpr(pp->pln_own, "\tLaunching from ");
75 if (pp->pln_ship >= 0) {
76 getship(pp->pln_ship, &ship);
77 mpr(pp->pln_own, "%s in ", prship(&ship));
78 if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
83 mpr(pp->pln_own, "%s\n",
84 xyas(ship.shp_x, ship.shp_y, pp->pln_own));
86 if (pp->pln_harden > 0) {
87 mpr(pp->pln_own, "missile silo at ");
90 from = "on launch pad";
91 mpr(pp->pln_own, "%s\n", xyas(pp->pln_x, pp->pln_y, pp->pln_own));
94 if (chance((0.05 + (100 - pp->pln_effic) / 100.0)
95 * (1 - techfact(pp->pln_tech, 1.0)))) {
96 mpr(pp->pln_own, "KABOOOOM! Missile explodes %s!\n", from);
97 if (getnuke(nuk_on_plane(pp), &nuke)) {
98 pr("%s lost!\n", prnuke(&nuke));
100 putnuke(nuke.nuk_uid, &nuke);
103 dam = pln_damage(pp, 'p', 0) / 2;
104 if (pp->pln_ship >= 0) {
105 shipdamage(&ship, dam);
106 putship(ship.shp_uid, &ship);
108 pr("Explosion damages %s %d%%\n",
109 xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
110 getsect(pp->pln_x, pp->pln_y, §);
111 sectdamage(§, dam);
118 CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
119 pp->pln_flags |= PLN_LAUNCHED;
120 putplane(pp->pln_uid, pp);
121 mpr(pp->pln_own, "\tSHWOOOOOSH! Missile launched!\n");
123 if (type != EF_PLANE)
124 mpr(victim, "Incoming %s missile sighted at %s...\n",
125 sublaunch ? "sub-launched" : cname(pp->pln_own),
128 if (type == EF_SECTOR || type == EF_LAND) {
129 if (msl_abm_intercept(pp, x, y, sublaunch))
132 if (type == EF_SHIP) {
133 if (shp_missile_defense(x, y, pp->pln_own, pln_def(pp))) {
139 *sublaunchp = sublaunch;
144 msl_hit(struct plnstr *pp, int hardtarget, int type,
145 int news_item, int snews_item, int sublaunch, natid victim)
149 if (nuk_on_plane(pp) >= 0) {
150 mpr(pp->pln_own, "\tArming nuclear warheads...\n");
153 hitchance = pln_hitchance(pp, hardtarget, type);
154 hit = (roll(100) <= hitchance);
155 mpr(pp->pln_own, "\t%d%% hitchance...%s\n", hitchance,
156 hit ? "HIT!" : "miss");
159 if (type != EF_PLANE)
160 mpr(victim, "...Incoming %s missile %s\n",
161 sublaunch ? "" : cname(pp->pln_own),
162 hit ? "HIT!\n" : "missed\n");
163 if (hit && news_item) {
165 nreport(victim, snews_item, 0, 1);
167 nreport(pp->pln_own, news_item, victim, 1);
173 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
174 int wantflags, int nowantflags, int mission)
176 struct plchrstr *pcp;
182 snxtitem_all(&ni, EF_PLANE);
183 while (nxtitem(&ni, &plane)) {
187 pcp = &plchr[(int)plane.pln_type];
188 if (!(pcp->pl_flags & P_M))
190 if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
192 if (nowantflags && pcp->pl_flags & nowantflags)
194 if (mission && plane.pln_mission != mission)
197 plane.pln_radius < mapdist(x, y, plane.pln_opx, plane.pln_opy))
199 if (getrel(getnatp(plane.pln_own), victim) >= NEUTRAL)
201 /* missiles go one way, so we can use all the range */
202 if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
204 if (plane.pln_mobil <= 0)
206 if (plane.pln_effic < 100)
208 if (!pln_airbase_ok(&plane, 1, 0))
210 /* got a valid interceptor */
211 irv = malloc(sizeof(*irv));
213 irv->pcp = &plchr[(int)plane.pln_type];
215 emp_insque(&irv->queue, list);
220 msl_intercept(struct plnstr *msl, struct sctstr *sp, int sublaunch,
221 struct emp_qelem *irvlist, char *att_name, char *def_name,
225 struct emp_qelem *intlist;
226 struct emp_qelem intfoo;
227 struct emp_qelem *qp;
228 struct emp_qelem *next;
232 char *who = sublaunch ? "" : cname(msl->pln_own);
235 emp_initque(intlist);
236 /* First choose interceptors belonging to the target sector */
237 /* only allow two defense missiles per missile attack */
238 for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
240 ip = (struct plist *)qp;
242 if (pp->pln_own != sp->sct_own)
244 if (mission_pln_equip(ip, NULL, 'i') < 0) {
249 /* got one interceptor, delete from irv_list and
253 emp_insque(qp, intlist);
254 putplane(pp->pln_uid, pp);
257 /* only allow two defense missiles per missile attack */
258 for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
260 ip = (struct plist *)qp;
262 if (mission_pln_equip(ip, NULL, 'i') < 0) {
267 /* got one interceptor, delete from irv_list and
271 emp_insque(qp, intlist);
272 putplane(pp->pln_uid, pp);
275 /* Now, clean out the queue */
276 while (!QEMPTY(irvlist)) {
277 qp = irvlist->q_forw;
282 mpr(sp->sct_own, "No %ss launched to intercept.\n", def_name);
286 /* attempt to destroy incoming missile */
289 while (!destroyed && !QEMPTY(intlist)) {
290 qp = intlist->q_forw;
291 ip = (struct plist *)qp;
294 mpr(msl->pln_own, "%s %s launched in defense!\n",
295 cname(pp->pln_own), def_name);
296 if (sp->sct_own == pp->pln_own) {
297 mpr(sp->sct_own, "%s launched to intercept %s %s!\n",
298 def_name, who, att_name);
301 "%s launched an %s to intercept the %s %s!\n",
302 cname(pp->pln_own), def_name, who, att_name);
304 "%s launched to intercept %s %s arcing towards %s territory!\n",
305 def_name, who, att_name, cname(sp->sct_own));
308 if (msl_launch(pp, EF_PLANE, att_name, sp->sct_x, sp->sct_y,
309 msl->pln_own, NULL) >= 0
310 && msl_hit(pp, pln_def(msl), EF_PLANE, 0, 0, 0, msl->pln_own)) {
311 mpr(msl->pln_own, "%s destroyed by %s %s!\n",
312 att_name, cname(pp->pln_own), def_name);
313 mpr(sp->sct_own, "%s %s intercepted!\n", who, att_name);
314 if (sp->sct_own != pp->pln_own)
315 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
317 nreport(pp->pln_own, news_item, 0, 1);
319 nreport(pp->pln_own, news_item, msl->pln_own, 1);
322 /* zap the missile */
324 putplane(pp->pln_uid, pp);
328 /* Clean out what is left in the list */
329 while (!QEMPTY(intlist)) {
330 qp = intlist->q_forw;
337 mpr(msl->pln_own, "%s made it through %s defenses!\n",
339 mpr(sp->sct_own, "%s made it through %s defenses!\n",
346 msl_abm_intercept(struct plnstr *msl, coord x, coord y, int sublaunch)
349 struct emp_qelem irvlist;
351 getsect(x, y, §);
352 msl_sel(&irvlist, x, y, msl->pln_own, P_N, P_O, 0);
353 return msl_intercept(msl, §, sublaunch,
354 &irvlist, "warhead", "abm",
355 sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP);
359 msl_asat_intercept(struct plnstr *msl, coord x, coord y)
362 struct emp_qelem irvlist;
364 getsect(x, y, §);
365 mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
366 cname(msl->pln_own), xyas(x, y, sect.sct_own));
367 msl_sel(&irvlist, x, y, msl->pln_own, P_O, 0, 0);
368 return msl_intercept(msl, §, 0,
369 &irvlist, "satellite", "a-sat missile",