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.70 by root, Tue Apr 11 14:36:02 2006 UTC vs.
Revision 1.139 by root, Wed Apr 19 00:47:35 2006 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3use strict; 3use strict;
4use utf8; 4use utf8;
5 5
6use Glib; 6use Time::HiRes 'time';
7use Gtk2 -init; 7use Event;
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;
14use SDL::OpenGL::Constants;
15 19
16use Crossfire; 20use Crossfire;
17use Crossfire::Protocol; 21use Crossfire::Protocol;
18 22
23use Compress::LZF;
24
19use CFClient; 25use CFClient;
20use CFClient::Widget; 26use CFClient::UI;
21 27
22our $VERSION = '0.1'; 28our $VERSION = '0.1';
23 29
24my $MAX_FPS = 30; 30my $MAX_FPS = 60;
25my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame 31my $MIN_FPS = 5; # unused as of yet
26 32
33our $META_SERVER = "crossfire.real-time.com:13326";
34
35our $FACEMAP;
36our $TILECACHE;
27our $FACECACHE; 37our $MAPCACHE;
38
39our $LAST_REFRESH;
40our $NOW;
28 41
29our $CFG; 42our $CFG;
30our $CONN; 43our $CONN;
44our $FAST; # fast, low-quality mode, possibly useful for software-rendering
31 45
46our @SDL_MODES;
32our $WIDTH; 47our $WIDTH;
33our $HEIGHT; 48our $HEIGHT;
34our $FULLSCREEN; 49our $FULLSCREEN;
50our $FONTSIZE;
35 51
52our $MAP;
36our $MAPWIDGET; 53our $MAPWIDGET;
37our $FONTSIZE; 54our $BUTTONBAR;
55our $LOGVIEW;
56our $CONSOLE;
57our $METASERVER;
38 58
59our $GAUGES;
60
39our $SDL_TIMER; 61our $SDL_ACTIVE;
40our $SDL_APP;
41our $SDL_EV; 62our $SDL_EV;
42our %SDL_CB; 63our %SDL_CB;
64
65our $SDL_MIXER;
66our @SOUNDS; # event => file mapping
67our %AUDIO_CHUNKS; # audio files
43 68
44our $ALT_ENTER_MESSAGE; 69our $ALT_ENTER_MESSAGE;
45our $STATUS_LINE; 70our $STATUS_LINE;
46our $DEBUG_STATUS; 71our $DEBUG_STATUS;
47 72
48my $last_refresh; 73sub status {
49my %ANIMATE; 74 $STATUS_LINE->set_text ($_[0]);
50my $refresh_handler; 75 $STATUS_LINE->move (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $STATUS_LINE->{h});
76}
51 77
52our ($tw, $te); # Test widget #d# 78sub debug {
79 $DEBUG_STATUS->set_text ($_[0]);
80 $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0, $DEBUG_STATUS->{w}, $DEBUG_STATUS->{h});
81}
53 82
54sub init_screen { 83sub start_game {
55 $SDL_APP = new SDL::App 84 status "logging in...";
56 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
57 -title => "Crossfire+ Client",
58 -width => $WIDTH,
59 -height => $HEIGHT,
60 -opengl => 1,
61 -red_size => 5,
62 -green_size => 5,
63 -blue_size => 5,
64 -alpha_size => 0,
65 -double_buffer => 1,
66 -fullscreen => $FULLSCREEN,
67 -resizeable => 0;
68 85
69 $SDL_EV = new SDL::Event; 86 my $mapsize = List::Util::min 32, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
70 $SDL_EV->set_unicode (1);
71 87
72 $SDL_TIMER = add Glib::Timeout 1000/50, sub { 88 $MAPCACHE = CFClient::db_table "mapcache_$CFG->{host}";
73 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
74 while $SDL_EV->poll;
75 89
76 1 90 $MAP = new CFClient::Map $mapsize, $mapsize;
77 };
78 91
79 $last_refresh = SDL::GetTicks; 92 my ($host, $port) = split /:/, $CFG->{host};
80
81 CFClient::gl_init;
82
83 $FONTSIZE = int $HEIGHT / 50;
84
85 #############################################################################
86
87 glClearColor 0.45, 0.45, 0.45, 1;
88
89 glEnable GL_TEXTURE_2D;
90 glEnable GL_COLOR_MATERIAL;
91 glShadeModel GL_FLAT;
92 glDisable GL_DEPTH_TEST;
93 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
94
95 #############################################################################
96
97 $DEBUG_STATUS = new CFClient::Widget::Label;
98 $CFClient::Widget::TOPLEVEL->add ($DEBUG_STATUS);
99 93
100 $STATUS_LINE = new CFClient::Widget::Label
101 y => $HEIGHT * 59 / 60 - $FONTSIZE;
102 $CFClient::Widget::TOPLEVEL->add ($STATUS_LINE);
103
104 $ALT_ENTER_MESSAGE = new CFClient::Widget::Label
105 y => $HEIGHT * 59 / 60,
106 height => $HEIGHT / 60,
107 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
108 $CFClient::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
109
110 $MAPWIDGET = new CFClient::Widget::MapWidget;
111 $CFClient::Widget::TOPLEVEL->add ($MAPWIDGET);
112 $MAPWIDGET->focus_in;
113
114 # Test code #d#
115 unless ($tw) { # haha...
116 my $w = new CFClient::Widget::Window
117 w => 300, h => 300,
118 child => (my $te = new CFClient::Widget::FancyFrame);
119 $CFClient::Widget::TOPLEVEL->add ($w);
120 $te->add (new CFClient::Widget::Entry);
121
122 $tw = new CFClient::Widget::Animator x => $WIDTH - 200, w => 600, h => 300;
123 my $lbl1 = new CFClient::Widget::Label text => "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
124 my $lbl2 = new CFClient::Widget::Label text => "LBL2";
125 my $vb = new CFClient::Widget::VBox;
126 my $f = new CFClient::Widget::FancyFrame;
127 my $f2 = new CFClient::Widget::FancyFrame;
128 $f->add ($lbl1);
129 $f2->add ($lbl2);
130 $vb->add ($f);
131 $vb->add ($f2, 1);
132
133 $tw->add ($vb);
134 $tw->moveto (0, 0);
135 $CFClient::Widget::TOPLEVEL->add ($tw);
136
137# $f->move ($WIDTH - 200, 0);
138# $CFClient::Widget::TOPLEVEL->add ($f);
139 }
140}
141
142sub destroy_screen {
143 remove Glib::Source $SDL_TIMER;
144 undef $SDL_APP;
145 undef $SDL_EV;
146 SDL::Quit;
147}
148
149sub start_game {
150 $WIDTH = $CFG->{width};
151 $HEIGHT = $CFG->{height};
152 $FULLSCREEN = 0;
153
154 init_screen;
155
156 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
157
158 $CONN = new conn 94 $CONN = new conn
159 host => $CFG->{host}, 95 host => $host,
160 port => $CFG->{port}, 96 port => $port || 13327,
161 user => $CFG->{user}, 97 user => $CFG->{user},
162 pass => $CFG->{password}, 98 pass => $CFG->{password},
163 mapw => $mapsize, 99 mapw => $mapsize,
164 maph => $mapsize, 100 maph => $mapsize,
165 ; 101 ;
166 102
103 status "login successful";
104
167 CFClient::lowdelay fileno $CONN->{fh}; 105 CFClient::lowdelay fileno $CONN->{fh};
168} 106}
169 107
170sub stop_game { 108sub stop_game {
171 remove Glib::Source $refresh_handler if $refresh_handler;
172 undef $refresh_handler;
173
174 undef $CONN; 109 undef $CONN;
175 destroy_screen;
176} 110}
177 111
178sub force_refresh { 112sub client_setup {
179 glViewport 0, 0, $WIDTH, $HEIGHT; 113 my $dialog = new CFClient::UI::FancyFrame
114 child => (my $vbox = new CFClient::UI::VBox);
115 $vbox->add (new CFClient::UI::Label align => 0, text => "Client Setup");
116 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
180 117
181 glMatrixMode GL_PROJECTION; 118 $table->add (0, 0, new CFClient::UI::Label align => 1, text => "Video Mode");
182 glLoadIdentity; 119 $table->add (1, 0, my $hbox = new CFClient::UI::HBox);
183 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
184 glMatrixMode GL_MODELVIEW;
185 120
186 glClear GL_COLOR_BUFFER_BIT; 121 $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]);
122 $hbox->add (my $mode_label = new CFClient::UI::Label height => $FONTSIZE * 0.8);
187 123
188 $CFClient::Widget::TOPLEVEL->draw; 124 $mode_slider->connect (changed => sub {
125 my ($self, $value) = @_;
189 126
190 SDL::GLSwapBuffers; 127 $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value;
191} 128 $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]});
129 });
130 $mode_slider->emit (changed => $mode_slider->{range}[0]);
192 131
193sub debug { 132 $table->add (0, 1, new CFClient::UI::Label align => 1, text => "Fullscreen");
194 $DEBUG_STATUS->set_text ($_[0]); 133 $table->add (1, 1, new CFClient::UI::CheckBox state => $CFG->{fullscreen}, connect_changed => sub {
195 $DEBUG_STATUS->size_allocate ($DEBUG_STATUS->size_request); 134 my ($self, $value) = @_;
196 $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0); 135 $CFG->{fullscreen} = $value;
197} 136 });
198 137
199my $FPS; 138 $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Fast &amp; Ugly");
139 $table->add (1, 2, new CFClient::UI::CheckBox state => $CFG->{fast}, connect_changed => sub {
140 my ($self, $value) = @_;
141 $CFG->{fast} = $value;
142 });
200 143
201sub refresh { 144 $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Fog of War");
202 $refresh_handler ||= add Glib::Idle sub { 145 $table->add (1, 3, new CFClient::UI::CheckBox state => $CFG->{fow_enable}, connect_changed => sub {
203 if ($SDL_APP) { 146 my ($self, $value) = @_;
204 my $next_refresh = SDL::GetTicks; 147 $CFG->{fow_enable} = $value;
148 });
205 149
206 if ($next_refresh - $last_refresh < $TICKS_PER_FRAME) { 150 $table->add (0, 4, new CFClient::UI::Label align => 1, text => "FoW Intensity");
207 SDL::Delay $TICKS_PER_FRAME - ($next_refresh - $last_refresh); 151 $table->add (1, 4, new CFClient::UI::Slider range => [$CFG->{fow_intensity}, 0, 1 + 0.001, 0.001], connect_changed => sub {
208 $next_refresh = SDL::GetTicks; 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
169 $table->add (1, 7, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub {
170 video_shutdown ();
171 video_init ();
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;
209 } 262 }
210 263 }
211 my $interval = ($next_refresh - $last_refresh) * 0.001; 264 });
212 $last_refresh = $next_refresh; 265}
213 266
214 if ($interval) { 267sub server_setup {
215 $FPS ||= 1 / $interval; 268 my $dialog = new CFClient::UI::FancyFrame
216 $FPS = $FPS * 0.96 + (1 / $interval) * 0.04; 269 child => (my $vbox = new CFClient::UI::VBox);
217 debug sprintf "%5.02f", $FPS; 270
271 $vbox->add (new CFClient::UI::Label align => 0, text => "Server Setup");
272 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
273 $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Host:Port");
274
275 {
276 $table->add (1, 2, my $vbox = new CFClient::UI::VBox);
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 }
289
290 $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Username");
291 $table->add (1, 4, new CFClient::UI::Entry text => $CFG->{user}, connect_changed => sub {
292 my ($self, $value) = @_;
293 $CFG->{user} = $value;
294 });
295
296 $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Password");
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 });
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
308 $table->add (0, 7, new CFClient::UI::Label align => 1, text => "Map Size");
309 $table->add (1, 7, new CFClient::UI::Slider
310 req_w => 100,
311 range => [$CFG->{mapsize}, 10, 100 + 1, 1],
312 connect_changed => sub {
313 my ($self, $value) = @_;
314
315 $CFG->{mapsize} = $self->{range}[0] = $value = int $value;
316 },
317 );
318
319 $table->add (1, 8, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub {
320 start_game;
321 });
322
323 $dialog
324}
325
326sub message_window {
327 my $window = new CFClient::UI::FancyFrame
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},
337 );
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;
218 } 347 }
348 delete $input->{auto_activated};
349 },
350 connect_activate => sub {
351 my ($input, $text) = @_;
352 $input->set_text ('');
219 353
220 force_refresh; 354 if ($text =~ /^\/(.*)/) {
221 $_->animate ($interval) for grep $_, values %ANIMATE; 355 $::CONN->user_send ($1);
222
223 if (%ANIMATE) {
224 1
225 } else { 356 } else {
226 undef $refresh_handler; 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
376}
377
378sub sdl_init {
379 #SDL::Init SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE
380 SDL::Init SDL_INIT_AUDIO | SDL_INIT_VIDEO
381 and die "SDL::Init failed!\n";
382}
383
384sub video_init {
385 sdl_init;
386
387 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
388 $FULLSCREEN = $CFG->{fullscreen};
389 $FAST = $CFG->{fast};
390
391 SDL::GLSetAttribute SDL_GL_RED_SIZE, 5;
392 SDL::GLSetAttribute SDL_GL_GREEN_SIZE, 5;
393 SDL::GLSetAttribute SDL_GL_BLUE_SIZE, 5;
394 SDL::GLSetAttribute SDL_GL_ALPHA_SIZE, 1;
395
396 SDL::GLSetAttribute SDL_GL_ACCUM_RED_SIZE, 0;
397 SDL::GLSetAttribute SDL_GL_ACCUM_GREEN_SIZE, 0;
398 SDL::GLSetAttribute SDL_GL_ACCUM_BLUE_SIZE, 0;
399 SDL::GLSetAttribute SDL_GL_ACCUM_ALPHA_SIZE, 0;
400
401 SDL::GLSetAttribute SDL_GL_DOUBLEBUFFER, 1;
402 SDL::GLSetAttribute SDL_GL_BUFFER_SIZE, 15;
403 SDL::GLSetAttribute SDL_GL_DEPTH_SIZE, 0;
404
405 SDL::SetVideoMode $WIDTH, $HEIGHT, 0,
406 SDL_HWSURFACE | SDL_ANYFORMAT | SDL_OPENGL | SDL_DOUBLEBUF
407 | ($FULLSCREEN ? SDL_FULLSCREEN : 0)
408 or die "SDL::SetVideoMode failed!\n";
409
410 SDL::WMSetCaption "Crossfire+ Client", "Crossfire+";
411
412 $SDL_EV = new SDL::Event;
413 $SDL_EV->set_unicode (1);
414
415 $SDL_ACTIVE = 1;
416
417 $LAST_REFRESH = time - 0.01;
418
419 CFClient::gl_init;
420
421 $FONTSIZE = int $HEIGHT / 40;
422
423 #############################################################################
424
425 $DEBUG_STATUS = new CFClient::UI::Label padding => 0, z => 100;
426 $CFClient::UI::ROOT->add ($DEBUG_STATUS);
427
428 $STATUS_LINE = new CFClient::UI::Label
429 padding => 0,
430 y => $HEIGHT * 44 / 45 - $FONTSIZE;
431 $CFClient::UI::ROOT->add ($STATUS_LINE);
432
433 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
434 padding => 0,
435 y => $HEIGHT * 44 / 45,
436 fontsize => $HEIGHT / 45,
437 markup => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
438 $CFClient::UI::ROOT->add ($ALT_ENTER_MESSAGE);
439
440 $CFClient::UI::ROOT->add ($MAPWIDGET = new CFClient::UI::MapWidget);
441 $MAPWIDGET->focus_in;
442 $MAPWIDGET->connect (activate_console => sub {
443 my ($mapwidget, $preset) = @_;
444
445 if ($CONSOLE) {
446 $CONSOLE->{input}->{auto_activated} = 1;
447 $CONSOLE->{input}->focus_in;
448
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 {
481 $CFClient::UI::ROOT->{children} = [];
482 undef $SDL_ACTIVE;
483 undef $SDL_EV;
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
227 0 512 };
228 } 513 }
229 } else { 514 } else {
230 undef $refresh_handler; 515 status "unable to open sound config: $!";
231 0
232 } 516 }
233 }; 517 }
518}
519
520sub audio_shutdown {
521 undef $SDL_MIXER;
522 @SOUNDS = ();
523 %AUDIO_CHUNKS = ();
524}
525
526my %animate_object;
527my $animate_timer;
528
529my $want_refresh;
530my $can_refresh;
531
532my $fps = 9;
533
534sub force_refresh {
535 $fps = $fps * 0.95 + 1 / ($NOW - $LAST_REFRESH) * 0.05;
536 debug sprintf "%3.2f", $fps;
537
538 $want_refresh = 0;
539 $can_refresh = 0;
540
541 $CFClient::UI::ROOT->draw;
542
543 SDL::GLSwapBuffers;
544
545 $LAST_REFRESH = $NOW;
546}
547
548my $refresh_watcher = Event->timer (after => 0, hard => 1, interval => 1 / $MAX_FPS, cb => sub {
549 $NOW = time;
550
551 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
552 while $SDL_EV->poll;
553
554 if (%animate_object) {
555 $_->animate ($LAST_REFRESH - $NOW) for values %animate_object;
556 $want_refresh++;
557 }
558
559 if ($want_refresh) {
560 force_refresh;
561 } else {
562 $can_refresh = 1;
563 }
564});
565
566sub refresh {
567 $want_refresh++;
234} 568}
235 569
236sub animation_start { 570sub animation_start {
237 my ($widget) = @_; 571 my ($widget) = @_;
238 $ANIMATE{$widget} = $widget; 572 $animate_object{$widget} = $widget;
239 Scalar::Util::weaken $ANIMATE{$widget};
240
241 refresh;
242} 573}
243 574
244sub animation_stop { 575sub animation_stop {
245 my ($widget) = @_; 576 my ($widget) = @_;
246 delete $ANIMATE{$widget}; 577 delete $animate_object{$widget};
578}
579
580@conn::ISA = Crossfire::Protocol::;
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
603sub conn::user_send {
604 my ($self, $command) = @_;
605
606 $self->send_command ($command);
607 status $command;
608}
609
610sub conn::map_scroll {
611 my ($self, $dx, $dy) = @_;
612
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
638}
639
640sub conn::map_clear {
641 my ($self) = @_;
642
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);
717}
718
719sub conn::face_find {
720 my ($self, $facenum, $face) = @_;
721
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)
751}
752
753sub conn::face_update {
754 my ($self, $facenum, $face) = @_;
755
756 $TILECACHE->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes
757
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#
787}
788
789sub conn::query {
790 my ($self, $flags, $prompt) = @_;
791
792 #TODO
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);
247} 816}
248 817
249%SDL_CB = ( 818%SDL_CB = (
250 SDL_QUIT() => sub { 819 SDL_QUIT() => sub {
251 main_quit Gtk2; 820 Event::unloop -1;
252 }, 821 },
253 SDL_VIDEORESIZE() => sub { 822 SDL_VIDEORESIZE() => sub {
254 }, 823 },
255 SDL_VIDEOEXPOSE() => sub { 824 SDL_VIDEOEXPOSE() => sub {
256 refresh; 825 refresh;
257 }, 826 },
258 SDL_KEYDOWN() => sub { 827 SDL_KEYDOWN() => sub {
259 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) {
260 # alt-enter 829 # alt-enter
261 $FULLSCREEN = !$FULLSCREEN; 830 video_shutdown;
262 init_screen; 831 $CFG->{fullscreen} = !$CFG->{fullscreen};
832 video_init;
263 } else { 833 } else {
264 CFClient::Widget::feed_sdl_key_down_event ($SDL_EV); 834 CFClient::UI::feed_sdl_key_down_event ($SDL_EV);
265 } 835 }
266 }, 836 },
267 SDL_KEYUP() => sub { 837 SDL_KEYUP() => sub {
268 CFClient::Widget::feed_sdl_key_up_event ($SDL_EV); 838 CFClient::UI::feed_sdl_key_up_event ($SDL_EV);
269 }, 839 },
270 SDL_MOUSEMOTION() => sub { 840 SDL_MOUSEMOTION() => sub {
271 CFClient::Widget::feed_sdl_motion_event ($SDL_EV); 841 CFClient::UI::feed_sdl_motion_event ($SDL_EV);
272 }, 842 },
273 SDL_MOUSEBUTTONDOWN() => sub { 843 SDL_MOUSEBUTTONDOWN() => sub {
274 CFClient::Widget::feed_sdl_button_down_event ($SDL_EV); 844 CFClient::UI::feed_sdl_button_down_event ($SDL_EV);
275 }, 845 },
276 SDL_MOUSEBUTTONUP() => sub { 846 SDL_MOUSEBUTTONUP() => sub {
277 CFClient::Widget::feed_sdl_button_up_event ($SDL_EV); 847 CFClient::UI::feed_sdl_button_up_event ($SDL_EV);
278 }, 848 },
279 SDL_ACTIVEEVENT() => sub { 849 SDL_ACTIVEEVENT() => sub {
280# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# 850# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
281 }, 851 },
282); 852);
283 853
284@conn::ISA = Crossfire::Protocol::;
285
286sub conn::map_update {
287 my ($self, $dirty) = @_;
288
289 $MAPWIDGET->update;
290}
291
292sub conn::map_scroll {
293 my ($self, $dx, $dy) = @_;
294
295# refresh;
296}
297
298sub conn::map_clear {
299 my ($self) = @_;
300
301# refresh;
302}
303
304sub conn::face_find {
305 my ($self, $face) = @_;
306
307 $FACECACHE->{"$face->{chksum},$face->{name}"}
308}
309
310sub conn::face_update {
311 my ($self, $face) = @_;
312
313 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
314
315 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
316}
317
318sub conn::query {
319 my ($self, $flags, $prompt) = @_;
320
321 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
322}
323
324sub gtk_add_cfg_field {
325 my ($tbl, $cfg, $klbl, $key, $value) = @_;
326 my $i = $cfg->{_i}++;
327 $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
328 $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
329 if ($key eq 'password') {
330 $ent->set_invisible_char ("*");
331 $ent->set (visibility => 0)
332 }
333 $ent->set_text ($value);
334 $ent->signal_connect (changed => sub {
335 my ($ent) = @_;
336 $cfg->{$key} = $ent->get_text;
337 # TODO: Mapsize should be a slider in the game gui
338 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
339 $cfg->{$key} = 100;
340 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
341 $cfg->{$key} = 50;
342 }
343 });
344}
345
346sub run_config_dialog {
347 my (%events) = @_;
348
349 my $w = Gtk2::Window->new;
350
351 my @cfg = (
352 [qw/Host host/],
353 [qw/Port port/],
354 [qw/Mapsize% mapsize/],
355 [qw/Username user/],
356 [qw/Password password/],
357 );
358
359 my $cfg = {};
360
361 my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE);
362 my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a;
363
364 $w->add (my $vb = Gtk2::VBox->new);
365 $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
366 my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
367 $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
368 $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
369 my $i = 0;
370 my $act = 0;
371 for (map { "$_->[0]x$_->[1]" } reverse @modes) {
372 if ($_ eq $selmode) { $act = $i }
373 $cb->append_text ($_);
374 $i++;
375 }
376 $cb->set_active ($act);
377 $cb->signal_connect (changed => sub {
378 my ($cb) = @_;
379 my $txt = $cb->get_active_text;
380 if ($txt =~ m/(\d+)x(\d+)/) {
381 $::CFG->{width} = $1;
382 $::CFG->{height} = $2;
383 }
384 });
385
386 $cfg->{_i} = 1;
387 for (@cfg) {
388 gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
389 }
390
391 $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
392 $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
393 $cb->signal_connect (clicked => sub {
394 for (keys %$cfg) {
395 $::CFG->{$_} = $cfg->{$_}
396 if $_ ne '_i';
397 }
398 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
399 });
400 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
401 $cb->signal_connect (clicked => sub {
402 for (keys %$cfg) {
403 $::CFG->{$_} = $cfg->{$_}
404 if $_ ne '_i';
405 }
406 my $cb = $events{login} || sub {};
407 $cb->($::CFG->{user}, $::CFG->{password});
408 });
409 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
410 $cb->signal_connect (clicked => sub {
411 my $cb = $events{logout} || sub {};
412 $cb->();
413 });
414 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
415 $cb->signal_connect (clicked => sub { $w->destroy });
416
417 $w->show_all;
418
419 $w->signal_connect (destroy => sub { Gtk2->main_quit });
420}
421
422
423############################################################################# 854#############################################################################
424 855
425SDL::Init SDL_INIT_EVERYTHING; 856$SIG{INT} = $SIG{TERM} = sub { exit };
857
858$TILECACHE = CFClient::db_table "tilecache";
859$FACEMAP = CFClient::db_table "facemap";
426 860
427CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; 861CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
428 862
429$CFG ||= { 863my %DEF_CFG = (
864 sdl_mode => 0,
430 width => 640, 865 width => 640,
431 height => 480, 866 height => 480,
867 fullscreen => 0,
868 fast => 0,
869 fow_enable => 1,
870 fow_intensity => 0.45,
871 fow_smooth => 0,
872 log_fontsize => 14,
432 mapsize => 100, 873 mapsize => 100,
433 fullscreen => 0,
434 host => "crossfire.schmorp.de", 874 host => "crossfire.schmorp.de",
435 port => 13327, 875 say_command => 'say',
436}; 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}
883
884sdl_init;
885
886@SDL_MODES = reverse
887 grep $_->[0] >= 640 && $_->[1] >= 480,
888 map [SDL::RectW ($_), SDL::RectH ($_)],
889 @{ SDL::ListModes 0, SDL_FULLSCREEN | SDL_HWSURFACE | SDL_OPENGL };
890
891@SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)";
892
893$CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES;
437 894
438{ 895{
439 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);
440 897
441 CFClient::add_font $_ for @fonts; 898 CFClient::add_font $_ for @fonts;
442 CFClient::set_font $fonts[0]; 899 CFClient::set_font $fonts[0];
443} 900}
444 901
445$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 902video_init;
903audio_init;
446 904
447run_config_dialog 905Event::loop;
448 login => sub { start_game },
449 logout => sub { stop_game };
450 906
451main Gtk2; 907END { SDL::Quit }
452 908
453Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; 909

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines