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