2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
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.
28 * snxtsct.c: Arrange sector selection using either distance or area
30 * Known contributors to this file:
42 #include "prototypes.h"
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()
55 snxtsct(struct nstr_sect *np, char *str)
63 if (str == 0 || *str == 0) {
64 if ((str = getstring("(sects)? ", buf)) == 0)
67 switch (sarg_type(str)) {
69 if (!sarg_area(str, &range))
71 snxtsct_area(np, &range);
74 if (!sarg_range(str, &cx, &cy, &dist))
76 snxtsct_dist(np, cx, cy, dist);
80 * Can't use snxtsct_all(), as it would disclose the real
81 * origin. Use a world-sized area instead.
83 natp = getnatp(player->cnum);
84 range.lx = xabs(natp, -WORLD_X / 2);
85 range.ly = yabs(natp, -WORLD_Y / 2);
86 range.hx = xabs(natp, WORLD_X / 2);
87 range.hy = yabs(natp, WORLD_Y / 2);
88 range.width = range.height = 0;
89 snxtsct_area(np, &range);
94 if (player->condarg == 0)
96 n = nstr_comp(np->cond, sizeof(np->cond) / sizeof(*np->cond),
97 EF_SECTOR, player->condarg);
98 np->ncond = n >= 0 ? n : 0;
103 snxtsct_all(struct nstr_sect *np)
105 struct range worldrange;
107 worldrange.lx = -WORLD_X / 2;
108 worldrange.ly = -WORLD_Y / 2;
109 worldrange.hx = WORLD_X / 2;
110 worldrange.hy = WORLD_Y / 2;
111 worldrange.width = worldrange.height = 0;
112 snxtsct_area(np, &worldrange);
116 snxtsct_area(struct nstr_sect *np, struct range *range)
118 memset(np, 0, sizeof(*np));
123 np->x = np->range.lx - 1;
124 np->y = np->range.ly;
127 xysize_range(&np->range);
131 snxtsct_rewind(struct nstr_sect *np)
133 np->x = np->range.lx - 1;
134 np->y = np->range.ly;
141 snxtsct_dist(struct nstr_sect *np, coord cx, coord cy, int dist)
143 memset(np, 0, sizeof(*np));
144 xydist_range(cx, cy, dist, &np->range);
151 np->x = np->range.lx - 1;
152 np->y = np->range.ly;
156 /* This function is now done elsewhere. */
157 /* It was not doing the right thing when the world was small */
158 xysize_range(&np->range);
163 xysize_range(struct range *rp)
165 if (rp->lx >= rp->hx)
166 rp->width = WORLD_X + rp->hx - rp->lx;
168 rp->width = rp->hx - rp->lx;
170 /* This is a necessary check for small, hitech worlds. */
171 if (rp->width > WORLD_X)
174 if (rp->ly >= rp->hy)
175 rp->height = WORLD_Y + rp->hy - rp->ly;
177 rp->height = rp->hy - rp->ly;
179 /* This is a necessary check for small, hitech worlds. */
180 if (rp->height > WORLD_Y)
181 rp->height = WORLD_Y;
185 /* This is called also called in snxtitem.c */
187 xydist_range(coord x, coord y, int dist, struct range *rp)
189 if (dist < WORLD_X / 4) {
190 rp->lx = xnorm((coord)(x - 2 * dist));
191 rp->hx = xnorm((coord)(x + 2 * dist) + 1);
192 rp->width = 4 * dist + 1;
194 /* Range is larger than the world */
195 /* Make sure we get lx in the right place. */
196 rp->lx = xnorm((coord)(x - WORLD_X / 2));
197 rp->hx = xnorm((coord)(rp->lx + WORLD_X - 1));
201 if (dist < WORLD_Y / 2) {
202 rp->ly = ynorm((coord)(y - dist));
203 rp->hy = ynorm((coord)(y + dist) + 1);
204 rp->height = 2 * dist + 1;
206 /* Range is larger than the world */
207 rp->ly = ynorm((coord)(y - WORLD_Y / 2));
208 rp->hy = ynorm((coord)(rp->ly + WORLD_Y - 1));
209 rp->height = WORLD_Y;