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 | |
|
|
6 | sub parse_facelist($$) { |
|
|
7 | my ($prefix, $list) = @_; |
|
|
8 | |
|
|
9 | return undef |
|
|
10 | unless defined $list; |
|
|
11 | |
|
|
12 | #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$_" or ((warn "unable to find music file $_"), undef)), |
|
|
17 | split /\s*,\s*/, $list |
|
|
18 | ] |
|
|
19 | } |
|
|
20 | |
|
|
21 | our %MUSIC_QUEUE; |
|
|
22 | |
|
|
23 | our $MUSIC_SCHEDULER = cf::async_ext { |
|
|
24 | while () { |
|
|
25 | for (keys %MUSIC_QUEUE) { |
|
|
26 | delete $MUSIC_QUEUE{$_}; |
|
|
27 | |
|
|
28 | my $pl = cf::player::find_active $_ or next; |
|
|
29 | $pl->ob->active or next; |
|
|
30 | my $ns = $pl->ns or next; |
|
|
31 | $ns->extcmd or next; |
|
|
32 | my $map = $pl->ob->map or next; |
|
|
33 | |
|
|
34 | my $faces; |
|
|
35 | |
|
|
36 | # 1. update map-specific music info |
|
|
37 | unless (exists $map->{music_faces}) { |
|
|
38 | $map->{music_faces} = parse_facelist "music/", $map->{music}; |
|
|
39 | } |
|
|
40 | |
|
|
41 | my $faces = $map->{music_faces}; |
|
|
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 | } |
|
|
55 | |
|
|
56 | $rgn->{music_faces} = parse_facelist "music/", $par->{music}; |
|
|
57 | } |
|
|
58 | |
|
|
59 | $faces = $rgn->{music_faces}; |
|
|
60 | } |
|
|
61 | |
|
|
62 | $faces |
|
|
63 | or next; |
|
|
64 | |
|
|
65 | my $facestr = join ",", @$faces; |
|
|
66 | $ns->{current_music_faces} ne $facestr |
|
|
67 | or next; |
|
|
68 | |
|
|
69 | warn "MUSIC CHANGE <$ns->{current_music_faces}> <$facestr>\n";#d# |
|
|
70 | |
|
|
71 | $ns->{current_music_faces} = $facestr; |
|
|
72 | |
|
|
73 | my $msg = { |
|
|
74 | play => $faces, |
|
|
75 | }; |
|
|
76 | |
|
|
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); |
|
|
82 | |
|
|
83 | cf::cede_to_tick; |
|
|
84 | } |
|
|
85 | Coro::schedule unless %MUSIC_QUEUE; |
|
|
86 | } |
|
|
87 | }; |
5 | |
88 | |
6 | cf::player->attach ( |
89 | cf::player->attach ( |
7 | on_region_change => sub { |
90 | on_region_change => sub { |
8 | my ($pl, $new, $old) = @_; |
91 | my ($pl, $new, $old) = @_; |
9 | |
92 | |
10 | $pl->ob->message ("You are now " . $new->longname . ". H<Use whereami for more details.>", $new->longname); |
93 | $pl->ob->message ("You are now " . $new->longname . ". H<Use whereami for more details.>", $new->longname); |
|
|
94 | |
|
|
95 | $MUSIC_QUEUE{$pl->ob->name} = undef; |
|
|
96 | $MUSIC_SCHEDULER->ready; |
|
|
97 | }, |
|
|
98 | on_map_change => sub { |
|
|
99 | my ($pl, $new) = @_; |
|
|
100 | |
|
|
101 | $MUSIC_QUEUE{$pl->ob->name} = undef; |
|
|
102 | $MUSIC_SCHEDULER->ready; |
11 | }, |
103 | }, |
12 | ); |
104 | ); |
13 | |
105 | |
14 | |
106 | |