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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines