]> git.pond.sub.org Git - empserver/blob - src/lib/commands/tran.c
Clean up move_ground()'s parsing of DIR_MAP
[empserver] / src / lib / commands / tran.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  tran.c: Transport nuclear devices and planes
28  *
29  *  Known contributors to this file:
30  *     Steve McClure, 2000
31  *     Markus Armbruster, 2006-2011
32  */
33
34 #include <config.h>
35
36 #include "commands.h"
37 #include "land.h"
38 #include "map.h"
39 #include "nuke.h"
40 #include "plane.h"
41 #include "ship.h"
42
43 static int tran_pmap(coord, coord, char *, char *);
44 static int tran_nmap(coord, coord, char *, char *);
45 static int tran_nuke(void);
46 static int tran_plane(void);
47
48 int
49 tran(void)
50 {
51     char *what;
52     char buf[1024];
53
54     what = getstarg(player->argp[1], "transport what (nuke or plane): ",
55                     buf);
56     if (!what)
57         return RET_SYN;
58     if (*what == 'n')
59         return tran_nuke();
60     else if (*what == 'p')
61         return tran_plane();
62     return RET_SYN;
63 }
64
65 static int
66 tran_nuke(void)
67 {
68     coord srcx, srcy;
69     coord dstx, dsty;
70     int mcost;
71     int weight, count;
72     int type, dam;
73     struct nstr_item nstr;
74     struct nukstr nuke;
75     struct sctstr sect;
76     struct sctstr endsect;
77
78     weight = 0;
79     count = 0;
80     if (!snxtitem(&nstr, EF_NUKE, player->argp[2], NULL))
81         return RET_SYN;
82     while (nxtitem(&nstr, &nuke)) {
83         if (!player->owner)
84             continue;
85         type = nuke.nuk_type;
86         if (nuke.nuk_plane >= 0) {
87             pr("%s is armed and can't be transported\n", prnuke(&nuke));
88             return RET_FAIL;
89         }
90         if (count == 0) {
91             srcx = nuke.nuk_x;
92             srcy = nuke.nuk_y;
93         } else {
94             if (nuke.nuk_x != srcx || nuke.nuk_y != srcy) {
95                 pr("All nukes must be in the same sector.\n");
96                 return RET_FAIL;
97             }
98         }
99         weight += nchr[type].n_weight;
100         ++count;
101     }
102     if (count == 0) {
103         pr("No nukes\n");
104         return RET_FAIL;
105     }
106     if (!getsect(srcx, srcy, &sect) || !player->owner) {
107         pr("You don't own %s\n", xyas(srcx, srcy, player->cnum));
108         return RET_FAIL;
109     }
110     if (!military_control(&sect)) {
111         pr("Military control required to move nukes.\n");
112         return RET_FAIL;
113     }
114     dam = 0;
115     mcost = move_ground(&sect, &endsect, weight,
116                         player->argp[3], tran_nmap, 0, &dam);
117     if (mcost < 0)
118         return 0;
119
120     dstx = endsect.sct_x;
121     dsty = endsect.sct_y;
122     snxtitem_rewind(&nstr);
123     while (nxtitem(&nstr, &nuke)) {
124         if (!player->owner)
125             continue;
126         /* TODO apply dam */
127         nuke.nuk_x = dstx;
128         nuke.nuk_y = dsty;
129         nuke.nuk_mission = 0;
130         putnuke(nuke.nuk_uid, &nuke);
131     }
132     if (mcost > 0)
133         pr("Total movement cost = %d\n", mcost);
134     else
135         pr("No mobility used\n");
136     getsect(srcx, srcy, &sect);
137     if (sect.sct_mobil >= mcost)
138         sect.sct_mobil -= mcost;
139     else
140         sect.sct_mobil = 0;
141     putsect(&sect);
142     return RET_OK;
143 }
144
145 static int
146 tran_plane(void)
147 {
148     coord srcx, srcy;
149     coord dstx, dsty;
150     int mcost;
151     int weight, count;
152     int type, dam;
153     struct nstr_item nstr;
154     struct plnstr plane;
155     struct sctstr sect;
156     struct sctstr endsect;
157
158     weight = 0;
159     count = 0;
160     if (!snxtitem(&nstr, EF_PLANE, player->argp[2], NULL))
161         return RET_SYN;
162     /*
163      * First do some sanity checks: make sure that they are all in the,
164      * same sector, not on ships, owned, etc.
165      * No one could seriously want to move planes in parallel from
166      * several sectors!
167      */
168     while (nxtitem(&nstr, &plane)) {
169         if (!player->owner)
170             continue;
171         type = plane.pln_type;
172         if (plane.pln_ship >= 0) {
173             pr("%s is at sea and can't be transported\n", prplane(&plane));
174             return RET_FAIL;
175         } else if (plane.pln_harden != 0) {
176             pr("%s has been hardened and can't be transported\n",
177                prplane(&plane));
178             return RET_FAIL;
179         } else if (pln_is_in_orbit(&plane)) {
180             pr("%s is in space and can't be transported\n",
181                prplane(&plane));
182             return RET_FAIL;
183         }
184         if (count == 0) {
185             srcx = plane.pln_x;
186             srcy = plane.pln_y;
187         } else {
188             if (plane.pln_x != srcx || plane.pln_y != srcy) {
189                 pr("All planes must be in the same sector.\n");
190                 return RET_FAIL;
191             }
192         }
193         weight += plchr[type].pl_lcm + (plchr[type].pl_hcm * 2);
194         ++count;
195     }
196     if (count == 0) {
197         pr("No planes\n");
198         return RET_FAIL;
199     }
200     if (!getsect(srcx, srcy, &sect) || !player->owner) {
201         pr("You don't own %s\n", xyas(srcx, srcy, player->cnum));
202         return RET_FAIL;
203     }
204     if (!military_control(&sect)) {
205         pr("Military control required to move planes.\n");
206         return RET_FAIL;
207     }
208     dam = 1;
209     mcost = move_ground(&sect, &endsect, weight,
210                         player->argp[3], tran_pmap, 0, &dam);
211     dam /= count;
212     if (mcost < 0)
213         return 0;
214
215     dstx = endsect.sct_x;
216     dsty = endsect.sct_y;
217     snxtitem_rewind(&nstr);
218     while (nxtitem(&nstr, &plane)) {
219         if (!player->owner)
220             continue;
221         if (dam)
222             planedamage(&plane, dam);
223         plane.pln_x = dstx;
224         plane.pln_y = dsty;
225         plane.pln_mission = 0;
226         putplane(plane.pln_uid, &plane);
227     }
228     if (mcost > 0)
229         pr("Total movement cost = %d\n", mcost);
230     else
231         pr("No mobility used\n");
232     getsect(srcx, srcy, &sect);
233     if (sect.sct_mobil >= mcost)
234         sect.sct_mobil -= mcost;
235     else
236         sect.sct_mobil = 0;
237     putsect(&sect);
238     return RET_OK;
239 }
240
241 /*
242  * Pretty tacky, but it works.
243  * If more commands start doing this, then
244  * rewrite map to do the right thing.
245  */
246 /*ARGSUSED*/
247 static int
248 tran_pmap(coord curx, coord cury, char *arg1, char *arg2)
249 {
250     return display_region_map(0, EF_PLANE, curx, cury, arg1, arg2);
251 }
252
253 static int
254 tran_nmap(coord curx, coord cury, char *arg1, char *arg2)
255 {
256     return display_region_map(0, EF_NUKE, curx, cury, arg1, arg2);
257 }