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:
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);
74 else if (x < -WORLD_X / 2)
80 yrel(struct natstr *np, coord absy)
84 y = YNORM(absy - np->nat_yorg);
87 else if (y < -WORLD_Y / 2)
93 xyrelrange(struct natstr *np, struct range *src, struct range *dst)
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;
104 xyabsrange(struct natstr *np, struct range *src, struct range *dst)
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;
115 * Convert initial part of STR to normalized x-coordinate.
116 * Return -1 on error. This works, as normalized coordinates are
118 * Assign pointer to first character after the coordinate to *END,
119 * unless END is a null pointer.
122 strtox(char *str, char **end)
127 l = strtol(str, end, 10);
128 if (*end == str || errno != 0)
134 * Convert initial part of STR to normalized y-coordinate.
135 * Return -1 on error. This works, as normalized coordinates are
137 * Assign pointer to first character after the coordinate to *END,
138 * unless END is a null pointer.
141 strtoy(char *str, char **end)
146 l = strtol(str, end, 10);
147 if (*end == str || errno != 0)
153 xabs(struct natstr *np, coord relx)
155 relx += np->nat_xorg;
160 yabs(struct natstr *np, coord rely)
162 rely += np->nat_yorg;
167 sctoff(coord x, coord y)
169 if (CANT_HAPPEN((x + y) & 1))
171 return XYOFFSET(XNORM(x), YNORM(y));
178 x = WORLD_X - (-x % WORLD_X);
186 y = WORLD_Y - (-y % WORLD_Y);
191 xyinrange(coord x, coord y, struct range *rp)
193 if (rp->lx < rp->hx) {
194 /* xrange doesn't wrap */
195 if (x < rp->lx || x > rp->hx)
198 if (x < rp->lx && x > rp->hx)
201 if (rp->ly < rp->hy) {
202 /* yrange doesn't wrap */
203 if (y < rp->ly || y > rp->hy)
206 if (y < rp->ly && y > rp->hy)
214 prbuf(char *format, ...)
216 static int nbuf = -1;
217 static char buf[20][1024];
223 va_start(ap, format);
224 (void)vsprintf(buf[nbuf], format, ap);