]> git.pond.sub.org Git - empserver/blob - src/lib/subs/coastal.c
Update copyright notice
[empserver] / src / lib / subs / coastal.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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  *  coastal.c: Routines to calculate the coastal flag
29  *
30  *  Known contributors to this file:
31  *     Ron Koenderink, 2005
32  */
33
34 #include <config.h>
35
36 #include "file.h"
37 #include "path.h"
38 #include "prototypes.h"
39 #include "sect.h"
40
41 static int
42 update_coastal_flag(coord x, coord y, coord ign_x, coord ign_y)
43 {
44     int n;
45     struct sctstr sect;
46
47     for (n = 1; n <= 6; n++) {  /* Directions */
48         getsect(x + diroff[n][0], y + diroff[n][1], &sect);
49         if (sect.sct_x == ign_x && sect.sct_y == ign_y)
50             continue;
51         if (sect.sct_type == SCT_WATER || sect.sct_type == SCT_BTOWER ||
52             sect.sct_type == SCT_BSPAN)
53             return 1;
54     }
55
56     return 0;
57 }
58
59 static int
60 coastal_sea_to_land(coord x, coord y)
61 {
62     int n, c;
63     struct sctstr sect;
64
65     for (n = 1; n <= 6; n++) {  /* Directions */
66         getsect(x + diroff[n][0], y + diroff[n][1], &sect);
67         c = update_coastal_flag(sect.sct_x, sect.sct_y, x, y);
68         if (!sect.sct_coastal != !c) {
69             sect.sct_coastal = c;
70             putsect(&sect);
71         }
72     }
73
74     return update_coastal_flag(x, y, x, y);
75 }
76
77 static int
78 coastal_land_to_sea(coord x, coord y)
79 {
80     int n;
81     struct sctstr sect;
82
83     for (n = 1; n <= 6; ++n) {  /* Directions */
84         getsect(x + diroff[n][0], y + diroff[n][1], &sect);
85         if (!sect.sct_coastal) {
86             sect.sct_coastal = 1;
87             putsect(&sect);
88         }
89     }
90
91     return 1;
92 }
93
94 /*
95  * Compute coastal flags for a change of SP from OLDDES to NEWDES.
96  * Update adjacent sectors, but don't touch SP.
97  * Return new coastal flag for SP.
98  */
99 void
100 set_coastal(struct sctstr *sp, int olddes, int newdes)
101 {
102     int old_water = olddes == SCT_WATER
103         || olddes == SCT_BTOWER || olddes == SCT_BSPAN;
104     int new_water = newdes == SCT_WATER
105         || newdes == SCT_BTOWER || newdes == SCT_BSPAN;
106
107     if (new_water != old_water)
108         sp->sct_coastal = new_water
109             ? coastal_land_to_sea(sp->sct_x, sp->sct_y)
110             : coastal_sea_to_land(sp->sct_x, sp->sct_y);
111 }