]> git.pond.sub.org Git - empserver/blob - src/lib/subs/list.c
Indented with src/scripts/indent-emp.
[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     double odds;
139
140     first = 1;
141     units = 0;
142     snxtitem_xy(&ni, EF_LAND, x, y);
143     while (nxtitem(&ni, (s_char *)&land)) {
144         if (land.lnd_effic < LAND_MINEFF || land.lnd_own == 0)
145             continue;
146         /* Can't bomb units on ships or other units */
147         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
148             continue;
149         lp = &lchr[(int)land.lnd_type];
150
151         if (wantflags) {
152             if ((lp->l_flags & wantflags) == 0)
153                 continue;
154         }
155         if (nowantflags) {
156             if (lp->l_flags & nowantflags)
157                 continue;
158         }
159
160         if (lp->l_flags & L_SPY) {
161             odds = (double)(100 - land.lnd_effic) + 0.10;
162             if (!(chance(odds)))
163                 continue;
164         }
165
166         if (first) {
167             pr(" #          owner           eff       type\n");
168             first = 0;
169         }
170         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
171            cname(land.lnd_own), effadv(land.lnd_effic), prland(&land));
172         units++;
173     }
174     return units;
175 }
176
177 int
178 planesatxy(coord x, coord y, int wantflags, int nowantflags,
179            struct emp_qelem *list)
180 {
181     int first;
182     int planes;
183     struct plnstr plane;
184     struct nstr_item ni;
185     struct plchrstr *plp;
186
187     planes = 0;
188     first = 1;
189     snxtitem_xy(&ni, EF_PLANE, x, y);
190     while (nxtitem(&ni, (s_char *)&plane)) {
191         if (plane.pln_effic < PLANE_MINEFF || plane.pln_own == 0)
192             continue;
193         if (plane.pln_flags & PLN_LAUNCHED)
194             continue;
195         /* Is this plane one of the ones flying somewhere? */
196         if (ac_isflying(&plane, list))
197             continue;
198         plp = &plchr[(int)plane.pln_type];
199         if (first) {
200             pr(" #          owner           eff       type\n");
201             first = 0;
202         }
203         if (wantflags) {
204             if ((plp->pl_flags & wantflags) == 0)
205                 continue;
206         }
207         if (nowantflags) {
208             if (plp->pl_flags & nowantflags)
209                 continue;
210         }
211         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
212            cname(plane.pln_own), effadv(plane.pln_effic), prplane(&plane));
213         planes++;
214     }
215     return planes;
216 }
217
218 int
219 asw_shipsatxy(coord x, coord y, int wantflags, int nowantflags,
220               struct plnstr *pp, struct shiplook *head)
221 {
222     int first;
223     int ships;
224     struct nstr_item ni;
225     struct mchrstr *mp;
226     struct shpstr ship;
227
228     first = 1;
229     ships = 0;
230     snxtitem_xy(&ni, EF_SHIP, x, y);
231     while (nxtitem(&ni, (s_char *)&ship)) {
232         if (player->owner)
233             continue;
234         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
235             continue;
236         mp = &mchr[(int)ship.shp_type];
237         if (wantflags) {
238             if ((mp->m_flags & wantflags) == 0)
239                 continue;
240         }
241         if (nowantflags) {
242             if (mp->m_flags & nowantflags)
243                 continue;
244         }
245         if (mp->m_flags & M_SUB) {
246             if (roll(100) > pln_hitchance(pp,
247                                           shp_hardtarget(&ship), EF_SHIP))
248                 continue;
249         }
250         set_have_found(ship.shp_uid, head);
251         if (first) {
252             pr(" #          owner           eff       type\n");
253             first = 0;
254         }
255         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
256            cname(ship.shp_own), effadv(ship.shp_effic), prship(&ship));
257         ships++;
258     }
259     return ships;
260 }
261
262 int
263 num_shipsatxy(coord x, coord y, int wantflags, int nowantflags)
264 {
265     int ships;
266     struct nstr_item ni;
267     struct mchrstr *mp;
268     struct shpstr ship;
269
270     ships = 0;
271     snxtitem_xy(&ni, EF_SHIP, x, y);
272     while (nxtitem(&ni, (s_char *)&ship)) {
273         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
274             continue;
275         mp = &mchr[(int)ship.shp_type];
276         if (wantflags) {
277             if ((mp->m_flags & wantflags) == 0)
278                 continue;
279         }
280         if (nowantflags) {
281             if (mp->m_flags & nowantflags)
282                 continue;
283         }
284         ships++;
285     }
286     return ships;
287 }
288
289 /*
290  * is p a list of ships/planes/units?
291  *
292  */
293
294 int
295 islist(s_char *p)
296 {
297     register int x;
298
299     x = 0;
300
301     while (*(p + x)) {
302         if (!isdigit(*(p + x)) && (*(p + x) != '/'))
303             return 0;
304
305         x++;
306     }
307
308     return 1;
309 }