]> 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-2017, 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-2016
32  */
33
34 #include <config.h>
35
36 #include <ctype.h>
37 #include "chance.h"
38 #include "land.h"
39 #include "misc.h"
40 #include "nat.h"
41 #include "nsc.h"
42 #include "path.h"
43 #include "plane.h"
44 #include "player.h"
45 #include "prototypes.h"
46 #include "sect.h"
47 #include "ship.h"
48 #include "xy.h"
49
50 static void
51 list_ship(struct shpstr *sp, int first)
52 {
53     if (first)
54         pr(" #          owner           eff       type\n");
55     pr("(#%3d) %10.10s  %12.12s  %s\n", sp->shp_uid,
56        cname(sp->shp_own), effadv(sp->shp_effic), prship(sp));
57 }
58
59 int
60 shipsatxy(coord x, coord y, int wantflags, int nowantflags, int only_count)
61 {
62     int ships;
63     struct nstr_item ni;
64     struct mchrstr *mp;
65     struct shpstr ship;
66
67     ships = 0;
68     snxtitem_xy(&ni, EF_SHIP, x, y);
69     while (nxtitem(&ni, &ship)) {
70         if (player->owner)
71             continue;
72         if (!ship.shp_own)
73             continue;
74         mp = &mchr[(int)ship.shp_type];
75         if (wantflags) {
76             if ((mp->m_flags & wantflags) == 0)
77                 continue;
78         }
79         if (nowantflags) {
80             if (mp->m_flags & nowantflags)
81                 continue;
82         }
83         if (!only_count)
84             list_ship(&ship, !ships);
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 ships;
96     struct nstr_item ni;
97     struct shpstr ship;
98
99     ships = 0;
100     snxtitem_xy(&ni, EF_SHIP, x, y);
101     while (nxtitem(&ni, &ship)) {
102         if (!ship.shp_own)
103             continue;
104         if (relations_with(ship.shp_own, own) != ALLIED)
105             continue;
106         if ((carrier_planes(&ship, 0) & (P_L | P_K)) == 0)
107             continue;
108         list_ship(&ship, !ships);
109         ships++;
110     }
111     return ships;
112 }
113
114 int
115 unitsatxy(coord x, coord y, int wantflags, int nowantflags)
116 {
117     int units;
118     struct nstr_item ni;
119     struct lchrstr *lp;
120     struct lndstr land;
121
122     units = 0;
123     snxtitem_xy(&ni, EF_LAND, x, y);
124     while (nxtitem(&ni, &land)) {
125         if (!land.lnd_own)
126             continue;
127         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
128             continue;
129         lp = &lchr[(int)land.lnd_type];
130
131         if (wantflags) {
132             if ((lp->l_flags & wantflags) == 0)
133                 continue;
134         }
135         if (nowantflags) {
136             if (lp->l_flags & nowantflags)
137                 continue;
138         }
139
140         if (!units)
141             pr(" #          owner           eff       type\n");
142         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
143            cname(land.lnd_own), effadv(land.lnd_effic), prland(&land));
144         units++;
145     }
146     return units;
147 }
148
149 int
150 planesatxy(coord x, coord y, int wantflags, int nowantflags)
151 {
152     int planes;
153     struct plnstr plane;
154     struct nstr_item ni;
155     struct plchrstr *plp;
156
157     planes = 0;
158     snxtitem_xy(&ni, EF_PLANE, x, y);
159     while (nxtitem(&ni, &plane)) {
160         if (!plane.pln_own)
161             continue;
162         if (plane.pln_ship >= 0 || plane.pln_land >= 0)
163             continue;
164         if (plane.pln_flags & PLN_LAUNCHED)
165             continue;
166         plp = &plchr[(int)plane.pln_type];
167         if (!planes)
168             pr(" #          owner           eff       type\n");
169         if (wantflags) {
170             if ((plp->pl_flags & wantflags) == 0)
171                 continue;
172         }
173         if (nowantflags) {
174             if (plp->pl_flags & nowantflags)
175                 continue;
176         }
177         pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
178            cname(plane.pln_own), effadv(plane.pln_effic), prplane(&plane));
179         planes++;
180     }
181     return planes;
182 }
183
184 int
185 asw_shipsatxy(coord x, coord y, int wantflags, int nowantflags,
186               struct plnstr *pp, struct shiplist **head)
187 {
188     int ships;
189     struct nstr_item ni;
190     struct mchrstr *mp;
191     struct shpstr ship;
192
193     ships = 0;
194     snxtitem_xy(&ni, EF_SHIP, x, y);
195     while (nxtitem(&ni, &ship)) {
196         if (player->owner)
197             continue;
198         if (!ship.shp_own)
199             continue;
200         mp = &mchr[(int)ship.shp_type];
201         if (wantflags) {
202             if ((mp->m_flags & wantflags) == 0)
203                 continue;
204         }
205         if (nowantflags) {
206             if (mp->m_flags & nowantflags)
207                 continue;
208         }
209         if (mp->m_flags & M_SUB) {
210             if (!pct_chance(pln_hitchance(pp,
211                                           shp_hardtarget(&ship), EF_SHIP)))
212                 continue;
213         }
214         add_shiplist(ship.shp_uid, head);
215         list_ship(&ship, !ships);
216         ships++;
217     }
218     return ships;
219 }
220
221 void
222 print_shiplist(struct shiplist *head)
223 {
224     struct shiplist *s;
225     struct shpstr ship;
226
227     for (s = head; s; s = s->next) {
228         getship(s->uid, &ship);
229         list_ship(&ship, s == head);
230     }
231 }
232
233 int
234 adj_units(coord x, coord y, natid own)
235 {
236     int i;
237     struct sctstr sect;
238
239     for (i = DIR_FIRST; i <= DIR_LAST; i++) {
240         getsect(x + diroff[i][0], y + diroff[i][1], &sect);
241         if (has_units(sect.sct_x, sect.sct_y, own))
242             return 1;
243     }
244     return 0;
245 }
246
247 int
248 has_units(coord x, coord y, natid cn)
249 {
250     int n;
251     struct lndstr land;
252
253     for (n = 0; ef_read(EF_LAND, n, &land); n++) {
254         if (land.lnd_x != x || land.lnd_y != y)
255             continue;
256         if (land.lnd_own == cn)
257             return 1;
258     }
259
260     return 0;
261 }