2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * xy.c: x-y related conversion routines
30 * Known contributors to this file:
47 * return pointer to a string containing the x,y
48 * coords as desired by a particular target country.
51 xyas(coord x, coord y, natid country)
55 np = getnatp(country);
56 return prbuf("%d,%d", xrel(np, x), yrel(np, y));
60 ownxy(struct sctstr *sp)
62 return xyas(sp->sct_x, sp->sct_y, sp->sct_own);
66 xrel(struct natstr *np, coord absx)
70 if ((np->nat_stat & STAT_ABS) == 0) {
71 x = XNORM(absx - np->nat_xorg);
77 else if (x < -WORLD_X / 2)
83 yrel(struct natstr *np, coord absy)
87 if ((np->nat_stat & STAT_ABS) == 0) {
88 y = YNORM(absy - np->nat_yorg);
94 else if (y < -WORLD_Y / 2)
100 xyrelrange(struct natstr *np, struct range *src, struct range *dst)
102 dst->lx = xrel(np, src->lx);
103 dst->hx = xrel(np, src->hx);
104 dst->ly = yrel(np, src->ly);
105 dst->hy = yrel(np, src->hy);
106 dst->width = src->width;
107 dst->height = src->height;
111 xyabsrange(struct natstr *np, struct range *src, struct range *dst)
113 dst->lx = xabs(np, src->lx);
114 dst->hx = xabs(np, src->hx);
115 dst->ly = yabs(np, src->ly);
116 dst->hy = yabs(np, src->hy);
117 dst->width = src->width;
118 dst->height = src->height;
122 * Convert initial part of STR to normalized x-coordinate.
123 * Return -1 on error. This works, as normalized coordinates are
125 * Assign pointer to first character after the coordinate to *END,
126 * unless END is a null pointer.
129 strtox(char *str, char **end)
134 l = strtol(str, end, 10);
135 if (*end == str || errno != 0)
141 * Convert initial part of STR to normalized y-coordinate.
142 * Return -1 on error. This works, as normalized coordinates are
144 * Assign pointer to first character after the coordinate to *END,
145 * unless END is a null pointer.
148 strtoy(char *str, char **end)
153 l = strtol(str, end, 10);
154 if (*end == str || errno != 0)
160 xabs(struct natstr *np, coord relx)
162 if ((np->nat_stat & STAT_ABS) == 0)
163 relx += np->nat_xorg;
168 yabs(struct natstr *np, coord rely)
170 if ((np->nat_stat & STAT_ABS) == 0)
171 rely += np->nat_yorg;
176 sctoff(coord x, coord y)
179 logerror("%d,%d is an invalid sector specification!\n", x, y);
182 return (YNORM(y) * WORLD_X + XNORM(x)) / 2;
186 xnorm(register coord x)
189 x = WORLD_X - (-x % WORLD_X);
194 ynorm(register coord y)
197 y = WORLD_Y - (-y % WORLD_Y);
202 xyinrange(coord x, coord y, struct range *rp)
204 if (rp->lx < rp->hx) {
205 /* xrange doesn't wrap */
206 if (x < rp->lx || x > rp->hx)
209 if (x < rp->lx && x > rp->hx)
212 if (rp->ly < rp->hy) {
213 /* yrange doesn't wrap */
214 if (y < rp->ly || y > rp->hy)
217 if (y < rp->ly && y > rp->hy)
225 prbuf(s_char *format, ...)
227 static int nbuf = -1;
228 static s_char buf[20][1024];
234 va_start(ap, format);
235 (void)vsprintf(buf[nbuf], format, ap);