]> git.pond.sub.org Git - empserver/blob - src/lib/update/nxtitemp.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / update / nxtitemp.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  *  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 "misc.h"
37 #include "xy.h"
38 #include "ship.h"
39 #include "land.h"
40 #include "nsc.h"
41 #include "file.h"
42 #include "genitem.h"
43 #include "player.h"
44 #include "update.h"
45 #include "gen.h"
46 #include "common.h"
47
48 void *
49 nxtitemp(struct nstr_item *np)
50 {
51     struct genitem *gp;
52     int selected;
53
54     if (np->sel == NS_UNDEF)
55         return 0;
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         gp = ef_ptr(np->type, np->cur);
66         if (gp == (struct genitem *)0)
67             return 0;
68
69         selected = 1;
70         switch (np->sel) {
71         case NS_LIST:
72             break;
73         case NS_ALL:
74             break;
75         case NS_DIST:
76             if (!xyinrange(gp->x, gp->y, &np->range)) {
77                 selected = 0;
78                 break;
79             }
80             np->curdist = mapdist((int)gp->x, (int)gp->y,
81                                   (int)np->cx, (int)np->cy);
82             if (np->curdist > np->dist)
83                 selected = 0;
84             break;
85         case NS_AREA:
86             if (!xyinrange(gp->x, gp->y, &np->range))
87                 selected = 0;
88             if (gp->x == np->range.hx || gp->y == np->range.hy)
89                 selected = 0;
90             break;
91         case NS_XY:
92             if (xnorm(gp->x) != np->cx || ynorm(gp->y) != np->cy)
93                 selected = 0;
94             break;
95         case NS_GROUP:
96             if (np->group != gp->group)
97                 selected = 0;
98             break;
99         default:
100             CANT_HAPPEN("bad np->sel");
101             return 0;
102         }
103         if (selected && np->ncond) {
104             /* nstr_exec is expensive, so we do it last */
105             if (!nstr_exec(np->cond, np->ncond, gp))
106                 selected = 0;
107         }
108     } while (!selected);
109     return gp;
110 }