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