ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/player-env.ext
Revision: 1.17
Committed: Thu Apr 29 07:52:02 2010 UTC (14 years ago) by root
Branch: MAIN
Changes since 1.16: +1 -1 lines
Log Message:
logging

File Contents

# Content
1 #! perl # mandatory
2
3 # this extension provides "environmental effects",
4 # meaning mostly background music and region messages right now.
5
6 sub parse_facelist($$) {
7 my ($prefix, $list) = @_;
8
9 return undef
10 unless defined $list;
11
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
18 ]
19 }
20
21 our %MUSIC_QUEUE;
22 our %MUSIC_FACE_CACHE; # cleared by reload_facedata
23
24 our $MUSIC_SCHEDULER = cf::async_ext {
25 $Coro::current->{desc} = "music scheduler";
26
27 while () {
28 for (keys %MUSIC_QUEUE) {
29 delete $MUSIC_QUEUE{$_};
30
31 cf::get_slot 0.01, -10, "music scheduler";
32
33 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
39 my $faces = delete $ns->{music_play_once};
40 $faces ||= $MUSIC_FACE_CACHE{$map} ||= do {
41 # 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
55 parse_facelist "music/", $par->{music}
56 }
57 ;
58 };
59
60 $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 my $pri = 0;
70 $ns->send_face ($_, --$pri - 110)
71 for @$faces;
72 $ns->flush_fx;
73
74 $ns->ext_msg (ambient_music => $faces);
75 }
76 Coro::schedule unless %MUSIC_QUEUE;
77 }
78 };
79
80 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
86 undef $MUSIC_QUEUE{$pl->ob->name};
87 $MUSIC_SCHEDULER->ready;
88 },
89 on_map_change => sub {
90 my ($pl, $new) = @_;
91
92 undef $MUSIC_QUEUE{$pl->ob->name};
93 $MUSIC_SCHEDULER->ready;
94 },
95 );
96
97 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