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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines