]> git.pond.sub.org Git - empserver/blob - src/lib/gen/mapdist.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / gen / mapdist.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  *  mapdist.c: Return the distance between two sectors
29  * 
30  *  Known contributors to this file:
31  *     
32  */
33
34 /*
35  * mapdist returns (integer) distance between two sectors.
36  * mapdsq returns the square of the distance -- more efficient.
37  */
38
39 #include "misc.h"
40 #include "gen.h"
41 #include "optlist.h"
42
43 int
44 diffx(int x1, int x2)
45 {
46     int dx;
47
48     dx = x1 - x2;
49     dx = dx % WORLD_X;
50     if (dx > WORLD_X / 2)
51         dx = dx - WORLD_X;
52     if (dx < -WORLD_X / 2)
53         dx = dx + WORLD_X;
54     return dx;
55 }
56
57 int
58 diffy(int y1, int y2)
59 {
60     int dy;
61
62     dy = y1 - y2;
63     dy = dy % WORLD_Y;
64     if (dy > WORLD_Y / 2)
65         dy = dy - WORLD_Y;
66     if (dy < -WORLD_Y / 2)
67         dy = dy + WORLD_Y;
68     return dy;
69 }
70
71 int
72 deltax(int x1, int x2)
73 {
74     int dx;
75
76     dx = abs(x1 - x2);
77     dx = dx % WORLD_X;
78     if (dx > WORLD_X / 2)
79         dx = WORLD_X - dx;
80     return dx;
81 }
82
83 int
84 deltay(int y1, int y2)
85 {
86     int dy;
87
88     dy = abs(y1 - y2);
89     dy = dy % WORLD_Y;
90     if (dy > WORLD_Y / 2)
91         dy = WORLD_Y - dy;
92     return dy;
93 }
94
95 int
96 mapdist(int x1, int y1, int x2, int y2)
97 {
98     int dx, dy;
99
100     x1 = x1 % WORLD_X;
101     y1 = y1 % WORLD_Y;
102     x2 = x2 % WORLD_X;
103     y2 = y2 % WORLD_Y;
104     dx = deltax(x1, x2);
105     dy = deltay(y1, y2);
106     if (dx > dy)
107         return (dx - dy) / 2 + dy;
108     return dy;
109 }
110
111 int
112 mapdsq(int x1, int y1, int x2, int y2)
113 {
114     int sq;
115
116     sq = mapdist(x1, y1, x2, y2);
117     return sq * sq;
118 }