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