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