]> git.pond.sub.org Git - empserver/blob - src/lib/common/xy.c
Import of Empire 4.2.12
[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 "deity.h"
41 #include "file.h"
42 #include <stdio.h>
43 #include "common.h"
44 #include "optlist.h"
45
46 /*
47  * return pointer to a string containing the x,y
48  * coords as desired by a particular target country.
49  */
50 s_char *
51 xyas(coord x, coord y, natid country)
52 {
53         struct  natstr *np;
54
55         np = getnatp(country);
56         return prbuf("%d,%d", xrel(np, x), yrel(np, y));
57 }
58
59 s_char *
60 ownxy(struct sctstr *sp)
61 {
62         return xyas(sp->sct_x, sp->sct_y, sp->sct_own);
63 }
64
65 coord
66 xrel(struct natstr *np, coord absx)
67 {
68         coord   x;
69
70         if ((np->nat_stat & STAT_ABS) == 0) {
71                 x = XNORM(absx - np->nat_xorg);
72         } else {
73                 x = XNORM(absx);
74         }
75         if (x >= WORLD_X/2)
76                 x -= WORLD_X;
77         else if (x < -WORLD_X/2)
78                 x += WORLD_X;
79         return x;
80 }
81
82 coord
83 yrel(struct natstr *np, coord absy)
84 {
85         coord   y;
86
87         if ((np->nat_stat & STAT_ABS) == 0) {
88                 y = YNORM(absy - np->nat_yorg);
89         } else {
90                 y = YNORM(absy);
91         }
92         if (y >= WORLD_Y/2)
93                 y -= WORLD_Y;
94         else if (y < -WORLD_Y/2)
95                 y += WORLD_Y;
96         return y;
97 }
98
99 void
100 xyrelrange(struct natstr *np, struct range *src, struct range *dst)
101 {
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;
108 }
109
110 void
111 xyabsrange(struct natstr *np, struct range *src, struct range *dst)
112 {
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;
119 }
120
121 void
122 inputxy(coord *xp, coord *yp, natid cn)
123 {
124         struct  natstr *np;
125
126         np = getnatp(cn);
127         *xp = xabs(np, *xp);
128         *yp = yabs(np, *yp);
129 }
130
131 coord
132 xabs(struct natstr *np, coord relx)
133 {
134         if ((np->nat_stat & STAT_ABS) == 0)
135                 relx += np->nat_xorg;
136         return XNORM(relx);
137 }
138
139 coord
140 yabs(struct natstr *np, coord rely)
141 {
142         if ((np->nat_stat & STAT_ABS) == 0)
143                 rely += np->nat_yorg;
144         return YNORM(rely);
145 }
146
147 int
148 sctoff(coord x, coord y)
149 {
150         if ((x + y) & 01) {
151                 logerror("%d,%d is an invalid sector specification!\n", x, y); 
152                 return -1;
153         }
154         return (YNORM(y) * WORLD_X + XNORM(x))/2;
155 }
156
157 coord
158 xnorm(register coord x)
159 {
160         if (x < 0)
161                 x = WORLD_X - (-x % WORLD_X);
162         return x % WORLD_X;
163 }
164
165 coord
166 ynorm(register coord y)
167 {
168         if (y < 0)
169                 y = WORLD_Y - (-y % WORLD_Y);
170         return y % WORLD_Y;
171 }
172
173 int
174 xyinrange(coord x, coord y, struct range *rp)
175 {
176         if (rp->lx < rp->hx) {
177                 /* xrange doesn't wrap */
178                 if (x < rp->lx || x > rp->hx)
179                         return 0;
180         } else {
181                 if (x < rp->lx && x > rp->hx)
182                         return 0;
183         }
184         if (rp->ly < rp->hy) {
185                 /* yrange doesn't wrap */
186                 if (y < rp->ly || y > rp->hy)
187                         return 0;
188         } else {
189                 if (y < rp->ly && y > rp->hy)
190                         return 0;
191         }
192         return 1;
193 }
194
195
196 s_char *
197 prbuf(s_char *format, ...)
198 {
199         static  int     nbuf = -1;
200         static  s_char  buf[20][1024];
201         va_list ap;
202
203         if (++nbuf > 19)
204                 nbuf = 0;
205
206         va_start(ap, format);
207         (void) vsprintf(buf[nbuf], format, ap);
208         va_end(ap);
209
210         return buf[nbuf];
211 }
212