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 * laun.c: Launch missiles from land or sea
29 * Known contributors to this file:
32 * Steve McClure, 1998-2000
33 * Markus Armbruster, 2005-2009
48 static int launch_as(struct plnstr *pp);
49 static int launch_missile(struct plnstr *pp);
50 static int launch_sat(struct plnstr *pp);
51 static int msl_equip(struct plnstr *, char);
59 struct nstr_item nstr;
64 if (!snxtitem(&nstr, EF_PLANE, player->argp[1], NULL))
66 while (nxtitem(&nstr, &plane)) {
67 if (plane.pln_own != player->cnum)
69 pcp = &plchr[(int)plane.pln_type];
70 if ((pcp->pl_flags & (P_M | P_O)) == 0) {
71 pr("%s isn't a missile!\n", prplane(&plane));
74 if (pcp->pl_flags & P_F) {
75 pr("%s is a surface-to-air missile!\n", prplane(&plane));
78 if (pcp->pl_flags & P_N) {
79 pr("%s is an anti-ballistic-missile missile!\n",
83 if (pln_is_in_orbit(&plane)) {
84 pr("%s already in orbit!\n", prplane(&plane));
88 if (ontradingblock(EF_PLANE, &plane)) {
89 pr("plane #%d inelligible - it's for sale.\n",
95 if (plane.pln_effic < 40) {
96 pr("%s is damaged (%d%%)\n", prplane(&plane), plane.pln_effic);
99 if (!pln_airbase_ok(&plane, 1, 1))
101 pr("%s at %s; range %d, eff %d%%\n", prplane(&plane),
102 xyas(plane.pln_x, plane.pln_y, player->cnum),
103 plane.pln_range, plane.pln_effic);
104 if (!(pcp->pl_flags & P_O)) {
105 retval = launch_missile(&plane);
107 } else if ((pcp->pl_flags & (P_M | P_O)) == (P_M | P_O)) {
108 retval = launch_as(&plane);
110 } else { /* satellites */
111 retval = launch_sat(&plane);
112 gone = !(plane.pln_flags & PLN_LAUNCHED);
114 if (retval != RET_OK)
118 putplane(plane.pln_uid, &plane);
125 * Launch anti-sat weapon PP.
126 * Return RET_OK if launched (even when missile explodes),
127 * else RET_SYN or RET_FAIL.
130 launch_as(struct plnstr *pp)
135 cp = getstarg(player->argp[2], "Target satellite? ", buf);
136 if (!check_plane_ok(pp))
140 if (!getplane(atoi(cp), &plane) || !plane.pln_own
141 || !pln_is_in_orbit(&plane)) {
142 pr("No such satellite exists!\n");
144 /* Can be abused to find satellite ids. Tolerable. */
147 if (mapdist(pp->pln_x, pp->pln_y, plane.pln_x, plane.pln_y)
149 pr("Range too great!\n");
152 if (msl_equip(pp, 'i') < 0)
154 if (msl_launch(pp, EF_PLANE, prplane(&plane),
155 plane.pln_x, plane.pln_y, plane.pln_own, NULL) < 0)
157 if (msl_hit(pp, pln_def(&plane), EF_PLANE, 0, 0, 0, plane.pln_own)) {
158 pr("Satellite shot down\n");
159 mpr(plane.pln_own, "%s anti-sat destroyed %s over %s\n",
160 cname(player->cnum), prplane(&plane),
161 xyas(plane.pln_x, plane.pln_y, plane.pln_own));
162 nreport(pp->pln_own, N_SAT_KILL, plane.pln_own, 1);
164 putplane(plane.pln_uid, &plane);
171 * Return RET_OK if launched (even when missile explodes),
172 * else RET_SYN or RET_FAIL.
175 launch_missile(struct plnstr *pp)
177 struct plchrstr *pcp = plchr + pp->pln_type;
179 int n, dam, sublaunch;
182 struct shpstr target_ship;
187 if (pcp->pl_flags & P_MAR)
188 cp = getstarg(player->argp[2], "Target ship? ", buf);
190 cp = getstarg(player->argp[2], "Target sector? ", buf);
193 if (!check_plane_ok(pp))
195 if (sarg_type(cp) == NS_LIST) {
196 if (!(pcp->pl_flags & P_MAR)) {
197 pr("Missile not designed to attack ships!\n");
201 if ((n < 0) || !getship(n, &target_ship) || !target_ship.shp_own) {
202 pr("Bad ship number!\n");
205 sx = target_ship.shp_x;
206 sy = target_ship.shp_y;
207 mcp = &mchr[(int)target_ship.shp_type];
208 if (mcp->m_flags & M_SUB) {
209 pr("Bad ship number!\n");
212 } else if (!sarg_xy(cp, &sx, &sy)) {
213 pr("Not a sector!\n");
216 if (pcp->pl_flags & P_MAR) {
217 pr("Missile designed to attack ships!\n");
222 if (mapdist(pp->pln_x, pp->pln_y, sx, sy) > pp->pln_range) {
223 pr("Range too great; try again!\n");
226 if (!(pcp->pl_flags & P_MAR)) {
227 if (msl_equip(pp, 's') < 0)
229 getsect(sx, sy, §);
230 if (msl_launch(pp, EF_SECTOR, "sector", sx, sy, sect.sct_own,
233 getsect(sx, sy, §);
234 if (!msl_hit(pp, SECT_HARDTARGET, EF_SECTOR,
235 N_SCT_MISS, N_SCT_SMISS, sublaunch, sect.sct_own))
237 if (getnuke(nuk_on_plane(pp), &nuke))
238 detonate(&nuke, sx, sy, pp->pln_flags & PLN_AIRBURST);
240 dam = pln_damage(pp, 's', 1);
241 pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
242 xyas(sx, sy, player->cnum));
243 if (sect.sct_own != 0) {
246 "Sub missile attack did %d damage in %s\n",
247 dam, xyas(sx, sy, sect.sct_own));
250 "%s missile attack did %d damage in %s\n",
251 cname(player->cnum), dam,
252 xyas(sx, sy, sect.sct_own));
254 sectdamage(§, dam);
258 if (msl_equip(pp, 'p') < 0)
260 if (msl_launch(pp, EF_SHIP, prship(&target_ship),
261 target_ship.shp_x, target_ship.shp_y,
262 target_ship.shp_own, &sublaunch) < 0)
264 getship(n, &target_ship);
265 if (!msl_hit(pp, shp_hardtarget(&target_ship), EF_SHIP,
266 N_SHP_MISS, N_SHP_SMISS, sublaunch,
267 target_ship.shp_own)) {
269 dam = pln_damage(pp, 'p', 0);
270 collateral_damage(target_ship.shp_x, target_ship.shp_y, dam);
273 dam = pln_damage(pp, 'p', 1);
274 check_retreat_and_do_shipdamage(&target_ship, dam);
275 putship(target_ship.shp_uid, &target_ship);
276 getship(target_ship.shp_uid, &target_ship);
277 if (!target_ship.shp_own)
278 pr("%s sunk!\n", prship(&target_ship));
284 * Launch a satellite.
285 * Return RET_OK if launched (even when satellite fails),
286 * else RET_SYN or RET_FAIL.
289 launch_sat(struct plnstr *pp)
300 cp = getstarg(player->argp[2], "Target sector? ", buf);
301 if (!check_plane_ok(pp))
305 if (!sarg_xy(cp, &sx, &sy)) {
306 pr("Bad sector designation!\n");
309 if ((dist = mapdist(pp->pln_x, pp->pln_y, sx, sy)) > pp->pln_range) {
310 pr("Range too great; try again!\n");
313 p = getstring("Geostationary orbit? ", buf);
316 if (!check_plane_ok(pp))
318 if (msl_equip(pp, 'r') < 0)
321 pp->pln_flags |= PLN_SYNCHRONOUS;
322 if (*p == 0 || *p == 'n')
323 pp->pln_flags &= ~(PLN_SYNCHRONOUS);
324 pr("3... 2... 1... Blastoff!!!\n");
325 if (chance(0.07 + (100 - pp->pln_effic) / 100.0)) {
326 pr("KABOOOOM! Range safety officer detonates booster!\n");
330 i = pp->pln_tech + pp->pln_effic;
331 if (chance(1.0 - (i / (i + 50.0)))) {
332 dir = (random() % 6) + 1;
333 sx = xnorm(sx + diroff[dir][0]);
334 sy = ynorm(sy + diroff[dir][1]);
335 pr("Your trajectory was a little off.\n");
337 nreport(player->cnum, N_LAUNCH, 0, 1);
338 if (msl_asat_intercept(pp, sx, sy))
342 CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
343 pp->pln_flags |= PLN_LAUNCHED;
344 pp->pln_mobil = pp->pln_mobil > dist ? pp->pln_mobil - dist : 0;
345 putplane(pp->pln_uid, pp);
346 pr("%s positioned over %s, will be ready for use in %d time units\n",
347 prplane(pp), xyas(sx, sy, player->cnum),
348 plane_mob_max - pp->pln_mobil);
353 msl_equip(struct plnstr *pp, char mission)
357 memset(&pl, 0, sizeof(struct plist));
358 pl.pcp = plchr + pp->pln_type;
360 return pln_equip(&pl, NULL, mission);