2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.
28 * xy.c: x-y related conversion routines
30 * Known contributors to this file:
32 * Markus Armbruster, 2004-2008
44 #include "prototypes.h"
49 * return pointer to a string containing the x,y
50 * coords as desired by a particular target country.
53 xyas(coord x, coord y, natid country)
57 np = getnatp(country);
58 return prbuf("%d,%d", xrel(np, x), yrel(np, y));
62 ownxy(struct sctstr *sp)
64 return xyas(sp->sct_x, sp->sct_y, sp->sct_own);
68 xrel(struct natstr *np, coord absx)
72 x = XNORM(absx - np->nat_xorg);
79 yrel(struct natstr *np, coord absy)
83 y = YNORM(absy - np->nat_yorg);
90 xyrelrange(struct natstr *np, struct range *src, struct range *dst)
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;
101 xyabsrange(struct natstr *np, struct range *src, struct range *dst)
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;
112 * Convert initial part of STR to normalized x-coordinate.
113 * Return -1 on error. This works, as normalized coordinates are
115 * Assign pointer to first character after the coordinate to *END,
116 * unless END is a null pointer.
119 strtox(char *str, char **end)
124 l = strtol(str, end, 10);
125 if (*end == str || errno != 0)
131 * Convert initial part of STR to normalized y-coordinate.
132 * Return -1 on error. This works, as normalized coordinates are
134 * Assign pointer to first character after the coordinate to *END,
135 * unless END is a null pointer.
138 strtoy(char *str, char **end)
143 l = strtol(str, end, 10);
144 if (*end == str || errno != 0)
150 xabs(struct natstr *np, coord relx)
152 relx += np->nat_xorg;
157 yabs(struct natstr *np, coord rely)
159 rely += np->nat_yorg;
164 sctoff(coord x, coord y)
166 if (CANT_HAPPEN((x + y) & 1))
168 return XYOFFSET(XNORM(x), YNORM(y));
184 xyinrange(coord x, coord y, struct range *rp)
186 if (rp->lx <= rp->hx) {
187 /* xrange doesn't wrap */
188 if (x < rp->lx || x > rp->hx)
191 if (x < rp->lx && x > rp->hx)
194 if (rp->ly <= rp->hy) {
195 /* yrange doesn't wrap */
196 if (y < rp->ly || y > rp->hy)
199 if (y < rp->ly && y > rp->hy)
207 prbuf(char *format, ...)
209 static int nbuf = -1;
210 static char buf[20][1024];
216 va_start(ap, format);
217 (void)vsprintf(buf[nbuf], format, ap);