]> 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-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  nxtitem.c: Get the next item from a list
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  */
32
33 #include <config.h>
34
35 #include "empobj.h"
36 #include "file.h"
37 #include "land.h"
38 #include "misc.h"
39 #include "nsc.h"
40 #include "prototypes.h"
41 #include "ship.h"
42 #include "unit.h"
43 #include "xy.h"
44
45 int
46 nxtitem(struct nstr_item *np, void *ptr)
47 {
48     struct empobj *gp;
49     int selected;
50
51     if (np->sel == NS_UNDEF)
52         return 0;
53     gp = (struct empobj *)ptr;
54     do {
55         if (np->sel == NS_LIST) {
56             np->index++;
57             if (np->index >= np->size)
58                 return 0;
59             np->cur = np->list[np->index];
60         } else if (np->sel == NS_CARGO) {
61             if (np->next < 0)
62                 return 0;
63             np->cur = np->next;
64             np->next = unit_cargo_next(np->type, np->next);
65         } else {
66             np->cur++;
67         }
68         if (!ef_read(np->type, np->cur, ptr))
69             return 0;
70         selected = 1;
71         switch (np->sel) {
72         case NS_LIST:
73         case NS_CARGO:
74         case NS_ALL:
75             break;
76         case NS_DIST:
77             if (CANT_HAPPEN(!(ef_flags(np->type) & EFF_XY)))
78                 return 0;
79             if (!xyinrange(gp->x, gp->y, &np->range)) {
80                 selected = 0;
81                 break;
82             }
83             np->curdist = mapdist(gp->x, gp->y, np->cx, np->cy);
84             if (np->curdist > np->dist)
85                 selected = 0;
86             break;
87         case NS_AREA:
88             if (CANT_HAPPEN(!(ef_flags(np->type) & EFF_XY)))
89                 return 0;
90             if (!xyinrange(gp->x, gp->y, &np->range))
91                 selected = 0;
92             break;
93         case NS_XY:
94             if (CANT_HAPPEN(!(ef_flags(np->type) & EFF_XY)))
95                 return 0;
96             if (xnorm(gp->x) != np->cx || ynorm(gp->y) != np->cy)
97                 selected = 0;
98             break;
99         case NS_GROUP:
100             if (CANT_HAPPEN(!(ef_flags(np->type) & EFF_GROUP)))
101                 return 0;
102             if (np->group != gp->group)
103                 selected = 0;
104             break;
105         default:
106             CANT_REACH();
107             return 0;
108         }
109         if (selected && np->ncond) {
110             /* nstr_exec is expensive, so we do it last */
111             if (!nstr_exec(np->cond, np->ncond, ptr))
112                 selected = 0;
113         }
114     } while (!selected);
115     return 1;
116 }