]> git.pond.sub.org Git - empserver/blob - src/lib/subs/list.c
0aa111ddd0453afeafc2e8a528b6d3e57cfd3e7f
[empserver] / src / lib / subs / list.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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, natid own)
89 {
90     int first;
91     int ships;
92     struct nstr_item ni;
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 ((carrier_planes(&ship, 0) & (P_L | P_K)) == 0)
105             continue;
106         if (first) {
107             pr(" #          owner           eff       type\n");
108             first = 0;
109         }
110         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
111            cname(ship.shp_own), effadv(ship.shp_effic), prship(&ship));
112         ships++;
113     }
114     return ships;
115 }
116
117 int
118 unitsatxy(coord x, coord y, int wantflags, int nowantflags)
119 {
120     int first;
121     int units;
122     struct nstr_item ni;
123     struct lchrstr *lp;
124     struct lndstr land;
125
126     first = 1;
127     units = 0;
128     snxtitem_xy(&ni, EF_LAND, x, y);
129     while (nxtitem(&ni, &land)) {
130         if (land.lnd_effic < LAND_MINEFF || land.lnd_own == 0)
131             continue;
132         /* Can't bomb units on ships or other units */
133         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
134             continue;
135         lp = &lchr[(int)land.lnd_type];
136
137         if (wantflags) {
138             if ((lp->l_flags & wantflags) == 0)
139                 continue;
140         }
141         if (nowantflags) {
142             if (lp->l_flags & nowantflags)
143                 continue;
144         }
145
146         if (lp->l_flags & L_SPY) {
147             if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
148                 continue;
149         }
150
151         if (first) {
152             pr(" #          owner           eff       type\n");
153             first = 0;
154         }
155         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
156            cname(land.lnd_own), effadv(land.lnd_effic), prland(&land));
157         units++;
158     }
159     return units;
160 }
161
162 int
163 planesatxy(coord x, coord y, int wantflags, int nowantflags,
164            struct emp_qelem *list)
165 {
166     int first;
167     int planes;
168     struct plnstr plane;
169     struct nstr_item ni;
170     struct plchrstr *plp;
171
172     planes = 0;
173     first = 1;
174     snxtitem_xy(&ni, EF_PLANE, x, y);
175     while (nxtitem(&ni, &plane)) {
176         if (plane.pln_effic < PLANE_MINEFF || plane.pln_own == 0)
177             continue;
178         if (plane.pln_flags & PLN_LAUNCHED)
179             continue;
180         /* Is this plane one of the ones flying somewhere? */
181         if (ac_isflying(&plane, list))
182             continue;
183         plp = &plchr[(int)plane.pln_type];
184         if (first) {
185             pr(" #          owner           eff       type\n");
186             first = 0;
187         }
188         if (wantflags) {
189             if ((plp->pl_flags & wantflags) == 0)
190                 continue;
191         }
192         if (nowantflags) {
193             if (plp->pl_flags & nowantflags)
194                 continue;
195         }
196         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
197            cname(plane.pln_own), effadv(plane.pln_effic), prplane(&plane));
198         planes++;
199     }
200     return planes;
201 }
202
203 int
204 asw_shipsatxy(coord x, coord y, int wantflags, int nowantflags,
205               struct plnstr *pp, struct shiplist **head)
206 {
207     int first;
208     int ships;
209     struct nstr_item ni;
210     struct mchrstr *mp;
211     struct shpstr ship;
212
213     first = 1;
214     ships = 0;
215     snxtitem_xy(&ni, EF_SHIP, x, y);
216     while (nxtitem(&ni, &ship)) {
217         if (player->owner)
218             continue;
219         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
220             continue;
221         mp = &mchr[(int)ship.shp_type];
222         if (wantflags) {
223             if ((mp->m_flags & wantflags) == 0)
224                 continue;
225         }
226         if (nowantflags) {
227             if (mp->m_flags & nowantflags)
228                 continue;
229         }
230         if (mp->m_flags & M_SUB) {
231             if (roll(100) > pln_hitchance(pp,
232                                           shp_hardtarget(&ship), EF_SHIP))
233                 continue;
234         }
235         add_shiplist(ship.shp_uid, head);
236         if (first) {
237             pr(" #          owner           eff       type\n");
238             first = 0;
239         }
240         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
241            cname(ship.shp_own), effadv(ship.shp_effic), prship(&ship));
242         ships++;
243     }
244     return ships;
245 }
246
247 int
248 num_shipsatxy(coord x, coord y, int wantflags, int nowantflags)
249 {
250     int ships;
251     struct nstr_item ni;
252     struct mchrstr *mp;
253     struct shpstr ship;
254
255     ships = 0;
256     snxtitem_xy(&ni, EF_SHIP, x, y);
257     while (nxtitem(&ni, &ship)) {
258         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
259             continue;
260         mp = &mchr[(int)ship.shp_type];
261         if (wantflags) {
262             if ((mp->m_flags & wantflags) == 0)
263                 continue;
264         }
265         if (nowantflags) {
266             if (mp->m_flags & nowantflags)
267                 continue;
268         }
269         ships++;
270     }
271     return ships;
272 }
273
274 /*
275  * is p a list of ships/planes/units?
276  */
277
278 int
279 islist(char *p)
280 {
281     for (; *p; p++) {
282         if (!isdigit(*p) && *p != '/')
283             return 0;
284     }
285     return 1;
286 }