]> 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-2016, 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  *  nxtitemp.c: Get next item from 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 "land.h"
37 #include "nsc.h"
38 #include "ship.h"
39 #include "unit.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 NULL;
50     do {
51         if (np->sel == NS_LIST) {
52             np->index++;
53             if (np->index >= np->size)
54                 return NULL;
55             np->cur = np->list[np->index];
56         } else if (np->sel == NS_CARGO) {
57             if (np->next < 0)
58                 return NULL;
59             np->cur = np->next;
60             np->next = unit_cargo_next(np->type, np->next);
61         } else {
62             np->cur++;
63         }
64         gp = ef_ptr(np->type, np->cur);
65         if (!gp)
66             return NULL;
67
68         selected = 1;
69         switch (np->sel) {
70         case NS_LIST:
71         case NS_CARGO:
72         case NS_ALL:
73             break;
74         case NS_DIST:
75             if (CANT_HAPPEN(!(ef_flags(np->type) & EFF_XY)))
76                 return NULL;
77             if (!xyinrange(gp->x, gp->y, &np->range)) {
78                 selected = 0;
79                 break;
80             }
81             np->curdist = mapdist(gp->x, gp->y, np->cx, np->cy);
82             if (np->curdist > np->dist)
83                 selected = 0;
84             break;
85         case NS_AREA:
86             if (CANT_HAPPEN(!(ef_flags(np->type) & EFF_XY)))
87                 return NULL;
88             if (!xyinrange(gp->x, gp->y, &np->range))
89                 selected = 0;
90             break;
91         case NS_XY:
92             if (CANT_HAPPEN(!(ef_flags(np->type) & EFF_XY)))
93                 return NULL;
94             if (xnorm(gp->x) != np->cx || ynorm(gp->y) != np->cy)
95                 selected = 0;
96             break;
97         case NS_GROUP:
98             if (CANT_HAPPEN(!(ef_flags(np->type) & EFF_GROUP)))
99                 return NULL;
100             if (np->group != gp->group)
101                 selected = 0;
102             break;
103         default:
104             CANT_REACH();
105             return NULL;
106         }
107         if (selected && np->ncond) {
108             /* nstr_exec is expensive, so we do it last */
109             if (!nstr_exec(np->cond, np->ncond, gp))
110                 selected = 0;
111         }
112     } while (!selected);
113     return gp;
114 }