]> git.pond.sub.org Git - empserver/blob - src/lib/commands/tran.c
Fix trailing whitespace
[empserver] / src / lib / commands / tran.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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  *     Markus Armbruster, 2006
33  */
34
35 #include <config.h>
36
37 #include "commands.h"
38 #include "land.h"
39 #include "nuke.h"
40 #include "plane.h"
41 #include "ship.h"
42
43 static int tran_pmap(coord curx, coord cury, char *arg);
44 static int tran_nmap(coord curx, coord cury, char *arg);
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 == 0)
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     sect.sct_mobil -= mcost;
138     if (sect.sct_mobil < 0)
139         sect.sct_mobil = 0;
140     putsect(&sect);
141     return RET_OK;
142 }
143
144 static int
145 tran_plane(void)
146 {
147     coord srcx, srcy;
148     coord dstx, dsty;
149     int mcost;
150     int weight, count;
151     int type, dam;
152     struct nstr_item nstr;
153     struct plnstr plane;
154     struct sctstr sect;
155     struct sctstr endsect;
156
157     weight = 0;
158     count = 0;
159     if (!snxtitem(&nstr, EF_PLANE, player->argp[2], NULL))
160         return RET_SYN;
161     /*
162      * First do some sanity checks: make sure that they are all in the,
163      * same sector, not on ships, owned, etc.
164      * No one could seriously want to move planes in parallel from
165      * several sectors!
166      */
167     while (nxtitem(&nstr, &plane)) {
168         if (!player->owner)
169             continue;
170         type = plane.pln_type;
171         if (plane.pln_ship >= 0) {
172             pr("%s is at sea and can't be transported\n", prplane(&plane));
173             return RET_FAIL;
174         } else if (plane.pln_harden != 0) {
175             pr("%s has been hardened and can't be transported\n",
176                prplane(&plane));
177             return RET_FAIL;
178         } else if (pln_is_in_orbit(&plane)) {
179             pr("%s is in space and can't be transported\n",
180                prplane(&plane));
181             return RET_FAIL;
182         }
183         if (count == 0) {
184             srcx = plane.pln_x;
185             srcy = plane.pln_y;
186         } else {
187             if (plane.pln_x != srcx || plane.pln_y != srcy) {
188                 pr("All planes must be in the same sector.\n");
189                 return RET_FAIL;
190             }
191         }
192         weight += plchr[type].pl_lcm + (plchr[type].pl_hcm * 2);
193         ++count;
194     }
195     if (count == 0) {
196         pr("No planes\n");
197         return RET_FAIL;
198     }
199     if (!getsect(srcx, srcy, &sect) || !player->owner) {
200         pr("You don't own %s\n", xyas(srcx, srcy, player->cnum));
201         return RET_FAIL;
202     }
203     if (!military_control(&sect)) {
204         pr("Military control required to move planes.\n");
205         return RET_FAIL;
206     }
207     dam = 1;
208     mcost = move_ground(&sect, &endsect, weight,
209                         player->argp[3], tran_pmap, 0, &dam);
210     dam /= count;
211     if (mcost < 0)
212         return 0;
213
214     dstx = endsect.sct_x;
215     dsty = endsect.sct_y;
216     snxtitem_rewind(&nstr);
217     while (nxtitem(&nstr, &plane)) {
218         if (!player->owner)
219             continue;
220         if (dam)
221             planedamage(&plane, dam);
222         plane.pln_x = dstx;
223         plane.pln_y = dsty;
224         plane.pln_mission = 0;
225         putplane(plane.pln_uid, &plane);
226     }
227     if (mcost > 0)
228         pr("Total movement cost = %d\n", mcost);
229     else
230         pr("No mobility used\n");
231     getsect(srcx, srcy, &sect);
232     sect.sct_mobil -= mcost;
233     if (sect.sct_mobil < 0)
234         sect.sct_mobil = 0;
235     putsect(&sect);
236     return RET_OK;
237 }
238
239 /*
240  * Pretty tacky, but it works.
241  * If more commands start doing this, then
242  * rewrite map to do the right thing.
243  */
244 /*ARGSUSED*/
245 static int
246 tran_pmap(coord curx, coord cury, char *arg)
247 {
248     return display_region_map(0, EF_PLANE, curx, cury, arg);
249 }
250
251 static int
252 tran_nmap(coord curx, coord cury, char *arg)
253 {
254     return display_region_map(0, EF_NUKE, curx, cury, arg);
255 }
256