Roguelike AoC – part 14
In the fourteenth lesson of a roguelike game tutorial a next type of monster move is added – the random one.
Like a staggering drunk
To be honest there was not much to be done in this lesson – an algorithm presented by the author actually works well – it’s just random position to be chosen for a monster to move. Despite the fact that I had to make the code comply with my Location structure nothing original happened. So the code is just self explaining:
int pathfindingRandom(Monster *monster)
{
int random = rand() % 5;
switch (random) {
case 0:
if (mvinch(monster->currentLocation->position.y - 1, monster->currentLocation->position.x) == *FLOOR_SIGN) {
monster->previousLocation = monster->currentLocation;
monster->currentLocation->position.y -= 1;
}
break;
case 1:
if (mvinch(monster->currentLocation->position.y + 1, monster->currentLocation->position.x) == *FLOOR_SIGN) {
monster->previousLocation = monster->currentLocation;
monster->currentLocation->position.y += 1;
}
break;
case 2:
if (mvinch(monster->currentLocation->position.y, monster->currentLocation->position.x - 1) == *FLOOR_SIGN) {
monster->previousLocation = monster->currentLocation;
monster->currentLocation->position.x -= 1;
}
break;
case 3:
if (mvinch(monster->currentLocation->position.y, monster->currentLocation->position.x + 1) == *FLOOR_SIGN) {
monster->previousLocation = monster->currentLocation;
monster->currentLocation->position.x += 1;
}
case 4:
break;
}
return 0;
}
Code
Here is the direct link to the GitHub repo.
Leave a Reply
You must be logged in to post a comment.