2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2021, 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-2021
47 #include "prototypes.h"
54 msl_launch(struct plnstr *pp, int type, char *what, coord x, coord y,
55 natid victim, int *sublaunchp)
60 char *base, *in_or_at, *from;
62 mpr(pp->pln_own, "Preparing to launch %s at %s %s %s%s\n",
66 type != EF_SECTOR ? "in " : "",
67 xyas(x, y, pp->pln_own));
68 if (pp->pln_ship >= 0) {
69 getship(pp->pln_ship, &ship);
72 if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
78 if (pp->pln_harden > 0) {
79 base = "missile silo";
84 from = "on launch pad";
87 mpr(pp->pln_own, "\tLaunching from %s%s%s\n",
88 base, in_or_at, xyas(pp->pln_x, pp->pln_y, pp->pln_own));
90 CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
91 pp->pln_flags |= PLN_LAUNCHED;
92 putplane(pp->pln_uid, pp);
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 mpr(pp->pln_own, "%s lost!\n", prnuke(&nuke));
100 putnuke(nuke.nuk_uid, &nuke);
104 * Disabled for now, because it breaks callers that call
105 * msl_launch() for each member of a list of planes, created
106 * by msl_sel() or perform_mission(). Damage to the base can
107 * damage other planes. Any copies of them in the list become
108 * stale. When msl_launch() modifies and writes back such a
109 * stale copy, the damage gets wiped out, triggering a seqno
116 dam = pln_damage(pp, 'p', NULL) / 2;
117 if (pp->pln_ship >= 0) {
118 shipdamage(&ship, dam);
119 putship(ship.shp_uid, &ship);
121 mpr(pp->pln_own, "Explosion damages %s %d%%\n",
122 xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
123 getsect(pp->pln_x, pp->pln_y, §);
124 sectdamage(§, dam);
132 mpr(pp->pln_own, "\tSHWOOOOOSH! Missile launched!\n");
134 if (type != EF_PLANE)
135 mpr(victim, "Incoming %s missile sighted at %s...\n",
136 sublaunch ? "sub-launched" : cname(pp->pln_own),
139 if (type == EF_SECTOR || type == EF_LAND) {
140 if (msl_abm_intercept(pp, x, y, sublaunch))
143 if (type == EF_SHIP) {
144 if (shp_missile_defense(x, y, pp->pln_own, pln_def(pp))) {
150 *sublaunchp = sublaunch;
155 msl_hit(struct plnstr *pp, int hardtarget, int type,
156 int news_item, int snews_item, int sublaunch, natid victim)
160 if (nuk_on_plane(pp) >= 0) {
161 mpr(pp->pln_own, "\tArming nuclear warheads...\n");
164 hitchance = pln_hitchance(pp, hardtarget, type);
165 hit = pct_chance(hitchance);
166 mpr(pp->pln_own, "\t%d%% hit chance...%s\n", hitchance,
167 hit ? "HIT!" : "miss");
170 if (type != EF_PLANE)
171 mpr(victim, "...Incoming %s missile %s\n",
172 sublaunch ? "" : cname(pp->pln_own),
173 hit ? "HIT!\n" : "missed\n");
174 if (hit && news_item) {
176 nreport(victim, snews_item, 0, 1);
178 nreport(pp->pln_own, news_item, victim, 1);
184 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
185 int wantflags, int nowantflags, int mission)
187 struct plchrstr *pcp;
193 snxtitem_all(&ni, EF_PLANE);
194 while (nxtitem(&ni, &plane)) {
198 pcp = &plchr[(int)plane.pln_type];
199 if (!(pcp->pl_flags & P_M))
201 if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
203 if (nowantflags && pcp->pl_flags & nowantflags)
205 if (mission && plane.pln_mission != mission)
208 plane.pln_radius < mapdist(x, y, plane.pln_opx, plane.pln_opy))
210 if (relations_with(plane.pln_own, victim) >= NEUTRAL)
212 /* missiles go one way, so we can use all the range */
213 if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
215 if (plane.pln_mobil <= 0)
217 if (plane.pln_effic < 100)
220 if (ontradingblock(EF_PLANE, &plane))
223 if (!pln_airbase_ok(&plane, 1, 0))
225 /* got a valid interceptor */
226 irv = malloc(sizeof(*irv));
228 irv->pstage = PLG_HEALTHY;
229 irv->pcp = &plchr[(int)plane.pln_type];
231 emp_insque(&irv->queue, list);
236 msl_intercept(struct plnstr *msl, struct sctstr *sp, int sublaunch,
237 struct emp_qelem *irvlist, char *att_name, char *def_name,
241 struct emp_qelem *intlist;
242 struct emp_qelem intfoo;
243 struct emp_qelem *qp;
244 struct emp_qelem *next;
248 char *who = sublaunch ? "" : cname(msl->pln_own);
251 emp_initque(intlist);
252 /* First choose interceptors belonging to the target sector */
253 /* only allow two defense missiles per missile attack */
254 for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
256 ip = (struct plist *)qp;
258 if (pp->pln_own != sp->sct_own)
260 if (mission_pln_equip(ip, NULL, 0) < 0) {
265 /* got one interceptor, delete from irv_list and
269 emp_insque(qp, intlist);
270 putplane(pp->pln_uid, pp);
273 /* only allow two defense missiles per missile attack */
274 for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
276 ip = (struct plist *)qp;
278 if (mission_pln_equip(ip, NULL, 0) < 0) {
283 /* got one interceptor, delete from irv_list and
287 emp_insque(qp, intlist);
288 putplane(pp->pln_uid, pp);
291 /* Now, clean out the queue */
292 while (!QEMPTY(irvlist)) {
293 qp = irvlist->q_forw;
298 mpr(sp->sct_own, "No %ss launched to intercept.\n", def_name);
302 /* attempt to destroy incoming missile */
305 while (!destroyed && !QEMPTY(intlist)) {
306 qp = intlist->q_forw;
307 ip = (struct plist *)qp;
310 mpr(msl->pln_own, "%s %s launched in defense!\n",
311 cname(pp->pln_own), def_name);
312 if (sp->sct_own == pp->pln_own) {
313 mpr(sp->sct_own, "%s launched to intercept %s %s!\n",
314 def_name, who, att_name);
317 "%s launched an %s to intercept the %s %s!\n",
318 cname(pp->pln_own), def_name, who, att_name);
320 "%s launched to intercept %s %s arcing towards %s territory!\n",
321 def_name, who, att_name, cname(sp->sct_own));
324 if (msl_launch(pp, EF_PLANE, att_name, sp->sct_x, sp->sct_y,
325 msl->pln_own, NULL) >= 0
326 && msl_hit(pp, pln_def(msl), EF_PLANE, 0, 0, 0, msl->pln_own)) {
327 mpr(msl->pln_own, "%s destroyed by %s %s!\n",
328 att_name, cname(pp->pln_own), def_name);
329 mpr(sp->sct_own, "%s %s intercepted!\n", who, att_name);
330 if (sp->sct_own != pp->pln_own)
331 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
333 nreport(pp->pln_own, news_item, 0, 1);
335 nreport(pp->pln_own, news_item, msl->pln_own, 1);
338 /* zap the missile */
340 putplane(pp->pln_uid, pp);
344 /* Clean out what is left in the list */
345 while (!QEMPTY(intlist)) {
346 qp = intlist->q_forw;
353 mpr(msl->pln_own, "%s made it through %s defenses!\n",
355 mpr(sp->sct_own, "%s made it through %s defenses!\n",
362 msl_abm_intercept(struct plnstr *msl, coord x, coord y, int sublaunch)
365 struct emp_qelem irvlist;
367 getsect(x, y, §);
368 msl_sel(&irvlist, x, y, msl->pln_own, P_N, P_O, 0);
369 return msl_intercept(msl, §, sublaunch,
370 &irvlist, "warhead", "abm",
371 sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP);
375 msl_asat_intercept(struct plnstr *msl, coord x, coord y)
378 struct emp_qelem irvlist;
380 getsect(x, y, §);
381 mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
382 cname(msl->pln_own), xyas(x, y, sect.sct_own));
383 msl_sel(&irvlist, x, y, msl->pln_own, P_O, 0, 0);
384 return msl_intercept(msl, §, 0,
385 &irvlist, "satellite", "a-sat missile",