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.2 by root, Wed Jul 11 16:26:26 2007 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
24sub parse_musiclist($) {
25 my ($list) = @_;
26
27 return undef
28 unless defined $list;
29
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 }
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 ]
52}
53
6our %MUSIC_QUEUE; 54our %MUSIC_QUEUE;
7 55
8our $MUSIC_SCHEDULER = cf::async_ext { 56our $MUSIC_SCHEDULER = cf::async_ext {
57 $Coro::current->{desc} = "music scheduler";
58
9 while () { 59 while () {
10 for (keys %MUSIC_QUEUE) { 60 for (keys %MUSIC_QUEUE) {
11 delete $MUSIC_QUEUE{$_}; 61 delete $MUSIC_QUEUE{$_};
62
63 cf::get_slot 0.01, -10, "music scheduler";
64
12 my $pl = cf::player::find_active $_ 65 my $pl = cf::player::find_active $_ or next;
66 my $ob = $pl->ob;
67 my $ns = $pl->ns or next;
68 my $map = $ob->map;
69 my $rgn = $ob->region;
70
71 my $id = join "\x00", $map->path, $rgn->name;
72
73 my $faces = delete $ns->{music_play_once};
74 $faces ||= $MUSIC_FACE_CACHE{$id} ||= do {
75 # 1. map-specific music info
76 parse_musiclist $map->{music}
77 or do {
78 # 2. fall back to region if no map-specific music
79 $rgn = $rgn->parent
80 while $rgn && !exists $rgn->{music};
81
82 parse_musiclist $rgn->{music}
83 }
84 ;
85 };
86
87 $faces
13 or next; 88 or next;
14 89
15 my $map = $pl->ob->map 90 my $facestr = join ",", @$faces;
91 $ns->{current_music_faces} ne $facestr
16 or next; 92 or next;
17 93
18 # 1. update map-specific music info 94 $ns->{current_music_faces} = $facestr;
19 unless (exists $map->{music}) {
20 95
21 } 96 my $pri = 0;
97 $ns->send_face ($_, --$pri - 110)
98 for @$faces;
99 $ns->flush_fx;
22 100
23 # 2. fall back to region if no map-specific music 101 $ns->ext_msg (ambient_music => $faces);
24 unless ($map->{music}) {
25 }
26
27 cf::cede_to_tick;
28 } 102 }
29 Coro::schedule unless %MUSIC_QUEUE; 103 Coro::schedule unless %MUSIC_QUEUE;
30 } 104 }
31}; 105};
32 106
34 on_region_change => sub { 108 on_region_change => sub {
35 my ($pl, $new, $old) = @_; 109 my ($pl, $new, $old) = @_;
36 110
37 $pl->ob->message ("You are now " . $new->longname . ". H<Use whereami for more details.>", $new->longname); 111 $pl->ob->message ("You are now " . $new->longname . ". H<Use whereami for more details.>", $new->longname);
38 112
39 $MUSIC_QUEUE{$pl->ob->name} = undef; 113 undef $MUSIC_QUEUE{$pl->ob->name};
40 $MUSIC_SCHEDULER->ready; 114 $MUSIC_SCHEDULER->ready;
41 }, 115 },
42 on_map_change => sub { 116 on_map_change => sub {
43 my ($pl, $new) = @_; 117 my ($pl, $new) = @_;
44 118
45 $MUSIC_QUEUE{$pl->ob->name} = undef; 119 undef $MUSIC_QUEUE{$pl->ob->name};
46 $MUSIC_SCHEDULER->ready; 120 $MUSIC_SCHEDULER->ready;
47 }, 121 },
48); 122);
49 123
124sub play_music_once {
125 my ($pl, $music) = @_;
50 126
127 $pl->ns->{music_play_once} = parse_musiclist $music;
128
129 undef $MUSIC_QUEUE{$pl->ob->name};
130 $MUSIC_SCHEDULER->ready;
131}
132

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines