2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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
49 #include "prototypes.h"
56 msl_equip(struct plnstr *pp, char mission)
60 memset(&pl, 0, sizeof(struct plist));
61 pl.pcp = plchr + pp->pln_type;
63 return mission_pln_equip(&pl, 0, 0, mission);
67 msl_hit(struct plnstr *pp, int hardtarget, int type, int news_item,
68 int snews_item, char *what, coord x, coord y, int victim)
74 struct plchrstr *pcp = plchr + pp->pln_type;
75 int hitchance = pln_hitchance(pp, hardtarget, type);
79 mpr(pp->pln_own, "Preparing to launch %s at %s %s %s%s\n",
83 (type == EF_SHIP || type == EF_PLANE) ? "in " : "",
84 xyas(x, y, pp->pln_own));
85 mpr(pp->pln_own, "\tLaunching from ");
86 if (pp->pln_ship >= 0) {
87 getship(pp->pln_ship, &ship);
88 mpr(pp->pln_own, "%s in ", prship(&ship));
89 if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
94 mpr(pp->pln_own, "%s\n",
95 xyas(ship.shp_x, ship.shp_y, pp->pln_own));
97 if (pp->pln_harden > 0) {
98 mpr(pp->pln_own, "missile silo at ");
101 from = "on launch pad";
102 mpr(pp->pln_own, "%s\n", xyas(pp->pln_x, pp->pln_y, pp->pln_own));
105 if (chance((0.05 + (100 - pp->pln_effic) / 100.0)
106 * (1 - techfact(pp->pln_tech, 1.0)))) {
107 mpr(pp->pln_own, "KABOOOOM! Missile explodes %s!\n", from);
109 dam = pln_damage(pp, pp->pln_x, pp->pln_y,
110 'p', &dummyi, 1) / 2;
112 if (pp->pln_ship >= 0) {
113 shipdamage(&ship, dam);
114 putship(ship.shp_uid, &ship);
116 pr("Explosion damages %s %d%%",
117 xyas(pp->pln_x, pp->pln_y, pp->pln_own), dam);
118 getsect(pp->pln_x, pp->pln_y, §);
119 sectdamage(§, dam);
127 mpr(pp->pln_own, "\tSHWOOOOOSH! Missile launched!\n");
129 if (pp->pln_nuketype != -1)
130 mpr(pp->pln_own, "\tArming nuclear warheads...\n");
132 if (pcp->pl_flags & P_T)
133 mpr(victim, "Incoming %s missile sighted at %s...\n",
134 sublaunch ? "sub-launched" : cname(pp->pln_own),
137 if (opt_PINPOINTMISSILE == 0 ||
138 (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 (pp->pln_nuketype != -1)
153 mpr(pp->pln_own, "\t%d%% hitchance...", hitchance);
154 hit = (roll(100) <= hitchance);
156 mpr(pp->pln_own, hit ? "HIT!\n" : "miss\n");
157 if (pcp->pl_flags & P_T)
158 mpr(victim, "...Incoming %s missile %s\n",
159 sublaunch ? "" : cname(pp->pln_own),
160 hit ? "HIT!\n" : "missed\n");
161 if (hit && news_item) {
163 nreport(victim, snews_item, 0, 1);
165 nreport(pp->pln_own, news_item, victim, 1);
171 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim,
172 int wantflags, int nowantflags, int mission)
174 struct plchrstr *pcp;
180 snxtitem_all(&ni, EF_PLANE);
181 while (nxtitem(&ni, &plane)) {
185 pcp = &plchr[(int)plane.pln_type];
186 if (!(pcp->pl_flags & P_M))
188 if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
190 if (nowantflags && pcp->pl_flags & nowantflags)
192 if (mission && plane.pln_mission != mission)
194 if (getrel(getnatp(plane.pln_own), victim) >= NEUTRAL)
196 /* missiles go one way, so we can use all the range */
197 if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
199 if (plane.pln_mobil <= 0)
201 if (plane.pln_effic < 100)
203 /* got a valid interceptor */
204 irv = malloc(sizeof(*irv));
207 irv->pcp = &plchr[(int)plane.pln_type];
209 emp_insque(&irv->queue, list);
214 msl_intercept(coord x, coord y, natid bombown, int hardtarget,
215 int sublaunch, int wantflags, int nowantflags)
218 struct plchrstr *pcp;
220 struct emp_qelem *irvlist;
221 struct emp_qelem foo;
222 struct emp_qelem *intlist;
223 struct emp_qelem intfoo;
224 struct emp_qelem *qp;
225 struct emp_qelem *next;
232 char *who = sublaunch ? "" : cname(bombown);
234 getsect(x, y, §);
235 if (wantflags == P_O && !nowantflags) {
236 att_name = "satellite";
237 def_name = "a-sat missile";
238 news_item = N_SAT_KILL;
239 mpr(sect.sct_own, "%s has positioned a satellite over %s\n",
240 sublaunch ? "someone" : cname(bombown),
241 xyas(x, y, sect.sct_own));
242 } else if (wantflags == P_N && nowantflags == P_O) {
243 att_name = "warhead";
245 news_item = sublaunch ? N_NUKE_SSTOP : N_NUKE_STOP;
247 att_name = "elephant";
248 def_name = "tomato"; /* heh -KHS */
249 news_item = N_NUKE_STOP;
253 /* get all hostile abms in range */
254 msl_sel(irvlist, x, y, bombown, wantflags, nowantflags, 0);
256 emp_initque(intlist);
257 /* First choose interceptors belonging to the target sector */
258 /* only allow two defense missiles per missile attack */
259 for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
261 ip = (struct plist *)qp;
263 if (pp->pln_own != sect.sct_own)
266 if (mission_pln_equip(ip, 0, 0, 'i') < 0) {
271 /* got one interceptor, delete from irv_list and
275 emp_insque(qp, intlist);
276 putplane(pp->pln_uid, pp);
279 /* only allow two defense missiles per missile attack */
280 for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
282 ip = (struct plist *)qp;
285 if (mission_pln_equip(ip, 0, 0, 'i') < 0) {
290 /* got one interceptor, delete from irv_list and
294 emp_insque(qp, intlist);
295 putplane(pp->pln_uid, pp);
298 /* Now, clean out the queue */
299 while (!QEMPTY(irvlist)) {
300 qp = irvlist->q_forw;
305 mpr(sect.sct_own, "No %ss launched to intercept.\n", def_name);
309 /* attempt to destroy incoming missile */
311 while (!QEMPTY(intlist)) {
312 qp = intlist->q_forw;
313 ip = (struct plist *)qp;
317 mpr(bombown, "%s %s launched in defense!\n",
318 cname(pp->pln_own), def_name);
319 if (sect.sct_own == pp->pln_own) {
320 mpr(sect.sct_own, "%s launched to intercept %s %s!\n",
321 def_name, who, att_name);
324 "%s launched an %s to intercept the %s %s!\n",
325 cname(pp->pln_own), def_name, who, att_name);
327 "%s launched to intercept %s %s arcing towards %s territory!\n",
328 def_name, who, att_name, cname(sect.sct_own));
332 msl_hit(pp, hardtarget, EF_PLANE, news_item, news_item,
333 att_name, x, y, bombown)) {
334 mpr(bombown, "%s destroyed by %s %s!\n",
335 att_name, cname(pp->pln_own), def_name);
336 mpr(sect.sct_own, "%s %s intercepted!\n", who, att_name);
337 if (sect.sct_own != pp->pln_own)
338 mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
341 /* zap the missile */
343 putplane(pp->pln_uid, pp);
349 /* Clean out what is left in the list */
350 while (!QEMPTY(intlist)) {
351 qp = intlist->q_forw;
358 mpr(bombown, "%s made it through %s defenses!\n",
360 mpr(sect.sct_own, "%s made it through %s defenses!\n",
366 /* Keep launching missiles on list until mindam damage has been done */
368 msl_launch_mindam(struct emp_qelem *list, coord x, coord y, int hardtarget,
369 int type, int mindam, char *whatp, int victim,
372 struct emp_qelem *qp;
373 struct emp_qelem *next;
380 if (type == EF_SHIP) {
381 news_item = N_SHP_MISS;
382 snews_item = N_SHP_SMISS;
383 } else if (type == EF_LAND) {
384 news_item = N_LND_MISS;
385 snews_item = N_LND_SMISS;
387 news_item = N_SCT_MISS;
388 snews_item = N_SCT_SMISS;
391 for (qp = list->q_back; qp != list && dam < mindam && !nukedam;
394 plp = (struct plist *)qp;
396 if (mission_pln_equip(plp, 0, 0, 'p') >= 0) {
397 if (msl_hit(&plp->plane,
398 hardtarget, type, news_item, snews_item,
399 whatp, x, y, victim)) {
400 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
402 if (mission == MI_INTERDICT && type == EF_SECTOR)
407 /* Missiles that miss have to hit somewhere! */
408 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
409 collateral_damage(x, y, newdam);
411 plp->plane.pln_effic = 0;
412 putplane(plp->plane.pln_uid, &plp->plane);