Angband
Data Structures | Macros | Typedefs | Enumerations | Functions
z-dice.c File Reference

Represent more complex dice than random_value. More...

#include "z-dice.h"
#include "z-virt.h"
#include "z-util.h"
#include "z-rand.h"
#include "z-expression.h"

Data Structures

struct  dice_expression_entry_s
 
struct  dice_s
 

Macros

#define DICE_MAX_EXPRESSIONS   4
 Hard limit on the number of variables/expressions. More...
 
#define DICE_TOKEN_SIZE   16
 Max size for a token/number to be parsed. More...
 

Typedefs

typedef struct dice_expression_entry_s dice_expression_entry_t
 
typedef enum dice_state_e dice_state_t
 String parser states. More...
 
typedef enum dice_input_e dice_input_t
 Input types for the parser state table. More...
 

Enumerations

enum  dice_state_e {
  DICE_STATE_START, DICE_STATE_BASE_DIGIT, DICE_STATE_FLUSH_BASE, DICE_STATE_DICE_DIGIT,
  DICE_STATE_FLUSH_DICE, DICE_STATE_SIDE_DIGIT, DICE_STATE_FLUSH_SIDE, DICE_STATE_BONUS,
  DICE_STATE_BONUS_DIGIT, DICE_STATE_FLUSH_BONUS, DICE_STATE_VAR, DICE_STATE_VAR_CHAR,
  DICE_STATE_FLUSH_ALL, DICE_STATE_MAX
}
 String parser states. More...
 
enum  dice_input_e {
  DICE_INPUT_AMP, DICE_INPUT_MINUS, DICE_INPUT_BASE, DICE_INPUT_DICE,
  DICE_INPUT_BONUS, DICE_INPUT_VAR, DICE_INPUT_DIGIT, DICE_INPUT_UPPER,
  DICE_INPUT_NULL, DICE_INPUT_MAX
}
 Input types for the parser state table. More...
 

Functions

static dice_input_t dice_input_for_char (char c)
 Return the appropriate input type based on the given character. More...
 
static dice_state_t dice_parse_state_transition (dice_state_t state, dice_input_t input)
 Perform a state transition for the given state and input. More...
 
static void dice_reset (dice_t *dice)
 Zero out the internal state of the dice object. More...
 
dice_tdice_new (void)
 Allocate and initialize a new dice object. More...
 
void dice_free (dice_t *dice)
 Deallocate a dice object. More...
 
static int dice_add_variable (dice_t *dice, const char *name)
 Add an entry to the dice object's symbol list. More...
 
int dice_bind_expression (dice_t *dice, const char *name, const expression_t *expression)
 Bind an expression to a variable name. More...
 
bool dice_parse_string (dice_t *dice, const char *string)
 Parse a formatted string for values and variables to represent a dice roll. More...
 
void dice_random_value (dice_t *dice, random_value *v)
 Extract a random_value by evaluating any bound expressions. More...
 
int dice_evaluate (dice_t *dice, int level, aspect aspect, random_value *v)
 Fully evaluates the dice object, using randcalc(). More...
 
int dice_roll (dice_t *dice, random_value *v)
 Evaluates the dice object, using damroll() (base + XdY). More...
 
bool dice_test_values (dice_t *dice, int base, int dice_count, int sides, int bonus)
 Test the dice object against the given values. More...
 
bool dice_test_variables (dice_t *dice, const char *base, const char *dice_name, const char *sides, const char *bonus)
 Check that the dice object has the given variables for the component. More...
 

Detailed Description

Represent more complex dice than random_value.

Copyright (c) 2013 Ben Semmler

This work is free software; you can redistribute it and/or modify it under the terms of either:

a) the GNU General Public License as published by the Free Software Foundation, version 2, or

b) the "Angband licence": This software may be copied and distributed for educational, research, and not for profit purposes provided that this copyright and statement are included in all such copies. Other copyrights may also apply.

Macro Definition Documentation

◆ DICE_MAX_EXPRESSIONS

#define DICE_MAX_EXPRESSIONS   4

Hard limit on the number of variables/expressions.

Shouldn't need more than the possible values.

Referenced by dice_add_variable(), dice_bind_expression(), and dice_reset().

◆ DICE_TOKEN_SIZE

#define DICE_TOKEN_SIZE   16

Max size for a token/number to be parsed.

Longer strings will be truncated.

Referenced by dice_parse_string().

Typedef Documentation

◆ dice_expression_entry_t

◆ dice_input_t

typedef enum dice_input_e dice_input_t

Input types for the parser state table.

◆ dice_state_t

typedef enum dice_state_e dice_state_t

String parser states.

Enumeration Type Documentation

◆ dice_input_e

Input types for the parser state table.

Enumerator
DICE_INPUT_AMP 
DICE_INPUT_MINUS 
DICE_INPUT_BASE 
DICE_INPUT_DICE 
DICE_INPUT_BONUS 
DICE_INPUT_VAR 
DICE_INPUT_DIGIT 
DICE_INPUT_UPPER 
DICE_INPUT_NULL 
DICE_INPUT_MAX 

◆ dice_state_e

String parser states.

Enumerator
DICE_STATE_START 
DICE_STATE_BASE_DIGIT 
DICE_STATE_FLUSH_BASE 
DICE_STATE_DICE_DIGIT 
DICE_STATE_FLUSH_DICE 
DICE_STATE_SIDE_DIGIT 
DICE_STATE_FLUSH_SIDE 
DICE_STATE_BONUS 
DICE_STATE_BONUS_DIGIT 
DICE_STATE_FLUSH_BONUS 
DICE_STATE_VAR 
DICE_STATE_VAR_CHAR 
DICE_STATE_FLUSH_ALL 
DICE_STATE_MAX 

Function Documentation

◆ dice_add_variable()

static int dice_add_variable ( dice_t dice,
const char *  name 
)
static

Add an entry to the dice object's symbol list.

Parameters
diceis the object the variable is being added to.
nameis the name of the variable.
Returns
The index of the variable name (if added or already found), or -1 for error.

References DICE_MAX_EXPRESSIONS, dice_s::expressions, i, mem_zalloc(), my_stricmp(), dice_expression_entry_s::name, NULL, and string_make().

Referenced by dice_parse_string().

◆ dice_bind_expression()

int dice_bind_expression ( dice_t dice,
const char *  name,
const expression_t expression 
)

Bind an expression to a variable name.

This function creates a deep copy of the expression that the dice object owns

Parameters
diceis the object that will use the expression..
nameis the variable that the expression should be bound to.
expressionis the expression to bind.
Returns
The index of the expression or -1 for error.

References DICE_MAX_EXPRESSIONS, dice_expression_entry_s::expression, expression_copy(), dice_s::expressions, i, my_stricmp(), dice_expression_entry_s::name, and NULL.

Referenced by parse_act_expr(), parse_class_expr(), parse_curse_expr(), parse_mon_spell_expr(), parse_object_expr(), parse_object_power_expr(), parse_trap_expr(), parse_trap_expr_xtra(), and test_evaluate().

◆ dice_evaluate()

int dice_evaluate ( dice_t dice,
int  level,
aspect  aspect,
random_value v 
)

Fully evaluates the dice object, using randcalc().

The random_value used is returned if desired.

Parameters
diceis the dice object to evaluate.
levelis the level value that is passed to randcalc().
aspectis the aspect that is passed to randcalc().
vis a pointer used to return the random_value used.

References random::base, random::dice, dice_random_value(), random::m_bonus, NULL, randcalc(), and random::sides.

Referenced by run_power_calculation(), and test_evaluate().

◆ dice_free()

void dice_free ( dice_t dice)

◆ dice_input_for_char()

static dice_input_t dice_input_for_char ( char  c)
static

Return the appropriate input type based on the given character.

References DICE_INPUT_AMP, DICE_INPUT_BASE, DICE_INPUT_BONUS, DICE_INPUT_DICE, DICE_INPUT_DIGIT, DICE_INPUT_MAX, DICE_INPUT_MINUS, DICE_INPUT_NULL, DICE_INPUT_UPPER, and DICE_INPUT_VAR.

Referenced by dice_parse_string().

◆ dice_new()

dice_t* dice_new ( void  )

◆ dice_parse_state_transition()

static dice_state_t dice_parse_state_transition ( dice_state_t  state,
dice_input_t  input 
)
static

Perform a state transition for the given state and input.

The state table is contained within this function, using a compact char-based format.

Parameters
stateis the current state.
inputis the input type to transition with.
Returns
The next state for the input, Or DICE_STATE_MAX for an invalid transition.

References DICE_INPUT_MAX, and DICE_STATE_MAX.

Referenced by dice_parse_string().

◆ dice_parse_string()

bool dice_parse_string ( dice_t dice,
const char *  string 
)

Parse a formatted string for values and variables to represent a dice roll.

This function can parse a number of formats in the general style of "1+2d3M4" (base, dice, sides, and bonus). Varibles (to which expressions can be bound) can be subsitituted for numeric values by using an all-uppercase name starting with $. Spaces are ignored, concatenating the strings on either side of the space character. Tokens (numbers and variable names) longer than the maximum will be truncated. The unit test demonstrates the variety of valid strings.

Parameters
diceis the dice object to parse the string into.
stringis the string to be parsed.
Returns
true if parsing was successful, false if not.

References dice_s::b, dice_add_variable(), DICE_INPUT_AMP, DICE_INPUT_BASE, DICE_INPUT_BONUS, DICE_INPUT_DICE, DICE_INPUT_DIGIT, dice_input_for_char(), DICE_INPUT_MAX, DICE_INPUT_MINUS, DICE_INPUT_NULL, DICE_INPUT_UPPER, DICE_INPUT_VAR, dice_parse_state_transition(), dice_reset(), DICE_STATE_BONUS, DICE_STATE_FLUSH_ALL, DICE_STATE_FLUSH_BASE, DICE_STATE_FLUSH_BONUS, DICE_STATE_FLUSH_DICE, DICE_STATE_FLUSH_SIDE, DICE_STATE_MAX, DICE_STATE_VAR, DICE_STATE_VAR_CHAR, DICE_TOKEN_SIZE, dice_s::ex_b, dice_s::ex_m, dice_s::ex_x, dice_s::ex_y, flush(), int, dice_s::m, NULL, value, dice_s::x, and dice_s::y.

Referenced by effect_simple(), grab_rand_value(), parse_act_dice(), parse_class_dice(), parse_curse_dice(), parse_ego_dice(), parse_mon_spell_dice(), parse_object_dice(), parse_object_power_dice(), parse_trap_dice(), parse_trap_dice_xtra(), parse_trap_visibility(), test_evaluate(), test_parse_failure(), and test_parse_success().

◆ dice_random_value()

void dice_random_value ( dice_t dice,
random_value v 
)

Extract a random_value by evaluating any bound expressions.

Parameters
diceis the object to get the random_value from.
vis the random_value to place the values into.

References dice_s::b, random::base, random::dice, dice_s::ex_b, dice_s::ex_m, dice_s::ex_x, dice_s::ex_y, dice_expression_entry_s::expression, expression_evaluate(), dice_s::expressions, dice_s::m, random::m_bonus, NULL, random::sides, dice_s::x, and dice_s::y.

Referenced by dice_evaluate(), dice_roll(), grab_rand_value(), and parse_trap_visibility().

◆ dice_reset()

static void dice_reset ( dice_t dice)
static

Zero out the internal state of the dice object.

This will only deallocate entries in the expressions table; it will not deallocate the table itself.

References dice_s::b, DICE_MAX_EXPRESSIONS, dice_s::ex_b, dice_s::ex_m, dice_s::ex_x, dice_s::ex_y, dice_expression_entry_s::expression, expression_free(), dice_s::expressions, i, dice_s::m, dice_expression_entry_s::name, NULL, string_free(), dice_s::x, and dice_s::y.

Referenced by dice_free(), dice_new(), and dice_parse_string().

◆ dice_roll()

int dice_roll ( dice_t dice,
random_value v 
)

Evaluates the dice object, using damroll() (base + XdY).

The random_value used is returned if desired.

Parameters
diceis the dice object to evaluate.
vis a pointer used to return the random_value used.

References random::base, damroll(), random::dice, dice_random_value(), random::m_bonus, NULL, and random::sides.

Referenced by describe_effect(), effect_do(), nonhp_dam(), spell_append_value_info(), and test_evaluate().

◆ dice_test_values()

bool dice_test_values ( dice_t dice,
int  base,
int  dice_count,
int  sides,
int  bonus 
)

Test the dice object against the given values.

References dice_s::b, dice_s::m, dice_s::x, and dice_s::y.

Referenced by test_parse_success().

◆ dice_test_variables()

bool dice_test_variables ( dice_t dice,
const char *  base,
const char *  dice_name,
const char *  sides,
const char *  bonus 
)

Check that the dice object has the given variables for the component.

References dice_s::b, dice_s::ex_b, dice_s::ex_m, dice_s::ex_x, dice_s::ex_y, dice_s::expressions, dice_s::m, my_stricmp(), dice_expression_entry_s::name, NULL, dice_s::x, and dice_s::y.

Referenced by test_parse_success().