diff options
author | Bardur Arantsson <bardur@scientician.net> | 2012-05-27 17:54:26 +0200 |
---|---|---|
committer | Bardur Arantsson <bardur@scientician.net> | 2012-05-29 05:38:01 +0200 |
commit | 48310612100205a13b4d743951fbc436d081d011 (patch) | |
tree | ad97e584547176272dfee043bf23232da6d635a5 /src | |
parent | 33f06d33cc2f4cd3d36a9616f7f9e9abc7287080 (diff) |
Lua: Add "dice_type" to support moving spells to C
Diffstat (limited to 'src')
-rw-r--r-- | src/externs.h | 6 | ||||
-rw-r--r-- | src/spells4.c | 93 | ||||
-rw-r--r-- | src/types.h | 10 |
3 files changed, 109 insertions, 0 deletions
diff --git a/src/externs.h b/src/externs.h index e622ea4c..337c503d 100644 --- a/src/externs.h +++ b/src/externs.h @@ -1925,6 +1925,12 @@ void lua_cast_school_spell(s32b spell_idx, bool_ no_cost); void spell_description_add_line(s32b spell_idx, cptr line); void device_allocation_init(device_allocation *device_allocation, byte tval); +void dice_init(dice_type *dice, long base, long num, long sides); +bool_ dice_parse(dice_type *dice, cptr s); +void dice_parse_checked(dice_type *dice, cptr s); +long dice_roll(dice_type *dice); +void dice_print(dice_type *dice, char *buf); + /* range.c */ extern void range_init(range_type *range, s32b min, s32b max); diff --git a/src/spells4.c b/src/spells4.c index e94274a2..1cbc451c 100644 --- a/src/spells4.c +++ b/src/spells4.c @@ -619,3 +619,96 @@ int compare_device_allocation(device_allocation *a, device_allocation *b) } SGLIB_DEFINE_LIST_FUNCTIONS(device_allocation, compare_device_allocation, next); + +void dice_init(dice_type *dice, long base, long num, long sides) +{ + assert(dice != NULL); + + dice->base = base; + dice->num = num; + dice->sides = sides; +} + +bool_ dice_parse(dice_type *dice, cptr s) +{ + long base, num, sides; + + if (sscanf(s, "%ld+%ldd%ld", &base, &num, &sides) == 3) + { + dice_init(dice, base, num, sides); + return TRUE; + } + + if (sscanf(s, "%ld+d%ld", &base, &sides) == 2) + { + dice_init(dice, base, 1, sides); + return TRUE; + } + + if (sscanf(s, "d%ld", &sides) == 1) + { + dice_init(dice, 0, 1, sides); + return TRUE; + } + + if (sscanf(s, "%ldd%ld", &num, &sides) == 2) + { + dice_init(dice, 0, num, sides); + return TRUE; + } + + if (sscanf(s, "%ld", &base) == 1) + { + dice_init(dice, base, 0, 0); + return TRUE; + } + + return FALSE; +} + +void dice_parse_checked(dice_type *dice, cptr s) +{ + bool_ result = dice_parse(dice, s); + if (!result) + { + abort(); + } +} + +long dice_roll(dice_type *dice) +{ + assert(dice != NULL); + return dice->base + damroll(dice->num, dice->sides); +} + +void dice_print(dice_type *dice, char *output) +{ + char buf[16]; + + output[0] = '\0'; + + if (dice->base > 0) + { + sprintf(buf, "%ld", dice->base); + strcat(output, buf); + } + + if ((dice->num > 0) || (dice->sides > 0)) + { + if (dice->base > 0) + { + strcat(output, "+"); + } + + if (dice->num > 1) + { + sprintf(buf, "%ld", dice->num); + strcat(output, buf); + } + + strcat(output, "d"); + + sprintf(buf, "%ld", dice->sides); + strcat(output, buf); + } +} diff --git a/src/types.h b/src/types.h index 23df8af5..796ed024 100644 --- a/src/types.h +++ b/src/types.h @@ -2440,6 +2440,16 @@ struct range_type s32b max; }; +/* + * Dice + */ +typedef struct dice_type dice_type; +struct dice_type +{ + long base; /* Base value to which roll is added. */ + long num; /* Number of dice */ + long sides; /* Sides per dice */ +}; /* * Device allocation for skill |