]> git.pond.sub.org Git - empserver/blob - src/lib/subs/nxtitem.c
(NS_ALL) [_WIN32]: Change NS_EVERYTHING back to NS_ALL, #undef NS_ALL to prevent...
[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 "ship.h"
38 #include "land.h"
39 #include "nsc.h"
40 #include "file.h"
41 #include "genitem.h"
42 #include "prototypes.h"
43
44 int
45 nxtitem(struct nstr_item *np, void *ptr)
46 {
47     struct genitem *gp;
48     int selected;
49
50     if (np->sel == NS_UNDEF)
51         return 0;
52     gp = (struct genitem *)ptr;
53     do {
54         if (np->sel == NS_LIST) {
55             np->index++;
56             if (np->index >= np->size)
57                 return 0;
58             np->cur = np->list[np->index];
59         } else {
60             np->cur++;
61         }
62         if (!np->read(np->type, np->cur, ptr)) {
63             /* if read fails, fatal */
64             return 0;
65         }
66         selected = 1;
67         switch (np->sel) {
68             /*
69              * This one won't work unless you're running in emp_player
70              */
71         case NS_LIST:
72             if ((np->flags & EFF_OWNER) && !player->owner)
73                 selected = 0;
74             break;
75         case NS_ALL:
76             /* XXX maybe combine NS_LIST and NS_ALL later */
77             break;
78         case NS_DIST:
79             if (!xyinrange(gp->x, gp->y, &np->range)) {
80                 selected = 0;
81                 break;
82             }
83             np->curdist = mapdist((int)gp->x, (int)gp->y,
84                                   (int)np->cx, (int)np->cy);
85             if (np->curdist > np->dist)
86                 selected = 0;
87             break;
88         case NS_AREA:
89             if (!xyinrange(gp->x, gp->y, &np->range))
90                 selected = 0;
91             if (gp->x == np->range.hx || gp->y == np->range.hy)
92                 selected = 0;
93             break;
94         case NS_XY:
95             if (xnorm(gp->x) != np->cx || ynorm(gp->y) != np->cy)
96                 selected = 0;
97             break;
98         case NS_GROUP:
99             if (np->group != gp->group)
100                 selected = 0;
101             break;
102         default:
103             CANT_HAPPEN("bad np->sel");
104             return 0;
105         }
106         if (selected && np->ncond) {
107             /* nstr_exec is expensive, so we do it last */
108             if (!nstr_exec(np->cond, np->ncond, ptr))
109                 selected = 0;
110         }
111     } while (!selected);
112     return 1;
113 }