2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, 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
50 #include "prototypes.h"
57 msl_equip(struct plnstr *pp, char mission)
61 memset(&pl, 0, sizeof(struct plist));
62 pl.pcp = plchr + pp->pln_type;
64 return mission_pln_equip(&pl, NULL, mission);
68 msl_hit(struct plnstr *pp, int hardtarget, int type, int news_item,
69 int snews_item, char *what, coord x, coord y, int victim)
75 struct plchrstr *pcp = plchr + pp->pln_type;
80 mpr(pp->pln_own, "Preparing to launch %s at %s %s %s%s\n",
84 (type == EF_SHIP || type == EF_PLANE) ? "in " : "",
85 xyas(x, y, pp->pln_own));
86 mpr(pp->pln_own, "\tLaunching from ");
87 if (pp->pln_ship >= 0) {
88 getship(pp->pln_ship, &ship);
89 mpr(pp->pln_own, "%s in ", prship(&ship));
90 if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
95 mpr(pp->pln_own, "%s\n",
96 xyas(ship.shp_x, ship.shp_y, pp->pln_own));
98 if (pp->pln_harden > 0) {
99 mpr(pp->pln_own, "missile silo at ");
102 from = "on launch pad";
103 mpr(pp->pln_own, "%s\n", xyas(pp->pln_x, pp->pln_y, pp->pln_own));
106 if (chance((0.05 + (100 - pp->pln_effic) / 100.0)
107 * (1 - techfact(pp->pln_tech, 1.0)))) {
108 mpr(pp->pln_own, "KABOOOOM! Missile explodes %s!\n", from);
110 dam = pln_damage(pp, pp->pln_x, pp->pln_y,
111 'p', &dummyi, 1) / 2;
113 if (pp->pln_ship >= 0) {
114 shipdamage(&ship, dam);
115 putship(ship.shp_uid, &ship);
117 pr("Explosion damages %s %d%%",
118 xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
119 getsect(pp->pln_x, pp->pln_y, §);
120 sectdamage(§, dam);
128 CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
129 pp->pln_flags |= PLN_LAUNCHED;
130 putplane(pp->pln_uid, pp);
131 mpr(pp->pln_own, "\tSHWOOOOOSH! Missile launched!\n");
133 if (pcp->pl_flags & P_T)
134 mpr(victim, "Incoming %s missile sighted at %s...\n",
135 sublaunch ? "sub-launched" : cname(pp->pln_own),
138 if ((pcp->pl_flags & P_T && !(pcp->pl_flags & P_MAR))) {
139 if (msl_intercept(x, y, pp->pln_own, pcp->pl_def,
140 sublaunch, P_N, P_O)) {
144 if (pcp->pl_flags & P_MAR) {
145 if (shp_missile_defense(x, y, pp->pln_own, pcp->pl_def)) {
150 if (nuk_on_plane(pp) >= 0) {
151 mpr(pp->pln_own, "\tArming nuclear warheads...\n");
154 hitchance = pln_hitchance(pp, hardtarget, type);
155 hit = (roll(100) <= hitchance);
156 mpr(pp->pln_own, "\t%d%% hitchance...%s\n", hitchance,
157 hit ? "HIT!" : "miss");
160 if (pcp->pl_flags & P_T)
161 mpr(victim, "...Incoming %s missile %s\n",
162 sublaunch ? "" : cname(pp->pln_own),
163 hit ? "HIT!\n" : "missed\n");
164 if (hit && news_item) {
166 nreport(victim, snews_item, 0, 1);
168 nreport(pp->pln_own, news_item, victim, 1);
174 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
175 int wantflags, int nowantflags, int mission)
177 struct plchrstr *pcp;
183 snxtitem_all(&ni, EF_PLANE);
184 while (nxtitem(&ni, &plane)) {
188 pcp = &plchr[(int)plane.pln_type];
189 if (!(pcp->pl_flags & P_M))
191 if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
193 if (nowantflags && pcp->pl_flags & nowantflags)
195 if (mission && plane.pln_mission != mission)
198 plane.pln_radius < mapdist(x, y, plane.pln_opx, plane.pln_opy))
200 if (getrel(getnatp(plane.pln_own), victim) >= NEUTRAL)
202 /* missiles go one way, so we can use all the range */
203 if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
205 if (plane.pln_mobil <= 0)
207 if (plane.pln_effic < 100)
209 if (!pln_airbase_ok(&plane, 1, 0))
211 /* got a valid interceptor */
212 irv = malloc(sizeof(*irv));
215 irv->pcp = &plchr[(int)plane.pln_type];
217 emp_insque(&irv->queue, list);
222 msl_intercept(coord x, coord y, natid bombown, int hardtarget,
223 int sublaunch, int wantflags, int nowantflags)
226 struct plchrstr *pcp;
228 struct emp_qelem *irvlist;
229 struct emp_qelem foo;
230 struct emp_qelem *intlist;
231 struct emp_qelem intfoo;
232 struct emp_qelem *qp;
233 struct emp_qelem *next;
240 char *who = sublaunch ? "" : cname(bombown);
242 getsect(x, y, §);
243 if (wantflags == P_O && !nowantflags) {
244 att_name = "satellite";
245 def_name = "a-sat missile";
246 news_item = N_SAT_KILL;
247 mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
248 sublaunch ? "someone" : cname(bombown),
249 xyas(x, y, sect.sct_own));
250 } else if (wantflags == P_N && nowantflags == P_O) {
251 att_name = "warhead";
253 news_item = sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP;
255 att_name = "elephant";
256 def_name = "tomato"; /* heh -KHS */
257 news_item = N_NUKE_STOP;
261 /* get all hostile abms in range */
262 msl_sel(irvlist, x, y, bombown, wantflags, nowantflags, 0);
264 emp_initque(intlist);
265 /* First choose interceptors belonging to the target sector */
266 /* only allow two defense missiles per missile attack */
267 for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
269 ip = (struct plist *)qp;
271 if (pp->pln_own != sect.sct_own)
274 if (mission_pln_equip(ip, NULL, 'i') < 0) {
279 /* got one interceptor, delete from irv_list and
283 emp_insque(qp, intlist);
284 putplane(pp->pln_uid, pp);
287 /* only allow two defense missiles per missile attack */
288 for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
290 ip = (struct plist *)qp;
293 if (mission_pln_equip(ip, NULL, 'i') < 0) {
298 /* got one interceptor, delete from irv_list and
302 emp_insque(qp, intlist);
303 putplane(pp->pln_uid, pp);
306 /* Now, clean out the queue */
307 while (!QEMPTY(irvlist)) {
308 qp = irvlist->q_forw;
313 mpr(sect.sct_own, "No %ss launched to intercept.\n", def_name);
317 /* attempt to destroy incoming missile */
319 while (!QEMPTY(intlist)) {
320 qp = intlist->q_forw;
321 ip = (struct plist *)qp;
325 mpr(bombown, "%s %s launched in defense!\n",
326 cname(pp->pln_own), def_name);
327 if (sect.sct_own == pp->pln_own) {
328 mpr(sect.sct_own, "%s launched to intercept %s %s!\n",
329 def_name, who, att_name);
332 "%s launched an %s to intercept the %s %s!\n",
333 cname(pp->pln_own), def_name, who, att_name);
335 "%s launched to intercept %s %s arcing towards %s territory!\n",
336 def_name, who, att_name, cname(sect.sct_own));
340 msl_hit(pp, hardtarget, EF_PLANE, news_item, news_item,
341 att_name, x, y, bombown)) {
342 mpr(bombown, "%s destroyed by %s %s!\n",
343 att_name, cname(pp->pln_own), def_name);
344 mpr(sect.sct_own, "%s %s intercepted!\n", who, att_name);
345 if (sect.sct_own != pp->pln_own)
346 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
349 /* zap the missile */
351 putplane(pp->pln_uid, pp);
357 /* Clean out what is left in the list */
358 while (!QEMPTY(intlist)) {
359 qp = intlist->q_forw;
366 mpr(bombown, "%s made it through %s defenses!\n",
368 mpr(sect.sct_own, "%s made it through %s defenses!\n",
374 /* Keep launching missiles on list until mindam damage has been done */
376 msl_launch_mindam(struct emp_qelem *list, coord x, coord y, int hardtarget,
377 int type, int mindam, char *whatp, int victim,
380 struct emp_qelem *qp;
381 struct emp_qelem *next;
388 if (type == EF_SHIP) {
389 news_item = N_SHP_MISS;
390 snews_item = N_SHP_SMISS;
391 } else if (type == EF_LAND) {
392 news_item = N_LND_MISS;
393 snews_item = N_LND_SMISS;
395 news_item = N_SCT_MISS;
396 snews_item = N_SCT_SMISS;
399 for (qp = list->q_back; qp != list && dam < mindam && !nukedam;
402 plp = (struct plist *)qp;
404 if (mission_pln_equip(plp, NULL, 'p') >= 0) {
405 if (msl_hit(&plp->plane,
406 hardtarget, type, news_item, snews_item,
407 whatp, x, y, victim)) {
408 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
410 if (mission == MI_INTERDICT && type == EF_SECTOR)
416 * FIXME want collateral damage on miss (which can't
417 * happen for nuclear war heads), but we get here too when
418 * launch fails or missile is intercepted
421 /* Missiles that miss have to hit somewhere! */
422 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
423 collateral_damage(x, y, newdam);
426 plp->plane.pln_effic = 0;
427 putplane(plp->plane.pln_uid, &plp->plane);