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