]> git.pond.sub.org Git - empserver/blob - src/lib/commands/retr.c
758dba9b1e105d6ee6874d7c22f52f86f41b838e
[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-2010
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 "retreat.h"
42 #include "ship.h"
43
44 /*
45  * Retreat flag characters
46  * 'X' means flag is not available
47  * Must agree with RET_ defines.
48  */
49 static char shp_rflagsc[] = "Xitshbdu";
50 static char lnd_rflagsc[] = "XiXXhbXX";
51
52 static int retreat(int);
53
54 int
55 retr(void)
56 {
57     return retreat(EF_SHIP);
58 }
59
60 int
61 lretr(void)
62 {
63     return retreat(EF_LAND);
64 }
65
66 static int
67 retreat(int type)
68 {
69     char *pq, *fl;
70     int nunits;
71     struct nstr_item ni;
72     union empobj_storage unit;
73     int rflags, ch, j;
74     unsigned i;
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     if (player->argp[2] != NULL)
88         pq = getstarg(player->argp[2], "Retreat path? ", buf1);
89     else
90         pq = NULL;
91
92     rflags = 0;
93     if (pq != NULL) {
94     again:
95         fl = getstarg(player->argp[3],
96                       "Retreat conditions ('?' to list available ones)? ",
97                       buf2);
98         if (!fl)
99             return RET_SYN;
100
101         for (i = 0; fl[i]; i++) {
102             ch = tolower(fl[i]);
103             if (ch == 'c') {
104                 *pq = 0;
105                 break;
106             }
107             if (ch == '?') {
108                 for (j = 1; rflagsc[j]; j++) {
109                     if (rflagsc[j] != 'X')
110                         pr("%c\tretreat when %s\n",
111                            rflagsc[j],
112                            symbol_by_value(1 << j, retreat_flags));
113                 }
114                 pr("c\tcancel retreat order\n");
115                 goto again;
116             }
117             p = strchr(rflagsc, ch);
118             if (!p) {
119                 pr("Bad retreat condition '%c'\n", fl[i]);
120                 return RET_SYN;
121             }
122             rflags |= 1 << (p - rflagsc);
123         }
124         if (*pq && !rflags) {
125             pr("Must give retreat conditions!\n");
126             return RET_FAIL;
127         }
128         if (ni.sel == NS_GROUP && ni.group)
129             rflags |= RET_GROUP;
130         if (!*pq)
131             rflags = 0;
132     }
133
134     while (nxtitem(&ni, &unit)) {
135         if (!player->owner || unit.gen.own == 0)
136             continue;
137         if (type == EF_SHIP) {
138             if (nunits++ == 0) {
139                 if (player->god)
140                     pr("own ");
141                 pr("shp#     ship type       x,y   fl path       as flt?  flags\n");
142             }
143             name = mchr[unit.ship.shp_type].m_name;
144             rpath = unit.ship.shp_rpath;
145             rflagsp = &unit.ship.shp_rflags;
146         } else {
147             if (nunits++ == 0) {
148                 if (player->god)
149                     pr("own ");
150                 pr("lnd#     unit type       x,y   ar path       as army? flags\n");
151             }
152             name = lchr[unit.land.lnd_type].l_name;
153             rpath = unit.land.lnd_rpath;
154             rflagsp = &unit.land.lnd_rflags;
155         }
156         if (pq) {
157             strncpy(rpath, pq, RET_LEN - 1);
158             *rflagsp = rflags;
159             put_empobj(type, unit.gen.uid, &unit);
160         }
161         if (player->god)
162             pr("%3d ", unit.gen.own);
163         pr("%4d ", ni.cur);
164         pr("%-16.16s ", name);
165         prxy("%4d,%-4d ", unit.gen.x, unit.gen.y);
166         pr("%1.1s", &unit.gen.group);
167         pr(" %-11s", rpath);
168         rflags = *rflagsp;
169         if (rflags & RET_GROUP)
170             pr("Yes      ");
171         else
172             pr("         ");
173         for (j = 1; rflagsc[j]; j++) {
174             if ((1 << j) & rflags) {
175                 if (CANT_HAPPEN(rflagsc[j] == 'X'))
176                     continue;
177                 pr("%c", rflagsc[j]);
178             }
179         }
180         pr("\n");
181     }
182     what = type == EF_SHIP ? "ship" : "unit";
183     if (nunits == 0) {
184         if (player->argp[1])
185             pr("%s: No %s(s)\n", player->argp[1], what);
186         else
187             pr("%s: No %s(s)\n", "", what);
188         return RET_FAIL;
189     } else
190         pr("%d %s%s\n", nunits, what, splur(nunits));
191     return RET_OK;
192 }