]> 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-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  list.c: List ships, planes, units at a given x,y
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Markus Armbruster, 2003-2012
32  */
33
34 #include <config.h>
35
36 #include <ctype.h>
37 #include "chance.h"
38 #include "file.h"
39 #include "land.h"
40 #include "misc.h"
41 #include "nat.h"
42 #include "nsc.h"
43 #include "path.h"
44 #include "plane.h"
45 #include "player.h"
46 #include "prototypes.h"
47 #include "sect.h"
48 #include "ship.h"
49 #include "xy.h"
50
51 int
52 shipsatxy(coord x, coord y, int wantflags, int nowantflags, int only_count)
53 {
54     int first;
55     int ships;
56     struct nstr_item ni;
57     struct mchrstr *mp;
58     struct shpstr ship;
59
60     first = 1;
61     ships = 0;
62     snxtitem_xy(&ni, EF_SHIP, x, y);
63     while (nxtitem(&ni, &ship)) {
64         if (player->owner)
65             continue;
66         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
67             continue;
68         mp = &mchr[(int)ship.shp_type];
69         if (wantflags) {
70             if ((mp->m_flags & wantflags) == 0)
71                 continue;
72         }
73         if (nowantflags) {
74             if (mp->m_flags & nowantflags)
75                 continue;
76         }
77         if (!only_count) {
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         }
85         ships++;
86     }
87     return ships;
88 }
89
90 /* This one only shows owned or allied ships */
91
92 int
93 carriersatxy(coord x, coord y, natid own)
94 {
95     int first;
96     int ships;
97     struct nstr_item ni;
98     struct shpstr ship;
99
100     first = 1;
101     ships = 0;
102     snxtitem_xy(&ni, EF_SHIP, x, y);
103     while (nxtitem(&ni, &ship)) {
104         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
105             continue;
106         if (relations_with(ship.shp_own, own) != ALLIED)
107             continue;
108         if ((carrier_planes(&ship, 0) & (P_L | P_K)) == 0)
109             continue;
110         if (first) {
111             pr(" #          owner           eff       type\n");
112             first = 0;
113         }
114         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
115            cname(ship.shp_own), effadv(ship.shp_effic), prship(&ship));
116         ships++;
117     }
118     return ships;
119 }
120
121 int
122 unitsatxy(coord x, coord y, int wantflags, int nowantflags)
123 {
124     int first;
125     int units;
126     struct nstr_item ni;
127     struct lchrstr *lp;
128     struct lndstr land;
129
130     first = 1;
131     units = 0;
132     snxtitem_xy(&ni, EF_LAND, x, y);
133     while (nxtitem(&ni, &land)) {
134         if (land.lnd_effic < LAND_MINEFF || land.lnd_own == 0)
135             continue;
136         /* Can't bomb units on ships or other units */
137         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
138             continue;
139         lp = &lchr[(int)land.lnd_type];
140
141         if (wantflags) {
142             if ((lp->l_flags & wantflags) == 0)
143                 continue;
144         }
145         if (nowantflags) {
146             if (lp->l_flags & nowantflags)
147                 continue;
148         }
149
150         if (lp->l_flags & L_SPY) {
151             if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
152                 continue;
153         }
154
155         if (first) {
156             pr(" #          owner           eff       type\n");
157             first = 0;
158         }
159         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
160            cname(land.lnd_own), effadv(land.lnd_effic), prland(&land));
161         units++;
162     }
163     return units;
164 }
165
166 int
167 planesatxy(coord x, coord y, int wantflags, int nowantflags)
168 {
169     int first;
170     int planes;
171     struct plnstr plane;
172     struct nstr_item ni;
173     struct plchrstr *plp;
174
175     planes = 0;
176     first = 1;
177     snxtitem_xy(&ni, EF_PLANE, x, y);
178     while (nxtitem(&ni, &plane)) {
179         if (plane.pln_effic < PLANE_MINEFF || plane.pln_own == 0)
180             continue;
181         if (plane.pln_ship >= 0 || plane.pln_land >= 0)
182             continue;
183         if (plane.pln_flags & PLN_LAUNCHED)
184             continue;
185         plp = &plchr[(int)plane.pln_type];
186         if (first) {
187             pr(" #          owner           eff       type\n");
188             first = 0;
189         }
190         if (wantflags) {
191             if ((plp->pl_flags & wantflags) == 0)
192                 continue;
193         }
194         if (nowantflags) {
195             if (plp->pl_flags & nowantflags)
196                 continue;
197         }
198         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
199            cname(plane.pln_own), effadv(plane.pln_effic), prplane(&plane));
200         planes++;
201     }
202     return planes;
203 }
204
205 int
206 asw_shipsatxy(coord x, coord y, int wantflags, int nowantflags,
207               struct plnstr *pp, struct shiplist **head)
208 {
209     int first;
210     int ships;
211     struct nstr_item ni;
212     struct mchrstr *mp;
213     struct shpstr ship;
214
215     first = 1;
216     ships = 0;
217     snxtitem_xy(&ni, EF_SHIP, x, y);
218     while (nxtitem(&ni, &ship)) {
219         if (player->owner)
220             continue;
221         if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
222             continue;
223         mp = &mchr[(int)ship.shp_type];
224         if (wantflags) {
225             if ((mp->m_flags & wantflags) == 0)
226                 continue;
227         }
228         if (nowantflags) {
229             if (mp->m_flags & nowantflags)
230                 continue;
231         }
232         if (mp->m_flags & M_SUB) {
233             if (!pct_chance(pln_hitchance(pp,
234                                           shp_hardtarget(&ship), EF_SHIP)))
235                 continue;
236         }
237         add_shiplist(ship.shp_uid, head);
238         if (first) {
239             pr(" #          owner           eff       type\n");
240             first = 0;
241         }
242         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
243            cname(ship.shp_own), effadv(ship.shp_effic), prship(&ship));
244         ships++;
245     }
246     return ships;
247 }
248
249 int
250 adj_units(coord x, coord y, natid own)
251 {
252     int i;
253     struct sctstr sect;
254
255     for (i = DIR_FIRST; i <= DIR_LAST; i++) {
256         getsect(x + diroff[i][0], y + diroff[i][1], &sect);
257         if (has_units(sect.sct_x, sect.sct_y, own, NULL))
258             return 1;
259     }
260     return 0;
261 }
262
263 int
264 has_units(coord x, coord y, natid cn, struct lndstr *lp)
265 {
266     int n;
267     struct lndstr land;
268
269     for (n = 0; ef_read(EF_LAND, n, &land); n++) {
270         if (land.lnd_x != x || land.lnd_y != y)
271             continue;
272         if (lp) {
273             /* Check this unit.  If it is this one, we don't want
274                it included in the count. */
275             if (lp->lnd_uid == land.lnd_uid)
276                 continue;
277         }
278         if (land.lnd_own == cn)
279             return 1;
280     }
281
282     return 0;
283 }
284
285 /*
286  * is p a list of ships/planes/units?
287  */
288
289 int
290 islist(char *p)
291 {
292     for (; *p; p++) {
293         if (!isdigit(*p) && *p != '/')
294             return 0;
295     }
296     return 1;
297 }