2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, 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-2008
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 * Convert initial part of STR to normalized x-coordinate.
112 * Return -1 on error. This works, as normalized coordinates are
114 * Assign pointer to first character after the coordinate to *END,
115 * unless END is a null pointer.
118 strtox(char *str, char **end)
123 l = strtol(str, end, 10);
124 if (*end == str || errno != 0)
130 * Convert initial part of STR to normalized y-coordinate.
131 * Return -1 on error. This works, as normalized coordinates are
133 * Assign pointer to first character after the coordinate to *END,
134 * unless END is a null pointer.
137 strtoy(char *str, char **end)
142 l = strtol(str, end, 10);
143 if (*end == str || errno != 0)
149 xabs(struct natstr *np, coord relx)
151 relx += np->nat_xorg;
156 yabs(struct natstr *np, coord rely)
158 rely += np->nat_yorg;
163 sctoff(coord x, coord y)
165 if (CANT_HAPPEN((x + y) & 1))
167 return XYOFFSET(XNORM(x), YNORM(y));
183 xyinrange(coord x, coord y, struct range *rp)
185 if (rp->lx <= rp->hx) {
186 /* xrange doesn't wrap */
187 if (x < rp->lx || x > rp->hx)
190 if (x < rp->lx && x > rp->hx)
193 if (rp->ly <= rp->hy) {
194 /* yrange doesn't wrap */
195 if (y < rp->ly || y > rp->hy)
198 if (y < rp->ly && y > rp->hy)
206 prbuf(char *format, ...)
208 static int nbuf = -1;
209 static char buf[20][1024];
215 va_start(ap, format);
216 (void)vsprintf(buf[nbuf], format, ap);