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)
65 char *base, *in_or_at, *from;
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 if (pp->pln_ship >= 0) {
75 getship(pp->pln_ship, &ship);
78 if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
84 if (pp->pln_harden > 0) {
85 base = "missile silo";
90 from = "on launch pad";
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));
96 CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
97 pp->pln_flags |= PLN_LAUNCHED;
98 putplane(pp->pln_uid, pp);
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));
106 putnuke(nuke.nuk_uid, &nuke);
109 dam = pln_damage(pp, 'p', 0) / 2;
110 if (pp->pln_ship >= 0) {
111 shipdamage(&ship, dam);
112 putship(ship.shp_uid, &ship);
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, §);
117 sectdamage(§, dam);
124 mpr(pp->pln_own, "\tSHWOOOOOSH! Missile launched!\n");
126 if (type != EF_PLANE)
127 mpr(victim, "Incoming %s missile sighted at %s...\n",
128 sublaunch ? "sub-launched" : cname(pp->pln_own),
131 if (type == EF_SECTOR || type == EF_LAND) {
132 if (msl_abm_intercept(pp, x, y, sublaunch))
135 if (type == EF_SHIP) {
136 if (shp_missile_defense(x, y, pp->pln_own, pln_def(pp))) {
142 *sublaunchp = sublaunch;
147 msl_hit(struct plnstr *pp, int hardtarget, int type,
148 int news_item, int snews_item, int sublaunch, natid victim)
152 if (nuk_on_plane(pp) >= 0) {
153 mpr(pp->pln_own, "\tArming nuclear warheads...\n");
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");
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) {
168 nreport(victim, snews_item, 0, 1);
170 nreport(pp->pln_own, news_item, victim, 1);
176 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
177 int wantflags, int nowantflags, int mission)
179 struct plchrstr *pcp;
185 snxtitem_all(&ni, EF_PLANE);
186 while (nxtitem(&ni, &plane)) {
190 pcp = &plchr[(int)plane.pln_type];
191 if (!(pcp->pl_flags & P_M))
193 if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
195 if (nowantflags && pcp->pl_flags & nowantflags)
197 if (mission && plane.pln_mission != mission)
200 plane.pln_radius < mapdist(x, y, plane.pln_opx, plane.pln_opy))
202 if (getrel(getnatp(plane.pln_own), victim) >= NEUTRAL)
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))
207 if (plane.pln_mobil <= 0)
209 if (plane.pln_effic < 100)
211 if (!pln_airbase_ok(&plane, 1, 0))
213 /* got a valid interceptor */
214 irv = malloc(sizeof(*irv));
216 irv->pcp = &plchr[(int)plane.pln_type];
218 emp_insque(&irv->queue, list);
223 msl_intercept(struct plnstr *msl, struct sctstr *sp, int sublaunch,
224 struct emp_qelem *irvlist, char *att_name, char *def_name,
228 struct emp_qelem *intlist;
229 struct emp_qelem intfoo;
230 struct emp_qelem *qp;
231 struct emp_qelem *next;
235 char *who = sublaunch ? "" : cname(msl->pln_own);
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) {
243 ip = (struct plist *)qp;
245 if (pp->pln_own != sp->sct_own)
247 if (mission_pln_equip(ip, NULL, 'i') < 0) {
252 /* got one interceptor, delete from irv_list and
256 emp_insque(qp, intlist);
257 putplane(pp->pln_uid, pp);
260 /* only allow two defense missiles per missile attack */
261 for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
263 ip = (struct plist *)qp;
265 if (mission_pln_equip(ip, NULL, 'i') < 0) {
270 /* got one interceptor, delete from irv_list and
274 emp_insque(qp, intlist);
275 putplane(pp->pln_uid, pp);
278 /* Now, clean out the queue */
279 while (!QEMPTY(irvlist)) {
280 qp = irvlist->q_forw;
285 mpr(sp->sct_own, "No %ss launched to intercept.\n", def_name);
289 /* attempt to destroy incoming missile */
292 while (!destroyed && !QEMPTY(intlist)) {
293 qp = intlist->q_forw;
294 ip = (struct plist *)qp;
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);
304 "%s launched an %s to intercept the %s %s!\n",
305 cname(pp->pln_own), def_name, who, att_name);
307 "%s launched to intercept %s %s arcing towards %s territory!\n",
308 def_name, who, att_name, cname(sp->sct_own));
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);
320 nreport(pp->pln_own, news_item, 0, 1);
322 nreport(pp->pln_own, news_item, msl->pln_own, 1);
325 /* zap the missile */
327 putplane(pp->pln_uid, pp);
331 /* Clean out what is left in the list */
332 while (!QEMPTY(intlist)) {
333 qp = intlist->q_forw;
340 mpr(msl->pln_own, "%s made it through %s defenses!\n",
342 mpr(sp->sct_own, "%s made it through %s defenses!\n",
349 msl_abm_intercept(struct plnstr *msl, coord x, coord y, int sublaunch)
352 struct emp_qelem irvlist;
354 getsect(x, y, §);
355 msl_sel(&irvlist, x, y, msl->pln_own, P_N, P_O, 0);
356 return msl_intercept(msl, §, sublaunch,
357 &irvlist, "warhead", "abm",
358 sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP);
362 msl_asat_intercept(struct plnstr *msl, coord x, coord y)
365 struct emp_qelem irvlist;
367 getsect(x, y, §);
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, §, 0,
372 &irvlist, "satellite", "a-sat missile",