]> git.pond.sub.org Git - empserver/blob - src/lib/commands/laun.c
retreat: Drop unused parameter @code
[empserver] / src / lib / commands / laun.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  laun.c: Launch missiles from land or sea
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Ken Stevens, 1995
32  *     Steve McClure, 1998-2000
33  *     Markus Armbruster, 2005-2021
34  */
35
36 #include <config.h>
37
38 #include "chance.h"
39 #include "commands.h"
40 #include "damage.h"
41 #include "mission.h"
42 #include "news.h"
43 #include "nuke.h"
44 #include "optlist.h"
45 #include "path.h"
46 #include "plague.h"
47 #include "plane.h"
48 #include "ship.h"
49
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);
54
55 /*
56  * laun <PLANES>
57  */
58 int
59 laun(void)
60 {
61     struct nstr_item nstr;
62     struct plnstr plane;
63     struct plchrstr *pcp;
64     int retval, gone;
65
66     if (!snxtitem(&nstr, EF_PLANE, player->argp[1], NULL))
67         return RET_SYN;
68     while (nxtitem(&nstr, &plane)) {
69         if (plane.pln_own != player->cnum)
70             continue;
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));
74             continue;
75         }
76         if (pcp->pl_flags & P_F) {
77             pr("%s is a surface-to-air missile!\n", prplane(&plane));
78             continue;
79         }
80         if (pcp->pl_flags & P_N) {
81             pr("%s is an anti-ballistic-missile missile!\n",
82                prplane(&plane));
83             continue;
84         }
85         if (pln_is_in_orbit(&plane)) {
86             pr("%s already in orbit!\n", prplane(&plane));
87             continue;
88         }
89         if (opt_MARKET) {
90             if (ontradingblock(EF_PLANE, &plane)) {
91                 pr("plane #%d ineligible - it's for sale.\n",
92                    plane.pln_uid);
93                 continue;
94             }
95         }
96
97         if (plane.pln_effic < 40) {
98             pr("%s is damaged (%d%%)\n", prplane(&plane), plane.pln_effic);
99             continue;
100         }
101         if (!pln_airbase_ok(&plane, 1, 1))
102             continue;
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);
108             gone = 1;
109         } else if ((pcp->pl_flags & (P_M | P_O)) == (P_M | P_O)) {
110             retval = launch_as(&plane);
111             gone = 1;
112         } else {                /* satellites */
113             retval = launch_sat(&plane);
114             gone = !(plane.pln_flags & PLN_LAUNCHED);
115         }
116         if (retval != RET_OK)
117             return retval;
118         if (gone) {
119             plane.pln_effic = 0;
120             putplane(plane.pln_uid, &plane);
121         }
122     }
123     return RET_OK;
124 }
125
126 /*
127  * Launch anti-sat weapon @pp.
128  * Return RET_OK if launched (even when missile explodes),
129  * else RET_SYN or RET_FAIL.
130  */
131 static int
132 launch_as(struct plnstr *pp)
133 {
134     char *cp, buf[1024];
135     struct plnstr plane;
136
137     cp = getstarg(player->argp[2], "Target satellite? ", buf);
138     if (!cp || !*cp)
139         return RET_SYN;
140     if (!check_plane_ok(pp))
141         return RET_FAIL;
142     if (!getplane(atoi(cp), &plane) || !plane.pln_own
143         || !pln_is_in_orbit(&plane)) {
144         pr("No such satellite exists!\n");
145         return RET_FAIL;
146         /* Can be abused to find satellite IDs.  Tolerable. */
147     }
148
149     if (mapdist(pp->pln_x, pp->pln_y, plane.pln_x, plane.pln_y)
150         > pp->pln_range) {
151         pr("Range too great!\n");
152         return RET_FAIL;
153     }
154     if (msl_equip(pp, 'i') < 0)
155         return RET_FAIL;
156     if (msl_launch(pp, EF_PLANE, prplane(&plane),
157                    plane.pln_x, plane.pln_y, plane.pln_own, NULL) < 0)
158         return RET_OK;
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);
165         plane.pln_effic = 0;
166         putplane(plane.pln_uid, &plane);
167     }
168     return RET_OK;
169 }
170
171 /*
172  * Launch missile @pp.
173  * Return RET_OK if launched (even when missile explodes),
174  * else RET_SYN or RET_FAIL.
175  */
176 static int
177 launch_missile(struct plnstr *pp)
178 {
179     struct plchrstr *pcp = plchr + pp->pln_type;
180     coord sx, sy;
181     int n, dam, sublaunch;
182     char *cp;
183     struct mchrstr *mcp;
184     struct shpstr target_ship;
185     struct sctstr sect;
186     struct nukstr nuke;
187     char buf[1024];
188
189     if (pcp->pl_flags & P_MAR)
190         cp = getstarg(player->argp[2], "Target ship? ", buf);
191     else
192         cp = getstarg(player->argp[2], "Target sector? ", buf);
193     if (!cp || !*cp)
194         return RET_SYN;
195     if (!check_plane_ok(pp))
196         return RET_FAIL;
197     if (sarg_type(cp) == NS_LIST) {
198         if (!(pcp->pl_flags & P_MAR)) {
199             pr("Missile not designed to attack ships!\n");
200             return RET_FAIL;
201         }
202         n = atoi(cp);
203         if ((n < 0) || !getship(n, &target_ship) || !target_ship.shp_own) {
204             pr("Bad ship number!\n");
205             return RET_FAIL;
206         }
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");
212             return RET_FAIL;
213         }
214     } else if (!sarg_xy(cp, &sx, &sy)) {
215         pr("Not a sector!\n");
216         return RET_FAIL;
217     } else {
218         if (pcp->pl_flags & P_MAR) {
219             pr("Missile designed to attack ships!\n");
220             return RET_FAIL;
221         }
222     }
223
224     if (mapdist(pp->pln_x, pp->pln_y, sx, sy) > pp->pln_range) {
225         pr("Range too great; try again!\n");
226         return RET_FAIL;
227     }
228     if (!(pcp->pl_flags & P_MAR)) {
229         if (msl_equip(pp, 's') < 0)
230             return RET_FAIL;
231         getsect(sx, sy, &sect);
232         if (msl_launch(pp, EF_SECTOR, "sector", sx, sy, sect.sct_own,
233                        &sublaunch) < 0)
234             return RET_OK;
235         getsect(sx, sy, &sect);
236         if (!msl_hit(pp, SECT_HARDTARGET, EF_SECTOR,
237                      N_SCT_MISS, N_SCT_SMISS, sublaunch, sect.sct_own))
238             CANT_REACH();
239         if (getnuke(nuk_on_plane(pp), &nuke))
240             detonate(&nuke, sx, sy, pp->pln_flags & PLN_AIRBURST);
241         else {
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) {
246                 if (sublaunch)
247                     wu(0, sect.sct_own,
248                        "Sub missile attack did %d damage in %s\n",
249                        dam, xyas(sx, sy, sect.sct_own));
250                 else
251                     wu(0, 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));
255             }
256             sectdamage(&sect, dam);
257             putsect(&sect);
258         }
259     } else {
260         if (msl_equip(pp, 'p') < 0)
261             return RET_FAIL;
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)
265             return RET_OK;
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)) {
270             pr("splash\n");
271             dam = pln_damage(pp, 'p', NULL);
272             collateral_damage(target_ship.shp_x, target_ship.shp_y, dam);
273             return RET_OK;
274         }
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);
281         putship(target_ship.shp_uid, &target_ship);
282     }
283     return RET_OK;
284 }
285
286 /*
287  * Launch satellite @pp.
288  * Return RET_OK if launched (even when satellite fails),
289  * else RET_SYN or RET_FAIL.
290  */
291 static int
292 launch_sat(struct plnstr *pp)
293 {
294     coord sx, sy;
295     int i;
296     int dist;
297     int dir;
298     char *cp;
299     char *p;
300     char buf[1024];
301
302     pr("\n");
303     cp = getstarg(player->argp[2], "Target sector? ", buf);
304     if (!cp || !*cp)
305         return RET_SYN;
306     if (!check_plane_ok(pp))
307         return RET_FAIL;
308     if (!sarg_xy(cp, &sx, &sy)) {
309         pr("Bad sector designation!\n");
310         return RET_SYN;
311     }
312     if ((dist = mapdist(pp->pln_x, pp->pln_y, sx, sy)) > pp->pln_range) {
313         pr("Range too great; try again!\n");
314         return RET_FAIL;
315     }
316     p = getstring("Geostationary orbit? ", buf);
317     if (!p)
318         return RET_SYN;
319     if (!check_plane_ok(pp))
320         return RET_FAIL;
321     if (msl_equip(pp, 'r') < 0)
322         return RET_FAIL;
323     pp->pln_theta = 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");
330         pp->pln_effic = 0;
331         return RET_OK;
332     }
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");
339     }
340     nreport(player->cnum, N_LAUNCH, 0, 1);
341     if (msl_asat_intercept(pp, sx, sy))
342         return RET_OK;
343     pp->pln_x = sx;
344     pp->pln_y = 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     pp->pln_ship = pp->pln_land = -1;
349     putplane(pp->pln_uid, pp);
350     pr("%s positioned over %s, will be ready for use in %d time units\n",
351        prplane(pp), xyas(sx, sy, player->cnum),
352        plane_mob_max - pp->pln_mobil);
353     return RET_OK;
354 }
355
356 static int
357 msl_equip(struct plnstr *pp, char mission)
358 {
359     struct plist pl;
360
361     pl.load = 0;
362     pl.pstage = PLG_HEALTHY;
363     pl.pcp = plchr + pp->pln_type;
364     pl.plane = *pp;
365     emp_initque(&pl.queue);
366     return pln_equip(&pl, NULL, mission);
367 }