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