]> git.pond.sub.org Git - empserver/blob - src/lib/common/xy.c
Include config.h.
[empserver] / src / lib / common / xy.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  xy.c: x-y related conversion routines
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  */
33
34 #include <config.h>
35
36 #include <ctype.h>
37 #include <errno.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include "misc.h"
41 #include "xy.h"
42 #include "nat.h"
43 #include "sect.h"
44 #include "file.h"
45 #include "common.h"
46 #include "optlist.h"
47
48 /*
49  * return pointer to a string containing the x,y
50  * coords as desired by a particular target country.
51  */
52 s_char *
53 xyas(coord x, coord y, natid country)
54 {
55     struct natstr *np;
56
57     np = getnatp(country);
58     return prbuf("%d,%d", xrel(np, x), yrel(np, y));
59 }
60
61 s_char *
62 ownxy(struct sctstr *sp)
63 {
64     return xyas(sp->sct_x, sp->sct_y, sp->sct_own);
65 }
66
67 coord
68 xrel(struct natstr *np, coord absx)
69 {
70     coord x;
71
72     if ((np->nat_stat & STAT_ABS) == 0) {
73         x = XNORM(absx - np->nat_xorg);
74     } else {
75         x = XNORM(absx);
76     }
77     if (x >= WORLD_X / 2)
78         x -= WORLD_X;
79     else if (x < -WORLD_X / 2)
80         x += WORLD_X;
81     return x;
82 }
83
84 coord
85 yrel(struct natstr *np, coord absy)
86 {
87     coord y;
88
89     if ((np->nat_stat & STAT_ABS) == 0) {
90         y = YNORM(absy - np->nat_yorg);
91     } else {
92         y = YNORM(absy);
93     }
94     if (y >= WORLD_Y / 2)
95         y -= WORLD_Y;
96     else if (y < -WORLD_Y / 2)
97         y += WORLD_Y;
98     return y;
99 }
100
101 void
102 xyrelrange(struct natstr *np, struct range *src, struct range *dst)
103 {
104     dst->lx = xrel(np, src->lx);
105     dst->hx = xrel(np, src->hx);
106     dst->ly = yrel(np, src->ly);
107     dst->hy = yrel(np, src->hy);
108     dst->width = src->width;
109     dst->height = src->height;
110 }
111
112 void
113 xyabsrange(struct natstr *np, struct range *src, struct range *dst)
114 {
115     dst->lx = xabs(np, src->lx);
116     dst->hx = xabs(np, src->hx);
117     dst->ly = yabs(np, src->ly);
118     dst->hy = yabs(np, src->hy);
119     dst->width = src->width;
120     dst->height = src->height;
121 }
122
123 /*
124  * Convert initial part of STR to normalized x-coordinate.
125  * Return -1 on error.  This works, as normalized coordinates are
126  * non-negative.
127  * Assign pointer to first character after the coordinate to *END,
128  * unless END is a null pointer.
129  */
130 coord
131 strtox(char *str, char **end)
132 {
133     long l;
134
135     errno = 0;
136     l = strtol(str, end, 10);
137     if (*end == str || errno != 0)
138         return -1;
139     return XNORM(l);
140 }
141
142 /*
143  * Convert initial part of STR to normalized y-coordinate.
144  * Return -1 on error.  This works, as normalized coordinates are
145  * non-negative.
146  * Assign pointer to first character after the coordinate to *END,
147  * unless END is a null pointer.
148  */
149 coord
150 strtoy(char *str, char **end)
151 {
152     long l;
153
154     errno = 0;
155     l = strtol(str, end, 10);
156     if (*end == str || errno != 0)
157         return -1;
158     return YNORM(l);
159 }
160
161 coord
162 xabs(struct natstr *np, coord relx)
163 {
164     if ((np->nat_stat & STAT_ABS) == 0)
165         relx += np->nat_xorg;
166     return XNORM(relx);
167 }
168
169 coord
170 yabs(struct natstr *np, coord rely)
171 {
172     if ((np->nat_stat & STAT_ABS) == 0)
173         rely += np->nat_yorg;
174     return YNORM(rely);
175 }
176
177 int
178 sctoff(coord x, coord y)
179 {
180     if ((x + y) & 01) {
181         logerror("%d,%d is an invalid sector specification!\n", x, y);
182         return -1;
183     }
184     return (YNORM(y) * WORLD_X + XNORM(x)) / 2;
185 }
186
187 coord
188 xnorm(coord x)
189 {
190     if (x < 0)
191         x = WORLD_X - (-x % WORLD_X);
192     return x % WORLD_X;
193 }
194
195 coord
196 ynorm(coord y)
197 {
198     if (y < 0)
199         y = WORLD_Y - (-y % WORLD_Y);
200     return y % WORLD_Y;
201 }
202
203 int
204 xyinrange(coord x, coord y, struct range *rp)
205 {
206     if (rp->lx < rp->hx) {
207         /* xrange doesn't wrap */
208         if (x < rp->lx || x > rp->hx)
209             return 0;
210     } else {
211         if (x < rp->lx && x > rp->hx)
212             return 0;
213     }
214     if (rp->ly < rp->hy) {
215         /* yrange doesn't wrap */
216         if (y < rp->ly || y > rp->hy)
217             return 0;
218     } else {
219         if (y < rp->ly && y > rp->hy)
220             return 0;
221     }
222     return 1;
223 }
224
225
226 s_char *
227 prbuf(s_char *format, ...)
228 {
229     static int nbuf = -1;
230     static s_char buf[20][1024];
231     va_list ap;
232
233     if (++nbuf > 19)
234         nbuf = 0;
235
236     va_start(ap, format);
237     (void)vsprintf(buf[nbuf], format, ap);
238     va_end(ap);
239
240     return buf[nbuf];
241 }