2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
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.
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, see <http://www.gnu.org/licenses/>.
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.
27 * mslsub.c: Missile subroutine stuff
29 * Known contributors to this file:
31 * Steve McClure, 1996-2000
32 * Markus Armbruster, 2004-2012
48 #include "prototypes.h"
55 msl_launch(struct plnstr *pp, int type, char *what, coord x, coord y,
56 natid victim, int *sublaunchp)
61 char *base, *in_or_at, *from;
63 mpr(pp->pln_own, "Preparing to launch %s at %s %s %s%s\n",
67 type != EF_SECTOR ? "in " : "",
68 xyas(x, y, pp->pln_own));
69 if (pp->pln_ship >= 0) {
70 getship(pp->pln_ship, &ship);
73 if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
79 if (pp->pln_harden > 0) {
80 base = "missile silo";
85 from = "on launch pad";
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));
91 CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
92 pp->pln_flags |= PLN_LAUNCHED;
93 putplane(pp->pln_uid, pp);
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));
101 putnuke(nuke.nuk_uid, &nuke);
105 * Disabled for now, because it breaks callers that call
106 * msl_launch() for each member of a list of planes, created
107 * by msl_sel() or perform_mission(). Damage to the base can
108 * damage other planes. Any copies of them in the list become
109 * stale. When msl_launch() modifies and writes back such a
110 * stale copy, the damage gets wiped out, triggering a seqno
117 dam = pln_damage(pp, 'p', NULL) / 2;
118 if (pp->pln_ship >= 0) {
119 shipdamage(&ship, dam);
120 putship(ship.shp_uid, &ship);
122 mpr(pp->pln_own, "Explosion damages %s %d%%\n",
123 xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
124 getsect(pp->pln_x, pp->pln_y, §);
125 sectdamage(§, dam);
133 mpr(pp->pln_own, "\tSHWOOOOOSH! Missile launched!\n");
135 if (type != EF_PLANE)
136 mpr(victim, "Incoming %s missile sighted at %s...\n",
137 sublaunch ? "sub-launched" : cname(pp->pln_own),
140 if (type == EF_SECTOR || type == EF_LAND) {
141 if (msl_abm_intercept(pp, x, y, sublaunch))
144 if (type == EF_SHIP) {
145 if (shp_missile_defense(x, y, pp->pln_own, pln_def(pp))) {
151 *sublaunchp = sublaunch;
156 msl_hit(struct plnstr *pp, int hardtarget, int type,
157 int news_item, int snews_item, int sublaunch, natid victim)
161 if (nuk_on_plane(pp) >= 0) {
162 mpr(pp->pln_own, "\tArming nuclear warheads...\n");
165 hitchance = pln_hitchance(pp, hardtarget, type);
166 hit = pct_chance(hitchance);
167 mpr(pp->pln_own, "\t%d%% hitchance...%s\n", hitchance,
168 hit ? "HIT!" : "miss");
171 if (type != EF_PLANE)
172 mpr(victim, "...Incoming %s missile %s\n",
173 sublaunch ? "" : cname(pp->pln_own),
174 hit ? "HIT!\n" : "missed\n");
175 if (hit && news_item) {
177 nreport(victim, snews_item, 0, 1);
179 nreport(pp->pln_own, news_item, victim, 1);
185 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
186 int wantflags, int nowantflags, int mission)
188 struct plchrstr *pcp;
194 snxtitem_all(&ni, EF_PLANE);
195 while (nxtitem(&ni, &plane)) {
199 pcp = &plchr[(int)plane.pln_type];
200 if (!(pcp->pl_flags & P_M))
202 if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
204 if (nowantflags && pcp->pl_flags & nowantflags)
206 if (mission && plane.pln_mission != mission)
209 plane.pln_radius < mapdist(x, y, plane.pln_opx, plane.pln_opy))
211 if (relations_with(plane.pln_own, victim) >= NEUTRAL)
213 /* missiles go one way, so we can use all the range */
214 if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
216 if (plane.pln_mobil <= 0)
218 if (plane.pln_effic < 100)
221 if (ontradingblock(EF_PLANE, &plane))
224 if (!pln_airbase_ok(&plane, 1, 0))
226 /* got a valid interceptor */
227 irv = malloc(sizeof(*irv));
229 irv->pstage = PLG_HEALTHY;
230 irv->pcp = &plchr[(int)plane.pln_type];
232 emp_insque(&irv->queue, list);
237 msl_intercept(struct plnstr *msl, struct sctstr *sp, int sublaunch,
238 struct emp_qelem *irvlist, char *att_name, char *def_name,
242 struct emp_qelem *intlist;
243 struct emp_qelem intfoo;
244 struct emp_qelem *qp;
245 struct emp_qelem *next;
249 char *who = sublaunch ? "" : cname(msl->pln_own);
252 emp_initque(intlist);
253 /* First choose interceptors belonging to the target sector */
254 /* only allow two defense missiles per missile attack */
255 for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
257 ip = (struct plist *)qp;
259 if (pp->pln_own != sp->sct_own)
261 if (mission_pln_equip(ip, NULL, 'i') < 0) {
266 /* got one interceptor, delete from irv_list and
270 emp_insque(qp, intlist);
271 putplane(pp->pln_uid, pp);
274 /* only allow two defense missiles per missile attack */
275 for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
277 ip = (struct plist *)qp;
279 if (mission_pln_equip(ip, NULL, 'i') < 0) {
284 /* got one interceptor, delete from irv_list and
288 emp_insque(qp, intlist);
289 putplane(pp->pln_uid, pp);
292 /* Now, clean out the queue */
293 while (!QEMPTY(irvlist)) {
294 qp = irvlist->q_forw;
299 mpr(sp->sct_own, "No %ss launched to intercept.\n", def_name);
303 /* attempt to destroy incoming missile */
306 while (!destroyed && !QEMPTY(intlist)) {
307 qp = intlist->q_forw;
308 ip = (struct plist *)qp;
311 mpr(msl->pln_own, "%s %s launched in defense!\n",
312 cname(pp->pln_own), def_name);
313 if (sp->sct_own == pp->pln_own) {
314 mpr(sp->sct_own, "%s launched to intercept %s %s!\n",
315 def_name, who, att_name);
318 "%s launched an %s to intercept the %s %s!\n",
319 cname(pp->pln_own), def_name, who, att_name);
321 "%s launched to intercept %s %s arcing towards %s territory!\n",
322 def_name, who, att_name, cname(sp->sct_own));
325 if (msl_launch(pp, EF_PLANE, att_name, sp->sct_x, sp->sct_y,
326 msl->pln_own, NULL) >= 0
327 && msl_hit(pp, pln_def(msl), EF_PLANE, 0, 0, 0, msl->pln_own)) {
328 mpr(msl->pln_own, "%s destroyed by %s %s!\n",
329 att_name, cname(pp->pln_own), def_name);
330 mpr(sp->sct_own, "%s %s intercepted!\n", who, att_name);
331 if (sp->sct_own != pp->pln_own)
332 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
334 nreport(pp->pln_own, news_item, 0, 1);
336 nreport(pp->pln_own, news_item, msl->pln_own, 1);
339 /* zap the missile */
341 putplane(pp->pln_uid, pp);
345 /* Clean out what is left in the list */
346 while (!QEMPTY(intlist)) {
347 qp = intlist->q_forw;
354 mpr(msl->pln_own, "%s made it through %s defenses!\n",
356 mpr(sp->sct_own, "%s made it through %s defenses!\n",
363 msl_abm_intercept(struct plnstr *msl, coord x, coord y, int sublaunch)
366 struct emp_qelem irvlist;
368 getsect(x, y, §);
369 msl_sel(&irvlist, x, y, msl->pln_own, P_N, P_O, 0);
370 return msl_intercept(msl, §, sublaunch,
371 &irvlist, "warhead", "abm",
372 sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP);
376 msl_asat_intercept(struct plnstr *msl, coord x, coord y)
379 struct emp_qelem irvlist;
381 getsect(x, y, §);
382 mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
383 cname(msl->pln_own), xyas(x, y, sect.sct_own));
384 msl_sel(&irvlist, x, y, msl->pln_own, P_O, 0, 0);
385 return msl_intercept(msl, §, 0,
386 &irvlist, "satellite", "a-sat missile",