]> git.pond.sub.org Git - empserver/blob - src/lib/subs/nxtitem.c
494f78cff5c4ed87a2314caed3677e4dfd771cc0
[empserver] / src / lib / subs / nxtitem.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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 "empobj.h"
37 #include "file.h"
38 #include "land.h"
39 #include "misc.h"
40 #include "nsc.h"
41 #include "player.h"
42 #include "prototypes.h"
43 #include "ship.h"
44 #include "xy.h"
45
46 int
47 nxtitem(struct nstr_item *np, void *ptr)
48 {
49     struct empobj *gp;
50     int selected;
51
52     if (np->sel == NS_UNDEF)
53         return 0;
54     gp = (struct empobj *)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 (!ef_read(np->type, np->cur, ptr))
65             return 0;
66         selected = 1;
67         switch (np->sel) {
68         case NS_LIST:
69             break;
70         case NS_ALL:
71             break;
72         case NS_DIST:
73             if (CANT_HAPPEN(!(ef_flags(np->type) & EFF_XY)))
74                 return 0;
75             if (!xyinrange(gp->x, gp->y, &np->range)) {
76                 selected = 0;
77                 break;
78             }
79             np->curdist = mapdist(gp->x, gp->y, np->cx, np->cy);
80             if (np->curdist > np->dist)
81                 selected = 0;
82             break;
83         case NS_AREA:
84             if (CANT_HAPPEN(!(ef_flags(np->type) & EFF_XY)))
85                 return 0;
86             if (!xyinrange(gp->x, gp->y, &np->range))
87                 selected = 0;
88             break;
89         case NS_XY:
90             if (CANT_HAPPEN(!(ef_flags(np->type) & EFF_XY)))
91                 return 0;
92             if (xnorm(gp->x) != np->cx || ynorm(gp->y) != np->cy)
93                 selected = 0;
94             break;
95         case NS_GROUP:
96             if (CANT_HAPPEN(!(ef_flags(np->type) & EFF_GROUP)))
97                 return 0;
98             if (np->group != gp->group)
99                 selected = 0;
100             break;
101         default:
102             CANT_REACH();
103             return 0;
104         }
105         if (selected && np->ncond) {
106             /* nstr_exec is expensive, so we do it last */
107             if (!nstr_exec(np->cond, np->ncond, ptr))
108                 selected = 0;
109         }
110     } while (!selected);
111     return 1;
112 }