1 |
root |
1.17 |
#! perl # mandatory |
2 |
root |
1.1 |
|
3 |
root |
1.26 |
use Coro::AIO; |
4 |
|
|
|
5 |
root |
1.5 |
cf::map->register (qr{^\?random/([0-9a-f]{32})}); |
6 |
root |
1.1 |
|
7 |
|
|
sub init { |
8 |
|
|
my ($self) = @_; |
9 |
|
|
|
10 |
root |
1.5 |
$self->{random_id} = $1; |
11 |
|
|
|
12 |
root |
1.14 |
if (0 < Coro::AIO::aio_load "$cf::RANDOMDIR/$self->{random_id}.meta", my $data) { |
13 |
root |
1.19 |
$self->{random} = cf::decode_json $data; |
14 |
root |
1.3 |
$self->{random}{custom} ||= "$self->{random}{origin_map}+$self->{random}{origin_x}+$self->{random}{origin_y}"; |
15 |
root |
1.7 |
} else { |
16 |
|
|
warn "unable to read meta file for $self->{random_id}\n"; |
17 |
|
|
return 0; |
18 |
root |
1.3 |
} |
19 |
root |
1.5 |
|
20 |
|
|
1 |
21 |
root |
1.1 |
} |
22 |
|
|
|
23 |
root |
1.6 |
sub thawer_merge { |
24 |
|
|
# we have to keep some variables in memory intact |
25 |
|
|
local $_[0]{random_id}; |
26 |
|
|
local $_[0]{random}; |
27 |
|
|
|
28 |
|
|
$_[0]->SUPER::thawer_merge ($_[1]); |
29 |
|
|
} |
30 |
|
|
|
31 |
root |
1.1 |
sub visible_name { |
32 |
|
|
my ($self) = @_; |
33 |
|
|
|
34 |
root |
1.2 |
my $rmp = $self->{random}; |
35 |
root |
1.4 |
"random map at $rmp->{custom}, level $rmp->{dungeon_level}" |
36 |
root |
1.1 |
} |
37 |
|
|
|
38 |
|
|
sub save_path { |
39 |
|
|
my ($self) = @_; |
40 |
|
|
|
41 |
root |
1.14 |
sprintf "%s/%s.map", $cf::RANDOMDIR, $self->{random_id} |
42 |
root |
1.1 |
} |
43 |
|
|
|
44 |
|
|
sub uniq_path { |
45 |
|
|
undef |
46 |
|
|
} |
47 |
|
|
|
48 |
root |
1.8 |
sub load_header_orig { |
49 |
root |
1.1 |
my ($self) = @_; |
50 |
|
|
|
51 |
root |
1.5 |
return unless $self->{random}; |
52 |
|
|
|
53 |
|
|
$self->generate_random_map ($self->{random}); |
54 |
root |
1.15 |
$self->activate; |
55 |
root |
1.5 |
|
56 |
|
|
1 |
57 |
root |
1.1 |
} |
58 |
|
|
|
59 |
root |
1.3 |
sub clean_random_maps { |
60 |
root |
1.14 |
my $files = Coro::AIO::aio_readdir $cf::RANDOMDIR |
61 |
root |
1.10 |
or return; |
62 |
|
|
|
63 |
|
|
my $META_TIMEOUT = $cf::CFG{map_random_meta_timeout} || 86400 * 7; |
64 |
|
|
|
65 |
|
|
for my $file (@$files) { |
66 |
root |
1.16 |
next unless $file =~ /\.meta$/; |
67 |
root |
1.10 |
|
68 |
root |
1.14 |
Coro::AIO::aio_stat "$cf::RANDOMDIR/$file" |
69 |
root |
1.10 |
and next; |
70 |
|
|
|
71 |
|
|
my $age = $cf::NOW - (stat _)[8]; |
72 |
|
|
|
73 |
root |
1.16 |
if ($age > $META_TIMEOUT) { |
74 |
|
|
warn "resetting random meta data for $file"; |
75 |
|
|
IO::AIO::aio_unlink "$cf::RANDOMDIR/$file"; |
76 |
root |
1.3 |
} |
77 |
root |
1.10 |
} |
78 |
root |
1.3 |
} |
79 |
|
|
|
80 |
root |
1.26 |
# called by the random map generator |
81 |
|
|
sub find_style_; |
82 |
|
|
sub 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 |
|
|
|
129 |
|
|
sub 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 |
|
|
|
145 |
root |
1.3 |
# clean up old temp maps regularly |
146 |
root |
1.18 |
our $CLEAN_RANDOM_MAPS = cf::periodic 3600, Coro::unblock_sub { |
147 |
|
|
clean_random_maps; |
148 |
|
|
}; |
149 |
root |
1.3 |
|
150 |
root |
1.20 |
# map generator stresstest, NEVER enable under normal circumstances |
151 |
root |
1.24 |
if (0) { |
152 |
root |
1.11 |
cf::async { |
153 |
|
|
my $seed = 0; |
154 |
|
|
while () { |
155 |
|
|
my $map = cf::map::new; |
156 |
|
|
$map->generate_random_map ({ |
157 |
|
|
region => "scorn", |
158 |
|
|
random_seed => $seed++, |
159 |
root |
1.20 |
xsize => (int rand 90) + 3, |
160 |
|
|
ysize => (int rand 90) + 3, |
161 |
root |
1.11 |
}); |
162 |
root |
1.22 |
# warn sprintf "%d: %d %d\n", $seed, $map->width, $map->height;#d# |
163 |
root |
1.11 |
$map->destroy; |
164 |
|
|
} |
165 |
|
|
}; |
166 |
|
|
} |
167 |
|
|
|
168 |
root |
1.25 |
# prefetch test, load some ocean-maps |
169 |
|
|
if (0) { |
170 |
|
|
cf::async { |
171 |
|
|
# 0.58 |
172 |
|
|
Coro::Timer::sleep 2; |
173 |
|
|
for my $x (200..219) { |
174 |
|
|
for my $y (200..219) { |
175 |
|
|
(cf::map::find "/world/world_$x\_$y")->load; |
176 |
|
|
} |
177 |
|
|
} |
178 |
|
|
}; |
179 |
|
|
} |
180 |
|
|
|
181 |
root |
1.1 |
1 |
182 |
|
|
|