]> git.pond.sub.org Git - empserver/blob - src/lib/commands/tran.c
COPYING duplicates information from README. Remove. Move GPL from
[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(s_char *what, coord curx, coord cury, s_char *arg);
52 static int tran_nuke(void);
53 static int tran_plane(void);
54
55 int
56 tran(void)
57 {
58     s_char *what;
59     s_char buf[1024];
60
61     what =
62         getstarg(player->argp[1], "transport what (nuke or plane): ", 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     s_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     s_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((s_char *)&nuke, &sect, &endsect,
147                         (double)ncp->n_weight * moving,
148                         player->argp[5], tran_map, 0, &dam);
149
150     if (mcost < 0)
151         return 0;
152
153     if (mcost > 0)
154         pr("Total movement cost = %d\n", mcost);
155     else
156         pr("No mobility used\n");
157
158     dstx = endsect.sct_x;
159     dsty = endsect.sct_y;
160     /*
161      * decrement mobility from src sector
162      */
163     getsect(nuke.nuk_x, nuke.nuk_y, &sect);
164     sect.sct_mobil -= mcost;
165     if (sect.sct_mobil < 0)
166         sect.sct_mobil = 0;
167     putsect(&sect);
168     /*
169      * update old nuke
170      */
171     if (!getnuke(nuke.nuk_uid, &nuke)) {
172         pr("Could not find that stockpile again.\n");
173         return RET_FAIL;
174     }
175     if (nuke.nuk_types[nuketype] < moving || nuke.nuk_own != player->cnum) {
176         pr("Stockpile changed!\n");
177         return RET_FAIL;
178     }
179     nuk_delete(&nuke, nuketype, moving);
180     nuk_add(dstx, dsty, nuketype, moving);
181     return RET_OK;
182 }
183
184 static int
185 tran_plane(void)
186 {
187     int srcx, srcy;
188     int dstx, dsty;
189     int mcost;
190     int weight, count;
191     int type, dam;
192     struct nstr_item nstr;
193     struct plnstr plane;
194     struct sctstr sect;
195     struct sctstr endsect;
196
197     weight = 0;
198     count = 0;
199     if (!snxtitem(&nstr, EF_PLANE, player->argp[2]))
200         return RET_SYN;
201     /*
202      * First do some sanity checks: make sure that they are all in the,
203      * same sector, not on ships, owned, etc.
204      * No one could seriously want to move planes in parallel from
205      * several sectors!
206      */
207     while (nxtitem(&nstr, &plane)) {
208         if (!player->owner)
209             continue;
210         type = plane.pln_type;
211         if (plane.pln_ship >= 0) {
212             pr("%s is at sea and can't be transported\n", prplane(&plane));
213             return RET_FAIL;
214         } else if (plane.pln_harden != 0) {
215             pr("%s has been hardened and can't be transported\n",
216                prplane(&plane));
217             return RET_FAIL;
218         } else if ((plane.pln_flags & PLN_LAUNCHED) &&
219                    (plchr[type].pl_flags & P_O)) {
220             pr("%s is in space and can't be transported\n",
221                prplane(&plane));
222             return RET_FAIL;
223         }
224         if (count == 0) {
225             srcx = plane.pln_x;
226             srcy = plane.pln_y;
227         } else {
228             if (plane.pln_x != srcx || plane.pln_y != srcy) {
229                 pr("All planes must be in the same sector.\n");
230                 return RET_FAIL;
231             }
232         }
233         weight += plchr[type].pl_lcm + (plchr[type].pl_hcm * 2);
234         ++count;
235     }
236     if (count == 0) {
237         pr("No planes\n");
238         return RET_FAIL;
239     }
240     if (!getsect(srcx, srcy, &sect) || !player->owner) {
241         pr("You don't own %s\n", xyas(srcx, srcy, player->cnum));
242         return RET_FAIL;
243     }
244     if (!military_control(&sect)) {
245         pr("Military control required to move planes.\n");
246         return RET_FAIL;
247     }
248     dam = 1;
249     mcost = move_ground((s_char *)&plane, &sect, &endsect,
250                         (double)weight,
251                         player->argp[3], tran_map, 0, &dam);
252     dam /= count;
253     if (mcost < 0)
254         return 0;
255
256     dstx = endsect.sct_x;
257     dsty = endsect.sct_y;
258     snxtitem_rewind(&nstr);
259     while (nxtitem(&nstr, &plane)) {
260         if (!player->owner)
261             continue;
262         if (dam)
263             planedamage(&plane, dam);
264         plane.pln_x = dstx;
265         plane.pln_y = dsty;
266         plane.pln_mission = 0;
267         putplane(plane.pln_uid, &plane);
268     }
269     if (mcost > 0)
270         pr("Total movement cost = %d\n", mcost);
271     else
272         pr("No mobility used\n");
273     getsect(srcx, srcy, &sect);
274     sect.sct_mobil -= mcost;
275     if (sect.sct_mobil < 0)
276         sect.sct_mobil = 0;
277     putsect(&sect);
278     return RET_OK;
279 }
280
281 /*
282  * Pretty tacky, but it works.
283  * If more commands start doing this, then
284  * rewrite map to do the right thing.
285  */
286 /* I think this is no longer used, check subs/move.c:move_ground() */
287 /*ARGSUSED*/
288 static int
289 tran_map(s_char *what, coord curx, coord cury, s_char *arg)
290 {
291     player->argp[1] = arg;
292     player->condarg = 0;
293     return map();
294 }