2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, 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-2009
49 static int launch_as(struct plnstr *pp);
50 static int launch_missile(struct plnstr *pp);
51 static int launch_sat(struct plnstr *pp);
52 static int msl_equip(struct plnstr *, char);
60 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))
102 pr("%s at %s; range %d, eff %d%%\n", prplane(&plane),
103 xyas(plane.pln_x, plane.pln_y, player->cnum),
104 plane.pln_range, plane.pln_effic);
105 if (!(pcp->pl_flags & P_O)) {
106 retval = launch_missile(&plane);
108 } else if ((pcp->pl_flags & (P_M | P_O)) == (P_M | P_O)) {
109 retval = launch_as(&plane);
111 } else { /* satellites */
112 retval = launch_sat(&plane);
113 gone = !(plane.pln_flags & PLN_LAUNCHED);
115 if (retval != RET_OK)
119 putplane(plane.pln_uid, &plane);
126 * Launch anti-sat weapon PP.
127 * Return RET_OK if launched (even when missile explodes),
128 * else RET_SYN or RET_FAIL.
131 launch_as(struct plnstr *pp)
136 cp = getstarg(player->argp[2], "Target satellite? ", buf);
137 if (!check_plane_ok(pp))
141 if (!getplane(atoi(cp), &plane) || !plane.pln_own
142 || !pln_is_in_orbit(&plane)) {
143 pr("No such satellite exists!\n");
145 /* Can be abused to find satellite ids. Tolerable. */
148 if (mapdist(pp->pln_x, pp->pln_y, plane.pln_x, plane.pln_y)
150 pr("Range too great!\n");
153 if (msl_equip(pp, 'i') < 0)
155 if (msl_launch(pp, EF_PLANE, prplane(&plane),
156 plane.pln_x, plane.pln_y, plane.pln_own, NULL) < 0)
158 if (msl_hit(pp, pln_def(&plane), EF_PLANE, 0, 0, 0, plane.pln_own)) {
159 pr("Satellite shot down\n");
160 mpr(plane.pln_own, "%s anti-sat destroyed %s over %s\n",
161 cname(player->cnum), prplane(&plane),
162 xyas(plane.pln_x, plane.pln_y, plane.pln_own));
163 nreport(pp->pln_own, N_SAT_KILL, plane.pln_own, 1);
165 putplane(plane.pln_uid, &plane);
172 * Return RET_OK if launched (even when missile explodes),
173 * else RET_SYN or RET_FAIL.
176 launch_missile(struct plnstr *pp)
178 struct plchrstr *pcp = plchr + pp->pln_type;
180 int n, dam, sublaunch;
183 struct shpstr target_ship;
188 if (pcp->pl_flags & P_MAR)
189 cp = getstarg(player->argp[2], "Target ship? ", buf);
191 cp = getstarg(player->argp[2], "Target sector? ", buf);
194 if (!check_plane_ok(pp))
196 if (sarg_type(cp) == NS_LIST) {
197 if (!(pcp->pl_flags & P_MAR)) {
198 pr("Missile not designed to attack ships!\n");
202 if ((n < 0) || !getship(n, &target_ship) || !target_ship.shp_own) {
203 pr("Bad ship number!\n");
206 sx = target_ship.shp_x;
207 sy = target_ship.shp_y;
208 mcp = &mchr[(int)target_ship.shp_type];
209 if (mcp->m_flags & M_SUB) {
210 pr("Bad ship number!\n");
213 } else if (!sarg_xy(cp, &sx, &sy)) {
214 pr("Not a sector!\n");
217 if (pcp->pl_flags & P_MAR) {
218 pr("Missile designed to attack ships!\n");
223 if (mapdist(pp->pln_x, pp->pln_y, sx, sy) > pp->pln_range) {
224 pr("Range too great; try again!\n");
227 if (!(pcp->pl_flags & P_MAR)) {
228 if (msl_equip(pp, 's') < 0)
230 getsect(sx, sy, §);
231 if (msl_launch(pp, EF_SECTOR, "sector", sx, sy, sect.sct_own,
234 getsect(sx, sy, §);
235 if (!msl_hit(pp, SECT_HARDTARGET, EF_SECTOR,
236 N_SCT_MISS, N_SCT_SMISS, sublaunch, sect.sct_own))
238 if (getnuke(nuk_on_plane(pp), &nuke))
239 detonate(&nuke, sx, sy, pp->pln_flags & PLN_AIRBURST);
241 dam = pln_damage(pp, 's', 1);
242 pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
243 xyas(sx, sy, player->cnum));
244 if (sect.sct_own != 0) {
247 "Sub missile attack did %d damage in %s\n",
248 dam, xyas(sx, sy, sect.sct_own));
251 "%s missile attack did %d damage in %s\n",
252 cname(player->cnum), dam,
253 xyas(sx, sy, sect.sct_own));
255 sectdamage(§, dam);
259 if (msl_equip(pp, 'p') < 0)
261 if (msl_launch(pp, EF_SHIP, prship(&target_ship),
262 target_ship.shp_x, target_ship.shp_y,
263 target_ship.shp_own, &sublaunch) < 0)
265 getship(n, &target_ship);
266 if (!msl_hit(pp, shp_hardtarget(&target_ship), EF_SHIP,
267 N_SHP_MISS, N_SHP_SMISS, sublaunch,
268 target_ship.shp_own)) {
270 dam = pln_damage(pp, 'p', 0);
271 collateral_damage(target_ship.shp_x, target_ship.shp_y, dam);
274 dam = pln_damage(pp, 'p', 1);
275 check_retreat_and_do_shipdamage(&target_ship, dam);
276 putship(target_ship.shp_uid, &target_ship);
277 getship(target_ship.shp_uid, &target_ship);
278 if (!target_ship.shp_own)
279 pr("%s sunk!\n", prship(&target_ship));
285 * Launch a satellite.
286 * Return RET_OK if launched (even when satellite fails),
287 * else RET_SYN or RET_FAIL.
290 launch_sat(struct plnstr *pp)
301 cp = getstarg(player->argp[2], "Target sector? ", buf);
302 if (!check_plane_ok(pp))
306 if (!sarg_xy(cp, &sx, &sy)) {
307 pr("Bad sector designation!\n");
310 if ((dist = mapdist(pp->pln_x, pp->pln_y, sx, sy)) > pp->pln_range) {
311 pr("Range too great; try again!\n");
314 p = getstring("Geostationary orbit? ", buf);
317 if (!check_plane_ok(pp))
319 if (msl_equip(pp, 'r') < 0)
322 pp->pln_flags |= PLN_SYNCHRONOUS;
323 if (*p == 0 || *p == 'n')
324 pp->pln_flags &= ~(PLN_SYNCHRONOUS);
325 pr("3... 2... 1... Blastoff!!!\n");
326 if (chance(0.07 + (100 - pp->pln_effic) / 100.0)) {
327 pr("KABOOOOM! Range safety officer detonates booster!\n");
331 i = pp->pln_tech + pp->pln_effic;
332 if (chance(1.0 - (i / (i + 50.0)))) {
333 dir = (random() % 6) + 1;
334 sx = xnorm(sx + diroff[dir][0]);
335 sy = ynorm(sy + diroff[dir][1]);
336 pr("Your trajectory was a little off.\n");
338 nreport(player->cnum, N_LAUNCH, 0, 1);
339 if (msl_asat_intercept(pp, sx, sy))
343 CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
344 pp->pln_flags |= PLN_LAUNCHED;
345 pp->pln_mobil = pp->pln_mobil > dist ? pp->pln_mobil - dist : 0;
346 putplane(pp->pln_uid, pp);
347 pr("%s positioned over %s, will be ready for use in %d time units\n",
348 prplane(pp), xyas(sx, sy, player->cnum),
349 plane_mob_max - pp->pln_mobil);
354 msl_equip(struct plnstr *pp, char mission)
358 memset(&pl, 0, sizeof(struct plist));
359 pl.pcp = plchr + pp->pln_type;
361 return pln_equip(&pl, NULL, mission);