ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/player-env.ext
(Generate patch)

Comparing deliantra/server/ext/player-env.ext (file contents):
Revision 1.17 by root, Thu Apr 29 07:52:02 2010 UTC vs.
Revision 1.24 by root, Sun Nov 11 05:53:11 2012 UTC

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
6our %MUSIC_GROUP;
7our %MUSIC_FACE_CACHE; # cleared by reload_facedata
8
9sub 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
20cf::post_init {
21 $_[0] ? cf::async { reload } : reload
22};
23
6sub parse_facelist($$) { 24sub 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...)
13 [
14 sort { (cf::face::get_data_size $a) <=> (cf::face::get_data_size $b) }
15 grep $_,
16 map +(cf::face::find "$prefix$_" or ((cf::error "unable to find music file $_"), undef)),
17 split /\s*,\s*/, $list 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 }
18 ] 48 }
49
50 # sort by size, smallest first, because it's the fastest to download...
51 [ sort { ($cf::face::SIZE[0][$a]) <=> ($cf::face::SIZE[0][$b]) } @face ]
19} 52}
20 53
21our %MUSIC_QUEUE; 54our %MUSIC_QUEUE;
22our %MUSIC_FACE_CACHE; # cleared by reload_facedata
23 55
24our $MUSIC_SCHEDULER = cf::async_ext { 56our $MUSIC_SCHEDULER = cf::async_ext {
25 $Coro::current->{desc} = "music scheduler"; 57 $Coro::current->{desc} = "music scheduler";
26 58
27 while () { 59 while () {
29 delete $MUSIC_QUEUE{$_}; 61 delete $MUSIC_QUEUE{$_};
30 62
31 cf::get_slot 0.01, -10, "music scheduler"; 63 cf::get_slot 0.01, -10, "music scheduler";
32 64
33 my $pl = cf::player::find_active $_ or next; 65 my $pl = cf::player::find_active $_ or next;
34 $pl->ob->active or next; 66 my $ob = $pl->ob;
35 my $ns = $pl->ns or next; 67 my $ns = $pl->ns or next;
36 $ns->extcmd or next;
37 my $map = $pl->ob->map or next; 68 my $map = $ob->map;
69 my $rgn = $ob->region;
70
71 my $id = join "\x00", $map->path, $rgn->name;
38 72
39 my $faces = delete $ns->{music_play_once}; 73 my $faces = delete $ns->{music_play_once};
40 $faces ||= $MUSIC_FACE_CACHE{$map} ||= do { 74 $faces ||= $MUSIC_FACE_CACHE{$id} ||= do {
41 # 1. map-specific music info 75 # 1. map-specific music info
42 parse_facelist "music/", $map->{music} 76 parse_musiclist $map->{music}
43 or do { 77 or do {
44 # 2. fall back to region if no map-specific music 78 # 2. fall back to region if no map-specific music
45 my $rgn = $pl->ob->region 79 $rgn = $rgn->parent
46 or next; 80 while $rgn && !exists $rgn->{music};
47 81
48 my $par = $rgn;
49 while () {
50 last if exists $par->{music};
51 $par = $par->parent
52 or last;
53 }
54
55 parse_facelist "music/", $par->{music} 82 parse_musiclist $rgn->{music}
56 } 83 }
57 ; 84 ;
58 }; 85 };
59 86
60 $faces 87 $faces
95); 122);
96 123
97sub play_music_once { 124sub play_music_once {
98 my ($pl, $music) = @_; 125 my ($pl, $music) = @_;
99 126
100 $pl->ns->{music_play_once} = parse_facelist "music/", $music; 127 $pl->ns->{music_play_once} = parse_musiclist $music;
101 128
102 undef $MUSIC_QUEUE{$pl->ob->name}; 129 undef $MUSIC_QUEUE{$pl->ob->name};
103 $MUSIC_SCHEDULER->ready; 130 $MUSIC_SCHEDULER->ready;
104} 131}
105 132

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines