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