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