1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2021, 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-2015
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 static int retreat_show(int, struct nstr_item *);
55 
56 int
c_retreat(void)57 c_retreat(void)
58 {
59     return retreat(EF_SHIP);
60 }
61 
62 int
c_lretreat(void)63 c_lretreat(void)
64 {
65     return retreat(EF_LAND);
66 }
67 
68 static int
retreat(int type)69 retreat(int type)
70 {
71     char *pq, *fl;
72     int nunits;
73     struct nstr_item ni;
74     union empobj_storage unit;
75     int i, rflags, ch, j;
76     char *rflagsc, *p;
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 
87     if (player->argp[1] && !player->argp[2]) {
88 	pr("Omitting the second argument is deprecated and will cease to work in a\n"
89 	   "future release.  Please use '%s q' to query retreat orders.\n\n",
90 	   player->combuf);
91 	pq = "q";
92     } else {
93 	/*
94 	 * TODO getpath() or similar would be nice once the deprecated
95 	 * syntax is gone.
96 	 */
97 	pq = getstarg(player->argp[2], "Retreat path, or q to query? ",
98 		      buf1);
99 	if (!pq || !*pq)
100 	    return RET_SYN;
101     }
102 
103     if (*pq == 'q')
104 	return retreat_show(type, &ni);
105 
106     for (i = 0; i < RET_LEN - 1 && pq[i]; i++) {
107 	if (chkdir(pq[i], DIR_STOP, DIR_LAST) < 0) {
108 	    pr("'%c' is not a valid direction...\n", pq[i]);
109 	    direrr(NULL, NULL, NULL);
110 	    return RET_SYN;
111 	}
112     }
113     for (i--; i >= 0 && pq[i] == dirch[DIR_STOP]; i--)
114 	pq[i] = 0;
115 
116     rflags = 0;
117     if (*pq) {
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 		/* Deprecated, but keeping it around doesn't hurt */
129 		*pq = 0;
130 		break;
131 	    }
132 	    if (ch == '?' && !player->argp[3]) {
133 		for (j = 1; rflagsc[j]; j++) {
134 		    if (rflagsc[j] != 'X')
135 			pr("%c\tretreat when %s\n",
136 			   rflagsc[j],
137 			   symbol_by_value(1 << j, retreat_flags));
138 		}
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     nunits = 0;
157     while (nxtitem(&ni, &unit)) {
158 	if (!player->owner || unit.gen.own == 0)
159 	    continue;
160 	if (type == EF_SHIP) {
161 	    strncpy(unit.ship.shp_rpath, pq, RET_LEN - 1);
162 	    unit.ship.shp_rflags = rflags;
163 	} else {
164 	    strncpy(unit.land.lnd_rpath, pq, RET_LEN - 1);
165 	    unit.land.lnd_rflags = rflags;
166 	}
167 	put_empobj(type, unit.gen.uid, &unit);
168 	nunits++;
169     }
170     if (rflags) {
171 	symbol_set_fmt(buf2, sizeof(buf2), rflags & ~RET_GROUP,
172 		       retreat_flags, ", ", 0);
173 	pr("%d %s%s ordered to retreat%s along path %s when %s\n",
174 	   nunits, ef_nameof_pretty(type), splur(nunits),
175 	   rflags & RET_GROUP ? " as group" : "", pq, buf2);
176     } else
177 	pr("%d %s%s ordered not to retreat\n",
178 	   nunits, ef_nameof_pretty(type), splur(nunits));
179     return RET_OK;
180 }
181 
182 static int
retreat_show(int type,struct nstr_item * np)183 retreat_show(int type, struct nstr_item *np)
184 {
185     char *rflagsc = type == EF_SHIP ? shp_rflagsc : lnd_rflagsc;
186     union empobj_storage unit;
187     int nunits;
188     char *name, *rpath, *what;
189     int *rflagsp, rflags, i;
190 
191     nunits = 0;
192     while (nxtitem(np, &unit)) {
193 	if (!player->owner || unit.gen.own == 0)
194 	    continue;
195 	if (type == EF_SHIP) {
196 	    if (nunits++ == 0) {
197 		if (player->god)
198 		    pr("own ");
199 		pr("shp#     ship type       x,y   fl path       as flt?  flags\n");
200 	    }
201 	    name = mchr[unit.ship.shp_type].m_name;
202 	    rpath = unit.ship.shp_rpath;
203 	    rflagsp = &unit.ship.shp_rflags;
204 	} else {
205 	    if (nunits++ == 0) {
206 		if (player->god)
207 		    pr("own ");
208 		pr("lnd#     unit type       x,y   ar path       as army? flags\n");
209 	    }
210 	    name = lchr[unit.land.lnd_type].l_name;
211 	    rpath = unit.land.lnd_rpath;
212 	    rflagsp = &unit.land.lnd_rflags;
213 	}
214 	if (player->god)
215 	    pr("%3d ", unit.gen.own);
216 	pr("%4d ", np->cur);
217 	pr("%-16.16s ", name);
218 	prxy("%4d,%-4d ", unit.gen.x, unit.gen.y);
219 	pr("%1.1s", &unit.gen.group);
220 	pr(" %-11s", rpath);
221 	rflags = *rflagsp;
222 	if (rflags & RET_GROUP)
223 	    pr("Yes      ");
224 	else
225 	    pr("         ");
226 	for (i = 1; rflagsc[i]; i++) {
227 	    if ((1 << i) & rflags) {
228 		if (CANT_HAPPEN(rflagsc[i] == 'X'))
229 		    continue;
230 		pr("%c", rflagsc[i]);
231 	    }
232 	}
233 	pr("\n");
234     }
235     what = type == EF_SHIP ? "ship" : "unit";
236     if (nunits == 0) {
237 	if (player->argp[1])
238 	    pr("%s: No %s(s)\n", player->argp[1], what);
239 	else
240 	    pr("%s: No %s(s)\n", "", what);
241 	return RET_FAIL;
242     } else
243 	pr("%d %s%s\n", nunits, what, splur(nunits));
244     return RET_OK;
245 }
246