# Provides random maprotation for the server.
#
# Usage example:
# %maps = (
#  ctf2 => { probability => 0.5 },
#  ctf4 => { probability => 0.5 },
#  ctf5 => { probability => 0.3 }
# );
#
# normalize_maps;
#
# The 'probability' hash entries defines relative probability for a map 
# to appear. 'Relative' means that they don't have to be normalized; 
# it is achieved by calling normalize_maps after the definition 
# (and every modification) of %maps.


# Computes real probabilities based on 'probability' keys in %maps
sub normalize_maps {
 my $norm = 0;
 $norm += $_->{'probability'} foreach values %maps;
 $_->{'p'} = $_->{'probability'} / $norm foreach values %maps;
 return $norm;
};

sub list_maps {
 return keys %maps;
};

sub get_next_map {
 my $r = rand;
 my $x = 0;
 foreach (keys %maps) {
  $x += $maps{$_}{'p'};
  return $_ if $x >= $r;
 };
};

sub next_map {
 set_map get_next_map;
};

$hooks{'cyclemap'}{'random maps'} = \&next_map;

$commands{'next_map'} = {
 gid  => $g_admin,
 help => 'Proceed to the next random map',
 man  => 'Usage: next_map',
 sub  => \&next_map,
 vgid => $g_all
};

1;
