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 $map; |
86 |
|
|
|
87 |
|
|
$map = cf::map::find $path |
88 |
root |
1.27 |
unless aio_stat "$cf::MAPDIR/$path.map"; |
89 |
root |
1.26 |
|
90 |
|
|
unless ($map) { |
91 |
|
|
# search files and/or dirs |
92 |
root |
1.27 |
if (my ($dirs, $nondirs) = aio_scandir "$cf::MAPDIR/$path/", 1) { |
93 |
root |
1.26 |
my @entries = sort grep s/\.map$//, @$nondirs; |
94 |
|
|
|
95 |
|
|
if ($difficulty < 0) { |
96 |
|
|
# pick a fully random map, but only a map, do not recurse |
97 |
|
|
$map = cf::map::find "$path/$entries[cf::rmg_rndm scalar @entries]" |
98 |
|
|
if @entries; |
99 |
|
|
} else { |
100 |
|
|
# pick a map with nearest difficulty value ("mapname_<difficulty>.map") |
101 |
|
|
@entries = sort @$dirs |
102 |
|
|
unless @entries; |
103 |
|
|
|
104 |
|
|
my $min_diff = 1e99; |
105 |
|
|
|
106 |
|
|
for my $name (@entries) { |
107 |
|
|
if ($name =~ /_(\d+)$/) { |
108 |
|
|
my $diff = abs $difficulty - $1 + 0.5; # prefer the more difficult version |
109 |
|
|
($map, $min_diff) = ($name, $diff) if $diff < $min_diff; |
110 |
|
|
} |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
unless ($map) { |
114 |
|
|
# no map with given pattern found, choose a random map |
115 |
|
|
$map = $entries[cf::rmg_rndm scalar @entries]; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
$map = find_style_ "$path/$map", $difficulty |
119 |
|
|
if $map; |
120 |
|
|
} |
121 |
|
|
} |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
$map |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
sub find_style($$$) { |
128 |
|
|
my ($dir, $name, $difficulty) = @_; |
129 |
|
|
|
130 |
|
|
cf::cede_to_tick; |
131 |
|
|
|
132 |
|
|
my $map = find_style_ $name ? "$dir/$name" : $dir, $difficulty; |
133 |
|
|
|
134 |
|
|
if ($map) { |
135 |
|
|
$map->load; |
136 |
|
|
$map->deactivate; |
137 |
|
|
} |
138 |
|
|
|
139 |
root |
1.28 |
#warn "return $dir,$name,$difficulty => $map\n" if $difficulty >= 0;#d# |
140 |
root |
1.26 |
$map |
141 |
|
|
} |
142 |
|
|
|
143 |
root |
1.3 |
# clean up old temp maps regularly |
144 |
root |
1.18 |
our $CLEAN_RANDOM_MAPS = cf::periodic 3600, Coro::unblock_sub { |
145 |
|
|
clean_random_maps; |
146 |
|
|
}; |
147 |
root |
1.3 |
|
148 |
root |
1.20 |
# map generator stresstest, NEVER enable under normal circumstances |
149 |
root |
1.29 |
if ($ENV{STRESSTEST}) { |
150 |
root |
1.11 |
cf::async { |
151 |
|
|
my $seed = 0; |
152 |
|
|
while () { |
153 |
|
|
my $map = cf::map::new; |
154 |
|
|
$map->generate_random_map ({ |
155 |
|
|
region => "scorn", |
156 |
|
|
random_seed => $seed++, |
157 |
root |
1.29 |
xsize => (int rand 50) + 3, |
158 |
|
|
ysize => (int rand 50) + 3, |
159 |
root |
1.11 |
}); |
160 |
root |
1.29 |
warn sprintf "%d: %dx%d o# %d\n", $seed, $map->width, $map->height, &cf::object::objects_size;#d# |
161 |
root |
1.11 |
$map->destroy; |
162 |
|
|
} |
163 |
|
|
}; |
164 |
|
|
} |
165 |
|
|
|
166 |
root |
1.25 |
# prefetch test, load some ocean-maps |
167 |
|
|
if (0) { |
168 |
|
|
cf::async { |
169 |
|
|
# 0.58 |
170 |
|
|
Coro::Timer::sleep 2; |
171 |
|
|
for my $x (200..219) { |
172 |
|
|
for my $y (200..219) { |
173 |
|
|
(cf::map::find "/world/world_$x\_$y")->load; |
174 |
|
|
} |
175 |
|
|
} |
176 |
|
|
}; |
177 |
|
|
} |
178 |
|
|
|
179 |
root |
1.1 |
1 |
180 |
|
|
|