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