Code: Select all
void
do_dice(P_char ch, char *arg, int cmd)
{
char Gbuf1[MAX_STRING_LENGTH];
char Gbuf2[MAX_STRING_LENGTH];
int rolls, size, result;
if(!SanityCheck(ch, "do_dice"))
return;
if(!*arg) {
send_to_char("You need to specify the dice size!\n ex: 'dice 1 8' - one roll with an eight sided die.\n", ch);
return;
}
half_chop(arg, Gbuf1, Gbuf2);
if(is_number(Gbuf1)) {
rolls = atoi(Gbuf1);
if(rolls < 1 || rolls > 10000) {
send_to_char("Sorry bub, the first parameter is out of range.\n", ch);
return;
}
}
else {
send_to_char("The first parameter needs to be a number. Try again.\n", ch);
return;
}
if(!*Gbuf2) {
send_to_char("You need to specify the dice size as the second parameter.\n", ch);
return;
}
one_argument(Gbuf2, Gbuf1);
if(is_number(Gbuf1)) {
size = atoi(Gbuf1);
if(size < 1 || size > 10000) {
send_to_char("Sorry bub, the second number is out of range.\n", ch);
return;
}
}
else {
send_to_char("The second parameter needs to be a number. Try again.\n", ch);
return;
}
result = venom_roll_dice(rolls, size);
// result = dice(rolls, size);
sprintf(Gbuf1, "You roll a %d sided dice %d times, the total result is: &+B%d&N\n", size, rolls, result);
send_to_char(Gbuf1, ch);
sprintf(Gbuf1, "A %d sided dice is rolled by %s %d times, the total result is: &+B%d&N\n", size, GET_NAME(ch), rolls, result);
send_to_room(Gbuf1, ch->in_room);
return;
}
Code: Select all
/// Dice: dice rolling and random number generation
extern crate rand;
use self::rand::distributions::{IndependentSample, Range};
/// Roll some dice!
#[no_mangle]
pub fn venom_roll_dice(size: i32, num: i32) -> i32 {
let between = Range::new(1, num + 1);
let mut rng = rand::thread_rng();
let mut result = 0;
for _ in 0..size {
result += between.ind_sample(&mut rng);
}
result
}
/// Get a random i32 between a range.
#[no_mangle]
pub fn venom_get_random_i32(min: i32, max: i32) -> i32 {
let between = Range::new(min, max + 1);
let mut rng = rand::thread_rng();
between.ind_sample(&mut rng)
}