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