]> git.pond.sub.org Git - empserver/blob - src/lib/common/xy.c
deity.h is redundant, remove it.
[empserver] / src / lib / common / xy.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  xy.c: x-y related conversion routines
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  */
33
34 #include <ctype.h>
35 #include <stdarg.h>
36 #include "misc.h"
37 #include "xy.h"
38 #include "nat.h"
39 #include "sect.h"
40 #include "file.h"
41 #include <stdio.h>
42 #include "common.h"
43 #include "optlist.h"
44
45 /*
46  * return pointer to a string containing the x,y
47  * coords as desired by a particular target country.
48  */
49 s_char *
50 xyas(coord x, coord y, natid country)
51 {
52     struct natstr *np;
53
54     np = getnatp(country);
55     return prbuf("%d,%d", xrel(np, x), yrel(np, y));
56 }
57
58 s_char *
59 ownxy(struct sctstr *sp)
60 {
61     return xyas(sp->sct_x, sp->sct_y, sp->sct_own);
62 }
63
64 coord
65 xrel(struct natstr *np, coord absx)
66 {
67     coord x;
68
69     if ((np->nat_stat & STAT_ABS) == 0) {
70         x = XNORM(absx - np->nat_xorg);
71     } else {
72         x = XNORM(absx);
73     }
74     if (x >= WORLD_X / 2)
75         x -= WORLD_X;
76     else if (x < -WORLD_X / 2)
77         x += WORLD_X;
78     return x;
79 }
80
81 coord
82 yrel(struct natstr *np, coord absy)
83 {
84     coord y;
85
86     if ((np->nat_stat & STAT_ABS) == 0) {
87         y = YNORM(absy - np->nat_yorg);
88     } else {
89         y = YNORM(absy);
90     }
91     if (y >= WORLD_Y / 2)
92         y -= WORLD_Y;
93     else if (y < -WORLD_Y / 2)
94         y += WORLD_Y;
95     return y;
96 }
97
98 void
99 xyrelrange(struct natstr *np, struct range *src, struct range *dst)
100 {
101     dst->lx = xrel(np, src->lx);
102     dst->hx = xrel(np, src->hx);
103     dst->ly = yrel(np, src->ly);
104     dst->hy = yrel(np, src->hy);
105     dst->width = src->width;
106     dst->height = src->height;
107 }
108
109 void
110 xyabsrange(struct natstr *np, struct range *src, struct range *dst)
111 {
112     dst->lx = xabs(np, src->lx);
113     dst->hx = xabs(np, src->hx);
114     dst->ly = yabs(np, src->ly);
115     dst->hy = yabs(np, src->hy);
116     dst->width = src->width;
117     dst->height = src->height;
118 }
119
120 void
121 inputxy(coord *xp, coord *yp, natid cn)
122 {
123     struct natstr *np;
124
125     np = getnatp(cn);
126     *xp = xabs(np, *xp);
127     *yp = yabs(np, *yp);
128 }
129
130 coord
131 xabs(struct natstr *np, coord relx)
132 {
133     if ((np->nat_stat & STAT_ABS) == 0)
134         relx += np->nat_xorg;
135     return XNORM(relx);
136 }
137
138 coord
139 yabs(struct natstr *np, coord rely)
140 {
141     if ((np->nat_stat & STAT_ABS) == 0)
142         rely += np->nat_yorg;
143     return YNORM(rely);
144 }
145
146 int
147 sctoff(coord x, coord y)
148 {
149     if ((x + y) & 01) {
150         logerror("%d,%d is an invalid sector specification!\n", x, y);
151         return -1;
152     }
153     return (YNORM(y) * WORLD_X + XNORM(x)) / 2;
154 }
155
156 coord
157 xnorm(register coord x)
158 {
159     if (x < 0)
160         x = WORLD_X - (-x % WORLD_X);
161     return x % WORLD_X;
162 }
163
164 coord
165 ynorm(register coord y)
166 {
167     if (y < 0)
168         y = WORLD_Y - (-y % WORLD_Y);
169     return y % WORLD_Y;
170 }
171
172 int
173 xyinrange(coord x, coord y, struct range *rp)
174 {
175     if (rp->lx < rp->hx) {
176         /* xrange doesn't wrap */
177         if (x < rp->lx || x > rp->hx)
178             return 0;
179     } else {
180         if (x < rp->lx && x > rp->hx)
181             return 0;
182     }
183     if (rp->ly < rp->hy) {
184         /* yrange doesn't wrap */
185         if (y < rp->ly || y > rp->hy)
186             return 0;
187     } else {
188         if (y < rp->ly && y > rp->hy)
189             return 0;
190     }
191     return 1;
192 }
193
194
195 s_char *
196 prbuf(s_char *format, ...)
197 {
198     static int nbuf = -1;
199     static s_char buf[20][1024];
200     va_list ap;
201
202     if (++nbuf > 19)
203         nbuf = 0;
204
205     va_start(ap, format);
206     (void)vsprintf(buf[nbuf], format, ap);
207     va_end(ap);
208
209     return buf[nbuf];
210 }