]> git.pond.sub.org Git - empserver/blob - src/lib/subs/snxtsct.c
Update copyright notice.
[empserver] / src / lib / subs / snxtsct.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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  */
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 == 0 || *str == 0) {
64         if ((str = getstring("(sects)? ", buf)) == 0)
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 = xabs(natp, WORLD_X / 2);
87         range.hy = yabs(natp, WORLD_Y / 2);
88         range.width = range.height = 0;
89         snxtsct_area(np, &range);
90         break;
91     default:
92         return 0;
93     }
94     if (player->condarg == 0)
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 = -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);
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->read = ef_read;
123     np->x = np->range.lx - 1;
124     np->y = np->range.ly;
125     np->dx = -1;
126     np->dy = 0;
127     xysize_range(&np->range);
128 }
129
130 void
131 snxtsct_rewind(struct nstr_sect *np)
132 {
133     np->x = np->range.lx - 1;
134     np->y = np->range.ly;
135     np->dx = -1;
136     np->dy = 0;
137     np->id = -1;
138 }
139
140 void
141 snxtsct_dist(struct nstr_sect *np, coord cx, coord cy, int dist)
142 {
143     memset(np, 0, sizeof(*np));
144     xydist_range(cx, cy, dist, &np->range);
145     np->cx = cx;
146     np->cy = cy;
147     np->ncond = 0;
148     np->dist = dist;
149     np->type = NS_DIST;
150     np->read = ef_read;
151     np->x = np->range.lx - 1;
152     np->y = np->range.ly;
153     np->dx = -1;
154     np->dy = 0;
155 #if 0
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);
159 #endif
160 }
161
162 void
163 xysize_range(struct range *rp)
164 {
165     if (rp->lx >= rp->hx)
166         rp->width = WORLD_X + rp->hx - rp->lx;
167     else
168         rp->width = rp->hx - rp->lx;
169 #ifndef HAY
170     /* This is a necessary check for small, hitech worlds. */
171     if (rp->width > WORLD_X)
172         rp->width = WORLD_X;
173 #endif
174     if (rp->ly >= rp->hy)
175         rp->height = WORLD_Y + rp->hy - rp->ly;
176     else
177         rp->height = rp->hy - rp->ly;
178 #ifndef HAY
179     /* This is a necessary check for small, hitech worlds. */
180     if (rp->height > WORLD_Y)
181         rp->height = WORLD_Y;
182 #endif
183 }
184
185 /* This is called also called in snxtitem.c */
186 void
187 xydist_range(coord x, coord y, int dist, struct range *rp)
188 {
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;
193     } else {
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));
198         rp->width = WORLD_X;
199     }
200
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;
205     } else {
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;
210     }
211 }