]> git.pond.sub.org Git - empserver/blob - src/lib/commands/arm.c
Remove a bunch of redundant casts.
[empserver] / src / lib / commands / arm.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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  */
35
36 #include <string.h>
37 #include "misc.h"
38 #include "player.h"
39 #include "nuke.h"
40 #include "plane.h"
41 #include "xy.h"
42 #include "nsc.h"
43 #include "file.h"
44 #include "commands.h"
45 #include "optlist.h"
46
47 int
48 arm(void)
49 {
50     struct nchrstr *ncp;
51     struct plchrstr *plc;
52     struct plnstr pl;
53     struct plnstr start;        /* Used for sanity checking */
54     struct nukstr nuke;
55     s_char *p;
56     int i;
57     int pno;
58     int nuketype;
59     int nukenum;
60     int len;
61     struct nstr_item ni;
62     s_char buf[1024];
63     int disarm = **player->argp == 'd';
64     s_char *prompt = disarm ? "Disarm plane: " : "Arm plane: ";
65
66     if (!(p = getstarg(player->argp[1], prompt, buf)) || !*p)
67         return RET_SYN;
68     pno = atoi(p);
69     if (pno < 0 || !getplane(pno, &pl) || pl.pln_own != player->cnum)
70         return RET_FAIL;
71     memcpy(&start, &pl, sizeof(struct plnstr));
72     plc = &plchr[(int)pl.pln_type];
73     if ((plc->pl_flags & (P_O | P_M)) == (P_O | P_M)) {
74         pr("A %s cannot carry nuclear devices!\n", plc->pl_name);
75         return RET_FAIL;
76     }
77     if (opt_MARKET) {
78         if (ontradingblock(EF_PLANE, (int *)&pl)) {
79             pr("You cannot arm/disarm an item on the trading block!\n");
80             return RET_FAIL;
81         }
82     }
83     if (pl.pln_nuketype == -1) {
84         if (disarm) {
85             pr("%s is not carrying any nuclear devices\n", prplane(&pl));
86             return RET_FAIL;
87         }
88         if ((p = getstarg(player->argp[2], "Device type: ", buf)) == 0)
89             return RET_SYN;
90         if (!check_plane_ok(&start))
91             return RET_FAIL;
92         len = strlen(p);
93         for (i = 0, ncp = nchr; i < N_MAXNUKE; i++, ncp++) {
94             if (strncmp(ncp->n_name, p, len) == 0)
95                 break;
96         }
97         if (i >= N_MAXNUKE) {
98             pr("No such nuke type!\n");
99             return RET_SYN;
100         }
101         nuketype = i;
102         nukenum = -1;
103         snxtitem_all(&ni, EF_NUKE);
104         while (nxtitem(&ni, &nuke)) {
105             if (nuke.nuk_own != player->cnum)
106                 continue;
107             if (nuke.nuk_x != pl.pln_x || nuke.nuk_y != pl.pln_y)
108                 continue;
109             nukenum = ni.cur;
110             break;
111         }
112         if (nukenum < 0) {
113             pr("You don't own any nukes in that sector.\n");
114             return RET_FAIL;
115         }
116         if (nuke.nuk_types[nuketype] == 0) {
117             pr("No nukes of that type in that sector.\n");
118             return RET_FAIL;
119         }
120         if (pl.pln_load < ncp->n_weight) {
121             pr("A %s cannot carry %s devices!\n", plc->pl_name,
122                ncp->n_name);
123             return RET_FAIL;
124         }
125         p = getstarg(player->argp[3], "Airburst [n]? ", buf);
126
127         if (!check_plane_ok(&start))
128             return RET_FAIL;
129
130         if (p && (*p == 'y' || *p == 'Y'))
131             pl.pln_flags |= PLN_AIRBURST;
132         else
133             pl.pln_flags &= ~PLN_AIRBURST;
134
135         pl.pln_nuketype = nuketype;
136         nuk_delete(&nuke, nuketype, 1);
137     } else if (!disarm) {
138         pr("%s already carrying a warhead.\n", prplane(&pl));
139     }
140     if (disarm) {
141         pr("%s warhead removed from %s and added to %s\n",
142            nchr[(int)pl.pln_nuketype].n_name,
143            prplane(&pl), xyas(pl.pln_x, pl.pln_y, player->cnum));
144         nuk_add(pl.pln_x, pl.pln_y, pl.pln_nuketype, 1);
145         pl.pln_nuketype = -1;
146         pl.pln_flags &= ~PLN_AIRBURST;
147     } else {
148         pr("%s armed with a %s warhead.\n", prplane(&pl),
149            nchr[(int)pl.pln_nuketype].n_name);
150         pr("Warhead on %s is programmed to %s\n",
151            prplane(&pl),
152            pl.pln_flags & PLN_AIRBURST ? "airburst" : "groundburst");
153     }
154
155     putplane(pl.pln_uid, &pl);
156     return RET_OK;
157 }