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