Atrinik Server  4.0
Macros | Functions | Variables
map.c File Reference
#include <global.h>
#include <loader.h>
#include <toolkit/string.h>
#include <arch.h>
#include <player.h>
#include <object.h>
#include <exit.h>
#include <door.h>
#include <check_inv.h>
#include <magic_mirror.h>
#include <object_methods.h>
#include <toolkit/path.h>

Go to the source code of this file.

Macros

#define DEBUG_OLDFLAGS   0
 

Functions

static void load_objects (mapstruct *m, FILE *fp, int mapflags)
 
static void save_objects (mapstruct *m, FILE *fp, FILE *fp2)
 
static void allocate_map (mapstruct *m)
 
static void free_all_objects (mapstruct *m)
 
static void map_debugger (mapstruct *map, char *buf, size_t size)
 
static bool map_validator (mapstruct *map)
 
static void map_constructor (mapstruct *map)
 
static void map_destructor (mapstruct *map)
 
void map_init (void)
 
static mapstructload_and_link_tiled_map (mapstruct *orig_map, int tile_num)
 
static int relative_tile_position_rec (mapstruct *map1, mapstruct *map2, int *x, int *y, int *z, uint32_t id, int level, int flags)
 
static int relative_tile_position (mapstruct *map1, mapstruct *map2, int *x, int *y, int *z, int flags)
 
mapstructhas_been_loaded_sh (shstr *name)
 
char * create_pathname (const char *name)
 
static char * create_items_path (shstr *s)
 
int wall (mapstruct *m, int x, int y)
 
int blocks_view (mapstruct *m, int x, int y)
 
int blocks_magic (mapstruct *m, int x, int y)
 
static bool blocked_self (object *op, mapstruct *m, int x, int y)
 
int blocked (object *op, mapstruct *m, int x, int y, int terrain)
 
int blocked_tile (object *op, mapstruct *m, int x, int y)
 
int arch_blocked (struct archetype *at, object *op, mapstruct *m, int x, int y)
 
void set_map_darkness (mapstruct *m, int value)
 
mapstructget_linked_map (void)
 
mapstructget_empty_map (int sizex, int sizey)
 
void map_set_tile (mapstruct *m, int tile, const char *pathname)
 
mapstructload_original_map (const char *filename, mapstruct *originator, int flags)
 
static mapstructload_temporary_map (mapstruct *m)
 
static void delete_unique_items (mapstruct *m)
 
static void load_unique_objects (mapstruct *m)
 
int new_save_map (mapstruct *m, int flag)
 
void free_map (mapstruct *m, int flag)
 
void delete_map (mapstruct *m)
 
mapstructready_map_name (const char *name, mapstruct *originator, int flags)
 
void clean_tmp_map (mapstruct *m)
 
void free_all_maps (void)
 
void update_position (mapstruct *m, int x, int y)
 
void set_map_reset_time (mapstruct *map)
 
mapstructget_map_from_tiled (mapstruct *m, int tiled)
 
mapstructget_map_from_coord (mapstruct *m, int *x, int *y)
 
mapstructget_map_from_coord2 (mapstruct *m, int *x, int *y)
 
static int coords_distance_to_dir (int x, int y)
 
int get_rangevector (object *op1, object *op2, rv_vector *retval, int flags)
 
int get_rangevector_from_mapcoords (mapstruct *map1, int x, int y, mapstruct *map2, int x2, int y2, rv_vector *retval, int flags)
 
static int on_same_map_exits (mapstruct *map1, mapstruct *map2)
 
int on_same_map (object *op1, object *op2)
 
int players_on_map (mapstruct *m)
 
int wall_blocked (mapstruct *m, int x, int y)
 
int map_get_darkness (mapstruct *m, int x, int y, object **mirror)
 
int map_path_isabs (const char *path)
 
char * map_get_path (mapstruct *m, const char *path, uint8_t unique, const char *name)
 
mapstructmap_force_reset (mapstruct *m)
 
static int map_redraw_internal (mapstruct *tiled, mapstruct *map, int x, int y, int layer, int sub_layer)
 
void map_redraw (mapstruct *m, int x, int y, int layer, int sub_layer)
 
objectmap_find_arch (mapstruct *m, int x, int y, archetype_t *at)
 
objectmap_find_type (mapstruct *m, int x, int y, uint8_t type)
 
int map_free_spot (mapstruct *m, int x, int y, int start, int stop, archetype_t *at, object *op)
 
int map_free_spot_first (mapstruct *m, int x, int y, archetype_t *at, object *op)
 

Variables

int global_darkness_table [MAX_DARKNESS+1]
 
int map_tiled_reverse [TILED_NUM]
 
static int map_tiled_coords [TILED_NUM][3]
 
static mempool_struct * pool_map
 Map structures pool.
 
static uint32_t map_count
 

Detailed Description

Map related functions.

Definition in file map.c.

Function Documentation

static void allocate_map ( mapstruct m)
static

This basically allocates the dynamic array of spaces for the map.

Parameters
mMap to allocate spaces for.

Definition at line 959 of file map.c.

int arch_blocked ( struct archetype at,
object op,
mapstruct m,
int  x,
int  y 
)

Check if an archetype can fit to a position.

Parameters
atArchetype to check.
opObject. If not NULL, will check for terrain as well.
mMap.
xX position.
yY position.
Returns
0 if the space to check is not blocked, return value of blocked() otherwise.

Definition at line 728 of file map.c.

int blocked ( object op,
mapstruct m,
int  x,
int  y,
int  terrain 
)

Check if specified object cannot move onto x, y on the given map and terrain.

Parameters
opObject.
mThe map.
xX coordinate.
yY coordinate.
terrainTerrain type.
Returns
0 if not blocked by anything, combination of Map look flags otherwise.

Definition at line 598 of file map.c.

static bool blocked_self ( object op,
mapstruct m,
int  x,
int  y 
)
static

Check if the specified object would block itself on the specified coordinates. This is required for pathfinding with multi-part monsters; otherwise, what ends up happening is that all the tiles the monster's tails are on will be considered impassable.

Parameters
opObject that is being checked. Cannot be NULL.
mMap position to check. Cannot be NULL.
xX coordinate to check.
yY coordinate to check.
Returns
Whether the object is blocked by itself on the specified square.

Definition at line 556 of file map.c.

int blocked_tile ( object op,
mapstruct m,
int  x,
int  y 
)

This is used for any action which needs to browse through the objects of the tile node, for special objects like inventory checkers.

Parameters
opObject trying to move to map at x, y.
mMap we want to check.
xX position to check for.
yY position to check for.
Returns
1 if the tile is blocked, 0 otherwise.

Definition at line 676 of file map.c.

int blocks_magic ( mapstruct m,
int  x,
int  y 
)

Check if given coordinates on the given map block magic.

Parameters
mMap.
xX position on the map.
yY position on the map.
Returns
1 if the given location blocks magic.

Definition at line 529 of file map.c.

int blocks_view ( mapstruct m,
int  x,
int  y 
)

Check if it's impossible to see through the given coordinate on the given map.

Parameters
mMap.
xX position on the map.
yY position on the map.
Returns
1 if the given location blocks view.

Definition at line 507 of file map.c.

void clean_tmp_map ( mapstruct m)

Remove the temporary file used by the map.

Parameters
mMap.

Definition at line 1662 of file map.c.

static int coords_distance_to_dir ( int  x,
int  y 
)
static

Computes a direction from delta X/Y.

Parameters
xDelta X.
yDelta Y.
Returns
Direction.

Definition at line 2166 of file map.c.

static char* create_items_path ( shstr *  s)
static

This makes absolute path to the itemfile where unique objects will be saved.

Converts '/' to '@'.

Parameters
sPath of the map for the items.
Returns
The absolute path.

Definition at line 445 of file map.c.

char* create_pathname ( const char *  name)

Makes a path absolute outside the world of Atrinik.

In other words, it prepends LIBDIR/MAPDIR/ to the given path and returns the pointer to a static array containing the result.

Parameters
namePath of the map.
Returns
The full path.

Definition at line 422 of file map.c.

void delete_map ( mapstruct m)

Deletes all the data on the map (freeing pointers) and then removes this map from the global linked list of maps.

Parameters
mThe map to delete.

Definition at line 1555 of file map.c.

static void delete_unique_items ( mapstruct m)
static

Goes through a map and removes any unique items on the map.

Parameters
mThe map to go through.

Definition at line 1284 of file map.c.

void free_all_maps ( void  )

Free all allocated maps.

Definition at line 1677 of file map.c.

static void free_all_objects ( mapstruct m)
static

Remove and free all objects in the given map.

Parameters
mThe map.

Definition at line 1467 of file map.c.

void free_map ( mapstruct m,
int  flag 
)

Frees everything allocated by the given map structure.

Don't free tmpname - our caller is left to do that.

Parameters
mMap to free.
flagIf set, free all objects on the map.

Definition at line 1493 of file map.c.

mapstruct* get_empty_map ( int  sizex,
int  sizey 
)

Creates and returns a map of the specified size.

Used in random maps code.

Parameters
sizexX size of the map.
sizeyY size of the map.
Returns
The new map structure.

Definition at line 985 of file map.c.

mapstruct* get_linked_map ( void  )

Allocates, initializes, and returns a pointer to a mapstruct.

Returns
The new map structure.

Definition at line 948 of file map.c.

mapstruct* get_map_from_coord ( mapstruct m,
int *  x,
int *  y 
)

Get real coordinates from map.

Return NULL if no map is valid (coordinates out of bounds and no tiled map), otherwise it returns the map the coordinates are really on, and updates x and y to be the localized coordinates.

Parameters
mMap to consider.
[out]xWill contain the real X position that was checked.
[out]yWill contain the real Y position that was checked.
Returns
Map that is at specified location. Will be NULL if not on any map.

Definition at line 1869 of file map.c.

mapstruct* get_map_from_coord2 ( mapstruct m,
int *  x,
int *  y 
)

Same as get_map_from_coord(), but this version doesn't load tiled maps into memory, if they are not already.

Parameters
mMap to consider.
[out]xWill contain the real X position that was checked. If coordinates are not in a map this is set to 0, or to -1 if there is a tiled map but it's not loaded.
[out]yWill contain the real Y position that was checked.
Returns
Map that is at specified location. Will be NULL if not on any map.

Definition at line 2000 of file map.c.

mapstruct* get_map_from_tiled ( mapstruct m,
int  tiled 
)

Get tiled map, loading it if necessary.

Parameters
mMap.
tiledTile ID to get.
Returns
Tiled map on success, NULL on failure.

Definition at line 1838 of file map.c.

int get_rangevector ( object op1,
object op2,
rv_vector retval,
int  flags 
)

This is used by get_player to determine where the other creature is. get_rangevector takes into account map tiling, so you just can not look the the map coordinates and get the right value. distance_x/y are distance away, which can be negative. direction is the crossfire direction scheme that the creature should head. part is the part of the monster that is closest.

get_rangevector looks at op1 and op2, and fills in the structure for op1 to get to op2. We already trust that the caller has verified that the two objects are at least on adjacent maps. If not, results are not likely to be what is desired. if the objects are not on maps, results are also likely to be unexpected

Flags: 0x1 is don't translate for closest body part. 0x2 is do recursive search on adjacent tiles.

Returns TRUE if successful, or FALSE otherwise.

Todo:
Document.

Definition at line 2238 of file map.c.

int get_rangevector_from_mapcoords ( mapstruct map1,
int  x,
int  y,
mapstruct map2,
int  x2,
int  y2,
rv_vector retval,
int  flags 
)

This is the base for get_rangevector above, but can more generally compute the rangvector between any two points on any maps.

The part field of the rangevector is always set to NULL by this function. (Since we don't actually know about any objects)

If the function fails (because of the maps being separate), it will return FALSE and the vector is not otherwise touched. Otherwise it will return TRUE.

Calculates manhattan distance (dx+dy) per default. (fast)

  • Flags: 0x4 - calculate euclidean (straight line) distance (slow) 0x8 - calculate diagonal (max(dx + dy)) distance (fast) 0x8|0x04 - don't calculate distance (or direction) (fastest)
    Todo:
    Document.

Definition at line 2297 of file map.c.

mapstruct* has_been_loaded_sh ( shstr *  name)

Check whether a specified map has been loaded already. Returns the mapstruct which has a name matching the given argument.

Parameters
nameShared string of the map path name.
Returns
mapstruct which has a name matching the given argument, NULL if no such map.

Definition at line 389 of file map.c.

static mapstruct* load_and_link_tiled_map ( mapstruct orig_map,
int  tile_num 
)
inlinestatic

Try loading the connected map tile with the given number.

Parameters
orig_mapBase map.
tile_numTile number to connect to.
Returns
NULL if loading or tiling fails, loaded neighbor map otherwise.

Definition at line 168 of file map.c.

static void load_objects ( mapstruct m,
FILE *  fp,
int  mapflags 
)
static

Loads (and parses) the objects into a given map from the specified file pointer.

Parameters
mMap being loaded.
fpFile to read from.
mapflagsThe same as we get with load_original_map().

Definition at line 774 of file map.c.

mapstruct* load_original_map ( const char *  filename,
mapstruct originator,
int  flags 
)

Opens the file "filename" and reads information about the map from the given file, and stores it in a newly allocated mapstruct.

Parameters
filenameMap path.
flagsOne of (or combination of):
  • MAP_PLAYER_UNIQUE: We don't do any name changes.
  • MAP_BLOCK: We block on this load. This happens in all cases, no matter if this flag is set or not.
  • MAP_STYLE: Style map - don't add active objects, don't add to server managed map list.
Returns
The loaded map structure, NULL on failure.

Definition at line 1055 of file map.c.

static mapstruct* load_temporary_map ( mapstruct m)
static

Loads a map, which has been loaded earlier, from file.

Parameters
mMap we want to reload.
Returns
The map object we load into (this can change from the passed option if we can't find the original map).

Definition at line 1235 of file map.c.

static void load_unique_objects ( mapstruct m)
static

Loads unique objects from file(s) into the map which is in memory.

Parameters
mThe map to load unique items into.

Definition at line 1318 of file map.c.

static void map_constructor ( mapstruct map)
static

Definition at line 109 of file map.c.

static void map_debugger ( mapstruct map,
char *  buf,
size_t  size 
)
static

Definition at line 89 of file map.c.

static void map_destructor ( mapstruct map)
static

Definition at line 130 of file map.c.

object* map_find_arch ( mapstruct m,
int  x,
int  y,
archetype_t at 
)

Searches for any object with a matching archetype at the given map and coordinates.

Parameters
mMap.
xX coordinate on map.
yY coordinate on map.
atArchetype to look for.
Returns
First matching object, or NULL if none matches.

Definition at line 2856 of file map.c.

object* map_find_type ( mapstruct m,
int  x,
int  y,
uint8_t  type 
)

Searches for any object with a matching type at the given map and coordinates.

Parameters
mMap.
xX coordinate on map.
yY coordinate on map.
typeType to find.
Returns
First matching object, or NULL if none matches.

Definition at line 2891 of file map.c.

mapstruct* map_force_reset ( mapstruct m)

Force the reset of a map, removing any players on it, swapping it out and reloading it.

Parameters
mMap to reset.
Returns
Reset map on success, NULL on failure.

Definition at line 2691 of file map.c.

int map_free_spot ( mapstruct m,
int  x,
int  y,
int  start,
int  stop,
archetype_t at,
object op 
)

Will search for a spot at the given map and coordinates which will be able to contain the given archetype, and returns a random choice among the alternatives found (if any).

'start' and 'stop' specify how many squares to search (see the freearr_x/freearr_y definitions); 1-9 would search the 8 immediately adjacent tiles.

Note
This only checks to see if there is space for the head of the object - if it is a multi-part object, this should be called for all pieces.
Parameters
mMap to search on.
xX coordinate.
yY coordinate.
startStarting index in the freearr_x/freearr_y arrays; at least 0, and less than SIZEOFFREE.
stopLast index in the freearr_x/freearr_y arrays; at least 0, less than SIZEOFFREE and greater than 'start'.
atArchetype to use for blocking checks.
opObject to use for blocking checks; can be NULL.
Returns
An index usable in the freearr_x/freearr_y arrays, -1 if there is no usable spot.

Definition at line 2942 of file map.c.

int map_free_spot_first ( mapstruct m,
int  x,
int  y,
archetype_t at,
object op 
)

Works like map_free_spot(), but it will search maximum possible number of squares (SIZEOFFREE3).

It will return the first available spot, not a random choice.

Parameters
mMap to search on.
xX coordinate.
yY coordinate.
atArchetype to use for blocking checks.
opObject to use for blocking checks; can be NULL.
Returns
An index usable in the freearr_x/freearr_y arrays, -1 if there is no usable spot.

Definition at line 3009 of file map.c.

void map_init ( void  )

Initialize map-related code.

Definition at line 148 of file map.c.

void map_redraw ( mapstruct m,
int  x,
int  y,
int  layer,
int  sub_layer 
)

Force redrawing of all objects on the specified tile, for all players that can see it.

Parameters
mMap.
xX coordinate.
yY coordinate.
layerLayer to redraw, -1 for all.
sub_layerSub-layer to redraw, -1 for all.
Warning
This is a costly function, and should only be called sparingly, and only if it's really necessary. Do not EVER call it from code that is called very often (combat code for example).

Definition at line 2824 of file map.c.

static int map_redraw_internal ( mapstruct tiled,
mapstruct map,
int  x,
int  y,
int  layer,
int  sub_layer 
)
static

Internal function used by map_redraw().

Parameters
tiledTiled map.
mapMap.
xX coordinate.
yY coordinate.
layerLayer to redraw, defaults to all.
sub_layerSub-layer to redraw, defaults to all.
Returns
0.

Definition at line 2758 of file map.c.

static bool map_validator ( mapstruct map)
static

Definition at line 103 of file map.c.

int new_save_map ( mapstruct m,
int  flag 
)

Saves a map to file. If flag is set, it is saved into the same file it was (originally) loaded from. Otherwise a temporary filename will be generated, and the file will be stored there. The temporary filename will be stored in the mapstructure. If the map is unique, we also save to the filename in the map (this should have been updated when first loaded).

Parameters
mThe map to save.
flagSave flag.
Returns

Definition at line 1363 of file map.c.

int on_same_map ( object op1,
object op2 
)

Checks whether two objects are on the same map, taking map tiling into account.

Parameters
op1First object to check for.
op2Second object to check against the first.
Returns
1 if both objects are on same map, 0 otherwise.

Definition at line 2414 of file map.c.

int players_on_map ( mapstruct m)

Count the players on a map, using the local map player list.

Parameters
mThe map.
Returns
Number of players on this map.

Definition at line 2455 of file map.c.

mapstruct* ready_map_name ( const char *  name,
mapstruct originator,
int  flags 
)

Makes sure the given map is loaded and swapped in.

Parameters
namePath name of the map.
flagsPossible flags:
  • MAP_FLUSH: Flush the map - always load from the map directory, and don't do unique items or the like.
    • MAP_PLAYER_UNIQUE: This is an unique map for each player. Don't do any more name translation on it.
Returns
Pointer to the given map.

Definition at line 1584 of file map.c.

static int relative_tile_position ( mapstruct map1,
mapstruct map2,
int *  x,
int *  y,
int *  z,
int  flags 
)
static

Find the distance between two map tiles on a tiled map.

The distance from the topleft (0, 0) corner of map1 to the topleft corner of map2 will be added to x and y.

This function does not work well with asymmetrically tiled maps.

It will also (naturally) perform bad on very large tilesets such as the world map as it may need to load all tiles into memory before finding a path between two tiles.

We probably want to handle the world map as a special case, considering that all tiles are of equal size, and that we might be able to parse their coordinates from their names...

Parameters
map1
map2
x
y
z
Returns
1 if the two tiles are part of the same map, 0 otherwise.

Definition at line 348 of file map.c.

static int relative_tile_position_rec ( mapstruct map1,
mapstruct map2,
int *  x,
int *  y,
int *  z,
uint32_t  id,
int  level,
int  flags 
)
static

Recursive part of the relative_tile_position() function.

Parameters
map1
map2
x
y
z
id
levelRecursion level.
Returns
Todo:
A bidirectional breadth-first search would be more efficient.

Definition at line 219 of file map.c.

static void save_objects ( mapstruct m,
FILE *  fp,
FILE *  fp2 
)
static

This saves all the objects on the map in a non destructive fashion.

Parameters
mMap to save.
fpFile where regular objects are saved.
fp2File to save unique objects.

Definition at line 843 of file map.c.

void set_map_darkness ( mapstruct m,
int  value 
)

Sets darkness for map, both light_value and darkness.

Takes care of checking the passed value.

Parameters
mPointer to the map's structure.
valueThe darkness value.

Definition at line 933 of file map.c.

void set_map_reset_time ( mapstruct map)

Updates the map's timeout.

Parameters
mapMap to update.

Definition at line 1814 of file map.c.

void update_position ( mapstruct m,
int  x,
int  y 
)

This function updates various attributes about a specific space on the map (what it looks like, whether it blocks magic, has a living creatures, prevents people from passing through, etc).

Parameters
mMap to update.
xX position on the given map.
yY position on the given map.

Definition at line 1706 of file map.c.

int wall ( mapstruct m,
int  x,
int  y 
)

Check if there is a wall on specified map at x, y.

Caller should check for P_PASS_THRU in the return value to see if it can cross here.

The P_PLAYER_ONLY flag here is analyzed without checking the caller type. That is possible because player movement related functions should always used blocked().

Parameters
mMap we're checking for.
xX position where to check.
yY position where to check.
Returns
1 if a wall is present at the given location.

Definition at line 486 of file map.c.

int wall_blocked ( mapstruct m,
int  x,
int  y 
)

Returns true if square x, y has P_NO_PASS set, which is true for walls and doors but not monsters.

Parameters
mMap to check for
xX coordinate to check for
yY coordinate to check for
Returns
Non zero if blocked, 0 otherwise.

Definition at line 2479 of file map.c.

Variable Documentation

int global_darkness_table[MAX_DARKNESS+1]
Initial value:
= {
0, 20, 40, 80, 160, 320, 640, 1280
}

Definition at line 43 of file map.c.

int map_tiled_coords[TILED_NUM][3]
static
Initial value:
= {
{0, -1, 0},
{1, 0, 0},
{0, 1, 0},
{-1, 0, 0},
{1, -1, 0},
{1, 1, 0},
{-1, 1, 0},
{-1, -1, 0},
{0, 0, 1},
{0, 0, -1},
}

Definition at line 65 of file map.c.

int map_tiled_reverse[TILED_NUM]
Initial value:
= {
}
#define TILED_EAST
Definition: global.h:189
#define TILED_UP
Definition: global.h:203
#define TILED_SOUTHWEST
Definition: global.h:199
#define TILED_NORTH
Definition: global.h:187
#define TILED_SOUTH
Definition: global.h:191
#define TILED_NORTHEAST
Definition: global.h:195
#define TILED_SOUTHEAST
Definition: global.h:197
#define TILED_DOWN
Definition: global.h:205
#define TILED_NORTHWEST
Definition: global.h:201
#define TILED_WEST
Definition: global.h:193

Used to get the reverse direction for all the tiled maps.

For example: TILED_NORTH -> TILED_SOUTH

Definition at line 52 of file map.c.