]> git.pond.sub.org Git - empserver/blob - src/lib/commands/tran.c
Update copyright notice.
[empserver] / src / lib / commands / tran.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  *  tran.c: Transport nuclear devices and planes
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 2000
32  *  
33  */
34
35 #include <string.h>
36 #include "misc.h"
37 #include "player.h"
38 #include "var.h"
39 #include "sect.h"
40 #include "nuke.h"
41 #include "xy.h"
42 #include "nsc.h"
43 #include "plane.h"
44 #include "ship.h"
45 #include "file.h"
46 #include "nat.h"
47 #include "land.h"
48 #include "commands.h"
49
50 #include <stdio.h>
51
52 static int tran_map(s_char *what, coord curx, coord cury, s_char *arg);
53 static int tran_nuke(void);
54 static int tran_plane(void);
55
56 int
57 tran(void)
58 {
59     s_char *what;
60     s_char buf[1024];
61
62     what =
63         getstarg(player->argp[1], "transport what (nuke or plane): ", buf);
64     if (what == 0)
65         return RET_SYN;
66     if (*what == 'n')
67         return tran_nuke();
68     else if (*what == 'p')
69         return tran_plane();
70     return RET_SYN;
71 }
72
73 /*
74  * Kinda silly; only moves the first nuke.
75  * Maybe nukes should be made into commodities?
76  */
77 static int
78 tran_nuke(void)
79 {
80     struct nchrstr *ncp;
81     int len;
82     coord x, y;
83     coord dstx, dsty;
84     int found;
85     s_char *p;
86     int i;
87     int nuketype;
88     int moving;
89     struct nukstr nuke;
90     struct sctstr sect;
91     struct sctstr endsect;
92     int mcost, dam;
93     struct nstr_item nstr;
94     s_char buf[1024];
95
96     if (!(p = getstarg(player->argp[2], "from sector : ", buf)))
97         return RET_SYN;
98     if (!sarg_xy(p, &x, &y))
99         return RET_SYN;
100     if (!getsect(x, y, &sect) || !player->owner) {
101         pr("Not yours\n");
102         return RET_FAIL;
103     }
104     snxtitem_xy(&nstr, EF_NUKE, sect.sct_x, sect.sct_y);
105     found = 0;
106     while (nxtitem(&nstr, &nuke)) {
107         if (player->owner) {
108             found = 1;
109             break;
110         }
111     }
112     if (!found) {
113         pr("There are no nukes in %s\n",
114            xyas(sect.sct_x, sect.sct_y, player->cnum));
115         return RET_FAIL;
116     }
117     if (!(p = getstarg(player->argp[3], "warhead type : ", buf)))
118         return RET_SYN;
119     if (!check_sect_ok(&sect))
120         return RET_FAIL;
121     len = strlen(p);
122     for (i = 0, ncp = nchr; i < N_MAXNUKE; i++, ncp++) {
123         if (strncmp(ncp->n_name, p, len) == 0)
124             break;
125     }
126     if (i >= N_MAXNUKE) {
127         pr("No such nuke type!\n");
128         return RET_SYN;
129     }
130     nuketype = i;
131     if (!nuke.nuk_types[nuketype]) {
132         pr("No %s nukes in %s\n",
133            ncp->n_name, xyas(sect.sct_x, sect.sct_y, player->cnum));
134         return RET_FAIL;
135     }
136     p = getstarg(player->argp[4], "number of warheads : ", buf);
137     if (!check_sect_ok(&sect))
138         return RET_FAIL;
139     if (p == 0 || *p == 0 || (moving = atoi(p)) < 0)
140         return RET_FAIL;
141     if (moving > nuke.nuk_types[nuketype]) {
142         moving = nuke.nuk_types[nuketype];
143         if (moving)
144             pr("only moving %d\n", moving);
145         else
146             return RET_FAIL;
147     }
148     if (!military_control(&sect)) {
149         pr("Military control required to move nukes.\n");
150         return RET_FAIL;
151     }
152     dam = 0;
153     mcost = move_ground((s_char *)&nuke, &sect, &endsect,
154                         (double)ncp->n_weight * moving,
155                         player->argp[5], tran_map, 0, &dam);
156
157     if (mcost < 0)
158         return 0;
159
160     if (mcost > 0)
161         pr("Total movement cost = %d\n", mcost);
162     else
163         pr("No mobility used\n");
164
165     dstx = endsect.sct_x;
166     dsty = endsect.sct_y;
167     /*
168      * decrement mobility from src sector
169      */
170     getsect(nuke.nuk_x, nuke.nuk_y, &sect);
171     sect.sct_mobil -= mcost;
172     if (sect.sct_mobil < 0)
173         sect.sct_mobil = 0;
174     putsect(&sect);
175     /*
176      * update old nuke
177      */
178     if (!getnuke(nuke.nuk_uid, &nuke)) {
179         pr("Could not find that stockpile again.\n");
180         return RET_FAIL;
181     }
182     if (nuke.nuk_types[nuketype] < moving || nuke.nuk_own != player->cnum) {
183         pr("Stockpile changed!\n");
184         return RET_FAIL;
185     }
186     nuk_delete(&nuke, nuketype, moving);
187     nuk_add(dstx, dsty, nuketype, moving);
188     return RET_OK;
189 }
190
191 static int
192 tran_plane(void)
193 {
194     int srcx, srcy;
195     int dstx, dsty;
196     int mcost;
197     int weight, count;
198     int type, dam;
199     struct nstr_item nstr;
200     struct plnstr plane;
201     struct sctstr sect;
202     struct sctstr endsect;
203
204     weight = 0;
205     count = 0;
206     if (!snxtitem(&nstr, EF_PLANE, player->argp[2]))
207         return RET_SYN;
208     /*
209      * First do some sanity checks: make sure that they are all in the,
210      * same sector, not on ships, owned, etc.
211      * No one could seriously want to move planes in parallel from
212      * several sectors!
213      */
214     while (nxtitem(&nstr, (s_char *)&plane)) {
215         if (!player->owner)
216             continue;
217         type = plane.pln_type;
218         if (plane.pln_ship >= 0) {
219             pr("%s is at sea and can't be transported\n", prplane(&plane));
220             return RET_FAIL;
221         } else if (plane.pln_harden != 0) {
222             pr("%s has been hardened and can't be transported\n",
223                prplane(&plane));
224             return RET_FAIL;
225         } else if ((plane.pln_flags & PLN_LAUNCHED) &&
226                    (plchr[type].pl_flags & P_O)) {
227             pr("%s is in space and can't be transported\n",
228                prplane(&plane));
229             return RET_FAIL;
230         }
231         if (count == 0) {
232             srcx = plane.pln_x;
233             srcy = plane.pln_y;
234         } else {
235             if (plane.pln_x != srcx || plane.pln_y != srcy) {
236                 pr("All planes must be in the same sector.\n");
237                 return RET_FAIL;
238             }
239         }
240         weight += plchr[type].pl_lcm + (plchr[type].pl_hcm * 2);
241         ++count;
242     }
243     if (count == 0) {
244         pr("No planes\n");
245         return RET_FAIL;
246     }
247     if (!getsect(srcx, srcy, &sect) || !player->owner) {
248         pr("You don't own %s\n", xyas(srcx, srcy, player->cnum));
249         return RET_FAIL;
250     }
251     if (!military_control(&sect)) {
252         pr("Military control required to move planes.\n");
253         return RET_FAIL;
254     }
255     dam = 1;
256     mcost = move_ground((s_char *)&plane, &sect, &endsect,
257                         (double)weight,
258                         player->argp[3], tran_map, 0, &dam);
259     dam /= count;
260     if (mcost < 0)
261         return 0;
262
263     dstx = endsect.sct_x;
264     dsty = endsect.sct_y;
265     snxtitem_rewind(&nstr);
266     while (nxtitem(&nstr, (s_char *)&plane)) {
267         if (!player->owner)
268             continue;
269         if (dam)
270             planedamage(&plane, dam);
271         plane.pln_x = dstx;
272         plane.pln_y = dsty;
273         plane.pln_mission = 0;
274         putplane(plane.pln_uid, &plane);
275     }
276     if (mcost > 0)
277         pr("Total movement cost = %d\n", mcost);
278     else
279         pr("No mobility used\n");
280     getsect(srcx, srcy, &sect);
281     sect.sct_mobil -= mcost;
282     if (sect.sct_mobil < 0)
283         sect.sct_mobil = 0;
284     putsect(&sect);
285     return RET_OK;
286 }
287
288 /*
289  * Pretty tacky, but it works.
290  * If more commands start doing this, then
291  * rewrite map to do the right thing.
292  */
293 /* I think this is no longer used, check subs/move.c:move_ground() */
294 /*ARGSUSED*/
295 static int
296 tran_map(s_char *what, coord curx, coord cury, s_char *arg)
297 {
298     player->argp[1] = arg;
299     player->condarg = 0;
300     return map();
301 }