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