2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2013, 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-2012
50 static int launch_as(struct plnstr *pp);
51 static int launch_missile(struct plnstr *pp);
52 static int launch_sat(struct plnstr *pp);
53 static int msl_equip(struct plnstr *, char);
61 struct nstr_item nstr;
66 if (!snxtitem(&nstr, EF_PLANE, player->argp[1], NULL))
68 while (nxtitem(&nstr, &plane)) {
69 if (plane.pln_own != player->cnum)
71 pcp = &plchr[(int)plane.pln_type];
72 if ((pcp->pl_flags & (P_M | P_O)) == 0) {
73 pr("%s isn't a missile!\n", prplane(&plane));
76 if (pcp->pl_flags & P_F) {
77 pr("%s is a surface-to-air missile!\n", prplane(&plane));
80 if (pcp->pl_flags & P_N) {
81 pr("%s is an anti-ballistic-missile missile!\n",
85 if (pln_is_in_orbit(&plane)) {
86 pr("%s already in orbit!\n", prplane(&plane));
90 if (ontradingblock(EF_PLANE, &plane)) {
91 pr("plane #%d inelligible - it's for sale.\n",
97 if (plane.pln_effic < 40) {
98 pr("%s is damaged (%d%%)\n", prplane(&plane), plane.pln_effic);
101 if (!pln_airbase_ok(&plane, 1, 1))
103 pr("%s at %s; range %d, eff %d%%\n", prplane(&plane),
104 xyas(plane.pln_x, plane.pln_y, player->cnum),
105 plane.pln_range, plane.pln_effic);
106 if (!(pcp->pl_flags & P_O)) {
107 retval = launch_missile(&plane);
109 } else if ((pcp->pl_flags & (P_M | P_O)) == (P_M | P_O)) {
110 retval = launch_as(&plane);
112 } else { /* satellites */
113 retval = launch_sat(&plane);
114 gone = !(plane.pln_flags & PLN_LAUNCHED);
116 if (retval != RET_OK)
120 putplane(plane.pln_uid, &plane);
127 * Launch anti-sat weapon PP.
128 * Return RET_OK if launched (even when missile explodes),
129 * else RET_SYN or RET_FAIL.
132 launch_as(struct plnstr *pp)
137 cp = getstarg(player->argp[2], "Target satellite? ", buf);
138 if (!check_plane_ok(pp))
142 if (!getplane(atoi(cp), &plane) || !plane.pln_own
143 || !pln_is_in_orbit(&plane)) {
144 pr("No such satellite exists!\n");
146 /* Can be abused to find satellite ids. Tolerable. */
149 if (mapdist(pp->pln_x, pp->pln_y, plane.pln_x, plane.pln_y)
151 pr("Range too great!\n");
154 if (msl_equip(pp, 'i') < 0)
156 if (msl_launch(pp, EF_PLANE, prplane(&plane),
157 plane.pln_x, plane.pln_y, plane.pln_own, NULL) < 0)
159 if (msl_hit(pp, pln_def(&plane), EF_PLANE, 0, 0, 0, plane.pln_own)) {
160 pr("Satellite shot down\n");
161 mpr(plane.pln_own, "%s anti-sat destroyed %s over %s\n",
162 cname(player->cnum), prplane(&plane),
163 xyas(plane.pln_x, plane.pln_y, plane.pln_own));
164 nreport(pp->pln_own, N_SAT_KILL, plane.pln_own, 1);
166 putplane(plane.pln_uid, &plane);
173 * Return RET_OK if launched (even when missile explodes),
174 * else RET_SYN or RET_FAIL.
177 launch_missile(struct plnstr *pp)
179 struct plchrstr *pcp = plchr + pp->pln_type;
181 int n, dam, sublaunch;
184 struct shpstr target_ship;
189 if (pcp->pl_flags & P_MAR)
190 cp = getstarg(player->argp[2], "Target ship? ", buf);
192 cp = getstarg(player->argp[2], "Target sector? ", buf);
195 if (!check_plane_ok(pp))
197 if (sarg_type(cp) == NS_LIST) {
198 if (!(pcp->pl_flags & P_MAR)) {
199 pr("Missile not designed to attack ships!\n");
203 if ((n < 0) || !getship(n, &target_ship) || !target_ship.shp_own) {
204 pr("Bad ship number!\n");
207 sx = target_ship.shp_x;
208 sy = target_ship.shp_y;
209 mcp = &mchr[(int)target_ship.shp_type];
210 if (mcp->m_flags & M_SUB) {
211 pr("Bad ship number!\n");
214 } else if (!sarg_xy(cp, &sx, &sy)) {
215 pr("Not a sector!\n");
218 if (pcp->pl_flags & P_MAR) {
219 pr("Missile designed to attack ships!\n");
224 if (mapdist(pp->pln_x, pp->pln_y, sx, sy) > pp->pln_range) {
225 pr("Range too great; try again!\n");
228 if (!(pcp->pl_flags & P_MAR)) {
229 if (msl_equip(pp, 's') < 0)
231 getsect(sx, sy, §);
232 if (msl_launch(pp, EF_SECTOR, "sector", sx, sy, sect.sct_own,
235 getsect(sx, sy, §);
236 if (!msl_hit(pp, SECT_HARDTARGET, EF_SECTOR,
237 N_SCT_MISS, N_SCT_SMISS, sublaunch, sect.sct_own))
239 if (getnuke(nuk_on_plane(pp), &nuke))
240 detonate(&nuke, sx, sy, pp->pln_flags & PLN_AIRBURST);
242 dam = pln_damage(pp, 's', 1);
243 pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
244 xyas(sx, sy, player->cnum));
245 if (sect.sct_own != 0) {
248 "Sub missile attack did %d damage in %s\n",
249 dam, xyas(sx, sy, sect.sct_own));
252 "%s missile attack did %d damage in %s\n",
253 cname(player->cnum), dam,
254 xyas(sx, sy, sect.sct_own));
256 sectdamage(§, dam);
260 if (msl_equip(pp, 'p') < 0)
262 if (msl_launch(pp, EF_SHIP, prship(&target_ship),
263 target_ship.shp_x, target_ship.shp_y,
264 target_ship.shp_own, &sublaunch) < 0)
266 getship(n, &target_ship);
267 if (!msl_hit(pp, shp_hardtarget(&target_ship), EF_SHIP,
268 N_SHP_MISS, N_SHP_SMISS, sublaunch,
269 target_ship.shp_own)) {
271 dam = pln_damage(pp, 'p', 0);
272 collateral_damage(target_ship.shp_x, target_ship.shp_y, dam);
275 dam = pln_damage(pp, 'p', 1);
276 check_retreat_and_do_shipdamage(&target_ship, dam);
277 putship(target_ship.shp_uid, &target_ship);
278 getship(target_ship.shp_uid, &target_ship);
279 if (!target_ship.shp_own)
280 pr("%s sunk!\n", prship(&target_ship));
286 * Launch a satellite.
287 * Return RET_OK if launched (even when satellite fails),
288 * else RET_SYN or RET_FAIL.
291 launch_sat(struct plnstr *pp)
302 cp = getstarg(player->argp[2], "Target sector? ", buf);
303 if (!check_plane_ok(pp))
307 if (!sarg_xy(cp, &sx, &sy)) {
308 pr("Bad sector designation!\n");
311 if ((dist = mapdist(pp->pln_x, pp->pln_y, sx, sy)) > pp->pln_range) {
312 pr("Range too great; try again!\n");
315 p = getstring("Geostationary orbit? ", buf);
318 if (!check_plane_ok(pp))
320 if (msl_equip(pp, 'r') < 0)
323 pp->pln_flags |= PLN_SYNCHRONOUS;
324 if (*p == 0 || *p == 'n')
325 pp->pln_flags &= ~(PLN_SYNCHRONOUS);
326 pr("3... 2... 1... Blastoff!!!\n");
327 if (chance(0.07 + (100 - pp->pln_effic) / 100.0)) {
328 pr("KABOOOOM! Range safety officer detonates booster!\n");
332 i = pp->pln_tech + pp->pln_effic;
333 if (chance(1.0 - (i / (i + 50.0)))) {
334 dir = DIR_FIRST + roll0(6);
335 sx = xnorm(sx + diroff[dir][0]);
336 sy = ynorm(sy + diroff[dir][1]);
337 pr("Your trajectory was a little off.\n");
339 nreport(player->cnum, N_LAUNCH, 0, 1);
340 if (msl_asat_intercept(pp, sx, sy))
344 CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
345 pp->pln_flags |= PLN_LAUNCHED;
346 pp->pln_mobil = pp->pln_mobil > dist ? pp->pln_mobil - dist : 0;
347 putplane(pp->pln_uid, pp);
348 pr("%s positioned over %s, will be ready for use in %d time units\n",
349 prplane(pp), xyas(sx, sy, player->cnum),
350 plane_mob_max - pp->pln_mobil);
355 msl_equip(struct plnstr *pp, char mission)
360 pl.pstage = PLG_HEALTHY;
361 pl.pcp = plchr + pp->pln_type;
363 emp_initque(&pl.queue);
364 return pln_equip(&pl, NULL, mission);