]> git.pond.sub.org Git - empserver/blob - src/lib/commands/arm.c
Fix recently changed command failures to use BTUs
[empserver] / src / lib / commands / arm.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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  *  arm.c: Arm planes (missiles) with nuclear devices
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Ken Stevens, 1995
33  *     Steve McClure, 2000
34  *     Markus Armbruster, 2006-2008
35  */
36
37 #include <config.h>
38
39 #include <stdio.h>
40 #include "commands.h"
41 #include "nuke.h"
42 #include "optlist.h"
43 #include "plane.h"
44
45 int
46 arm(void)
47 {
48     struct nchrstr *ncp;
49     struct plchrstr *plc;
50     struct plnstr pl;
51     struct nukstr nuke;
52     char *p;
53     int nukno;
54     struct nstr_item ni;
55     char buf[1024];
56     char prompt[128];
57
58     if (!snxtitem(&ni, EF_PLANE, player->argp[1], NULL))
59         return RET_SYN;
60     while (nxtitem(&ni, &pl)) {
61         if (!player->owner
62             && getrel(getnatp(pl.pln_own), player->cnum) != ALLIED)
63             continue;
64         plc = &plchr[(int)pl.pln_type];
65         if ((plc->pl_flags & (P_O | P_M)) == (P_O | P_M)) {
66             pr("A %s cannot carry nuclear devices!\n", plc->pl_name);
67             return RET_FAIL;
68         }
69         if (opt_MARKET) {
70             if (ontradingblock(EF_PLANE, &pl)) {
71                 pr("You cannot arm %s while it is on the trading block!\n",
72                    prplane(&pl));
73                 return RET_FAIL;
74             }
75         }
76         if (pl.pln_nuketype < 0) {
77             sprintf(prompt, "Nuclear device for %s: ", prplane(&pl));
78             p = getstarg(player->argp[2], prompt, buf);
79             if (!p || !*p)
80                 return RET_SYN;
81             if (!check_plane_ok(&pl))
82                 return RET_FAIL;
83             nukno = atoi(p);
84             if (!getnuke(nukno, &nuke) || !player->owner)
85                 return RET_FAIL;
86         } else {
87             if (nuk_on_plane(&nuke, pl.pln_uid) < 0) {
88                 CANT_REACH();
89                 continue;
90             }
91         }
92         ncp = &nchr[nuke.nuk_type];
93         if (pln_load(&pl) < ncp->n_weight) {
94             pr("A %s cannot carry %s devices!\n",
95                plc->pl_name, ncp->n_name);
96             return RET_FAIL;
97         }
98         p = getstarg(player->argp[3], "Airburst [n]? ", buf);
99         if (!p)
100             return RET_FAIL;
101
102         if (!check_plane_ok(&pl) || !check_nuke_ok(&nuke))
103             return RET_FAIL;
104
105         if (nuke.nuk_plane >= 0 && nuke.nuk_plane != pl.pln_uid) {
106             pr("%s is already armed on plane #%d!\n",
107                prnuke(&nuke), nuke.nuk_plane);
108             return RET_FAIL;
109         }
110
111         if (*p == 'y' || *p == 'Y')
112             pl.pln_flags |= PLN_AIRBURST;
113         else
114             pl.pln_flags &= ~PLN_AIRBURST;
115
116         snprintf(buf, sizeof(buf), "armed on your %s in %s",
117                  prplane(&pl), xyas(pl.pln_x, pl.pln_y, pl.pln_own));
118         gift(pl.pln_own, player->cnum, &nuke, buf);
119         pl.pln_nuketype = nuke.nuk_type;
120         nuke.nuk_plane = pl.pln_uid;
121         putplane(pl.pln_uid, &pl);
122         putnuke(nuke.nuk_uid, &nuke);
123         pr("%s armed with %s.\n", prplane(&pl), prnuke(&nuke));
124         pr("Warhead on %s is programmed to %s\n",
125            prplane(&pl),
126            pl.pln_flags & PLN_AIRBURST ? "airburst" : "groundburst");
127     }
128
129     return RET_OK;
130 }
131
132 int
133 disarm(void)
134 {
135     struct plnstr pl;
136     struct nukstr nuke;
137     struct nstr_item ni;
138     struct sctstr sect;
139     char buf[128];
140
141     if (!snxtitem(&ni, EF_PLANE, player->argp[1], NULL))
142         return RET_SYN;
143     while (nxtitem(&ni, &pl)) {
144         if (!player->owner)
145             continue;
146         if (pl.pln_nuketype == -1)
147             continue;
148         if (opt_MARKET) {
149             if (ontradingblock(EF_PLANE, &pl)) {
150                 pr("You cannot disarm %s while it is on the trading block!\n",
151                    prplane(&pl));
152                 return RET_FAIL;
153             }
154         }
155         if (nuk_on_plane(&nuke, pl.pln_uid) < 0) {
156             CANT_REACH();
157             continue;
158         }
159         getsect(nuke.nuk_x, nuke.nuk_y, &sect);
160         if (!player->owner
161             && getrel(getnatp(sect.sct_own), player->cnum) != ALLIED) {
162             pr("Disarming %s in sector %s requires an alliance!\n",
163                prplane(&pl), xyas(sect.sct_x, sect.sct_y, player->cnum));
164             continue;
165         }
166         snprintf(buf, sizeof(buf), "unloaded in your %s at %s",
167                  dchr[sect.sct_type].d_name,
168                  xyas(sect.sct_x, sect.sct_y, sect.sct_own));
169         gift(sect.sct_own, player->cnum, &nuke, buf);
170         nuke.nuk_plane = -1;
171         pl.pln_nuketype = -1;
172         pl.pln_flags &= ~PLN_AIRBURST;
173         putplane(pl.pln_uid, &pl);
174         putnuke(nuke.nuk_uid, &nuke);
175         pr("%s removed from %s and added to %s\n",
176            prnuke(&nuke), prplane(&pl),
177            xyas(pl.pln_x, pl.pln_y, player->cnum));
178     }
179     return RET_OK;
180 }