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