typedef struct bfsqueuestruct BFSDATA; struct bfsqueuestruct { ROOMINDEX_DATA *room;…
- User
typedef struct bfs_queue_struct BFS_DATA; struct bfs_queue_struct { ROOM_INDEX_DATA *room; char dir; int num; BFS_DATA *next; }; static BFS_DATA *queue_head = NULL, *queue_tail = NULL, *room_queue = NULL;
/* Utility macros */ #define MARK(room) ( SET_BIT( (room)->room_flags_3, BFS_MARK ) ) #define UNMARK(room) ( REMOVE_BIT( (room)->room_flags_3, BFS_MARK ) ) #define IS_MARKED(room) ( IS_SET( (room)->room_flags_3, BFS_MARK ) )
#define BFS_ERROR -1 #define BFS_ALREADY_THERE -2 #define BFS_NO_PATH -3
bool valid_edge( EXIT_DATA *pexit ) { if( pexit->to_room && !IS_MARKED( pexit->to_room ) ) return TRUE; else return FALSE; }
void bfs_enqueue( ROOM_INDEX_DATA * room, char dir ) { BFS_DATA *curr;
curr = ( BFS_DATA * ) malloc( sizeof( BFS_DATA ) ); curr->room = room; curr->dir = dir; curr->next = NULL; if( queue_tail ) { queue_tail->next = curr; queue_tail = curr; } else queue_head = queue_tail = curr;}
void bfs_dequeue( void ) { BFS_DATA *curr;
curr = queue_head; if( !( queue_head = queue_head->next ) ) queue_tail = NULL; free( curr );}
void bfs_clear_queue( void ) { while( queue_head ) bfs_dequeue( ); }
void room_enqueue( ROOM_INDEX_DATA *room ) { BFS_DATA *curr;
curr = ( BFS_DATA * ) malloc( sizeof( BFS_DATA ) ); curr->room = room; curr->next = room_queue; room_queue = curr;}
void clean_room_queue( void ) { BFS_DATA *curr, *curr_next;
for( curr = room_queue; curr; curr = curr_next ) { UNMARK( curr->room ); curr_next = curr->next; free( curr ); } room_queue = NULL;}
const char *const short_dir_name[] = { "n", "e", "s", "w", "u", "d" };
int find_first_step( ROOM_INDEX_DATA *src, ROOM_INDEX_DATA *target, int maxdist, bool bBetweenAreas ) { int curr_dir, count, dir; EXIT_DATA *pexit;
if( !src || !target ) { BUG( "Src o target NULL" ); return BFS_ERROR; } if( src == target ) return BFS_ALREADY_THERE; if( !bBetweenAreas && src->area != target->area ) return BFS_NO_PATH; room_enqueue( src ); MARK( src ); // first, enqueue the first steps, saving which direction we're going. for( dir = 0; dir < MAX_DIR; dir++ ) { pexit = src->exit[dir]; if( exit_ok( pexit ) ) if( valid_edge( pexit ) ) { curr_dir = dir; MARK( pexit->to_room ); room_enqueue( pexit->to_room ); bfs_enqueue( pexit->to_room, curr_dir ); } } count = 0; while( queue_head ) { if( ++count > maxdist ) { bfs_clear_queue( ); clean_room_queue( ); return BFS_NO_PATH; } if( queue_head->room == target ) { curr_dir = queue_head->dir; bfs_clear_queue( ); clean_room_queue( ); return curr_dir; } else { for( dir = 0; dir < MAX_DIR; dir++ ) { pexit = queue_head->room->exit[dir]; if( exit_ok( pexit ) ) if( valid_edge( pexit ) ) { curr_dir = dir; MARK( pexit->to_room ); room_enqueue( pexit->to_room ); bfs_enqueue( pexit->to_room, queue_head->dir ); } } bfs_dequeue( ); } } clean_room_queue( ); return BFS_NO_PATH; - Assistant
This code defines a C structure and several functions for performing Breadth-First Search (BFS) in a graph representing an area in a MUD (Multi-User Dungeon) game. The idea is to find the shortest path from one room (represented by a ROOM_INDEX_DATA structure) to another room, given some constraints.
The structure bfs_queue_struct represents an element in the BFS queue and contains information about the current room visited, its direction, a number indicating its order in the BFS traversal, and a pointer to the next element in the BFS queue.
The code defines several utility macros to mark and unmark rooms and check if a room is marked as visited. This is used to avoid visiting the same room multiple times during the search.
The find_first_step function takes a source and target rooms, a maximum distance to search, and a flag indicating whether to search between different areas in the MUD. It returns the first step in the shortest path from the source to the target within the maximum distance allowed, or an error code otherwise. It uses a BFS approach, enqueuing and dequeuing rooms as they are visited, and maintaining a separate queue of marked rooms to clean up later.
- User
can you improve it and make it faster?
- Assistant
While the current implementation uses a BFS approach, which is generally fast and efficient for finding the shortest paths in a graph, there is some room for optimization and code improvement to potentially make it faster or more concise:
- Instead of using a MARK/UNMARK approach to check if a ROOM_INDEX_DATA is visited or not, you can consider using a hash table (e.g., using the room's vnum as the key) to store and check visited rooms.
- User
can you implement this functions? bool is_visited(int vnum); void mark_visited(int vnum); void clear_visited();
- Assistant
Sure! Here's the implementation for the
is_visited,mark_visited, andclear_visitedfunctions using a linked list to store the visited roomvnums:typedef struct bfs_visited_struct BFS_VISITED; struct bfs_visited_struct { int vnum; BFS_VISITED *next; }; static BFS_VISITED *visited_head = NULL; bool is_visited(int vnum) { BFS_VISITED *curr; for (curr = visited_head; curr != NULL; curr = curr->next) { if (curr->vnum == vnum) return TRUE; } return FALSE; } void mark_visited(int vnum) { BFS_VISITED *new_visited; if (!is_visited(vnum)) { new_visited = (BFS_VISITED *)malloc(sizeof(BFS_VISITED)); new_visited->vnum = vnum; new_visited->next = visited_head; visited_head = new_visited; } } void clear_visited() { BFS_VISITED *curr, *next; for (curr = visited_head; curr != NULL; curr = next) { next = curr->next; free(curr); } visited_head = NULL; } - User
- Assistant
The above code implements a function named
distancia_mapato find the shortest path between two rooms in a MUD game based on a maximum distance and other constraints. It defines a new linked list structure calledPASO_DATAto simplify the previous logic of marking and unmarking visited rooms.Functions
nuevo_paso,paso_desenlazar,liberar_paso, andpaso_enlazarare helper functions that manage the PASO_DATA structure.distancia_mapauses these functions to manage the visited rooms and find the path.The implementation uses a static boolean array
visitadato check if a room has been visited or not, instead of marking and unmarking the visited rooms using their flags.The
PASO_DATAstructure simplifies the original implementation; however, it can still be optimized further. Here are some improvements to consider: - User
can you correct it?
- Assistant