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