… | |
… | |
21 | |
21 | |
22 | my $self = $class->SUPER::new (%arg, |
22 | my $self = $class->SUPER::new (%arg, |
23 | setup_req => { |
23 | setup_req => { |
24 | extmap => 1, |
24 | extmap => 1, |
25 | excmd => 1, |
25 | excmd => 1, |
26 | ywidget => 1,#d# |
26 | xwidget1 => 1,#d# |
27 | %{$arg{setup_req} || {}}, |
27 | %{$arg{setup_req} || {}}, |
28 | }, |
28 | }, |
29 | ); |
29 | ); |
30 | |
30 | |
31 | $self->{map_widget}->clr_commands; |
31 | $self->{map_widget}->clr_commands; |
… | |
… | |
48 | sort { (length $a) <=> (length $b) } |
48 | sort { (length $a) <=> (length $b) } |
49 | @args |
49 | @args |
50 | } sort { $a->{par} <=> $b->{par} } |
50 | } sort { $a->{par} <=> $b->{par} } |
51 | CFPlus::Pod::find command => "*"; |
51 | CFPlus::Pod::find command => "*"; |
52 | |
52 | |
53 | $self->connect_ext (music => sub { |
53 | $self->connect_ext (ambient_music => sub { |
54 | my ($ev) = @_; |
54 | my ($songs) = @_; |
55 | |
55 | &::audio_music_set_ambient ($songs); |
56 | &::audio_music_set ($self->{songs} = $ev->{play}) |
|
|
57 | if $ev->{play}; |
|
|
58 | }); |
56 | }); |
59 | |
57 | |
60 | $self->connect_ext (capabilities => sub { |
58 | $self->connect_ext (capabilities => sub { |
61 | my (%cap) = @_; |
59 | my (%cap) = @_; |
62 | |
60 | |
63 | #$self->send ("setup sound 0"); # we use a different protocol |
61 | #$self->send ("setup sound 0"); # we use a different protocol |
|
|
62 | $self->update_fx_want; |
64 | |
63 | |
65 | $self->send_exti_msg (fx_want => { |
64 | $self->send_exti_req (resource => "exp_table", sub { |
66 | 3 => !!$::CFG->{bgm_enable}, # FT_MUSIC |
65 | my ($exp_table) = @_; |
67 | 5 => !!$::CFG->{audio_enable}, # FT_SOUND |
66 | |
|
|
67 | $self->register_face_handler ($exp_table, sub { |
|
|
68 | my ($face) = @_; |
|
|
69 | |
|
|
70 | $self->{exp_table} = $self->{json_coder}->decode (delete $face->{data}); |
|
|
71 | |
|
|
72 | #TODO: update all interested parties |
|
|
73 | }); |
|
|
74 | |
|
|
75 | () |
68 | }); |
76 | }); |
69 | |
77 | |
70 | if (my $ts = $cap{tileset}) { |
78 | if (my $ts = $cap{tileset}) { |
71 | if (my ($default) = grep $_->[2] & 1, @$ts) { |
79 | if (my ($default) = grep $_->[2] & 1, @$ts) { |
72 | $self->{tileset} = $default; |
80 | $self->{tileset} = $default; |
… | |
… | |
110 | # widgetset create |
118 | # widgetset create |
111 | $self->connect_ext (ws_c => sub { |
119 | $self->connect_ext (ws_c => sub { |
112 | my ($ws, $id, $class, $args) = @_; |
120 | my ($ws, $id, $class, $args) = @_; |
113 | |
121 | |
114 | for my $ev (grep /^on_/, keys %$args) { |
122 | for my $ev (grep /^on_/, keys %$args) { |
|
|
123 | my $rid = $args->{$ev}; |
115 | $args->{$ev} = sub { |
124 | $args->{$ev} = sub { |
116 | my $id = shift->{s_id}; |
125 | my $id = shift->{s_id}; |
117 | $self->send_exti_msg (w_e => $id, $ev, \@_); |
126 | $self->send_exti_msg (w_e => $id, $rid, @_); |
118 | |
127 | |
119 | 1 |
128 | 1 |
120 | }; |
129 | }; |
121 | } |
130 | } |
122 | |
131 | |
123 | if (my $widget = eval { |
132 | $self->widget_associate ( |
|
|
133 | $ws, $id => scalar eval { |
124 | local $SIG{__DIE__}; |
134 | local $SIG{__DIE__}; |
125 | "CFPlus::UI::$class"->new ( |
135 | "CFPlus::UI::$class"->new (%$args) |
126 | %$args, |
|
|
127 | s_ws => $ws, |
|
|
128 | s_id => $id, |
|
|
129 | ) |
|
|
130 | } |
136 | } |
131 | ) { |
|
|
132 | $self->{widget}{$id} |
|
|
133 | = $self->{widgetset}{$ws}{w}{$id} |
|
|
134 | = $widget; |
|
|
135 | |
|
|
136 | $widget->connect (on_destroy => sub { |
|
|
137 | my ($widget) = @_; |
|
|
138 | |
|
|
139 | delete $self->{widget}{$widget->{s_id}}; |
|
|
140 | delete $self->{widgetset}{$widget->{s_ws}}{$widget->{s_id}}; |
|
|
141 | }); |
|
|
142 | } else { |
|
|
143 | warn "server failed creating client-side widget " . (CFPlus::to_json $class) . ": $@\n"; |
137 | ) or warn "server failed creating client-side widget " . (CFPlus::to_json $class) . ": $@\n"; |
144 | $self->send_exti_msg (w_e => $id, "destroy"); |
138 | }); |
|
|
139 | |
|
|
140 | # widgetset associate |
|
|
141 | $self->connect_ext (ws_a => sub { |
|
|
142 | my (%ass) = @_; |
|
|
143 | |
|
|
144 | # everything that has a name, wether conceivably useful or not |
|
|
145 | my %wkw = ( |
|
|
146 | root => $CFPlus::UI::ROOT, |
|
|
147 | tooltip => $CFPlus::UI::TOOLTIP, |
|
|
148 | |
|
|
149 | mapwidget => $::MAPWIDGET, |
|
|
150 | buttonbar => $::BUTTONBAR, |
|
|
151 | metaserver => $::METASERVER, |
|
|
152 | buttonbar => $::BUTTONBAR, |
|
|
153 | login_button => $::LOGIN_BUTTON, |
|
|
154 | quit_dialog => $::QUIT_DIALOG, |
|
|
155 | host_entry => $::HOST_ENTRY, |
|
|
156 | metaserver => $::METASERVER, |
|
|
157 | server_info => $::SERVER_INFO, |
|
|
158 | |
|
|
159 | setup_dialog => $::SETUP_DIALOG, |
|
|
160 | setup_notebook => $::SETUP_NOTEBOOK, |
|
|
161 | setup_server => $::SETUP_SERVER, |
|
|
162 | setup_keyboard => $::SETUP_KEYBOARD, |
|
|
163 | |
|
|
164 | pl_notebook => $::PL_NOTEBOOK, |
|
|
165 | pl_window => $::PL_WINDOW, |
|
|
166 | inventory_page => $::INVENTORY_PAGE, |
|
|
167 | stats_page => $::STATS_PAGE, |
|
|
168 | skill_page => $::SKILL_PAGE, |
|
|
169 | spell_page => $::SPELL_PAGE, |
|
|
170 | spell_list => $::SPELL_LIST, |
|
|
171 | |
|
|
172 | floorbox => $::FLOORBOX, |
|
|
173 | help_window => $::HELP_WINDOW, |
|
|
174 | message_window => $::MESSAGE_WINDOW, |
|
|
175 | statusbox => $::SDTATUSBOX, |
|
|
176 | |
|
|
177 | inv => $::INV, |
|
|
178 | invr => $::INVR, |
|
|
179 | invr_hb => $::INVR_HB, |
|
|
180 | |
|
|
181 | ); |
|
|
182 | |
|
|
183 | while (my ($id, $name) = each %ass) { |
|
|
184 | $self->widget_associate (undef, $id => $wkw{$name}) |
|
|
185 | or warn "server failed to associate non-existent well-known widget $name\n"; |
145 | } |
186 | } |
146 | }); |
187 | }); |
147 | |
188 | |
148 | # widget call |
189 | # widget call |
149 | $self->connect_ext (w_c => sub { |
190 | $self->connect_ext (w_c => sub { |
150 | my ($id, $rid, $method, $args) = @_; |
191 | my ($id, $rid, $method, @args) = @_; |
151 | |
192 | |
152 | my $w = $self->{widget}{$id} |
193 | my $w = $self->{widget}{$id} |
153 | or return; |
194 | or return; |
154 | |
195 | |
155 | $args ||= []; |
|
|
156 | |
|
|
157 | if ($rid) { |
196 | if ($rid) { |
158 | $self->send_exti_msg (w_r => $rid, [$w->$method (@$args)]); |
197 | $self->send_exti_msg (w_r => $rid, $w->$method (@args)); |
159 | } else { |
198 | } else { |
160 | $w->$method (@$args); |
199 | $w->$method (@args); |
161 | } |
200 | } |
162 | }); |
201 | }); |
163 | |
202 | |
164 | # widget set |
203 | # widget set |
165 | $self->connect_ext (w_s => sub { |
204 | $self->connect_ext (w_s => sub { |
… | |
… | |
225 | $self->{mapcache} = "mapcache_$self->{host}_$self->{port}"; |
264 | $self->{mapcache} = "mapcache_$self->{host}_$self->{port}"; |
226 | |
265 | |
227 | $self |
266 | $self |
228 | } |
267 | } |
229 | |
268 | |
|
|
269 | sub widget_associate { |
|
|
270 | my ($self, $ws, $id, $widget) = @_; |
|
|
271 | |
|
|
272 | if ($widget) { |
|
|
273 | $widget->{s_id} = $id; |
|
|
274 | $self->{widget}{$id} = $widget; |
|
|
275 | |
|
|
276 | if ($ws) { |
|
|
277 | $widget->{s_ws} = $ws; |
|
|
278 | $self->{widgetset}{$ws}{w}{$id} = $widget; |
|
|
279 | } |
|
|
280 | |
|
|
281 | $widget->connect (on_destroy => sub { |
|
|
282 | my ($widget) = @_; |
|
|
283 | |
|
|
284 | delete $self->{widget}{$widget->{s_id}}; |
|
|
285 | delete $self->{widgetset}{$widget->{s_ws}}{$widget->{s_id}} |
|
|
286 | if exists $widget->{s_ws}; |
|
|
287 | }); |
|
|
288 | |
|
|
289 | 1 |
|
|
290 | } else { |
|
|
291 | $self->send_exti_msg (w_e => $id, undef); |
|
|
292 | |
|
|
293 | 0 |
|
|
294 | } |
|
|
295 | } |
|
|
296 | |
|
|
297 | sub update_fx_want { |
|
|
298 | my ($self) = @_; |
|
|
299 | |
|
|
300 | $self->send_exti_msg (fx_want => { |
|
|
301 | 3 => !!$::CFG->{bgm_enable}, # FT_MUSIC |
|
|
302 | 5 => !!$::CFG->{audio_enable}, # FT_SOUND |
|
|
303 | 6 => 1, # FT_RSRC |
|
|
304 | }); |
|
|
305 | } |
|
|
306 | |
230 | sub logprint { |
307 | sub logprint { |
231 | my ($self, @a) = @_; |
308 | my ($self, @a) = @_; |
232 | |
309 | |
233 | $self->{log_fh} ||= do { |
310 | CFPlus::DB::logprint "$Crossfire::VARDIR/log.$self->{host}" => (join "", @a), sub { }; |
234 | my $path = "$Crossfire::VARDIR/log.$self->{host}"; |
|
|
235 | |
|
|
236 | open my $fh, ">>:utf8", $path |
|
|
237 | or die "Couldn't open logfile $path: $!"; |
|
|
238 | |
|
|
239 | $fh->autoflush (1); |
|
|
240 | |
|
|
241 | $fh; |
|
|
242 | }; |
|
|
243 | |
|
|
244 | my ($sec, $min, $hour, $mday, $mon, $year) = localtime time; |
|
|
245 | |
|
|
246 | my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d", |
|
|
247 | $year + 1900, $mon + 1, $mday, $hour, $min, $sec; |
|
|
248 | |
|
|
249 | print {$self->{log_fh}} "$ts ", @a, "\n"; |
|
|
250 | } |
311 | } |
251 | |
312 | |
252 | sub _stat_numdiff { |
313 | sub _stat_numdiff { |
253 | my ($self, $name, $old, $new) = @_; |
314 | my ($self, $name, $old, $new) = @_; |
254 | |
315 | |
… | |
… | |
519 | } |
580 | } |
520 | |
581 | |
521 | sub feed_map1a { |
582 | sub feed_map1a { |
522 | my ($self, $data) = @_; |
583 | my ($self, $data) = @_; |
523 | |
584 | |
524 | $self->{map}->map1a_update ($data, $self->{setup}{extmap}); |
585 | my $missing = $self->{map}->map1a_update ($data, $self->{setup}{extmap}); |
|
|
586 | my $delay; |
|
|
587 | |
|
|
588 | for my $tile (@$missing) { |
|
|
589 | next if $self->{delay}{$tile}; |
|
|
590 | |
|
|
591 | $delay = 1; |
|
|
592 | |
|
|
593 | if (my $tex = $::CONN->{texture}[$tile]) { |
|
|
594 | $tex->upload; |
|
|
595 | } else { |
|
|
596 | $self->{delay}{$tile} = 1; |
|
|
597 | |
|
|
598 | # we assume the face is in-flight and will eventually come |
|
|
599 | push @{$self->{tile_cb}{$tile}}, sub { |
|
|
600 | delete $self->{delay}{$tile}; |
|
|
601 | $_[0]->upload; |
|
|
602 | }; |
|
|
603 | } |
|
|
604 | } |
|
|
605 | |
|
|
606 | if ($delay) { |
|
|
607 | # delay the map drawing a tiny bit in the hope of getting the missing fetched |
|
|
608 | Event->timer (after => 0.03, cb => sub { |
|
|
609 | $_[0]->w->cancel; |
|
|
610 | $self->{map_widget}->update; |
|
|
611 | }); |
|
|
612 | } else { |
525 | $self->{map_widget}->update; |
613 | $self->{map_widget}->update; |
|
|
614 | } |
526 | } |
615 | } |
527 | |
616 | |
528 | sub magicmap { |
617 | sub magicmap { |
529 | my ($self, $w, $h, $x, $y, $data) = @_; |
618 | my ($self, $w, $h, $x, $y, $data) = @_; |
530 | |
619 | |
… | |
… | |
537 | my $map_info = delete $self->{map_info} |
626 | my $map_info = delete $self->{map_info} |
538 | or return; |
627 | or return; |
539 | |
628 | |
540 | my ($hash, $x, $y, $w, $h) = @$map_info; |
629 | my ($hash, $x, $y, $w, $h) = @$map_info; |
541 | |
630 | |
542 | my $data = $self->{map}->get_rect ($x, $y, $w, $h); |
631 | my $data = Compress::LZF::compress $self->{map}->get_rect ($x, $y, $w, $h); |
|
|
632 | $self->{map_cache_new}{$hash} = \$data; |
543 | CFPlus::DB::put $self->{mapcache} => $hash => Compress::LZF::compress $data, sub { }; |
633 | CFPlus::DB::put $self->{mapcache} => $hash => $data, sub { }; |
544 | #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# |
|
|
545 | } |
634 | } |
546 | |
635 | |
547 | sub map_clear { |
636 | sub map_clear { |
548 | my ($self) = @_; |
637 | my ($self) = @_; |
549 | |
638 | |
… | |
… | |
555 | } |
644 | } |
556 | |
645 | |
557 | sub bg_fetch { |
646 | sub bg_fetch { |
558 | my ($self) = @_; |
647 | my ($self) = @_; |
559 | |
648 | |
560 | my $id; |
649 | my $tile; |
561 | |
650 | |
562 | do { |
651 | do { |
563 | $id = pop @{$self->{bg_fetch}} |
652 | $tile = pop @{$self->{bg_fetch}} |
564 | or return; |
653 | or return; |
565 | } while $self->{texture}[$id]; |
654 | } while $self->{texture}[$tile]; |
566 | |
655 | |
567 | CFPlus::DB::get tilecache => $id, sub { |
656 | CFPlus::DB::get tilecache => $tile, sub { |
568 | my ($data) = @_; |
657 | my ($data) = @_; |
569 | |
658 | |
570 | return unless $self->{map}; # stop when destroyed |
659 | return unless $self->{map}; # stop when destroyed |
571 | |
660 | |
572 | $self->set_texture ($id => $data) |
|
|
573 | if defined $data; |
661 | if (defined $data) { |
|
|
662 | $self->have_tile ($tile, $data); |
|
|
663 | $self->{texture}[$tile]->upload; |
|
|
664 | } |
574 | |
665 | |
575 | $self->bg_fetch; |
666 | $self->bg_fetch; |
576 | }; |
667 | }; |
577 | } |
668 | } |
578 | |
669 | |
579 | sub load_map($$$) { |
670 | sub load_map($$$) { |
580 | my ($self, $hash, $x, $y) = @_; |
671 | my ($self, $hash, $x, $y) = @_; |
581 | |
672 | |
582 | my $gen = $self->{map_change_gen}; |
673 | my $gen = $self->{map_change_gen}; |
583 | |
674 | |
584 | CFPlus::DB::get $self->{mapcache} => $hash, sub { |
675 | my $cb = sub { |
585 | return unless $gen == $self->{map_change_gen}; |
676 | return unless $gen == $self->{map_change_gen}; |
586 | |
677 | |
587 | my ($data) = @_; |
678 | my ($data) = @_; |
588 | |
679 | |
589 | if (defined $data) { |
680 | if (defined $data) { |
|
|
681 | $self->{map_cache_new}{$hash} = \$data; |
|
|
682 | |
590 | $data = Compress::LZF::decompress $data; |
683 | my $data = Compress::LZF::decompress $data; |
591 | #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d# |
|
|
592 | |
684 | |
593 | my $inprogress = @{ $self->{bg_fetch} || [] }; |
685 | my $inprogress = @{ $self->{bg_fetch} || [] }; |
594 | unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data); |
686 | unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data); |
595 | $self->bg_fetch unless $inprogress; |
687 | $self->bg_fetch unless $inprogress; |
596 | } |
688 | } |
597 | }; |
689 | }; |
|
|
690 | |
|
|
691 | if (my $rdata = $self->{map_cache_old}{$hash}) { |
|
|
692 | $cb->($$rdata); |
|
|
693 | } else { |
|
|
694 | CFPlus::DB::get $self->{mapcache} => $hash, $cb; |
|
|
695 | } |
598 | } |
696 | } |
599 | |
697 | |
600 | # hardcode /world/world_xxx_xxx map names, the savings are enourmous, |
698 | # hardcode /world/world_xxx_xxx map names, the savings are enourmous, |
601 | # (server resource,s latency, bandwidth), so this hack is warranted. |
699 | # (server resource,s latency, bandwidth), so this hack is warranted. |
602 | # the right fix is to make real tiled maps with an overview file |
700 | # the right fix is to make real tiled maps with an overview file |
… | |
… | |
695 | my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; |
793 | my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; |
696 | |
794 | |
697 | $self->flush_map; |
795 | $self->flush_map; |
698 | |
796 | |
699 | ++$self->{map_change_gen}; |
797 | ++$self->{map_change_gen}; |
|
|
798 | $self->{map_cache_old} = delete $self->{map_cache_new}; |
700 | |
799 | |
701 | my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); |
800 | my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); |
702 | |
801 | |
703 | my $mapmapw = $self->{mapmap}->{w}; |
802 | my $mapmapw = $self->{mapmap}->{w}; |
704 | my $mapmaph = $self->{mapmap}->{h}; |
803 | my $mapmaph = $self->{mapmap}->{h}; |
… | |
… | |
726 | my ($self, $facenum, $face, $cb) = @_; |
825 | my ($self, $facenum, $face, $cb) = @_; |
727 | |
826 | |
728 | if ($face->{type} == 0) { # FT_FACE |
827 | if ($face->{type} == 0) { # FT_FACE |
729 | my $id = CFPlus::DB::get_tile_id_sync $face->{name}; |
828 | my $id = CFPlus::DB::get_tile_id_sync $face->{name}; |
730 | |
829 | |
731 | $face->{id} = $id; |
830 | $face->{id} = $id; |
732 | $self->{faceid}[$facenum] = $id; |
|
|
733 | |
|
|
734 | $self->{map}->set_tileid ($facenum => $id); |
831 | $self->{map}->set_tileid ($facenum => $id); |
735 | |
832 | |
736 | CFPlus::DB::get tilecache => $id, $cb; |
833 | CFPlus::DB::get tilecache => $id, $cb; |
737 | |
834 | |
738 | } elsif ($face->{type} & 1) { # with metadata |
835 | } elsif ($face->{type} & 1) { # with metadata |
739 | #::message ({ markup => "starting to download song #$face/$pri, check your output-rate setting if your connection gets laggy." }); |
|
|
740 | CFPlus::DB::get "res-metadata" => $face->{name}, $cb; |
836 | CFPlus::DB::get res_meta => $face->{name}, $cb; |
|
|
837 | |
|
|
838 | } else { # no metadata |
|
|
839 | CFPlus::DB::get res_data => $face->{name}, $cb; |
741 | } |
840 | } |
742 | } |
841 | } |
743 | |
842 | |
744 | sub face_update { |
843 | sub face_update { |
745 | my ($self, $facenum, $face, $changed) = @_; |
844 | my ($self, $facenum, $face, $changed) = @_; |
746 | |
845 | |
747 | if ($face->{type} == 0) { # FT_FACE |
846 | if ($face->{type} == 0) { |
|
|
847 | # image, FT_FACE |
748 | CFPlus::DB::put tilecache => $face->{id} => $face->{data}, sub { } |
848 | CFPlus::DB::put tilecache => $face->{id} => $face->{data}, sub { } |
749 | if $changed; |
849 | if $changed; |
750 | |
850 | |
751 | $self->set_texture ($face->{id} => delete $face->{data}); |
851 | $self->have_tile ($face->{id}, delete $face->{data}); |
752 | |
852 | |
753 | } elsif ($face->{type} & 1) { # split metadata |
853 | } elsif ($face->{type} & 1) { |
754 | my $cb = sub { }; |
854 | # split metadata case, FT_MUSIC, FT_SOUND |
|
|
855 | if ($changed) { # new data |
|
|
856 | my ($meta, $data) = unpack "(w/a*)*", $face->{data}; |
|
|
857 | $face->{data} = $meta; |
|
|
858 | |
|
|
859 | CFPlus::DB::put res_data => $face->{name} => $data, sub { }; |
|
|
860 | CFPlus::DB::put res_meta => $face->{name} => $meta, sub { }; |
|
|
861 | } |
|
|
862 | |
|
|
863 | $face->{data} = $self->{json_coder}->decode ($face->{data}); |
|
|
864 | |
|
|
865 | ::add_license ($face); |
755 | |
866 | |
756 | if ($face->{type} == 3) { # FT_MUSIC |
867 | if ($face->{type} == 3) { # FT_MUSIC |
757 | $cb = sub { |
|
|
758 | $self->{music_meta}{$facenum} = $face; |
|
|
759 | |
|
|
760 | ::message ({ markup => "downloaded song #$facenum" }) |
868 | ::message ({ markup => "downloaded song #$facenum" }) |
761 | if $changed; |
869 | if $changed; |
762 | |
870 | |
763 | &::audio_music_set ($self->{songs}); |
871 | &::audio_music_push ($facenum); |
|
|
872 | } elsif ($face->{type} == 5) { # FT_SOUND |
|
|
873 | ::message ({ markup => "downloaded sound #$facenum" }) |
|
|
874 | if $changed; |
|
|
875 | |
|
|
876 | &::audio_sound_push ($facenum); |
764 | }; |
877 | } |
765 | } |
|
|
766 | |
878 | |
767 | if ($changed) { # new data |
|
|
768 | my ($meta_json, $data) = unpack "(w/a*)*", delete $face->{data}; |
|
|
769 | my $path = CFPlus::DB::path_of_res $face->{name}; |
|
|
770 | |
|
|
771 | CFPlus::DB::write_file $path, $data, sub { |
|
|
772 | CFPlus::DB::put "res-metadata" => $face->{name} => $meta_json, sub { |
|
|
773 | $face->{meta} = $self->{json_coder}->decode ($meta_json); |
|
|
774 | $face->{path} = $path; |
|
|
775 | |
|
|
776 | $cb->(); |
|
|
777 | }; |
|
|
778 | }; |
|
|
779 | } else { |
879 | } else { |
780 | $face->{meta} = $self->{json_coder}->decode (delete $face->{data}); |
880 | # flat resource case, FT_RSRC |
781 | $cb->(); |
881 | CFPlus::DB::put res_data => $face->{name} => $face->{data}, sub { } |
782 | } |
882 | if $changed; |
|
|
883 | } |
|
|
884 | |
|
|
885 | if (my $cbs = $self->{face_cb}{$facenum}) { |
|
|
886 | $_->($face, $changed) for @$cbs; |
783 | } |
887 | } |
784 | } |
888 | } |
785 | |
889 | |
786 | sub smooth_update { |
890 | sub smooth_update { |
787 | my ($self, $facenum, $face) = @_; |
891 | my ($self, $facenum, $face) = @_; |
788 | |
892 | |
789 | $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel}); |
893 | $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel}); |
790 | } |
894 | } |
791 | |
895 | |
792 | sub set_texture { |
896 | sub have_tile { |
793 | my ($self, $id, $data) = @_; |
897 | my ($self, $tile, $data) = @_; |
794 | |
898 | |
795 | return unless $self->{map}; |
899 | return unless $self->{map}; |
796 | |
900 | |
797 | $self->{texture}[$id] = my $tex = |
901 | my $tex = $self->{texture}[$tile] ||= |
798 | new_from_image CFPlus::Texture |
902 | new CFPlus::Texture |
|
|
903 | tile => $tile, |
|
|
904 | image => $data, delete_image => 1, |
799 | $data, minify => 1, mipmap => 1; |
905 | minify => 1, mipmap => 1; |
800 | |
906 | |
801 | $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); |
907 | if (my $cbs = delete $self->{tile_cb}{$tile}) { |
802 | $self->{map_widget}->update; |
908 | $_->($tex) for @$cbs; |
803 | |
909 | } |
804 | $_->() for @{(delete $self->{face_cb}{$id}) || []}; |
|
|
805 | } |
910 | } |
806 | |
911 | |
807 | sub connect_face_update { |
912 | # call in non-void context registers a temporary |
|
|
913 | # hook with handle, otherwise its permanent |
|
|
914 | sub on_face_change { |
808 | my ($self, $id, $cb) = @_; |
915 | my ($self, $num, $cb) = @_; |
809 | |
916 | |
810 | push @{$self->{face_cb}{$id}}, $cb; |
917 | push @{$self->{face_cb}{$num}}, $cb; |
811 | |
918 | |
|
|
919 | defined wantarray |
812 | CFPlus::guard { |
920 | ? CFPlus::guard { |
813 | @{$self->{face_cb}{$id}} |
921 | @{$self->{face_cb}{$num}} |
814 | = grep $_ != $cb, |
922 | = grep $_ != $cb, |
815 | @{$self->{face_cb}{$id}}; |
923 | @{$self->{face_cb}{$num}}; |
816 | } |
924 | } |
|
|
925 | : () |
|
|
926 | } |
|
|
927 | |
|
|
928 | # call in non-void context registers a temporary |
|
|
929 | # hook with handle, otherwise its permanent |
|
|
930 | sub register_face_handler { |
|
|
931 | my ($self, $num, $cb) = @_; |
|
|
932 | |
|
|
933 | # invoke if available right now |
|
|
934 | $cb->($self->{face}[$num], 0) |
|
|
935 | unless exists $self->{face}[$num]{loading}; |
|
|
936 | |
|
|
937 | # future changes |
|
|
938 | $self->on_face_change ($num => $cb) |
817 | } |
939 | } |
818 | |
940 | |
819 | sub sound_play { |
941 | sub sound_play { |
820 | my ($self, $x, $y, $soundnum, $type) = @_; |
942 | my ($self, $type, $face, $dx, $dy, $vol) = @_; |
821 | warn "sound play <@_>\n";#d# |
|
|
822 | |
943 | |
823 | #$self->{sound_play}->($x, $y, $soundnum, $type); |
944 | &::audio_sound_play ($face, $dx, $dy, $vol) |
|
|
945 | unless $type & 1; # odd types are silent for future expansion |
824 | } |
946 | } |
825 | |
947 | |
826 | my $LAST_QUERY; # server is stupid, stupid, stupid |
948 | my $LAST_QUERY; # server is stupid, stupid, stupid |
827 | |
949 | |
828 | sub query { |
950 | sub query { |
… | |
… | |
893 | $self->{$type} = $text; |
1015 | $self->{$type} = $text; |
894 | } else { |
1016 | } else { |
895 | $self->logprint ("msg: ", $text); |
1017 | $self->logprint ("msg: ", $text); |
896 | return if $color < 0; # negative color == ignore if not understood |
1018 | return if $color < 0; # negative color == ignore if not understood |
897 | |
1019 | |
898 | my $fg = $CF_COLOR[$color % @CF_COLOR]; |
1020 | my $fg = $CF_COLOR[$color & NDI_COLOR_MASK] || [1, 0, 0]; |
899 | |
1021 | |
900 | ## try to create single paragraphs of multiple lines sent by the server |
1022 | ## try to create single paragraphs of multiple lines sent by the server |
901 | # no longer neecssary with TRT servers |
1023 | # no longer neecssary with TRT servers |
902 | #$text =~ s/(?<=\S)\n(?=\w)/ /g; |
1024 | #$text =~ s/(?<=\S)\n(?=\w)/ /g; |
903 | |
1025 | |
904 | ::message ({ fg => $fg, markup => $_, type => $type, extra => [@extra] }) |
1026 | ::message ({ |
|
|
1027 | fg => $fg, |
|
|
1028 | markup => $_, |
|
|
1029 | type => $type, |
|
|
1030 | extra => [@extra], |
|
|
1031 | color_flags => $color |
905 | for split /\n/, $text; |
1032 | }) for split /\n/, $text; |
906 | |
1033 | |
907 | $self->{statusbox}->add ($text, |
1034 | $self->{statusbox}->add ($text, |
908 | group => $text, |
1035 | group => $text, |
909 | fg => $fg, |
1036 | fg => $fg, |
910 | timeout => $color >= 2 ? 180 : 10, |
1037 | timeout => $color >= 2 ? 180 : 10, |
… | |
… | |
966 | $self->{map_widget}->clr_commands; |
1093 | $self->{map_widget}->clr_commands; |
967 | |
1094 | |
968 | ::stop_game (); |
1095 | ::stop_game (); |
969 | } |
1096 | } |
970 | |
1097 | |
971 | sub image_info { |
|
|
972 | my ($self, $numfaces) = @_; |
|
|
973 | |
|
|
974 | $self->{num_faces} = $numfaces; |
|
|
975 | $self->{face_prefetch} = [1 .. $numfaces]; |
|
|
976 | $self->face_prefetch; |
|
|
977 | } |
|
|
978 | |
|
|
979 | sub face_prefetch { |
|
|
980 | my ($self) = @_; |
|
|
981 | |
|
|
982 | return unless $::CFG->{face_prefetch}; |
|
|
983 | |
|
|
984 | if ($self->{num_faces}) { |
|
|
985 | return if @{ $self->{send_queue} || [] }; |
|
|
986 | my $todo = @{ $self->{face_prefetch} } |
|
|
987 | or return; |
|
|
988 | |
|
|
989 | my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, (); |
|
|
990 | |
|
|
991 | $self->send ("requestinfo image_sums $face $face"); |
|
|
992 | |
|
|
993 | $self->{statusbox}->add (CFPlus::asxml "prefetching $todo", |
|
|
994 | group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]); |
|
|
995 | } elsif (!exists $self->{num_faces}) { |
|
|
996 | $self->send ("requestinfo image_info"); |
|
|
997 | |
|
|
998 | $self->{num_faces} = 0; |
|
|
999 | |
|
|
1000 | $self->{statusbox}->add (CFPlus::asxml "starting to prefetch", |
|
|
1001 | group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]); |
|
|
1002 | } |
|
|
1003 | } |
|
|
1004 | |
|
|
1005 | sub update_floorbox { |
1098 | sub update_floorbox { |
1006 | $CFPlus::UI::ROOT->on_refresh ($::FLOORBOX => sub { |
1099 | $CFPlus::UI::ROOT->on_refresh ($::FLOORBOX => sub { |
1007 | return unless $::CONN; |
1100 | return unless $::CONN; |
1008 | |
1101 | |
1009 | $::FLOORBOX->clear; |
1102 | $::FLOORBOX->clear; |
… | |
… | |
1038 | } |
1131 | } |
1039 | |
1132 | |
1040 | $::FLOORBOX->add_at (@add); |
1133 | $::FLOORBOX->add_at (@add); |
1041 | }); |
1134 | }); |
1042 | |
1135 | |
1043 | $::WANT_REFRESH++; |
1136 | $::WANT_REFRESH->start; |
1044 | } |
1137 | } |
1045 | |
1138 | |
1046 | sub set_opencont { |
1139 | sub set_opencont { |
1047 | my ($conn, $tag, $name) = @_; |
1140 | my ($conn, $tag, $name) = @_; |
1048 | $conn->{open_container} = $tag; |
1141 | $conn->{open_container} = $tag; |
1049 | update_floorbox; |
1142 | update_floorbox; |
1050 | |
1143 | |
1051 | $::INV_RIGHT_HB->clear (); |
1144 | $::INVR_HB->clear (); |
1052 | $::INV_RIGHT_HB->add (new CFPlus::UI::Label align => 0, expand => 1, text => $name); |
1145 | $::INVR_HB->add (new CFPlus::UI::Label align => 0, expand => 1, text => $name); |
1053 | |
1146 | |
1054 | if ($tag != 0) { # Floor isn't closable, is it? |
1147 | if ($tag != 0) { # Floor isn't closable, is it? |
1055 | $::INV_RIGHT_HB->add (new CFPlus::UI::Button |
1148 | $::INVR_HB->add (new CFPlus::UI::Button |
1056 | text => "Close container", |
1149 | text => "Close container", |
1057 | tooltip => "Close the currently open container (if one is open)", |
1150 | tooltip => "Close the currently open container (if one is open)", |
1058 | on_activate => sub { |
1151 | on_activate => sub { |
1059 | $::CONN->send ("apply $tag") # $::CONN->{open_container}") |
1152 | $::CONN->send ("apply $tag") # $::CONN->{open_container}") |
1060 | if $tag != 0; |
1153 | if $tag != 0; |
1061 | #if $CONN->{open_container} != 0; |
|
|
1062 | 0 |
1154 | 0 |
1063 | }, |
1155 | }, |
1064 | ); |
1156 | ); |
1065 | } |
1157 | } |
1066 | |
1158 | |