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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines