2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2020, 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
42 #include "prototypes.h"
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 x = XNORM(absx - np->nat_xorg);
77 yrel(struct natstr *np, coord absy)
81 y = YNORM(absy - np->nat_yorg);
88 xyrelrange(struct natstr *np, struct range *src, struct range *dst)
90 dst->lx = xrel(np, src->lx);
91 dst->hx = xrel(np, src->hx);
92 dst->ly = yrel(np, src->ly);
93 dst->hy = yrel(np, src->hy);
94 dst->width = src->width;
95 dst->height = src->height;
99 xyabsrange(struct natstr *np, struct range *src, struct range *dst)
101 dst->lx = xabs(np, src->lx);
102 dst->hx = xabs(np, src->hx);
103 dst->ly = yabs(np, src->ly);
104 dst->hy = yabs(np, src->hy);
105 dst->width = src->width;
106 dst->height = src->height;
110 xysize_range(struct range *rp)
113 rp->width = WORLD_X + rp->hx + 1 - rp->lx;
115 rp->width = rp->hx + 1 - rp->lx;
116 if (CANT_HAPPEN(rp->width > WORLD_X))
119 rp->height = WORLD_Y + rp->hy + 1 - rp->ly;
121 rp->height = rp->hy + 1 - rp->ly;
122 if (CANT_HAPPEN(rp->height > WORLD_Y))
123 rp->height = WORLD_Y;
127 xydist_range(coord x, coord y, int dist, struct range *rp)
129 if (4 * dist + 1 < WORLD_X) {
130 rp->lx = xnorm(x - 2 * dist);
131 rp->hx = xnorm(x + 2 * dist);
132 rp->width = 4 * dist + 1;
134 rp->lx = xnorm(x - WORLD_X / 2);
135 rp->hx = xnorm(rp->lx + WORLD_X - 1);
139 if (2 * dist + 1 < WORLD_Y) {
140 rp->ly = ynorm(y - dist);
141 rp->hy = ynorm(y + dist);
142 rp->height = 2 * dist + 1;
144 rp->ly = ynorm(y - WORLD_Y / 2);
145 rp->hy = ynorm(rp->ly + WORLD_Y - 1);
146 rp->height = WORLD_Y;
151 * Convert initial part of @str to normalized x-coordinate.
152 * Return -1 on error. This works, as normalized coordinates are
154 * Assign pointer to first character after the coordinate to *@end,
155 * unless @end is a null pointer.
158 strtox(char *str, char **end)
163 l = strtol(str, end, 10);
164 if (*end == str || errno != 0)
170 * Convert initial part of @str to normalized y-coordinate.
171 * Return -1 on error. This works, as normalized coordinates are
173 * Assign pointer to first character after the coordinate to *@end,
174 * unless @end is a null pointer.
177 strtoy(char *str, char **end)
182 l = strtol(str, end, 10);
183 if (*end == str || errno != 0)
189 xabs(struct natstr *np, coord relx)
191 relx += np->nat_xorg;
196 yabs(struct natstr *np, coord rely)
198 rely += np->nat_yorg;
203 sctoff(coord x, coord y)
205 if (CANT_HAPPEN((x + y) & 1))
207 return XYOFFSET(XNORM(x), YNORM(y));
210 void sctoff2xy(coord *x, coord *y, int off)
212 *y = off * 2 / WORLD_X;
213 *x = off * 2 % WORLD_X + *y % 2;
229 xyinrange(coord x, coord y, struct range *rp)
231 if (rp->lx <= rp->hx) {
232 /* xrange doesn't wrap */
233 if (x < rp->lx || x > rp->hx)
236 if (x < rp->lx && x > rp->hx)
239 if (rp->ly <= rp->hy) {
240 /* yrange doesn't wrap */
241 if (y < rp->ly || y > rp->hy)
244 if (y < rp->ly && y > rp->hy)
252 prbuf(char *format, ...)
254 static int nbuf = -1;
255 static char buf[20][1024];
261 va_start(ap, format);
262 (void)vsprintf(buf[nbuf], format, ap);