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

#include <enemy.h>

Inheritance diagram for Enemy:
Character

Public Member Functions

 Enemy ()
 
 Enemy (int _x, int _y, int _chunk_x, int _chunk_y, int _depth, EnemyType enemy)
 
void run_ai (TilePointerMatrix &surroundings, IntPoint sur_chunk, IntPoint sur_coords, std::vector< Character * > char_list, long delta_ms)
 
bool validate_target ()
 
void aggressive_ai (TilePointerMatrix &, IntPoint sur_chunk, IntPoint sur_coords, std::vector< Character * > char_list, long delta_ms)
 
void passive_ai (TilePointerMatrix &surroundings, IntPoint sur_chunk, IntPoint sur_coords, std::vector< Character * > char_list, long delta_ms)
 
int get_sight ()
 
int get_id ()
 
void dump_matrix (TilePointerMatrix &map, IntPoint tl, IntPoint br)
 
std::vector< IntPointsight_tiles ()
 
- Public Member Functions inherited from Character
 Character ()
 
 Character (std::vector< int > _stats, int _x, int _y, Tile _sprite, MiscType _corpse, int _chunk_x, int _chunk_y, int _depth, int _morality, int _speed)
 
 Character (int _x, int _y, int _chunk_x, int _chunk_y, int _depth)
 
void act (long ms)
 
bool is_alive () const
 
void take_damage (int damage)
 
void attack (Character *_chara)
 
int did_hit (Character *_chara)
 
int damage_dealt (Character *_chara)
 
std::vector< Item * > * get_inventory ()
 
std::vector< Item * > * get_equipment ()
 
void add_item (Item *new_item)
 
void drop_item (Item *item)
 
void drop_item (int item)
 
void destroy_item (Item *item)
 
void equip_item (Item *item)
 
void remove_item (int item)
 
void remove_all ()
 
int get_x ()
 
int get_y ()
 
IntPoint get_coords ()
 
IntPoint get_chunk ()
 
int get_chunk_x ()
 
int get_chunk_y ()
 
Tile get_char ()
 
Itemget_corpse ()
 
int get_depth ()
 
Characterget_target ()
 
int get_max_hp ()
 
int get_cur_hp ()
 
int get_moral ()
 
int get_armor_hit (int body_part, int type)
 
float get_armor_dam (int body_part, int type)
 
void set_x (int _x)
 
void set_y (int _y)
 
void set_chunk (IntPoint _chunk)
 
void set_chunk_x (int _chunk_x)
 
void set_chunk_y (int _chunk_y)
 
void set_depth (int)
 
void set_target (Character *_target)
 
void consume_item (Item *item)
 
int get_stat (int)
 
void set_stat (int stat, int amount)
 
int get_current_stat (int)
 
void set_current_stat (int stat, int amount)
 
void regain_consciousness ()
 
void pass_out ()
 
bool is_conscious ()
 
void reduce_endurance (int factor)
 
void gain_endurance (int factor)
 
bool can_act ()
 

Protected Member Functions

std::vector< Equipment * > generate_equipment (std::vector< EquipType > equipment_list)
 
Weapongenerate_weapon (std::vector< WeaponType >)
 
void move (int x_change, int y_change)
 
IntPoint get_next_step (IntPoint goal, TilePointerMatrix &surroundings, IntPoint cur_coords)
 
int is_in (IntPoint point, std::vector< ATile > list)
 
std::vector< IntPointa_star (IntPoint start, IntPoint goal, TilePointerMatrix &surroundings)
 
int manhattan (IntPoint, IntPoint)
 
int get_smallest_f (std::vector< ATile > &list)
 
IntPoint get_sur_coords (IntPoint sur_chunk, IntPoint sur_coords, IntPoint _chunk, IntPoint _coords)
 
Characterfind_best_target (int target_id, int selectability, std::vector< Character * > enemy_list)
 
Characterpassive_best_target (int target_id, int selectability, std::vector< Character * > enemy_list)
 
IntPoint get_spooked (IntPoint abs_coords, IntPoint target_abs)
 
bool in_sight_range (IntPoint _coords, IntPoint _chunk)
 
bool in_sight (IntPoint _coords, IntPoint _chunk)
 
void turn (IntPoint coords)
 
IntPoint get_fov ()
 

Protected Attributes

int id
 
int sight
 
int direction
 
int view
 
bool spooked
 
IntPoint direction_spooked
 
int time_spooked
 
std::string name
 
- Protected Attributes inherited from Character
int moral
 
std::vector< int > stats
 
std::vector< int > current_stats
 
int x
 
int y
 
std::vector< Item * > inventory
 
std::vector< Item * > equipment
 
int inventory_size
 
Tile sprite
 
Itemcorpse
 
int depth
 
IntPoint chunk
 
Charactertarget
 
bool conscious
 
long timer
 
int speed
 

Private Types

typedef std::vector
< std::vector< Tile * > > 
TilePointerMatrix
 

Detailed Description

The enemy class. The primary antagonists of the game. A class which will randomly spawn and attempt to kill or assist the main character. Right now it's a fairly simple class, but it should become increasingly complex as we go.

Constructor & Destructor Documentation

Enemy::Enemy ( )

The default constructor.

Enemy::Enemy ( int  _x,
int  _y,
int  _chunk_x,
int  _chunk_y,
int  _depth,
EnemyType  enemy 
)

Member Function Documentation

std::vector< IntPoint > Enemy::a_star ( IntPoint  start,
IntPoint  goal,
TilePointerMatrix &  surroundings 
)
protected

A pathfinding algorithm. This calculates the best path between two coordinates on a given array of tiles using the a* algorithm. This is done by keeping track of an "open" and a "closed" list of tiles. The open list represents tiles that could potentially be looked at, and the closed list is a list of tiles that have already been looked at.

The current tile is the tile that is currently being considered, and every tile around it is added to the open list, assuming that those tiles can be moved through and are not on the closed list. Each tile added to the open list is given a g, h, and f score, and the curren tile becomes the parent. G represents the distance from the start point to the tile, h (standing for heuristic) is a measure from the tile to the goal, and f = h + g. If a tile is on the open list, then f is recalculated for the current tile. If it is lower, then the current tile becomes the new parent.

Each loop, the current tile is the one on the open list with the shortest f score. If the goal is on the open list, or the open list is empty, then the algorithm is stopped. See the source for further documentation.

Parameters
startThe starting point for the algorithm.
goalThe place the enemy is trying to get to.
surroundingsThe surroundings within the sight of the enemy.
Returns
A vector containing a list of IntPoints representing the best path, or an empty vector if there is no path.
void Enemy::aggressive_ai ( TilePointerMatrix &  surroundings,
IntPoint  sur_chunk,
IntPoint  sur_coords,
std::vector< Character * >  char_list,
long  delta_ms 
)

The ai for the aggressive enemies. The AI searches for a target (find_best_target()) and moves towards that target. If it is next to the target, it will attack it. If no target is found, it will move randomly.

Parameters
surroundingsThe tiles around the enemy.
sur_chunkThe chunk of the surrounding coords.
sur_coordsThe coordinates of the surrounding tilematrix.
char_listThe list of characters the enemy can see.
delta_msThe change in millisenconds since the last time ai was called.
void Enemy::dump_matrix ( TilePointerMatrix &  map,
IntPoint  tl,
IntPoint  br 
)

A debugging function. Dumps the paths to cout as they are calculated by a-star. Only enable if a-star is not working propery or if you like seeing the a-star algrorithm work. Or if you like massive dumps to cout. Alternatively, you can use it to dump a different matrix.

Parameters
mapThe matrix to dump.
tlThe top left corner of the area to dump.
brThe bottom right corner of the area to dump.
Character * Enemy::find_best_target ( int  target_id,
int  selectability,
std::vector< Character * >  enemy_list 
)
protected

Finds a target for non-passive enemies. Finds the best target to act on by searching for the character with the closest morality to the target_id.

Parameters
target_idThe target morality that the enemy is looking for.
selectabilityThe range that the morality can fall within.
enemy_listThe list of enemies to check.
Returns
The character that is found as the best target. NULL if none is found.
std::vector< Equipment * > Enemy::generate_equipment ( std::vector< EquipType equipment_list)
protected

Generates the equipment from a list of EquipType. Loops through a list of possible equipment that an enemy can have, and the returned list goes into the inventory.

Todo:
Make the enemy equip the items.
Parameters
equipment_listA list of possible equipment that the enemy can have.
Returns
A list of equipment that the enemy does have.
Weapon * Enemy::generate_weapon ( std::vector< WeaponType weapon_list)
protected

Generates the weapon from a list of WeaponType. Loops through a list of possible weapons that an enemy can have, and the returned weapon goes into the inventory.

Todo:

Make the enemy equip the weapon.

Posbbily make this multiple weapons and have it pick the best one.

Parameters
weapon_listA list of possible weapons that the enemy can have.
Returns
The weapon that the enemy has.
IntPoint Enemy::get_fov ( )
protected

Calculates the upper and lower angles (from 1-100) of the field of view.

Returns
An IntPoint containing the upper and lower bounds o the fov as angles.
int Enemy::get_id ( )

Public accessor for the id.

Returns
The member id.
See also
id
IntPoint Enemy::get_next_step ( IntPoint  goal,
TilePointerMatrix &  surroundings,
IntPoint  cur_coords 
)
protected

Determines the best next move to make to reach a goal. This function loods at the current coordinates, the coordinates of the goal, and surroundings, and decides what the next move on the shortest path is to reach that goal.

Parameters
goalTHe coordinates of the goal to reach
surroundingsA matrix of the tiles surrounding the enemy.
cur_coordsThe current coordinates in the surroundings matrix.
Returns
The coordinates of the best next move.
See also
a_star(IntPoint start, IntPoint goal, TilePointerMatrix& surroundings)
int Enemy::get_sight ( )

Public accessor for the sight.

Returns
The member variable sight.
int Enemy::get_smallest_f ( std::vector< ATile > &  list)
protected

Returns the ATile with the smallest f value. A helper fucntion for the a-star algorithm which finds the smallest f-value.

Parameters
Areference to a list of ATiles.
Returns
The index of the smallest f value found in the list.
IntPoint Enemy::get_spooked ( IntPoint  abs_coords,
IntPoint  target_abs 
)
protected

Gets the direction that an enemy should be spooked. Takes in a chunk and coordinates in the direction of the spooker, and calculates the direction to run away from the the spooker.

Parameters
abs_coordsThe absolute coordinates of the spook-e.
target_absThe absolute coordinates of the spooker.
Returns
The direction to run away in.
See also
spooked
IntPoint Enemy::get_sur_coords ( IntPoint  sur_chunk,
IntPoint  sur_coords,
IntPoint  _chunk,
IntPoint  _coords 
)
protected

Converts chunk/coords into the enemies surroundings coordinates. Converts the passed in chunk/coordinates to absolute coordinates, and then subtracts that from the top right of the enemy's line of sight, givng coordinates relative to the enemy's LOS.

Parameters
_chunkThe chunk of the object to be converted.
_coordsThe coords of the object to be converted.
sur_chunkThe chunk of top left corner of the surroundings.
sur_coordsThe coordinates of the top left corner of the surroundings.
Returns
The coordinates of the object where (0, 0) is the top corner of the enemies LOS>
bool Enemy::in_sight ( IntPoint  _coords,
IntPoint  _chunk 
)
protected

Checks to see if the enemy can see something. Looks at a point/chunk to see if it it's within the enemies slice of view. Assumes that the enemy can't see everything, as having the upper and lower bounds being the same will make everything pretty wonky. _coords The coordinates of the target. _chunk The chunk of the target.

Returns
True if the coords/chunk can be seen.
bool Enemy::in_sight_range ( IntPoint  _coords,
IntPoint  _chunk 
)
protected

Checks to see if a point is within the enemy's range of sight. Looks at a point/chunk to see if it it's within the enemies range of vision. _coords The coordinates of the target. _chunk The chunk of the target.

Returns
True if the coords/chunk can be seen.
int Enemy::is_in ( IntPoint  point,
std::vector< ATile list 
)
protected

Determines whether the coords are in the list of Tiles. A helper function for the a_star which determines if a given set of coords already exists in a list of ATiles.

Parameters
pointThe point to check against.
listThe list of ATiles which may possess the point.
Returns
The index of the location of the point in the list, or -1 if it is not in the list.
See also
a_star(IntPoint start, IntPoint goal, TilePointerMatrix& surroundings)
int Enemy::manhattan ( IntPoint  current,
IntPoint  goal 
)
protected

A heuristic to estimate the distance from a point to the goal. A helper function for the a-star algorithm, it calculates the distance as the x_change + y_change between a point and the goal.

Parameters
currentThe current point.
goalThe goal the enemy is trying to reach.
Returns
The evaluated value.
See also
a_star(IntPoint start, IntPoint goal, TilePointerMatrix&surroundings)
void Enemy::move ( int  x_change,
int  y_change 
)
protected

Changes the enemies x and y. This function changes the enemies x and y. It does not check whether or not it is a valid move, that must be done by whatever is calling this function.

Parameters
x_changeThe change in the x direction.
y_changeThe change in the y direction.
See also
x
y
void Enemy::passive_ai ( TilePointerMatrix &  surroundings,
IntPoint  sur_chunk,
IntPoint  sur_coords,
std::vector< Character * >  char_list,
long  delta_ms 
)

The ai for the passive enemies. The AI searches for a target (passive_best_target()) and will run away from any non-passive targets found. As time passes, it has a greater and greater chance of being un-spooked and going back to grazing.

Parameters
surroundingsThe tiles around the enemy.
sur_chunkThe chunk of the surrounding coords.
sur_coordsThe coordinates of the surrounding tilematrix.
char_listThe list of characters the enemy can see.
delta_msThe change in millisenconds since the last time ai was called.
Todo:
Make them find a food source and move toward that.
See also
spooked
Character * Enemy::passive_best_target ( int  target_id,
int  selectability,
std::vector< Character * >  enemy_list 
)
protected

Finds an enemy that isn't pasive. If a character is found that isn't passive, it returns that character. Otherwise it returns NULL.

Parameters
selectabilityThe range that the morality can fall within.
enemy_listThe list of enemies to check.
Returns
The character that is found as the best target. NULL if none is found.
void Enemy::run_ai ( TilePointerMatrix &  surroundings,
IntPoint  sur_chunk,
IntPoint  sur_coords,
std::vector< Character * >  char_list,
long  delta_ms 
)

Wrapper function for calling different types of AI. Depending on the type of enemy, this function will call a different type of AI. It is necessary to pass in the sur_chunk and sur_coords so that the coordinates of the character an be converted into the coords of the surroundnigs tilematrix.

Parameters
surroundingsThe tiles around the enemy.
sur_chunkThe chunk of the surrounding coords.
sur_coordsThe coordinates of the surrounding tilematrix.
char_listThe list of characters the enemy can see.
delta_msThe change in millisenconds since the last time ai was called.
See also
aggressive_ai(TilePointerMatrix surroundings, std::vector<character*> char_list, long delta_ms)
passive_ai(TilePointerMatrix surroundings, std::vector<character*> char_list, long delta_ms)
std::vector< IntPoint > Enemy::sight_tiles ( )

Gets all the coordinates that the enemy can currently see.

Returns
A list of coords the enemy can see.
void Enemy::turn ( IntPoint  coords)
protected

Turns the enemy. coords The difference between the target coords and current coords.

Todo:
Implement a max turn amount.
bool Enemy::validate_target ( )

Ensures that the target is still within the view.

Member Data Documentation

int Enemy::direction
protected

Member variable to hold where the enemy is looking. This is 0-100, where 0 (and 100) are directly right.

IntPoint Enemy::direction_spooked
protected

The direction that the spooked enemy will run in. Once the enemy has been spooked, the direction for it to go will be determined based on what spooked it. direction_spooked will be an IntPoint with the possible values of +/- 1 or 0.

See also
get_spooked(IntPoint abs_coords, IntPoint target_abs)
int Enemy::id
protected

The id of the enemy. Each enemy type has an id, starting at 0, e.g. kobolds id=0. This is to allow accessing things by index based on the id of the enemy.

std::string Enemy::name
protected

A string of the name of the enemy. This is a generic string, as in "kobold," or "rabbit."

int Enemy::sight
protected

Member variable to hold how far the enemy can see.

bool Enemy::spooked
protected

Determines whether or not the enemy will flee. If spooked is true, enemies will begin to flee from anything with a morality that is different from its own.

int Enemy::time_spooked
protected

The number of this enemy's actions which have passed since it was spooked.

int Enemy::view
protected

Member variable to hold the enemies field of view. This is a percentage of the field of view (1-100).


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