Rehti MMORPG  1.0.0
Rehti MMORPG is a free and open source MMORPG game.
MapLoader.hpp File Reference
#include "rapidjson/document.h"
#include "rapidjson/prettywriter.h"
#include <exception>
#include <filesystem>
#include <stdlib.h>
#include "RehtiUtils.hpp"
#include "Utils.hpp"
Include dependency graph for MapLoader.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

static const std::vector< std::vector< std::string > > fetchAreaMap ()
 Fetches the area map from the JSON file. Throws an exception if the file corrupted. More...
 
static void loadHeightAndTextureMap (const std::vector< std::vector< std::string >> &areaMap, std::vector< std::vector< int >> &heightMap, std::vector< std::vector< int >> &textureMap)
 Loads a height map and map's texture map into given parameters. Loads them together to avoid reading the same image file twice. More...
 
static void insertObjectTileMap (std::vector< std::vector< std::string >> &objectBlockMap, std::vector< std::vector< std::string >> &objectTileMap, unsigned areaY, unsigned areaX, unsigned currentAreaRowIndex, unsigned currentAreaColumnIndex)
 Inserts the object tile map to the map. More...
 
static void changeBlockDirection (std::vector< std::vector< std::string >> &objectTileMap, unsigned rotation)
 When object tile map is rotated, its direction needs to be rotated as well. More...
 
static const std::vector< std::vector< std::string > > createObjectBlockMap (const std::vector< std::vector< std::string >> &areaMap, GameObjects gameObjects, const std::vector< std::vector< int >> &heightMap)
 Generate the object block map. The map defines how the objects block the tiles around itself. More...
 
static const std::vector< std::vector< unsigned > > generateAccessMap (const std::vector< std::vector< int >> &heightMap, const std::vector< std::vector< std::string >> &objectBlockMap)
 Generates access map from height map & object block map. Access map defines how tiles can or cannot be accessed from it's neighbours. More...
 

Variables

const std::string OBJECT_TILE_MAP_CENTER = "X"
 
const std::string OBJECT_TILE_MAP_NORTH_BLOCK = "N"
 
const std::string OBJECT_TILE_MAP_EAST_BLOCK = "E"
 
const std::string OBJECT_TILE_MAP_SOUTH_BLOCK = "S"
 
const std::string OBJECT_TILE_MAP_WEST_BLOCK = "W"
 
const std::string OBJECT_TILE_MAP_FULL_BLOCK = "B"
 
const std::string OBJECT_TILE_MAP_NO_BLOCK = " "
 
const unsigned NON_OBJECT_ID = 255 * 255
 

Function Documentation

◆ changeBlockDirection()

static void changeBlockDirection ( std::vector< std::vector< std::string >> &  objectTileMap,
unsigned  rotation 
)
static

When object tile map is rotated, its direction needs to be rotated as well.

For example, if the object is rotated 90 degrees clockwise, the south block becomes west block and so on.

Parameters
objectTileMapThe object tile map to be rotated.
rotationThe rotation of the object. 0-4 (North, East, South, West)
Returns
void, but modifies the objectTileMap.
Here is the caller graph for this function:

◆ createObjectBlockMap()

static const std::vector<std::vector<std::string> > createObjectBlockMap ( const std::vector< std::vector< std::string >> &  areaMap,
GameObjects  gameObjects,
const std::vector< std::vector< int >> &  heightMap 
)
static

Generate the object block map. The map defines how the objects block the tiles around itself.

  1. Go through the area map and read the corresponding image file for each area. (<area_name>-obj.png).
  2. For each area read the image file and check if there are any objects in the tile. Object is defined if R * G is not 255 * 255.
  3. If there is an object in the tile, read the object tile map provided by the gameObjects parameter.
  4. Input the object tile map to the object block map. Consider the rotation of the object as well. Rotation is stored in the B-value.

ALSO, stores objects with their locations to generated/objects.json. This is used by the game server to spawn the objects on start.

Parameters
areaMapMatrix of all the area names.
gameObjectsA GameObjects object containing all the object definitions.
heightMapA matrix representing the height of the map.
Returns
const std::vector<std::vector<std::string>>
Here is the call graph for this function:

◆ fetchAreaMap()

static const std::vector<std::vector<std::string> > fetchAreaMap ( )
static

Fetches the area map from the JSON file. Throws an exception if the file corrupted.

Returns
const std::vector<std::vector<std::string>>

◆ generateAccessMap()

static const std::vector<std::vector<unsigned> > generateAccessMap ( const std::vector< std::vector< int >> &  heightMap,
const std::vector< std::vector< std::string >> &  objectBlockMap 
)
static

Generates access map from height map & object block map. Access map defines how tiles can or cannot be accessed from it's neighbours.

Access map is defined as a matrix where each cell contains 4-bit value. 1st bit is the rightmost bit

  1. bit = north
  2. bit = east
  3. bit = south
  4. bit = west.

If bit = 1, the tile is accessible from that direction. Otherwise not.

Access map is constructed from the object block map and the height map as follows:

North is blocked if, the tile is on the top row, height difference with the north tile is > 1 or the tile has a north block (N). East is blocked if, the tile is on the rightmost column, height difference with the east tile is > 1 or the tile has a east block (E). South is blocked if, the tile is on the bottom row, height difference with the south tile is > 1 or the tile has a south block (S). West is blocked if, the tile is on the leftmost column, height difference with the west tile is > 1 or the tile has a west block (W).

Parameters
heightMapA matrix representing the height map.
objectBlockMapA matrix representing the object block map.
Returns
A matrix representing the access map.
Here is the call graph for this function:

◆ insertObjectTileMap()

static void insertObjectTileMap ( std::vector< std::vector< std::string >> &  objectBlockMap,
std::vector< std::vector< std::string >> &  objectTileMap,
unsigned  areaY,
unsigned  areaX,
unsigned  currentAreaRowIndex,
unsigned  currentAreaColumnIndex 
)
static

Inserts the object tile map to the map.

  1. First find the object's center tile.
  2. Insert the object tile map to the object block map.
Parameters
objectBlockMapThe object block map to be modified.
objectTileMapThe object tile map to be inserted.
areaYThe y-coordinate of the area in the area map.
areaXThe x-coordinate of the area in the area map.
currentAreaRowIndexThe y-coordinate of the area in the area map.
currentAreaColumnIndexThe x-coordinate of the area in the area map.
Here is the caller graph for this function:

◆ loadHeightAndTextureMap()

static void loadHeightAndTextureMap ( const std::vector< std::vector< std::string >> &  areaMap,
std::vector< std::vector< int >> &  heightMap,
std::vector< std::vector< int >> &  textureMap 
)
static

Loads a height map and map's texture map into given parameters. Loads them together to avoid reading the same image file twice.

Height map is formed by:

  1. Going through the area map and reading the corresponding image file for each area.
  2. Going through the image file and reading the height of each pixel. Pixels height is calculated by G * B. But the Green values first bit is a sign bit.

Map's texture map is formed by:

  1. Going through the area map and reading the corresponding image file for each area.
  2. Going through the image file and reading the texture id of each pixel. Texture id is the R value.
Parameters
areaMapMatrix of all the area names
heightMapEmpty height matrix to be filled
textureMapEmpty texture map to be filled
Here is the call graph for this function:

Variable Documentation

◆ NON_OBJECT_ID

const unsigned NON_OBJECT_ID = 255 * 255

◆ OBJECT_TILE_MAP_CENTER

const std::string OBJECT_TILE_MAP_CENTER = "X"

◆ OBJECT_TILE_MAP_EAST_BLOCK

const std::string OBJECT_TILE_MAP_EAST_BLOCK = "E"

◆ OBJECT_TILE_MAP_FULL_BLOCK

const std::string OBJECT_TILE_MAP_FULL_BLOCK = "B"

◆ OBJECT_TILE_MAP_NO_BLOCK

const std::string OBJECT_TILE_MAP_NO_BLOCK = " "

◆ OBJECT_TILE_MAP_NORTH_BLOCK

const std::string OBJECT_TILE_MAP_NORTH_BLOCK = "N"

◆ OBJECT_TILE_MAP_SOUTH_BLOCK

const std::string OBJECT_TILE_MAP_SOUTH_BLOCK = "S"

◆ OBJECT_TILE_MAP_WEST_BLOCK

const std::string OBJECT_TILE_MAP_WEST_BLOCK = "W"