1 |
root |
1.1 |
#! perl # mandatory |
2 |
|
|
|
3 |
|
|
# this extension provides "environmental effects", |
4 |
|
|
# meaning mostly background music and region messages right now. |
5 |
|
|
|
6 |
root |
1.3 |
sub parse_facelist($$) { |
7 |
|
|
my ($prefix, $list) = @_; |
8 |
|
|
|
9 |
|
|
return undef |
10 |
|
|
unless defined $list; |
11 |
|
|
|
12 |
root |
1.4 |
#TODO, sort by size (smallest first...) |
13 |
|
|
[ |
14 |
|
|
sort { (cf::face::get_data_size $a) <=> (cf::face::get_data_size $b) } |
15 |
|
|
grep $_, |
16 |
|
|
map +(cf::face::find "$prefix$_"), |
17 |
|
|
split /\s*,\s*/, $list |
18 |
|
|
] |
19 |
root |
1.3 |
} |
20 |
|
|
|
21 |
root |
1.2 |
our %MUSIC_QUEUE; |
22 |
|
|
|
23 |
|
|
our $MUSIC_SCHEDULER = cf::async_ext { |
24 |
|
|
while () { |
25 |
|
|
for (keys %MUSIC_QUEUE) { |
26 |
|
|
delete $MUSIC_QUEUE{$_}; |
27 |
|
|
my $pl = cf::player::find_active $_ |
28 |
|
|
or next; |
29 |
|
|
|
30 |
root |
1.4 |
my $ns = $pl->ns |
31 |
|
|
or next; |
32 |
|
|
|
33 |
|
|
$ns->extcmd |
34 |
|
|
or next; |
35 |
|
|
|
36 |
root |
1.3 |
my $faces; |
37 |
|
|
|
38 |
root |
1.2 |
my $map = $pl->ob->map |
39 |
|
|
or next; |
40 |
|
|
|
41 |
|
|
# 1. update map-specific music info |
42 |
root |
1.3 |
unless (exists $map->{music_faces}) { |
43 |
|
|
$map->{music_faces} = parse_facelist "music/", $map->{music}; |
44 |
|
|
} |
45 |
root |
1.2 |
|
46 |
root |
1.3 |
my $faces = $map->{music_faces}; |
47 |
root |
1.2 |
|
48 |
|
|
# 2. fall back to region if no map-specific music |
49 |
root |
1.3 |
unless ($faces) { |
50 |
|
|
my $rgn = $pl->ob->region |
51 |
|
|
or next; |
52 |
|
|
|
53 |
|
|
unless (exists $rgn->{music_faces}) { |
54 |
|
|
my $par = $rgn; |
55 |
|
|
while () { |
56 |
|
|
last if exists $par->{music}; |
57 |
|
|
$par = $par->parent |
58 |
|
|
or last; |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
$rgn->{music_faces} = parse_facelist "music/", $par->{music}; |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
$faces = $rgn->{music_faces}; |
65 |
root |
1.2 |
} |
66 |
|
|
|
67 |
root |
1.4 |
$faces |
68 |
|
|
or next; |
69 |
|
|
|
70 |
|
|
my $facestr = join ",", @$faces; |
71 |
|
|
$ns->{current_music_faces} ne $facestr |
72 |
|
|
or next; |
73 |
|
|
|
74 |
|
|
$ns->{current_music_faces} = $facestr; |
75 |
|
|
|
76 |
|
|
my $msg = { |
77 |
|
|
faces => $faces, |
78 |
|
|
}; |
79 |
|
|
|
80 |
|
|
$msg->{chksum}{$_} = cf::face::get_chksum $_ |
81 |
|
|
for grep $ns->must_send_face ($_), @$faces; |
82 |
|
|
|
83 |
|
|
$ns->ext_event (music => %$msg); |
84 |
root |
1.3 |
|
85 |
root |
1.2 |
cf::cede_to_tick; |
86 |
|
|
} |
87 |
|
|
Coro::schedule unless %MUSIC_QUEUE; |
88 |
|
|
} |
89 |
|
|
}; |
90 |
|
|
|
91 |
root |
1.1 |
cf::player->attach ( |
92 |
|
|
on_region_change => sub { |
93 |
|
|
my ($pl, $new, $old) = @_; |
94 |
|
|
|
95 |
|
|
$pl->ob->message ("You are now " . $new->longname . ". H<Use whereami for more details.>", $new->longname); |
96 |
root |
1.2 |
|
97 |
|
|
$MUSIC_QUEUE{$pl->ob->name} = undef; |
98 |
|
|
$MUSIC_SCHEDULER->ready; |
99 |
|
|
}, |
100 |
|
|
on_map_change => sub { |
101 |
|
|
my ($pl, $new) = @_; |
102 |
|
|
|
103 |
|
|
$MUSIC_QUEUE{$pl->ob->name} = undef; |
104 |
|
|
$MUSIC_SCHEDULER->ready; |
105 |
root |
1.1 |
}, |
106 |
|
|
); |
107 |
|
|
|
108 |
|
|
|