]> git.pond.sub.org Git - empserver/blob - src/lib/commands/miss.c
Declare all configuration variables in optlist.h. Include that
[empserver] / src / lib / commands / miss.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 "var.h"
39 #include "xy.h"
40 #include "sect.h"
41 #include "retreat.h"
42 #include "ship.h"
43 #include "land.h"
44 #include "plane.h"
45 #include "nat.h"
46 #include "nsc.h"
47 #include "deity.h"
48 #include "file.h"
49 #include "path.h"
50 #include "mission.h"
51 #include "genitem.h"
52 #include "commands.h"
53 #include "optlist.h"
54
55 /*
56  *  mission <type> <planes/ships/units> <mission type> <op sector> [<radius>]
57  */
58 int
59 mission(void)
60 {
61     s_char *p;
62     int type;
63     int mission;
64     coord x, y;
65     int size, desired_radius, radius;
66     struct sctstr opsect;
67     s_char *block;
68     struct genitem *gp;
69     int num = 0, mobmax, mobused, dist;
70     struct nstr_item ni;
71     s_char prompt[128];
72     s_char buf[1024];
73
74     if ((p =
75          getstarg(player->argp[1], "Ship, plane or land unit (p,sh,la)? ",
76                   buf)) == 0)
77         return RET_SYN;
78     type = ef_byname(p);
79     if (type == EF_SECTOR)
80         type = EF_SHIP;
81     if (type != EF_SHIP && type != EF_LAND && type != EF_PLANE) {
82         pr("Ships, land units or planes only! (s, l, p)\n");
83         return RET_SYN;
84     }
85     sprintf(prompt, "%s(s)? ", ef_nameof(type));
86     p = getstarg(player->argp[2], prompt, buf);
87     if (!snxtitem(&ni, type, p))
88         return RET_SYN;
89
90     if ((p =
91          getstarg(player->argp[3],
92                   "Mission (int, sup, osup, dsup, esc, res, air, query, clear)? ",
93                   buf)) == 0)
94         return RET_SYN;
95
96 /* 
97  * 'i'     interdiction
98  * 's'     support
99  * 'o'     support attacks
100  * 'd'     support defenders
101  * 'e'     escort
102  * 'r'     defensive reserve
103  * 'a'     air defense (intercepts)
104  */
105     switch (*p) {
106     case 'I':
107     case 'i':
108         mission = MI_INTERDICT;
109         break;
110     case 'O':
111     case 'o':
112         mission = MI_OSUPPORT;
113         break;
114     case 'D':
115     case 'd':
116         mission = MI_DSUPPORT;
117         break;
118     case 'S':
119     case 's':
120         mission = MI_SUPPORT;
121         break;
122     case 'C':
123     case 'c':
124         mission = 0;
125         break;
126     case 'E':
127     case 'e':
128         mission = MI_ESCORT;
129         break;
130     case 'R':
131     case 'r':
132         mission = MI_RESERVE;
133         break;
134     case 'A':
135     case 'a':
136         mission = MI_AIR_DEFENSE;
137         break;
138     case 'q':
139         show_mission(type, &ni);
140         return RET_OK;
141     default:
142         pr("bad condition\n");
143         pr("i\tinterdiction (any)\n");
144         pr("s\tsupport (tactical planes only)\n");
145         pr("o\toffensive support (tactical planes only)\n");
146         pr("d\tdefensive support (tactical planes only)\n");
147         pr("r\treserve (land units only)\n");
148         pr("e\tescort (tactical or escort planes only)\n");
149         pr("a\tair defense (intercept planes only)\n");
150         pr("c\tclear mission\n");
151         pr("q\tquery\n");
152         return RET_SYN;
153     }
154
155     if (mission && !cando(mission, type)) {
156         pr("A %s cannot do that mission!\n", ef_nameof(type));
157         pr("i\tinterdiction (any)\n");
158         pr("s\tsupport (planes only)\n");
159         pr("o\toffensive support (planes only)\n");
160         pr("d\tdefensive support (planes only)\n");
161         pr("r\treserve (land units only)\n");
162         pr("e\tescort (planes only)\n");
163         pr("a\tair defense (planes only)\n");
164         return RET_FAIL;
165     }
166
167     if (mission && ((mission != MI_RESERVE) && (mission != MI_ESCORT))) {
168         if ((p = getstarg(player->argp[4], "operations point? ", buf)) == 0
169             || *p == 0)
170             return RET_SYN;
171
172         if (*p != '.') {
173             if (!sarg_xy(p, &x, &y))
174                 return RET_SYN;
175
176             if (!getsect(x, y, &opsect))
177                 return RET_FAIL;
178         }
179     } else {
180         x = 0;
181         y = 0;
182     }
183
184     if (player->argp[5] != (s_char *)0) {
185         desired_radius = atoi(player->argp[5]);
186         if (desired_radius < 0) {
187             pr("Radius must be greater than zero!\n");
188             return RET_FAIL;
189         }
190     } else {
191         desired_radius = 9999;
192     }
193
194     size = max(sizeof(struct lndstr), sizeof(struct plnstr));
195     size = max(size, sizeof(struct shpstr));
196     block = (s_char *)malloc(size);
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, block)) {
212         gp = (struct genitem *)block;
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(buf, 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(buf, gp, type), mission_name(mission), plus);
341         } else if (mission) {
342             pr("%s on %s mission\n", nameofitem(buf, 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, block);
354             break;
355         case EF_LAND:
356             putland(gp->uid, block);
357             break;
358         case EF_PLANE:
359             putplane(gp->uid, block);
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 }