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