1 | #! perl # mandatory |
1 | #! perl # mandatory |
2 | |
2 | |
3 | # this extension provides "environmental effects", |
3 | # this extension provides "environmental effects", |
4 | # meaning mostly background music and region messages right now. |
4 | # meaning mostly background music and region messages right now. |
5 | |
5 | |
|
|
6 | our %MUSIC_GROUP; |
|
|
7 | |
|
|
8 | sub reload { |
|
|
9 | cf::trace "loading music config from $DATADIR/music\n"; |
|
|
10 | |
|
|
11 | 0 < Coro::AIO::aio_load "$DATADIR/music", my $data |
|
|
12 | or die "$DATADIR/music $!"; |
|
|
13 | |
|
|
14 | my $conf = JSON::XS->new->utf8->relaxed->decode ($data); |
|
|
15 | %MUSIC_GROUP = %{ $conf->{group} }; |
|
|
16 | } |
|
|
17 | |
|
|
18 | cf::post_init { |
|
|
19 | $_[0] ? cf::async { reload } : reload |
|
|
20 | }; |
|
|
21 | |
6 | sub parse_facelist($$) { |
22 | sub parse_musiclist($) { |
7 | my ($prefix, $list) = @_; |
23 | my ($list) = @_; |
8 | |
24 | |
9 | return undef |
25 | return undef |
10 | unless defined $list; |
26 | unless defined $list; |
11 | |
27 | |
12 | #TODO, sort by size (smallest first...) |
28 | my @name = split /\s*,\s*/, $list; |
|
|
29 | my @face; |
|
|
30 | |
|
|
31 | while (@name) { |
|
|
32 | my $name = shift @name; |
|
|
33 | |
|
|
34 | if ($name =~ /^\@(.*)$/) { |
|
|
35 | my $group = $MUSIC_GROUP{$1} |
|
|
36 | or cf::error "music group $1 does not exist (referenced in '$list')"; |
|
|
37 | |
|
|
38 | unshift @name, @$group; |
|
|
39 | } else { |
|
|
40 | my $face = cf::face::find "music/$name" |
|
|
41 | or "unable to find music face $name (referenced in '$list')"; |
|
|
42 | |
|
|
43 | push @face, $face |
|
|
44 | if $face; |
|
|
45 | } |
13 | [ |
46 | } |
|
|
47 | |
|
|
48 | # sort by size, smallest first, because it'S the fastest to download... |
14 | sort { (cf::face::get_data_size $a) <=> (cf::face::get_data_size $b) } |
49 | [ sort { (cf::face::get_data_size $a) <=> (cf::face::get_data_size $b) } @face ] |
15 | grep $_, |
|
|
16 | map +(cf::face::find "$prefix$_" or ((warn "unable to find music file $_"), undef)), |
|
|
17 | split /\s*,\s*/, $list |
|
|
18 | ] |
|
|
19 | } |
50 | } |
20 | |
51 | |
21 | our %MUSIC_QUEUE; |
52 | our %MUSIC_QUEUE; |
|
|
53 | our %MUSIC_FACE_CACHE; # cleared by reload_facedata |
22 | |
54 | |
23 | our $MUSIC_SCHEDULER = cf::async_ext { |
55 | our $MUSIC_SCHEDULER = cf::async_ext { |
|
|
56 | $Coro::current->{desc} = "music scheduler"; |
|
|
57 | |
24 | while () { |
58 | while () { |
25 | for (keys %MUSIC_QUEUE) { |
59 | for (keys %MUSIC_QUEUE) { |
26 | delete $MUSIC_QUEUE{$_}; |
60 | delete $MUSIC_QUEUE{$_}; |
27 | |
61 | |
28 | cf::get_slot 0.01, -10; |
62 | cf::get_slot 0.01, -10, "music scheduler"; |
29 | |
63 | |
30 | my $pl = cf::player::find_active $_ or next; |
64 | my $pl = cf::player::find_active $_ or next; |
31 | $pl->ob->active or next; |
65 | $pl->ob->active or next; |
32 | my $ns = $pl->ns or next; |
66 | my $ns = $pl->ns or next; |
33 | $ns->extcmd or next; |
67 | $ns->extcmd or next; |
34 | my $map = $pl->ob->map or next; |
68 | my $map = $pl->ob->map or next; |
35 | |
69 | |
36 | my $faces; |
70 | my $faces = delete $ns->{music_play_once}; |
|
|
71 | $faces ||= $MUSIC_FACE_CACHE{$map} ||= do { |
|
|
72 | # 1. map-specific music info |
|
|
73 | parse_musiclist $map->{music} |
|
|
74 | or do { |
|
|
75 | # 2. fall back to region if no map-specific music |
|
|
76 | my $rgn = $pl->ob->region |
|
|
77 | or next; |
37 | |
78 | |
38 | # 1. update map-specific music info |
79 | my $par = $rgn; |
39 | unless (exists $map->{music_faces}) { |
80 | while () { |
40 | $map->{music_faces} = parse_facelist "music/", $map->{music}; |
81 | last if exists $par->{music}; |
41 | } |
82 | $par = $par->parent |
|
|
83 | or last; |
|
|
84 | } |
42 | |
85 | |
43 | my $faces = $map->{music_faces}; |
|
|
44 | |
|
|
45 | # 2. fall back to region if no map-specific music |
|
|
46 | unless ($faces) { |
|
|
47 | my $rgn = $pl->ob->region |
|
|
48 | or next; |
|
|
49 | |
|
|
50 | unless (exists $rgn->{music_faces}) { |
|
|
51 | my $par = $rgn; |
|
|
52 | while () { |
|
|
53 | last if exists $par->{music}; |
86 | parse_musiclist $par->{music} |
54 | $par = $par->parent |
|
|
55 | or last; |
|
|
56 | } |
87 | } |
57 | |
|
|
58 | $rgn->{music_faces} = parse_facelist "music/", $par->{music}; |
|
|
59 | } |
88 | ; |
60 | |
|
|
61 | $faces = $rgn->{music_faces}; |
|
|
62 | } |
89 | }; |
63 | |
90 | |
64 | $faces |
91 | $faces |
65 | or next; |
92 | or next; |
66 | |
93 | |
67 | my $facestr = join ",", @$faces; |
94 | my $facestr = join ",", @$faces; |
68 | $ns->{current_music_faces} ne $facestr |
95 | $ns->{current_music_faces} ne $facestr |
69 | or next; |
96 | or next; |
70 | |
97 | |
71 | $ns->{current_music_faces} = $facestr; |
98 | $ns->{current_music_faces} = $facestr; |
72 | |
99 | |
73 | my $msg = { |
100 | my $pri = 0; |
74 | play => $faces, |
101 | $ns->send_face ($_, --$pri - 110) |
75 | }; |
102 | for @$faces; |
|
|
103 | $ns->flush_fx; |
76 | |
104 | |
77 | my $pri = 0; |
|
|
78 | push @{$msg->{faces}}, [$_, $pri++, cf::face::get_chksum $_] |
|
|
79 | for grep $ns->must_send_face ($_), @$faces; |
|
|
80 | |
|
|
81 | $ns->ext_event (music => %$msg); |
105 | $ns->ext_msg (ambient_music => $faces); |
82 | } |
106 | } |
83 | Coro::schedule unless %MUSIC_QUEUE; |
107 | Coro::schedule unless %MUSIC_QUEUE; |
84 | } |
108 | } |
85 | }; |
109 | }; |
86 | |
110 | |
… | |
… | |
88 | on_region_change => sub { |
112 | on_region_change => sub { |
89 | my ($pl, $new, $old) = @_; |
113 | my ($pl, $new, $old) = @_; |
90 | |
114 | |
91 | $pl->ob->message ("You are now " . $new->longname . ". H<Use whereami for more details.>", $new->longname); |
115 | $pl->ob->message ("You are now " . $new->longname . ". H<Use whereami for more details.>", $new->longname); |
92 | |
116 | |
93 | $MUSIC_QUEUE{$pl->ob->name} = undef; |
117 | undef $MUSIC_QUEUE{$pl->ob->name}; |
94 | $MUSIC_SCHEDULER->ready; |
118 | $MUSIC_SCHEDULER->ready; |
95 | }, |
119 | }, |
96 | on_map_change => sub { |
120 | on_map_change => sub { |
97 | my ($pl, $new) = @_; |
121 | my ($pl, $new) = @_; |
98 | |
122 | |
99 | $MUSIC_QUEUE{$pl->ob->name} = undef; |
123 | undef $MUSIC_QUEUE{$pl->ob->name}; |
100 | $MUSIC_SCHEDULER->ready; |
124 | $MUSIC_SCHEDULER->ready; |
101 | }, |
125 | }, |
102 | ); |
126 | ); |
103 | |
127 | |
|
|
128 | sub play_music_once { |
|
|
129 | my ($pl, $music) = @_; |
104 | |
130 | |
|
|
131 | $pl->ns->{music_play_once} = parse_musiclist $music; |
|
|
132 | |
|
|
133 | undef $MUSIC_QUEUE{$pl->ob->name}; |
|
|
134 | $MUSIC_SCHEDULER->ready; |
|
|
135 | } |
|
|
136 | |