empserver/src/lib/subs/coastal.c
Markus Armbruster ad0a37eca4 (update_coastal_flag, coast_sea_to_land, coastal_land_to_sea): Return
new coastal flag instead of updating the sector.  This is a bit
simpler.  Change arguments from sectors to coordinates to make it
obvious that the sector is not touched.
(set_coastal): Update the coastal flag, but leave putsect() to
callers.  All existing callers do that anyway.
2006-07-21 18:01:28 +00:00

106 lines
2.8 KiB
C

/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See files README, COPYING and CREDITS in the root of the source
* tree for related information and legal notices. It is expected
* that future projects/authors will amend these files as needed.
*
* ---
*
* coastal.c: Routines to calculate the coastal flag
*
* Known contributors to this file:
* Ron Koenderink, 2005
*/
#include <config.h>
#include "file.h"
#include "path.h"
#include "prototypes.h"
#include "sect.h"
static int
update_coastal_flag(coord x, coord y, coord ign_x, coord ign_y)
{
int n;
struct sctstr sect;
for (n = 1; n <= 6; n++) { /* Directions */
getsect(x + diroff[n][0], y + diroff[n][1], &sect);
if (sect.sct_x == ign_x && sect.sct_y == ign_y)
continue;
if (sect.sct_type == SCT_WATER || sect.sct_type == SCT_BTOWER ||
sect.sct_type == SCT_BSPAN)
return 1;
}
return 0;
}
static int
coastal_sea_to_land(coord x, coord y)
{
int n, c;
struct sctstr sect;
for (n = 1; n <= 6; n++) { /* Directions */
getsect(x + diroff[n][0], y + diroff[n][1], &sect);
c = update_coastal_flag(sect.sct_x, sect.sct_y, x, y);
if (!sect.sct_coastal != !c) {
sect.sct_coastal = c;
putsect(&sect);
}
}
return update_coastal_flag(x, y, x, y);
}
static int
coastal_land_to_sea(coord x, coord y)
{
int n;
struct sctstr sect;
for (n = 1; n <= 6; ++n) { /* Directions */
getsect(x + diroff[n][0], y + diroff[n][1], &sect);
if (!sect.sct_coastal) {
sect.sct_coastal = 1;
putsect(&sect);
}
}
return 1;
}
void
set_coastal(struct sctstr *sp, int des)
{
int old_water = sp->sct_type == SCT_WATER
|| sp->sct_type == SCT_BTOWER || sp->sct_type == SCT_BSPAN;
int new_water = des == SCT_WATER
|| des == SCT_BTOWER || des == SCT_BSPAN;
if (new_water != old_water)
sp->sct_coastal = new_water
? coastal_land_to_sea(sp->sct_x, sp->sct_y)
: coastal_sea_to_land(sp->sct_x, sp->sct_y);
}