2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * xy.c: x-y related conversion routines
29 * Known contributors to this file:
31 * Markus Armbruster, 2004-2013
43 #include "prototypes.h"
48 * return pointer to a string containing the x,y
49 * coords as desired by a particular target country.
52 xyas(coord x, coord y, natid country)
56 np = getnatp(country);
57 return prbuf("%d,%d", xrel(np, x), yrel(np, y));
61 ownxy(struct sctstr *sp)
63 return xyas(sp->sct_x, sp->sct_y, sp->sct_own);
67 xrel(struct natstr *np, coord absx)
71 x = XNORM(absx - np->nat_xorg);
78 yrel(struct natstr *np, coord absy)
82 y = YNORM(absy - np->nat_yorg);
89 xyrelrange(struct natstr *np, struct range *src, struct range *dst)
91 dst->lx = xrel(np, src->lx);
92 dst->hx = xrel(np, src->hx);
93 dst->ly = yrel(np, src->ly);
94 dst->hy = yrel(np, src->hy);
95 dst->width = src->width;
96 dst->height = src->height;
100 xyabsrange(struct natstr *np, struct range *src, struct range *dst)
102 dst->lx = xabs(np, src->lx);
103 dst->hx = xabs(np, src->hx);
104 dst->ly = yabs(np, src->ly);
105 dst->hy = yabs(np, src->hy);
106 dst->width = src->width;
107 dst->height = src->height;
111 xysize_range(struct range *rp)
114 rp->width = WORLD_X + rp->hx + 1 - rp->lx;
116 rp->width = rp->hx + 1 - rp->lx;
117 if (CANT_HAPPEN(rp->width > WORLD_X))
120 rp->height = WORLD_Y + rp->hy + 1 - rp->ly;
122 rp->height = rp->hy + 1 - rp->ly;
123 if (CANT_HAPPEN(rp->height > WORLD_Y))
124 rp->height = WORLD_Y;
128 xydist_range(coord x, coord y, int dist, struct range *rp)
130 if (4 * dist + 1 < WORLD_X) {
131 rp->lx = xnorm(x - 2 * dist);
132 rp->hx = xnorm(x + 2 * dist);
133 rp->width = 4 * dist + 1;
135 rp->lx = xnorm(x - WORLD_X / 2);
136 rp->hx = xnorm(rp->lx + WORLD_X - 1);
140 if (2 * dist + 1 < WORLD_Y) {
141 rp->ly = ynorm(y - dist);
142 rp->hy = ynorm(y + dist);
143 rp->height = 2 * dist + 1;
145 rp->ly = ynorm(y - WORLD_Y / 2);
146 rp->hy = ynorm(rp->ly + WORLD_Y - 1);
147 rp->height = WORLD_Y;
152 * Convert initial part of @str to normalized x-coordinate.
153 * Return -1 on error. This works, as normalized coordinates are
155 * Assign pointer to first character after the coordinate to *@end,
156 * unless @end is a null pointer.
159 strtox(char *str, char **end)
164 l = strtol(str, end, 10);
165 if (*end == str || errno != 0)
171 * Convert initial part of @str to normalized y-coordinate.
172 * Return -1 on error. This works, as normalized coordinates are
174 * Assign pointer to first character after the coordinate to *@end,
175 * unless @end is a null pointer.
178 strtoy(char *str, char **end)
183 l = strtol(str, end, 10);
184 if (*end == str || errno != 0)
190 xabs(struct natstr *np, coord relx)
192 relx += np->nat_xorg;
197 yabs(struct natstr *np, coord rely)
199 rely += np->nat_yorg;
204 sctoff(coord x, coord y)
206 if (CANT_HAPPEN((x + y) & 1))
208 return XYOFFSET(XNORM(x), YNORM(y));
211 void sctoff2xy(coord *x, coord *y, int off)
213 *y = off * 2 / WORLD_X;
214 *x = off * 2 % WORLD_X + *y % 2;
230 xyinrange(coord x, coord y, struct range *rp)
232 if (rp->lx <= rp->hx) {
233 /* xrange doesn't wrap */
234 if (x < rp->lx || x > rp->hx)
237 if (x < rp->lx && x > rp->hx)
240 if (rp->ly <= rp->hy) {
241 /* yrange doesn't wrap */
242 if (y < rp->ly || y > rp->hy)
245 if (y < rp->ly && y > rp->hy)
253 prbuf(char *format, ...)
255 static int nbuf = -1;
256 static char buf[20][1024];
262 va_start(ap, format);
263 (void)vsprintf(buf[nbuf], format, ap);