]> git.pond.sub.org Git - empserver/blob - src/lib/commands/retr.c
retreat lretreat: Change query syntax to match mission
[empserver] / src / lib / commands / retr.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  retr.c: Set retreat conditionals for ships and land units
28  *
29  *  Known contributors to this file:
30  *     Ken Stevens, 1995
31  *     Steve McClure, 2000
32  *     Markus Armbruster, 2008-2014
33  */
34
35 #include <config.h>
36
37 #include <ctype.h>
38 #include "commands.h"
39 #include "empobj.h"
40 #include "land.h"
41 #include "path.h"
42 #include "retreat.h"
43 #include "ship.h"
44
45 /*
46  * Retreat flag characters
47  * 'X' means flag is not available
48  * Must agree with RET_ defines.
49  */
50 static char shp_rflagsc[] = "Xitshbdu";
51 static char lnd_rflagsc[] = "XiXXhbXX";
52
53 static int retreat(int);
54
55 int
56 retr(void)
57 {
58     return retreat(EF_SHIP);
59 }
60
61 int
62 lretr(void)
63 {
64     return retreat(EF_LAND);
65 }
66
67 static int
68 retreat(int type)
69 {
70     char *pq, *fl;
71     int nunits;
72     struct nstr_item ni;
73     union empobj_storage unit;
74     int i, rflags, ch, j;
75     char *rflagsc, *p, *name, *rpath, *what;
76     int *rflagsp;
77     char buf1[1024];
78     char buf2[1024];
79
80     if (CANT_HAPPEN(type != EF_LAND && type != EF_SHIP))
81         type = EF_SHIP;
82     rflagsc = type == EF_SHIP ? shp_rflagsc : lnd_rflagsc;
83
84     if (!snxtitem(&ni, type, player->argp[1], NULL))
85         return RET_SYN;
86     nunits = 0;
87
88     if (player->argp[1] && !player->argp[2]) {
89         pr("Omitting the second argument is deprecated and will cease to work in a\n"
90            "future release.  Please use '%s q' to query retreat orders.\n\n",
91            player->combuf);
92         pq = "q";
93     } else {
94         /*
95          * TODO getpath() or similar would be nice once the deprecated
96          * syntax is gone.
97          */
98         pq = getstarg(player->argp[2], "Retreat path, or q to query? ",
99                       buf1);
100         if (!pq || !*pq)
101             return RET_SYN;
102     }
103
104     rflags = 0;
105     if (*pq == 'q') {
106         pq = NULL;
107     } else {
108         for (i = 0; i < RET_LEN - 1 && pq[i]; i++) {
109             if (chkdir(pq[i], DIR_STOP, DIR_LAST) < 0) {
110                 pr("'%c' is not a valid direction...\n", pq[i]);
111                 direrr(NULL, NULL, NULL);
112                 return RET_SYN;
113             }
114         }
115         for (i--; i >= 0 && pq[i] == dirch[DIR_STOP]; i--)
116             pq[i] = 0;
117
118     again:
119         fl = getstarg(player->argp[3],
120                       "Retreat conditions ('?' to list available ones)? ",
121                       buf2);
122         if (!fl)
123             return RET_SYN;
124
125         for (i = 0; fl[i]; i++) {
126             ch = tolower(fl[i]);
127             if (ch == 'c') {
128                 *pq = 0;
129                 break;
130             }
131             if (ch == '?' && !player->argp[3]) {
132                 for (j = 1; rflagsc[j]; j++) {
133                     if (rflagsc[j] != 'X')
134                         pr("%c\tretreat when %s\n",
135                            rflagsc[j],
136                            symbol_by_value(1 << j, retreat_flags));
137                 }
138                 pr("c\tcancel retreat order\n");
139                 goto again;
140             }
141             p = strchr(rflagsc, ch);
142             if (!p) {
143                 pr("Bad retreat condition '%c'\n", fl[i]);
144                 return RET_SYN;
145             }
146             rflags |= 1 << (p - rflagsc);
147         }
148         if (*pq && !rflags)
149             return RET_SYN;
150         if (ni.sel == NS_GROUP && ni.group)
151             rflags |= RET_GROUP;
152         if (!*pq)
153             rflags = 0;
154     }
155
156     while (nxtitem(&ni, &unit)) {
157         if (!player->owner || unit.gen.own == 0)
158             continue;
159         if (type == EF_SHIP) {
160             if (nunits++ == 0) {
161                 if (player->god)
162                     pr("own ");
163                 pr("shp#     ship type       x,y   fl path       as flt?  flags\n");
164             }
165             name = mchr[unit.ship.shp_type].m_name;
166             rpath = unit.ship.shp_rpath;
167             rflagsp = &unit.ship.shp_rflags;
168         } else {
169             if (nunits++ == 0) {
170                 if (player->god)
171                     pr("own ");
172                 pr("lnd#     unit type       x,y   ar path       as army? flags\n");
173             }
174             name = lchr[unit.land.lnd_type].l_name;
175             rpath = unit.land.lnd_rpath;
176             rflagsp = &unit.land.lnd_rflags;
177         }
178         if (pq) {
179             strncpy(rpath, pq, RET_LEN - 1);
180             *rflagsp = rflags;
181             put_empobj(type, unit.gen.uid, &unit);
182         }
183         if (player->god)
184             pr("%3d ", unit.gen.own);
185         pr("%4d ", ni.cur);
186         pr("%-16.16s ", name);
187         prxy("%4d,%-4d ", unit.gen.x, unit.gen.y);
188         pr("%1.1s", &unit.gen.group);
189         pr(" %-11s", rpath);
190         rflags = *rflagsp;
191         if (rflags & RET_GROUP)
192             pr("Yes      ");
193         else
194             pr("         ");
195         for (j = 1; rflagsc[j]; j++) {
196             if ((1 << j) & rflags) {
197                 if (CANT_HAPPEN(rflagsc[j] == 'X'))
198                     continue;
199                 pr("%c", rflagsc[j]);
200             }
201         }
202         pr("\n");
203     }
204     what = type == EF_SHIP ? "ship" : "unit";
205     if (nunits == 0) {
206         if (player->argp[1])
207             pr("%s: No %s(s)\n", player->argp[1], what);
208         else
209             pr("%s: No %s(s)\n", "", what);
210         return RET_FAIL;
211     } else
212         pr("%d %s%s\n", nunits, what, splur(nunits));
213     return RET_OK;
214 }