LI2 Roguelite
Functions
map.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ncurses.h>
#include "map.h"
#include "cell.h"
#include "player.h"
#include "state.h"
#include "monster.h"
#include "item.h"
#include "combat.h"
#include "inventory.h"
#include "menu.h"
Include dependency graph for map.c:

Functions

MapinitMap (int width, int height)
 Initialize a new map. More...
 
void freeMap (void *p)
 Free the map structure. More...
 
int radius_count (State *s, int row, int col, int radius)
 Counts the number of walls within a given radius around a specific position. More...
 
void generateMap (State *st)
 Generate the map based on algorithm TODO explain more. More...
 
void drawMap (Map *map, int mode)
 Draw the map according to the mode. More...
 
void getPlayerInitialPosition (Map *map, int *x, int *y)
 Get the player initial (3x3 empty space) More...
 
void getRandomCoordinates (Map *map, int *x, int *y)
 Get random coordinates for monsters. More...
 
void calculateDistances (Map *map, int x, int y)
 Calculate the distances from the player to all the cells, also updates the effect and effect_duration and color of the effected cells. Finally updates the is_visible field in the cells. More...
 
void calculateVisionAux (Map *map, int x, int y, int direction, int n)
 Calculates the vision for one line of sight. More...
 
void calculateVision (Map *map, Player *player)
 Calculate the vision of the player and updates the is_visible and was_visible field in the cells. More...
 
void distributeItems (Map *map, Item **items, int nr_items)
 Distribute the items on the map. More...
 
void pickUpItem (State *st)
 Get the item on the player position. More...
 
MonstergetCloserMonster (State *st)
 Get the closer monster to the player. More...
 
MonstergetCloserUnhurtMonster (State *st)
 Get the closer unhurt monster to the player. More...
 

Function Documentation

◆ calculateDistances()

void calculateDistances ( Map map,
int  x,
int  y 
)

Calculate the distances from the player to all the cells, also updates the effect and effect_duration and color of the effected cells. Finally updates the is_visible field in the cells.

The distance is calculated as the maximum between the x and y distance

Parameters
mapMap to calculate
xStarting x coordinate
yStarting y coordinate

◆ calculateVision()

void calculateVision ( Map map,
Player player 
)

Calculate the vision of the player and updates the is_visible and was_visible field in the cells.

Uses the calculateVisionAux() function to calculate the vision for all eight directions

Based on the player vision width calculates the number adjacent lines of sight

Parameters
mapMap to calculate the vision
playerPlayer to calculate the vision

◆ calculateVisionAux()

void calculateVisionAux ( Map map,
int  x,
int  y,
int  direction,
int  n 
)

Calculates the vision for one line of sight.

Recursive function given a starting position, a direction and a distance

Parameters
mapMap to calculate the specific line of sight
xStarting x coordinate
yStarting y coordinate
directionDirection of the line of sight
nDistance of the line of sight

◆ distributeItems()

void distributeItems ( Map map,
Item **  items,
int  nr_items 
)

Distribute the items on the map.

Distribute the items randomly on the map, avoiding the player, monsters and other items

Parameters
mapMap to distribute the items
itemsArray of items to distribute
nr_itemsNumber of items in the array

◆ drawMap()

void drawMap ( Map map,
int  mode 
)

Draw the map according to the mode.

NOTE: This could be improved by only drawing the cells that have changed This could be implemented by storing bool value in a cell, per example: is_changed

Parameters
mapMap to draw
modenormal mode, vision mode or distance mode
Returns
void

◆ freeMap()

void freeMap ( void *  p)

Free the map structure.

Parameters
pa pointer to the map to free
Returns
void

◆ generateMap()

void generateMap ( State st)

Generate the map based on algorithm TODO explain more.

  • Top, bottom, left and right walls
  • Fill the rest with a 40% chance of generating walls

Saves a (int**) map twice

Parameters
stState to generate the map
Returns
void

◆ getCloserMonster()

Monster* getCloserMonster ( State st)

Get the closer monster to the player.

The closer monster is the one with the minimum distance to the player. Return the first monster found if there are more than one with the same distance.

Parameters
stState to get the closer monster
Returns
Monster* A close monster TODO: use for earing monsters and for attacking monsters (menu info)

◆ getCloserUnhurtMonster()

Monster* getCloserUnhurtMonster ( State st)

Get the closer unhurt monster to the player.

The closer monster is the one with the minimum distance to the player. Except if the monster is hurt or too close to the player (2 cells away). This function is used to determine which monster to ear.

Parameters
stState to get the monster
Returns
Monster* A close monster unhurt and 3+ cells away

◆ getPlayerInitialPosition()

void getPlayerInitialPosition ( Map map,
int *  x,
int *  y 
)

Get the player initial (3x3 empty space)

Parameters
mapMap to get the player initial position
xreturns the x coordinate
yreturns the y coordinate
Returns
void

◆ getRandomCoordinates()

void getRandomCoordinates ( Map map,
int *  x,
int *  y 
)

Get random coordinates for monsters.

Parameters
mapMap to get the random coordinates
xreturns the x coordinate
yreturns the y coordinate

◆ initMap()

Map* initMap ( int  width,
int  height 
)

Initialize a new map.

Allocates memory for the map and its cells, items and projectiles arrays.

Parameters
widthMap width
heightMap height
Returns
Map pointer to the new map

◆ pickUpItem()

void pickUpItem ( State st)

Get the item on the player position.

Removes the item from the map, adds it to the player inventory and equips it if there's no equipped item.

Also sends a message to the menu.

Parameters
stThe game state

◆ radius_count()

int radius_count ( State s,
int  row,
int  col,
int  radius 
)

Counts the number of walls within a given radius around a specific position.

Parameters
sState to count the walls
rowRow of the position
colColumn of the position
radiusRadius around the position
Returns
int Number of walls