I searched the net for a map randomizer and didn't come across one... so I put one together. This script basically reads the contents of the maps folder to create a master file..call it what you want. This master file is then randomized and the output is written to mapcycle.txt.
#!/usr/bin/perl #i############################################# # Dave K 1/17/02 # # # # Use this script to randomize a HL map cycle # ############################################### # # This should be the path to your halflife maps directory $mapsdir = "/Path/to/HL/maps/directory/"; # # let's assign our master map list # $master = "/Path/to/place/master/file/"; # # Cycle should be the map list configured for HL, default is mapcycle.txt # $cycle = "/Path/to/mapcycle.txt"; # # we'll need to remove the previously randomized mapcycle and master file # unlink $cycle; unlink $master; # # we need a routine to randomize the values in an array # sub shuffle { my $array = lines; my $i = scalar (@$array); my $j; foreach $item (@$array) { --$i; $j = int rand ($i+1); next if $i == $j; @$array [$i,$j] = @$array[$j,$i]; } return @$array; } # # read the maps dir using the readdir function and create an array # opendir(MAPSDIR, $mapsdir) || die "Can not open file: $1\n"; @maps = readdir(MAPSDIR); closedir(MAPSDIR); foreach $bsp (@maps) { unless ( ($bsp eq ".") || ($bsp eq "..") ) { open (MASTERLIST, ">>$master"); print MASTERLIST "$bsp\n"; close (MASTERLIST); } } # # we've created our master list, next we'll prepare it for randomizing # open MASTERLIST, $master || die "Could not open file: $1\n"; while (<MASTERLIST>) { # # go ahead and push the values in our master file into an array # push (@lines, $_); } # # declare our shuffle array # @reordered = shuffle(@lines); foreach (@reordered) { # # Let's print these reordered values to our map cycle file # open (MAPCYCLE, ">>$cycle"); print MAPCYCLE $_; close (MAPCYCLE); }

Replies are listed 'Best First'.
Re: Half Life map randomizer
by Anonymous Monk on Jan 21, 2002 at 05:51 UTC
    Fairly good, few questions/problems
    1) Why are you making the a "master" file if your goal is to randomize for mapcycle.txt
    2) Put your file open/close statements outside of the loop.
    3) Strict and warnings, but that's just me

    Here's what I'd do:(comments removed)
    #!/usr/bin/perl -w #i############################################# # Dave K 1/17/02 # # # # Use this script to randomize a HL map cycle # ############################################### use strict; my (@maps, @files, @reordered, $bsp); my $mapsdir = "/Path/to/HL/maps/directory/"; my $cycle = "/Path/to/mapcycle.txt"; opendir(MAPSDIR, $mapsdir) || die "Can not open $mapsdir: $!\n"; @files = readdir(MAPSDIR); closedir(MAPSDIR); foreach $bsp (@files) { unless ( ($bsp eq ".") || ($bsp eq "..") ) { push @maps, $bsp; } } @reordered = shuffle(@maps); open (MAPCYCLE, ">$cycle") || die "Can not open file $cycle: $!\n"; foreach (@reordered) { print MAPCYCLE $_; } close (MAPCYCLE); sub shuffle { my $array = @_; my $i = scalar (@$array); my $j; my $item; foreach $item (@$array) { --$i; $j = int rand ($i+1); next if $i == $j; @$array [$i,$j] = @$array[$j,$i]; } return @$array; }