]> git.pond.sub.org Git - empserver/blob - src/lib/commands/miss.c
Remove superflous casts and parenthesis.
[empserver] / src / lib / commands / miss.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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  *  miss.c: set missions for ships/planes/units
29  * 
30  *  Known contributors to this file:
31  *     Thomas Ruschak, 1992
32  *     Steve McClure, 2000
33  */
34
35 #include <config.h>
36
37 #include "misc.h"
38 #include "player.h"
39 #include "xy.h"
40 #include "sect.h"
41 #include "ship.h"
42 #include "land.h"
43 #include "plane.h"
44 #include "nat.h"
45 #include "nsc.h"
46 #include "file.h"
47 #include "path.h"
48 #include "mission.h"
49 #include "genitem.h"
50 #include "commands.h"
51 #include "optlist.h"
52
53 union item_u {
54     struct shpstr ship;
55     struct plnstr plane;
56     struct lndstr land;
57 };
58
59 /*
60  *  mission <type> <planes/ships/units> <mission type> <op sector> [<radius>]
61  */
62 int
63 mission(void)
64 {
65     static int ef_with_missions[] = { EF_SHIP, EF_LAND, EF_PLANE, EF_BAD };
66     char *p;
67     int type;
68     int mission;
69     coord x, y;
70     int desired_radius, radius;
71     struct sctstr opsect;
72     union item_u item;
73     struct genitem *gp;
74     int num = 0, mobmax, mobused, dist;
75     struct nstr_item ni;
76     char prompt[128];
77     char buf[1024];
78
79     if ((p =
80          getstarg(player->argp[1], "Ship, plane or land unit (p,sh,la)? ",
81                   buf)) == 0)
82         return RET_SYN;
83     type = ef_byname_from(p, ef_with_missions);
84     if (type < 0) {
85         pr("Ships, land units or planes only! (s, l, p)\n");
86         return RET_SYN;
87     }
88     sprintf(prompt, "%s(s)? ", ef_nameof(type));
89     p = getstarg(player->argp[2], prompt, buf);
90     if (!snxtitem(&ni, type, p))
91         return RET_SYN;
92
93     if ((p =
94          getstarg(player->argp[3],
95                   "Mission (int, sup, osup, dsup, esc, res, air, query, clear)? ",
96                   buf)) == 0)
97         return RET_SYN;
98
99 /* 
100  * 'i'     interdiction
101  * 's'     support
102  * 'o'     support attacks
103  * 'd'     support defenders
104  * 'e'     escort
105  * 'r'     defensive reserve
106  * 'a'     air defense (intercepts)
107  */
108     switch (*p) {
109     case 'I':
110     case 'i':
111         mission = MI_INTERDICT;
112         break;
113     case 'O':
114     case 'o':
115         mission = MI_OSUPPORT;
116         break;
117     case 'D':
118     case 'd':
119         mission = MI_DSUPPORT;
120         break;
121     case 'S':
122     case 's':
123         mission = MI_SUPPORT;
124         break;
125     case 'C':
126     case 'c':
127         mission = 0;
128         break;
129     case 'E':
130     case 'e':
131         mission = MI_ESCORT;
132         break;
133     case 'R':
134     case 'r':
135         mission = MI_RESERVE;
136         break;
137     case 'A':
138     case 'a':
139         mission = MI_AIR_DEFENSE;
140         break;
141     case 'q':
142         show_mission(type, &ni);
143         return RET_OK;
144     default:
145         pr("bad condition\n");
146         pr("i\tinterdiction (any)\n");
147         pr("s\tsupport (tactical planes only)\n");
148         pr("o\toffensive support (tactical planes only)\n");
149         pr("d\tdefensive support (tactical planes only)\n");
150         pr("r\treserve (land units only)\n");
151         pr("e\tescort (tactical or escort planes only)\n");
152         pr("a\tair defense (intercept planes only)\n");
153         pr("c\tclear mission\n");
154         pr("q\tquery\n");
155         return RET_SYN;
156     }
157
158     if (mission && !cando(mission, type)) {
159         pr("A %s cannot do that mission!\n", ef_nameof(type));
160         pr("i\tinterdiction (any)\n");
161         pr("s\tsupport (planes only)\n");
162         pr("o\toffensive support (planes only)\n");
163         pr("d\tdefensive support (planes only)\n");
164         pr("r\treserve (land units only)\n");
165         pr("e\tescort (planes only)\n");
166         pr("a\tair defense (planes only)\n");
167         return RET_FAIL;
168     }
169
170     if (mission && ((mission != MI_RESERVE) && (mission != MI_ESCORT))) {
171         if ((p = getstarg(player->argp[4], "operations point? ", buf)) == 0
172             || *p == 0)
173             return RET_SYN;
174
175         if (*p != '.') {
176             if (!sarg_xy(p, &x, &y))
177                 return RET_SYN;
178
179             if (!getsect(x, y, &opsect))
180                 return RET_FAIL;
181         }
182     } else {
183         x = 0;
184         y = 0;
185     }
186
187     if (player->argp[5] != NULL) {
188         desired_radius = atoi(player->argp[5]);
189         if (desired_radius < 0) {
190             pr("Radius must be greater than zero!\n");
191             return RET_FAIL;
192         }
193     } else {
194         desired_radius = 9999;
195     }
196
197     switch (type) {
198     case EF_SHIP:
199         mobmax = ship_mob_max;
200         break;
201     case EF_LAND:
202         mobmax = land_mob_max;
203         break;
204     case EF_PLANE:
205         mobmax = plane_mob_max;
206         break;
207     default:
208         CANT_HAPPEN("bad TYPE");
209         return RET_FAIL;
210     }
211
212     mobused = ldround(mission_mob_cost * (double)mobmax, 1);
213
214     while (nxtitem(&ni, &item)) {
215         gp = (struct genitem *)&item;
216
217         if (!player->owner || gp->own == 0)
218             continue;
219
220         if ((mission && (gp->mobil < mobused)) && mission_mob_cost) {
221             pr("%s #%d: not enough mobility! (needs %d)\n",
222                ef_nameof(type), gp->uid, mobused);
223             continue;
224         }
225         if (mission == MI_RESERVE && !lnd_can_attack((struct lndstr *)gp)) {
226             pr("%s is not designed to fight ground troops\n",
227                prland((struct lndstr *)gp));
228             continue;
229         }
230         if (*p == '.') {
231             x = gp->x;
232             y = gp->y;
233             if (!getsect(x, y, &opsect))
234                 return RET_FAIL;
235         }
236
237         dist = mapdist(gp->x, gp->y, x, y);
238         radius = 999;
239         if ((mission == MI_INTERDICT || mission == MI_SUPPORT ||
240              mission == MI_OSUPPORT || mission == MI_DSUPPORT ||
241              mission == MI_AIR_DEFENSE) &&
242             (oprange(gp, type, &radius) < dist)) {
243             pr("%s #%d: out of range! (range %d)\n",
244                ef_nameof(type), gp->uid, oprange(gp, type, &radius));
245             continue;
246         }
247
248         if (radius > desired_radius)
249             radius = desired_radius;
250
251 /*
252                 if (mission && (gp->effic < 60)){
253                         pr("%s #%d: not efficient enough! (must be>=60%)\n",
254                                 ef_nameof(type), gp->uid);
255                         continue;
256                 }
257  */
258
259         if ((mission == MI_INTERDICT) && (type == EF_SHIP))
260             if (mchr[(int)gp->type].m_frnge < 1) {
261                 pr("%s #%d: cannot fire at range!\n",
262                    ef_nameof(type), gp->uid);
263                 continue;
264             }
265
266         if ((mission == MI_INTERDICT) && (type == EF_LAND))
267             if (lchr[(int)gp->type].l_frg < 1) {
268                 pr("%s #%d: cannot fire at range!\n",
269                    ef_nameof(type), gp->uid);
270                 continue;
271             }
272
273         if ((mission == MI_INTERDICT) && (type == EF_PLANE)) {
274             struct plchrstr *pcp;
275
276             pcp = &plchr[(int)gp->type];
277             if (!(pcp->pl_flags & P_T)) {
278                 pr("Only planes with the tactical ability can interdict.\n"
279                    "%s #%d is ineligible\n",
280                    pcp->pl_name, gp->uid);
281                 continue;
282             }
283         }
284
285         if ((mission == MI_AIR_DEFENSE) && (type == EF_PLANE)) {
286             struct plchrstr *pcp;
287
288             pcp = &plchr[(int)gp->type];
289             if (!(pcp->pl_flags & P_F)) {
290                 pr("Only planes with the intercept abilities can perform air defense.\n"
291                    "%s #%d is ineligible\n",
292                    pcp->pl_name, gp->uid);
293                 continue;
294             }
295         }
296
297         if ((mission == MI_ESCORT) && (type == EF_PLANE)) {
298             struct plchrstr *pcp;
299
300             pcp = &plchr[(int)gp->type];
301             if (!(pcp->pl_flags & P_ESC) && !(pcp->pl_flags & P_F)) {
302                 pr("Only planes with the escort or intercept abilities can escort.\n"
303                    "%s #%d is ineligible\n",
304                    pcp->pl_name, gp->uid);
305                 continue;
306             }
307         }
308
309         if ((mission == MI_SUPPORT || mission == MI_OSUPPORT ||
310              mission == MI_DSUPPORT) && (type == EF_PLANE)) {
311             struct plchrstr *pcp;
312
313             pcp = &plchr[(int)gp->type];
314             if (!(pcp->pl_flags & P_T)) {
315                 pr("Only planes with the tactical ability can support.\n"
316                    "%s #%d is ineligible\n",
317                    pcp->pl_name, gp->uid);
318                 continue;
319             }
320         }
321
322         num++;                  /* good one.. go with it */
323
324         if (mission == MI_INTERDICT || mission == MI_SUPPORT ||
325             mission == MI_OSUPPORT || mission == MI_DSUPPORT ||
326             mission == MI_AIR_DEFENSE)
327             gp->radius = radius;
328         else
329             gp->radius = 0;
330
331         if (mission == MI_SUPPORT || mission == MI_OSUPPORT ||
332             mission == MI_DSUPPORT || mission == MI_INTERDICT ||
333             mission == MI_AIR_DEFENSE) {
334             pr("%s on %s mission, centered on %s, radius %d\n",
335                nameofitem(gp, type), mission_name(mission),
336                xyas(x, y, player->cnum), gp->radius);
337         } else if (mission == MI_RESERVE) {
338             int plus = 2;
339
340             if (((struct lndstr *)gp)->lnd_rad_max == 0) {
341                 plus = 0;
342             } else {
343                 getsect(gp->x, gp->y, &opsect);
344                 if ((opsect.sct_type == SCT_HEADQ)
345                     && (opsect.sct_effic >= 60))
346                     plus++;
347                 plus += ((struct lndstr *)gp)->lnd_rad_max;
348             }
349
350             pr("%s on %s mission with maximum reaction radius %d\n",
351                nameofitem(gp, type), mission_name(mission), plus);
352         } else if (mission) {
353             pr("%s on %s mission\n", nameofitem(gp, type),
354                mission_name(mission));
355         }
356
357         if (mission)
358             gp->mobil -= mobused;
359         gp->mission = mission;
360         gp->opx = x;
361         gp->opy = y;
362         switch (type) {
363         case EF_SHIP:
364             putship(gp->uid, &item.ship);
365             break;
366         case EF_LAND:
367             putland(gp->uid, &item.land);
368             break;
369         case EF_PLANE:
370             putplane(gp->uid, &item.plane);
371             break;
372         }
373     }
374     if (num == 0) {
375         pr("No %s%s\n", ef_nameof(type), splur(num));
376         return RET_FAIL;
377     }
378     pr("%d %s%s\n", num, ef_nameof(type), splur(num));
379     return RET_OK;
380 }