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.15 by root, Wed Sep 19 21:56:30 2007 UTC vs.
Revision 1.19 by root, Fri May 14 23:47:37 2010 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...) 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
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 () {
34 $pl->ob->active or next; 66 $pl->ob->active or next;
35 my $ns = $pl->ns or next; 67 my $ns = $pl->ns or next;
36 $ns->extcmd or next; 68 $ns->extcmd or next;
37 my $map = $pl->ob->map or next; 69 my $map = $pl->ob->map or next;
38 70
71 my $faces = delete $ns->{music_play_once};
39 my $faces = $MUSIC_FACE_CACHE{$map} ||= do { 72 $faces ||= $MUSIC_FACE_CACHE{$map} ||= do {
40 # 1. map-specific music info 73 # 1. map-specific music info
41 parse_facelist "music/", $map->{music} 74 parse_musiclist $map->{music}
42 or do { 75 or do {
43 # 2. fall back to region if no map-specific music 76 # 2. fall back to region if no map-specific music
44 my $rgn = $pl->ob->region 77 my $rgn = $pl->ob->region
45 or next; 78 or next;
46 79
49 last if exists $par->{music}; 82 last if exists $par->{music};
50 $par = $par->parent 83 $par = $par->parent
51 or last; 84 or last;
52 } 85 }
53 86
54 parse_facelist "music/", $par->{music} 87 parse_musiclist $par->{music}
55 } 88 }
56 ; 89 ;
57 }; 90 };
58 91
59 $faces 92 $faces
80 on_region_change => sub { 113 on_region_change => sub {
81 my ($pl, $new, $old) = @_; 114 my ($pl, $new, $old) = @_;
82 115
83 $pl->ob->message ("You are now " . $new->longname . ". H<Use whereami for more details.>", $new->longname); 116 $pl->ob->message ("You are now " . $new->longname . ". H<Use whereami for more details.>", $new->longname);
84 117
85 $MUSIC_QUEUE{$pl->ob->name} = undef; 118 undef $MUSIC_QUEUE{$pl->ob->name};
86 $MUSIC_SCHEDULER->ready; 119 $MUSIC_SCHEDULER->ready;
87 }, 120 },
88 on_map_change => sub { 121 on_map_change => sub {
89 my ($pl, $new) = @_; 122 my ($pl, $new) = @_;
90 123
91 $MUSIC_QUEUE{$pl->ob->name} = undef; 124 undef $MUSIC_QUEUE{$pl->ob->name};
92 $MUSIC_SCHEDULER->ready; 125 $MUSIC_SCHEDULER->ready;
93 }, 126 },
94); 127);
95 128
129sub play_music_once {
130 my ($pl, $music) = @_;
96 131
132 $pl->ns->{music_play_once} = parse_musiclist $music;
133
134 undef $MUSIC_QUEUE{$pl->ob->name};
135 $MUSIC_SCHEDULER->ready;
136}
137

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines