]> git.pond.sub.org Git - empserver/blob - src/lib/commands/tran.c
License upgrade to GPL version 3 or later
[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-2009
32  */
33
34 #include <config.h>
35
36 #include "commands.h"
37 #include "land.h"
38 #include "nuke.h"
39 #include "plane.h"
40 #include "ship.h"
41
42 static int tran_pmap(coord curx, coord cury, char *arg);
43 static int tran_nmap(coord curx, coord cury, char *arg);
44 static int tran_nuke(void);
45 static int tran_plane(void);
46
47 int
48 tran(void)
49 {
50     char *what;
51     char buf[1024];
52
53     what = getstarg(player->argp[1], "transport what (nuke or plane): ",
54                     buf);
55     if (!what)
56         return RET_SYN;
57     if (*what == 'n')
58         return tran_nuke();
59     else if (*what == 'p')
60         return tran_plane();
61     return RET_SYN;
62 }
63
64 static int
65 tran_nuke(void)
66 {
67     coord srcx, srcy;
68     coord dstx, dsty;
69     int mcost;
70     int weight, count;
71     int type, dam;
72     struct nstr_item nstr;
73     struct nukstr nuke;
74     struct sctstr sect;
75     struct sctstr endsect;
76
77     weight = 0;
78     count = 0;
79     if (!snxtitem(&nstr, EF_NUKE, player->argp[2], NULL))
80         return RET_SYN;
81     while (nxtitem(&nstr, &nuke)) {
82         if (!player->owner)
83             continue;
84         type = nuke.nuk_type;
85         if (nuke.nuk_plane >= 0) {
86             pr("%s is armed and can't be transported\n", prnuke(&nuke));
87             return RET_FAIL;
88         }
89         if (count == 0) {
90             srcx = nuke.nuk_x;
91             srcy = nuke.nuk_y;
92         } else {
93             if (nuke.nuk_x != srcx || nuke.nuk_y != srcy) {
94                 pr("All nukes must be in the same sector.\n");
95                 return RET_FAIL;
96             }
97         }
98         weight += nchr[type].n_weight;
99         ++count;
100     }
101     if (count == 0) {
102         pr("No nukes\n");
103         return RET_FAIL;
104     }
105     if (!getsect(srcx, srcy, &sect) || !player->owner) {
106         pr("You don't own %s\n", xyas(srcx, srcy, player->cnum));
107         return RET_FAIL;
108     }
109     if (!military_control(&sect)) {
110         pr("Military control required to move nukes.\n");
111         return RET_FAIL;
112     }
113     dam = 0;
114     mcost = move_ground(&sect, &endsect, weight,
115                         player->argp[3], tran_nmap, 0, &dam);
116     if (mcost < 0)
117         return 0;
118
119     dstx = endsect.sct_x;
120     dsty = endsect.sct_y;
121     snxtitem_rewind(&nstr);
122     while (nxtitem(&nstr, &nuke)) {
123         if (!player->owner)
124             continue;
125         /* TODO apply dam */
126         nuke.nuk_x = dstx;
127         nuke.nuk_y = dsty;
128         nuke.nuk_mission = 0;
129         putnuke(nuke.nuk_uid, &nuke);
130     }
131     if (mcost > 0)
132         pr("Total movement cost = %d\n", mcost);
133     else
134         pr("No mobility used\n");
135     getsect(srcx, srcy, &sect);
136     if (sect.sct_mobil >= mcost)
137         sect.sct_mobil -= mcost;
138     else
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     if (sect.sct_mobil >= mcost)
233         sect.sct_mobil -= mcost;
234     else
235         sect.sct_mobil = 0;
236     putsect(&sect);
237     return RET_OK;
238 }
239
240 /*
241  * Pretty tacky, but it works.
242  * If more commands start doing this, then
243  * rewrite map to do the right thing.
244  */
245 /*ARGSUSED*/
246 static int
247 tran_pmap(coord curx, coord cury, char *arg)
248 {
249     return display_region_map(0, EF_PLANE, curx, cury, arg);
250 }
251
252 static int
253 tran_nmap(coord curx, coord cury, char *arg)
254 {
255     return display_region_map(0, EF_NUKE, curx, cury, arg);
256 }