]> git.pond.sub.org Git - empserver/blob - src/lib/subs/coastal.c
37f94d2f50b82d6eb570249d88e8abc1575e8b9c
[empserver] / src / lib / subs / coastal.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2012, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  coastal.c: Routines to calculate the coastal flag
28  *
29  *  Known contributors to this file:
30  *     Ron Koenderink, 2005
31  */
32
33 #include <config.h>
34
35 #include "file.h"
36 #include "path.h"
37 #include "prototypes.h"
38 #include "sect.h"
39
40 static int
41 update_coastal_flag(coord x, coord y, coord ign_x, coord ign_y)
42 {
43     int n;
44     struct sctstr sect;
45
46     for (n = 1; n <= 6; n++) {  /* Directions */
47         getsect(x + diroff[n][0], y + diroff[n][1], &sect);
48         if (sect.sct_x == ign_x && sect.sct_y == ign_y)
49             continue;
50         if (sect.sct_type == SCT_WATER || sect.sct_type == SCT_BTOWER ||
51             sect.sct_type == SCT_BSPAN)
52             return 1;
53     }
54
55     return 0;
56 }
57
58 static int
59 coastal_sea_to_land(coord x, coord y)
60 {
61     int n, c;
62     struct sctstr sect;
63
64     for (n = 1; n <= 6; n++) {  /* Directions */
65         getsect(x + diroff[n][0], y + diroff[n][1], &sect);
66         c = update_coastal_flag(sect.sct_x, sect.sct_y, x, y);
67         if (!sect.sct_coastal != !c) {
68             sect.sct_coastal = c;
69             putsect(&sect);
70         }
71     }
72
73     return update_coastal_flag(x, y, x, y);
74 }
75
76 static int
77 coastal_land_to_sea(coord x, coord y)
78 {
79     int n;
80     struct sctstr sect;
81
82     for (n = 1; n <= 6; ++n) {  /* Directions */
83         getsect(x + diroff[n][0], y + diroff[n][1], &sect);
84         if (!sect.sct_coastal) {
85             sect.sct_coastal = 1;
86             putsect(&sect);
87         }
88     }
89
90     return 1;
91 }
92
93 /*
94  * Compute coastal flags for a change of SP from OLDDES to NEWDES.
95  * Update adjacent sectors, but don't touch SP.
96  * Return new coastal flag for SP.
97  */
98 void
99 set_coastal(struct sctstr *sp, int olddes, int newdes)
100 {
101     int old_water = olddes == SCT_WATER
102         || olddes == SCT_BTOWER || olddes == SCT_BSPAN;
103     int new_water = newdes == SCT_WATER
104         || newdes == SCT_BTOWER || newdes == SCT_BSPAN;
105
106     if (new_water != old_water)
107         sp->sct_coastal = new_water
108             ? coastal_land_to_sea(sp->sct_x, sp->sct_y)
109             : coastal_sea_to_land(sp->sct_x, sp->sct_y);
110 }