]> git.pond.sub.org Git - empserver/blob - src/lib/subs/nxtitem.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / subs / nxtitem.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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  *  nxtitem.c: Get the next item from a list
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  */
33
34 #include <config.h>
35
36 #include "misc.h"
37 #include "player.h"
38 #include "xy.h"
39 #include "ship.h"
40 #include "land.h"
41 #include "nsc.h"
42 #include "file.h"
43 #include "genitem.h"
44 #include "prototypes.h"
45
46 int
47 nxtitem(struct nstr_item *np, void *ptr)
48 {
49     struct genitem *gp;
50     int selected;
51
52     if (np->sel == NS_UNDEF)
53         return 0;
54     gp = (struct genitem *)ptr;
55     do {
56         if (np->sel == NS_LIST) {
57             np->index++;
58             if (np->index >= np->size)
59                 return 0;
60             np->cur = np->list[np->index];
61         } else {
62             np->cur++;
63         }
64         if (!np->read(np->type, np->cur, ptr)) {
65             /* if read fails, fatal */
66             return 0;
67         }
68         selected = 1;
69         switch (np->sel) {
70         case NS_LIST:
71             break;
72         case NS_ALL:
73             break;
74         case NS_DIST:
75             if (!xyinrange(gp->x, gp->y, &np->range)) {
76                 selected = 0;
77                 break;
78             }
79             np->curdist = mapdist((int)gp->x, (int)gp->y,
80                                   (int)np->cx, (int)np->cy);
81             if (np->curdist > np->dist)
82                 selected = 0;
83             break;
84         case NS_AREA:
85             if (!xyinrange(gp->x, gp->y, &np->range))
86                 selected = 0;
87             if (gp->x == np->range.hx || gp->y == np->range.hy)
88                 selected = 0;
89             break;
90         case NS_XY:
91             if (xnorm(gp->x) != np->cx || ynorm(gp->y) != np->cy)
92                 selected = 0;
93             break;
94         case NS_GROUP:
95             if (np->group != gp->group)
96                 selected = 0;
97             break;
98         default:
99             CANT_HAPPEN("bad np->sel");
100             return 0;
101         }
102         if (selected && np->ncond) {
103             /* nstr_exec is expensive, so we do it last */
104             if (!nstr_exec(np->cond, np->ncond, ptr))
105                 selected = 0;
106         }
107     } while (!selected);
108     return 1;
109 }