]> git.pond.sub.org Git - empserver/blob - src/lib/update/nxtitemp.c
Update copyright notice.
[empserver] / src / lib / update / nxtitemp.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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  *  nxtitemp.c: Get next item from 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 "land.h"
38 #include "nsc.h"
39 #include "ship.h"
40 #include "update.h"
41
42 void *
43 nxtitemp(struct nstr_item *np)
44 {
45     struct empobj *gp;
46     int selected;
47
48     if (np->sel == NS_UNDEF)
49         return 0;
50     do {
51         if (np->sel == NS_LIST) {
52             np->index++;
53             if (np->index >= np->size)
54                 return 0;
55             np->cur = np->list[np->index];
56         } else {
57             np->cur++;
58         }
59         gp = ef_ptr(np->type, np->cur);
60         if (!gp)
61             return 0;
62
63         selected = 1;
64         switch (np->sel) {
65         case NS_LIST:
66             break;
67         case NS_ALL:
68             break;
69         case NS_DIST:
70             if (!xyinrange(gp->x, gp->y, &np->range)) {
71                 selected = 0;
72                 break;
73             }
74             np->curdist = mapdist((int)gp->x, (int)gp->y,
75                                   (int)np->cx, (int)np->cy);
76             if (np->curdist > np->dist)
77                 selected = 0;
78             break;
79         case NS_AREA:
80             if (!xyinrange(gp->x, gp->y, &np->range))
81                 selected = 0;
82             if (gp->x == np->range.hx || gp->y == np->range.hy)
83                 selected = 0;
84             break;
85         case NS_XY:
86             if (xnorm(gp->x) != np->cx || ynorm(gp->y) != np->cy)
87                 selected = 0;
88             break;
89         case NS_GROUP:
90             if (np->group != gp->group)
91                 selected = 0;
92             break;
93         default:
94             CANT_REACH();
95             return 0;
96         }
97         if (selected && np->ncond) {
98             /* nstr_exec is expensive, so we do it last */
99             if (!nstr_exec(np->cond, np->ncond, gp))
100                 selected = 0;
101         }
102     } while (!selected);
103     return gp;
104 }