ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/map-random.ext
(Generate patch)

Comparing deliantra/server/ext/map-random.ext (file contents):
Revision 1.25 by root, Sat May 3 15:17:13 2008 UTC vs.
Revision 1.26 by root, Sun May 4 14:12:37 2008 UTC

1#! perl # mandatory 1#! perl # mandatory
2
3use Coro::AIO;
2 4
3cf::map->register (qr{^\?random/([0-9a-f]{32})}); 5cf::map->register (qr{^\?random/([0-9a-f]{32})});
4 6
5sub init { 7sub init {
6 my ($self) = @_; 8 my ($self) = @_;
73 IO::AIO::aio_unlink "$cf::RANDOMDIR/$file"; 75 IO::AIO::aio_unlink "$cf::RANDOMDIR/$file";
74 } 76 }
75 } 77 }
76} 78}
77 79
80# called by the random map generator
81sub find_style_;
82sub find_style_($$) {
83 my ($path, $difficulty) = @_;
84
85 my $mapdir = cf::datadir . "/" . cf::mapdir;
86
87 my $map;
88
89 $map = cf::map::find $path
90 unless aio_stat "$mapdir/$path.map";
91
92 unless ($map) {
93 # search files and/or dirs
94 if (my ($dirs, $nondirs) = aio_scandir "$mapdir/$path/", 1) {
95 my @entries = sort grep s/\.map$//, @$nondirs;
96
97 if ($difficulty < 0) {
98 # pick a fully random map, but only a map, do not recurse
99 $map = cf::map::find "$path/$entries[cf::rmg_rndm scalar @entries]"
100 if @entries;
101 } else {
102 # pick a map with nearest difficulty value ("mapname_<difficulty>.map")
103 @entries = sort @$dirs
104 unless @entries;
105
106 my $min_diff = 1e99;
107
108 for my $name (@entries) {
109 if ($name =~ /_(\d+)$/) {
110 my $diff = abs $difficulty - $1 + 0.5; # prefer the more difficult version
111 ($map, $min_diff) = ($name, $diff) if $diff < $min_diff;
112 }
113 }
114
115 unless ($map) {
116 # no map with given pattern found, choose a random map
117 $map = $entries[cf::rmg_rndm scalar @entries];
118 }
119
120 $map = find_style_ "$path/$map", $difficulty
121 if $map;
122 }
123 }
124 }
125
126 $map
127}
128
129sub find_style($$$) {
130 my ($dir, $name, $difficulty) = @_;
131
132 cf::cede_to_tick;
133
134 my $map = find_style_ $name ? "$dir/$name" : $dir, $difficulty;
135
136 if ($map) {
137 $map->load;
138 $map->deactivate;
139 }
140
141 warn "return $dir,$name,$difficulty => $map\n" if $difficulty >= 0;#d#
142 $map
143}
144
78# clean up old temp maps regularly 145# clean up old temp maps regularly
79our $CLEAN_RANDOM_MAPS = cf::periodic 3600, Coro::unblock_sub { 146our $CLEAN_RANDOM_MAPS = cf::periodic 3600, Coro::unblock_sub {
80 clean_random_maps; 147 clean_random_maps;
81}; 148};
82 149

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines