ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.145
Committed: Wed Apr 19 20:46:45 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.144: +14 -37 lines
Log Message:
got rid of base SDL perl module

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