ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.154
Committed: Thu Apr 20 16:29:40 2006 UTC (18 years, 1 month ago) by elmex
Branch: MAIN
Changes since 1.153: +30 -10 lines
Log Message:
implemented stats view

File Contents

# User Rev Content
1 root 1.1 #!/opt/bin/perl
2 root 1.25
3 root 1.2 use strict;
4 root 1.25 use utf8;
5 root 1.2
6 root 1.87 use Time::HiRes 'time';
7     use Event;
8 root 1.13
9 elmex 1.11 use Crossfire;
10 root 1.2 use Crossfire::Protocol;
11    
12 root 1.116 use Compress::LZF;
13    
14 root 1.67 use CFClient;
15 root 1.72 use CFClient::UI;
16 root 1.141 use CFClient::MapWidget;
17 elmex 1.10
18 root 1.63 our $VERSION = '0.1';
19    
20 root 1.96 my $MAX_FPS = 60;
21 root 1.90 my $MIN_FPS = 5; # unused as of yet
22 root 1.63
23 root 1.112 our $META_SERVER = "crossfire.real-time.com:13326";
24    
25 root 1.116 our $FACEMAP;
26     our $TILECACHE;
27     our $MAPCACHE;
28 root 1.19
29 root 1.87 our $LAST_REFRESH;
30     our $NOW;
31    
32 elmex 1.10 our $CFG;
33 root 1.13 our $CONN;
34 root 1.85 our $FAST; # fast, low-quality mode, possibly useful for software-rendering
35 root 1.2
36 root 1.75 our @SDL_MODES;
37 root 1.30 our $WIDTH;
38     our $HEIGHT;
39     our $FULLSCREEN;
40 root 1.99 our $FONTSIZE;
41 root 1.30
42 root 1.95 our $MAP;
43 root 1.69 our $MAPWIDGET;
44 root 1.112 our $BUTTONBAR;
45     our $LOGVIEW;
46     our $CONSOLE;
47     our $METASERVER;
48 root 1.57
49 elmex 1.125 our $GAUGES;
50 elmex 1.154 our $STATWIDS;
51 elmex 1.125
52 root 1.86 our $SDL_ACTIVE;
53 root 1.13 our %SDL_CB;
54 root 1.18
55 root 1.134 our $SDL_MIXER;
56     our @SOUNDS; # event => file mapping
57     our %AUDIO_CHUNKS; # audio files
58    
59 root 1.30 our $ALT_ENTER_MESSAGE;
60 root 1.51 our $STATUS_LINE;
61 root 1.64 our $DEBUG_STATUS;
62 root 1.30
63 root 1.82 sub status {
64     $STATUS_LINE->set_text ($_[0]);
65 root 1.128 $STATUS_LINE->move (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $STATUS_LINE->{h});
66 root 1.82 }
67    
68     sub debug {
69     $DEBUG_STATUS->set_text ($_[0]);
70 root 1.128 $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0, $DEBUG_STATUS->{w}, $DEBUG_STATUS->{h});
71 root 1.82 }
72    
73 root 1.84 sub start_game {
74 root 1.85 status "logging in...";
75    
76 root 1.106 my $mapsize = List::Util::min 32, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
77 root 1.84
78 root 1.116 $MAPCACHE = CFClient::db_table "mapcache_$CFG->{host}";
79    
80 root 1.95 $MAP = new CFClient::Map $mapsize, $mapsize;
81 root 1.112
82     my ($host, $port) = split /:/, $CFG->{host};
83 root 1.95
84 root 1.84 $CONN = new conn
85 root 1.112 host => $host,
86     port => $port || 13327,
87 root 1.84 user => $CFG->{user},
88     pass => $CFG->{password},
89     mapw => $mapsize,
90     maph => $mapsize,
91     ;
92    
93 root 1.85 status "login successful";
94    
95 root 1.84 CFClient::lowdelay fileno $CONN->{fh};
96     }
97    
98     sub stop_game {
99     undef $CONN;
100     }
101    
102 root 1.111 sub client_setup {
103 root 1.99 my $dialog = new CFClient::UI::FancyFrame
104 root 1.150 title => "Client Setup",
105 root 1.81 child => (my $vbox = new CFClient::UI::VBox);
106     $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
107    
108 root 1.140 $table->add (0, 0, new CFClient::UI::Label valign => 0, align => 1, text => "Video Mode");
109 root 1.81 $table->add (1, 0, my $hbox = new CFClient::UI::HBox);
110    
111     $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]);
112 root 1.150 $hbox->add (my $mode_label = new CFClient::UI::Label align => 0, valign => 0, height => 0.8, template => "9999x9999");
113 root 1.81
114     $mode_slider->connect (changed => sub {
115     my ($self, $value) = @_;
116    
117     $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value;
118     $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]});
119     });
120     $mode_slider->emit (changed => $mode_slider->{range}[0]);
121 root 1.82
122 root 1.140 $table->add (0, 1, new CFClient::UI::Label valign => 0, align => 1, text => "Fullscreen");
123 root 1.85 $table->add (1, 1, new CFClient::UI::CheckBox state => $CFG->{fullscreen}, connect_changed => sub {
124     my ($self, $value) = @_;
125     $CFG->{fullscreen} = $value;
126     });
127    
128 root 1.140 $table->add (0, 2, new CFClient::UI::Label valign => 0, align => 1, text => "Fast & Ugly");
129 root 1.89 $table->add (1, 2, new CFClient::UI::CheckBox state => $CFG->{fast}, connect_changed => sub {
130     my ($self, $value) = @_;
131     $CFG->{fast} = $value;
132     });
133    
134 root 1.140 $table->add (0, 3, new CFClient::UI::Label valign => 0, align => 1, text => "Fog of War");
135 root 1.97 $table->add (1, 3, new CFClient::UI::CheckBox state => $CFG->{fow_enable}, connect_changed => sub {
136     my ($self, $value) = @_;
137     $CFG->{fow_enable} = $value;
138     });
139    
140 root 1.140 $table->add (0, 4, new CFClient::UI::Label valign => 0, align => 1, text => "FoW Intensity");
141 root 1.97 $table->add (1, 4, new CFClient::UI::Slider range => [$CFG->{fow_intensity}, 0, 1 + 0.001, 0.001], connect_changed => sub {
142 root 1.90 my ($self, $value) = @_;
143     $CFG->{fow_intensity} = $value;
144     });
145    
146 root 1.140 $table->add (0, 5, new CFClient::UI::Label valign => 0, align => 1, text => "FoW Smooth");
147 root 1.97 $table->add (1, 5, new CFClient::UI::CheckBox state => $CFG->{fow_smooth}, connect_changed => sub {
148 root 1.91 my ($self, $value) = @_;
149     $CFG->{fow_smooth} = $value;
150 root 1.132 status "Fog of War smoothing requires OpenGL 1.2 or higher" if $CFClient::GL_VERSION < 1.2;
151 root 1.91 });
152    
153 root 1.141 $table->add (0, 6, new CFClient::UI::Label valign => 0, align => 1, text => "GUI Fontsize");
154     $table->add (1, 6, new CFClient::UI::Slider range => [$CFG->{gui_fontsize}, 0.7, 1.7, 0.1], connect_changed => sub {
155     $CFG->{gui_fontsize} = 0.1 * int $_[1] * 10;
156 root 1.140 # $FONTSIZE = int $HEIGHT / 40 * $CFG->{gui_fontsize};
157     });
158    
159 root 1.141 $table->add (0, 7, new CFClient::UI::Label valign => 0, align => 1, text => "Server Log Fontsize");
160     $table->add (1, 7, new CFClient::UI::Slider range => [$CFG->{log_fontsize}, 0.7, 1.7, 0.1], connect_changed => sub {
161     $LOGVIEW->set_fontsize ($CFG->{log_fontsize} = 0.1 * int $_[1] * 10);
162 root 1.105 });
163    
164 root 1.140 $table->add (1, 8, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub {
165 root 1.134 video_shutdown ();
166     video_init ();
167 root 1.82 });
168 root 1.111
169 root 1.140 $table->add (0, 9, new CFClient::UI::Label valign => 0, align => 1, text => "Audio Enable");
170     $table->add (1, 9, new CFClient::UI::CheckBox state => $CFG->{audio_enable}, connect_changed => sub {
171     $CFG->{audio_enable} = $_[1];
172     });
173     # $table->add (0, 9, new CFClient::UI::Label valign => 0, align => 1, text => "Effects Volume");
174     # $table->add (1, 8, new CFClient::UI::Slider range => [$CFG->{effects_volume}, 0, 128, 1], connect_changed => sub {
175     # $CFG->{effects_volume} = $_[1];
176     # });
177 root 1.141 $table->add (0, 10, new CFClient::UI::Label valign => 0, align => 1, text => "Background Music");
178 root 1.140 $table->add (1, 10, my $hbox = new CFClient::UI::HBox);
179     $hbox->add (new CFClient::UI::CheckBox expand => 1, state => $CFG->{bgm_enable}, connect_changed => sub {
180     $CFG->{bgm_enable} = $_[1];
181     });
182 root 1.146 $hbox->add (new CFClient::UI::Slider expand => 1, range => [$CFG->{bgm_volume}, 0, 1, 0.1], connect_changed => sub {
183 root 1.140 $CFG->{bgm_volume} = $_[1];
184 root 1.149 CFClient::MixMusic::volume $_[1] * 128;
185 elmex 1.137 });
186 root 1.140
187     $table->add (1, 11, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub {
188 elmex 1.137 audio_shutdown ();
189     audio_init ();
190     });
191    
192 root 1.111 $dialog
193     }
194    
195 elmex 1.154 sub make_stats_window {
196     my $tgw = new CFClient::UI::FancyFrame (x => $WIDTH * 2/3, y => 0, title => "Stats");
197     $tgw->add (my $vb = new CFClient::UI::VBox ());
198     $vb->add ($STATWIDS->{stat} = new CFClient::UI::Label valign => 0, align => -1);
199     $vb->add (my $hb2 = new CFClient::UI::HBox);
200     $hb2->add (my $hg = new CFClient::UI::Gauge (type => 'hp'));
201     $hb2->add (my $mg = new CFClient::UI::Gauge (type => 'mana'));
202     $hb2->add (my $gg = new CFClient::UI::Gauge (type => 'grace'));
203     $hb2->add (my $fg = new CFClient::UI::Gauge (type => 'food'));
204     $GAUGES = { food => $fg, mana => $mg, hp => $hg, grace => $gg };
205     $tgw
206     }
207    
208     sub update_stats_window {
209     my ($stats) = @_;
210    
211     $STATWIDS->{stat}->set_text (
212     "Title: $stats->{21}\nExp.: " . ($stats->{11} || $stats->{28})
213     . " Level: $stats->{12}\n"
214     . (sprintf "S%d D%d Co%d I%d W%d P%d Ch%2d, Wc%d Ac%d\n",
215     @$stats{5,8,9,6,7,22,10, 13, 14})
216     . (sprintf "Dam %d Arm %d Speed %.2f/%.2f",
217     @$stats{15,16,17,19})
218     );
219     }
220    
221 root 1.112 sub metaserver_dialog {
222     my $dialog = new CFClient::UI::FancyFrame
223 root 1.150 title => "Metaserver",
224 root 1.112 child => (my $vbox = new CFClient::UI::VBox);
225    
226     $vbox->add ($dialog->{table} = new CFClient::UI::Table);
227    
228     $dialog
229     }
230    
231     sub update_metaserver {
232 root 1.114 my ($HOST) = @_;
233    
234 root 1.112 status "fetching metaserver list...";
235    
236     my $buf;
237    
238     my $fh = new IO::Socket::INET PeerHost => $META_SERVER, Blocking => 0;
239    
240     Event->io (fd => $fh, poll => 'r', cb => sub {
241     my $res = sysread $fh, $buf, 8192, length $buf;
242    
243     if (!defined $res) {
244     $_[0]->w->cancel;
245     status "metaserver: $!";
246     } elsif ($res == 0) {
247     $_[0]->w->cancel;
248     status "server list retrieved";
249 root 1.113
250     my $table = $METASERVER->{table};
251    
252     $table->clear;
253    
254 root 1.114 my @col = qw(Use #Users Host Uptime Version Description);
255 root 1.113 $table->add ($_, 0, new CFClient::UI::Label align => 0, fg => [1, 1, 0], text => $col[$_])
256     for 0 .. $#col;
257    
258     my @align = qw(1 0 1 1 -1);
259    
260     my $y = 0;
261 root 1.114 for my $m (sort { $b->[3] <=> $a->[3] } map [split /\|/], split /\015?\012/, $buf) {
262 root 1.113 my ($ip, $last, $host, $users, $version, $desc, $ibytes, $obytes, $uptime) = @$m;
263    
264     for ($desc) {
265     s/<br>/\n/gi;
266     s/<li>/\n· /gi;
267     s/<.*?>//sgi;
268     s/&/&amp;/g;
269     s/</&lt;/g;
270     s/>/&gt;/g;
271     }
272    
273     $uptime = sprintf "%dd %02d:%02d:%02d",
274     (int $m->[8] / 86400),
275     (int $m->[8] / 3600) % 24,
276     (int $m->[8] / 60) % 60,
277     $m->[8] % 60;
278    
279     $m = [$users, $host, $uptime, $version, $desc];
280    
281     $y++;
282 root 1.114
283     $table->add (0, $y, new CFClient::UI::VBox children => [
284     (new CFClient::UI::Button text => " ", connect_activate => sub {
285     $HOST->set_text ($CFG->{host} = $host);
286     }),
287     (new CFClient::UI::Empty expand => 1),
288     ]);
289    
290 root 1.140 $table->add ($_ + 1, $y, new CFClient::UI::Label align => $align[$_], text => $m->[$_], fontsize => 0.8)
291 root 1.113 for 0 .. $#$m;
292     }
293 root 1.112 }
294     });
295     }
296    
297 root 1.111 sub server_setup {
298     my $dialog = new CFClient::UI::FancyFrame
299 root 1.150 title => "Server Setup",
300 root 1.111 child => (my $vbox = new CFClient::UI::VBox);
301 root 1.81
302 root 1.82 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
303 root 1.141 $table->add (0, 2, new CFClient::UI::Label valign => 0, align => 1, text => "Host:Port");
304 root 1.112
305     {
306     $table->add (1, 2, my $vbox = new CFClient::UI::VBox);
307    
308 root 1.141 $vbox->add (my $HOST = new CFClient::UI::Entry expand => 1, text => $CFG->{host}, connect_changed => sub {
309 root 1.112 my ($self, $value) = @_;
310     $CFG->{host} = $value;
311     });
312    
313     $METASERVER = metaserver_dialog;
314 elmex 1.101
315 root 1.141 $vbox->add (new CFClient::UI::Flopper expand => 1, text => "Metaserver", other => $METASERVER, connect_open => sub {
316 root 1.114 update_metaserver $HOST;
317 root 1.112 });
318     }
319 root 1.81
320 root 1.141 $table->add (0, 4, new CFClient::UI::Label valign => 0, align => 1, text => "Username");
321 root 1.114 $table->add (1, 4, new CFClient::UI::Entry text => $CFG->{user}, connect_changed => sub {
322 elmex 1.101 my ($self, $value) = @_;
323     $CFG->{user} = $value;
324     });
325 root 1.81
326 root 1.141 $table->add (0, 5, new CFClient::UI::Label valign => 0, align => 1, text => "Password");
327 root 1.114 $table->add (1, 5, new CFClient::UI::Entry text => $CFG->{password}, hidden => 1, connect_changed => sub {
328 elmex 1.101 my ($self, $value) = @_;
329     $CFG->{password} = $value;
330     });
331    
332 root 1.141 $table->add (0, 6, new CFClient::UI::Label valign => 0, align => 1, text => "Def. say cmd");
333 elmex 1.101 $table->add (1, 6, my $saycmd = new CFClient::UI::Entry text => $CFG->{say_command}, connect_changed => sub {
334     my ($self, $value) = @_;
335     $CFG->{say_command} = $value;
336     });
337 root 1.81
338 root 1.141 $table->add (0, 7, new CFClient::UI::Label valign => 0, align => 1, text => "Map Size");
339 elmex 1.101 $table->add (1, 7, new CFClient::UI::Slider
340 root 1.81 req_w => 100,
341     range => [$CFG->{mapsize}, 10, 100 + 1, 1],
342     connect_changed => sub {
343     my ($self, $value) = @_;
344    
345     $CFG->{mapsize} = $self->{range}[0] = $value = int $value;
346     },
347     );
348    
349 elmex 1.101 $table->add (1, 8, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub {
350 root 1.84 start_game;
351 root 1.82 });
352    
353 root 1.98 $dialog
354 root 1.81 }
355 root 1.58
356 root 1.111 sub message_window {
357 root 1.99 my $window = new CFClient::UI::FancyFrame
358 root 1.150 title => "Messages",
359 root 1.99 border_bg => [1, 1, 1, 0.5],
360     bg => [0.3, 0.3, 0.3, 0.8],
361 root 1.124 user_w => int $::WIDTH / 3,
362     user_h => int $::HEIGHT / 5,
363 root 1.99 child => (my $vbox = new CFClient::UI::VBox);
364    
365 root 1.105 $vbox->add ($LOGVIEW = new CFClient::UI::TextView
366     expand => 1,
367     fontsize => $::CFG->{log_fontsize},
368     );
369    
370 root 1.122 $vbox->add (my $input = new CFClient::UI::Entry
371 elmex 1.118 connect_focus_in => sub {
372     my ($input, $prev_focus) = @_;
373    
374     delete $input->{refocus_map};
375    
376     if ($prev_focus == $MAPWIDGET && $input->{auto_activated}) {
377     $input->{refocus_map} = 1;
378     }
379     delete $input->{auto_activated};
380     },
381 root 1.116 connect_activate => sub {
382 elmex 1.100 my ($input, $text) = @_;
383     $input->set_text ('');
384    
385     if ($text =~ /^\/(.*)/) {
386 root 1.123 $::CONN->user_send ($1);
387 elmex 1.100 } else {
388 elmex 1.101 my $say_cmd = $::CFG->{say_command} || 'say';
389 root 1.123 $::CONN->user_send ("$say_cmd $text");
390 elmex 1.100 }
391 elmex 1.118 if ($input->{refocus_map}) {
392     delete $input->{refocus_map};
393     $MAPWIDGET->focus_in
394     }
395 root 1.116 },
396     connect_escape => sub {
397 elmex 1.102 $MAPWIDGET->focus_in
398 root 1.116 },
399     );
400 elmex 1.102
401     $CONSOLE = {
402     window => $window,
403     input => $input
404     };
405 root 1.99
406     $window
407     }
408    
409 root 1.89 sub sdl_init {
410 root 1.145 CFClient::SDL_Init
411 root 1.89 and die "SDL::Init failed!\n";
412     }
413    
414 root 1.134 sub video_init {
415 root 1.89 sdl_init;
416    
417 root 1.84 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
418     $FULLSCREEN = $CFG->{fullscreen};
419 root 1.89 $FAST = $CFG->{fast};
420 root 1.84
421 root 1.145 CFClient::SDL_SetVideoMode $WIDTH, $HEIGHT, $FULLSCREEN
422     or die "SDL_SetVideoMode failed!\n";
423 root 1.2
424 root 1.86 $SDL_ACTIVE = 1;
425    
426 root 1.87 $LAST_REFRESH = time - 0.01;
427 root 1.45
428 root 1.67 CFClient::gl_init;
429 root 1.30
430 root 1.140 $FONTSIZE = int $HEIGHT / 40 * $CFG->{gui_fontsize};
431 root 1.39
432 root 1.52 #############################################################################
433    
434 root 1.99 $DEBUG_STATUS = new CFClient::UI::Label padding => 0, z => 100;
435 root 1.140 $DEBUG_STATUS->show;
436 root 1.52
437 root 1.72 $STATUS_LINE = new CFClient::UI::Label
438 root 1.77 padding => 0,
439 root 1.140 y => $HEIGHT - $FONTSIZE * 1.8;
440     $STATUS_LINE->show;
441 root 1.51
442 root 1.72 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
443 root 1.123 padding => 0,
444 root 1.140 fontsize => 0.8,
445 root 1.123 markup => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
446 root 1.140 $ALT_ENTER_MESSAGE->show;
447     $ALT_ENTER_MESSAGE->move (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h});
448 root 1.30
449 root 1.141 $CFClient::UI::ROOT->add ($MAPWIDGET = new CFClient::MapWidget);
450 root 1.69 $MAPWIDGET->focus_in;
451 elmex 1.102 $MAPWIDGET->connect (activate_console => sub {
452 elmex 1.103 my ($mapwidget, $preset) = @_;
453    
454 elmex 1.102 if ($CONSOLE) {
455 elmex 1.118 $CONSOLE->{input}->{auto_activated} = 1;
456 elmex 1.102 $CONSOLE->{input}->focus_in;
457 elmex 1.103
458     if ($preset && $CONSOLE->{input}->get_text eq '') {
459     $CONSOLE->{input}->set_text ($preset);
460     }
461 elmex 1.102 }
462     });
463 root 1.81
464 root 1.111 $CFClient::UI::ROOT->add ($BUTTONBAR = new CFClient::UI::HBox);
465    
466     $BUTTONBAR->add (new CFClient::UI::Flopper text => "Client Setup", other => client_setup);
467     $BUTTONBAR->add (new CFClient::UI::Flopper text => "Server Setup", other => server_setup);
468     $BUTTONBAR->add (new CFClient::UI::Flopper text => "Message Window", other => message_window);
469    
470     $BUTTONBAR->add (new CFClient::UI::Button text => "Save Config", connect_activate => sub {
471     CFClient::write_cfg "$Crossfire::VARDIR/pclientrc";
472     status "Configuration Saved";
473     });
474 root 1.98
475 root 1.119 $BUTTONBAR->{children}[1]->emit ("activate"); # pop up server setup
476 elmex 1.125
477 root 1.126
478 elmex 1.154 $CFClient::UI::ROOT->add (make_stats_window);
479 root 1.2 }
480    
481 root 1.134 sub video_shutdown {
482 root 1.111 $CFClient::UI::ROOT->{children} = [];
483 root 1.86 undef $SDL_ACTIVE;
484 root 1.134 }
485    
486 root 1.153 my @bgmusic = qw(game1.ogg game2.ogg game3.ogg game5.ogg game6.ogg ross1.ogg ross2.ogg ross3.ogg ross4.ogg ross5.ogg); #d#
487 root 1.135 my $bgmusic;#TODO#hack#d#
488    
489 root 1.153 sub audio_music_finished {
490     return unless $CFG->{bgm_enable};
491    
492     # TODO: hack, do play loop and mood music
493     $bgmusic = new_from_file CFClient::MixMusic CFClient::find_rcfile "music/$bgmusic[0]";
494     $bgmusic->play (0);
495    
496     push @bgmusic, shift @bgmusic;
497     }
498    
499 root 1.134 sub audio_init {
500 root 1.139 if ($CFG->{audio_enable}) {
501 root 1.134 if (open my $fh, "<:utf8", CFClient::find_rcfile "sounds/config") {
502 root 1.146 $SDL_MIXER = !CFClient::Mix_OpenAudio;
503     CFClient::Mix_AllocateChannels 8;
504 root 1.149 CFClient::MixMusic::volume $CFG->{bgm_volume} * 128;
505 root 1.134
506 root 1.153 audio_music_finished;
507 root 1.135
508 root 1.134 while (<$fh>) {
509     next if /^\s*#/;
510     next if /^\s*$/;
511    
512     my ($file, $volume, $event) = split /\s+/, $_, 3;
513    
514     push @SOUNDS, "$volume,$file";
515    
516     $AUDIO_CHUNKS{"$volume,$file"} ||= do {
517 root 1.146 my $chunk = new_from_file CFClient::MixChunk CFClient::find_rcfile "sounds/$file";
518 root 1.134 $chunk->volume ($volume * 128 / 100);
519     $chunk
520     };
521     }
522     } else {
523     status "unable to open sound config: $!";
524     }
525     }
526     }
527    
528     sub audio_shutdown {
529 root 1.146 CFClient::Mix_CloseAudio if $SDL_MIXER;
530 root 1.134 undef $SDL_MIXER;
531     @SOUNDS = ();
532     %AUDIO_CHUNKS = ();
533 root 1.62 }
534    
535 root 1.87 my %animate_object;
536     my $animate_timer;
537    
538     my $want_refresh;
539     my $can_refresh;
540    
541     my $fps = 9;
542    
543 root 1.30 sub force_refresh {
544 root 1.87 $fps = $fps * 0.95 + 1 / ($NOW - $LAST_REFRESH) * 0.05;
545     debug sprintf "%3.2f", $fps;
546    
547 root 1.96 $want_refresh = 0;
548 root 1.87 $can_refresh = 0;
549    
550 root 1.111 $CFClient::UI::ROOT->draw;
551 root 1.1
552 root 1.148 CFClient::SDL_GL_SwapBuffers;
553 root 1.87
554     $LAST_REFRESH = $NOW;
555 root 1.1 }
556    
557 root 1.87 my $refresh_watcher = Event->timer (after => 0, hard => 1, interval => 1 / $MAX_FPS, cb => sub {
558     $NOW = time;
559    
560 root 1.147 ($SDL_CB{$_->{type}} || sub { warn "unhandled event $_->{type}" })->($_)
561     for CFClient::SDL_PollEvent;
562 root 1.87
563     if (%animate_object) {
564     $_->animate ($LAST_REFRESH - $NOW) for values %animate_object;
565     $want_refresh++;
566     }
567    
568     if ($want_refresh) {
569     force_refresh;
570     } else {
571     $can_refresh = 1;
572     }
573     });
574 root 1.64
575 root 1.30 sub refresh {
576 root 1.87 $want_refresh++;
577 root 1.30 }
578    
579 root 1.45 sub animation_start {
580     my ($widget) = @_;
581 root 1.87 $animate_object{$widget} = $widget;
582 root 1.45 }
583    
584     sub animation_stop {
585     my ($widget) = @_;
586 root 1.87 delete $animate_object{$widget};
587 root 1.45 }
588    
589 root 1.2 @conn::ISA = Crossfire::Protocol::;
590 root 1.1
591 elmex 1.125 sub conn::stats_update {
592     my ($self, $stats) = @_;
593    
594     # i love text protocols!!!
595     # FIXME: the stats are somehow weird
596     my $hp = $stats->{1};
597     my $hp_m = $stats->{2};
598     my $sp = $stats->{3};
599     my $sp_m = $stats->{4};
600     my $fo = $stats->{18};
601 root 1.150 my $fo_m = 999;
602 elmex 1.125 my $gr = $stats->{23};
603     my $gr_m = $stats->{24};
604    
605     #d# warn "DATA $hp $hp_m $sp $sp_m $fo $fo_m $gr $gr_m\n";
606 root 1.150 $GAUGES->{hp} ->set_value ($hp, $hp_m);
607     $GAUGES->{mana} ->set_value ($sp, $sp_m);
608     $GAUGES->{food} ->set_value ($fo, $fo_m);
609 elmex 1.125 $GAUGES->{grace}->set_value ($gr, $gr_m);
610 elmex 1.154
611     update_stats_window ($stats);
612 elmex 1.125 }
613    
614 root 1.89 sub conn::user_send {
615 root 1.88 my ($self, $command) = @_;
616    
617 root 1.123 $self->send_command ($command);
618 root 1.88 status $command;
619     }
620    
621 root 1.119 sub conn::map_scroll {
622     my ($self, $dx, $dy) = @_;
623    
624     $MAP->scroll ($dx, $dy);
625     }
626    
627 root 1.94 sub conn::feed_map1a {
628     my ($self, $data) = @_;
629    
630 root 1.95 # $self->Crossfire::Protocol::feed_map1a ($data);
631 root 1.1
632 root 1.95 $MAP->map1a_update ($data);
633 root 1.69 $MAPWIDGET->update;
634 root 1.1 }
635    
636 root 1.116 sub conn::flush_map {
637     my ($self) = @_;
638    
639     my $map_info = delete $self->{map_info}
640     or return;
641    
642     my ($hash, $x, $y, $w, $h) = @$map_info;
643    
644     my $data = $MAP->get_rect ($x, $y, $w, $h);
645     $MAPCACHE->put ($hash => Compress::LZF::compress $data);
646 root 1.152 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
647 root 1.116 }
648 root 1.1
649 root 1.2 sub conn::map_clear {
650 root 1.1 my ($self) = @_;
651    
652 root 1.116 $self->flush_map;
653 root 1.150 delete $self->{neigh_map};
654 root 1.116
655 root 1.95 $MAP->clear;
656 root 1.1 }
657    
658 root 1.116
659 root 1.119 sub conn::load_map($$$) {
660     my ($self, $hash, $x, $y) = @_;
661 root 1.115
662 root 1.116 if (defined (my $data = $MAPCACHE->get ($hash))) {
663     $data = Compress::LZF::decompress $data;
664 root 1.152 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
665 root 1.116 for my $id ($MAP->set_rect ($x, $y, $data)) {
666     my $data = $TILECACHE->get ($id)
667     or next;
668    
669     $self->set_texture ($id => $data);
670     }
671     }
672 root 1.115 }
673    
674 root 1.152 # this method does a "flood fill" into every tile direction
675     # it assumes that tiles are arranged in a rectangular grid,
676     # i.e. a map is the same as the left of the right map etc.
677     # failure to comply are harmless and result in display errors
678     # at worst.
679 root 1.119 sub conn::flood_fill {
680 root 1.150 my ($self, $gx, $gy, $path, $hash, $flags) = @_;
681 root 1.119
682 root 1.121 # the server does not allow map paths > 6
683 root 1.120 return if 6 <= length $path;
684    
685 root 1.150 my ($x0, $y0, $x1, $y1) = @{$self->{neigh_rect}};
686    
687     for (
688     [1, 0, -1],
689     [2, 1, 0],
690     [3, 0, 1],
691     [4, -1, 0],
692     ) {
693     my ($tile, $dx, $dy) = @$_;
694    
695     my $gx = $gx + $dx;
696     my $gy = $gy + $dy;
697    
698 root 1.119 next unless $flags & (1 << ($tile - 1));
699 root 1.150 next if $self->{neigh_grid}{$gx, $gy}++;
700 root 1.119
701 root 1.150 my $neigh = $self->{neigh_map}{$hash} ||= [];
702     if (my $info = $neigh->[$tile]) {
703     my ($flags, $x, $y, $w, $h, $hash) = @$info;
704 root 1.119
705 root 1.150 $self->flood_fill ($gx, $gy, "$path$tile", $hash, $flags)
706     if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
707    
708     } else {
709     $self->send_mapinfo ("spatial $path$tile", sub {
710     my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
711 root 1.119
712 root 1.150 return if $mode ne "spatial";
713 root 1.119
714 root 1.150 $x += $MAP->ox;
715     $y += $MAP->oy;
716    
717     $self->load_map ($hash, $x, $y)
718     unless $self->{neigh_map}{$hash}[5]++;#d#
719 root 1.119
720 root 1.150 $neigh->[$tile] = [$flags, $x, $y, $w, $h, $hash];
721 root 1.119
722 root 1.150 $self->flood_fill ($gx, $gy, "$path$tile", $hash, $flags)
723     if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
724     });
725     }
726 root 1.119 }
727     }
728    
729     sub conn::map_change {
730     my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
731    
732     $self->flush_map;
733    
734     my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
735    
736     my $mapmapw = 250;
737     my $mapmaph = 250;
738 root 1.150
739     $self->{neigh_rect} = [
740 root 1.152 $ox - $mapmapw * 0.5, $oy - $mapmapw * 0.5,
741     $ox + $mapmapw * 0.5 + $w, $oy + $mapmapw * 0.5 + $h,
742 root 1.150 ];
743 root 1.119
744 root 1.150 delete $self->{neigh_grid};
745     $self->flood_fill (0, 0, "", $hash, $flags);
746 root 1.119
747     $x += $ox;
748     $y += $oy;
749    
750     $self->{map_info} = [$hash, $x, $y, $w, $h];
751    
752     $self->load_map ($hash, $x, $y);
753     }
754    
755 root 1.19 sub conn::face_find {
756 root 1.116 my ($self, $facenum, $face) = @_;
757    
758     my $hash = "$face->{chksum},$face->{name}";
759    
760     my $id = $FACEMAP->get ($hash);
761    
762     unless ($id) {
763     # create new id for face
764     # i love transactions
765     for (1..100) {
766     my $txn = $CFClient::DB_ENV->txn_begin;
767     my $status = $FACEMAP->db_get (id => $id, BerkeleyDB::DB_RMW);
768     if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
769     $id++;
770     if ($FACEMAP->put (id => $id) == 0
771     && $FACEMAP->put ($hash => $id) == 0) {
772     $txn->txn_commit;
773    
774     goto gotid;
775     }
776     }
777     $txn->abort;
778     }
779 root 1.19
780 root 1.116 CFClient::fatal "maximum number of transaction retries reached - database problems?";
781     }
782 root 1.114
783 root 1.116 gotid:
784     $face->{id} = $id;
785     $MAP->set_face ($facenum => $id);
786     $TILECACHE->get ($id)
787 root 1.19 }
788    
789 root 1.2 sub conn::face_update {
790 root 1.95 my ($self, $facenum, $face) = @_;
791 root 1.19
792 root 1.116 $TILECACHE->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes
793    
794     $self->set_texture ($face->{id} => delete $face->{image});
795     }
796 root 1.1
797 root 1.116 sub conn::set_texture {
798     my ($self, $id, $data) = @_;
799 root 1.95
800 root 1.116 $self->{texture}[$id] ||= do {
801     my $tex =
802     new_from_image CFClient::Texture
803     $data, minify => 1;
804    
805     $MAP->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
806     $MAPWIDGET->update;
807    
808     $tex
809     };
810 root 1.1 }
811    
812 root 1.134 sub conn::sound_play {
813     my ($self, $x, $y, $soundnum, $type) = @_;
814    
815 root 1.139 $SDL_MIXER
816     or return;
817    
818 root 1.134 my $chunk = $AUDIO_CHUNKS{$SOUNDS[$soundnum]}
819     or return;
820    
821 root 1.146 $chunk->play;
822 root 1.143 # warn "sound $x,$y,$soundnum,$type\n";#d#
823 root 1.134 }
824    
825 root 1.33 sub conn::query {
826     my ($self, $flags, $prompt) = @_;
827    
828 root 1.143 #TODO, display dialog with relevant information
829 root 1.33 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
830     }
831    
832 root 1.99 sub conn::drawinfo {
833     my ($self, $color, $text) = @_;
834    
835     my @color = (
836     [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
837     [1.00, 1.00, 1.00],
838 root 1.117 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
839 root 1.99 [1.00, 0.00, 0.00],
840     [1.00, 0.54, 0.00],
841     [0.11, 0.56, 1.00],
842     [0.93, 0.46, 0.00],
843     [0.18, 0.54, 0.34],
844     [0.56, 0.73, 0.56],
845     [0.80, 0.80, 0.80],
846     [0.55, 0.41, 0.13],
847     [0.99, 0.77, 0.26],
848     [0.74, 0.65, 0.41],
849     );
850    
851     $LOGVIEW->add_paragraph ($color[$color], $text);
852     }
853    
854 root 1.144 sub conn::spell_add {
855 root 1.143 my ($self, $spell) = @_;
856    
857 root 1.144 $MAPWIDGET->add_command ("invoke $spell->{name}", $spell->{message}, sub {
858     });
859     $MAPWIDGET->add_command ("cast $spell->{name}", $spell->{message}, sub {
860     });
861     }
862    
863     sub conn::spell_delete {
864     my ($self, $spell) = @_;
865     }
866    
867     sub conn::addme_success {
868     my ($self) = @_;
869    
870     for my $skill (values %{$self->{skill_info}}) {
871     $MAPWIDGET->add_command ("ready_skill $skill", "", sub {
872     });
873     $MAPWIDGET->add_command ("use_skill $skill", "", sub {
874     });
875     }
876 root 1.143 }
877    
878 root 1.87 %SDL_CB = (
879 root 1.145 CFClient::SDL_QUIT => sub {
880 root 1.87 Event::unloop -1;
881     },
882 root 1.145 CFClient::SDL_VIDEORESIZE => sub {
883 root 1.87 },
884 root 1.153 CFClient::SDL_VIDEOEXPOSE => \&refresh,
885     CFClient::SDL_ACTIVEEVENT => sub {
886     # printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
887 root 1.87 },
888 root 1.145 CFClient::SDL_KEYDOWN => sub {
889 root 1.147 if ($_[0]{mod} & CFClient::KMOD_ALT && $_[0]{sym} == 13) {
890 root 1.87 # alt-enter
891 root 1.134 video_shutdown;
892 root 1.99 $CFG->{fullscreen} = !$CFG->{fullscreen};
893 root 1.134 video_init;
894 root 1.87 } else {
895 root 1.147 CFClient::UI::feed_sdl_key_down_event ($_[0]);
896 elmex 1.23 }
897 root 1.87 },
898 root 1.153 CFClient::SDL_KEYUP => \&CFClient::UI::feed_sdl_key_up_event,
899     CFClient::SDL_MOUSEMOTION => \&CFClient::UI::feed_sdl_motion_event,
900     CFClient::SDL_MOUSEBUTTONDOWN => \&CFClient::UI::feed_sdl_button_down_event,
901     CFClient::SDL_MOUSEBUTTONUP => \&CFClient::UI::feed_sdl_button_up_event,
902     CFClient::SDL_USEREVENT => \&audio_music_finished,
903 root 1.87 );
904 elmex 1.23
905 root 1.1 #############################################################################
906    
907 root 1.131 $SIG{INT} = $SIG{TERM} = sub { exit };
908    
909 root 1.116 $TILECACHE = CFClient::db_table "tilecache";
910     $FACEMAP = CFClient::db_table "facemap";
911 root 1.114
912 root 1.67 CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
913 elmex 1.10
914 root 1.90 my %DEF_CFG = (
915 root 1.105 sdl_mode => 0,
916 root 1.90 width => 640,
917     height => 480,
918 root 1.105 fullscreen => 0,
919 root 1.90 fast => 0,
920 root 1.97 fow_enable => 1,
921 root 1.90 fow_intensity => 0.45,
922 root 1.92 fow_smooth => 0,
923 root 1.140 gui_fontsize => 1,
924 root 1.105 log_fontsize => 14,
925 root 1.90 mapsize => 100,
926     host => "crossfire.schmorp.de",
927 elmex 1.101 say_command => 'say',
928 root 1.139 audio_enable => 1,
929     bgm_enable => 1,
930 root 1.149 bgm_volume => 0.25,
931 root 1.90 );
932    
933     while (my ($k, $v) = each %DEF_CFG) {
934     $CFG->{$k} = $v unless exists $CFG->{$k};
935     }
936 elmex 1.12
937 root 1.89 sdl_init;
938 root 1.87
939 root 1.93 @SDL_MODES = reverse
940     grep $_->[0] >= 640 && $_->[1] >= 480,
941 root 1.145 CFClient::SDL_ListModes;
942 root 1.87
943 root 1.89 @SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)";
944    
945     $CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES;
946    
947 root 1.65 {
948 root 1.67 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
949 root 1.65
950 root 1.67 CFClient::add_font $_ for @fonts;
951     CFClient::set_font $fonts[0];
952 root 1.65 }
953 root 1.40
954 root 1.134 video_init;
955     audio_init;
956 root 1.122
957 root 1.87 Event::loop;
958 root 1.19
959 root 1.148 END { CFClient::SDL_Quit }
960 root 1.131
961 root 1.82