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 * laun.c: Launch missiles from land or sea
30 * Known contributors to this file:
33 * Steve McClure, 1998-2000
34 * Markus Armbruster, 2005-2008
48 static int launch_as(struct plnstr *pp);
49 static int launch_missile(struct plnstr *pp, int sublaunch);
50 static int launch_sat(struct plnstr *pp, int sublaunch);
58 struct nstr_item nstr;
65 if (!snxtitem(&nstr, EF_PLANE, player->argp[1], NULL))
67 while (nxtitem(&nstr, &plane)) {
68 if (plane.pln_own != player->cnum)
70 pcp = &plchr[(int)plane.pln_type];
71 if ((pcp->pl_flags & (P_M | P_O)) == 0) {
72 pr("%s isn't a missile!\n", prplane(&plane));
75 if (pcp->pl_flags & P_F) {
76 pr("%s is a surface-to-air missile!\n", prplane(&plane));
79 if (pcp->pl_flags & P_N) {
80 pr("%s is an anti-ballistic-missile missile!\n",
84 if (pln_is_in_orbit(&plane)) {
85 pr("%s already in orbit!\n", prplane(&plane));
89 if (ontradingblock(EF_PLANE, &plane)) {
90 pr("plane #%d inelligible - it's for sale.\n",
96 if (plane.pln_effic < 40) {
97 pr("%s is damaged (%d%%)\n", prplane(&plane), plane.pln_effic);
100 if (!pln_airbase_ok(&plane, 1, 1))
103 if (plane.pln_ship >= 0) {
104 getship(plane.pln_ship, &ship);
105 if (mchr[(int)ship.shp_type].m_flags & M_SUB)
108 pr("%s at %s; range %d, eff %d%%\n", prplane(&plane),
109 xyas(plane.pln_x, plane.pln_y, player->cnum),
110 plane.pln_range, plane.pln_effic);
111 if (!(pcp->pl_flags & P_O)) {
112 retval = launch_missile(&plane, sublaunch);
114 } else if ((pcp->pl_flags & (P_M | P_O)) == (P_M | P_O)) {
115 retval = launch_as(&plane);
117 } else { /* satellites */
118 retval = launch_sat(&plane, sublaunch);
119 gone = !(plane.pln_flags & PLN_LAUNCHED);
121 if (retval != RET_OK)
125 putplane(plane.pln_uid, &plane);
132 * Launch anti-sat weapon PP.
133 * Return RET_OK if launched (even when missile explodes),
134 * else RET_SYN or RET_FAIL.
137 launch_as(struct plnstr *pp)
144 cp = getstarg(player->argp[2], "Target satellite? ", buf);
145 if (!check_plane_ok(pp))
149 if (!getplane(atoi(cp), &plane) || !plane.pln_own
150 || !pln_is_in_orbit(&plane)) {
151 pr("No such satellite exists!\n");
153 /* Can be abused to find satellite ids. Tolerable. */
156 if (mapdist(pp->pln_x, pp->pln_y, plane.pln_x, plane.pln_y)
158 pr("Range too great!\n");
161 if (msl_equip(pp, 'p') < 0) {
162 pr("%s not enough petrol or shells!\n", prplane(pp));
165 if (msl_hit(pp, pln_def(&plane), EF_PLANE, N_SAT_KILL, N_SAT_KILL,
166 prplane(&plane), plane.pln_x, plane.pln_y, plane.pln_own)) {
167 dam = pln_damage(pp, plane.pln_x, plane.pln_y, 'p', &nukedam, 1);
168 CANT_HAPPEN(nukedam);
169 oldown = plane.pln_own;
170 planedamage(&plane, dam);
171 pr("Hit satellite for %d%% damage!\n", dam);
172 mpr(oldown, "%s anti-sat did %d%% damage to %s over %s\n",
173 cname(player->cnum), dam, prplane(&plane),
174 xyas(plane.pln_x, plane.pln_y, plane.pln_own));
175 putplane(plane.pln_uid, &plane);
177 mpr(oldown, "Satellite shot down\n");
184 * If SUBLAUNCH, it's sub-launched.
185 * Return RET_OK if launched (even when missile explodes),
186 * else RET_SYN or RET_FAIL.
189 launch_missile(struct plnstr *pp, int sublaunch)
191 struct plchrstr *pcp = plchr + pp->pln_type;
196 struct shpstr target_ship;
203 if (pcp->pl_flags & P_MAR)
204 cp = getstarg(player->argp[2], "Target ship? ", buf);
206 cp = getstarg(player->argp[2], "Target sector? ", buf);
209 if (!check_plane_ok(pp))
211 if (sarg_type(cp) == NS_LIST) {
212 if (!(pcp->pl_flags & P_MAR)) {
213 pr("Missile not designed to attack ships!\n");
217 if ((n < 0) || !getship(n, &target_ship) ||
218 !target_ship.shp_own) {
219 pr("Bad ship number!\n");
222 sx = target_ship.shp_x;
223 sy = target_ship.shp_y;
224 mcp = &mchr[(int)target_ship.shp_type];
225 if (mcp->m_flags & M_SUB) {
226 pr("Bad ship number!\n");
229 } else if (!sarg_xy(cp, &sx, &sy)) {
230 pr("Not a sector!\n");
233 if (pcp->pl_flags & P_MAR) {
234 pr("Missile designed to attack ships!\n");
239 if (mapdist(pp->pln_x, pp->pln_y, sx, sy) > pp->pln_range) {
240 pr("Range too great; try again!\n");
243 if (msl_equip(pp, 'p') < 0) {
244 pr("%s not enough shells!\n", prplane(pp));
247 if (!(pcp->pl_flags & P_MAR)) {
248 getsect(sx, sy, §);
250 natp = getnatp(player->cnum);
251 rel = getrel(natp, sect.sct_own);
252 if ((rel != AT_WAR) && (sect.sct_own != player->cnum) &&
253 (sect.sct_own) && (sect.sct_oldown != player->cnum)) {
254 pr("You are not at war with the player->owner of the target sector!\n");
257 pr("Missile monitoring officer destroys RV before detonation.\n");
261 if (!msl_hit(pp, SECT_HARDTARGET, EF_SECTOR, N_SCT_MISS,
262 N_SCT_SMISS, "sector", sx, sy, sect.sct_own)) {
265 * FIXME want collateral damage on miss (which can't
266 * happen for nuclear war heads), but we get here too when
267 * launch fails or missile is intercepted
269 dam = pln_damage(pp, sect.sct_x, sect.sct_y, 's', &nukedam, 0);
270 collateral_damage(sect.sct_x, sect.sct_y, dam, 0);
274 dam = pln_damage(pp, sect.sct_x, sect.sct_y, 's', &nukedam, 1);
276 pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
277 xyas(sx, sy, player->cnum));
278 if (sect.sct_own != 0) {
281 "Sub missile attack did %d damage in %s\n",
282 dam, xyas(sx, sy, sect.sct_own));
285 "%s missile attack did %d damage in %s\n",
286 cname(player->cnum), dam,
287 xyas(sx, sy, sect.sct_own));
289 sectdamage(§, dam);
293 if (!msl_hit(pp, shp_hardtarget(&target_ship), EF_SHIP,
294 N_SHP_MISS, N_SHP_SMISS, prship(&target_ship),
295 target_ship.shp_x, target_ship.shp_y,
296 target_ship.shp_own)) {
298 #if 0 /* FIXME see above */
299 dam = pln_damage(pp,target_ship.shp_x,target_ship.shp_y,'p',&nukedam, 0);
300 collateral_damage(target_ship.shp_x, target_ship.shp_y, dam, 0);
304 dam = pln_damage(pp, target_ship.shp_x, target_ship.shp_y, 'p',
307 check_retreat_and_do_shipdamage(&target_ship, dam);
308 putship(target_ship.shp_uid, &target_ship);
310 getship(target_ship.shp_uid, &target_ship);
311 if (!target_ship.shp_own)
312 pr("%s sunk!\n", prship(&target_ship));
318 * Launch a satellite.
319 * Return RET_OK if launched (even when satellite fails),
320 * else RET_SYN or RET_FAIL.
323 launch_sat(struct plnstr *pp, int sublaunch)
325 struct plchrstr *pcp = plchr + pp->pln_type;
335 cp = getstarg(player->argp[2], "Target sector? ", buf);
336 if (!check_plane_ok(pp))
340 if (!sarg_xy(cp, &sx, &sy)) {
341 pr("Bad sector designation!\n");
344 if ((dist = mapdist(pp->pln_x, pp->pln_y, sx, sy)) > pp->pln_range) {
345 pr("Range too great; try again!\n");
348 p = getstring("Geostationary orbit? ", buf);
351 if (!check_plane_ok(pp))
353 if (msl_equip(pp, 'r') < 0) {
354 pr("%s not enough petrol!\n", prplane(pp));
358 pp->pln_flags |= PLN_SYNCHRONOUS;
359 if (*p == 0 || *p == 'n')
360 pp->pln_flags &= ~(PLN_SYNCHRONOUS);
361 pr("3... 2... 1... Blastoff!!!\n");
362 if (chance(0.07 + (100 - pp->pln_effic) / 100.0)) {
363 pr("KABOOOOM! Range safety officer detonates booster!\n");
367 i = pp->pln_tech + pp->pln_effic;
368 if (chance(1.0 - (i / (i + 50.0)))) {
369 dir = (random() % 6) + 1;
370 sx = xnorm(sx + diroff[dir][0]);
371 sy = ynorm(sy + diroff[dir][1]);
372 pr("Your trajectory was a little off.\n");
374 nreport(player->cnum, N_LAUNCH, 0, 1);
375 if (msl_intercept(sx, sy, pp->pln_own, pcp->pl_def, sublaunch, P_O, 0)) {
380 CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
381 pp->pln_flags |= PLN_LAUNCHED;
382 pp->pln_mobil = pp->pln_mobil > dist ? pp->pln_mobil - dist : 0;
383 putplane(pp->pln_uid, pp);
384 pr("%s positioned over %s, will be ready for use in %d time units\n",
385 prplane(pp), xyas(sx, sy, player->cnum),
386 plane_mob_max - pp->pln_mobil);