ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.141
Committed: Wed Apr 19 06:21:07 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.140: +17 -16 lines
Log Message:
*** empty log message ***

File Contents

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