penguins  1.0.0
utils.c File Reference

Go to the source code of this file.

Functions

bool coords_same (Coords a, Coords b)
 Checks if two Coords pairs are equal. More...
 
const char * strip_prefix (const char *str, const char *prefix)
 Returns a substring with the prefix removed if the given string starts with the prefix, otherwise returns NULL. More...
 
bool parse_number (const char *str, long *result)
 Converts a string into a number, returns false if the string was invalid. More...
 
void * memdup (const void *src, size_t size)
 A shorthand for malloc + memcpy (analogous to strdup). More...
 
static void random_init (void)
 
static int random_range (Rng *rng, int min, int max)
 
Rng init_stdlib_rng (void)
 Returns an RNG implementation based on the rand function from <stdlib.h> (and seeds it with srand). More...
 
uint32_t fnv32_hash (uint32_t state, const void *buf, size_t len)
 Computes the 32-bit FNV-1a hash of a given byte sequence. More...
 

Variables

const Coords DIRECTION_TO_COORDS [DIRECTION_MAX]
 A table that maps Direction variants to relative Coords. More...
 
const Coords NEIGHBOR_TO_COORDS [NEIGHBOR_MAX]
 A table that maps Neighbor variants to relative Coords. More...
 
const Neighbor DIRECTION_TO_NEIGHBOR [DIRECTION_MAX]
 A table that maps Direction variants to corresponding Neighbor variants. More...
 

Function Documentation

◆ coords_same()

◆ strip_prefix()

const char* strip_prefix ( const char *  str,
const char *  prefix 
)

Returns a substring with the prefix removed if the given string starts with the prefix, otherwise returns NULL.

Definition at line 46 of file utils.c.

Referenced by parse_arguments().

◆ parse_number()

bool parse_number ( const char *  str,
long *  result 
)

Converts a string into a number, returns false if the string was invalid.

A wrapper around the strtol function, which, unlike atoi, can reliably tell us whether an error has occurred, whereas atoi simply returns zero, so there is no way to distinguish between an error or the string legitimately containing a zero.

Parameters
[in]str
[out]result
See also
https://en.cppreference.com/w/c/string/byte/strtol

Definition at line 64 of file utils.c.

Referenced by parse_arguments().

◆ memdup()

void* memdup ( const void *  src,
size_t  size 
)

A shorthand for malloc + memcpy (analogous to strdup).

Definition at line 79 of file utils.c.

Referenced by Game::game_clone().

◆ random_init()

static void random_init ( void  )
static

Definition at line 87 of file utils.c.

Referenced by init_stdlib_rng().

◆ random_range()

static int random_range ( Rng rng,
int  min,
int  max 
)
static

Definition at line 105 of file utils.c.

Referenced by init_stdlib_rng().

◆ init_stdlib_rng()

Rng init_stdlib_rng ( void  )

Returns an RNG implementation based on the rand function from <stdlib.h> (and seeds it with srand).

Warning
It is probably not thread-safe though... Then again, it's not like we are using threads in the C program.

Definition at line 123 of file utils.c.

Referenced by run_autonomous_mode(), and run_interactive_mode().

◆ fnv32_hash()

uint32_t fnv32_hash ( uint32_t  state,
const void *  buf,
size_t  len 
)
inline

Computes the 32-bit FNV-1a hash of a given byte sequence.

This is a fast and simple hash function that shouldn't be used for anything serious. The invocations may be chained together to compute the hash of, say, a struct or an array by passing the returned value to the state argument of the next call, however, the first call must use FNV32_INITIAL_STATE as the value of state.

See also
https://en.wikipedia.org/wiki/Fowler-Noll-Vo_hash_function

Definition at line 151 of file utils.h.

Referenced by CanvasPanel::paint_tiles().

Variable Documentation

◆ DIRECTION_TO_COORDS

const Coords DIRECTION_TO_COORDS[DIRECTION_MAX]
Initial value:
= {
[DIRECTION_RIGHT] = { 1, 0 },
[DIRECTION_DOWN] = { 0, 1 },
[DIRECTION_LEFT] = { -1, 0 },
[DIRECTION_UP] = { 0, -1 },
}
@ DIRECTION_DOWN
Definition: utils.h:22
@ DIRECTION_LEFT
Definition: utils.h:23
@ DIRECTION_UP
Definition: utils.h:24
@ DIRECTION_RIGHT
Definition: utils.h:21

A table that maps Direction variants to relative Coords.

Definition at line 19 of file utils.c.

Referenced by BotState::bot_generate_all_moves_list(), BotState::bot_rate_move(), BotState::bot_rate_moves_list(), Game::calculate_penguin_possible_moves(), Game::count_obstructed_directions(), and PlayerMovementController::update_tile_attributes().

◆ NEIGHBOR_TO_COORDS

const Coords NEIGHBOR_TO_COORDS[NEIGHBOR_MAX]
Initial value:
= {
[NEIGHBOR_RIGHT] = { 1, 0 }, [NEIGHBOR_BOTTOM_RIGHT] = { 1, 1 },
[NEIGHBOR_BOTTOM] = { 0, 1 }, [NEIGHBOR_BOTTOM_LEFT] = { -1, 1 },
[NEIGHBOR_LEFT] = { -1, 0 }, [NEIGHBOR_TOP_LEFT] = { -1, -1 },
[NEIGHBOR_TOP] = { 0, -1 }, [NEIGHBOR_TOP_RIGHT] = { 1, -1 },
}
@ NEIGHBOR_TOP_RIGHT
Definition: utils.h:36
@ NEIGHBOR_BOTTOM_RIGHT
Definition: utils.h:30
@ NEIGHBOR_BOTTOM
Definition: utils.h:31
@ NEIGHBOR_TOP_LEFT
Definition: utils.h:34
@ NEIGHBOR_RIGHT
Definition: utils.h:29
@ NEIGHBOR_TOP
Definition: utils.h:35
@ NEIGHBOR_BOTTOM_LEFT
Definition: utils.h:32
@ NEIGHBOR_LEFT
Definition: utils.h:33

A table that maps Neighbor variants to relative Coords.

Definition at line 27 of file utils.c.

Referenced by BotState::bot_quick_junction_check(), and CanvasPanel::on_paint().

◆ DIRECTION_TO_NEIGHBOR

const Neighbor DIRECTION_TO_NEIGHBOR[DIRECTION_MAX]
Initial value:

A table that maps Direction variants to corresponding Neighbor variants.

Definition at line 35 of file utils.c.

Referenced by BotState::bot_quick_junction_check().