ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
(Generate patch)

Comparing deliantra/Deliantra-Client/bin/pclient (file contents):
Revision 1.108 by root, Sat Apr 15 01:13:47 2006 UTC vs.
Revision 1.136 by root, Tue Apr 18 06:34:30 2006 UTC

8 8
9use SDL; 9use SDL;
10use SDL::App; 10use SDL::App;
11use SDL::Event; 11use SDL::Event;
12use SDL::Surface; 12use SDL::Surface;
13
14use SDL::Mixer;
15use SDL::Sound;
16use SDL::Music;
17
13use SDL::OpenGL; 18use SDL::OpenGL;
14 19
15use Crossfire; 20use Crossfire;
16use Crossfire::Protocol; 21use Crossfire::Protocol;
17 22
23use Compress::LZF;
24
18use CFClient; 25use CFClient;
19use CFClient::UI; 26use CFClient::UI;
20 27
21our $VERSION = '0.1'; 28our $VERSION = '0.1';
22 29
23my $MAX_FPS = 60; 30my $MAX_FPS = 60;
24my $MIN_FPS = 5; # unused as of yet 31my $MIN_FPS = 5; # unused as of yet
25 32
33our $META_SERVER = "crossfire.real-time.com:13326";
34
35our $FACEMAP;
36our $TILECACHE;
26our $FACECACHE; 37our $MAPCACHE;
27 38
28our $LAST_REFRESH; 39our $LAST_REFRESH;
29our $NOW; 40our $NOW;
30 41
31our $CFG; 42our $CFG;
38our $FULLSCREEN; 49our $FULLSCREEN;
39our $FONTSIZE; 50our $FONTSIZE;
40 51
41our $MAP; 52our $MAP;
42our $MAPWIDGET; 53our $MAPWIDGET;
54our $BUTTONBAR;
55our $LOGVIEW;
56our $CONSOLE;
57our $METASERVER;
58
59our $GAUGES;
43 60
44our $SDL_ACTIVE; 61our $SDL_ACTIVE;
45our $SDL_EV; 62our $SDL_EV;
46our %SDL_CB; 63our %SDL_CB;
47 64
65our $SDL_MIXER;
66our @SOUNDS; # event => file mapping
67our %AUDIO_CHUNKS; # audio files
68
48our $ALT_ENTER_MESSAGE; 69our $ALT_ENTER_MESSAGE;
49our $STATUS_LINE; 70our $STATUS_LINE;
50our $DEBUG_STATUS; 71our $DEBUG_STATUS;
51our $BUTTONBAR;
52our $LOGVIEW;
53our $CONSOLE;
54 72
55sub status { 73sub status {
56 $STATUS_LINE->set_text ($_[0]); 74 $STATUS_LINE->set_text ($_[0]);
57 my ($w, $h) = $STATUS_LINE->size_request;
58 $STATUS_LINE->size_allocate (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $h, $w, $h); 75 $STATUS_LINE->move (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $STATUS_LINE->{h});
59} 76}
60 77
61sub debug { 78sub debug {
62 $DEBUG_STATUS->set_text ($_[0]); 79 $DEBUG_STATUS->set_text ($_[0]);
63 my ($w, $h) = $DEBUG_STATUS->size_request; 80 $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0, $DEBUG_STATUS->{w}, $DEBUG_STATUS->{h});
64 $DEBUG_STATUS->size_allocate ($WIDTH - $w, 0, $w, $h);
65} 81}
66 82
67sub start_game { 83sub start_game {
68 status "logging in..."; 84 status "logging in...";
69 85
70 my $mapsize = List::Util::min 32, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32; 86 my $mapsize = List::Util::min 32, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
71 87
88 $MAPCACHE = CFClient::db_table "mapcache_$CFG->{host}";
89
72 $MAP = new CFClient::Map $mapsize, $mapsize; 90 $MAP = new CFClient::Map $mapsize, $mapsize;
91
92 my ($host, $port) = split /:/, $CFG->{host};
73 93
74 $CONN = new conn 94 $CONN = new conn
75 host => $CFG->{host}, 95 host => $host,
76 port => $CFG->{port}, 96 port => $port || 13327,
77 user => $CFG->{user}, 97 user => $CFG->{user},
78 pass => $CFG->{password}, 98 pass => $CFG->{password},
79 mapw => $mapsize, 99 mapw => $mapsize,
80 maph => $mapsize, 100 maph => $mapsize,
81 ; 101 ;
87 107
88sub stop_game { 108sub stop_game {
89 undef $CONN; 109 undef $CONN;
90} 110}
91 111
92sub config_dialog { 112sub client_setup {
93 my $dialog = new CFClient::UI::FancyFrame 113 my $dialog = new CFClient::UI::FancyFrame
94 child => (my $vbox = new CFClient::UI::VBox); 114 child => (my $vbox = new CFClient::UI::VBox);
95 $vbox->add (new CFClient::UI::Label align => 0, text => "Client Setup"); 115 $vbox->add (new CFClient::UI::Label align => 0, text => "Client Setup");
96 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]); 116 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
97 117
135 155
136 $table->add (0, 5, new CFClient::UI::Label align => 1, text => "FoW Smooth"); 156 $table->add (0, 5, new CFClient::UI::Label align => 1, text => "FoW Smooth");
137 $table->add (1, 5, new CFClient::UI::CheckBox state => $CFG->{fow_smooth}, connect_changed => sub { 157 $table->add (1, 5, new CFClient::UI::CheckBox state => $CFG->{fow_smooth}, connect_changed => sub {
138 my ($self, $value) = @_; 158 my ($self, $value) = @_;
139 $CFG->{fow_smooth} = $value; 159 $CFG->{fow_smooth} = $value;
160 status "Fog of War smoothing requires OpenGL 1.2 or higher" if $CFClient::GL_VERSION < 1.2;
140 }); 161 });
141 162
142 $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Log Fontsize"); 163 $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Log Fontsize");
143 $table->add (1, 5, new CFClient::UI::Slider range => [$CFG->{log_fontsize}, 8, 30, 1], connect_changed => sub { 164 $table->add (1, 6, new CFClient::UI::Slider range => [$CFG->{log_fontsize}, 8, 30, 1], connect_changed => sub {
144 my ($self, $value) = @_; 165 my ($self, $value) = @_;
145 $LOGVIEW->set_fontsize ($CFG->{log_fontsize} = int $value); 166 $LOGVIEW->set_fontsize ($CFG->{log_fontsize} = int $value);
146 }); 167 });
147 168
148 $table->add (1, 6, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub { 169 $table->add (1, 7, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub {
149 destroy_screen (); 170 video_shutdown ();
150 init_screen (); 171 video_init ();
151 }); 172 });
173
174 $dialog
175}
176
177sub metaserver_dialog {
178 my $dialog = new CFClient::UI::FancyFrame
179 child => (my $vbox = new CFClient::UI::VBox);
180
181 $vbox->add ($dialog->{table} = new CFClient::UI::Table);
182
183 $dialog
184}
185
186sub update_metaserver {
187 my ($HOST) = @_;
188
189 status "fetching metaserver list...";
190
191 my $buf;
192
193 my $fh = new IO::Socket::INET PeerHost => $META_SERVER, Blocking => 0;
194
195 Event->io (fd => $fh, poll => 'r', cb => sub {
196 my $res = sysread $fh, $buf, 8192, length $buf;
197
198 if (!defined $res) {
199 $_[0]->w->cancel;
200 status "metaserver: $!";
201 } elsif ($res == 0) {
202 $_[0]->w->cancel;
203 status "server list retrieved";
204
205 my $table = $METASERVER->{table};
206
207 $table->clear;
208
209 my @col = qw(Use #Users Host Uptime Version Description);
210 $table->add ($_, 0, new CFClient::UI::Label align => 0, fg => [1, 1, 0], text => $col[$_])
211 for 0 .. $#col;
212
213 my @align = qw(1 0 1 1 -1);
214
215 my $y = 0;
216 for my $m (sort { $b->[3] <=> $a->[3] } map [split /\|/], split /\015?\012/, $buf) {
217 my ($ip, $last, $host, $users, $version, $desc, $ibytes, $obytes, $uptime) = @$m;
218
219 for ($desc) {
220 s/<br>/\n/gi;
221 s/<li>/\n· /gi;
222 s/<.*?>//sgi;
223 s/&/&amp;/g;
224 s/</&lt;/g;
225 s/>/&gt;/g;
226 }
227
228 $uptime = sprintf "%dd %02d:%02d:%02d",
229 (int $m->[8] / 86400),
230 (int $m->[8] / 3600) % 24,
231 (int $m->[8] / 60) % 60,
232 $m->[8] % 60;
233
234 $m = [$users, $host, $uptime, $version, $desc];
235
236 $y++;
237
238 $table->add (0, $y, new CFClient::UI::VBox children => [
239 (new CFClient::UI::Button text => " ", connect_activate => sub {
240 $HOST->set_text ($CFG->{host} = $host);
241 }),
242 (new CFClient::UI::Empty expand => 1),
243 ]);
244
245 $table->add ($_ + 1, $y, new CFClient::UI::Label align => $align[$_], text => $m->[$_], fontsize => $FONTSIZE * 0.8)
246 for 0 .. $#$m;
247 }
248 }
249 });
250}
251
252sub server_setup {
253 my $dialog = new CFClient::UI::FancyFrame
254 child => (my $vbox = new CFClient::UI::VBox);
152 255
153 $vbox->add (new CFClient::UI::Label align => 0, text => "Server Setup"); 256 $vbox->add (new CFClient::UI::Label align => 0, text => "Server Setup");
154 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]); 257 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
155 $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Host"); 258 $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Host:Port");
259
260 {
261 $table->add (1, 2, my $vbox = new CFClient::UI::VBox);
262
156 $table->add (1, 2, my $host = new CFClient::UI::Entry text => $CFG->{host}, connect_changed => sub { 263 $vbox->add (my $HOST = new CFClient::UI::Entry text => $CFG->{host}, connect_changed => sub {
157 my ($self, $value) = @_; 264 my ($self, $value) = @_;
158 $CFG->{host} = $value; 265 $CFG->{host} = $value;
159 }); 266 });
160 267
161 $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Port"); 268 $METASERVER = metaserver_dialog;
162 $table->add (1, 3, my $port = new CFClient::UI::Entry text => $CFG->{port}, connect_changed => sub { 269
163 my ($self, $value) = @_; 270 $vbox->add (new CFClient::UI::Flopper text => "Metaserver", other => $METASERVER, connect_open => sub {
164 $CFG->{port} = $value; 271 update_metaserver $HOST;
165 }); 272 });
273 }
166 274
167 $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Username"); 275 $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Username");
168 $table->add (1, 4, my $user = new CFClient::UI::Entry text => $CFG->{user}, connect_changed => sub { 276 $table->add (1, 4, new CFClient::UI::Entry text => $CFG->{user}, connect_changed => sub {
169 my ($self, $value) = @_; 277 my ($self, $value) = @_;
170 $CFG->{user} = $value; 278 $CFG->{user} = $value;
171 }); 279 });
172 280
173 $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Password"); 281 $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Password");
174 $table->add (1, 5, my $pass = new CFClient::UI::Entry text => $CFG->{password}, hidden => 1, connect_changed => sub { 282 $table->add (1, 5, new CFClient::UI::Entry text => $CFG->{password}, hidden => 1, connect_changed => sub {
175 my ($self, $value) = @_; 283 my ($self, $value) = @_;
176 $CFG->{password} = $value; 284 $CFG->{password} = $value;
177 }); 285 });
178 286
179 $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Def. say cmd"); 287 $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Def. say cmd");
195 303
196 $table->add (1, 8, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub { 304 $table->add (1, 8, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub {
197 start_game; 305 start_game;
198 }); 306 });
199 307
200 $vbox->add (my $hbox = new CFClient::UI::HBox);
201
202 $hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Save", connect_activate => sub {
203 CFClient::write_cfg "$Crossfire::VARDIR/pclientrc";
204 status "Configuration Saved";
205 });
206
207 $dialog 308 $dialog
208} 309}
209 310
210sub console_window { 311sub message_window {
211 my $window = new CFClient::UI::FancyFrame 312 my $window = new CFClient::UI::FancyFrame
212 border_bg => [1, 1, 1, 0.5], 313 border_bg => [1, 1, 1, 0.5],
213 bg => [0.3, 0.3, 0.3, 0.8], 314 bg => [0.3, 0.3, 0.3, 0.8],
214 user_w => $::WIDTH/4, 315 user_w => int $::WIDTH / 3,
215 user_h => $::HEIGHT, 316 user_h => int $::HEIGHT / 5,
216 child => (my $vbox = new CFClient::UI::VBox); 317 child => (my $vbox = new CFClient::UI::VBox);
217 318
218 $vbox->add ($LOGVIEW = new CFClient::UI::TextView 319 $vbox->add ($LOGVIEW = new CFClient::UI::TextView
219 expand => 1, 320 expand => 1,
220 fontsize => $::CFG->{log_fontsize}, 321 fontsize => $::CFG->{log_fontsize},
221 ); 322 );
222 323
223 $vbox->add (my $input = new CFClient::UI::LineEntry); 324 $vbox->add (my $input = new CFClient::UI::Entry
325 connect_focus_in => sub {
326 my ($input, $prev_focus) = @_;
327
328 delete $input->{refocus_map};
329
330 if ($prev_focus == $MAPWIDGET && $input->{auto_activated}) {
331 $input->{refocus_map} = 1;
332 }
333 delete $input->{auto_activated};
334 },
224 $input->connect (activate => sub { 335 connect_activate => sub {
225 my ($input, $text) = @_; 336 my ($input, $text) = @_;
226 $input->set_text (''); 337 $input->set_text ('');
227 338
228 if ($text =~ /^\/(.*)/) { 339 if ($text =~ /^\/(.*)/) {
229 $::CONN->user_send ("command $1"); 340 $::CONN->user_send ($1);
230 } else { 341 } else {
231 my $say_cmd = $::CFG->{say_command} || 'say'; 342 my $say_cmd = $::CFG->{say_command} || 'say';
232 $::CONN->user_send ("command $say_cmd $text"); 343 $::CONN->user_send ("$say_cmd $text");
233 } 344 }
345 if ($input->{refocus_map}) {
346 delete $input->{refocus_map};
347 $MAPWIDGET->focus_in
234 1 348 }
235 }); 349 },
236 $input->connect (escape => sub { 350 connect_escape => sub {
237 $MAPWIDGET->focus_in 351 $MAPWIDGET->focus_in
238 }); 352 },
239 $input->focus_in; 353 );
240 354
241 $CONSOLE = { 355 $CONSOLE = {
242 window => $window, 356 window => $window,
243 input => $input 357 input => $input
244 }; 358 };
245 359
246 $window 360 $window
247} 361}
248 362
249sub sdl_init { 363sub sdl_init {
250 SDL::Init SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE 364 #SDL::Init SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE
251 #SDL::Init SDL_INIT_AUDIO | SDL_INIT_VIDEO 365 SDL::Init SDL_INIT_AUDIO | SDL_INIT_VIDEO
252 and die "SDL::Init failed!\n"; 366 and die "SDL::Init failed!\n";
253} 367}
254 368
255sub init_screen { 369sub video_init {
256 sdl_init; 370 sdl_init;
257 371
258 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] }; 372 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
259 $FULLSCREEN = $CFG->{fullscreen}; 373 $FULLSCREEN = $CFG->{fullscreen};
260 $FAST = $CFG->{fast}; 374 $FAST = $CFG->{fast};
261 375
262 SDL::GLSetAttribute SDL_GL_RED_SIZE, 5; 376 SDL::GLSetAttribute SDL_GL_RED_SIZE, 5;
263 SDL::GLSetAttribute SDL_GL_GREEN_SIZE, 5; 377 SDL::GLSetAttribute SDL_GL_GREEN_SIZE, 5;
264 SDL::GLSetAttribute SDL_GL_BLUE_SIZE, 5; 378 SDL::GLSetAttribute SDL_GL_BLUE_SIZE, 5;
265 SDL::GLSetAttribute SDL_GL_ALPHA_SIZE, 0; 379 SDL::GLSetAttribute SDL_GL_ALPHA_SIZE, 1;
266 380
267 SDL::GLSetAttribute SDL_GL_ACCUM_RED_SIZE, 0; 381 SDL::GLSetAttribute SDL_GL_ACCUM_RED_SIZE, 0;
268 SDL::GLSetAttribute SDL_GL_ACCUM_GREEN_SIZE, 0; 382 SDL::GLSetAttribute SDL_GL_ACCUM_GREEN_SIZE, 0;
269 SDL::GLSetAttribute SDL_GL_ACCUM_BLUE_SIZE, 0; 383 SDL::GLSetAttribute SDL_GL_ACCUM_BLUE_SIZE, 0;
270 SDL::GLSetAttribute SDL_GL_ACCUM_ALPHA_SIZE, 0; 384 SDL::GLSetAttribute SDL_GL_ACCUM_ALPHA_SIZE, 0;
292 $FONTSIZE = int $HEIGHT / 40; 406 $FONTSIZE = int $HEIGHT / 40;
293 407
294 ############################################################################# 408 #############################################################################
295 409
296 $DEBUG_STATUS = new CFClient::UI::Label padding => 0, z => 100; 410 $DEBUG_STATUS = new CFClient::UI::Label padding => 0, z => 100;
297 $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS); 411 $CFClient::UI::ROOT->add ($DEBUG_STATUS);
298 412
299 $STATUS_LINE = new CFClient::UI::Label 413 $STATUS_LINE = new CFClient::UI::Label
300 padding => 0, 414 padding => 0,
301 y => $HEIGHT * 44 / 45 - $FONTSIZE; 415 y => $HEIGHT * 44 / 45 - $FONTSIZE;
302 $CFClient::UI::TOPLEVEL->add ($STATUS_LINE); 416 $CFClient::UI::ROOT->add ($STATUS_LINE);
303 417
304 $ALT_ENTER_MESSAGE = new CFClient::UI::Label 418 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
305 padding => 0, 419 padding => 0,
306 y => $HEIGHT * 44 / 45, 420 y => $HEIGHT * 44 / 45,
307 height => $HEIGHT / 45, 421 fontsize => $HEIGHT / 45,
308 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode"; 422 markup => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
309 $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE); 423 $CFClient::UI::ROOT->add ($ALT_ENTER_MESSAGE);
310 424
311 $CFClient::UI::TOPLEVEL->add ($MAPWIDGET = new CFClient::UI::MapWidget); 425 $CFClient::UI::ROOT->add ($MAPWIDGET = new CFClient::UI::MapWidget);
312 $MAPWIDGET->focus_in; 426 $MAPWIDGET->focus_in;
313 $MAPWIDGET->connect (activate_console => sub { 427 $MAPWIDGET->connect (activate_console => sub {
314 my ($mapwidget, $preset) = @_; 428 my ($mapwidget, $preset) = @_;
315 429
316 if ($CONSOLE) { 430 if ($CONSOLE) {
431 $CONSOLE->{input}->{auto_activated} = 1;
317 $CONSOLE->{input}->focus_in; 432 $CONSOLE->{input}->focus_in;
318 433
319 if ($preset && $CONSOLE->{input}->get_text eq '') { 434 if ($preset && $CONSOLE->{input}->get_text eq '') {
320 $CONSOLE->{input}->set_text ($preset); 435 $CONSOLE->{input}->set_text ($preset);
321 } 436 }
322 } 437 }
323 }); 438 });
324 439
325 $CFClient::UI::TOPLEVEL->add ($BUTTONBAR = new CFClient::UI::HBox); 440 $CFClient::UI::ROOT->add ($BUTTONBAR = new CFClient::UI::HBox);
326 441
327 $BUTTONBAR->add (my $setup = new CFClient::UI::Flopper x => 0, y => 0, text => "Setup", other => config_dialog, state => 1); 442 $BUTTONBAR->add (new CFClient::UI::Flopper text => "Client Setup", other => client_setup);
328 $BUTTONBAR->add (my $setup = new CFClient::UI::Flopper x => 0, y => 0, text => "Console", other => console_window); 443 $BUTTONBAR->add (new CFClient::UI::Flopper text => "Server Setup", other => server_setup);
329} 444 $BUTTONBAR->add (new CFClient::UI::Flopper text => "Message Window", other => message_window);
330 445
331sub destroy_screen { 446 $BUTTONBAR->add (new CFClient::UI::Button text => "Save Config", connect_activate => sub {
447 CFClient::write_cfg "$Crossfire::VARDIR/pclientrc";
448 status "Configuration Saved";
449 });
450
451 $BUTTONBAR->{children}[1]->emit ("activate"); # pop up server setup
452
453 my $tgw = new CFClient::UI::FancyFrame (x => $WIDTH - 300, y => 0);
454 $tgw->add (my $hbox = new CFClient::UI::HBox ());
455
456 $hbox->add (my $hg = new CFClient::UI::VGauge (gauge => 'hp'));
457 $hbox->add (my $mg = new CFClient::UI::VGauge (gauge => 'mana'));
458 $hbox->add (my $gg = new CFClient::UI::VGauge (gauge => 'grace'));
459 $hbox->add (my $fg = new CFClient::UI::VGauge (gauge => 'food'));
460
461 $GAUGES = { food => $fg, mana => $mg, hp => $hg, grace => $gg };
462 $CFClient::UI::ROOT->add ($tgw);
463}
464
465sub video_shutdown {
332 $CFClient::UI::TOPLEVEL->{children} = []; 466 $CFClient::UI::ROOT->{children} = [];
333 undef $SDL_ACTIVE; 467 undef $SDL_ACTIVE;
334 undef $SDL_EV; 468 undef $SDL_EV;
335 SDL::Quit; 469}
470
471my $bgmusic;#TODO#hack#d#
472
473sub audio_init {
474 if ($CFG->{sound} || 1) {
475 if (open my $fh, "<:utf8", CFClient::find_rcfile "sounds/config") {
476 $SDL_MIXER = new SDL::Mixer;
477 $SDL_MIXER->allocate_channels (8);
478
479 $bgmusic = new SDL::Music CFClient::find_rcfile "music/game3.ogg";
480 $SDL_MIXER->play_music ($bgmusic, -1);
481
482 while (<$fh>) {
483 next if /^\s*#/;
484 next if /^\s*$/;
485
486 my ($file, $volume, $event) = split /\s+/, $_, 3;
487
488 push @SOUNDS, "$volume,$file";
489
490 $AUDIO_CHUNKS{"$volume,$file"} ||= do {
491 my $chunk = new SDL::Sound CFClient::find_rcfile "sounds/$file";
492 $chunk->volume ($volume * 128 / 100);
493 $chunk
494 };
495 }
496 } else {
497 status "unable to open sound config: $!";
498 }
499 }
500}
501
502sub audio_shutdown {
503 undef $SDL_MIXER;
504 @SOUNDS = ();
505 %AUDIO_CHUNKS = ();
336} 506}
337 507
338my %animate_object; 508my %animate_object;
339my $animate_timer; 509my $animate_timer;
340 510
348 debug sprintf "%3.2f", $fps; 518 debug sprintf "%3.2f", $fps;
349 519
350 $want_refresh = 0; 520 $want_refresh = 0;
351 $can_refresh = 0; 521 $can_refresh = 0;
352 522
353 $CFClient::UI::TOPLEVEL->draw; 523 $CFClient::UI::ROOT->draw;
354 524
355 SDL::GLSwapBuffers; 525 SDL::GLSwapBuffers;
356 526
357 $LAST_REFRESH = $NOW; 527 $LAST_REFRESH = $NOW;
358} 528}
389 delete $animate_object{$widget}; 559 delete $animate_object{$widget};
390} 560}
391 561
392@conn::ISA = Crossfire::Protocol::; 562@conn::ISA = Crossfire::Protocol::;
393 563
564sub conn::stats_update {
565 my ($self, $stats) = @_;
566
567 # i love text protocols!!!
568 # FIXME: the stats are somehow weird
569 my $hp = $stats->{1};
570 my $hp_m = $stats->{2};
571 my $sp = $stats->{3};
572 my $sp_m = $stats->{4};
573 my $fo = $stats->{18};
574 my $fo_m = 1000;
575 my $gr = $stats->{23};
576 my $gr_m = $stats->{24};
577
578 #d# warn "DATA $hp $hp_m $sp $sp_m $fo $fo_m $gr $gr_m\n";
579 $GAUGES->{hp}->set_value ($hp, $hp_m);
580 $GAUGES->{mana}->set_value ($sp, $sp_m);
581 $GAUGES->{food}->set_value ($fo, $fo_m);
582 $GAUGES->{grace}->set_value ($gr, $gr_m);
583}
584
394sub conn::user_send { 585sub conn::user_send {
395 my ($self, $command) = @_; 586 my ($self, $command) = @_;
396 587
397 $self->send ($command); 588 $self->send_command ($command);
398 status $command; 589 status $command;
590}
591
592sub conn::map_scroll {
593 my ($self, $dx, $dy) = @_;
594
595 $MAP->scroll ($dx, $dy);
399} 596}
400 597
401sub conn::feed_map1a { 598sub conn::feed_map1a {
402 my ($self, $data) = @_; 599 my ($self, $data) = @_;
403 600
404# $self->Crossfire::Protocol::feed_map1a ($data); 601# $self->Crossfire::Protocol::feed_map1a ($data);
405 602
406 $MAP->scroll (delete $self->{delayed_scroll_x}, delete $self->{delayed_scroll_y});
407 $MAP->map1a_update ($data); 603 $MAP->map1a_update ($data);
408 $MAPWIDGET->update; 604 $MAPWIDGET->update;
409} 605}
410 606
411#sub conn::map_update { 607sub conn::flush_map {
412# my ($self, $dirty) = @_; 608 my ($self) = @_;
413# 609
414# $MAPWIDGET->update; 610 my $map_info = delete $self->{map_info}
415#} 611 or return;
612
613 my ($hash, $x, $y, $w, $h) = @$map_info;
614
615 my $data = $MAP->get_rect ($x, $y, $w, $h);
616 $MAPCACHE->put ($hash => Compress::LZF::compress $data);
617
618 warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
619
620}
416 621
417sub conn::map_clear { 622sub conn::map_clear {
418 my ($self) = @_; 623 my ($self) = @_;
419 624
625 $self->flush_map;
626 delete $self->{neigh};
627
420 $MAP->clear; 628 $MAP->clear;
629}
421 630
422# refresh; 631
632sub conn::load_map($$$) {
633 my ($self, $hash, $x, $y) = @_;
634
635 if (defined (my $data = $MAPCACHE->get ($hash))) {
636 $data = Compress::LZF::decompress $data;
637 warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
638 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}
646
647sub conn::flood_fill {
648 my ($self, $path, $hash, $flags, $x0, $y0, $x1, $y1) = @_;
649
650 # the server does not allow map paths > 6
651 return if 6 <= length $path;
652
653 for my $tile (1..4) {
654 next if $self->{neigh}{$hash}[$tile];
655 next unless $flags & (1 << ($tile - 1));
656
657 my $neigh = $self->{neigh}{$hash} ||= [];
658
659 $self->send_mapinfo ("spatial $path$tile", sub {
660 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
661
662 #warn "map<$path>_$tile=<$mode,$x,$y,$w,$h,$hash>\n";#d#
663 return if $mode ne "spatial";
664
665 $x += $MAP->ox;
666 $y += $MAP->oy;
667
668 $self->load_map ($hash, $x, $y)
669 unless $self->{neigh}{$hash}[5]++;#d#
670
671 $neigh->[$tile] = [$x, $y, $w, $h];
672
673 $self->flood_fill ("$path$tile", $hash, $flags, $x0, $y0, $x1, $y1)
674 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
675 });
676 }
677}
678
679sub conn::map_change {
680 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
681
682 $self->flush_map;
683
684 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
685
686 my $mapmapw = 250;
687 my $mapmaph = 250;
688
689 $self->flood_fill ("", $hash, $flags,
690 $ox - $mapmapw * 0.5, $oy - $mapmapw * 0.5,
691 $ox + $mapmapw * 0.5, $oy + $mapmapw * 0.5);
692
693 $x += $ox;
694 $y += $oy;
695
696 $self->{map_info} = [$hash, $x, $y, $w, $h];
697
698 $self->load_map ($hash, $x, $y);
423} 699}
424 700
425sub conn::face_find { 701sub conn::face_find {
426 my ($self, $face) = @_; 702 my ($self, $facenum, $face) = @_;
427 703
428 $FACECACHE->{"$face->{chksum},$face->{name}"} 704 my $hash = "$face->{chksum},$face->{name}";
705
706 my $id = $FACEMAP->get ($hash);
707
708 unless ($id) {
709 # create new id for face
710 # i love transactions
711 for (1..100) {
712 my $txn = $CFClient::DB_ENV->txn_begin;
713 my $status = $FACEMAP->db_get (id => $id, BerkeleyDB::DB_RMW);
714 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
715 $id++;
716 if ($FACEMAP->put (id => $id) == 0
717 && $FACEMAP->put ($hash => $id) == 0) {
718 $txn->txn_commit;
719
720 goto gotid;
721 }
722 }
723 $txn->abort;
724 }
725
726 CFClient::fatal "maximum number of transaction retries reached - database problems?";
727 }
728
729gotid:
730 $face->{id} = $id;
731 $MAP->set_face ($facenum => $id);
732 $TILECACHE->get ($id)
429} 733}
430 734
431sub conn::face_update { 735sub conn::face_update {
432 my ($self, $facenum, $face) = @_; 736 my ($self, $facenum, $face) = @_;
433 737
434 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; 738 $TILECACHE->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes
435 739
436 my $tex = $face->{texture} = 740 $self->set_texture ($face->{id} => delete $face->{image});
741}
742
743sub conn::set_texture {
744 my ($self, $id, $data) = @_;
745
746 $self->{texture}[$id] ||= do {
747 my $tex =
437 new_from_image CFClient::Texture 748 new_from_image CFClient::Texture
438 delete $face->{image}, minify => 1; 749 $data, minify => 1;
439 750
440 $MAP->set_texture ($facenum, @$tex{qw(name w h s t)}, @{$tex->{minified}}); 751 $MAP->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
441 $MAPWIDGET->update; 752 $MAPWIDGET->update;
753
754 $tex
755 };
756}
757
758sub conn::sound_play {
759 my ($self, $x, $y, $soundnum, $type) = @_;
760
761 my $chunk = $AUDIO_CHUNKS{$SOUNDS[$soundnum]}
762 or return;
763
764 $SDL_MIXER->play_channel (-1, $chunk);
765 warn "sound $x,$y,$soundnum,$type\n";#d#
442} 766}
443 767
444sub conn::query { 768sub conn::query {
445 my ($self, $flags, $prompt) = @_; 769 my ($self, $flags, $prompt) = @_;
446 770
452 my ($self, $color, $text) = @_; 776 my ($self, $color, $text) = @_;
453 777
454 my @color = ( 778 my @color = (
455 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00], 779 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
456 [1.00, 1.00, 1.00], 780 [1.00, 1.00, 1.00],
457 [0.00, 0.00, 0.55], 781 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
458 [1.00, 0.00, 0.00], 782 [1.00, 0.00, 0.00],
459 [1.00, 0.54, 0.00], 783 [1.00, 0.54, 0.00],
460 [0.11, 0.56, 1.00], 784 [0.11, 0.56, 1.00],
461 [0.93, 0.46, 0.00], 785 [0.93, 0.46, 0.00],
462 [0.18, 0.54, 0.34], 786 [0.18, 0.54, 0.34],
480 refresh; 804 refresh;
481 }, 805 },
482 SDL_KEYDOWN() => sub { 806 SDL_KEYDOWN() => sub {
483 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { 807 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
484 # alt-enter 808 # alt-enter
485 destroy_screen; 809 video_shutdown;
486 $CFG->{fullscreen} = !$CFG->{fullscreen}; 810 $CFG->{fullscreen} = !$CFG->{fullscreen};
487 init_screen; 811 video_init;
488 } else { 812 } else {
489 CFClient::UI::feed_sdl_key_down_event ($SDL_EV); 813 CFClient::UI::feed_sdl_key_down_event ($SDL_EV);
490 } 814 }
491 }, 815 },
492 SDL_KEYUP() => sub { 816 SDL_KEYUP() => sub {
506 }, 830 },
507); 831);
508 832
509############################################################################# 833#############################################################################
510 834
835$SIG{INT} = $SIG{TERM} = sub { exit };
836
837$TILECACHE = CFClient::db_table "tilecache";
838$FACEMAP = CFClient::db_table "facemap";
839
511CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; 840CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
512 841
513my %DEF_CFG = ( 842my %DEF_CFG = (
514 sdl_mode => 0, 843 sdl_mode => 0,
515 width => 640, 844 width => 640,
520 fow_intensity => 0.45, 849 fow_intensity => 0.45,
521 fow_smooth => 0, 850 fow_smooth => 0,
522 log_fontsize => 14, 851 log_fontsize => 14,
523 mapsize => 100, 852 mapsize => 100,
524 host => "crossfire.schmorp.de", 853 host => "crossfire.schmorp.de",
525 port => 13327,
526 say_command => 'say', 854 say_command => 'say',
527); 855);
528 856
529while (my ($k, $v) = each %DEF_CFG) { 857while (my ($k, $v) = each %DEF_CFG) {
530 $CFG->{$k} = $v unless exists $CFG->{$k}; 858 $CFG->{$k} = $v unless exists $CFG->{$k};
539 867
540@SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)"; 868@SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)";
541 869
542$CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES; 870$CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES;
543 871
544init_screen;
545
546{ 872{
547 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); 873 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
548 874
549 CFClient::add_font $_ for @fonts; 875 CFClient::add_font $_ for @fonts;
550 CFClient::set_font $fonts[0]; 876 CFClient::set_font $fonts[0];
551} 877}
552 878
553$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 879video_init;
880audio_init;
554 881
555Event::loop; 882Event::loop;
556 883
557Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; 884END { SDL::Quit }
558 885
886

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines