ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.153
Committed: Thu Apr 20 09:13:32 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.152: +20 -22 lines
Log Message:
bg music loop

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