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