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
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-2010
46 #include "prototypes.h"
53 msl_launch(struct plnstr *pp, int type, char *what, coord x, coord y,
54 natid victim, int *sublaunchp)
59 char *base, *in_or_at, *from;
61 mpr(pp->pln_own, "Preparing to launch %s at %s %s %s%s\n",
65 type != EF_SECTOR ? "in " : "",
66 xyas(x, y, pp->pln_own));
67 if (pp->pln_ship >= 0) {
68 getship(pp->pln_ship, &ship);
71 if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
77 if (pp->pln_harden > 0) {
78 base = "missile silo";
83 from = "on launch pad";
86 mpr(pp->pln_own, "\tLaunching from %s%s%s\n",
87 base, in_or_at, xyas(pp->pln_x, pp->pln_y, pp->pln_own));
89 CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
90 pp->pln_flags |= PLN_LAUNCHED;
91 putplane(pp->pln_uid, pp);
93 if (chance((0.05 + (100 - pp->pln_effic) / 100.0)
94 * (1 - techfact(pp->pln_tech, 1.0)))) {
95 mpr(pp->pln_own, "KABOOOOM! Missile explodes %s!\n", from);
96 if (getnuke(nuk_on_plane(pp), &nuke)) {
97 mpr(pp->pln_own, "%s lost!\n", prnuke(&nuke));
99 putnuke(nuke.nuk_uid, &nuke);
103 * Disabled for now, because it breaks callers that call
104 * msl_launch() for each member of a list of planes, created
105 * by msl_sel() or perform_mission(). Damage to the base can
106 * damage other planes. Any copies of them in the list become
107 * stale. When msl_launch() modifies and writes back such a
108 * stale copy, the damage gets wiped out, triggering a seqno
115 dam = pln_damage(pp, 'p', 0) / 2;
116 if (pp->pln_ship >= 0) {
117 shipdamage(&ship, dam);
118 putship(ship.shp_uid, &ship);
120 mpr(pp->pln_own, "Explosion damages %s %d%%\n",
121 xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
122 getsect(pp->pln_x, pp->pln_y, §);
123 sectdamage(§, dam);
131 mpr(pp->pln_own, "\tSHWOOOOOSH! Missile launched!\n");
133 if (type != EF_PLANE)
134 mpr(victim, "Incoming %s missile sighted at %s...\n",
135 sublaunch ? "sub-launched" : cname(pp->pln_own),
138 if (type == EF_SECTOR || type == EF_LAND) {
139 if (msl_abm_intercept(pp, x, y, sublaunch))
142 if (type == EF_SHIP) {
143 if (shp_missile_defense(x, y, pp->pln_own, pln_def(pp))) {
149 *sublaunchp = sublaunch;
154 msl_hit(struct plnstr *pp, int hardtarget, int type,
155 int news_item, int snews_item, int sublaunch, natid victim)
159 if (nuk_on_plane(pp) >= 0) {
160 mpr(pp->pln_own, "\tArming nuclear warheads...\n");
163 hitchance = pln_hitchance(pp, hardtarget, type);
164 hit = (roll(100) <= hitchance);
165 mpr(pp->pln_own, "\t%d%% hitchance...%s\n", hitchance,
166 hit ? "HIT!" : "miss");
169 if (type != EF_PLANE)
170 mpr(victim, "...Incoming %s missile %s\n",
171 sublaunch ? "" : cname(pp->pln_own),
172 hit ? "HIT!\n" : "missed\n");
173 if (hit && news_item) {
175 nreport(victim, snews_item, 0, 1);
177 nreport(pp->pln_own, news_item, victim, 1);
183 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
184 int wantflags, int nowantflags, int mission)
186 struct plchrstr *pcp;
192 snxtitem_all(&ni, EF_PLANE);
193 while (nxtitem(&ni, &plane)) {
197 pcp = &plchr[(int)plane.pln_type];
198 if (!(pcp->pl_flags & P_M))
200 if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
202 if (nowantflags && pcp->pl_flags & nowantflags)
204 if (mission && plane.pln_mission != mission)
207 plane.pln_radius < mapdist(x, y, plane.pln_opx, plane.pln_opy))
209 if (relations_with(plane.pln_own, victim) >= NEUTRAL)
211 /* missiles go one way, so we can use all the range */
212 if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
214 if (plane.pln_mobil <= 0)
216 if (plane.pln_effic < 100)
219 if (ontradingblock(EF_PLANE, &plane))
222 if (!pln_airbase_ok(&plane, 1, 0))
224 /* got a valid interceptor */
225 irv = malloc(sizeof(*irv));
227 irv->pcp = &plchr[(int)plane.pln_type];
229 emp_insque(&irv->queue, list);
234 msl_intercept(struct plnstr *msl, struct sctstr *sp, int sublaunch,
235 struct emp_qelem *irvlist, char *att_name, char *def_name,
239 struct emp_qelem *intlist;
240 struct emp_qelem intfoo;
241 struct emp_qelem *qp;
242 struct emp_qelem *next;
246 char *who = sublaunch ? "" : cname(msl->pln_own);
249 emp_initque(intlist);
250 /* First choose interceptors belonging to the target sector */
251 /* only allow two defense missiles per missile attack */
252 for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
254 ip = (struct plist *)qp;
256 if (pp->pln_own != sp->sct_own)
258 if (mission_pln_equip(ip, NULL, 'i') < 0) {
263 /* got one interceptor, delete from irv_list and
267 emp_insque(qp, intlist);
268 putplane(pp->pln_uid, pp);
271 /* only allow two defense missiles per missile attack */
272 for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
274 ip = (struct plist *)qp;
276 if (mission_pln_equip(ip, NULL, 'i') < 0) {
281 /* got one interceptor, delete from irv_list and
285 emp_insque(qp, intlist);
286 putplane(pp->pln_uid, pp);
289 /* Now, clean out the queue */
290 while (!QEMPTY(irvlist)) {
291 qp = irvlist->q_forw;
296 mpr(sp->sct_own, "No %ss launched to intercept.\n", def_name);
300 /* attempt to destroy incoming missile */
303 while (!destroyed && !QEMPTY(intlist)) {
304 qp = intlist->q_forw;
305 ip = (struct plist *)qp;
308 mpr(msl->pln_own, "%s %s launched in defense!\n",
309 cname(pp->pln_own), def_name);
310 if (sp->sct_own == pp->pln_own) {
311 mpr(sp->sct_own, "%s launched to intercept %s %s!\n",
312 def_name, who, att_name);
315 "%s launched an %s to intercept the %s %s!\n",
316 cname(pp->pln_own), def_name, who, att_name);
318 "%s launched to intercept %s %s arcing towards %s territory!\n",
319 def_name, who, att_name, cname(sp->sct_own));
322 if (msl_launch(pp, EF_PLANE, att_name, sp->sct_x, sp->sct_y,
323 msl->pln_own, NULL) >= 0
324 && msl_hit(pp, pln_def(msl), EF_PLANE, 0, 0, 0, msl->pln_own)) {
325 mpr(msl->pln_own, "%s destroyed by %s %s!\n",
326 att_name, cname(pp->pln_own), def_name);
327 mpr(sp->sct_own, "%s %s intercepted!\n", who, att_name);
328 if (sp->sct_own != pp->pln_own)
329 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
331 nreport(pp->pln_own, news_item, 0, 1);
333 nreport(pp->pln_own, news_item, msl->pln_own, 1);
336 /* zap the missile */
338 putplane(pp->pln_uid, pp);
342 /* Clean out what is left in the list */
343 while (!QEMPTY(intlist)) {
344 qp = intlist->q_forw;
351 mpr(msl->pln_own, "%s made it through %s defenses!\n",
353 mpr(sp->sct_own, "%s made it through %s defenses!\n",
360 msl_abm_intercept(struct plnstr *msl, coord x, coord y, int sublaunch)
363 struct emp_qelem irvlist;
365 getsect(x, y, §);
366 msl_sel(&irvlist, x, y, msl->pln_own, P_N, P_O, 0);
367 return msl_intercept(msl, §, sublaunch,
368 &irvlist, "warhead", "abm",
369 sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP);
373 msl_asat_intercept(struct plnstr *msl, coord x, coord y)
376 struct emp_qelem irvlist;
378 getsect(x, y, §);
379 mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
380 cname(msl->pln_own), xyas(x, y, sect.sct_own));
381 msl_sel(&irvlist, x, y, msl->pln_own, P_O, 0, 0);
382 return msl_intercept(msl, §, 0,
383 &irvlist, "satellite", "a-sat missile",