/* Definitions relating to worlds and areas in Xconq.
   Copyright (C) 1987-1989, 1991-2000 Stanley T. Shebs.

Xconq 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.  See the file COPYING.  */

typedef struct a_world {
    int circumference;		/* number of cells going around the world */
    int daylength;		/* number of turns in a day */
    int yearlength;		/* number of turns in a year */
    int axial_tilt;		/* controls extrema of seasons */
    int daylight_fraction;	/* percentage of circumference that is lit */
    int twilight_fraction;	/* percentage of circumference that is partly lit */
    int daylight_width;		/* width of world that is lit */
    int twilight_width;		/* width of world that is at least partly lit */
} World;

/* Theoretically, there is no maximum size to Xconq areas, but the minimum
   size is set by mystical properties, and is not negotiable. */

#define MINWIDTH 3
#define MINHEIGHT 3

/* An "area" is always basically a rectangular array of positions.  The
   hex effect can be achieved by interpreting neighborliness and direction
   differently, but that's all that's needed.  All of the "layers" are
   dynamically allocated as needed, to save (considerable!) space. */

typedef struct a_area {
    short width, height;	/* size of the area */
    short halfwidth, halfheight; /* width / 2, height / 2 */
    short maxdim;		/* max of the two dims */
    short xwrap;		/* true if x coords wrap around */
    int numcells;		/* number of cells in area */
    int latitude, longitude;	/* position within whole world */
    short projection;
    int cellwidth;		/* distance across one cell */
    short fullwidth, fullheight; /* size of the area being used for data */
    short fullx, fully;		/* offset within full area to get data from */
    short sunx, suny;		/* location at which sun is overhead */
    Obj *temp_year;		/* cycle of temperatures within year */
    char *image_name;		/* name of image to display for map */
    /* Pointers to the various "layers". */
    struct a_unit **units;	/* pointer to units if any */
    char *terrain;		/* terrain type at this spot */
    char **auxterrain;		/* vector of extra types */
    char *peopleside;		/* the overt side alignment of the locals */
    char *controlside;		/* the side actually in charge */
    short *features;		/* layer of ids of features */
    short *elevations;		/* layer of elevations */
    short avgelev;		/* calculated average of elevations */
    short minelev, maxelev;	/* calculated min/max of elevations */
    short **materials;		/* layer of materials in each cell */
    short *temperature;		/* layer of cell temperatures */
    short *clouds;		/* types of clouds in the layer */
    short *cloudbottoms;	/* altitudes of clouds of cloud layer */
    short *cloudheights;	/* heights of clouds in the cloud layer */
    short *winds;		/* layer of force/dir of winds. */
    /* These layers are for temporary use in calculations. */
    short *tmp1;
    short *tmp2;
    short *tmp3;
    struct a_region **regions;
    int *user;			/* IDs of units using materials in each cell */
} Area;

/* Named geographical features. */

typedef struct a_feature {
    int type;			/* index of the general type */
    short id;			/* which one this is */
    char *name;			/* the name of the region */
    char *typename;		/* its category, such as "island" or "bay" */
    /* something for syntax? "foo bay" vs "bay of foo" */
    struct a_feature *next;	/* arranged in a linked list */
    /* caches */
    int size;
    int x, y;
    short xmin, ymin;
    short xmax, ymax;
    int mindiam;
    int maxdiam;
    int relabel;
} Feature;

/* Paths. */

typedef struct a_waypoint {
    int x, y;
    int note;
} Waypoint;

typedef struct a_path {
    int numwps;
    Waypoint wps[100];
} Path;

/* Regions. */

typedef struct a_region {
    int ttype;
    int size;
    short xmin, ymin;
    short xmax, ymax;
    struct a_region *merge;
    struct a_region_link *neighbors;
    struct a_region *next;
} TRegion;

typedef struct a_region_link {
    struct a_region *neighbor;
    struct a_region_link *next;
} TRegionLink;

/* Use this macro to get a area-spanning layer of the given type. */

#define malloc_area_layer(TYPE)  \
  ((TYPE *) xmalloc(area.width * area.height * sizeof(TYPE)))

#define zero_area_layer(ADDR, TYPE)  \
  (memset(ADDR, 0, area.width * area.height * sizeof(TYPE)))

/* General 2D malloced area array usage.  Names from Lisp. */

#define aref(m,x,y) ((m)[area.width * (y) + (x)])

#define aset(m,x,y,v) ((m)[area.width * (y) + (x)] = (v))

#define aadd(m,x,y,v) ((m)[area.width * (y) + (x)] += (v))

/* Advanced unit support. */

#define user_defined() (area.user != NULL)

#define user_at(x, y) aref(area.user, x, y)

#define set_user_at(x, y, u) aset(area.user, x, y, u)

/* Test if cell at (x1, y1) is within n steps from (x, y) */
#define cell_is_within_range(x, y, n, x1, y1) \
	( (x) - (x1) > (n) || (x1) - (x) > (n) || (y) - (y1) > (n) \
	|| (y1) - (y) > (n) || (x) + (y) - (x1) - (y1) > (n) \
	|| (x1) + (y1) - (x) - (y) > (n) ) ? FALSE : TRUE

/* Test if cell at (x1, y1) is within reach of unit u */
#define cell_is_within_reach(u, x1, y1) \
	cell_is_within_range((u)->x, (u)->y, (u)->reach, (x1), (y1))

/* Repeat for all cells at (x1, y1) within n steps from (x, y) */
#define for_all_cells_within_range(x, y, n, x1, y1) \
	for((x1) = (x) - (n); (x1) <= (x) + (n); (x1)++) \
	for((y1) = max((y) - (n), (y) - (n) + (x) - (x1)); \
	       (y1) <= min((y) + (n), (y) + (n) + (x) - (x1)); (y1)++)

/* Repeat for all cells at (x1, y1) within reach of unit u */
#define for_all_cells_within_reach(u, x1, y1) \
	for_all_cells_within_range((u)->x, (u)->y, (u)->reach, (x1), (y1))

/* The unit is a raw pointer - this macro is used a *lot*.  This could
   be space-optimized by using a 16-bit unit id. */

#define unit_at(x,y) aref(area.units, x, y)

#define set_unit_at(x,y,u) aset(area.units, x, y, u)

/* Iterate through all units in this cell (but not their occs) . */

#define for_all_stack(x,y,var)  \
  for ((var) = unit_at((x), (y)); (var) != NULL;  (var) = (var)->nexthere)

/* Iterate through all units in this cell including occs within occs
   within occs. */

/* This nifty little macro will climb the occupant : nexthere tree and
   follow all branches four levels down to find all the occs within
   occs within occs within units in the stack. */

#define for_all_stack_with_occs(x,y,var)  \
  for ((var) = unit_at((x), (y)); \
          (var) != NULL; \
  	 (var) = ((var)->occupant != NULL ? \
  	 	        (var)->occupant : \
  		      ((var)->nexthere != NULL ? \
  		        (var)->nexthere : \
  		      ((var)->transport != NULL && \
  		        (var)->transport->nexthere != NULL ? \
  		        (var)->transport->nexthere : \
  		      ((var)->transport != NULL && \
  		        (var)->transport->transport != NULL && \
  		        (var)->transport->transport->nexthere != NULL ? \
  		        (var)->transport->transport->nexthere : \
  		      ((var)->transport != NULL && \
  		        (var)->transport->transport != NULL && \
  		        (var)->transport->transport->transport != NULL && \
  		        (var)->transport->transport->transport->nexthere != NULL ? \
  		        (var)->transport->transport->transport->nexthere : NULL))))))

/* Test if the terrain has been allocated yet. */

#define terrain_defined() (area.terrain != NULL)

/* The terrain at each cell is just the number of the terrain type. */

#define terrain_at(x,y) aref(area.terrain, x, y)

#define set_terrain_at(x,y,t) aset(area.terrain, x, y, t)

/* Auxiliary terrain array of layers. */

#define any_aux_terrain_defined() (area.auxterrain != NULL)

#define aux_terrain_defined(t)  \
  (any_aux_terrain_defined() && area.auxterrain[t] != NULL)

#define aux_terrain_at(x,y,t) aref(area.auxterrain[t], x, y)

#define set_aux_terrain_at(x,y,t,v) aset(area.auxterrain[t], x, y, v)

/* Not really correct, should finish. */

#define any_borders_at(x,y,b) (aux_terrain_at(x, y, b) != 0)

#define border_at(x,y,dir,t)  \
  (aux_terrain_defined(t) ? (aux_terrain_at(x, y, t) & (1 << (dir))) : FALSE)

#define any_connections_at(x,y,c) (aux_terrain_at(x, y, c) != 0)

#define connection_at(x,y,dir,t)  \
  (aux_terrain_defined(t) ? (aux_terrain_at(x, y, t) & (1 << (dir))) : FALSE)

/* Elevation layer. */

#define world_is_flat() (minelev == maxelev)

#define elevations_defined() (area.elevations != NULL)

#define elev_at(x,y) aref(area.elevations, x, y)

#define set_elev_at(x,y,v) aset(area.elevations, x, y, v)

/* Feature layer. */

#define features_defined() (area.features != NULL)

/* The "raw feature" is its "short" identifier. */

#define raw_feature_at(x,y) aref(area.features, x, y)

#define set_raw_feature_at(x,y,f) aset(area.features, x, y, f)

/* Population layer. */

#define people_sides_defined() (area.peopleside != NULL)

#define people_side_at(x,y) aref(area.peopleside, x, y)

#define set_people_side_at(x,y,s) aset(area.peopleside, x, y, s)

/* A cell might be entirely uninhabited, so need an extra value to indicate. */

/* This value is chosen to be well above any possible MAXSIDES, and encodes
   in layers as 'X', which is convenient. */

#define NOBODY (60)

#define populated(x,y) (people_side_at(x,y) != NOBODY)

/* Control layer. */

#define control_sides_defined() (area.controlside != NULL)

#define control_side_at(x,y) aref(area.controlside, x, y)

#define set_control_side_at(x,y,s) aset(area.controlside, x, y, s)

/* A cell might be entirely uncontrolled, so need an extra value to indicate. */

/* This value is chosen to be well above any possible MAXSIDES, and encodes
   in layers as 'X', which is convenient. */

#define NOCONTROL (60)

#define controlled(x,y) (control_side_at(x,y) != NOCONTROL)

/* Array of material layers. */

#define any_cell_materials_defined() (area.materials != NULL)

#define cell_material_defined(m) (area.materials[m] != NULL)

#define material_at(x,y,m) aref(area.materials[m], x, y)

#define set_material_at(x,y,m,v) aset(area.materials[m], x, y, v)

/* Temperature layer. */

#define temperatures_defined() (area.temperature != NULL)

#define temperature_at(x,y) aref(area.temperature, x, y)

#define set_temperature_at(x,y,v) aset(area.temperature, x, y, v)

/* Clouds layer. */

#define clouds_defined() (area.clouds != NULL)

#define raw_cloud_at(x,y) aref(area.clouds, x, y)

#define set_raw_cloud_at(x,y,v) aset(area.clouds, x, y, v)

#define cloud_bottoms_defined() (area.cloudbottoms != NULL)

#define raw_cloud_bottom_at(x,y) aref(area.cloudbottoms, x, y)

#define set_raw_cloud_bottom_at(x,y,v) aset(area.cloudbottoms, x, y, v)

#define cloud_heights_defined() (area.cloudheights != NULL)

#define raw_cloud_height_at(x,y) aref(area.cloudheights, x, y)

#define set_raw_cloud_height_at(x,y,v) aset(area.cloudheights, x, y, v)

/* Winds layer. */

#define winds_defined() (area.winds != NULL)

#define raw_wind_at(x,y) aref(area.winds, x, y)

#define set_raw_wind_at(x,y,v) aset(area.winds, x, y, v)

#define wind_dir(w) ((w) & 0x07)

#define wind_force(w) ((w) >> 3)

#define wind_dir_at(x,y) (wind_dir(raw_wind_at(x, y)))

#define wind_force_at(x,y) (wind_force(raw_wind_at(x, y)))

#define set_wind_at(x,y,d,f) (set_raw_wind_at(x, y, ((f) << 3) | (d)))

#define CALM (0)

/* Handlers for scratch layers. */

#define tmp1_at(x,y) aref(area.tmp1, x, y)

#define set_tmp1_at(x,y,v) aset(area.tmp1, x, y, v)

#define tmp2_at(x,y) aref(area.tmp2, x, y)

#define set_tmp2_at(x,y,v) aset(area.tmp2, x, y, v)

#define tmp3_at(x,y) aref(area.tmp3, x, y)

#define set_tmp3_at(x,y,v) aset(area.tmp3, x, y, v)

/* This little macro implements wraparound in the x direction. */
/* The stupid add of shifted width is for the benefit of brain-damaged
   mod operators that don't handle negative numbers properly. */

#define wrapx(x) (area.xwrap ? (((x) + (area.width << 8)) % area.width) : (x))

/* Constrain y to northern and southern edges. */

#define limity(y) (max(0, min((y), (area.height-1))))

#define interior(y) (max(1, min((y), (area.height-2))))

#define xy_in_dir(x,y,d,nx,ny) \
  (nx) = wrapx((x) + dirx[d]);  (ny) = (y) + diry[dir];

/* in_area and inside_area are very heavily used; any optimization will likely
   speed up Xconq overall. */

/* Test whether x,y is a valid position anywhere in the current area. */

#define in_area(x, y)  \
  (between(0, (y), area.height-1) && x_in_area(x, y))

#define x_in_area(x, y)  \
  (area.xwrap ? TRUE : (between(0, (x), area.width-1) &&  \
			between(area.halfheight,  \
				(x)+(y),  \
				area.width+area.halfheight-1)))

/* This is true if the given x, y position is a valid position for units. */

/* Does x testing work right for even/odd heights? */

#define inside_area(x, y)  \
  (between(1, (y), area.height-2) && x_inside_area(x, y))

#define x_inside_area(x, y)  \
  (area.xwrap ? TRUE : (between(1, (x), area.width-2) &&  \
			between(area.halfheight+1,  \
				(x)+(y),  \
				area.width+area.halfheight-2)))

/* Iteration over all valid cell positions in a area.  These should be
   used carefully, since they don't (can't) have any grouping braces
   embedded. */

#define for_all_cells(x,y)  \
  for (x = 0; x < area.width; ++x)  \
    for (y = 0; y < area.height; ++y)  \
      if (x_in_area(x, y))

/* This doesn't generate positions along area edges.  Typically more
   useful within game. */

#define for_all_interior_cells(x,y)  \
  for (x = 0; x < area.width; ++x)  \
    for (y = 1; y < area.height - 1; ++y)  \
      if (x_inside_area(x, y))

#define for_all_features(feat)  \
  for ((feat) = featurelist; (feat) != NULL; (feat) = (feat)->next)

/* Returns the lighting state of a given position. */
/* (should opencode distance call here) */

#define lighting(x,y,snx,sny)  \
  ((world_distance(x, y, snx, sny) < world.daylight_width) ? 2 : \
   ((world_distance(x, y, snx, sny) < world.twilight_width) ? 1 : 0))

/* True if the given x,y is dark. */

#define night_at(x,y)  \
  (daynight && lighting((x), (y), (int) sunx, (int) suny) == 0)

/* World-related variables. */

extern World world;

extern Area area;

extern int midturnrestore;

extern int numcelltypes;
extern int numbordtypes;
extern int numconntypes;
extern int numcoattypes;

extern int numfeatures;

extern int minelev;
extern int maxelev;
extern int mintemp;
extern int maxtemp;
extern int minwindforce;
extern int maxwindforce;

extern int any_materials_in_terrain;
extern int any_elev_variation;
extern int any_temp_variation;
extern int any_temp_variation_in_layer;
extern int any_wind_variation;
extern int any_wind_variation_in_layer;
extern int any_clouds;

/* World-related functions. */

extern void init_world(void);
extern int set_world_circumference(int circum, int warn);
extern int set_area_shape(int width, int height, int warn);
extern int valid_area_shape(int width, int height, int warn);
extern void check_area_shape(void);
extern void calculate_world_globals(void);
extern void count_terrain_subtypes(void);
extern void final_init_world(void);
extern void compute_elevation_bounds(void);

extern void allocate_area_terrain(void);
extern void allocate_area_aux_terrain(int t);
extern void allocate_area_scratch(int n);
extern void allocate_area_elevations(void);
extern void allocate_area_temperatures(void);
extern void allocate_area_people_sides(void);
extern void allocate_area_control_sides(void);
extern void allocate_area_material(int m);
extern void allocate_area_clouds(void);
extern void allocate_area_cloud_altitudes(void);
extern void allocate_area_cloud_bottoms(void);
extern void allocate_area_cloud_heights(void);
extern void allocate_area_winds(void);

extern int fn_terrain_at(int x, int y);
extern int fn_aux_terrain_at(int x, int y);
extern int fn_feature_at(int x, int y);
extern int fn_elevation_at(int x, int y);
extern int fn_people_side_at(int x, int y);
extern int fn_control_side_at(int x, int y);
extern int fn_material_at(int x, int y);
extern int fn_temperature_at(int x, int y);
extern int fn_raw_cloud_at(int x, int y);
extern int fn_raw_cloud_bottom_at(int x, int y);
extern int fn_raw_cloud_height_at(int x, int y);
extern int fn_raw_wind_at(int x, int y);
extern void allocate_area_users(void);
extern int fn_user_at(int x, int y);
extern void fn_set_user_at(int x, int y, int val);
extern void fn_set_terrain_at(int x, int y, int val);
extern void fn_set_aux_terrain_at(int x, int y, int val);
extern void fn_set_people_side_at(int x, int y, int val);
extern void fn_set_control_side_at(int x, int y, int val);
extern void fn_set_raw_feature_at(int x, int y, int val);
extern void fn_set_elevation_at(int x, int y, int val);
extern void fn_set_material_at(int x, int y, int val);
extern void fn_set_temperature_at(int x, int y, int val);
extern void fn_set_raw_wind_at(int x, int y, int val);
extern void fn_set_raw_cloud_at(int x, int y, int val);
extern void fn_set_raw_cloud_bottom_at(int x, int y, int val);
extern void fn_set_raw_cloud_height_at(int x, int y, int val);

extern void change_terrain_type(int x, int y, int t2);

extern int search_around(int x0, int y0, int maxdist, int (*pred)(int, int),
			 int *rxp, int *ryp, int incr);
extern int search_and_apply(int x0, int y0, int maxdist, int (*pred)(int, int),
			    int *rxp, int *ryp, int incr,
			    void (*fn)(int, int), int num);
extern void apply_to_area(int x0, int y0, int dist, void (*fn)(int, int));
extern void apply_to_area_plus_edge(int x0, int y0, int dist,
				    void (*fn)(int, int));
extern void apply_to_ring(int x0, int y0, int distmin, int distmax,
			  void (*fn)(int, int));
extern void apply_to_hexagon(int x0, int y0, int w2, int h2,
			     void (*fn)(int, int));
extern void apply_to_path(int fx, int fy, int tx, int ty,
			  int (*dirtest)(int x, int y, int dir),
			  int (*dirsort)(int x, int y, int *dirchoices,
					 int numchoices),
			  int (*fn)(int x, int y, int dir, int j,
				    int numchoices),
			  int shortest);
#if 0
int find_path(int fx, int fy, int tx, int ty,
	      int (*chooser)(int, int, int, int, int *),
	      int maxwps, Waypoint *waypoints, int *numwpsp);
#endif
extern int search_straight_line(int x0, int y0, int x1, int y1,
				int (*pred)(int, int), int *rxp, int *ryp);

extern void set_border_at(int x, int y, int dir, int t, int onoff);
extern void set_connection_at(int x, int y, int dir, int t, int onoff);
extern void patch_linear_terrain(int t);

extern void init_features(void);
extern Feature *find_feature(int fid);
extern Feature *feature_at(int x, int y);
extern void compute_all_feature_centroids(void);
extern void compute_feature_centroid(Feature *feature);

extern int point_in_dir(int x, int y, int dir, int *xp, int *yp);
extern int interior_point_in_dir(int x, int y, int dir, int *xp, int *yp);
extern int point_in_dir_n(int x, int y, int dir, int n, int *xp, int *yp);
extern int interior_point_in_dir_n(int x, int y, int dir, int n,
				   int *xp, int *yp);
extern int random_point(int *xp, int *yp);
extern int random_edge_point(int *xp, int *yp);
extern int random_point_near(int cx, int cy, int radius, int *xp, int *yp);
extern int random_point_in_area(int cx, int cy, int rx, int ry,
				int *xp, int *yp);
extern void terrain_subtype_warning(char *context, int t);
extern int approx_dir(int dx, int dy);
extern int hextant(int dx, int dy);
extern int distance(int x1, int y1, int x2, int y2);
extern int world_distance(int x1, int y1, int x2, int y2);
extern int closest_dir(int x, int y);

extern void xy_to_latlong(int x, int y, int xf, int yf, int *latp, int *lonp);
extern int latlong_to_xy(int lat, int lon, int *xp, int *yp,
			 int *xfp, int *yfp);
extern int num_people_at(int x, int y);
