]> git.pond.sub.org Git - empserver/blob - src/lib/subs/list.c
License upgrade to GPL version 3 or later
[empserver] / src / lib / subs / list.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  *  list.c: List ships, planes, units at a given x,y
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  */
32
33 #include <config.h>
34
35 #include <ctype.h>
36 #include "file.h"
37 #include "land.h"
38 #include "misc.h"
39 #include "nat.h"
40 #include "nsc.h"
41 #include "path.h"
42 #include "plane.h"
43 #include "player.h"
44 #include "prototypes.h"
45 #include "sect.h"
46 #include "ship.h"
47 #include "xy.h"
48
49 int
50 shipsatxy(coord x, coord y, int wantflags, int nowantflags, int only_count)
51 {
52     int first;
53     int ships;
54     struct nstr_item ni;
55     struct mchrstr *mp;
56     struct shpstr ship;
57
58     first = 1;
59     ships = 0;
60     snxtitem_xy(&ni, EF_SHIP, x, y);
61     while (nxtitem(&ni, &ship)) {
62         if (player->owner)
63             continue;
64         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
65             continue;
66         mp = &mchr[(int)ship.shp_type];
67         if (wantflags) {
68             if ((mp->m_flags & wantflags) == 0)
69                 continue;
70         }
71         if (nowantflags) {
72             if (mp->m_flags & nowantflags)
73                 continue;
74         }
75         if (!only_count) {
76             if (first) {
77                 pr(" #          owner           eff       type\n");
78                 first = 0;
79             }
80             pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
81                cname(ship.shp_own), effadv(ship.shp_effic), prship(&ship));
82         }
83         ships++;
84     }
85     return ships;
86 }
87
88 /* This one only shows owned or allied ships */
89
90 int
91 carriersatxy(coord x, coord y, natid own)
92 {
93     int first;
94     int ships;
95     struct nstr_item ni;
96     struct shpstr ship;
97
98     first = 1;
99     ships = 0;
100     snxtitem_xy(&ni, EF_SHIP, x, y);
101     while (nxtitem(&ni, &ship)) {
102         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
103             continue;
104         if (relations_with(ship.shp_own, own) != ALLIED)
105             continue;
106         if ((carrier_planes(&ship, 0) & (P_L | P_K)) == 0)
107             continue;
108         if (first) {
109             pr(" #          owner           eff       type\n");
110             first = 0;
111         }
112         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
113            cname(ship.shp_own), effadv(ship.shp_effic), prship(&ship));
114         ships++;
115     }
116     return ships;
117 }
118
119 int
120 unitsatxy(coord x, coord y, int wantflags, int nowantflags)
121 {
122     int first;
123     int units;
124     struct nstr_item ni;
125     struct lchrstr *lp;
126     struct lndstr land;
127
128     first = 1;
129     units = 0;
130     snxtitem_xy(&ni, EF_LAND, x, y);
131     while (nxtitem(&ni, &land)) {
132         if (land.lnd_effic < LAND_MINEFF || land.lnd_own == 0)
133             continue;
134         /* Can't bomb units on ships or other units */
135         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
136             continue;
137         lp = &lchr[(int)land.lnd_type];
138
139         if (wantflags) {
140             if ((lp->l_flags & wantflags) == 0)
141                 continue;
142         }
143         if (nowantflags) {
144             if (lp->l_flags & nowantflags)
145                 continue;
146         }
147
148         if (lp->l_flags & L_SPY) {
149             if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
150                 continue;
151         }
152
153         if (first) {
154             pr(" #          owner           eff       type\n");
155             first = 0;
156         }
157         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
158            cname(land.lnd_own), effadv(land.lnd_effic), prland(&land));
159         units++;
160     }
161     return units;
162 }
163
164 int
165 planesatxy(coord x, coord y, int wantflags, int nowantflags)
166 {
167     int first;
168     int planes;
169     struct plnstr plane;
170     struct nstr_item ni;
171     struct plchrstr *plp;
172
173     planes = 0;
174     first = 1;
175     snxtitem_xy(&ni, EF_PLANE, x, y);
176     while (nxtitem(&ni, &plane)) {
177         if (plane.pln_effic < PLANE_MINEFF || plane.pln_own == 0)
178             continue;
179         if (plane.pln_ship >= 0 || plane.pln_land >= 0)
180             continue;
181         if (plane.pln_flags & PLN_LAUNCHED)
182             continue;
183         plp = &plchr[(int)plane.pln_type];
184         if (first) {
185             pr(" #          owner           eff       type\n");
186             first = 0;
187         }
188         if (wantflags) {
189             if ((plp->pl_flags & wantflags) == 0)
190                 continue;
191         }
192         if (nowantflags) {
193             if (plp->pl_flags & nowantflags)
194                 continue;
195         }
196         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
197            cname(plane.pln_own), effadv(plane.pln_effic), prplane(&plane));
198         planes++;
199     }
200     return planes;
201 }
202
203 int
204 asw_shipsatxy(coord x, coord y, int wantflags, int nowantflags,
205               struct plnstr *pp, struct shiplist **head)
206 {
207     int first;
208     int ships;
209     struct nstr_item ni;
210     struct mchrstr *mp;
211     struct shpstr ship;
212
213     first = 1;
214     ships = 0;
215     snxtitem_xy(&ni, EF_SHIP, x, y);
216     while (nxtitem(&ni, &ship)) {
217         if (player->owner)
218             continue;
219         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
220             continue;
221         mp = &mchr[(int)ship.shp_type];
222         if (wantflags) {
223             if ((mp->m_flags & wantflags) == 0)
224                 continue;
225         }
226         if (nowantflags) {
227             if (mp->m_flags & nowantflags)
228                 continue;
229         }
230         if (mp->m_flags & M_SUB) {
231             if (roll(100) > pln_hitchance(pp,
232                                           shp_hardtarget(&ship), EF_SHIP))
233                 continue;
234         }
235         add_shiplist(ship.shp_uid, head);
236         if (first) {
237             pr(" #          owner           eff       type\n");
238             first = 0;
239         }
240         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
241            cname(ship.shp_own), effadv(ship.shp_effic), prship(&ship));
242         ships++;
243     }
244     return ships;
245 }
246
247 int
248 adj_units(coord x, coord y, natid own)
249 {
250     int i;
251     struct sctstr sect;
252
253     for (i = DIR_FIRST; i <= DIR_LAST; i++) {
254         getsect(x + diroff[i][0], y + diroff[i][1], &sect);
255         if (has_units(sect.sct_x, sect.sct_y, own, NULL))
256             return 1;
257     }
258     return 0;
259 }
260
261 int
262 has_units(coord x, coord y, natid cn, struct lndstr *lp)
263 {
264     int n;
265     struct lndstr land;
266
267     for (n = 0; ef_read(EF_LAND, n, &land); n++) {
268         if (land.lnd_x != x || land.lnd_y != y)
269             continue;
270         if (lp) {
271             /* Check this unit.  If it is this one, we don't want
272                it included in the count. */
273             if (lp->lnd_uid == land.lnd_uid)
274                 continue;
275         }
276         if (land.lnd_own == cn)
277             return 1;
278     }
279
280     return 0;
281 }
282
283 /*
284  * is p a list of ships/planes/units?
285  */
286
287 int
288 islist(char *p)
289 {
290     for (; *p; p++) {
291         if (!isdigit(*p) && *p != '/')
292             return 0;
293     }
294     return 1;
295 }