]> git.pond.sub.org Git - empserver/blob - src/lib/commands/miss.c
(mission): Plug memory leak. s_char purge.
[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     }
208
209     mobused = ldround((mission_mob_cost * (double)mobmax), 1);
210
211     while (nxtitem(&ni, &item)) {
212         gp = (struct genitem *)&item;
213
214         if (!player->owner || gp->own == 0)
215             continue;
216
217         if ((mission && (gp->mobil < mobused)) && mission_mob_cost) {
218             pr("%s #%d: not enough mobility! (needs %d)\n",
219                ef_nameof(type), gp->uid, mobused);
220             continue;
221         }
222         if (mission == MI_RESERVE && !lnd_can_attack((struct lndstr *)gp)) {
223             pr("%s is not designed to fight ground troops\n",
224                prland((struct lndstr *)gp));
225             continue;
226         }
227         if (*p == '.') {
228             x = gp->x;
229             y = gp->y;
230             if (!getsect(x, y, &opsect))
231                 return RET_FAIL;
232         }
233
234         dist = mapdist(gp->x, gp->y, x, y);
235         radius = 999;
236         if ((mission == MI_INTERDICT || mission == MI_SUPPORT ||
237              mission == MI_OSUPPORT || mission == MI_DSUPPORT ||
238              mission == MI_AIR_DEFENSE) &&
239             (oprange(gp, type, &radius) < dist)) {
240             pr("%s #%d: out of range! (range %d)\n",
241                ef_nameof(type), gp->uid, oprange(gp, type, &radius));
242             continue;
243         }
244
245         if (radius > desired_radius)
246             radius = desired_radius;
247
248 /*
249                 if (mission && (gp->effic < 60)){
250                         pr("%s #%d: not efficient enough! (must be>=60%)\n",
251                                 ef_nameof(type), gp->uid);
252                         continue;
253                 }
254  */
255
256         if ((mission == MI_INTERDICT) && (type == EF_SHIP))
257             if (mchr[(int)gp->type].m_frnge < 1) {
258                 pr("%s #%d: cannot fire at range!\n",
259                    ef_nameof(type), gp->uid);
260                 continue;
261             }
262
263         if ((mission == MI_INTERDICT) && (type == EF_LAND))
264             if (lchr[(int)gp->type].l_frg < 1) {
265                 pr("%s #%d: cannot fire at range!\n",
266                    ef_nameof(type), gp->uid);
267                 continue;
268             }
269
270         if ((mission == MI_INTERDICT) && (type == EF_PLANE)) {
271             struct plchrstr *pcp;
272
273             pcp = &plchr[(int)gp->type];
274             if (!(pcp->pl_flags & P_T)) {
275                 pr("Only planes with the tactical ability can interdict.\n%s #%d is ineligible\n", pcp->pl_name, gp->uid);
276                 continue;
277             }
278         }
279
280         if ((mission == MI_AIR_DEFENSE) && (type == EF_PLANE)) {
281             struct plchrstr *pcp;
282
283             pcp = &plchr[(int)gp->type];
284             if (!(pcp->pl_flags & P_F)) {
285                 pr("Only planes with the intercept abilities can perform air defense.\n%s #%d is ineligible\n", pcp->pl_name, gp->uid);
286                 continue;
287             }
288         }
289
290         if ((mission == MI_ESCORT) && (type == EF_PLANE)) {
291             struct plchrstr *pcp;
292
293             pcp = &plchr[(int)gp->type];
294             if (!(pcp->pl_flags & P_ESC) && !(pcp->pl_flags & P_F)) {
295                 pr("Only planes with the escort or intercept abilities can escort.\n%s #%d is ineligible\n", pcp->pl_name, gp->uid);
296                 continue;
297             }
298         }
299
300         if ((mission == MI_SUPPORT || mission == MI_OSUPPORT ||
301              mission == MI_DSUPPORT) && (type == EF_PLANE)) {
302             struct plchrstr *pcp;
303
304             pcp = &plchr[(int)gp->type];
305             if (!(pcp->pl_flags & P_T)) {
306                 pr("Only planes with the tactical ability can support.\n%s #%d is ineligible\n", pcp->pl_name, gp->uid);
307                 continue;
308             }
309         }
310
311         num++;                  /* good one.. go with it */
312
313         if (mission == MI_INTERDICT || mission == MI_SUPPORT ||
314             mission == MI_OSUPPORT || mission == MI_DSUPPORT ||
315             mission == MI_AIR_DEFENSE)
316             gp->radius = radius;
317         else
318             gp->radius = 0;
319
320         if (mission == MI_SUPPORT || mission == MI_OSUPPORT ||
321             mission == MI_DSUPPORT || mission == MI_INTERDICT ||
322             mission == MI_AIR_DEFENSE) {
323             pr("%s on %s mission, centered on %s, radius %d\n",
324                nameofitem(gp, type), mission_name(mission),
325                xyas(x, y, player->cnum), gp->radius);
326         } else if (mission == MI_RESERVE) {
327             int plus = 2;
328
329             if (((struct lndstr *)gp)->lnd_rad_max == 0) {
330                 plus = 0;
331             } else {
332                 getsect(gp->x, gp->y, &opsect);
333                 if ((opsect.sct_type == SCT_HEADQ)
334                     && (opsect.sct_effic >= 60))
335                     plus++;
336                 plus += ((struct lndstr *)gp)->lnd_rad_max;
337             }
338
339             pr("%s on %s mission with maximum reaction radius %d\n",
340                nameofitem(gp, type), mission_name(mission), plus);
341         } else if (mission) {
342             pr("%s on %s mission\n", nameofitem(gp, type),
343                mission_name(mission));
344         }
345
346         if (mission)
347             gp->mobil -= mobused;
348         gp->mission = mission;
349         gp->opx = x;
350         gp->opy = y;
351         switch (type) {
352         case EF_SHIP:
353             putship(gp->uid, &item.ship);
354             break;
355         case EF_LAND:
356             putland(gp->uid, &item.land);
357             break;
358         case EF_PLANE:
359             putplane(gp->uid, &item.plane);
360             break;
361         }
362     }
363     if (num == 0) {
364         pr("No %s%s\n", ef_nameof(type), splur(num));
365         return RET_FAIL;
366     }
367     pr("%d %s%s\n", num, ef_nameof(type), splur(num));
368     return RET_OK;
369 }