ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/player-env.ext
Revision: 1.16
Committed: Thu Apr 8 18:27:17 2010 UTC (14 years, 1 month ago) by root
Branch: MAIN
Changes since 1.15: +12 -3 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
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 root 1.5 map +(cf::face::find "$prefix$_" or ((warn "unable to find music file $_"), undef)),
17 root 1.4 split /\s*,\s*/, $list
18     ]
19 root 1.3 }
20    
21 root 1.2 our %MUSIC_QUEUE;
22 root 1.14 our %MUSIC_FACE_CACHE; # cleared by reload_facedata
23 root 1.2
24     our $MUSIC_SCHEDULER = cf::async_ext {
25 root 1.15 $Coro::current->{desc} = "music scheduler";
26    
27 root 1.2 while () {
28     for (keys %MUSIC_QUEUE) {
29     delete $MUSIC_QUEUE{$_};
30 root 1.4
31 root 1.9 cf::get_slot 0.01, -10, "music scheduler";
32 root 1.8
33 root 1.6 my $pl = cf::player::find_active $_ or next;
34     $pl->ob->active or next;
35     my $ns = $pl->ns or next;
36     $ns->extcmd or next;
37     my $map = $pl->ob->map or next;
38 root 1.4
39 root 1.16 my $faces = delete $ns->{music_play_once};
40     $faces ||= $MUSIC_FACE_CACHE{$map} ||= do {
41 root 1.14 # 1. map-specific music info
42     parse_facelist "music/", $map->{music}
43     or do {
44     # 2. fall back to region if no map-specific music
45     my $rgn = $pl->ob->region
46     or next;
47    
48     my $par = $rgn;
49     while () {
50     last if exists $par->{music};
51     $par = $par->parent
52     or last;
53     }
54 root 1.3
55 root 1.14 parse_facelist "music/", $par->{music}
56 root 1.3 }
57 root 1.14 ;
58     };
59 root 1.2
60 root 1.4 $faces
61     or next;
62    
63     my $facestr = join ",", @$faces;
64     $ns->{current_music_faces} ne $facestr
65     or next;
66    
67     $ns->{current_music_faces} = $facestr;
68    
69 root 1.6 my $pri = 0;
70 root 1.11 $ns->send_face ($_, --$pri - 110)
71     for @$faces;
72     $ns->flush_fx;
73 root 1.4
74 root 1.13 $ns->ext_msg (ambient_music => $faces);
75 root 1.2 }
76     Coro::schedule unless %MUSIC_QUEUE;
77     }
78     };
79    
80 root 1.1 cf::player->attach (
81     on_region_change => sub {
82     my ($pl, $new, $old) = @_;
83    
84     $pl->ob->message ("You are now " . $new->longname . ". H<Use whereami for more details.>", $new->longname);
85 root 1.2
86 root 1.16 undef $MUSIC_QUEUE{$pl->ob->name};
87 root 1.2 $MUSIC_SCHEDULER->ready;
88     },
89     on_map_change => sub {
90     my ($pl, $new) = @_;
91    
92 root 1.16 undef $MUSIC_QUEUE{$pl->ob->name};
93 root 1.2 $MUSIC_SCHEDULER->ready;
94 root 1.1 },
95     );
96    
97 root 1.16 sub play_music_once {
98     my ($pl, $music) = @_;
99    
100     $pl->ns->{music_play_once} = parse_facelist "music/", $music;
101    
102     undef $MUSIC_QUEUE{$pl->ob->name};
103     $MUSIC_SCHEDULER->ready;
104     }
105 root 1.1