ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.164
Committed: Sun Apr 23 01:05:04 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.163: +5 -17 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #!/opt/bin/perl
2
3 use strict;
4 use utf8;
5
6 use Time::HiRes 'time';
7 use Event;
8
9 use Crossfire;
10 use Crossfire::Protocol;
11
12 use Compress::LZF;
13
14 use CFClient;
15 use CFClient::UI;
16 use CFClient::MapWidget;
17
18 our $VERSION = '0.1';
19
20 my $MAX_FPS = 60;
21 my $MIN_FPS = 5; # unused as of yet
22
23 our $META_SERVER = "crossfire.real-time.com:13326";
24
25 our $FACEMAP;
26 our $TILECACHE;
27 our $MAPCACHE;
28
29 our $LAST_REFRESH;
30 our $NOW;
31
32 our $CFG;
33 our $CONN;
34 our $FAST; # fast, low-quality mode, possibly useful for software-rendering
35
36 our @SDL_MODES;
37 our $WIDTH;
38 our $HEIGHT;
39 our $FULLSCREEN;
40 our $FONTSIZE;
41
42 our $MAP;
43 our $MAPWIDGET;
44 our $BUTTONBAR;
45 our $LOGVIEW;
46 our $CONSOLE;
47 our $METASERVER;
48
49 our $GAUGES;
50 our $STATWIDS;
51
52 our $SDL_ACTIVE;
53 our %SDL_CB;
54
55 our $SDL_MIXER;
56 our @SOUNDS; # event => file mapping
57 our %AUDIO_CHUNKS; # audio files
58
59 our $ALT_ENTER_MESSAGE;
60 our $STATUS_LINE;
61 our $DEBUG_STATUS;
62
63 sub status {
64 $STATUS_LINE->set_text ($_[0]);
65 $STATUS_LINE->move (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $STATUS_LINE->{h});
66 }
67
68 sub debug {
69 $DEBUG_STATUS->set_text ($_[0]);
70 $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0, $DEBUG_STATUS->{w}, $DEBUG_STATUS->{h});
71 }
72
73 sub start_game {
74 status "logging in...";
75
76 my $mapsize = List::Util::min 32, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
77
78 $MAPCACHE = CFClient::db_table "mapcache_$CFG->{host}";
79
80 $MAP = new CFClient::Map $mapsize, $mapsize;
81
82 my ($host, $port) = split /:/, $CFG->{host};
83
84 $CONN = new conn
85 host => $host,
86 port => $port || 13327,
87 user => $CFG->{user},
88 pass => $CFG->{password},
89 mapw => $mapsize,
90 maph => $mapsize,
91 ;
92
93 status "login successful";
94
95 CFClient::lowdelay fileno $CONN->{fh};
96 }
97
98 sub stop_game {
99 undef $CONN;
100 }
101
102 sub client_setup {
103 my $dialog = new CFClient::UI::FancyFrame
104 title => "Client Setup",
105 child => (my $vbox = new CFClient::UI::VBox);
106 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
107
108 $table->add (0, 0, new CFClient::UI::Label valign => 0, align => 1, text => "Video Mode");
109 $table->add (1, 0, my $hbox = new CFClient::UI::HBox);
110
111 $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]);
112 $hbox->add (my $mode_label = new CFClient::UI::Label align => 0, valign => 0, height => 0.8, template => "9999x9999");
113
114 $mode_slider->connect (changed => sub {
115 my ($self, $value) = @_;
116
117 $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value;
118 $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]});
119 });
120 $mode_slider->emit (changed => $mode_slider->{range}[0]);
121
122 my $row = 1;
123
124 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Fullscreen");
125 $table->add (1, $row++, new CFClient::UI::CheckBox state => $CFG->{fullscreen}, connect_changed => sub {
126 my ($self, $value) = @_;
127 $CFG->{fullscreen} = $value;
128 });
129
130 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Fast & Ugly");
131 $table->add (1, $row++, new CFClient::UI::CheckBox
132 state => $CFG->{fast},
133 tooltip => "Lower the visual quality considerably to speed up rendering.",
134 connect_changed => sub {
135 my ($self, $value) = @_;
136 $CFG->{fast} = $value;
137 }
138 );
139
140 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Fog of War");
141 $table->add (1, $row++, new CFClient::UI::CheckBox
142 state => $CFG->{fow_enable},
143 tooltip => "Fog-of-War marks areas that cannot be seen by the player",
144 connect_changed => sub {
145 my ($self, $value) = @_;
146 $CFG->{fow_enable} = $value;
147 }
148 );
149
150 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "FoW Intensity");
151 $table->add (1, $row++, new CFClient::UI::Slider
152 range => [$CFG->{fow_intensity}, 0, 1 + 0.001, 0.001],
153 tooltip => "The higher the intensity, the lighter the Fog-of-War color",
154 connect_changed => sub {
155 my ($self, $value) = @_;
156 $CFG->{fow_intensity} = $value;
157 }
158 );
159
160 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "FoW Smooth");
161 $table->add (1, $row++, new CFClient::UI::CheckBox
162 state => $CFG->{fow_smooth},
163 tooltip => "Smooth the Fog-of-War a bit to make it more realistic",
164 connect_changed => sub {
165 my ($self, $value) = @_;
166 $CFG->{fow_smooth} = $value;
167 status "Fog of War smoothing requires OpenGL 1.2 or higher" if $CFClient::GL_VERSION < 1.2;
168 }
169 );
170
171 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "GUI Fontsize");
172 $table->add (1, $row++, new CFClient::UI::Slider
173 range => [$CFG->{gui_fontsize}, 0.5, 2, 0.1],
174 tooltip => "The font size used by most GUI elements",
175 connect_changed => sub {
176 $CFG->{gui_fontsize} = 0.1 * int $_[1] * 10;
177 # $FONTSIZE = int $HEIGHT / 40 * $CFG->{gui_fontsize};
178 }
179 );
180
181 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Server Log Fontsize");
182 $table->add (1, $row++, new CFClient::UI::Slider
183 range => [$CFG->{log_fontsize}, 0.5, 2, 0.1],
184 tooltip => "The font size used by the server log window only",
185 connect_changed => sub {
186 $LOGVIEW->set_fontsize ($CFG->{log_fontsize} = 0.1 * int $_[1] * 10);
187 }
188 );
189
190 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Stats Fontsize");
191
192 $table->add (1, $row++, new CFClient::UI::Slider
193 range => [$CFG->{stat_fontsize}, 0.5, 2, 0.1],
194 tooltip => "The font size used by the statistics window only",
195 connect_changed => sub {
196 $CFG->{stat_fontsize} = 0.1 * int $_[1] * 10;
197 &set_stats_window_fontsize;
198 }
199 );
200
201 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Gauge size");
202 $table->add (1, $row++, new CFClient::UI::Slider
203 range => [$CFG->{gauge_size}, 0.2, 0.8, 0.02],
204 tooltip => "Adjust the size of the stats gauges at the bottom right",
205 connect_changed => sub {
206 $CFG->{gauge_size} = $_[1];
207 my $h = int $HEIGHT * $CFG->{gauge_size};
208 $GAUGES->{win}->set_size ($WIDTH, $h);
209 $GAUGES->{win}->move (0, $HEIGHT - $h);
210 }
211 );
212
213 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Gauge fontsize");
214 $table->add (1, $row++, new CFClient::UI::Slider
215 range => [$CFG->{gauge_fontsize}, 0.5, 2.0, 0.1],
216 connect_changed => sub {
217 $CFG->{gauge_fontsize} = 0.1 * int $_[1] * 10;
218 &set_gauge_window_fontsize;
219 #$GAUGES->{win}->check_size;
220 #$GAUGES->{win}->update;
221 }
222 );
223
224 $table->add (1, $row++, new CFClient::UI::Button
225 expand => 1, align => 0, text => "Apply",
226 tooltip => "Apply the video settings (unless they are auto-apply)",
227 connect_activate => sub {
228 video_shutdown ();
229 video_init ();
230 }
231 );
232
233 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Audio Enable");
234 $table->add (1, $row++, new CFClient::UI::CheckBox
235 state => $CFG->{audio_enable},
236 tooltip => "If enabled, sound effects and music will be played. If disabled, no audio will be used and the soundcard will not be opened.",
237 connect_changed => sub {
238 $CFG->{audio_enable} = $_[1];
239 }
240 );
241 # $table->add (0, 9, new CFClient::UI::Label valign => 0, align => 1, text => "Effects Volume");
242 # $table->add (1, 8, new CFClient::UI::Slider range => [$CFG->{effects_volume}, 0, 128, 1], connect_changed => sub {
243 # $CFG->{effects_volume} = $_[1];
244 # });
245 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Background Music");
246 $table->add (1, $row++, my $hbox = new CFClient::UI::HBox);
247 $hbox->add (new CFClient::UI::CheckBox
248 expand => 1, state => $CFG->{bgm_enable},
249 tooltip => "Enable background music playing",
250 connect_changed => sub {
251 $CFG->{bgm_enable} = $_[1];
252 }
253 );
254 $hbox->add (new CFClient::UI::Slider
255 expand => 1, range => [$CFG->{bgm_volume}, 0, 1, 0.1],
256 tooltip => "The volume of the background music",
257 connect_changed => sub {
258 $CFG->{bgm_volume} = $_[1];
259 CFClient::MixMusic::volume $_[1] * 128;
260 }
261 );
262
263 $table->add (1, $row++, new CFClient::UI::Button
264 expand => 1, align => 0, text => "Apply",
265 tooltip => "Apply the audio settings that are not auto-apply",
266 connect_activate => sub {
267 audio_shutdown ();
268 audio_init ();
269 }
270 );
271
272 $dialog
273 }
274
275 sub set_stats_window_fontsize {
276 for (values %{$STATWIDS}) {
277 $_->set_fontsize ($::CFG->{stat_fontsize});
278 }
279 }
280
281 sub set_gauge_window_fontsize {
282 for (map { $GAUGES->{$_} } grep { $_ ne 'win' } keys %{$GAUGES}) {
283 $_->set_fontsize ($::CFG->{gauge_fontsize});
284 }
285 }
286
287 sub make_gauge_window {
288 my $gh = int ($HEIGHT * $CFG->{gauge_size});
289 # my $gw = int ($WIDTH * $CFG->{gauge_w_size});
290
291 my $win = new CFClient::UI::Frame (
292 y => $HEIGHT - $gh, x => 0, req_w => $WIDTH, req_h => $gh
293 );
294 $win->add (my $vb = new CFClient::UI::VBox);
295
296 $vb->add (my $hbg = new CFClient::UI::HBox expand => 1);
297
298
299 $hbg->add (new CFClient::UI::Empty expand => 1);
300 $hbg->add (my $hb = new CFClient::UI::HBox);
301 $hb->add (my $hg = new CFClient::UI::Gauge type => 'hp');
302 $hb->add (my $mg = new CFClient::UI::Gauge type => 'mana');
303 $hb->add (my $gg = new CFClient::UI::Gauge type => 'grace');
304 $hb->add (my $fg = new CFClient::UI::Gauge type => 'food');
305
306 $vb->add (my $exp = new CFClient::UI::Label valign => 0, align => 1, text => "XP:");
307 # $vb->add (my $lvl = new CFClient::UI::Label valign => 0, align => 1, text => "Lvl:");
308 $vb->add (my $rng = new CFClient::UI::Label valign => 0, align => 1, text => "Rng:");
309
310
311 $GAUGES = {
312 exp => $exp,# lvl => $lvl,
313 win => $win, range => $rng,
314 food => $fg, mana => $mg, hp => $hg, grace => $gg
315 };
316 $win
317 }
318
319 sub make_stats_window {
320 my $tgw = new CFClient::UI::FancyFrame (x => $WIDTH * 2/5, y => 0, title => "Stats");
321
322 $tgw->add (my $vb = new CFClient::UI::VBox);
323 $vb->add (my $uhb = new CFClient::UI::HBox);
324 $uhb->add ($STATWIDS->{title} = new CFClient::UI::Label valign => 0, align => -1, text => "Title:", expand => 1);
325 $uhb->add ($STATWIDS->{map} = new CFClient::UI::Label valign => 0, align => -1, text => "Map:", expand => 1);
326
327 $vb->add (my $hb = new CFClient::UI::HBox expand => 1);
328
329 $hb->add (my $tbl = new CFClient::UI::Table expand => 1);
330
331 $tbl->add (0, 0, $STATWIDS->{st_str_lbl} = new CFClient::UI::Label valign => 0, align => +1, text => "Str");
332 $tbl->add (0, 1, $STATWIDS->{st_dex_lbl} = new CFClient::UI::Label valign => 0, align => +1, text => "Dex");
333 $tbl->add (0, 2, $STATWIDS->{st_con_lbl} = new CFClient::UI::Label valign => 0, align => +1, text => "Con");
334 $tbl->add (0, 3, $STATWIDS->{st_int_lbl} = new CFClient::UI::Label valign => 0, align => +1, text => "Int");
335 $tbl->add (0, 4, $STATWIDS->{st_wis_lbl} = new CFClient::UI::Label valign => 0, align => +1, text => "Wis");
336 $tbl->add (0, 5, $STATWIDS->{st_pow_lbl} = new CFClient::UI::Label valign => 0, align => +1, text => "Pow");
337 $tbl->add (0, 6, $STATWIDS->{st_cha_lbl} = new CFClient::UI::Label valign => 0, align => +1, text => "Cha");
338
339 $tbl->add (1, 0, $STATWIDS->{st_str} = new CFClient::UI::Label valign => 0, align => +1, text => "");
340 $tbl->add (1, 1, $STATWIDS->{st_dex} = new CFClient::UI::Label valign => 0, align => +1, text => "");
341 $tbl->add (1, 2, $STATWIDS->{st_con} = new CFClient::UI::Label valign => 0, align => +1, text => "");
342 $tbl->add (1, 3, $STATWIDS->{st_int} = new CFClient::UI::Label valign => 0, align => +1, text => "");
343 $tbl->add (1, 4, $STATWIDS->{st_wis} = new CFClient::UI::Label valign => 0, align => +1, text => "");
344 $tbl->add (1, 5, $STATWIDS->{st_pow} = new CFClient::UI::Label valign => 0, align => +1, text => "");
345 $tbl->add (1, 6, $STATWIDS->{st_cha} = new CFClient::UI::Label valign => 0, align => +1, text => "");
346
347 $tbl->add (2, 0, $STATWIDS->{st_wc_lbl} = new CFClient::UI::Label valign => 0, align => +1, text => "Wc");
348 $tbl->add (2, 1, $STATWIDS->{st_ac_lbl} = new CFClient::UI::Label valign => 0, align => +1, text => "Ac");
349 $tbl->add (2, 2, $STATWIDS->{st_dam_lbl} = new CFClient::UI::Label valign => 0, align => +1, text => "Dam");
350 $tbl->add (2, 3, $STATWIDS->{st_arm_lbl} = new CFClient::UI::Label valign => 0, align => +1, text => "Arm");
351 $tbl->add (2, 4, $STATWIDS->{st_spd_lbl} = new CFClient::UI::Label valign => 0, align => +1, text => "Sp");
352 $tbl->add (2, 5, $STATWIDS->{st_wspd_lbl} = new CFClient::UI::Label valign => 0, align => +1, text => "WSp");
353
354 $tbl->add (3, 0, $STATWIDS->{st_wc} = new CFClient::UI::Label valign => 0, align => +1, text => "");
355 $tbl->add (3, 1, $STATWIDS->{st_ac} = new CFClient::UI::Label valign => 0, align => +1, text => "");
356 $tbl->add (3, 2, $STATWIDS->{st_dam} = new CFClient::UI::Label valign => 0, align => +1, text => "");
357 $tbl->add (3, 3, $STATWIDS->{st_arm} = new CFClient::UI::Label valign => 0, align => +1, text => "");
358 $tbl->add (3, 4, $STATWIDS->{st_spd} = new CFClient::UI::Label valign => 0, align => +1, text => "");
359 $tbl->add (3, 5, $STATWIDS->{st_wspd} = new CFClient::UI::Label valign => 0, align => +1, text => "");
360
361 $hb->add (my $tbl2 = new CFClient::UI::Table expand => 1);
362
363 my $row = 0;
364 my $col = 0;
365
366 for (qw/slow holyw conf fire depl magic
367 drain acid pois para deat phys
368 blind fear tund elec cold ghit/)
369 {
370 $tbl2->add ($col, $row,
371 $STATWIDS->{"res_$_"} =
372 new CFClient::UI::Label text => "0", align => +1, valign => 0
373 );
374 $tbl2->add ($col + 1, $row, new CFClient::UI::Image image => "ui/resist/resist_$_.png");
375
376 $row++;
377 if ($row % 6 == 0) {
378 $col += 2;
379 $row = 0;
380 }
381 }
382
383 &set_stats_window_fontsize;
384 update_stats_window ({});
385
386 $tgw
387 }
388
389 sub update_stats_window {
390 my ($stats) = @_;
391
392 # i love text protocols!!!
393 my $hp = $stats->{1} * 1;
394 my $hp_m = $stats->{2} * 1;
395 my $sp = $stats->{3} * 1;
396 my $sp_m = $stats->{4} * 1;
397 my $fo = $stats->{18} * 1;
398 my $fo_m = 999;
399 my $gr = $stats->{23} * 1;
400 my $gr_m = $stats->{24} * 1;
401
402 $GAUGES->{hp} ->set_value ($hp, $hp_m);
403 $GAUGES->{mana} ->set_value ($sp, $sp_m);
404 $GAUGES->{food} ->set_value ($fo, $fo_m);
405 $GAUGES->{grace} ->set_value ($gr, $gr_m);
406 $GAUGES->{exp} ->set_text ("XP: " . ($stats->{11} || $stats->{28}) * 1
407 ." LVL: " . $stats->{12} * 1);
408 my $rng = $stats->{20};
409 $rng =~ s/^Range: //; # thank you so much dear server
410 $GAUGES->{range} ->set_text ("Rng: " . $rng);
411 # $GAUGES->{lvl} ->set_text ("LVL: " . $stats->{12});
412 $STATWIDS->{title} ->set_text ("Title: " . $stats->{21});
413
414 if (0) { # this code can vanish, just wanted to preserver it for a checkin
415 $STATWIDS->{st_str} ->set_text (sprintf "S%d", $stats->{5});
416 $STATWIDS->{st_dex} ->set_text (sprintf "D%d", $stats->{8});
417 $STATWIDS->{st_con} ->set_text (sprintf "Co%d", $stats->{9});
418 $STATWIDS->{st_int} ->set_text (sprintf "I%d", $stats->{6});
419 $STATWIDS->{st_wis} ->set_text (sprintf "W%d", $stats->{7});
420 $STATWIDS->{st_pow} ->set_text (sprintf "P%d", $stats->{22});
421 $STATWIDS->{st_cha} ->set_text (sprintf "Ch%d", $stats->{10});
422 $STATWIDS->{st_wc} ->set_text (sprintf "Wc%d", $stats->{13});
423 $STATWIDS->{st_ac} ->set_text (sprintf "Ac%d", $stats->{14});
424 $STATWIDS->{st_dam} ->set_text (sprintf "Dam%d", $stats->{15});
425 $STATWIDS->{st_arm} ->set_text (sprintf "Arm%d", $stats->{16});
426 $STATWIDS->{st_spd} ->set_text (sprintf "Sp%.1f", $stats->{17});
427 $STATWIDS->{st_wspd}->set_text (sprintf "WSp%.1f", $stats->{19});
428 } else {
429 $STATWIDS->{st_str} ->set_text (sprintf "%d", $stats->{5});
430 $STATWIDS->{st_dex} ->set_text (sprintf "%d", $stats->{8});
431 $STATWIDS->{st_con} ->set_text (sprintf "%d", $stats->{9});
432 $STATWIDS->{st_int} ->set_text (sprintf "%d", $stats->{6});
433 $STATWIDS->{st_wis} ->set_text (sprintf "%d", $stats->{7});
434 $STATWIDS->{st_pow} ->set_text (sprintf "%d", $stats->{22});
435 $STATWIDS->{st_cha} ->set_text (sprintf "%d", $stats->{10});
436 $STATWIDS->{st_wc} ->set_text (sprintf "%d", $stats->{13});
437 $STATWIDS->{st_ac} ->set_text (sprintf "%d", $stats->{14});
438 $STATWIDS->{st_dam} ->set_text (sprintf "%d", $stats->{15});
439 $STATWIDS->{st_arm} ->set_text (sprintf "%d", $stats->{16});
440 $STATWIDS->{st_spd} ->set_text (sprintf "%.1f", $stats->{17});
441 $STATWIDS->{st_wspd}->set_text (sprintf "%.1f", $stats->{19});
442 }
443
444 my %tbl = (
445 phys => 100,
446 magic => 101,
447 fire => 102,
448 elec => 103,
449 cold => 104,
450 conf => 105,
451 acid => 106,
452 drain => 107,
453 ghit => 108,
454 pois => 109,
455 slow => 110,
456 para => 111,
457 tund => 112,
458 fear => 113,
459 deat => 115,
460 holyw => 116,
461 blind => 117
462 );
463
464 for (keys %tbl) {
465 $STATWIDS->{"res_$_"}->set_text (sprintf "%d%", $stats->{$tbl{$_}});
466 }
467
468 }
469
470 sub metaserver_dialog {
471 my $dialog = new CFClient::UI::FancyFrame
472 title => "Metaserver",
473 child => (my $vbox = new CFClient::UI::VBox);
474
475 $vbox->add ($dialog->{table} = new CFClient::UI::Table);
476
477 $dialog
478 }
479
480 sub update_metaserver {
481 my ($HOST) = @_;
482
483 status "fetching metaserver list...";
484
485 my $buf;
486
487 my $fh = new IO::Socket::INET PeerHost => $META_SERVER, Blocking => 0;
488
489 Event->io (fd => $fh, poll => 'r', cb => sub {
490 my $res = sysread $fh, $buf, 8192, length $buf;
491
492 if (!defined $res) {
493 $_[0]->w->cancel;
494 status "metaserver: $!";
495 } elsif ($res == 0) {
496 $_[0]->w->cancel;
497 status "server list retrieved";
498
499 my $table = $METASERVER->{table};
500
501 $table->clear;
502
503 my @col = qw(Use #Users Host Uptime Version Description);
504 $table->add ($_, 0, new CFClient::UI::Label align => 0, fg => [1, 1, 0], text => $col[$_])
505 for 0 .. $#col;
506
507 my @align = qw(1 0 1 1 -1);
508
509 my $y = 0;
510 for my $m (sort { $b->[3] <=> $a->[3] } map [split /\|/], split /\015?\012/, $buf) {
511 my ($ip, $last, $host, $users, $version, $desc, $ibytes, $obytes, $uptime) = @$m;
512
513 for ($desc) {
514 s/<br>/\n/gi;
515 s/<li>/\n· /gi;
516 s/<.*?>//sgi;
517 s/&/&amp;/g;
518 s/</&lt;/g;
519 s/>/&gt;/g;
520 }
521
522 $uptime = sprintf "%dd %02d:%02d:%02d",
523 (int $m->[8] / 86400),
524 (int $m->[8] / 3600) % 24,
525 (int $m->[8] / 60) % 60,
526 $m->[8] % 60;
527
528 $m = [$users, $host, $uptime, $version, $desc];
529
530 $y++;
531
532 $table->add (0, $y, new CFClient::UI::VBox children => [
533 (new CFClient::UI::Button text => " ", connect_activate => sub {
534 $HOST->set_text ($CFG->{host} = $host);
535 }),
536 (new CFClient::UI::Empty expand => 1),
537 ]);
538
539 $table->add ($_ + 1, $y, new CFClient::UI::Label align => $align[$_], text => $m->[$_], fontsize => 0.8)
540 for 0 .. $#$m;
541 }
542 }
543 });
544 }
545
546 sub server_setup {
547 my $dialog = new CFClient::UI::FancyFrame
548 title => "Server Setup",
549 child => (my $vbox = new CFClient::UI::VBox);
550
551 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
552 $table->add (0, 2, new CFClient::UI::Label valign => 0, align => 1, text => "Host:Port");
553
554 {
555 $table->add (1, 2, my $vbox = new CFClient::UI::VBox);
556
557 $vbox->add (my $HOST = new CFClient::UI::Entry expand => 1, text => $CFG->{host}, connect_changed => sub {
558 my ($self, $value) = @_;
559 $CFG->{host} = $value;
560 });
561
562 $METASERVER = metaserver_dialog;
563
564 $vbox->add (new CFClient::UI::Flopper expand => 1, text => "Metaserver", other => $METASERVER, connect_open => sub {
565 update_metaserver $HOST;
566 });
567 }
568
569 $table->add (0, 4, new CFClient::UI::Label valign => 0, align => 1, text => "Username");
570 $table->add (1, 4, new CFClient::UI::Entry text => $CFG->{user}, connect_changed => sub {
571 my ($self, $value) = @_;
572 $CFG->{user} = $value;
573 });
574
575 $table->add (0, 5, new CFClient::UI::Label valign => 0, align => 1, text => "Password");
576 $table->add (1, 5, new CFClient::UI::Entry text => $CFG->{password}, hidden => 1, connect_changed => sub {
577 my ($self, $value) = @_;
578 $CFG->{password} = $value;
579 });
580
581 $table->add (0, 6, new CFClient::UI::Label valign => 0, align => 1, text => "Def. say cmd");
582 $table->add (1, 6, my $saycmd = new CFClient::UI::Entry text => $CFG->{say_command}, connect_changed => sub {
583 my ($self, $value) = @_;
584 $CFG->{say_command} = $value;
585 });
586
587 $table->add (0, 7, new CFClient::UI::Label valign => 0, align => 1, text => "Map Size");
588 $table->add (1, 7, new CFClient::UI::Slider
589 req_w => 100,
590 range => [$CFG->{mapsize}, 10, 100 + 1, 1],
591 connect_changed => sub {
592 my ($self, $value) = @_;
593
594 $CFG->{mapsize} = $self->{range}[0] = $value = int $value;
595 },
596 );
597
598 $table->add (1, 8, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub {
599 start_game;
600 });
601
602 $dialog
603 }
604
605 sub message_window {
606 my $window = new CFClient::UI::FancyFrame
607 title => "Messages",
608 border_bg => [1, 1, 1, 0.5],
609 bg => [0.3, 0.3, 0.3, 0.8],
610 user_w => int $::WIDTH / 3,
611 user_h => int $::HEIGHT / 5,
612 child => (my $vbox = new CFClient::UI::VBox);
613
614 $vbox->add ($LOGVIEW = new CFClient::UI::TextView
615 expand => 1,
616 fontsize => $::CFG->{log_fontsize},
617 );
618
619 $vbox->add (my $input = new CFClient::UI::Entry
620 connect_focus_in => sub {
621 my ($input, $prev_focus) = @_;
622
623 delete $input->{refocus_map};
624
625 if ($prev_focus == $MAPWIDGET && $input->{auto_activated}) {
626 $input->{refocus_map} = 1;
627 }
628 delete $input->{auto_activated};
629 },
630 connect_activate => sub {
631 my ($input, $text) = @_;
632 $input->set_text ('');
633
634 if ($text =~ /^\/(.*)/) {
635 $::CONN->user_send ($1);
636 } else {
637 my $say_cmd = $::CFG->{say_command} || 'say';
638 $::CONN->user_send ("$say_cmd $text");
639 }
640 if ($input->{refocus_map}) {
641 delete $input->{refocus_map};
642 $MAPWIDGET->focus_in
643 }
644 },
645 connect_escape => sub {
646 $MAPWIDGET->focus_in
647 },
648 );
649
650 $CONSOLE = {
651 window => $window,
652 input => $input
653 };
654
655 $window
656 }
657
658 sub sdl_init {
659 CFClient::SDL_Init
660 and die "SDL::Init failed!\n";
661 }
662
663 sub video_init {
664 sdl_init;
665
666 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
667 $FULLSCREEN = $CFG->{fullscreen};
668 $FAST = $CFG->{fast};
669
670 CFClient::SDL_SetVideoMode $WIDTH, $HEIGHT, $FULLSCREEN
671 or die "SDL_SetVideoMode failed!\n";
672
673 $SDL_ACTIVE = 1;
674
675 $LAST_REFRESH = time - 0.01;
676
677 CFClient::gl_init;
678
679 $FONTSIZE = int $HEIGHT / 40 * $CFG->{gui_fontsize};
680
681 #############################################################################
682
683 $DEBUG_STATUS = new CFClient::UI::Label padding => 0, z => 100;
684 $DEBUG_STATUS->show;
685
686 $STATUS_LINE = new CFClient::UI::Label
687 padding => 0,
688 y => $HEIGHT - $FONTSIZE * 1.8;
689 $STATUS_LINE->show;
690
691 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
692 padding => 0,
693 fontsize => 0.8,
694 markup => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
695 $ALT_ENTER_MESSAGE->show;
696 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h});
697
698 $CFClient::UI::ROOT->add ($MAPWIDGET = new CFClient::MapWidget);
699 $MAPWIDGET->focus_in;
700 $MAPWIDGET->connect (activate_console => sub {
701 my ($mapwidget, $preset) = @_;
702
703 if ($CONSOLE) {
704 $CONSOLE->{input}->{auto_activated} = 1;
705 $CONSOLE->{input}->focus_in;
706
707 if ($preset && $CONSOLE->{input}->get_text eq '') {
708 $CONSOLE->{input}->set_text ($preset);
709 }
710 }
711 });
712
713 $CFClient::UI::ROOT->add ($BUTTONBAR = new CFClient::UI::HBox);
714
715 $BUTTONBAR->add (new CFClient::UI::Flopper text => "Client Setup", other => client_setup);
716 $BUTTONBAR->add (new CFClient::UI::Flopper text => "Server Setup", other => server_setup);
717 $BUTTONBAR->add (new CFClient::UI::Flopper text => "Message Window", other => message_window);
718
719 $BUTTONBAR->add (new CFClient::UI::Button text => "Save Config", connect_activate => sub {
720 CFClient::write_cfg "$Crossfire::VARDIR/pclientrc";
721 status "Configuration Saved";
722 });
723
724 $CFClient::UI::ROOT->add (make_gauge_window); # XXX: this has to be set before make_stats_window as make_stats_window calls update_stats_window which updated the gauges also X-D
725 $BUTTONBAR->add (new CFClient::UI::Flopper text => "Stats Window", other => make_stats_window);
726
727 $BUTTONBAR->{children}[1]->emit ("activate"); # pop up server setup
728
729
730 }
731
732 sub video_shutdown {
733 $CFClient::UI::ROOT->{children} = [];
734 undef $SDL_ACTIVE;
735 }
736
737 my @bgmusic = qw(game1.ogg game2.ogg game3.ogg game5.ogg game6.ogg ross1.ogg ross2.ogg ross3.ogg ross4.ogg ross5.ogg); #d#
738 my $bgmusic;#TODO#hack#d#
739
740 sub audio_music_finished {
741 return unless $CFG->{bgm_enable};
742
743 # TODO: hack, do play loop and mood music
744 $bgmusic = new_from_file CFClient::MixMusic CFClient::find_rcfile "music/$bgmusic[0]";
745 $bgmusic->play (0);
746
747 push @bgmusic, shift @bgmusic;
748 }
749
750 sub audio_init {
751 if ($CFG->{audio_enable}) {
752 if (open my $fh, "<:utf8", CFClient::find_rcfile "sounds/config") {
753 $SDL_MIXER = !CFClient::Mix_OpenAudio;
754 CFClient::Mix_AllocateChannels 8;
755 CFClient::MixMusic::volume $CFG->{bgm_volume} * 128;
756
757 audio_music_finished;
758
759 while (<$fh>) {
760 next if /^\s*#/;
761 next if /^\s*$/;
762
763 my ($file, $volume, $event) = split /\s+/, $_, 3;
764
765 push @SOUNDS, "$volume,$file";
766
767 $AUDIO_CHUNKS{"$volume,$file"} ||= do {
768 my $chunk = new_from_file CFClient::MixChunk CFClient::find_rcfile "sounds/$file";
769 $chunk->volume ($volume * 128 / 100);
770 $chunk
771 };
772 }
773 } else {
774 status "unable to open sound config: $!";
775 }
776 }
777 }
778
779 sub audio_shutdown {
780 CFClient::Mix_CloseAudio if $SDL_MIXER;
781 undef $SDL_MIXER;
782 @SOUNDS = ();
783 %AUDIO_CHUNKS = ();
784 }
785
786 my %animate_object;
787 my $animate_timer;
788
789 my $want_refresh;
790 my $can_refresh;
791
792 my $fps = 9;
793
794 sub force_refresh {
795 $fps = $fps * 0.95 + 1 / ($NOW - $LAST_REFRESH) * 0.05;
796 debug sprintf "%3.2f", $fps;
797
798 $want_refresh = 0;
799 $can_refresh = 0;
800
801 $CFClient::UI::ROOT->draw;
802
803 CFClient::SDL_GL_SwapBuffers;
804
805 $LAST_REFRESH = $NOW;
806 }
807
808 my $refresh_watcher = Event->timer (after => 0, hard => 1, interval => 1 / $MAX_FPS, cb => sub {
809 $NOW = time;
810
811 ($SDL_CB{$_->{type}} || sub { warn "unhandled event $_->{type}" })->($_)
812 for CFClient::SDL_PollEvent;
813
814 if (%animate_object) {
815 $_->animate ($LAST_REFRESH - $NOW) for values %animate_object;
816 $want_refresh++;
817 }
818
819 if ($want_refresh) {
820 force_refresh;
821 } else {
822 $can_refresh = 1;
823 }
824 });
825
826 sub refresh {
827 $want_refresh++;
828 }
829
830 sub animation_start {
831 my ($widget) = @_;
832 $animate_object{$widget} = $widget;
833 }
834
835 sub animation_stop {
836 my ($widget) = @_;
837 delete $animate_object{$widget};
838 }
839
840 @conn::ISA = Crossfire::Protocol::;
841
842 sub conn::stats_update {
843 my ($self, $stats) = @_;
844
845 update_stats_window ($stats);
846 }
847
848 sub conn::user_send {
849 my ($self, $command) = @_;
850
851 $self->send_command ($command);
852 status $command;
853 }
854
855 sub conn::map_scroll {
856 my ($self, $dx, $dy) = @_;
857
858 $MAP->scroll ($dx, $dy);
859 }
860
861 sub conn::feed_map1a {
862 my ($self, $data) = @_;
863
864 # $self->Crossfire::Protocol::feed_map1a ($data);
865
866 $MAP->map1a_update ($data);
867 $MAPWIDGET->update;
868 }
869
870 sub conn::flush_map {
871 my ($self) = @_;
872
873 my $map_info = delete $self->{map_info}
874 or return;
875
876 my ($hash, $x, $y, $w, $h) = @$map_info;
877
878 my $data = $MAP->get_rect ($x, $y, $w, $h);
879 $MAPCACHE->put ($hash => Compress::LZF::compress $data);
880 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
881 }
882
883 sub conn::map_clear {
884 my ($self) = @_;
885
886 $self->flush_map;
887 delete $self->{neigh_map};
888
889 $MAP->clear;
890 }
891
892
893 sub conn::load_map($$$) {
894 my ($self, $hash, $x, $y) = @_;
895
896 if (defined (my $data = $MAPCACHE->get ($hash))) {
897 $data = Compress::LZF::decompress $data;
898 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
899 for my $id ($MAP->set_rect ($x, $y, $data)) {
900 my $data = $TILECACHE->get ($id)
901 or next;
902
903 $self->set_texture ($id => $data);
904 }
905 }
906 }
907
908 # this method does a "flood fill" into every tile direction
909 # it assumes that tiles are arranged in a rectangular grid,
910 # i.e. a map is the same as the left of the right map etc.
911 # failure to comply are harmless and result in display errors
912 # at worst.
913 sub conn::flood_fill {
914 my ($self, $gx, $gy, $path, $hash, $flags) = @_;
915
916 # the server does not allow map paths > 6
917 return if 6 <= length $path;
918
919 my ($x0, $y0, $x1, $y1) = @{$self->{neigh_rect}};
920
921 for (
922 [1, 0, -1],
923 [2, 1, 0],
924 [3, 0, 1],
925 [4, -1, 0],
926 ) {
927 my ($tile, $dx, $dy) = @$_;
928
929 my $gx = $gx + $dx;
930 my $gy = $gy + $dy;
931
932 next unless $flags & (1 << ($tile - 1));
933 next if $self->{neigh_grid}{$gx, $gy}++;
934
935 my $neigh = $self->{neigh_map}{$hash} ||= [];
936 if (my $info = $neigh->[$tile]) {
937 my ($flags, $x, $y, $w, $h, $hash) = @$info;
938
939 $self->flood_fill ($gx, $gy, "$path$tile", $hash, $flags)
940 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
941
942 } else {
943 $self->send_mapinfo ("spatial $path$tile", sub {
944 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
945
946 return if $mode ne "spatial";
947
948 $x += $MAP->ox;
949 $y += $MAP->oy;
950
951 $self->load_map ($hash, $x, $y)
952 unless $self->{neigh_map}{$hash}[5]++;#d#
953
954 $neigh->[$tile] = [$flags, $x, $y, $w, $h, $hash];
955
956 $self->flood_fill ($gx, $gy, "$path$tile", $hash, $flags)
957 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
958 });
959 }
960 }
961 }
962
963 sub conn::map_change {
964 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
965
966 $self->flush_map;
967
968 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
969
970 my $mapmapw = 250;
971 my $mapmaph = 250;
972
973 $self->{neigh_rect} = [
974 $ox - $mapmapw * 0.5, $oy - $mapmapw * 0.5,
975 $ox + $mapmapw * 0.5 + $w, $oy + $mapmapw * 0.5 + $h,
976 ];
977
978 delete $self->{neigh_grid};
979 $self->flood_fill (0, 0, "", $hash, $flags);
980
981 $x += $ox;
982 $y += $oy;
983
984 $self->{map_info} = [$hash, $x, $y, $w, $h];
985
986 my $map = $self->{map_info}[0];
987 $map =~ s/^.*?\/([^\/]+)$/\1/;
988 $STATWIDS->{map}->set_text ("Map: " . $map);
989
990 $self->load_map ($hash, $x, $y);
991 }
992
993 sub conn::face_find {
994 my ($self, $facenum, $face) = @_;
995
996 my $hash = "$face->{chksum},$face->{name}";
997
998 my $id = $FACEMAP->get ($hash);
999
1000 unless ($id) {
1001 # create new id for face
1002 # i love transactions
1003 for (1..100) {
1004 my $txn = $CFClient::DB_ENV->txn_begin;
1005 my $status = $FACEMAP->db_get (id => $id, BerkeleyDB::DB_RMW);
1006 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
1007 $id++;
1008 if ($FACEMAP->put (id => $id) == 0
1009 && $FACEMAP->put ($hash => $id) == 0) {
1010 $txn->txn_commit;
1011
1012 goto gotid;
1013 }
1014 }
1015 $txn->abort;
1016 }
1017
1018 CFClient::fatal "maximum number of transaction retries reached - database problems?";
1019 }
1020
1021 gotid:
1022 $face->{id} = $id;
1023 $MAP->set_face ($facenum => $id);
1024 $TILECACHE->get ($id)
1025 }
1026
1027 sub conn::face_update {
1028 my ($self, $facenum, $face) = @_;
1029
1030 $TILECACHE->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes
1031
1032 $self->set_texture ($face->{id} => delete $face->{image});
1033 }
1034
1035 sub conn::set_texture {
1036 my ($self, $id, $data) = @_;
1037
1038 $self->{texture}[$id] ||= do {
1039 my $tex =
1040 new_from_image CFClient::Texture
1041 $data, minify => 1;
1042
1043 $MAP->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
1044 $MAPWIDGET->update;
1045
1046 $tex
1047 };
1048 }
1049
1050 sub conn::sound_play {
1051 my ($self, $x, $y, $soundnum, $type) = @_;
1052
1053 $SDL_MIXER
1054 or return;
1055
1056 my $chunk = $AUDIO_CHUNKS{$SOUNDS[$soundnum]}
1057 or return;
1058
1059 $chunk->play;
1060 # warn "sound $x,$y,$soundnum,$type\n";#d#
1061 }
1062
1063 sub conn::query {
1064 my ($self, $flags, $prompt) = @_;
1065
1066 #TODO, display dialog with relevant information
1067 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
1068 }
1069
1070 sub conn::drawinfo {
1071 my ($self, $color, $text) = @_;
1072
1073 my @color = (
1074 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
1075 [1.00, 1.00, 1.00],
1076 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
1077 [1.00, 0.00, 0.00],
1078 [1.00, 0.54, 0.00],
1079 [0.11, 0.56, 1.00],
1080 [0.93, 0.46, 0.00],
1081 [0.18, 0.54, 0.34],
1082 [0.56, 0.73, 0.56],
1083 [0.80, 0.80, 0.80],
1084 [0.55, 0.41, 0.13],
1085 [0.99, 0.77, 0.26],
1086 [0.74, 0.65, 0.41],
1087 );
1088
1089 $LOGVIEW->add_paragraph ($color[$color], $text);
1090 }
1091
1092 sub conn::spell_add {
1093 my ($self, $spell) = @_;
1094
1095 $MAPWIDGET->add_command ("invoke $spell->{name}", $spell->{message}, sub {
1096 });
1097 $MAPWIDGET->add_command ("cast $spell->{name}", $spell->{message}, sub {
1098 });
1099 }
1100
1101 sub conn::spell_delete {
1102 my ($self, $spell) = @_;
1103 }
1104
1105 sub conn::addme_success {
1106 my ($self) = @_;
1107
1108 for my $skill (values %{$self->{skill_info}}) {
1109 $MAPWIDGET->add_command ("ready_skill $skill", "", sub {
1110 });
1111 $MAPWIDGET->add_command ("use_skill $skill", "", sub {
1112 });
1113 }
1114 }
1115
1116 %SDL_CB = (
1117 CFClient::SDL_QUIT => sub {
1118 Event::unloop -1;
1119 },
1120 CFClient::SDL_VIDEORESIZE => sub {
1121 },
1122 CFClient::SDL_VIDEOEXPOSE => \&refresh,
1123 CFClient::SDL_ACTIVEEVENT => sub {
1124 # printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
1125 },
1126 CFClient::SDL_KEYDOWN => sub {
1127 if ($_[0]{mod} & CFClient::KMOD_ALT && $_[0]{sym} == 13) {
1128 # alt-enter
1129 video_shutdown;
1130 $CFG->{fullscreen} = !$CFG->{fullscreen};
1131 video_init;
1132 } else {
1133 CFClient::UI::feed_sdl_key_down_event ($_[0]);
1134 }
1135 },
1136 CFClient::SDL_KEYUP => \&CFClient::UI::feed_sdl_key_up_event,
1137 CFClient::SDL_MOUSEMOTION => \&CFClient::UI::feed_sdl_motion_event,
1138 CFClient::SDL_MOUSEBUTTONDOWN => \&CFClient::UI::feed_sdl_button_down_event,
1139 CFClient::SDL_MOUSEBUTTONUP => \&CFClient::UI::feed_sdl_button_up_event,
1140 CFClient::SDL_USEREVENT => \&audio_music_finished,
1141 );
1142
1143 #############################################################################
1144
1145 $SIG{INT} = $SIG{TERM} = sub { exit };
1146
1147 $TILECACHE = CFClient::db_table "tilecache";
1148 $FACEMAP = CFClient::db_table "facemap";
1149
1150 CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
1151
1152 my %DEF_CFG = (
1153 sdl_mode => 0,
1154 width => 640,
1155 height => 480,
1156 fullscreen => 0,
1157 fast => 0,
1158 fow_enable => 1,
1159 fow_intensity => 0.45,
1160 fow_smooth => 0,
1161 gui_fontsize => 1,
1162 log_fontsize => 1,
1163 gauge_fontsize => 1,
1164 gauge_size => 0.35,
1165 stat_fontsize => 1,
1166 mapsize => 100,
1167 host => "crossfire.schmorp.de",
1168 say_command => 'say',
1169 audio_enable => 1,
1170 bgm_enable => 1,
1171 bgm_volume => 0.25,
1172 );
1173
1174 while (my ($k, $v) = each %DEF_CFG) {
1175 $CFG->{$k} = $v unless exists $CFG->{$k};
1176 }
1177
1178 sdl_init;
1179
1180 @SDL_MODES = reverse
1181 grep $_->[0] >= 640 && $_->[1] >= 480,
1182 CFClient::SDL_ListModes;
1183
1184 @SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)";
1185
1186 $CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES;
1187
1188 {
1189 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
1190
1191 CFClient::add_font $_ for @fonts;
1192 CFClient::set_font $fonts[0];
1193 }
1194
1195 video_init;
1196 audio_init;
1197
1198 Event::loop;
1199
1200 END { CFClient::SDL_Quit }
1201
1202