]> git.pond.sub.org Git - empserver/blob - src/lib/subs/snxtsct.c
Make snxtsct_area() expect correct range width and height
[empserver] / src / lib / subs / snxtsct.c
1 /*
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
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         xysize_range(&range);
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     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->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 }
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->read = ef_read;
150     np->x = np->range.lx - 1;
151     np->y = np->range.ly;
152     np->dx = -1;
153     np->dy = 0;
154 }
155
156 void
157 xysize_range(struct range *rp)
158 {
159     if (rp->lx >= rp->hx)
160         rp->width = WORLD_X + rp->hx - rp->lx;
161     else
162         rp->width = rp->hx - rp->lx;
163     if (CANT_HAPPEN(rp->width > WORLD_X))
164         rp->width = WORLD_X;
165     if (rp->ly >= rp->hy)
166         rp->height = WORLD_Y + rp->hy - rp->ly;
167     else
168         rp->height = rp->hy - rp->ly;
169     if (CANT_HAPPEN(rp->height > WORLD_Y))
170         rp->height = WORLD_Y;
171 }
172
173 /* This is called also called in snxtitem.c */
174 void
175 xydist_range(coord x, coord y, int dist, struct range *rp)
176 {
177     if (dist < WORLD_X / 4) {
178         rp->lx = xnorm((coord)(x - 2 * dist));
179         rp->hx = xnorm((coord)(x + 2 * dist) + 1);
180         rp->width = 4 * dist + 1;
181     } else {
182         /* Range is larger than the world */
183         /* Make sure we get lx in the right place. */
184         rp->lx = xnorm((coord)(x - WORLD_X / 2));
185         rp->hx = xnorm((coord)(rp->lx + WORLD_X - 1));
186         rp->width = WORLD_X;
187     }
188
189     if (dist < WORLD_Y / 2) {
190         rp->ly = ynorm((coord)(y - dist));
191         rp->hy = ynorm((coord)(y + dist) + 1);
192         rp->height = 2 * dist + 1;
193     } else {
194         /* Range is larger than the world */
195         rp->ly = ynorm((coord)(y - WORLD_Y / 2));
196         rp->hy = ynorm((coord)(rp->ly + WORLD_Y - 1));
197         rp->height = WORLD_Y;
198     }
199 }