Angband
Typedefs | Functions
z-dice.h File Reference

Represent more complex dice than random_value. More...

#include "h-basic.h"
#include "z-rand.h"
#include "z-expression.h"

Go to the source code of this file.

Typedefs

typedef struct dice_s dice_t

Functions

dice_tdice_new (void)
 Allocate and initialize a new dice object.
void dice_free (dice_t *dice)
 Deallocate a dice object.
bool dice_parse_string (dice_t *dice, const char *string)
 Parse a formatted string for values and variables to represent a dice roll.
int dice_bind_expression (dice_t *dice, const char *name, const expression_t *expression)
 Bind an expression to a variable name.
void dice_random_value (dice_t *dice, random_value *v)
 Extract a random_value by evaluating any bound expressions.
int dice_evaluate (dice_t *dice, int level, aspect aspect, random_value *v)
 Fully evaluates the dice object, using randcalc().
int dice_roll (dice_t *dice, random_value *v)
 Evaluates the dice object, using damroll() (base + XdY).
bool dice_test_values (dice_t *dice, int base, int dice_count, int sides, int bonus)
 Test the dice object against the given values.
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.

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.

Typedef Documentation

typedef struct dice_s dice_t

Function Documentation

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(), and dice_expression_entry_s::name.

Referenced by parse_act_expr(), parse_class_expr(), parse_mon_spell_expr(), parse_object_expr(), parse_trap_expr(), and test_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, randcalc(), and random::sides.

Referenced by test_evaluate().

void dice_free ( dice_t dice)
dice_t* dice_new ( void  )
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_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, FALSE, flush(), int, dice_s::m, TRUE, value, dice_s::x, and dice_s::y.

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

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, random::sides, dice_s::x, and dice_s::y.

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

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, and random::sides.

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

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, TRUE, dice_s::x, and dice_s::y.

Referenced by test_parse_success().

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, FALSE, dice_s::m, my_stricmp(), dice_expression_entry_s::name, TRUE, dice_s::x, and dice_s::y.

Referenced by test_parse_success().