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
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.
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.
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/>.
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.
27 * retr.c: Set retreat conditionals for ships and land units
29 * Known contributors to this file:
32 * Markus Armbruster, 2008-2015
46 * Retreat flag characters
47 * 'X' means flag is not available
48 * Must agree with RET_ defines.
50 static char shp_rflagsc[] = "Xitshbdu";
51 static char lnd_rflagsc[] = "XiXXhbXX";
53 static int retreat(int);
54 static int retreat_show(int, struct nstr_item *);
59 return retreat(EF_SHIP);
65 return retreat(EF_LAND);
74 union empobj_storage unit;
80 if (CANT_HAPPEN(type != EF_LAND && type != EF_SHIP))
82 rflagsc = type == EF_SHIP ? shp_rflagsc : lnd_rflagsc;
84 if (!snxtitem(&ni, type, player->argp[1], NULL))
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",
94 * TODO getpath() or similar would be nice once the deprecated
97 pq = getstarg(player->argp[2], "Retreat path, or q to query? ",
104 return retreat_show(type, &ni);
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);
113 for (i--; i >= 0 && pq[i] == dirch[DIR_STOP]; i--)
119 fl = getstarg(player->argp[3],
120 "Retreat conditions ('?' to list available ones)? ",
125 for (i = 0; fl[i]; i++) {
128 /* Deprecated, but keeping it around doesn't hurt */
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",
137 symbol_by_value(1 << j, retreat_flags));
141 p = strchr(rflagsc, ch);
143 pr("Bad retreat condition '%c'\n", fl[i]);
146 rflags |= 1 << (p - rflagsc);
150 if (ni.sel == NS_GROUP && ni.group)
157 while (nxtitem(&ni, &unit)) {
158 if (!player->owner || unit.gen.own == 0)
160 if (type == EF_SHIP) {
161 strncpy(unit.ship.shp_rpath, pq, RET_LEN - 1);
162 unit.ship.shp_rflags = rflags;
164 strncpy(unit.land.lnd_rpath, pq, RET_LEN - 1);
165 unit.land.lnd_rflags = rflags;
167 put_empobj(type, unit.gen.uid, &unit);
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);
177 pr("%d %s%s ordered not to retreat\n",
178 nunits, ef_nameof_pretty(type), splur(nunits));
183 retreat_show(int type, struct nstr_item *np)
185 char *rflagsc = type == EF_SHIP ? shp_rflagsc : lnd_rflagsc;
186 union empobj_storage unit;
188 char *name, *rpath, *what;
189 int *rflagsp, rflags, i;
192 while (nxtitem(np, &unit)) {
193 if (!player->owner || unit.gen.own == 0)
195 if (type == EF_SHIP) {
199 pr("shp# ship type x,y fl path as flt? flags\n");
201 name = mchr[unit.ship.shp_type].m_name;
202 rpath = unit.ship.shp_rpath;
203 rflagsp = &unit.ship.shp_rflags;
208 pr("lnd# unit type x,y ar path as army? flags\n");
210 name = lchr[unit.land.lnd_type].l_name;
211 rpath = unit.land.lnd_rpath;
212 rflagsp = &unit.land.lnd_rflags;
215 pr("%3d ", unit.gen.own);
217 pr("%-16.16s ", name);
218 prxy("%4d,%-4d ", unit.gen.x, unit.gen.y);
219 pr("%1.1s", &unit.gen.group);
222 if (rflags & RET_GROUP)
226 for (i = 1; rflagsc[i]; i++) {
227 if ((1 << i) & rflags) {
228 if (CANT_HAPPEN(rflagsc[i] == 'X'))
230 pr("%c", rflagsc[i]);
235 what = type == EF_SHIP ? "ship" : "unit";
238 pr("%s: No %s(s)\n", player->argp[1], what);
240 pr("%s: No %s(s)\n", "", what);
243 pr("%d %s%s\n", nunits, what, splur(nunits));