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