]> git.pond.sub.org Git - empserver/blob - src/lib/commands/tran.c
47e1b2c6145150fac7c133d22d0cfd7e2f960c73
[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 stowed on ship #%d, and can't be transported\n",
174                prplane(&plane), plane.pln_ship);
175             return RET_FAIL;
176         } else if (plane.pln_land >= 0) {
177             pr("%s is stowed on land #%d, and can't be transported\n",
178                prplane(&plane), plane.pln_land);
179             return RET_FAIL;
180         } else if (plane.pln_harden != 0) {
181             pr("%s has been hardened and can't be transported\n",
182                prplane(&plane));
183             return RET_FAIL;
184         } else if (pln_is_in_orbit(&plane)) {
185             pr("%s is in space and can't be transported\n",
186                prplane(&plane));
187             return RET_FAIL;
188         }
189         if (count == 0) {
190             srcx = plane.pln_x;
191             srcy = plane.pln_y;
192         } else {
193             if (plane.pln_x != srcx || plane.pln_y != srcy) {
194                 pr("All planes must be in the same sector.\n");
195                 return RET_FAIL;
196             }
197         }
198         weight += plchr[type].pl_lcm + (plchr[type].pl_hcm * 2);
199         ++count;
200     }
201     if (count == 0) {
202         pr("No planes\n");
203         return RET_FAIL;
204     }
205     if (!getsect(srcx, srcy, &sect) || !player->owner) {
206         pr("You don't own %s\n", xyas(srcx, srcy, player->cnum));
207         return RET_FAIL;
208     }
209     if (!military_control(&sect)) {
210         pr("Military control required to move planes.\n");
211         return RET_FAIL;
212     }
213     dam = 1;
214     mcost = move_ground(&sect, &endsect, weight,
215                         player->argp[3], tran_pmap, 0, &dam);
216     dam /= count;
217     if (mcost < 0)
218         return 0;
219
220     dstx = endsect.sct_x;
221     dsty = endsect.sct_y;
222     snxtitem_rewind(&nstr);
223     while (nxtitem(&nstr, &plane)) {
224         if (!player->owner)
225             continue;
226         if (dam)
227             planedamage(&plane, dam);
228         plane.pln_x = dstx;
229         plane.pln_y = dsty;
230         plane.pln_mission = 0;
231         putplane(plane.pln_uid, &plane);
232     }
233     if (mcost > 0)
234         pr("Total movement cost = %d\n", mcost);
235     else
236         pr("No mobility used\n");
237     getsect(srcx, srcy, &sect);
238     if (sect.sct_mobil >= mcost)
239         sect.sct_mobil -= mcost;
240     else
241         sect.sct_mobil = 0;
242     putsect(&sect);
243     return RET_OK;
244 }
245
246 /*
247  * Pretty tacky, but it works.
248  * If more commands start doing this, then
249  * rewrite map to do the right thing.
250  */
251 /*ARGSUSED*/
252 static int
253 tran_pmap(coord curx, coord cury, char *arg1, char *arg2)
254 {
255     return display_region_map(0, EF_PLANE, curx, cury, arg1, arg2);
256 }
257
258 static int
259 tran_nmap(coord curx, coord cury, char *arg1, char *arg2)
260 {
261     return display_region_map(0, EF_NUKE, curx, cury, arg1, arg2);
262 }