]> git.pond.sub.org Git - empserver/blob - src/lib/commands/laun.c
a2fa234d83d5497bc579cb11c49e75f8d0f95f71
[empserver] / src / lib / commands / laun.c
1 /*
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
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-2009
34  */
35
36 #include <config.h>
37
38 #include "commands.h"
39 #include "damage.h"
40 #include "mission.h"
41 #include "news.h"
42 #include "nuke.h"
43 #include "optlist.h"
44 #include "path.h"
45 #include "plane.h"
46 #include "ship.h"
47
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);
52
53 /*
54  * laun <PLANES>
55  */
56 int
57 laun(void)
58 {
59     struct nstr_item nstr;
60     struct plnstr plane;
61     struct plchrstr *pcp;
62     int retval, gone;
63
64     if (!snxtitem(&nstr, EF_PLANE, player->argp[1], NULL))
65         return RET_SYN;
66     while (nxtitem(&nstr, &plane)) {
67         if (plane.pln_own != player->cnum)
68             continue;
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));
72             continue;
73         }
74         if (pcp->pl_flags & P_F) {
75             pr("%s is a surface-to-air missile!\n", prplane(&plane));
76             continue;
77         }
78         if (pcp->pl_flags & P_N) {
79             pr("%s is an anti-ballistic-missile missile!\n",
80                prplane(&plane));
81             continue;
82         }
83         if (pln_is_in_orbit(&plane)) {
84             pr("%s already in orbit!\n", prplane(&plane));
85             continue;
86         }
87         if (opt_MARKET) {
88             if (ontradingblock(EF_PLANE, &plane)) {
89                 pr("plane #%d inelligible - it's for sale.\n",
90                    plane.pln_uid);
91                 continue;
92             }
93         }
94
95         if (plane.pln_effic < 40) {
96             pr("%s is damaged (%d%%)\n", prplane(&plane), plane.pln_effic);
97             continue;
98         }
99         if (!pln_airbase_ok(&plane, 1, 1))
100             continue;
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);
106             gone = 1;
107         } else if ((pcp->pl_flags & (P_M | P_O)) == (P_M | P_O)) {
108             retval = launch_as(&plane);
109             gone = 1;
110         } else {                /* satellites */
111             retval = launch_sat(&plane);
112             gone = !(plane.pln_flags & PLN_LAUNCHED);
113         }
114         if (retval != RET_OK)
115             return retval;
116         if (gone) {
117             plane.pln_effic = 0;
118             putplane(plane.pln_uid, &plane);
119         }
120     }
121     return RET_OK;
122 }
123
124 /*
125  * Launch anti-sat weapon PP.
126  * Return RET_OK if launched (even when missile explodes),
127  * else RET_SYN or RET_FAIL.
128  */
129 static int
130 launch_as(struct plnstr *pp)
131 {
132     char *cp, buf[1024];
133     struct plnstr plane;
134
135     cp = getstarg(player->argp[2], "Target satellite? ", buf);
136     if (!check_plane_ok(pp))
137         return RET_FAIL;
138     if (!cp || !*cp)
139         return RET_SYN;
140     if (!getplane(atoi(cp), &plane) || !plane.pln_own
141         || !pln_is_in_orbit(&plane)) {
142         pr("No such satellite exists!\n");
143         return RET_FAIL;
144         /* Can be abused to find satellite ids.  Tolerable.  */
145     }
146
147     if (mapdist(pp->pln_x, pp->pln_y, plane.pln_x, plane.pln_y)
148         > pp->pln_range) {
149         pr("Range too great!\n");
150         return RET_FAIL;
151     }
152     if (msl_equip(pp, 'i') < 0)
153         return RET_FAIL;
154     if (msl_launch(pp, EF_PLANE, prplane(&plane),
155                    plane.pln_x, plane.pln_y, plane.pln_own, NULL) < 0)
156         return RET_OK;
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);
163         plane.pln_effic = 0;
164         putplane(plane.pln_uid, &plane);
165     }
166     return RET_OK;
167 }
168
169 /*
170  * Launch missile PP.
171  * Return RET_OK if launched (even when missile explodes),
172  * else RET_SYN or RET_FAIL.
173  */
174 static int
175 launch_missile(struct plnstr *pp)
176 {
177     struct plchrstr *pcp = plchr + pp->pln_type;
178     coord sx, sy;
179     int n, dam, sublaunch;
180     char *cp;
181     struct mchrstr *mcp;
182     struct shpstr target_ship;
183     struct sctstr sect;
184     struct nukstr nuke;
185     char buf[1024];
186
187     if (pcp->pl_flags & P_MAR)
188         cp = getstarg(player->argp[2], "Target ship? ", buf);
189     else
190         cp = getstarg(player->argp[2], "Target sector? ", buf);
191     if (!cp || !*cp)
192         return RET_SYN;
193     if (!check_plane_ok(pp))
194         return RET_FAIL;
195     if (sarg_type(cp) == NS_LIST) {
196         if (!(pcp->pl_flags & P_MAR)) {
197             pr("Missile not designed to attack ships!\n");
198             return RET_FAIL;
199         }
200         n = atoi(cp);
201         if ((n < 0) || !getship(n, &target_ship) || !target_ship.shp_own) {
202             pr("Bad ship number!\n");
203             return RET_FAIL;
204         }
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");
210             return RET_FAIL;
211         }
212     } else if (!sarg_xy(cp, &sx, &sy)) {
213         pr("Not a sector!\n");
214         return RET_FAIL;
215     } else {
216         if (pcp->pl_flags & P_MAR) {
217             pr("Missile designed to attack ships!\n");
218             return RET_FAIL;
219         }
220     }
221
222     if (mapdist(pp->pln_x, pp->pln_y, sx, sy) > pp->pln_range) {
223         pr("Range too great; try again!\n");
224         return RET_FAIL;
225     }
226     if (!(pcp->pl_flags & P_MAR)) {
227         if (msl_equip(pp, 's') < 0)
228             return RET_FAIL;
229         getsect(sx, sy, &sect);
230         if (msl_launch(pp, EF_SECTOR, "sector", sx, sy, sect.sct_own,
231                        &sublaunch) < 0)
232             return RET_OK;
233         getsect(sx, sy, &sect);
234         if (!msl_hit(pp, SECT_HARDTARGET, EF_SECTOR,
235                      N_SCT_MISS, N_SCT_SMISS, sublaunch, sect.sct_own))
236             CANT_REACH();
237         if (getnuke(nuk_on_plane(pp), &nuke))
238             detonate(&nuke, sx, sy, pp->pln_flags & PLN_AIRBURST);
239         else {
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) {
244                 if (sublaunch)
245                     wu(0, sect.sct_own,
246                        "Sub missile attack did %d damage in %s\n",
247                        dam, xyas(sx, sy, sect.sct_own));
248                 else
249                     wu(0, 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));
253             }
254             sectdamage(&sect, dam);
255             putsect(&sect);
256         }
257     } else {
258         if (msl_equip(pp, 'p') < 0)
259             return RET_FAIL;
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)
263             return RET_OK;
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)) {
268             pr("splash\n");
269             dam = pln_damage(pp, 'p', 0);
270             collateral_damage(target_ship.shp_x, target_ship.shp_y, dam);
271             return RET_OK;
272         }
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));
279     }
280     return RET_OK;
281 }
282
283 /*
284  * Launch a satellite.
285  * Return RET_OK if launched (even when satellite fails),
286  * else RET_SYN or RET_FAIL.
287  */
288 static int
289 launch_sat(struct plnstr *pp)
290 {
291     coord sx, sy;
292     int i;
293     int dist;
294     int dir;
295     char *cp;
296     char *p;
297     char buf[1024];
298
299     pr("\n");
300     cp = getstarg(player->argp[2], "Target sector? ", buf);
301     if (!check_plane_ok(pp))
302         return RET_FAIL;
303     if (!cp || !*cp)
304         return RET_SYN;
305     if (!sarg_xy(cp, &sx, &sy)) {
306         pr("Bad sector designation!\n");
307         return RET_SYN;
308     }
309     if ((dist = mapdist(pp->pln_x, pp->pln_y, sx, sy)) > pp->pln_range) {
310         pr("Range too great; try again!\n");
311         return RET_FAIL;
312     }
313     p = getstring("Geostationary orbit? ", buf);
314     if (!p)
315         return RET_SYN;
316     if (!check_plane_ok(pp))
317         return RET_FAIL;
318     if (msl_equip(pp, 'r') < 0)
319         return RET_FAIL;
320     pp->pln_theta = 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");
327         pp->pln_effic = 0;
328         return RET_OK;
329     }
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");
336     }
337     nreport(player->cnum, N_LAUNCH, 0, 1);
338     if (msl_asat_intercept(pp, sx, sy))
339         return RET_OK;
340     pp->pln_x = sx;
341     pp->pln_y = 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);
349     return RET_OK;
350 }
351
352 static int
353 msl_equip(struct plnstr *pp, char mission)
354 {
355     struct plist pl;
356
357     memset(&pl, 0, sizeof(struct plist));
358     pl.pcp = plchr + pp->pln_type;
359     pl.plane = *pp;
360     return pln_equip(&pl, NULL, mission);
361 }