-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38a8e5d
commit aa2e78f
Showing
2 changed files
with
87 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
/******************************************************************* | ||
All inportant functions for ghost on MicroZed | ||
based MZ_APO board designed by Petr Porazil at PiKRON | ||
ghost.c - all important functions for ghost | ||
(C) Copyright 2021 by Lukas Nejezchleb | ||
e-mail: [email protected] | ||
license: any combination of GPL, LGPL, MPL or BSD licenses | ||
*******************************************************************/ | ||
/** | ||
* @file ghost.c | ||
* @author Lukas Nejezchleb ([email protected]) | ||
* @brief Module where all the logic and movement of ghost is placed | ||
* @version 0.1 | ||
* @date 2021-05-04 | ||
* | ||
* @copyright Copyright (c) 2021 | ||
* | ||
*/ | ||
#include "ghost.h" | ||
#include "data_structures.h" | ||
#include "draw_shapes.h" | ||
|
@@ -18,17 +17,6 @@ | |
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
//internal functions | ||
//returns true if pacman can move in given direction | ||
bool ghost_can_move(ghost_type *ghost, int dirx, int diry, map_data *map); | ||
|
||
//returns ammount of directions added to moves_arr | ||
int create_moves(moves_costs_t *moves_arr, ghost_type *ghost, map_data *map, pacman_type *pacman); | ||
|
||
//changes ghosts direction | ||
void change_direction(ghost_type *ghost, int possibilities, moves_costs_t *moves_options); | ||
// end of internal functions | ||
|
||
bool ghost_move(ghost_type *ghost, map_data *map, pacman_type *pacman) | ||
{ | ||
if ((pacman->location.x == ghost->location.x) && (pacman->location.y == ghost->location.y)) | ||
|
@@ -85,7 +73,7 @@ bool ghost_can_move(ghost_type *ghost, int dirx, int diry, map_data *map) | |
|
||
int create_moves(moves_costs_t *moves_arr, ghost_type *ghost, map_data *map, pacman_type *pacman) | ||
{ | ||
//both 4 on next lines is ammount of directions and should not be changed | ||
// both 4 on next lines is ammount of directions and should not be changed | ||
coords dirs[4] = {{.x = 0, .y = -1}, {.x = 0, .y = 1}, {.x = 1, .y = 0}, {.x = -1, .y = 0}}; | ||
int ret = 0; | ||
for (int i = 0; i < 4; ++i) | ||
|
@@ -94,7 +82,7 @@ int create_moves(moves_costs_t *moves_arr, ghost_type *ghost, map_data *map, pac | |
{ | ||
if ((dirs[i].x != -ghost->direction.x) || (dirs[i].y != -ghost->direction.y)) | ||
{ | ||
//add the direction | ||
// add the direction | ||
moves_arr[ret].dir = dirs[i]; | ||
int cost = (pacman->location.x - ghost->location.x - dirs[i].x) * | ||
(pacman->location.x - ghost->location.x - dirs[i].x) + | ||
|
@@ -105,7 +93,7 @@ int create_moves(moves_costs_t *moves_arr, ghost_type *ghost, map_data *map, pac | |
} | ||
} | ||
} | ||
return ret; //return ammount of valid options to move | ||
return ret; // return ammount of valid options to move | ||
} | ||
|
||
void draw_ghost(fb_data *fb, ghost_type *ghost, map_data *map) | ||
|
@@ -137,13 +125,13 @@ void change_direction(ghost_type *ghost, int possibilities, moves_costs_t *moves | |
{ | ||
if (possibilities == 0) | ||
{ | ||
//host reached dead_end | ||
// host reached dead_end | ||
ghost->direction.x = -ghost->direction.x; | ||
ghost->direction.x = -ghost->direction.y; | ||
} | ||
else if (possibilities == 1) | ||
{ | ||
//only one way possible, pick that one | ||
// only one way possible, pick that one | ||
ghost->direction = moves_options[0].dir; | ||
} | ||
else if (ghost->moving_randomly) | ||
|
@@ -152,7 +140,7 @@ void change_direction(ghost_type *ghost, int possibilities, moves_costs_t *moves | |
} | ||
else | ||
{ | ||
//pick direction with smallest cost | ||
// pick direction with smallest cost | ||
int lowest_cost = moves_options[0].cost; | ||
int lowest_index = 0; | ||
for (int i = 1; i < possibilities; ++i) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,87 @@ | ||
/******************************************************************* | ||
All inportant functions for ghost on MicroZed | ||
based MZ_APO board designed by Petr Porazil at PiKRON | ||
ghost.h - all important functions for ghost | ||
(C) Copyright 2021 by Lukas Nejezchleb | ||
e-mail: [email protected] | ||
license: any combination of GPL, LGPL, MPL or BSD licenses | ||
*******************************************************************/ | ||
/** | ||
* @file ghost.h | ||
* @author Lukas Nejezchleb ([email protected]) | ||
* @brief Module where all the logic and movement of ghost is placed | ||
* @version 0.1 | ||
* @date 2021-05-04 | ||
* | ||
* @copyright Copyright (c) 2021 | ||
* | ||
*/ | ||
#ifndef GHOST_H | ||
#define GHOST_H | ||
|
||
#include "data_structures.h" | ||
//internal data structure | ||
|
||
/** | ||
* @brief price of movement | ||
* | ||
*/ | ||
typedef struct | ||
{ | ||
coords dir; | ||
int cost; | ||
} moves_costs_t; | ||
/* | ||
returns ghost structure | ||
*/ | ||
|
||
/** | ||
* @brief Creates the ghost at spawning point at map | ||
* | ||
* @param map | ||
* @param ghost_nr | ||
* @return ghost_type | ||
*/ | ||
ghost_type create_ghost(map_data *map, int ghost_nr); | ||
|
||
/* | ||
draws the ghost | ||
*/ | ||
/** | ||
* @brief Draws the ghost to frame buffer with color indicating its state | ||
* | ||
* @param fb | ||
* @param ghost | ||
* @param map | ||
*/ | ||
void draw_ghost(fb_data *fb, ghost_type *ghost, map_data *map); | ||
|
||
/* | ||
moves the ghost, returns true if pacman was eaten by the ghost | ||
*/ | ||
/** | ||
* @brief Has small chance to change the ghost movement from random to targeting and vice versa. Changes the ghosts direction if there are more possibilities, moves the ghost in the resulting direction. | ||
* | ||
* @param ghost | ||
* @param map | ||
* @param pacman | ||
* @return true | ||
* @return false | ||
*/ | ||
bool ghost_move(ghost_type *ghost, map_data *map, pacman_type *pacman); | ||
|
||
/** | ||
* @brief Returns true if ghost can move in given direction | ||
* | ||
* @param ghost | ||
* @param dirx | ||
* @param diry | ||
* @param map | ||
* @return true | ||
* @return false | ||
*/ | ||
bool ghost_can_move(ghost_type *ghost, int dirx, int diry, map_data *map); | ||
|
||
/** | ||
* @brief Returns number of possible moves, which it has put into the array along with their cost | ||
* | ||
* @param moves_arr | ||
* @param ghost | ||
* @param map | ||
* @param pacman | ||
* @return returns number of possible moves, which it has put into the array along with their cost | ||
*/ | ||
int create_moves(moves_costs_t *moves_arr, ghost_type *ghost, map_data *map, pacman_type *pacman); | ||
|
||
/** | ||
* @brief Changes the direction of ghost to the path that would lead the closest to pacman or random if ghost is moving randomly | ||
* | ||
* @param ghost | ||
* @param possibilities | ||
* @param moves_options | ||
*/ | ||
void change_direction(ghost_type *ghost, int possibilities, moves_costs_t *moves_options); | ||
|
||
#endif |