]> git.pond.sub.org Git - empserver/blob - src/lib/commands/laun.c
dac323240bc94560bdfc8d25a5f8bb0a9dbaba5f
[empserver] / src / lib / commands / laun.c
1 /*
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
5  *
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.
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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  laun.c: Launch missiles from land or sea
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Ken Stevens, 1995
33  *     Steve McClure, 1998-2000
34  *     Markus Armbruster, 2005-2009
35  */
36
37 #include <config.h>
38
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 "plane.h"
47 #include "ship.h"
48
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);
53
54 /*
55  * laun <PLANES>
56  */
57 int
58 laun(void)
59 {
60     struct nstr_item nstr;
61     struct plnstr plane;
62     struct plchrstr *pcp;
63     int retval, gone;
64
65     if (!snxtitem(&nstr, EF_PLANE, player->argp[1], NULL))
66         return RET_SYN;
67     while (nxtitem(&nstr, &plane)) {
68         if (plane.pln_own != player->cnum)
69             continue;
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));
73             continue;
74         }
75         if (pcp->pl_flags & P_F) {
76             pr("%s is a surface-to-air missile!\n", prplane(&plane));
77             continue;
78         }
79         if (pcp->pl_flags & P_N) {
80             pr("%s is an anti-ballistic-missile missile!\n",
81                prplane(&plane));
82             continue;
83         }
84         if (pln_is_in_orbit(&plane)) {
85             pr("%s already in orbit!\n", prplane(&plane));
86             continue;
87         }
88         if (opt_MARKET) {
89             if (ontradingblock(EF_PLANE, &plane)) {
90                 pr("plane #%d inelligible - it's for sale.\n",
91                    plane.pln_uid);
92                 continue;
93             }
94         }
95
96         if (plane.pln_effic < 40) {
97             pr("%s is damaged (%d%%)\n", prplane(&plane), plane.pln_effic);
98             continue;
99         }
100         if (!pln_airbase_ok(&plane, 1, 1))
101             continue;
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);
107             gone = 1;
108         } else if ((pcp->pl_flags & (P_M | P_O)) == (P_M | P_O)) {
109             retval = launch_as(&plane);
110             gone = 1;
111         } else {                /* satellites */
112             retval = launch_sat(&plane);
113             gone = !(plane.pln_flags & PLN_LAUNCHED);
114         }
115         if (retval != RET_OK)
116             return retval;
117         if (gone) {
118             plane.pln_effic = 0;
119             putplane(plane.pln_uid, &plane);
120         }
121     }
122     return RET_OK;
123 }
124
125 /*
126  * Launch anti-sat weapon PP.
127  * Return RET_OK if launched (even when missile explodes),
128  * else RET_SYN or RET_FAIL.
129  */
130 static int
131 launch_as(struct plnstr *pp)
132 {
133     char *cp, buf[1024];
134     struct plnstr plane;
135
136     cp = getstarg(player->argp[2], "Target satellite? ", buf);
137     if (!check_plane_ok(pp))
138         return RET_FAIL;
139     if (!cp || !*cp)
140         return RET_SYN;
141     if (!getplane(atoi(cp), &plane) || !plane.pln_own
142         || !pln_is_in_orbit(&plane)) {
143         pr("No such satellite exists!\n");
144         return RET_FAIL;
145         /* Can be abused to find satellite ids.  Tolerable.  */
146     }
147
148     if (mapdist(pp->pln_x, pp->pln_y, plane.pln_x, plane.pln_y)
149         > pp->pln_range) {
150         pr("Range too great!\n");
151         return RET_FAIL;
152     }
153     if (msl_equip(pp, 'i') < 0)
154         return RET_FAIL;
155     if (msl_launch(pp, EF_PLANE, prplane(&plane),
156                    plane.pln_x, plane.pln_y, plane.pln_own, NULL) < 0)
157         return RET_OK;
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);
164         plane.pln_effic = 0;
165         putplane(plane.pln_uid, &plane);
166     }
167     return RET_OK;
168 }
169
170 /*
171  * Launch missile PP.
172  * Return RET_OK if launched (even when missile explodes),
173  * else RET_SYN or RET_FAIL.
174  */
175 static int
176 launch_missile(struct plnstr *pp)
177 {
178     struct plchrstr *pcp = plchr + pp->pln_type;
179     coord sx, sy;
180     int n, dam, sublaunch;
181     char *cp;
182     struct mchrstr *mcp;
183     struct shpstr target_ship;
184     struct sctstr sect;
185     int rel;
186     struct natstr *natp;
187     struct nukstr nuke;
188     char buf[1024];
189
190     if (pcp->pl_flags & P_MAR)
191         cp = getstarg(player->argp[2], "Target ship? ", buf);
192     else
193         cp = getstarg(player->argp[2], "Target sector? ", buf);
194     if (!cp || !*cp)
195         return RET_SYN;
196     if (!check_plane_ok(pp))
197         return RET_FAIL;
198     if (sarg_type(cp) == NS_LIST) {
199         if (!(pcp->pl_flags & P_MAR)) {
200             pr("Missile not designed to attack ships!\n");
201             return RET_FAIL;
202         }
203         n = atoi(cp);
204         if ((n < 0) || !getship(n, &target_ship) || !target_ship.shp_own) {
205             pr("Bad ship number!\n");
206             return RET_FAIL;
207         }
208         sx = target_ship.shp_x;
209         sy = target_ship.shp_y;
210         mcp = &mchr[(int)target_ship.shp_type];
211         if (mcp->m_flags & M_SUB) {
212             pr("Bad ship number!\n");
213             return RET_FAIL;
214         }
215     } else if (!sarg_xy(cp, &sx, &sy)) {
216         pr("Not a sector!\n");
217         return RET_FAIL;
218     } else {
219         if (pcp->pl_flags & P_MAR) {
220             pr("Missile designed to attack ships!\n");
221             return RET_FAIL;
222         }
223     }
224
225     if (mapdist(pp->pln_x, pp->pln_y, sx, sy) > pp->pln_range) {
226         pr("Range too great; try again!\n");
227         return RET_FAIL;
228     }
229     if (!(pcp->pl_flags & P_MAR)) {
230         if (msl_equip(pp, 's') < 0)
231             return RET_FAIL;
232         getsect(sx, sy, &sect);
233         if (opt_SLOW_WAR) {
234             natp = getnatp(player->cnum);
235             rel = getrel(natp, sect.sct_own);
236             if ((rel != AT_WAR) && (sect.sct_own != player->cnum) &&
237                 (sect.sct_own) && (sect.sct_oldown != player->cnum)) {
238                 pr("You are not at war with the player->owner of the target sector!\n");
239                 pr_beep();
240                 pr("Kaboom!!!\n");
241                 pr("Missile monitoring officer destroys RV before detonation.\n");
242                 return RET_OK;
243             }
244         }
245         if (msl_launch(pp, EF_SECTOR, "sector", sx, sy, sect.sct_own,
246                        &sublaunch) < 0)
247             return RET_OK;
248         getsect(sx, sy, &sect);
249         if (!msl_hit(pp, SECT_HARDTARGET, EF_SECTOR,
250                      N_SCT_MISS, N_SCT_SMISS, sublaunch, sect.sct_own))
251             CANT_REACH();
252         if (getnuke(nuk_on_plane(pp), &nuke))
253             detonate(&nuke, sx, sy, pp->pln_flags & PLN_AIRBURST);
254         else {
255             dam = pln_damage(pp, 's', 1);
256             pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
257                xyas(sx, sy, player->cnum));
258             if (sect.sct_own != 0) {
259                 if (sublaunch)
260                     wu(0, sect.sct_own,
261                        "Sub missile attack did %d damage in %s\n",
262                        dam, xyas(sx, sy, sect.sct_own));
263                 else
264                     wu(0, sect.sct_own,
265                        "%s missile attack did %d damage in %s\n",
266                        cname(player->cnum), dam,
267                        xyas(sx, sy, sect.sct_own));
268             }
269             sectdamage(&sect, dam);
270             putsect(&sect);
271         }
272     } else {
273         if (msl_equip(pp, 'p') < 0)
274             return RET_FAIL;
275         if (msl_launch(pp, EF_SHIP, prship(&target_ship),
276                        target_ship.shp_x, target_ship.shp_y,
277                        target_ship.shp_own, &sublaunch) < 0)
278             return RET_OK;
279         getship(n, &target_ship);
280         if (!msl_hit(pp, shp_hardtarget(&target_ship), EF_SHIP,
281                      N_SHP_MISS, N_SHP_SMISS, sublaunch,
282                      target_ship.shp_own)) {
283             pr("splash\n");
284             dam = pln_damage(pp, 'p', 0);
285             collateral_damage(target_ship.shp_x, target_ship.shp_y, dam);
286             return RET_OK;
287         }
288         dam = pln_damage(pp, 'p', 1);
289         check_retreat_and_do_shipdamage(&target_ship, dam);
290         putship(target_ship.shp_uid, &target_ship);
291         getship(target_ship.shp_uid, &target_ship);
292         if (!target_ship.shp_own)
293             pr("%s sunk!\n", prship(&target_ship));
294     }
295     return RET_OK;
296 }
297
298 /*
299  * Launch a satellite.
300  * Return RET_OK if launched (even when satellite fails),
301  * else RET_SYN or RET_FAIL.
302  */
303 static int
304 launch_sat(struct plnstr *pp)
305 {
306     coord sx, sy;
307     int i;
308     int dist;
309     int dir;
310     char *cp;
311     char *p;
312     char buf[1024];
313
314     pr("\n");
315     cp = getstarg(player->argp[2], "Target sector? ", buf);
316     if (!check_plane_ok(pp))
317         return RET_FAIL;
318     if (!cp || !*cp)
319         return RET_SYN;
320     if (!sarg_xy(cp, &sx, &sy)) {
321         pr("Bad sector designation!\n");
322         return RET_SYN;
323     }
324     if ((dist = mapdist(pp->pln_x, pp->pln_y, sx, sy)) > pp->pln_range) {
325         pr("Range too great; try again!\n");
326         return RET_FAIL;
327     }
328     p = getstring("Geostationary orbit? ", buf);
329     if (!p)
330         return RET_SYN;
331     if (!check_plane_ok(pp))
332         return RET_FAIL;
333     if (msl_equip(pp, 'r') < 0)
334         return RET_FAIL;
335     pp->pln_theta = 0;
336     pp->pln_flags |= PLN_SYNCHRONOUS;
337     if (*p == 0 || *p == 'n')
338         pp->pln_flags &= ~(PLN_SYNCHRONOUS);
339     pr("3... 2... 1... Blastoff!!!\n");
340     if (chance(0.07 + (100 - pp->pln_effic) / 100.0)) {
341         pr("KABOOOOM!  Range safety officer detonates booster!\n");
342         pp->pln_effic = 0;
343         return RET_OK;
344     }
345     i = pp->pln_tech + pp->pln_effic;
346     if (chance(1.0 - (i / (i + 50.0)))) {
347         dir = (random() % 6) + 1;
348         sx = xnorm(sx + diroff[dir][0]);
349         sy = ynorm(sy + diroff[dir][1]);
350         pr("Your trajectory was a little off.\n");
351     }
352     nreport(player->cnum, N_LAUNCH, 0, 1);
353     if (msl_asat_intercept(pp, sx, sy))
354         return RET_OK;
355     pp->pln_x = sx;
356     pp->pln_y = sy;
357     CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED);
358     pp->pln_flags |= PLN_LAUNCHED;
359     pp->pln_mobil = pp->pln_mobil > dist ? pp->pln_mobil - dist : 0;
360     putplane(pp->pln_uid, pp);
361     pr("%s positioned over %s, will be ready for use in %d time units\n",
362        prplane(pp), xyas(sx, sy, player->cnum),
363        plane_mob_max - pp->pln_mobil);
364     return RET_OK;
365 }
366
367 static int
368 msl_equip(struct plnstr *pp, char mission)
369 {
370     struct plist pl;
371
372     memset(&pl, 0, sizeof(struct plist));
373     pl.pcp = plchr + pp->pln_type;
374     pl.plane = *pp;
375     return pln_equip(&pl, NULL, mission);
376 }