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