[Freeciv-Dev] (PR#7220) create terrain.c
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
<URL: http://rt.freeciv.org/Ticket/Display.html?id=7220 >
The attached patch creates common/terrain.c. It is populated with:
- Macros from terrain.h, which have been turned into functions.
- Code moved over from map.h.
- One function extracted from mapgen.c (needed to solve a function
dependency).
Terrain.c will be needed eventually for gen-terrain. This patch just
moves code around to make it happen. Note that there are some changes
that aren't just cut-and-past: new functions are written, and
tile_type_free has been merged into tile_types_free.
In the future more accesses will be done on terrain flags rather than
specific terrain types. The "functions which operate on a general
terrain type" will be doubled up (or replaced by) to operate on terrain
flags as well, and the "terrain-specific functions" will use these
instead. It will be beautiful. The only question is what flags exactly
will be needed.
jason
? common/terrain.c
Index: common/Makefile.am
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/Makefile.am,v
retrieving revision 1.47
diff -u -r1.47 Makefile.am
--- common/Makefile.am 2003/11/28 17:37:21 1.47
+++ common/Makefile.am 2004/01/08 19:31:00
@@ -84,6 +84,7 @@
support.h \
tech.c \
tech.h \
+ terrain.c \
terrain.h \
timing.c \
timing.h \
Index: common/map.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.c,v
retrieving revision 1.153
diff -u -r1.153 map.c
--- common/map.c 2003/12/08 19:19:56 1.153
+++ common/map.c 2004/01/08 19:31:01
@@ -37,7 +37,6 @@
/* these are initialized from the terrain ruleset */
struct terrain_misc terrain_control;
-struct tile_type tile_types[T_LAST];
/* used to compute neighboring tiles */
const int DIR_DX[8] = { -1, 0, 1, -1, 1, -1, 0, 1 };
@@ -282,37 +281,6 @@
/***************************************************************
...
***************************************************************/
-struct tile_type *get_tile_type(enum tile_terrain_type type)
-{
- return &tile_types[type];
-}
-
-/***************************************************************
-...
-***************************************************************/
-enum tile_terrain_type get_terrain_by_name(const char * name)
-{
- enum tile_terrain_type tt;
- for (tt = T_FIRST; tt < T_COUNT; tt++) {
- if (0 == strcmp (tile_types[tt].terrain_name, name)) {
- break;
- }
- }
- return tt;
-}
-
-/***************************************************************
-...
-***************************************************************/
-const char *get_terrain_name(enum tile_terrain_type type)
-{
- assert(type < T_COUNT);
- return tile_types[type].terrain_name;
-}
-
-/***************************************************************
-...
-***************************************************************/
enum tile_special_type get_special_by_name(const char * name)
{
int i;
@@ -386,57 +354,6 @@
}
/***************************************************************
-...
-***************************************************************/
-bool is_terrain_near_tile(int x, int y, enum tile_terrain_type t)
-{
- adjc_iterate(x, y, x1, y1) {
- if (map_get_terrain(x1, y1) == t)
- return TRUE;
- } adjc_iterate_end;
-
- return FALSE;
-}
-
-/***************************************************************
- counts tiles close to x,y having terrain t
-***************************************************************/
-int count_terrain_near_tile(int x, int y, enum tile_terrain_type t)
-{
- int count = 0;
-
- adjc_iterate(x, y, x1, y1) {
- if (map_get_terrain(x1, y1) == t)
- count++;
- } adjc_iterate_end;
-
- return count;
-}
-
-/****************************************************************************
- Return the terrain flag matching the given string, or TER_LAST if there's
- no match.
-****************************************************************************/
-enum terrain_flag_id terrain_flag_from_str(const char *s)
-{
- enum terrain_flag_id flag;
- const char *flag_names[] = {
- /* Must match terrain flags in terrain.h. */
- "NoBarbs"
- };
-
- assert(ARRAY_SIZE(flag_names) == TER_COUNT);
-
- for (flag = TER_FIRST; flag < TER_LAST; flag++) {
- if (mystrcasecmp(flag_names[flag], s) == 0) {
- return flag;
- }
- }
-
- return TER_LAST;
-}
-
-/***************************************************************
determines if any tile close to x,y has special spe
***************************************************************/
bool is_special_near_tile(int x, int y, enum tile_special_type spe)
@@ -1671,27 +1588,4 @@
map_distance_vector(&diff_x, &diff_y, start_x, start_y, end_x, end_y);
return (diff_x == 0) || (diff_y == 0);
-}
-
-/**************************************************************************
- Free memory which is associated with this terrain type.
-**************************************************************************/
-static void tile_type_free(enum tile_terrain_type type)
-{
- struct tile_type *p = get_tile_type(type);
-
- free(p->helptext);
- p->helptext = NULL;
-}
-
-/**************************************************************************
- Free memory which is associated with terrain types.
-**************************************************************************/
-void tile_types_free(void)
-{
- enum tile_terrain_type i;
-
- for (i = T_FIRST; i < T_COUNT; i++) {
- tile_type_free(i);
- }
}
Index: common/map.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.h,v
retrieving revision 1.165
diff -u -r1.165 map.h
--- common/map.h 2003/11/28 17:37:21 1.165
+++ common/map.h 2004/01/08 19:31:01
@@ -81,7 +81,6 @@
tile_type for each terrain type
expand with government bonuses??
*****************************************************************/
-BV_DEFINE(bv_terrain_flags, TER_MAX);
struct tile_type {
char terrain_name[MAX_LEN_NAME]; /* "" if unused */
char terrain_name_orig[MAX_LEN_NAME]; /* untranslated copy */
@@ -310,15 +309,8 @@
bool is_tiles_adjacent(int x0, int y0, int x1, int y1);
bool is_move_cardinal(int start_x, int start_y, int end_x, int end_y);
int map_move_cost(struct unit *punit, int x, int y);
-struct tile_type *get_tile_type(enum tile_terrain_type type);
-void tile_types_free(void);
-enum tile_terrain_type get_terrain_by_name(const char * name);
-const char *get_terrain_name(enum tile_terrain_type type);
enum tile_special_type get_special_by_name(const char * name);
const char *get_special_name(enum tile_special_type type);
-bool is_terrain_near_tile(int x, int y, enum tile_terrain_type t);
-int count_terrain_near_tile(int x, int y, enum tile_terrain_type t);
-enum terrain_flag_id terrain_flag_from_str(const char *s);
bool is_special_near_tile(int x, int y, enum tile_special_type spe);
int count_special_near_tile(int x, int y, enum tile_special_type spe);
bool is_coastline(int x,int y);
@@ -359,7 +351,6 @@
extern struct civ_map map;
extern struct terrain_misc terrain_control;
-extern struct tile_type tile_types[T_LAST];
/* This iterates outwards from the starting point (Duh?).
Every tile within max_dist will show up exactly once. (even takes
Index: common/terrain.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/terrain.h,v
retrieving revision 1.7
diff -u -r1.7 terrain.h
--- common/terrain.h 2003/11/19 15:02:29 1.7
+++ common/terrain.h 2004/01/08 19:31:01
@@ -83,15 +83,29 @@
TILE_UNKNOWN, TILE_KNOWN_FOGGED, TILE_KNOWN
};
-/* These gets used very commonly, but we might change the definition of
- * ocean at a later date, and then we'll need to change only these
- * defines. */
-#define is_ocean(x) ((x) == T_OCEAN)
-#define is_ocean_near_tile(x, y) is_terrain_near_tile(x, y, T_OCEAN)
-#define adjacent_ocean_tiles4(x, y) adjacent_terrain_tiles4(x, y, T_OCEAN)
-#define count_ocean_near_tile(x,y) count_terrain_near_tile(x,y, T_OCEAN)
+BV_DEFINE(bv_terrain_flags, TER_MAX);
-#define terrain_has_flag(terr, flag) BV_ISSET(tile_types[(terr)].flags, flag)
+extern struct tile_type tile_types[T_LAST];
+
+/* General accessor functions. */
+struct tile_type *get_tile_type(enum tile_terrain_type type);
+enum tile_terrain_type get_terrain_by_name(const char * name);
+const char *get_terrain_name(enum tile_terrain_type type);
+enum terrain_flag_id terrain_flag_from_str(const char *s);
+bool terrain_has_flag(enum tile_terrain_type terrain,
+ enum terrain_flag_id flag);
+void tile_types_free(void);
+
+/* Functions to operate on a general terrain type. */
+bool is_terrain_near_tile(int map_x, int map_y, enum tile_terrain_type t);
+int count_terrain_near_tile(int map_x, int map_y, enum tile_terrain_type t);
+int adjacent_terrain_tiles4(int map_x, int map_y, enum tile_terrain_type t);
+
+/* Terrain-specific functions. */
+bool is_ocean(enum tile_terrain_type terrain);
+bool is_ocean_near_tile(int map_x, int map_y);
+int adjacent_ocean_tiles4(int map_x, int map_y);
+int count_ocean_near_tile(int map_x, int map_y);
/* This iterator iterates over all terrain types. */
#define terrain_type_iterate(id) \
Index: server/mapgen.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/mapgen.c,v
retrieving revision 1.123
diff -u -r1.123 mapgen.c
--- server/mapgen.c 2004/01/08 17:02:58 1.123
+++ server/mapgen.c 2004/01/08 19:31:02
@@ -298,23 +298,6 @@
}
/*********************************************************************
- Returns the number of adjacent tiles of a tile with the terrain
- terrain. This can be 0 to 4. -Erik Sigra
-*********************************************************************/
-static int adjacent_terrain_tiles4(int x, int y,
- enum tile_terrain_type terrain)
-{
- int num_adjacent = 0;
-
- cartesian_adjacent_iterate(x, y, x1, y1) {
- if (map_get_terrain(x1, y1) == terrain)
- num_adjacent++;
- } cartesian_adjacent_iterate_end;
-
- return num_adjacent;
-}
-
-/*********************************************************************
Help function used in make_river(). See the help there.
*********************************************************************/
static int river_test_blocked(int x, int y)
/**********************************************************************
Freeciv - Copyright (C) 2003 - The Freeciv Project
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, 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.
***********************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "map.h"
#include "shared.h"
#include "support.h"
#include "terrain.h"
struct tile_type tile_types[T_LAST];
/***************************************************************
...
***************************************************************/
struct tile_type *get_tile_type(enum tile_terrain_type type)
{
return &tile_types[type];
}
/***************************************************************
...
***************************************************************/
enum tile_terrain_type get_terrain_by_name(const char * name)
{
enum tile_terrain_type tt;
for (tt = T_FIRST; tt < T_COUNT; tt++) {
if (0 == strcmp (tile_types[tt].terrain_name, name)) {
break;
}
}
return tt;
}
/***************************************************************
...
***************************************************************/
const char *get_terrain_name(enum tile_terrain_type type)
{
assert(type < T_COUNT);
return tile_types[type].terrain_name;
}
/****************************************************************************
Return the terrain flag matching the given string, or TER_LAST if there's
no match.
****************************************************************************/
enum terrain_flag_id terrain_flag_from_str(const char *s)
{
enum terrain_flag_id flag;
const char *flag_names[] = {
/* Must match terrain flags in terrain.h. */
"NoBarbs"
};
assert(ARRAY_SIZE(flag_names) == TER_COUNT);
for (flag = TER_FIRST; flag < TER_LAST; flag++) {
if (mystrcasecmp(flag_names[flag], s) == 0) {
return flag;
}
}
return TER_LAST;
}
/****************************************************************************
Return TRUE iff the terrain has this flag.
****************************************************************************/
bool terrain_has_flag(enum tile_terrain_type terrain,
enum terrain_flag_id flag)
{
return BV_ISSET(tile_types[terrain].flags, flag);
}
/****************************************************************************
Free memory which is associated with terrain types.
****************************************************************************/
void tile_types_free(void)
{
terrain_type_iterate(t) {
struct tile_type *p = get_tile_type(t);
free(p->helptext);
p->helptext = NULL;
} terrain_type_iterate_end;
}
/****************************************************************************
Returns TRUE iff any adjacent tile contains the given terrain.
****************************************************************************/
bool is_terrain_near_tile(int map_x, int map_y, enum tile_terrain_type t)
{
adjc_iterate(map_x, map_y, adjc_x, adjc_y) {
if (map_get_terrain(adjc_x, adjc_y) == t) {
return TRUE;
}
} adjc_iterate_end;
return FALSE;
}
/****************************************************************************
Return the number of adjacent tiles that have the given terrain.
****************************************************************************/
int count_terrain_near_tile(int map_x, int map_y, enum tile_terrain_type t)
{
int count = 0;
adjc_iterate(map_x, map_y, adjc_x, adjc_y) {
if (map_get_terrain(adjc_x, adjc_y) == t) {
count++;
}
} adjc_iterate_end;
return count;
}
/****************************************************************************
Return the number of cardinally adjacent tiles that have the given terrain.
****************************************************************************/
int adjacent_terrain_tiles4(int map_x, int map_y, enum tile_terrain_type t)
{
int num_adjacent = 0;
cartesian_adjacent_iterate(map_x, map_y, adjc_x, adjc_y) {
if (map_get_terrain(adjc_x, adjc_y) == t) {
num_adjacent++;
}
} cartesian_adjacent_iterate_end;
return num_adjacent;
}
/****************************************************************************
Return TRUE iff the terrain is oceanic.
****************************************************************************/
bool is_ocean(enum tile_terrain_type terrain)
{
return (terrain == T_OCEAN);
}
/****************************************************************************
Return TRUE iff there is ocean adjacent to the map position.
****************************************************************************/
bool is_ocean_near_tile(int map_x, int map_y)
{
return is_terrain_near_tile(map_x, map_y, T_OCEAN);
}
/****************************************************************************
Return the number of cardinally adjacent tiles that are oceanic.
****************************************************************************/
int adjacent_ocean_tiles4(int map_x, int map_y)
{
return adjacent_terrain_tiles4(map_x, map_y, T_OCEAN);
}
/****************************************************************************
Return the number of oceanic tiles adjacent to the position.
****************************************************************************/
int count_ocean_near_tile(int map_x, int map_y)
{
return count_terrain_near_tile(map_x, map_y, T_OCEAN);
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Freeciv-Dev] (PR#7220) create terrain.c,
Jason Short <=
|
|