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
50 #include "prototypes.h"
57 msl_launch(struct plnstr *pp, int type, char *what, coord x, coord y,
58 natid victim, int *sublaunchp)
64 char *base, *in_or_at, *from;
67 mpr(pp->pln_own, "Preparing to launch %s at %s %s %s%s\n",
71 type != EF_SECTOR ? "in " : "",
72 xyas(x, y, pp->pln_own));
73 if (pp->pln_ship >= 0) {
74 getship(pp->pln_ship, &ship);
77 if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
83 if (pp->pln_harden > 0) {
84 base = "missile silo";
89 from = "on launch pad";
92 mpr(pp->pln_own, "\tLaunching from %s%s%s\n",
93 base, in_or_at, xyas(pp->pln_x, pp->pln_y, pp->pln_own));
95 CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
96 pp->pln_flags |= PLN_LAUNCHED;
97 putplane(pp->pln_uid, pp);
99 if (chance((0.05 + (100 - pp->pln_effic) / 100.0)
100 * (1 - techfact(pp->pln_tech, 1.0)))) {
101 mpr(pp->pln_own, "KABOOOOM! Missile explodes %s!\n", from);
102 if (getnuke(nuk_on_plane(pp), &nuke)) {
103 mpr(pp->pln_own, "%s lost!\n", prnuke(&nuke));
105 putnuke(nuke.nuk_uid, &nuke);
108 dam = pln_damage(pp, 'p', 0) / 2;
109 if (pp->pln_ship >= 0) {
110 shipdamage(&ship, dam);
111 putship(ship.shp_uid, &ship);
113 mpr(pp->pln_own, "Explosion damages %s %d%%\n",
114 xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
115 getsect(pp->pln_x, pp->pln_y, §);
116 sectdamage(§, dam);
123 mpr(pp->pln_own, "\tSHWOOOOOSH! Missile launched!\n");
125 if (type != EF_PLANE)
126 mpr(victim, "Incoming %s missile sighted at %s...\n",
127 sublaunch ? "sub-launched" : cname(pp->pln_own),
130 if (type == EF_SECTOR || type == EF_LAND) {
131 if (msl_abm_intercept(pp, x, y, sublaunch))
134 if (type == EF_SHIP) {
135 if (shp_missile_defense(x, y, pp->pln_own, pln_def(pp))) {
141 *sublaunchp = sublaunch;
146 msl_hit(struct plnstr *pp, int hardtarget, int type,
147 int news_item, int snews_item, int sublaunch, natid victim)
151 if (nuk_on_plane(pp) >= 0) {
152 mpr(pp->pln_own, "\tArming nuclear warheads...\n");
155 hitchance = pln_hitchance(pp, hardtarget, type);
156 hit = (roll(100) <= hitchance);
157 mpr(pp->pln_own, "\t%d%% hitchance...%s\n", hitchance,
158 hit ? "HIT!" : "miss");
161 if (type != EF_PLANE)
162 mpr(victim, "...Incoming %s missile %s\n",
163 sublaunch ? "" : cname(pp->pln_own),
164 hit ? "HIT!\n" : "missed\n");
165 if (hit && news_item) {
167 nreport(victim, snews_item, 0, 1);
169 nreport(pp->pln_own, news_item, victim, 1);
175 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
176 int wantflags, int nowantflags, int mission)
178 struct plchrstr *pcp;
184 snxtitem_all(&ni, EF_PLANE);
185 while (nxtitem(&ni, &plane)) {
189 pcp = &plchr[(int)plane.pln_type];
190 if (!(pcp->pl_flags & P_M))
192 if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
194 if (nowantflags && pcp->pl_flags & nowantflags)
196 if (mission && plane.pln_mission != mission)
199 plane.pln_radius < mapdist(x, y, plane.pln_opx, plane.pln_opy))
201 if (relations_with(plane.pln_own, victim) >= NEUTRAL)
203 /* missiles go one way, so we can use all the range */
204 if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
206 if (plane.pln_mobil <= 0)
208 if (plane.pln_effic < 100)
211 if (ontradingblock(EF_PLANE, &plane))
214 if (!pln_airbase_ok(&plane, 1, 0))
216 /* got a valid interceptor */
217 irv = malloc(sizeof(*irv));
219 irv->pcp = &plchr[(int)plane.pln_type];
221 emp_insque(&irv->queue, list);
226 msl_intercept(struct plnstr *msl, struct sctstr *sp, int sublaunch,
227 struct emp_qelem *irvlist, char *att_name, char *def_name,
231 struct emp_qelem *intlist;
232 struct emp_qelem intfoo;
233 struct emp_qelem *qp;
234 struct emp_qelem *next;
238 char *who = sublaunch ? "" : cname(msl->pln_own);
241 emp_initque(intlist);
242 /* First choose interceptors belonging to the target sector */
243 /* only allow two defense missiles per missile attack */
244 for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
246 ip = (struct plist *)qp;
248 if (pp->pln_own != sp->sct_own)
250 if (mission_pln_equip(ip, NULL, 'i') < 0) {
255 /* got one interceptor, delete from irv_list and
259 emp_insque(qp, intlist);
260 putplane(pp->pln_uid, pp);
263 /* only allow two defense missiles per missile attack */
264 for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
266 ip = (struct plist *)qp;
268 if (mission_pln_equip(ip, NULL, 'i') < 0) {
273 /* got one interceptor, delete from irv_list and
277 emp_insque(qp, intlist);
278 putplane(pp->pln_uid, pp);
281 /* Now, clean out the queue */
282 while (!QEMPTY(irvlist)) {
283 qp = irvlist->q_forw;
288 mpr(sp->sct_own, "No %ss launched to intercept.\n", def_name);
292 /* attempt to destroy incoming missile */
295 while (!destroyed && !QEMPTY(intlist)) {
296 qp = intlist->q_forw;
297 ip = (struct plist *)qp;
300 mpr(msl->pln_own, "%s %s launched in defense!\n",
301 cname(pp->pln_own), def_name);
302 if (sp->sct_own == pp->pln_own) {
303 mpr(sp->sct_own, "%s launched to intercept %s %s!\n",
304 def_name, who, att_name);
307 "%s launched an %s to intercept the %s %s!\n",
308 cname(pp->pln_own), def_name, who, att_name);
310 "%s launched to intercept %s %s arcing towards %s territory!\n",
311 def_name, who, att_name, cname(sp->sct_own));
314 if (msl_launch(pp, EF_PLANE, att_name, sp->sct_x, sp->sct_y,
315 msl->pln_own, NULL) >= 0
316 && msl_hit(pp, pln_def(msl), EF_PLANE, 0, 0, 0, msl->pln_own)) {
317 mpr(msl->pln_own, "%s destroyed by %s %s!\n",
318 att_name, cname(pp->pln_own), def_name);
319 mpr(sp->sct_own, "%s %s intercepted!\n", who, att_name);
320 if (sp->sct_own != pp->pln_own)
321 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
323 nreport(pp->pln_own, news_item, 0, 1);
325 nreport(pp->pln_own, news_item, msl->pln_own, 1);
328 /* zap the missile */
330 putplane(pp->pln_uid, pp);
334 /* Clean out what is left in the list */
335 while (!QEMPTY(intlist)) {
336 qp = intlist->q_forw;
343 mpr(msl->pln_own, "%s made it through %s defenses!\n",
345 mpr(sp->sct_own, "%s made it through %s defenses!\n",
352 msl_abm_intercept(struct plnstr *msl, coord x, coord y, int sublaunch)
355 struct emp_qelem irvlist;
357 getsect(x, y, §);
358 msl_sel(&irvlist, x, y, msl->pln_own, P_N, P_O, 0);
359 return msl_intercept(msl, §, sublaunch,
360 &irvlist, "warhead", "abm",
361 sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP);
365 msl_asat_intercept(struct plnstr *msl, coord x, coord y)
368 struct emp_qelem irvlist;
370 getsect(x, y, §);
371 mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
372 cname(msl->pln_own), xyas(x, y, sect.sct_own));
373 msl_sel(&irvlist, x, y, msl->pln_own, P_O, 0, 0);
374 return msl_intercept(msl, §, 0,
375 &irvlist, "satellite", "a-sat missile",