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.7 by root, Thu Jul 12 19:36:57 2007 UTC vs.
Revision 1.20 by root, Sun May 16 01:58:50 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;
22 55
23our $MUSIC_SCHEDULER = cf::async_ext { 56our $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; 68 $ns->extcmd or next;
32 my $map = $pl->ob->map or next; 69 my $map = $ob->map;
70 my $rgn = $ob->region;
33 71
34 my $faces; 72 my $id = join "\x00", $map->path, $rgn->name;
35 73
74 my $faces = delete $ns->{music_play_once};
75 $faces ||= $MUSIC_FACE_CACHE{$id} ||= do {
36 # 1. update map-specific music info 76 # 1. map-specific music info
37 unless (exists $map->{music_faces}) { 77 parse_musiclist $map->{music}
38 $map->{music_faces} = parse_facelist "music/", $map->{music}; 78 or do {
39 } 79 # 2. fall back to region if no map-specific music
80 $rgn = $rgn->parent
81 while $rgn && !exists $rgn->{music};
40 82
41 my $faces = $map->{music_faces}; 83 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 } 84 }
55
56 $rgn->{music_faces} = parse_facelist "music/", $par->{music};
57 } 85 ;
58
59 $faces = $rgn->{music_faces};
60 } 86 };
61 87
62 $faces 88 $faces
63 or next; 89 or next;
64 90
65 my $facestr = join ",", @$faces; 91 my $facestr = join ",", @$faces;
66 $ns->{current_music_faces} ne $facestr 92 $ns->{current_music_faces} ne $facestr
67 or next; 93 or next;
68 94
69 $ns->{current_music_faces} = $facestr; 95 $ns->{current_music_faces} = $facestr;
70 96
71 my $msg = { 97 my $pri = 0;
72 play => $faces, 98 $ns->send_face ($_, --$pri - 110)
73 }; 99 for @$faces;
100 $ns->flush_fx;
74 101
75 my $pri = 0;
76 push @{$msg->{faces}}, [$_, $pri++, cf::face::get_chksum $_]
77 for grep $ns->must_send_face ($_), @$faces;
78
79 $ns->ext_event (music => %$msg); 102 $ns->ext_msg (ambient_music => $faces);
80
81 cf::cede_to_tick;
82 } 103 }
83 Coro::schedule unless %MUSIC_QUEUE; 104 Coro::schedule unless %MUSIC_QUEUE;
84 } 105 }
85}; 106};
86 107
88 on_region_change => sub { 109 on_region_change => sub {
89 my ($pl, $new, $old) = @_; 110 my ($pl, $new, $old) = @_;
90 111
91 $pl->ob->message ("You are now " . $new->longname . ". H<Use whereami for more details.>", $new->longname); 112 $pl->ob->message ("You are now " . $new->longname . ". H<Use whereami for more details.>", $new->longname);
92 113
93 $MUSIC_QUEUE{$pl->ob->name} = undef; 114 undef $MUSIC_QUEUE{$pl->ob->name};
94 $MUSIC_SCHEDULER->ready; 115 $MUSIC_SCHEDULER->ready;
95 }, 116 },
96 on_map_change => sub { 117 on_map_change => sub {
97 my ($pl, $new) = @_; 118 my ($pl, $new) = @_;
98 119
99 $MUSIC_QUEUE{$pl->ob->name} = undef; 120 undef $MUSIC_QUEUE{$pl->ob->name};
100 $MUSIC_SCHEDULER->ready; 121 $MUSIC_SCHEDULER->ready;
101 }, 122 },
102); 123);
103 124
125sub play_music_once {
126 my ($pl, $music) = @_;
104 127
128 $pl->ns->{music_play_once} = parse_musiclist $music;
129
130 undef $MUSIC_QUEUE{$pl->ob->name};
131 $MUSIC_SCHEDULER->ready;
132}
133

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines