Fixes: #21 Complex method issue in monster.c

At least this is an attempt at a fix
This commit is contained in:
Linus Probert 2018-07-31 21:08:36 +02:00
parent b12e5d4f7a
commit 58e9c7ed13
1 changed files with 38 additions and 31 deletions

View File

@ -245,8 +245,8 @@ monster_drunk_walk(Monster *m, RoomMatrix *rm)
} }
} }
static void static Direction
monster_agressive_walk(Monster *m, RoomMatrix *rm) get_optimal_move_towards(Monster *m, RoomMatrix *rm, const Position *dest)
{ {
int x_dist, y_dist; int x_dist, y_dist;
Position mPos; Position mPos;
@ -261,21 +261,21 @@ monster_agressive_walk(Monster *m, RoomMatrix *rm)
unsigned int nextScore = 0; unsigned int nextScore = 0;
switch (i) { switch (i) {
case UP: case UP:
next.y -= 1; next.y -= 1;
break; break;
case DOWN: case DOWN:
next.y += 1; next.y += 1;
break; break;
case LEFT: case LEFT:
next.x -= 1; next.x -= 1;
break; break;
case RIGHT: case RIGHT:
next.x += 1; next.x += 1;
break; break;
} }
if (position_equals(&next, &rm->playerRoomPos)) { if (position_equals(&next, dest)) {
chosenDirection = (Direction) i; chosenDirection = (Direction) i;
break; break;
} }
@ -283,8 +283,8 @@ monster_agressive_walk(Monster *m, RoomMatrix *rm)
if (!position_in_roommatrix(&next)) if (!position_in_roommatrix(&next))
continue; continue;
x_dist = abs(next.x - rm->playerRoomPos.x); x_dist = abs(next.x - dest->x);
y_dist = abs(next.y - rm->playerRoomPos.y); y_dist = abs(next.y - dest->y);
if (rm->spaces[next.x][next.y].occupied || rm->spaces[next.x][next.y].lethal) { if (rm->spaces[next.x][next.y].occupied || rm->spaces[next.x][next.y].lethal) {
nextScore += 50; nextScore += 50;
@ -293,23 +293,31 @@ monster_agressive_walk(Monster *m, RoomMatrix *rm)
nextScore += x_dist > y_dist ? x_dist : y_dist; nextScore += x_dist > y_dist ? x_dist : y_dist;
if (nextScore < currentScore) { if (nextScore < currentScore) {
currentScore = nextScore; currentScore = nextScore;
chosenDirection = (Direction)i; chosenDirection = (Direction) i;
} }
} }
return chosenDirection;
}
static void
monster_agressive_walk(Monster *m, RoomMatrix *rm)
{
unsigned int chosenDirection = get_optimal_move_towards(m, rm, &rm->playerRoomPos);
switch (chosenDirection) { switch (chosenDirection) {
case UP: case UP:
move(m, rm, VECTOR2D_UP); move(m, rm, VECTOR2D_UP);
break; break;
case DOWN: case DOWN:
move(m, rm, VECTOR2D_DOWN); move(m, rm, VECTOR2D_DOWN);
break; break;
case LEFT: case LEFT:
move(m, rm, VECTOR2D_LEFT); move(m, rm, VECTOR2D_LEFT);
break; break;
case RIGHT: case RIGHT:
move(m, rm, VECTOR2D_RIGHT); move(m, rm, VECTOR2D_RIGHT);
break; break;
} }
} }
@ -324,7 +332,6 @@ monster_coward_walk(Monster *m, RoomMatrix *rm)
x_dist = mPos.x - rm->playerRoomPos.x; x_dist = mPos.x - rm->playerRoomPos.x;
y_dist = mPos.y - rm->playerRoomPos.y; y_dist = mPos.y - rm->playerRoomPos.y;
if (abs(x_dist) > abs(y_dist)) { if (abs(x_dist) > abs(y_dist)) {
if (x_dist > 0) if (x_dist > 0)
move(m, rm, VECTOR2D_RIGHT); move(m, rm, VECTOR2D_RIGHT);