]> git.pond.sub.org Git - empserver/blob - src/lib/gen/mapdist.c
ede7a0b1426938a79e92b845bd3872c2948d0c75
[empserver] / src / lib / gen / mapdist.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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  */
37
38 #include <config.h>
39
40 #include "misc.h"
41 #include "optlist.h"
42 #include "prototypes.h"
43
44 int
45 diffx(int x1, int x2)
46 {
47     int dx;
48
49     dx = x1 - x2;
50     dx = dx % WORLD_X;
51     if (dx > WORLD_X / 2)
52         dx = dx - WORLD_X;
53     if (dx < -WORLD_X / 2)
54         dx = dx + WORLD_X;
55     return dx;
56 }
57
58 int
59 diffy(int y1, int y2)
60 {
61     int dy;
62
63     dy = y1 - y2;
64     dy = dy % WORLD_Y;
65     if (dy > WORLD_Y / 2)
66         dy = dy - WORLD_Y;
67     if (dy < -WORLD_Y / 2)
68         dy = dy + WORLD_Y;
69     return dy;
70 }
71
72 int
73 deltax(int x1, int x2)
74 {
75     int dx;
76
77     dx = abs(x1 - x2);
78     dx = dx % WORLD_X;
79     if (dx > WORLD_X / 2)
80         dx = WORLD_X - dx;
81     return dx;
82 }
83
84 int
85 deltay(int y1, int y2)
86 {
87     int dy;
88
89     dy = abs(y1 - y2);
90     dy = dy % WORLD_Y;
91     if (dy > WORLD_Y / 2)
92         dy = WORLD_Y - dy;
93     return dy;
94 }
95
96 int
97 mapdist(int x1, int y1, int x2, int y2)
98 {
99     int dx, dy;
100
101     x1 = x1 % WORLD_X;
102     y1 = y1 % WORLD_Y;
103     x2 = x2 % WORLD_X;
104     y2 = y2 % WORLD_Y;
105     dx = deltax(x1, x2);
106     dy = deltay(y1, y2);
107     if (dx > dy)
108         return (dx - dy) / 2 + dy;
109     return dy;
110 }