]> git.pond.sub.org Git - empserver/blob - src/lib/subs/snxtsct.c
License upgrade to GPL version 3 or later
[empserver] / src / lib / subs / snxtsct.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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  *  snxtsct.c: Arrange sector selection using either distance or area
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  *     Markus Armbruster, 2006-2009
32  */
33
34 #include <config.h>
35
36 #include "file.h"
37 #include "misc.h"
38 #include "nat.h"
39 #include "nsc.h"
40 #include "optlist.h"
41 #include "player.h"
42 #include "prototypes.h"
43 #include "xy.h"
44
45 /*
46  * setup the nstr_sect structure for sector selection.
47  * can select on either NS_ALL, NS_AREA, or NS_RANGE
48  * iterate thru the "condarg" string looking
49  * for arguments to compile into the nstr.
50  * Using this function for anything but command arguments is usually
51  * incorrect, because it respects conditionals.  Use the snxtsct_FOO()
52  * instead.
53  */
54 int
55 snxtsct(struct nstr_sect *np, char *str)
56 {
57     struct range range;
58     struct natstr *natp;
59     coord cx, cy;
60     int dist, n;
61     char buf[1024];
62
63     if (!str || !*str) {
64         if (!(str = getstring("(sects)? ", buf)))
65             return 0;
66     }
67     switch (sarg_type(str)) {
68     case NS_AREA:
69         if (!sarg_area(str, &range))
70             return 0;
71         snxtsct_area(np, &range);
72         break;
73     case NS_DIST:
74         if (!sarg_range(str, &cx, &cy, &dist))
75             return 0;
76         snxtsct_dist(np, cx, cy, dist);
77         break;
78     case NS_ALL:
79         /*
80          * Can't use snxtsct_all(), as it would disclose the real
81          * origin.  Use a world-sized area instead.
82          */
83         natp = getnatp(player->cnum);
84         range.lx = xabs(natp, -WORLD_X / 2);
85         range.ly = yabs(natp, -WORLD_Y / 2);
86         range.hx = xnorm(range.lx + WORLD_X - 1);
87         range.hy = ynorm(range.ly + WORLD_Y - 1);
88         xysize_range(&range);
89         snxtsct_area(np, &range);
90         break;
91     default:
92         return 0;
93     }
94     if (!player->condarg)
95         return 1;
96     n = nstr_comp(np->cond, sizeof(np->cond) / sizeof(*np->cond),
97                   EF_SECTOR, player->condarg);
98     np->ncond = n >= 0 ? n : 0;
99     return n >= 0;
100 }
101
102 void
103 snxtsct_all(struct nstr_sect *np)
104 {
105     struct range worldrange;
106
107     worldrange.lx = 0;
108     worldrange.ly = 0;
109     worldrange.hx = WORLD_X - 1;
110     worldrange.hy = WORLD_Y - 1;
111     xysize_range(&worldrange);
112     snxtsct_area(np, &worldrange);
113 }
114
115 void
116 snxtsct_area(struct nstr_sect *np, struct range *range)
117 {
118     memset(np, 0, sizeof(*np));
119     np->range = *range;
120     np->ncond = 0;
121     np->type = NS_AREA;
122     np->x = np->range.lx - 1;
123     np->y = np->range.ly;
124     np->dx = -1;
125     np->dy = 0;
126 }
127
128 void
129 snxtsct_rewind(struct nstr_sect *np)
130 {
131     np->x = np->range.lx - 1;
132     np->y = np->range.ly;
133     np->dx = -1;
134     np->dy = 0;
135     np->id = -1;
136 }
137
138 void
139 snxtsct_dist(struct nstr_sect *np, coord cx, coord cy, int dist)
140 {
141     memset(np, 0, sizeof(*np));
142     xydist_range(cx, cy, dist, &np->range);
143     np->cx = cx;
144     np->cy = cy;
145     np->ncond = 0;
146     np->dist = dist;
147     np->type = NS_DIST;
148     np->x = np->range.lx - 1;
149     np->y = np->range.ly;
150     np->dx = -1;
151     np->dy = 0;
152 }
153
154 void
155 xysize_range(struct range *rp)
156 {
157     if (rp->lx > rp->hx)
158         rp->width = WORLD_X + rp->hx + 1 - rp->lx;
159     else
160         rp->width = rp->hx + 1 - rp->lx;
161     if (CANT_HAPPEN(rp->width > WORLD_X))
162         rp->width = WORLD_X;
163     if (rp->ly > rp->hy)
164         rp->height = WORLD_Y + rp->hy + 1 - rp->ly;
165     else
166         rp->height = rp->hy + 1 - rp->ly;
167     if (CANT_HAPPEN(rp->height > WORLD_Y))
168         rp->height = WORLD_Y;
169 }
170
171 void
172 xydist_range(coord x, coord y, int dist, struct range *rp)
173 {
174     if (4 * dist + 1 < WORLD_X) {
175         rp->lx = xnorm(x - 2 * dist);
176         rp->hx = xnorm(x + 2 * dist);
177         rp->width = 4 * dist + 1;
178     } else {
179         rp->lx = xnorm(x - WORLD_X / 2);
180         rp->hx = xnorm(rp->lx + WORLD_X - 1);
181         rp->width = WORLD_X;
182     }
183
184     if (2 * dist + 1 < WORLD_Y) {
185         rp->ly = ynorm(y - dist);
186         rp->hy = ynorm(y + dist);
187         rp->height = 2 * dist + 1;
188     } else {
189         rp->ly = ynorm(y - WORLD_Y / 2);
190         rp->hy = ynorm(rp->ly + WORLD_Y - 1);
191         rp->height = WORLD_Y;
192     }
193 }