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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines