Roguelike
 All Classes Namespaces Functions Variables Typedefs Friends Pages
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
Chunk Class Reference

#include <chunk.h>

Public Member Functions

 Chunk ()
 
 Chunk (MapTile tile_type, int _world_row, int _world_col)
 
void init (MapTile tile_type, int _world_row, int _world_col)
 
bool build_chunk_with_dungeons ()
 
void build_land_chunk ()
 
void build_forest_chunk ()
 
void build_water_chunk ()
 
void build_beach_chunk ()
 
IntPoint get_world_loc () const
 
IntPoint get_up_stair (int depth) const
 
IntPoint get_down_stair (int depth) const
 
std::vector< Item * > & get_items (int depth)
 
void remove_item (Item *item, int depth)
 
void add_item (Item *item, int depth)
 
const std::vector< std::vector
< Tile > > & 
get_floor (int depth)
 
Tileget_tile (int depth, int row, int col)
 
void set_tile (int depth, int row, int col, Tile tile)
 
int get_depth () const
 
bool out_of_bounds (int depth, int row, int col) const
 
Spawner get_spawner (int depth)
 
void dungeon_dump (int depth)
 
MapTile get_type ()
 
void serialize ()
 

Private Types

typedef std::vector
< std::vector< Tile > > 
TileMatrix
 

Private Member Functions

bool find_serialized_chunk ()
 
int calculate_file_size (int bytes_per_tile)
 
void deserialize (string file_name)
 

Private Attributes

chunk_meta cm
 
MapTile chunk_type
 
std::vector< ChunkLayerlayers
 

Detailed Description

The Chunk class is the largest data model in the game.

Each Chunk contains a vector of dungeons and an overworld. Chunks are in charge of their own loading method: When a new chunk is constructed or its init() method is called (the first implies the second), it will look in the chunk data folder to see if the chunk at its coordinates has previously been serialized. If not, the chunk controls its own creation, and if so, the chunk calls its deserialize() method, loading it with the appropriate data.

Constructor & Destructor Documentation

Chunk::Chunk ( )

Empty constructor.

Todo:
check if this can be empty.
Chunk::Chunk ( MapTile  tile_type,
int  _world_row,
int  _world_col 
)

Typical use case constructor. Will handle file deserialization by itself: When provided with a location, the function will decide whether or not it should deserialize an offloaded chunk.

Parameters
tile_type- the type of the map tile on which this chunk lies.
_world_col- The row of this chunk's location on the world map.
_world_col- The column of this chunk's location on the world map.
See also
MapTile

Member Function Documentation

void Chunk::add_item ( Item item,
int  depth 
)

Will add the given item to the chunk's item list.

Parameters
item- a pointer to the item to add
depth- the depth at which to add the item.
void Chunk::build_beach_chunk ( )

Builds a chunk with only a sandy overworld.

bool Chunk::build_chunk_with_dungeons ( )
Todo:
this documentation
void Chunk::build_forest_chunk ( )

Builds a chunk with grass, trees, groves, and dungeons.

void Chunk::build_land_chunk ( )

Builds a chunk with dirt, trees, and dungeons. Behaves as the default overworld.

void Chunk::build_water_chunk ( )

Builds a chunk with only a watery overworld.

int Chunk::calculate_file_size ( int  bytes_per_tile)
private

Determines the size of the file that will be created for this chunk.

Returns
integer file size in bytes.
void Chunk::deserialize ( string  file_name)
private

Loads this chunk's information from a file. Essentially does a simple reverse of the serialization process. The "header" bytes, which always must exist in the file, are processed one-by-one and loaded into the class member variables to restore the data. Then, data which may or may not exist (such as stair/spawner locations) is loaded based on header information. Next, the bytes of the dungeon and overworld are loaded in a triple-nested for-loop.

Parameters
file_name- The name of the file from which the chunk will be loaded.
world_row- This chunk's row on the world map.
world_col- This chunk's column on the world map.
Todo:
does this have to be dynamically allocated?

TODO still differentiating between overworld and dungeons here.

void Chunk::dungeon_dump ( int  depth)

Prints a graphical representation of the given layer to stdout.

Parameters
depth- the depth of the layer to print.
bool Chunk::find_serialized_chunk ( )
private

The entry point for deserialization. Will attempt to find an offloaded chunk with the given coordinates. If an offloaded chunk is found in the filesystem, calls the deserialize() function and returns true. Otherwise, returns false.

Parameters
row- The world row of the chunk to find.
col- The world column of the chunk to find.
Returns
whether or not a serialized chunk was found.
See also
deserialize
int Chunk::get_depth ( ) const

Determine how many layers are beneath the overworld in this chunk.

Returns
the depth.
IntPoint Chunk::get_down_stair ( int  depth) const

Returns the location of the down stair in the chunk as an IntPoint.

Parameters
depth- the depth of this chunk.
const std::vector< std::vector< Tile > > & Chunk::get_floor ( int  depth)
Parameters
depth- the depth to get (-1 is the overworld)
Returns
a reference to the TileMatrix of the given floor
vector< Item * > & Chunk::get_items ( int  depth)

Returns a vector of all the items on the ground in this chunk.

Parameters
depth- the depth of this chunk.
Returns
a pointer to a vector of items in the chunk.
Todo:
should this return a reference instead of a pointer?
Spawner Chunk::get_spawner ( int  depth)

Returns the spawner object at the given depth.

Parameters
depth
Returns
the spawner object
Tile * Chunk::get_tile ( int  depth,
int  row,
int  col 
)
Parameters
depth
row
col
Returns
a pointer to the tile at the given location.
MapTile Chunk::get_type ( )
Returns
the type of chunk.
IntPoint Chunk::get_up_stair ( int  depth) const

Returns the location of the up stair in the chunk as an IntPoint.

Parameters
depth- the depth of this chunk.
IntPoint Chunk::get_world_loc ( ) const

Returns an IntPoint representing the chunk's location on the world map.

Todo:
Just use an IntPoint to store world location in general.
Returns
the chunk's location on the world map
void Chunk::init ( MapTile  tile_type,
int  _world_row,
int  _world_col 
)

Resets all important information in the chunk, deserializing as needed.

Parameters
tile_type- The type of the map tile on which this chunk lies.
_world_row- The row of this chunk's location on the world map.
_world_col- The column of this chunk's location on the world map.
See also
deserialize
MapTile
bool Chunk::out_of_bounds ( int  depth,
int  row,
int  col 
) const

Given a point, will determine whether the point is out of bounds.

Parameters
depth
row
col
Returns
true if the given location is out of bounds on the chunk.
void Chunk::remove_item ( Item item,
int  depth 
)

Will remove the given item from the chunk.

Parameters
item- a pointer to the item to remove
depth- the depth at which to remove the item.
void Chunk::serialize ( )

Saves all of the important information for this chunk in a file. The file size is first carefully calculated, then a char array is created with that size and populated with the dungeon data. The file is saved in data/chunk.

Some bitwise arithmetic is used to pack ints and bools together in a single byte. As we get over 128 tile IDs, we will have to change this functionality.

if((cm.depth < 0) || (cm.depth > 10)) { cout<<"CHUNK DEPTH: "<<cm.depth<<endl; }

void Chunk::set_tile ( int  depth,
int  row,
int  col,
Tile  tile 
)

Sets a tile at the given position to the given tile type. If the depth is -1, the tile will be set at the overworld.

Parameters
depth- the depth of this chunk.
row- the row of the tile to set.
col- the column of the tile to set.
tile- the tile type.

Member Data Documentation

MapTile Chunk::chunk_type
private

The type of this chunk, found in defs.h.

See also
MapTile
std::vector<ChunkLayer> Chunk::layers
private

A vector containing the layers of this chunk. Generally this is the main data chokepoint in the Chunk class.


The documentation for this class was generated from the following files: