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