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