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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines