2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2017, 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-2015
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 ineligible - 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);
140 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);
172 * Launch missile @pp.
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', "");
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', NULL);
272 collateral_damage(target_ship.shp_x, target_ship.shp_y, dam);
275 dam = pln_damage(pp, 'p', "");
276 shipdamage(&target_ship, dam);
277 if (target_ship.shp_effic < SHIP_MINEFF)
278 pr("%s sunk!\n", prship(&target_ship));
279 if (dam && (target_ship.shp_rflags & RET_INJURED))
280 retreat_ship(&target_ship, target_ship.shp_own, 'i');
281 putship(target_ship.shp_uid, &target_ship);
287 * Launch satellite @pp.
288 * Return RET_OK if launched (even when satellite fails),
289 * else RET_SYN or RET_FAIL.
292 launch_sat(struct plnstr *pp)
303 cp = getstarg(player->argp[2], "Target sector? ", buf);
306 if (!check_plane_ok(pp))
308 if (!sarg_xy(cp, &sx, &sy)) {
309 pr("Bad sector designation!\n");
312 if ((dist = mapdist(pp->pln_x, pp->pln_y, sx, sy)) > pp->pln_range) {
313 pr("Range too great; try again!\n");
316 p = getstring("Geostationary orbit? ", buf);
319 if (!check_plane_ok(pp))
321 if (msl_equip(pp, 'r') < 0)
324 pp->pln_flags |= PLN_SYNCHRONOUS;
325 if (*p == 0 || *p == 'n')
326 pp->pln_flags &= ~(PLN_SYNCHRONOUS);
327 pr("3... 2... 1... Blastoff!!!\n");
328 if (chance(0.07 + (100 - pp->pln_effic) / 100.0)) {
329 pr("KABOOOOM! Range safety officer detonates booster!\n");
333 i = pp->pln_tech + pp->pln_effic;
334 if (chance(1.0 - (i / (i + 50.0)))) {
335 dir = DIR_FIRST + roll0(6);
336 sx = xnorm(sx + diroff[dir][0]);
337 sy = ynorm(sy + diroff[dir][1]);
338 pr("Your trajectory was a little off.\n");
340 nreport(player->cnum, N_LAUNCH, 0, 1);
341 if (msl_asat_intercept(pp, sx, sy))
345 CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
346 pp->pln_flags |= PLN_LAUNCHED;
347 pp->pln_mobil = pp->pln_mobil > dist ? pp->pln_mobil - dist : 0;
348 putplane(pp->pln_uid, pp);
349 pr("%s positioned over %s, will be ready for use in %d time units\n",
350 prplane(pp), xyas(sx, sy, player->cnum),
351 plane_mob_max - pp->pln_mobil);
356 msl_equip(struct plnstr *pp, char mission)
361 pl.pstage = PLG_HEALTHY;
362 pl.pcp = plchr + pp->pln_type;
364 emp_initque(&pl.queue);
365 return pln_equip(&pl, NULL, mission);