ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.152
Committed: Thu Apr 20 08:11:57 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.151: +9 -6 lines
Log Message:
map overview rectangle

File Contents

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