From c88c64f4b8374f8c5e0e30017b9f87e49253425d Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Mon, 18 Dec 2017 09:59:01 +0100 Subject: [PATCH] Implemented stupid agressive and coward pathing. --- CMakeLists.txt | 1 - TODO.txt | 7 ++-- src/main.c | 1 + src/monster.c | 87 ++++++++++++++++++++++++++++++++++++++++++++---- src/monster.h | 3 ++ src/player.c | 9 ++--- src/roommatrix.c | 1 + src/roommatrix.h | 1 + src/stats.c | 13 +++++--- 9 files changed, 103 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 34d7312..80750a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,7 +47,6 @@ add_executable(breakhack src/stats src/actiontext src/random - src/pathfind ) target_link_libraries(breakhack diff --git a/TODO.txt b/TODO.txt index d1b144d..5da46ba 100644 --- a/TODO.txt +++ b/TODO.txt @@ -10,10 +10,13 @@ x Nicer enemy hits (Text textures, healthbars?) - This could need some love later on. (Misses seem to be broken) (Add hits to stats) x Moving enemies x Stupid roaming enemies - - Smart agressive enemies - - Fleeing enemies + x Smart agressive enemies + x Fleeing enemies + - Add hooks to lua - Lower levels o XP + - Some xp amount logic + - Level threshholds - gui - Items - More gui diff --git a/src/main.c b/src/main.c index d71b841..242f8bc 100644 --- a/src/main.c +++ b/src/main.c @@ -158,6 +158,7 @@ void run() position_to_matrix_coords(&gPlayer->sprite->pos); gRoomMatrix->spaces[rp.x][rp.y].occupied = true; gRoomMatrix->spaces[rp.x][rp.y].player = gPlayer; + gRoomMatrix->playerRoomPos = rp; map_move_monsters(gMap, gRoomMatrix); } diff --git a/src/monster.c b/src/monster.c index d716a44..d11517f 100644 --- a/src/monster.c +++ b/src/monster.c @@ -30,6 +30,7 @@ monster_create(SDL_Renderer *renderer) m->sprite = sprite_create(); m->sprite->clip = (SDL_Rect) { 0, 0, 16, 16 }; m->stats = (Stats) { 11, 1, 0, 0, 1, 1 }; + m->state = AGRESSIVE; monster_load_texts(m, renderer); @@ -55,6 +56,11 @@ has_collided(Monster *monster, RoomMatrix *matrix) Position roomPos = position_to_matrix_coords(&monster->sprite->pos); RoomSpace *space = &matrix->spaces[roomPos.x][roomPos.y]; + + if (space->player) { + stats_fight(&monster->stats, &space->player->stats); + } + return space->occupied; } @@ -102,19 +108,14 @@ move_down(Monster *m, RoomMatrix *rm) return true; } -void -monster_move(Monster *m, RoomMatrix *rm) +static void +monster_drunk_walk(Monster *m, RoomMatrix *rm) { unsigned int i, maxMoveAttempts = 6; - Position monsterRoomPos; if (get_random(2) == 0) return; - monsterRoomPos = position_to_matrix_coords(&m->sprite->pos); - rm->spaces[monsterRoomPos.x][monsterRoomPos.y].occupied = false; - rm->spaces[monsterRoomPos.x][monsterRoomPos.y].monster = NULL; - for (i = 0; i < maxMoveAttempts; ++i) { int move = get_random(3); if (move == 0 && move_left(m, rm)) @@ -126,6 +127,78 @@ monster_move(Monster *m, RoomMatrix *rm) else if (move == 3 && move_down(m, rm)) break; } +} + +static void +monster_agressive_walk(Monster *m, RoomMatrix *rm) +{ + int x_dist, y_dist; + Position mPos; + + mPos = position_to_matrix_coords(&m->sprite->pos); + + x_dist = mPos.x - rm->playerRoomPos.x; + y_dist = mPos.y - rm->playerRoomPos.y; + + + if (abs(x_dist) > abs(y_dist)) { + if (x_dist > 0) + move_left(m, rm); + else + move_right(m, rm); + } else { + if (y_dist > 0) + move_up(m, rm); + else + move_down(m, rm); + } +} + +static void +monster_coward_walk(Monster *m, RoomMatrix *rm) +{ + int x_dist, y_dist; + Position mPos; + + mPos = position_to_matrix_coords(&m->sprite->pos); + + x_dist = mPos.x - rm->playerRoomPos.x; + y_dist = mPos.y - rm->playerRoomPos.y; + + + if (abs(x_dist) > abs(y_dist)) { + if (x_dist > 0) + move_right(m, rm); + else + move_left(m, rm); + } else { + if (y_dist > 0) + move_down(m, rm); + else + move_up(m, rm); + } +} + +void +monster_move(Monster *m, RoomMatrix *rm) +{ + Position monsterRoomPos; + + monsterRoomPos = position_to_matrix_coords(&m->sprite->pos); + rm->spaces[monsterRoomPos.x][monsterRoomPos.y].occupied = false; + rm->spaces[monsterRoomPos.x][monsterRoomPos.y].monster = NULL; + + switch (m->state) { + case PASSIVE: + monster_drunk_walk(m, rm); + break; + case AGRESSIVE: + monster_agressive_walk(m, rm); + break; + case COWARD: + monster_coward_walk(m, rm); + break; + }; monster_update_pos(m, m->sprite->pos); diff --git a/src/monster.h b/src/monster.h index 25a1166..47e9a86 100644 --- a/src/monster.h +++ b/src/monster.h @@ -6,11 +6,14 @@ #include "stats.h" #include "actiontext.h" +typedef enum { PASSIVE, AGRESSIVE, COWARD } State; + typedef struct Monster_t { Sprite *sprite; ActionText *hitText; ActionText *missText; Stats stats; + State state; } Monster; Monster* monster_create(SDL_Renderer*); diff --git a/src/player.c b/src/player.c index 0f761ff..f3128a5 100644 --- a/src/player.c +++ b/src/player.c @@ -31,7 +31,6 @@ has_collided(Player *player, RoomMatrix *matrix) space->monster->hitText->active = true; space->monster->missText->active = false; } else { - // TODO(Linus): The misses seem to be missing space->monster->missText->active = true; space->monster->hitText->active = false; } @@ -48,8 +47,6 @@ has_collided(Player *player, RoomMatrix *matrix) static void player_step(Player *p, RoomMatrix* m) { - Position pos = position_to_matrix_coords(&p->sprite->pos); - m->spaces[pos.x][pos.y].occupied = true; p->total_steps++; p->steps++; } @@ -181,10 +178,10 @@ player_print(Player *p) printf("\n"); printf("--------=== <[ Player Stats ]> ===--------\n"); - printf("HP: %u\n", p->stats.hp); - printf("Level: %u XP: %u\n", p->stats.lvl, p->xp); + printf("HP: %d\n", p->stats.hp); + printf("Level: %d XP: %d\n", p->stats.lvl, p->xp); printf("Pos: %dx%d RoomPos: %dx%d\n", pos.x, pos.y, roomPos.x, roomPos.y); - printf("Steps: %u\n", p->total_steps); + printf("Steps: %d\n", p->total_steps); printf("------------------------------------------\n"); } diff --git a/src/roommatrix.c b/src/roommatrix.c index c5b222c..90298e6 100644 --- a/src/roommatrix.c +++ b/src/roommatrix.c @@ -157,6 +157,7 @@ void roommatrix_reset(RoomMatrix *m) } } m->roomPos = (Position) { 0, 0 }; + m->playerRoomPos = (Position) { 1, 1 }; } void roommatrix_destroy(RoomMatrix *m) diff --git a/src/roommatrix.h b/src/roommatrix.h index 95ec9e6..af7317d 100644 --- a/src/roommatrix.h +++ b/src/roommatrix.h @@ -22,6 +22,7 @@ typedef struct { typedef struct { RoomSpace spaces[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT]; Position roomPos; + Position playerRoomPos; } RoomMatrix; RoomMatrix* roommatrix_create(); diff --git a/src/stats.c b/src/stats.c index 3f02574..66d40c7 100644 --- a/src/stats.c +++ b/src/stats.c @@ -8,7 +8,7 @@ unsigned int stats_fight(Stats *attacker, Stats *defender) { - unsigned int atkRoll, defRoll, dmgRoll; + int atkRoll, defRoll, dmgRoll; bool critical = false; atkRoll = get_random(19) + 1; @@ -18,14 +18,19 @@ stats_fight(Stats *attacker, Stats *defender) defRoll = (get_random(19) + 1) + defender->def; dmgRoll = 0; - //printf("Attacking: %u, Defending: %u\n", atkRoll, defRoll); + printf("\n"); + printf("-----------[ FIGHT ]---------\n"); + printf("Attacking: %d Defending: %d\n", atkRoll, defRoll); + if (atkRoll > defRoll) { - dmgRoll = (rand() % 8) + attacker->dmg; + dmgRoll = (rand() % attacker->dmg) + 1; defender->hp -= dmgRoll; if (critical) defender->hp -= dmgRoll; } - //printf("Attacker hp: %u, Defender hp: %u\n", attacker->hp, defender->hp); + + printf("Attacker hp: %d Defender hp: %d\n", attacker->hp, defender->hp); + printf("-----------------------------\n"); return dmgRoll; }