]> git.pond.sub.org Git - empserver/blob - src/lib/commands/laun.c
66a3552e1bd21caa9ec0df8b803fedb4b1c8f7ef
[empserver] / src / lib / commands / laun.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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  */
35
36 #include <config.h>
37
38 #include "commands.h"
39 #include "damage.h"
40 #include "mission.h"
41 #include "news.h"
42 #include "optlist.h"
43 #include "path.h"
44 #include "plane.h"
45 #include "ship.h"
46
47 static int launch_as(struct plnstr *pp);
48 static int launch_missile(struct plnstr *pp, int sublaunch);
49 static int launch_sat(struct plnstr *pp, int sublaunch);
50
51 /*
52  * laun <PLANES>
53  */
54 int
55 laun(void)
56 {
57     struct nstr_item nstr;
58     struct plnstr plane;
59     struct shpstr ship;
60     struct sctstr sect;
61     int sublaunch;
62     struct plchrstr *pcp;
63     int rel, retval, gone;
64     struct natstr *natp;
65
66     if (!snxtitem(&nstr, EF_PLANE, player->argp[1]))
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 ((plane.pln_flags & PLN_LAUNCHED) && (pcp->pl_flags & P_O)) {
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 inelligible - it's for sale.\n",
92                    plane.pln_uid);
93                 continue;
94             }
95         }
96
97         sublaunch = 0;
98         if (plane.pln_ship >= 0) {
99             getship(plane.pln_ship, &ship);
100             if (!ship.shp_own) {
101                 pr("%s: ship #%d was sunk!\n",
102                    prplane(&plane), ship.shp_uid);
103                 plane.pln_effic = 0;
104                 putplane(plane.pln_uid, &plane);
105                 continue;
106             }
107             natp = getnatp(ship.shp_own);
108             rel = getrel(natp, player->cnum);
109             if (ship.shp_own != player->cnum && rel != ALLIED) {
110                 pr("%s: you or an ally do not own ship #%d\n",
111                    prplane(&plane), ship.shp_uid);
112                 plane.pln_effic = 0;
113                 putplane(plane.pln_uid, &plane);
114                 continue;
115             }
116             if (mchr[(int)ship.shp_type].m_flags & M_SUB)
117                 sublaunch = 1;
118         } else {
119             sublaunch = 0;
120             getsect(plane.pln_x, plane.pln_y, &sect);
121             natp = getnatp(sect.sct_own);
122             rel = getrel(natp, player->cnum);
123             if (sect.sct_own && sect.sct_own != player->cnum
124                 && rel != ALLIED) {
125                 pr("%s: you or an ally do not own sector %s!\n",
126                    prplane(&plane), xyas(plane.pln_x, plane.pln_y,
127                                          player->cnum));
128                 continue;
129             }
130         }
131         if (plane.pln_effic < 60) {
132             pr("%s is damaged (%d%%)\n", prplane(&plane), plane.pln_effic);
133             continue;
134         }
135         pr("%s at %s; range %d, eff %d%%\n", prplane(&plane),
136            xyas(plane.pln_x, plane.pln_y, player->cnum),
137            plane.pln_range, plane.pln_effic);
138         if (!(pcp->pl_flags & P_O)) {
139             retval = launch_missile(&plane, sublaunch);
140             gone = 1;
141         } else if ((pcp->pl_flags & (P_M | P_O)) == (P_M | P_O)) {
142             retval = launch_as(&plane);
143             gone = 1;
144         } else {                /* satellites */
145             retval = launch_sat(&plane, sublaunch);
146             gone = !(plane.pln_flags & PLN_LAUNCHED);
147         }
148         if (retval != RET_OK)
149             return retval;
150         if (gone) {
151             plane.pln_effic = 0;
152             putplane(plane.pln_uid, &plane);
153         }
154     }
155     return RET_OK;
156 }
157
158 /*
159  * Launch anti-sat weapon PP.
160  * Return RET_OK if launched (even when missile explodes),
161  * else RET_SYN or RET_FAIL.
162  */
163 static int
164 launch_as(struct plnstr *pp)
165 {
166     coord sx, sy;
167     char *cp, buf[1024];
168     struct plnstr plane;
169     struct nstr_item ni;
170     int goodtarget;
171     int dam, nukedam;
172     natid oldown;
173
174     if (msl_equip(pp) < 0) {
175         pr("%s not enough petrol or shells!\n", prplane(pp));
176         return RET_FAIL;
177     }
178     cp = getstarg(player->argp[2], "Target sector? ", buf);
179     if (!check_plane_ok(pp))
180         return RET_FAIL;
181     if (!cp || !*cp)
182         return RET_SYN;
183     if (!sarg_xy(cp, &sx, &sy)) {
184         pr("Bad sector designation!\n");
185         return RET_SYN;
186     }
187     if (mapdist(pp->pln_x, pp->pln_y, sx, sy) > pp->pln_range) {
188         pr("Range too great!\n");
189         return RET_FAIL;
190     }
191     goodtarget = 0;
192     snxtitem_dist(&ni, EF_PLANE, sx, sy, 0);
193     while (!goodtarget && nxtitem(&ni, &plane)) {
194         if (!plane.pln_own)
195             continue;
196         if (!(plane.pln_flags & PLN_LAUNCHED))
197             continue;
198         goodtarget = 1;
199
200     }
201     if (!goodtarget) {
202         pr("No satellites there!\n");
203         return RET_FAIL;
204     }
205     if (msl_hit(pp, plane.pln_def, EF_PLANE, N_SAT_KILL, N_SAT_KILL,
206                 prplane(&plane), sx, sy, plane.pln_own)) {
207         dam = pln_damage(pp, sx, sy, 'p', &nukedam, 1);
208         oldown = plane.pln_own;
209         planedamage(&plane, dam);
210         pr("Hit satellite for %d%% damage!\n", dam);
211         mpr(oldown, "%s anti-sat did %d%% damage to %s over %s\n",
212             cname(player->cnum), dam, prplane(&plane),
213             xyas(plane.pln_x, plane.pln_y, plane.pln_own));
214         putplane(plane.pln_uid, &plane);
215         if (!plane.pln_own)
216             mpr(oldown, "Satellite shot down\n");
217     }
218     return RET_OK;
219 }
220
221 /*
222  * Launch missile PP.
223  * If SUBLAUNCH, it's sub-launched.
224  * Return RET_OK if launched (even when missile explodes),
225  * else RET_SYN or RET_FAIL.
226  */
227 static int
228 launch_missile(struct plnstr *pp, int sublaunch)
229 {
230     struct plchrstr *pcp = plchr + pp->pln_type;
231     coord sx, sy;
232     int n, dam;
233     char *cp;
234     struct mchrstr *mcp;
235     struct shpstr target_ship;
236     struct sctstr sect;
237     int nukedam;
238     int rel;
239     struct natstr *natp;
240     char buf[1024];
241
242     if (pcp->pl_flags & P_MAR)
243         cp = getstarg(player->argp[2], "Target ship? ", buf);
244     else
245         cp = getstarg(player->argp[2], "Target sector? ", buf);
246     if (!cp || !*cp)
247         return RET_SYN;
248     if (!check_plane_ok(pp))
249         return RET_FAIL;
250     if (opt_PINPOINTMISSILE && sarg_type(cp) == NS_LIST) {
251         if (!(pcp->pl_flags & P_MAR)) {
252             pr("Missile not designed to attack ships!\n");
253             return RET_FAIL;
254         }
255         n = atoi(cp);
256         if ((n < 0) || !getship(n, &target_ship) ||
257             !target_ship.shp_own) {
258             pr("Bad ship number!\n");
259             return RET_FAIL;
260         }
261         sx = target_ship.shp_x;
262         sy = target_ship.shp_y;
263         mcp = &mchr[(int)target_ship.shp_type];
264         if (mcp->m_flags & M_SUB) {
265             pr("Bad ship number!\n");
266             return RET_FAIL;
267         }
268     } /* not PINPOINTMISSILE for ships */
269     else if (!sarg_xy(cp, &sx, &sy)) {
270         pr("Not a sector!\n");
271         return RET_FAIL;
272     } else if (opt_PINPOINTMISSILE) {
273         if (pcp->pl_flags & P_MAR) {
274             pr("Missile designed to attack ships!\n");
275             return RET_FAIL;
276         }
277     }
278     /* end PINPOINTMISSILE */
279     if (mapdist(pp->pln_x, pp->pln_y, sx, sy) > pp->pln_range) {
280         pr("Range too great; try again!\n");
281         return RET_FAIL;
282     }
283     if (msl_equip(pp) < 0) {
284         pr("%s not enough shells!\n", prplane(pp));
285         return RET_FAIL;
286     }
287     if (opt_PINPOINTMISSILE == 0 || !(pcp->pl_flags & P_MAR)) {
288         getsect(sx, sy, &sect);
289         if (opt_SLOW_WAR) {
290             natp = getnatp(player->cnum);
291             rel = getrel(natp, sect.sct_own);
292             if ((rel != AT_WAR) && (sect.sct_own != player->cnum) &&
293                 (sect.sct_own) && (sect.sct_oldown != player->cnum)) {
294                 pr("You are not at war with the player->owner of the target sector!\n");
295                 pr_beep();
296                 pr("Kaboom!!!\n");
297                 pr("Missile monitoring officer destroys RV before detonation.\n");
298                 return RET_OK;
299             }
300         }
301         if (!msl_hit(pp, SECT_HARDTARGET, EF_SECTOR, N_SCT_MISS,
302                      N_SCT_SMISS, "sector", sx, sy, sect.sct_own)) {
303             /*
304                dam = pln_damage(pp, sect.sct_x, sect.sct_y, 's', &nukedam, 0);
305                collateral_damage(sect.sct_x, sect.sct_y, dam, 0);
306              */
307             return RET_OK;
308         }
309         dam = pln_damage(pp, sect.sct_x, sect.sct_y, 's', &nukedam, 1);
310         if (!nukedam) {
311             pr("did %d damage in %s\n", PERCENT_DAMAGE(dam),
312                xyas(sx, sy, player->cnum));
313             if (sect.sct_own != 0) {
314                 if (sublaunch)
315                     wu(0, sect.sct_own,
316                        "Sub missile attack did %d damage in %s\n",
317                        dam, xyas(sx, sy, sect.sct_own));
318                 else
319                     wu(0, sect.sct_own,
320                        "%s missile attack did %d damage in %s\n",
321                        cname(player->cnum), dam,
322                        xyas(sx, sy, sect.sct_own));
323             }
324             sectdamage(&sect, dam, 0);
325             putsect(&sect);
326         }
327     } /* end PINPOINTMISSILE conditional */
328     else if (opt_PINPOINTMISSILE) {     /* else */
329         if (!msl_hit(pp, shp_hardtarget(&target_ship), EF_SHIP,
330                      N_SHP_MISS, N_SHP_SMISS, prship(&target_ship),
331                      target_ship.shp_x, target_ship.shp_y,
332                      target_ship.shp_own)) {
333             pr("splash\n");
334             /*
335                dam = pln_damage(pp,target_ship.shp_x,target_ship.shp_y,'p',&nukedam, 0);
336                collateral_damage(target_ship.shp_x, target_ship.shp_y, dam, 0);
337              */
338             return RET_OK;
339         }
340         dam =
341             pln_damage(pp, target_ship.shp_x, target_ship.shp_y, 'p',
342                        &nukedam, 1);
343         if (!nukedam) {
344             check_retreat_and_do_shipdamage(&target_ship, dam);
345             if (target_ship.shp_effic < SHIP_MINEFF)
346                 pr("\t%s sunk!\n", prship(&target_ship));
347             putship(target_ship.shp_uid, &target_ship);
348         }
349         getship(target_ship.shp_uid, &target_ship);
350         if (!target_ship.shp_own)
351             pr("%s sunk!\n", prship(&target_ship));
352     }
353     /* end PINPOINTMISSILE */
354     return RET_OK;
355 }
356
357 /*
358  * Launch a satellite.
359  * Return RET_OK if launched (even when satellite fails),
360  * else RET_SYN or RET_FAIL.
361  */
362 static int
363 launch_sat(struct plnstr *pp, int sublaunch)
364 {
365     struct plchrstr *pcp = plchr + pp->pln_type;
366     coord sx, sy;
367     int i;
368     int dist;
369     int dir;
370     char *cp;
371     char *p;
372     char buf[1024];
373
374     pr("\n");
375     cp = getstarg(player->argp[2], "Target sector? ", buf);
376     if (!check_plane_ok(pp))
377         return RET_FAIL;
378     if (!cp || !*cp)
379         return RET_SYN;
380     if (!sarg_xy(cp, &sx, &sy)) {
381         pr("Bad sector designation!\n");
382         return RET_SYN;
383     }
384     if ((dist = mapdist(pp->pln_x, pp->pln_y, sx, sy)) > pp->pln_range) {
385         pr("Range too great; try again!\n");
386         return RET_FAIL;
387     }
388     p = getstring("Geostationary orbit? ", buf);
389     if (p == 0)
390         return RET_SYN;
391     if (!check_plane_ok(pp))
392         return RET_FAIL;
393     pp->pln_theta = 0;
394     pp->pln_flags |= PLN_SYNCHRONOUS;
395     if (*p == 0 || *p == 'n')
396         pp->pln_flags &= ~(PLN_SYNCHRONOUS);
397     pr("3... 2... 1... Blastoff!!!\n");
398     if (chance(0.07 + (100 - pp->pln_effic) / 100.0)) {
399         pr("KABOOOOM!  Range safety officer detonates booster!\n");
400         pp->pln_effic = 0;
401         return RET_OK;
402     }
403     i = pp->pln_tech + pp->pln_effic;
404     if (chance(1.0 - (i / (i + 50.0)))) {
405         dir = (random() % 6) + 1;
406         sx += diroff[dir][0];
407         sy += diroff[dir][1];
408         pr("Your trajectory was a little off.\n");
409     }
410     nreport(player->cnum, N_LAUNCH, 0, 1);
411     pr("%s positioned over %s", prplane(pp), xyas(sx, sy, player->cnum));
412     if (msl_intercept(sx, sy, pp->pln_own, pcp->pl_def, sublaunch, P_O, 0)) {
413         return RET_OK;
414     }
415     pp->pln_x = sx;
416     pp->pln_y = sy;
417     pp->pln_flags |= PLN_LAUNCHED;
418     pp->pln_mobil = pp->pln_mobil > dist ? pp->pln_mobil - dist : 0;
419     putplane(pp->pln_uid, pp);
420     pr(", will be ready for use in %d time units\n",
421        plane_mob_max - pp->pln_mobil);
422     return RET_OK;
423 }