]> git.pond.sub.org Git - empserver/blob - src/lib/subs/list.c
(LND_SPY_DETECT_CHANCE): New.
[empserver] / src / lib / subs / list.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
35 #include "player.h"
36 #include "var.h"
37 #include "news.h"
38 #include "ship.h"
39 #include "land.h"
40 #include "sect.h"
41 #include "nuke.h"
42 #include "plane.h"
43 #include "nat.h"
44 #include "item.h"
45 #include "xy.h"
46 #include "nsc.h"
47 #include "file.h"
48 #include "prototypes.h"
49
50 extern s_char *effadv(int);
51
52 int
53 shipsatxy(coord x, coord y, int wantflags, int nowantflags)
54 {
55     int first;
56     int ships;
57     struct nstr_item ni;
58     struct mchrstr *mp;
59     struct shpstr ship;
60
61     first = 1;
62     ships = 0;
63     snxtitem_xy(&ni, EF_SHIP, x, y);
64     while (nxtitem(&ni, (s_char *)&ship)) {
65         if (player->owner)
66             continue;
67         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
68             continue;
69         mp = &mchr[(int)ship.shp_type];
70         if (wantflags) {
71             if ((mp->m_flags & wantflags) == 0)
72                 continue;
73         }
74         if (nowantflags) {
75             if (mp->m_flags & nowantflags)
76                 continue;
77         }
78         if (first) {
79             pr(" #          owner           eff       type\n");
80             first = 0;
81         }
82         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
83            cname(ship.shp_own), effadv(ship.shp_effic), prship(&ship));
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, int wantflags, int nowantflags, natid own)
93 {
94     int first;
95     int ships;
96     struct nstr_item ni;
97     struct mchrstr *mp;
98     struct shpstr ship;
99     int allied;
100
101     first = 1;
102     ships = 0;
103     snxtitem_xy(&ni, EF_SHIP, x, y);
104     while (nxtitem(&ni, (s_char *)&ship)) {
105         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
106             continue;
107         allied = (getrel(getnatp(ship.shp_own), own) == ALLIED);
108         if ((ship.shp_own != own) & !allied)
109             continue;
110         mp = &mchr[(int)ship.shp_type];
111         if (wantflags) {
112             if ((mp->m_flags & wantflags) == 0)
113                 continue;
114         }
115         if (nowantflags) {
116             if (mp->m_flags & nowantflags)
117                 continue;
118         }
119         if (first) {
120             pr(" #          owner           eff       type\n");
121             first = 0;
122         }
123         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
124            cname(ship.shp_own), effadv(ship.shp_effic), prship(&ship));
125         ships++;
126     }
127     return ships;
128 }
129
130 int
131 unitsatxy(coord x, coord y, int wantflags, int nowantflags)
132 {
133     int first;
134     int units;
135     struct nstr_item ni;
136     struct lchrstr *lp;
137     struct lndstr land;
138
139     first = 1;
140     units = 0;
141     snxtitem_xy(&ni, EF_LAND, x, y);
142     while (nxtitem(&ni, (s_char *)&land)) {
143         if (land.lnd_effic < LAND_MINEFF || land.lnd_own == 0)
144             continue;
145         /* Can't bomb units on ships or other units */
146         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
147             continue;
148         lp = &lchr[(int)land.lnd_type];
149
150         if (wantflags) {
151             if ((lp->l_flags & wantflags) == 0)
152                 continue;
153         }
154         if (nowantflags) {
155             if (lp->l_flags & nowantflags)
156                 continue;
157         }
158
159         if (lp->l_flags & L_SPY) {
160             if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
161                 continue;
162         }
163
164         if (first) {
165             pr(" #          owner           eff       type\n");
166             first = 0;
167         }
168         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
169            cname(land.lnd_own), effadv(land.lnd_effic), prland(&land));
170         units++;
171     }
172     return units;
173 }
174
175 int
176 planesatxy(coord x, coord y, int wantflags, int nowantflags,
177            struct emp_qelem *list)
178 {
179     int first;
180     int planes;
181     struct plnstr plane;
182     struct nstr_item ni;
183     struct plchrstr *plp;
184
185     planes = 0;
186     first = 1;
187     snxtitem_xy(&ni, EF_PLANE, x, y);
188     while (nxtitem(&ni, (s_char *)&plane)) {
189         if (plane.pln_effic < PLANE_MINEFF || plane.pln_own == 0)
190             continue;
191         if (plane.pln_flags & PLN_LAUNCHED)
192             continue;
193         /* Is this plane one of the ones flying somewhere? */
194         if (ac_isflying(&plane, list))
195             continue;
196         plp = &plchr[(int)plane.pln_type];
197         if (first) {
198             pr(" #          owner           eff       type\n");
199             first = 0;
200         }
201         if (wantflags) {
202             if ((plp->pl_flags & wantflags) == 0)
203                 continue;
204         }
205         if (nowantflags) {
206             if (plp->pl_flags & nowantflags)
207                 continue;
208         }
209         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
210            cname(plane.pln_own), effadv(plane.pln_effic), prplane(&plane));
211         planes++;
212     }
213     return planes;
214 }
215
216 int
217 asw_shipsatxy(coord x, coord y, int wantflags, int nowantflags,
218               struct plnstr *pp, struct shiplook *head)
219 {
220     int first;
221     int ships;
222     struct nstr_item ni;
223     struct mchrstr *mp;
224     struct shpstr ship;
225
226     first = 1;
227     ships = 0;
228     snxtitem_xy(&ni, EF_SHIP, x, y);
229     while (nxtitem(&ni, (s_char *)&ship)) {
230         if (player->owner)
231             continue;
232         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
233             continue;
234         mp = &mchr[(int)ship.shp_type];
235         if (wantflags) {
236             if ((mp->m_flags & wantflags) == 0)
237                 continue;
238         }
239         if (nowantflags) {
240             if (mp->m_flags & nowantflags)
241                 continue;
242         }
243         if (mp->m_flags & M_SUB) {
244             if (roll(100) > pln_hitchance(pp,
245                                           shp_hardtarget(&ship), EF_SHIP))
246                 continue;
247         }
248         set_have_found(ship.shp_uid, head);
249         if (first) {
250             pr(" #          owner           eff       type\n");
251             first = 0;
252         }
253         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
254            cname(ship.shp_own), effadv(ship.shp_effic), prship(&ship));
255         ships++;
256     }
257     return ships;
258 }
259
260 int
261 num_shipsatxy(coord x, coord y, int wantflags, int nowantflags)
262 {
263     int ships;
264     struct nstr_item ni;
265     struct mchrstr *mp;
266     struct shpstr ship;
267
268     ships = 0;
269     snxtitem_xy(&ni, EF_SHIP, x, y);
270     while (nxtitem(&ni, (s_char *)&ship)) {
271         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
272             continue;
273         mp = &mchr[(int)ship.shp_type];
274         if (wantflags) {
275             if ((mp->m_flags & wantflags) == 0)
276                 continue;
277         }
278         if (nowantflags) {
279             if (mp->m_flags & nowantflags)
280                 continue;
281         }
282         ships++;
283     }
284     return ships;
285 }
286
287 /*
288  * is p a list of ships/planes/units?
289  *
290  */
291
292 int
293 islist(s_char *p)
294 {
295     register int x;
296
297     x = 0;
298
299     while (*(p + x)) {
300         if (!isdigit(*(p + x)) && (*(p + x) != '/'))
301             return 0;
302
303         x++;
304     }
305
306     return 1;
307 }