]> git.pond.sub.org Git - empserver/blob - src/lib/commands/tran.c
2f3c35f164ae5eed2752b788d95f250247dd92ce
[empserver] / src / lib / commands / tran.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2017, 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-2016
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 static int pln_weight(struct plnstr *);
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 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         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 += pln_weight(&plane);
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 static int
245 pln_weight(struct plnstr *pp)
246 {
247     int w, i;
248
249     w = 0;
250     for (i = I_NONE + 1; i <= I_MAX; i++)
251         w += plchr[pp->pln_type].pl_mat[i] * ichr[i].i_lbs;
252     return w;
253 }
254
255 /*
256  * Pretty tacky, but it works.
257  * If more commands start doing this, then
258  * rewrite map to do the right thing.
259  */
260 /*ARGSUSED*/
261 static int
262 tran_pmap(coord curx, coord cury, char *arg1, char *arg2)
263 {
264     return display_region_map(0, EF_PLANE, curx, cury, arg1, arg2);
265 }
266
267 static int
268 tran_nmap(coord curx, coord cury, char *arg1, char *arg2)
269 {
270     return display_region_map(0, EF_NUKE, curx, cury, arg1, arg2);
271 }