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