]> git.pond.sub.org Git - empserver/blob - src/lib/commands/retr.c
cf2fad530a22af86ade51513ab19e92b2b18a213
[empserver] / src / lib / commands / retr.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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
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 rflags, ch, j;
75     unsigned i;
76     char *rflagsc, *p, *name, *rpath, *what;
77     int *rflagsp;
78     char buf1[1024];
79     char buf2[1024];
80
81     if (CANT_HAPPEN(type != EF_LAND && type != EF_SHIP))
82         type = EF_SHIP;
83     rflagsc = type == EF_SHIP ? shp_rflagsc : lnd_rflagsc;
84
85     if (!snxtitem(&ni, type, player->argp[1], NULL))
86         return RET_SYN;
87     nunits = 0;
88     if (player->argp[2] != NULL)
89         pq = getstarg(player->argp[2], "Retreat path? ", buf1);
90     else
91         pq = NULL;
92
93     rflags = 0;
94     if (pq != NULL) {
95     again:
96         fl = getstarg(player->argp[3],
97                       "Retreat conditions ('?' to list available ones)? ",
98                       buf2);
99         if (!fl)
100             return RET_SYN;
101
102         for (i = 0; fl[i]; i++) {
103             ch = tolower(fl[i]);
104             if (ch == 'c') {
105                 *pq = 0;
106                 break;
107             }
108             if (ch == '?') {
109                 for (j = 1; rflagsc[j]; j++) {
110                     if (rflagsc[j] != 'X')
111                         pr("%c\tretreat when %s\n",
112                            rflagsc[j],
113                            symbol_by_value(1 << j, retreat_flags));
114                 }
115                 pr("c\tcancel retreat order\n");
116                 goto again;
117             }
118             p = strchr(rflagsc, ch);
119             if (!p) {
120                 pr("Bad retreat condition '%c'\n", fl[i]);
121                 return RET_SYN;
122             }
123             rflags |= 1 << (p - rflagsc);
124         }
125         if (*pq && !rflags) {
126             pr("Must give retreat conditions!\n");
127             return RET_FAIL;
128         }
129         if (ni.sel == NS_GROUP && ni.group)
130             rflags |= RET_GROUP;
131         if (!*pq)
132             rflags = 0;
133     }
134
135     while (nxtitem(&ni, &unit)) {
136         if (!player->owner || unit.gen.own == 0)
137             continue;
138         if (type == EF_SHIP) {
139             if (nunits++ == 0) {
140                 if (player->god)
141                     pr("own ");
142                 pr("shp#     ship type       x,y   fl path       as flt?  flags\n");
143             }
144             name = mchr[unit.ship.shp_type].m_name;
145             rpath = unit.ship.shp_rpath;
146             rflagsp = &unit.ship.shp_rflags;
147         } else {
148             if (nunits++ == 0) {
149                 if (player->god)
150                     pr("own ");
151                 pr("lnd#     unit type       x,y   ar path       as army? flags\n");
152             }
153             name = lchr[unit.land.lnd_type].l_name;
154             rpath = unit.land.lnd_rpath;
155             rflagsp = &unit.land.lnd_rflags;
156         }
157         if (pq) {
158             strncpy(rpath, pq, RET_LEN - 1);
159             *rflagsp = rflags;
160             put_empobj(type, unit.gen.uid, &unit);
161         }
162         if (player->god)
163             pr("%3d ", unit.gen.own);
164         pr("%4d ", ni.cur);
165         pr("%-16.16s ", name);
166         prxy("%4d,%-4d ", unit.gen.x, unit.gen.y);
167         pr("%1.1s", &unit.gen.group);
168         pr(" %-11s", rpath);
169         rflags = *rflagsp;
170         if (rflags & RET_GROUP)
171             pr("Yes      ");
172         else
173             pr("         ");
174         for (j = 1; rflagsc[j]; j++) {
175             if ((1 << j) & rflags) {
176                 if (CANT_HAPPEN(rflagsc[j] == 'X'))
177                     continue;
178                 pr("%c", rflagsc[j]);
179             }
180         }
181         pr("\n");
182     }
183     what = type == EF_SHIP ? "ship" : "unit";
184     if (nunits == 0) {
185         if (player->argp[1])
186             pr("%s: No %s(s)\n", player->argp[1], what);
187         else
188             pr("%s: No %s(s)\n", "", what);
189         return RET_FAIL;
190     } else
191         pr("%d %s%s\n", nunits, what, splur(nunits));
192     return RET_OK;
193 }