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