ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.139
Committed: Wed Apr 19 00:47:35 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.138: +12 -8 lines
Log Message:
*** empty log message ***

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