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