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