ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Main.pm
Revision: 1.22
Committed: Sun Nov 4 02:13:53 2012 UTC (11 years, 7 months ago) by root
Branch: MAIN
Changes since 1.21: +0 -7 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 package DC::Main;
2    
3     # "dummy" package, which contains the real "main" program
4     # bin/deliantra is just a wrapper, to reduce the amount
5     # of code that needs to be in BEGIN clauses, and
6     # to make upgrades easier.
7    
8     package main;
9    
10     if ($ENV{DELIANTRA_CORO_DEBUG}) {
11     eval '
12     use Coro;
13     use Coro::EV;
14     use Coro::Debug;
15     our $debug = new_unix_server Coro::Debug "/tmp/dc";
16     ';
17     die if $@;
18     }
19    
20     use common::sense;
21     use Carp 'verbose';
22 root 1.3 use Cwd ();
23 root 1.9 use Digest::MD5 ();
24 root 1.1 use EV;
25     BEGIN { *time = \&EV::time }
26    
27     use List::Util qw(max min);
28    
29     use Deliantra;
30     use Deliantra::Protocol::Constants;
31    
32     use AnyEvent::Util ();
33     use AnyEvent::Socket ();
34     use AnyEvent::DNS ();
35    
36     use Compress::LZF;
37     use JSON::XS;
38 root 1.19 use Urlader;
39 root 1.1
40     use DC;
41    
42     sub crash($;$) {
43     # nop at compiletime
44     }
45    
46     BEGIN {
47     $SIG{__DIE__} = sub {
48 root 1.4 return if $^S; # quick reject
49    
50 root 1.20 # return if there are any eval contexts in the call stack
51 root 1.4 for my $i (0..999) {
52     my ($sub, $is_require) = (caller $i)[3, 7]
53     or last;
54     return if $sub eq "(eval)" && !$is_require;
55     }
56    
57 root 1.1 crash "CRASH/DIE: $_[0]" => 1;
58     DC::fatal Carp::longmess "$_[0]";
59     }
60     }
61    
62     use DC::OpenGL ();
63 root 1.16 use DC::Audio ();
64 root 1.1 use DC::Protocol;
65     use DC::DB;
66     use DC::UI;
67     use DC::UI::Canvas;
68     use DC::UI::Inventory;
69     use DC::UI::SpellList;
70     use DC::UI::Dockable;
71     use DC::UI::Dockbar;
72     use DC::UI::ChatView;
73     use DC::MessageDistributor;
74     use DC::Pod;
75     use DC::MapWidget;
76     use DC::Macro;
77    
78     $SIG{QUIT} = sub { Carp::cluck "QUIT" };
79     $SIG{PIPE} = 'IGNORE';
80    
81     $EV::DIED = sub {
82     crash "CRASH/EV::DIED: $@" => 0;
83     DC::fatal Carp::longmess $@;
84     };
85    
86     # initialise the resolver before going fullscreen,
87     # as vista forces us back to the desktop
88     # when doing this later.
89     use AnyEvent::DNS;
90     AnyEvent::DNS::resolver ();
91    
92     our $MAX_FPS = 60;
93    
94     our $DEFAULT_SERVER = "gameserver.deliantra.net";
95    
96     our $META_SERVER = "http://metaserver.schmorp.de/current.json";
97    
98     our $LAST_REFRESH;
99     our $NOW;
100    
101     our $CFG;
102     our $PROFILE; # current profile
103     our $FAST; # fast, low-quality mode, possibly useful for software-rendering
104     our $DELIANTRA_DEBUG = $ENV{DELIANTRA_DEBUG} * 1;
105    
106     our $WANT_REFRESH;
107    
108     our $MODE_SLIDER;
109     our $CAVEAT_LABEL;
110    
111     our @SDL_MODES;
112     our $SDL_REINIT = 1;
113     our $WIDTH;
114     our $HEIGHT;
115     our $FULLSCREEN;
116     our $FONTSIZE;
117    
118     our $FONT_PROP;
119     our $FONT_FIXED;
120    
121     our $CONN;
122    
123     our $MAP;
124     our $MAPMAP;
125     our $MAPWIDGET;
126     our $COMPLETER;
127     our $MENUFRAME; # the rectangle at the top
128     our $MENUBAR; # the hbox at the top
129     our $MENUPOPUP;
130     our $BUTTONBAR; # the menu buttons
131     our $METASERVER;
132     our $LOGIN_BUTTON;
133     our $QUIT_DIALOG;
134     our $HOST_ENTRY;
135     our $FULLSCREEN_ENABLE;
136     our $PICKUP_ENABLE;
137     our $SERVER_INFO;
138    
139     our $SETUP_DIALOG;
140     our $SETUP_NOTEBOOK;
141     our $SETUP_SERVER;
142     our $SETUP_LOGIN;
143     our $SETUP_KEYBOARD;
144    
145     our $PL_NOTEBOOK;
146     our $PL_WINDOW;
147    
148     our $MUSIC_PLAYING_WIDGET;
149     our $LICENSE_WIDGET;
150 root 1.6 our $DOWNLOADS_WIDGET;
151 root 1.1
152     our $PICKUP_PAGE;
153     our $INVENTORY_PAGE;
154     our $STATS_PAGE;
155     our $SKILL_PAGE;
156     our $SPELL_PAGE;
157     our $SPELL_LIST;
158    
159     our $HELP_WINDOW;
160     our $MESSAGE_WINDOW;
161     our $MESSAGE_DIST;
162     our $FLOORBOX;
163     our $GAUGES;
164     our $STATWIDS;
165    
166     our $SDL_ACTIVE;
167     our @SDL_CB;
168    
169     our $ALT_ENTER_MESSAGE;
170     our $STATUSBOX;
171     our $MODBOX;
172     our $DEBUG_STATUS;
173    
174     our $INV;
175     our $INVR;
176     our $INVR_HB;
177    
178     #############################################################################
179    
180     # write a crash message blockingly to the socket, if possible
181     # this is a bit too complicated for my tastes, but it was easy.
182     *crash = sub($;$) {
183     my ($msg, $backtrace) = @_;
184    
185     warn $msg;
186    
187     return unless $CONN;
188    
189     my $fh = $CONN->{fh}
190     or return;
191    
192     my $buf = delete $CONN->{wbuf};
193    
194     $buf .= pack "n/a*", "exti " . JSON::XS::encode_json [clientlog => undef, substr $msg, 0, 8000];
195    
196     AnyEvent::Util::fh_nonblocking $fh, 0;
197     syswrite $fh, $buf;
198     AnyEvent::Util::fh_nonblocking $fh, 1;
199    
200     $msg =~ s/\s+$//;
201    
202     # backtrace as second step, in case it crashes, too
203 root 1.10 crash Carp::longmess "$msg\nbacktrace, for client version $DC::VERSION$Urlader::EXE_VER, generated"
204 root 1.1 if $backtrace;
205     };
206    
207     sub clienterror($;$) {
208     my ($msg, $backtrace) = @_;
209    
210     warn $msg;
211    
212     return unless $CONN;
213    
214     $CONN->send_exti_msg (clientlog => $msg);
215 root 1.10 $CONN->send_exti_msg (clientlog => Carp::longmess "$msg\nbacktrace, for client version $DC::VERSION$Urlader::EXE_VER, generated") if $backtrace;
216 root 1.1 }
217    
218     #############################################################################
219    
220     sub status {
221     $STATUSBOX->add (DC::asxml $_[0], pri => -10, group => "status", timeout => 10, fg => [1, 1, 0, 1]);
222     }
223    
224     sub debug {
225     $DEBUG_STATUS->set_text ($_[0]);
226     }
227    
228     sub message {
229     $MESSAGE_DIST->message (@_);
230     }
231    
232     sub update_modbox {
233     my $mod = DC::SDL_GetModState;
234    
235     my $markup;
236    
237     $markup .= $mod & DC::KMOD_CTRL
238     ? ($MAPWIDGET->{ctrl} ? "[REPEAT]" : "[<span foreground='#888'>REPEAT</span>]")
239     : "[<span foreground='#888'> once </span>]";
240    
241     $markup .= $mod & DC::KMOD_SHIFT
242     ? ($MAPWIDGET->{shft} ? "[FIRE]" : "[<span foreground='#888'>FIRE</span>]")
243     : "[<span foreground='#888'>move</span>]";
244    
245     $markup .= $mod & (DC::KMOD_ALT | DC::KMOD_META)
246     ? "[ALT]"
247     : "[<span foreground='#888'>alt</span>]";
248    
249     $markup .= $mod & DC::KMOD_NUM
250     ? "[NUM]"
251     : "[<span foreground='#888'>num</span>]";
252    
253     # <tt> around next statement works around some bug that keeps the
254     # "font =>" from being used on windows
255     $MODBOX->set_markup ("<tt>$markup</tt>");
256     }
257    
258     #############################################################################
259     #TODO: maybe move into own audio module...
260    
261     our $SDL_MIXER;
262    
263     our $MUSIC_DEFAULT = "in_a_heartbeat.ogg";
264     our $MUSIC_WANT; # arryref of ambient music we want to play
265     our @MUSIC_HAVE; # ambient music we have on disk
266     our $MUSIC_START;
267     our @MUSIC_JINGLE; # which jingles to play next
268     our $MUSIC_PLAYING_DATA;
269     our $MUSIC_PLAYING_META;
270     our $MUSIC_PLAYER;
271     our $MUSIC_RESUME = 30; # resume music when played less than these many seconds before
272    
273     our %AUDIO_CHUNK; # audio "files"
274     our %AUDIO_PLAY; # which audio faces should be played
275    
276     sub audio_channel_finished {
277     my ($channel) = @_;
278    
279     # warn "channel $channel finished\n";#d#
280     }
281    
282     sub audio_sound_push($) {
283     my ($face) = @_;
284    
285     $CFG->{effects_enable}
286     or return;
287    
288     $AUDIO_PLAY{$face}
289     or return;
290    
291     if (my $chunk = $AUDIO_CHUNK{$face}) {
292     for (grep $_->[0] >= EV::now, @{(delete $AUDIO_PLAY{$face}) || []}) {
293     my (undef, $dx, $dy, $vol) = @$_;
294    
295     my $channel = DC::Channel::find;
296     $channel->volume ($vol * $CFG->{effects_volume} * 128 / 255);
297     $channel->set_position_r ($dx, $dy, 20);
298     $chunk->play ($channel);
299     }
300     } else {
301     # sound_meta not set means data is in flight either way
302     my $meta = $CONN->{face}[$face]
303     or return;
304    
305     $meta->{data}
306     or return;
307    
308     # if it's a jingle, play it as ambient music
309     if ($meta->{data}{jingle}) {
310     if (delete $AUDIO_PLAY{$face}) { # take the jingle out of the sound queue
311     push @MUSIC_JINGLE, $meta; # push it unto the music/jingle queue
312     &audio_music_push ($face);
313     }
314     } else {
315     # fetch from database
316     DC::DB::get res_data => $meta->{name}, sub {
317     my $rwops = new DC::RW $_[0];
318     my $chunk = new DC::MixChunk $rwops
319     or Carp::confess "sound face " . (JSON::XS::encode_json $meta) . " (" . (unpack "H64", $_[0]) . ") unloadable: " . DC::Mix_GetError;
320     $chunk->volume (($meta->{data}{volume} || 1) * 128);
321     $AUDIO_CHUNK{$face} = $chunk;
322    
323     audio_sound_push ($face);
324     };
325     }
326     }
327     }
328    
329     sub audio_sound_play {
330     my ($face, $dx, $dy, $vol) = @_;
331    
332     $SDL_MIXER
333     or return;
334     $CFG->{effects_enable}
335     or return;
336    
337     my $queue = $AUDIO_PLAY{$face} ||= [];
338     push @$queue, [EV::now + 0.6, $dx, $dy, $vol]; # do not play sound for outdated events
339     audio_sound_push $face
340     unless @$queue > 1;
341     }
342    
343     sub audio_music_set_meta {
344     my ($meta) = @_;
345    
346     $MUSIC_PLAYING_META = $meta;
347     $MUSIC_PLAYING_WIDGET->set_markup (
348     "<b>Name</b>: " . (DC::asxml $meta->{data}{name}) . "\n"
349     . "<b>Author</b>: " . (DC::asxml $meta->{data}{author}) . "\n"
350     . "<b>Source</b>: " . (DC::asxml $meta->{data}{source}) . "\n"
351     . "<b>License</b>: " . (DC::asxml $meta->{data}{license})
352     );
353     }
354    
355     sub audio_music_update_volume {
356     return unless $MUSIC_PLAYING_META;
357     my $volume = $MUSIC_PLAYING_META->{data}{volume} || 1;
358     my $base = $MUSIC_PLAYING_META->{data}{jingle} ? 1 : $CFG->{bgm_volume};
359     DC::MixMusic::volume $base * $volume * 128;
360     }
361    
362     sub audio_music_start {
363     my $meta = $MUSIC_PLAYING_META;
364    
365     DC::DB::get res_data => $meta->{name}, sub {
366     return unless $SDL_MIXER;
367    
368     # music might have changed...
369     $meta eq $MUSIC_PLAYING_META
370     or return &audio_music_start ();
371    
372     audio_music_update_volume;
373    
374     $MUSIC_PLAYING_DATA = \$_[0];
375    
376     $meta->{path} or length $_[0]
377     or return clienterror "empty music face from res_data ($meta->{face})";#d#
378    
379     my $rwops = $meta->{path}
380     ? (new_from_file DC::RW $meta->{path} or return clienterror "unable to load music face $meta->{path}: $!")#d#clienterror
381     : new DC::RW $$MUSIC_PLAYING_DATA;
382    
383     $MUSIC_PLAYER = new DC::MixMusic $rwops
384     or return clienterror "music face $meta->{face} unloadable: " . DC::Mix_GetError => 1;
385    
386     my $NOW = time;
387    
388     if ($MUSIC_PLAYING_META->{stop_time} > $NOW - $MUSIC_RESUME) {
389     my $pos = $MUSIC_PLAYING_META->{stop_pos};
390     $MUSIC_PLAYER->fade_in_pos (0, 700, $pos);
391     $MUSIC_START = time - $pos;
392     } else {
393     $MUSIC_PLAYER->play (0);
394     $MUSIC_START = time;
395     }
396    
397     delete $meta->{stop_time};
398     delete $meta->{stop_pos};
399     }
400     }
401    
402     sub audio_music_push {
403     return unless $SDL_MIXER;
404    
405     my $fade_out;
406    
407     if (@MUSIC_JINGLE) {
408     $fade_out = 333;
409     @MUSIC_HAVE = $MUSIC_JINGLE[0];
410    
411     } else {
412     return unless $CFG->{bgm_enable};
413    
414     $fade_out = 700;
415    
416     @MUSIC_HAVE =
417     grep $_ && $_->{data},
418     map $CONN->{face}[$_],
419     @$MUSIC_WANT;
420    
421     # randomize music a bit so that the order is not always the same
422     $_->{stop_time} ||= rand for @MUSIC_HAVE;
423    
424     # default MUSIC_HAVE == MUSIC_DEFAULT
425     @MUSIC_HAVE = { path => DC::find_rcfile "music/$MUSIC_DEFAULT" }
426     unless @MUSIC_HAVE;
427     }
428    
429     # if the currently playing song is acceptable, let it continue
430     return if grep $MUSIC_PLAYING_META == $_, @MUSIC_HAVE;
431    
432     my $NOW = time;
433    
434     if ($MUSIC_PLAYING_META) {
435     $MUSIC_PLAYING_META->{stop_time} = $NOW;
436     $MUSIC_PLAYING_META->{stop_pos} = $NOW - $MUSIC_START;
437     DC::MixMusic::fade_out $fade_out;
438     } else {
439     # sort by stop time, oldest first
440     @MUSIC_HAVE = sort { $a->{stop_time} <=> $b->{stop_time} } @MUSIC_HAVE;
441    
442     # if the most recently-played piece played very recently,
443     # resume it, else choose the oldest piece for rotation.
444     audio_music_set_meta
445     $MUSIC_HAVE[-1]{stop_pos} && $MUSIC_HAVE[-1]{stop_time} > $NOW - $MUSIC_RESUME
446     ? $MUSIC_HAVE[-1]
447     : $MUSIC_HAVE[0];
448    
449     audio_music_start;
450     }
451     }
452    
453     sub audio_music_set_ambient {
454     my ($songs) = @_;
455    
456     $MUSIC_WANT = $songs;
457     audio_music_push;
458     }
459    
460     sub audio_music_finished {
461     if ($MUSIC_PLAYING_META) {
462     $MUSIC_PLAYING_META->{stop_time} = time;
463     }
464    
465     # we compress multiple jingles of the same type
466     shift @MUSIC_JINGLE
467     while @MUSIC_JINGLE && $MUSIC_PLAYING_META == $MUSIC_JINGLE[0];
468    
469     $MUSIC_PLAYING_WIDGET->clear;
470    
471     undef $MUSIC_PLAYER;
472     undef $MUSIC_PLAYING_META;
473     undef $MUSIC_PLAYING_DATA;
474    
475     audio_music_push;
476     }
477    
478     sub audio_init {
479     if ($CFG->{audio_enable}) {
480 root 1.15 DC::Audio::init $CFG->{audio_driver};
481 root 1.1
482     if ($SDL_MIXER) {
483     audio_music_finished;
484     } else {
485     status "Unable to open sound device: there will be no sound";
486     }
487     } else {
488     undef $SDL_MIXER;
489     }
490    
491     sub audio_tab_update;
492     audio_tab_update;
493     }
494    
495     sub audio_shutdown {
496     if ($SDL_MIXER) {
497     DC::MixMusic::halt;
498     DC::Mix_AllocateChannels 0;
499     }
500    
501     undef $MUSIC_PLAYER;
502     undef $MUSIC_PLAYING_META;
503     undef $MUSIC_PLAYING_DATA;
504    
505     $MUSIC_WANT = [];
506     @MUSIC_JINGLE = ();
507     %AUDIO_PLAY = ();
508     %AUDIO_CHUNK = ();
509    
510     DC::Mix_CloseAudio if $SDL_MIXER;
511     undef $SDL_MIXER;
512    
513     DC::SDL_QuitSubSystem DC::SDL_INIT_AUDIO;
514     }
515    
516     #############################################################################
517 root 1.8 # Over-the-air updates
518    
519     sub ota_update {
520 root 1.9 my ($face, $size, $md5) = @_;
521 root 1.8
522     my $coro = Coro::async_pool {
523     my $override = "$Urlader::EXE_DIR/override";
524    
525     $MESSAGE_DIST->add_channel ({
526     id => "ota_update",
527     title => "Update",
528     tooltip => "<b>Software Update Log</b>",
529     });
530    
531     $MESSAGE_DIST->message ({ type => "ota_update", markup => "preparing override..." });
532    
533     my $fh = Coro::AIO::aio_open "$override.tmp", IO::AIO::O_WRONLY | IO::AIO::O_CREAT | IO::AIO::O_TRUNC, 0777;
534    
535     unless ($fh) {
536 root 1.9 $MESSAGE_DIST->message ({ type => "ota_update", markup => (DC::asxml "unable to write software update:\n$Urlader::EXE_DIR/override.tmp:\n$!") });
537 root 1.8 return;
538     }
539    
540     $MESSAGE_DIST->message ({ type => "ota_update", markup => "downloading $size bytes..." });
541    
542     my $cv = AE::cv;
543     my $error;
544    
545     $cv->begin (Coro::rouse_cb);
546     $CONN->ask_face (
547     $face,
548     -1000,
549     sub {
550     $STATUSBOX->add (
551     (sprintf "update download: %d/%d", $size - $_[1], $size),
552     pri => -9, group => "ota_update", timeout => 60, fg => [1, 1, 0, 1]
553     );
554    
555     $cv->begin;
556     my $len = length $_[2];
557     IO::AIO::aio_write $fh, $_[1], $len, $_[2], undef, sub {
558     $error ||= $_[0] != $len;
559     $cv->end;
560     };
561     },
562     sub {
563     $cv->end;
564     },
565     );
566    
567     Coro::rouse_wait;
568    
569     $STATUSBOX->clr_group ("ota_update");
570    
571     $error ||= Coro::AIO::aio_fsync $fh;
572     $error ||= Coro::AIO::aio_close $fh;
573    
574     if ($error) {
575     $MESSAGE_DIST->message ({ type => "ota_update", markup => "file write error, update aborted." });
576     Coro::AIO::aio_unlink "$override.tmp";
577     return;
578     }
579    
580 root 1.9 {
581     $MESSAGE_DIST->message ({ type => "ota_update", markup => "verifying update file..." });
582    
583     my $fh = Coro::AIO::aio_open "$override.tmp", IO::AIO::O_RDONLY, 0;
584    
585     if ($fh) {
586     $error ||= Coro::AIO::aio_stat "$override.tmp";
587     $error ||= -s _ != $size;
588 root 1.14 Coro::AIO::aio_readahead $fh, 0, $size;
589 root 1.9
590     my $f_md5 = new Digest::MD5;
591 root 1.13 binmode $fh; # ugh :(
592 root 1.9 $f_md5->addfile ($fh);
593     $f_md5 = $f_md5->hexdigest;
594     $error ||= $md5 ne $f_md5;
595     }
596     }
597    
598     if ($error) {
599     $MESSAGE_DIST->message ({ type => "ota_update", markup => "verification failed, update aborted." });
600     Coro::AIO::aio_unlink "$override.tmp";
601     return;
602     }
603    
604 root 1.8 $MESSAGE_DIST->message ({ type => "ota_update", markup => "replacing override file..." });
605    
606     if (Coro::AIO::aio_rename "$override.tmp", $override) {
607     $MESSAGE_DIST->message ({ type => "ota_update", markup => "unable to replace override file, update aborted." });
608     Coro::AIO::aio_unlink "$override.tmp";
609     }
610    
611 root 1.9 $MESSAGE_DIST->message ({ type => "ota_update", markup => "success - update becomes active after restarting." });
612 root 1.8 };
613    
614     $CONN->{ota_update} = Guard::guard {
615     $coro->cancel;
616     };
617     }
618    
619     sub ota_update_ask {
620 root 1.9 my ($ok, $face, $ver, $size, $md5, $changes) = @_;
621 root 1.8
622     $CONN->{w}{ota_dialog} = my $dialog = new DC::UI::Toplevel
623 root 1.9 x => "center",
624     y => "center",
625     z => 55,
626     force_w => $::WIDTH * 0.7,
627     force_h => $::HEIGHT * 0.7,
628     title => "Software update available",
629     child => my $vbox = new DC::UI::VBox,
630 root 1.8 ;
631    
632     $vbox->add (new DC::UI::Label
633     ellipsise => 0,
634     text => "The server offers a software update, "
635     . "do you want to start downloading this update in the background?",
636     );
637    
638     $vbox->add (new DC::UI::FancyFrame
639     expand => 1,
640 root 1.9 label => "Details",
641 root 1.8 child => (new DC::UI::TextScroller
642     expand => 1, fontsize => 0.8, padding_x => 4, padding_y => 4,
643     par => [{
644     markup => "<tt>Old revision: $Urlader::EXE_VER\n"
645     . "New revision: $ver\n"
646     . "Download size: $size bytes</tt>\n\n"
647 root 1.9 . "Changes:\n\n"
648 root 1.8 . DC::asxml $changes
649     }],
650     ),
651     );
652    
653     $vbox->add (my $hbox = new DC::UI::HBox);
654    
655     $hbox->add (new DC::UI::Button
656     expand => 1,
657     text => "Not now",
658     on_activate => sub {
659     $dialog->destroy;
660     0
661     }
662     );
663     $hbox->add (new DC::UI::Button
664     expand => 1,
665     text => "Yes, start downloading",
666     on_activate => sub {
667     $dialog->destroy;
668 root 1.9 ota_update $face, $size, $md5;
669 root 1.8 0
670     },
671     );
672    
673     $dialog->show;
674     }
675    
676     sub ota_update_check {
677     return unless defined $Urlader::EXE_ID;
678    
679     ::message { markup => "Checking for software update..." };
680    
681     $CONN->send_exti_req (ota_update => $Urlader::URLADER_VERSION, $Urlader::EXE_ID, $Urlader::EXE_VER, sub {
682 root 1.9 my ($ok, $face, $ver, $size, $md5, $changes) = @_;
683 root 1.8
684     if ($ok) {
685     if (defined $ver) {
686     ::message { markup => "Server offers version $ver (we are version $Urlader::EXE_VER)." };
687     &ota_update_ask;
688     } else {
689     ::message { markup => "Server has no newer version." };
690     }
691     } else {
692     ::message { markup => "Server does not support software update." };
693     }
694    
695     ()
696     });
697     }
698    
699     #############################################################################
700 root 1.1
701     sub destroy_query_dialog {
702     (delete $_[0]{query_dialog})->destroy
703     if $_[0]{query_dialog};
704     }
705    
706     # FIXME: a very ugly hack to wait for stat update look below! #d#
707     our $QUERY_TIMER; #d#
708    
709     # server query dialog
710     sub server_query {
711     my ($conn, $flags, $prompt) = @_;
712    
713     # FIXME: a very ugly hack to wait for stat update #d#
714     if ($prompt =~ /roll new stats/ and not $conn->{stat_change_with}) {
715     unless ($QUERY_TIMER) {
716     $QUERY_TIMER = EV::timer 1, 0, sub {
717     server_query ($conn, $flags, $prompt, 1);
718     $QUERY_TIMER = undef
719     };
720    
721     return;
722     }
723     }
724    
725     $conn->{query_dialog} = my $dialog = new DC::UI::Toplevel
726     x => "center",
727     y => "center",
728     title => "Server Query",
729     child => my $vbox = new DC::UI::VBox,
730     ;
731    
732     my @dialog = my $label = new DC::UI::Label
733     max_w => $::WIDTH * 0.8,
734     ellipsise => 0,
735     text => $prompt;
736    
737     if ($flags & CS_QUERY_YESNO) {
738     push @dialog, my $hbox = new DC::UI::HBox;
739    
740     $hbox->add (new DC::UI::Button
741     text => "No",
742     on_activate => sub {
743     $conn->send ("reply n");
744     $dialog->destroy;
745     0
746     }
747     );
748     $hbox->add (new DC::UI::Button
749     text => "Yes",
750     on_activate => sub {
751     $conn->send ("reply y");
752     destroy_query_dialog $conn;
753     0
754     },
755     );
756    
757     $dialog->grab_focus;
758    
759     } elsif ($flags & CS_QUERY_SINGLECHAR) {
760     if ($prompt =~ /Now choose a character|Press any key for the next race/i) {
761     $dialog->{tooltip} = "#charcreation_focus";
762    
763     unshift @dialog, new DC::UI::Label
764     max_w => $::WIDTH * 0.8,
765     ellipsise => 0,
766     markup => "\nOr use your keyboard and the text entry below:\n";
767    
768     unshift @dialog, my $table = new DC::UI::Table;
769    
770     $table->add_at (0, 0, new DC::UI::Button
771     text => "Next Race",
772     on_activate => sub {
773     $conn->send ("reply n");
774     destroy_query_dialog $conn;
775     0
776     },
777     );
778     $table->add_at (2, 0, new DC::UI::Button
779     text => "Accept",
780     on_activate => sub {
781     $conn->send ("reply d");
782     destroy_query_dialog $conn;
783     0
784     },
785     );
786    
787     if ($conn->{chargen_race_description}) {
788     unshift @dialog, new DC::UI::Label
789     max_w => $::WIDTH * 0.8,
790     ellipsise => 0,
791     markup => "<span foreground='#ccccff'>$conn->{chargen_race_description}</span>",
792     ;
793     }
794    
795     unshift @dialog, new DC::UI::Face
796     face => $conn->{player}{face},
797     bg => [.2, .2, .2, 1],
798     min_w => 64,
799     min_h => 64,
800     ;
801    
802     if ($conn->{chargen_race_title}) {
803     unshift @dialog, new DC::UI::Label
804     allign => 1,
805     ellipsise => 0,
806     markup => "<span foreground='#ccccff' size='large'>Race: $conn->{chargen_race_title}</span>",
807     ;
808     }
809    
810     unshift @dialog, new DC::UI::Label
811     max_w => $::WIDTH * 0.4,
812     ellipsise => 0,
813     markup => (DC::Pod::section_label ui => "chargen_race"),
814     ;
815    
816     } elsif ($prompt =~ /roll new stats/) {
817     if (my $stat = delete $conn->{stat_change_with}) {
818     $conn->send ("reply $stat");
819     destroy_query_dialog $conn;
820     return;
821     }
822    
823     unshift @dialog, new DC::UI::Label
824     max_w => $::WIDTH * 0.4,
825     ellipsise => 0,
826     markup => "\nOr use your keyboard and the text entry below:\n";
827    
828     unshift @dialog, my $table = new DC::UI::Table;
829    
830     # left: re-roll
831     $table->add_at (0, 0, new DC::UI::Button
832     text => "Roll Again",
833     on_activate => sub {
834     $conn->send ("reply y");
835     destroy_query_dialog $conn;
836     0
837     },
838     );
839    
840     # center: swap stats
841     my ($sw1, $sw2) = map +(new DC::UI::Selector
842     expand => 1,
843     value => $_,
844     options => [
845     [1 => "Str", "Strength ($conn->{stat}{+CS_STAT_STR})"],
846     [2 => "Dex", "Dexterity ($conn->{stat}{+CS_STAT_DEX})"],
847     [3 => "Con", "Constitution ($conn->{stat}{+CS_STAT_CON})"],
848     [4 => "Int", "Intelligence ($conn->{stat}{+CS_STAT_INT})"],
849     [5 => "Wis", "Wisdom ($conn->{stat}{+CS_STAT_WIS})"],
850     [6 => "Pow", "Power ($conn->{stat}{+CS_STAT_POW})"],
851     [7 => "Cha", "Charisma ($conn->{stat}{+CS_STAT_CHA})"],
852     ],
853     ), 1 .. 2;
854    
855     $table->add_at (2, 0, new DC::UI::Button
856     text => "Swap Stats",
857     on_activate => sub {
858     $conn->{stat_change_with} = $sw2->{value};
859     $conn->send ("reply $sw1->{value}");
860     destroy_query_dialog $conn;
861     0
862     },
863     );
864     $table->add_at (2, 1, new DC::UI::HBox children => [$sw1, $sw2]);
865    
866     # right: accept
867     $table->add_at (4, 0, new DC::UI::Button
868     text => "Accept",
869     on_activate => sub {
870     $conn->send ("reply n");
871     destroy_query_dialog $conn;
872     0
873     },
874     );
875    
876     unshift @dialog, my $hbox = new DC::UI::HBox;
877     for (
878     [Str => CS_STAT_STR],
879     [Dex => CS_STAT_DEX],
880     [Con => CS_STAT_CON],
881     [Int => CS_STAT_INT],
882     [Wis => CS_STAT_WIS],
883     [Pow => CS_STAT_POW],
884     [Cha => CS_STAT_CHA],
885     ) {
886     my ($name, $id) = @$_;
887     $hbox->add (new DC::UI::Label
888     markup => "$conn->{stat}{$id} <span foreground='yellow'>$name</span>",
889     expand => 1,
890     can_events => 1,
891     can_hover => 1,
892     tooltip => "#stat_$name",
893     );
894     }
895    
896     unshift @dialog, new DC::UI::Label
897     max_w => $::WIDTH * 0.4,
898     ellipsise => 0,
899     markup => (DC::Pod::section_label ui => "chargen_stats"),
900     ;
901     }
902    
903     push @dialog, my $entry = new DC::UI::Entry
904     on_changed => sub {
905     $conn->send ("reply $_[1]");
906     destroy_query_dialog $conn;
907     0
908     },
909     ;
910    
911     $entry->grab_focus;
912    
913     } else {
914     $dialog->{tooltip} = "Enter the reply and press return (click on the entry to make sure it has keyboard focus)";
915    
916     push @dialog, my $entry = new DC::UI::Entry
917     $flags & CS_QUERY_HIDEINPUT ? (hidden => "*") : (),
918     on_activate => sub {
919     $conn->send ("reply $_[1]");
920     destroy_query_dialog $conn;
921     0
922     },
923     ;
924    
925     $entry->grab_focus;
926     }
927    
928     $vbox->add (@dialog);
929     $dialog->show;
930     }
931    
932     sub dc_connect {
933     my ($host, $port) = @_;
934    
935     my $mapw = List::Util::min 48, List::Util::max 11, int 1.5 + $WIDTH * $CFG->{mapsize} * 0.01 / 32;
936     my $maph = List::Util::min 48, List::Util::max 11, int 1.5 + $HEIGHT * $CFG->{mapsize} * 0.01 / 32;
937    
938     $CONN =
939     new DC::Protocol
940     host => $host,
941     port => $port,
942     user => $PROFILE->{user},
943     pass => $PROFILE->{password},
944     mapw => $mapw,
945     maph => $maph,
946    
947     c_version => {
948     client => "deliantra",
949 root 1.10 clientver => "$DC::VERSION$Urlader::EXE_VER",
950 root 1.1 gl_vendor => DC::OpenGL::gl_vendor,
951     gl_version => DC::OpenGL::gl_version,
952     },
953    
954     map_widget => $MAPWIDGET,
955     statusbox => $STATUSBOX,
956     map => $MAP,
957     mapmap => $MAPMAP,
958     query => \&server_query,
959    
960     setup_req => {
961     smoothing => $CFG->{map_smoothing}*1,
962     },
963    
964     on_connect => sub {
965     if ($_[0]) {
966     DC::lowdelay fileno $CONN->{fh};
967    
968 root 1.8 ota_update_check;
969    
970 root 1.1 status "successfully connected to the server";
971     } else {
972     undef $CONN;
973     status "unable to connect: $!";
974     stop_game();
975     }
976     },
977     ;
978     }
979    
980     sub start_game {
981     status "logging in...";
982    
983     my $server = $PROFILE->{host} || $DEFAULT_SERVER;
984     my ($host, $port) = AnyEvent::Socket::parse_hostport $server, "deliantra=13327"
985     or return status "$server: unable to parse server address, try an empty field.";
986    
987     $LOGIN_BUTTON->set_text ("Logout");
988     $SETUP_DIALOG->hide;
989    
990     $MAP = new DC::Map;
991    
992     # hack to make SURE we find the IP address all right
993     # can be removed once AnyEvent::DNS is proven stable.
994     if ($host eq "gameserver.deliantra.net") {
995     AnyEvent::DNS::a "dnstest.deliantra.net", sub {
996     if ($_[0] ne "80.101.114.108") { # P-e-r-l
997     status "dns failure, trying differently";
998     $host = eval { Socket::inet_ntoa Socket::inet_aton "gameserver.deliantra.net" };
999     unless (defined $host) {
1000     status "dns failure, using hardcoded address";
1001     $host = "194.126.175.154";
1002     }
1003     }
1004    
1005     dc_connect $host, $port;
1006     };
1007     } else {
1008     dc_connect $host, $port;
1009     }
1010     }
1011    
1012     sub stop_game {
1013     crash "stop_game";
1014    
1015     $LOGIN_BUTTON->set_text ("Login / Register");
1016     $SETUP_NOTEBOOK->set_current_page ($SETUP_LOGIN);
1017     $SETUP_DIALOG->show;
1018     $PL_WINDOW->hide;
1019     $SPELL_LIST->clear_spells;
1020     $DC::UI::ROOT->emit (stop_game => ! ! $CONN);
1021    
1022     &audio_music_set_ambient ([]);
1023    
1024     return unless $CONN;
1025    
1026     status "connection closed";
1027    
1028     destroy_query_dialog $CONN;
1029     $CONN->destroy;
1030     $CONN = 0; # false, does not autovivify
1031    
1032     undef $MAP;
1033     }
1034    
1035     sub graphics_setup {
1036     my $vbox = new DC::UI::VBox;
1037    
1038     {
1039     $vbox->add (my $frame = new DC::UI::FancyFrame expand => 1, label => "Video Mode");
1040    
1041     $frame->add (my $table = new DC::UI::Table expand => 1, col_expand => [0, 1]);
1042    
1043     my $row = 0;
1044    
1045     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "OpenGL Info");
1046     $table->add_at (1, $row++, new DC::UI::Label fontsize => 0.8, text => DC::OpenGL::gl_vendor . ", " . DC::OpenGL::gl_version,
1047     can_events => 1,
1048     tooltip => "<tt><span size='8192'>" . (DC::OpenGL::gl_extensions) . "</span></tt>");
1049    
1050     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Caveats");
1051     $table->add_at (1, $row++, $CAVEAT_LABEL = new DC::UI::Label fontsize => 0.8,
1052     can_events => 1,
1053     tooltip => "This field shows any known issues with your config or driver, such as "
1054     . "a non-accelerated display format. You can try to work around these issues "
1055     . "by selecting a different video mode, changing the settings below or "
1056     . "by installing the right driver for your graphics card.");
1057    
1058     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "UI Theme");
1059     $table->add_at (1, $row++, $FULLSCREEN_ENABLE = new DC::UI::Selector
1060     value => $CFG->{uitheme},
1061     options => [
1062     [wood => "Wood (the default)"],
1063     [plain => "Plain (very)"],
1064     [blue => "Blue (dark)"],
1065     [metal => "Metal (light)"],
1066     ],
1067     tooltip => "Choose the User Interface theme that you like most :)",
1068     on_changed => sub { my ($self, $value) = @_; $CFG->{uitheme} = $value; 0 }
1069     );
1070    
1071     my $vidmode_tooltip =
1072     "<b>Video Mode.</b> The video mode to use for fullscreen (and the window size for windowed operation). "
1073     . "The format is <i>width</i> x <i>height</i> \@ <i>depth-per-channel</i> + <i>alpha-channel</i>.";
1074    
1075     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Video Mode");
1076     $table->add_at (1, $row++, my $hbox = new DC::UI::HBox);
1077    
1078     $hbox->add ($MODE_SLIDER = new DC::UI::Slider
1079     c_rescale => 1,
1080     force_w => $WIDTH * 0.1, expand => 1,
1081     range => [ ($CFG->{sdl_mode}) x 3 ],
1082     tooltip => $vidmode_tooltip);
1083     $hbox->add (my $mode_label = new DC::UI::Label
1084     height => 0.8, template => "9999x9999@9+9",
1085     can_events => 1, tooltip => $vidmode_tooltip);
1086    
1087     $MODE_SLIDER->connect (changed => sub {
1088     my ($self, $value) = @_;
1089    
1090     $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value;
1091     $mode_label->set_text (sprintf '%dx%d@%d+%d', @{$SDL_MODES[$value]});
1092     });
1093     $MODE_SLIDER->emit (changed => $MODE_SLIDER->{range}[0]);
1094    
1095     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Fullscreen");
1096     $table->add_at (1, $row++, $FULLSCREEN_ENABLE = new DC::UI::CheckBox
1097     state => $CFG->{fullscreen},
1098     tooltip => "Bring the client into fullscreen mode.",
1099     on_changed => sub { my ($self, $value) = @_; $CFG->{fullscreen} = $value; 0 }
1100     );
1101    
1102     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Force OpenGL 1.1");
1103     $table->add_at (1, $row++, new DC::UI::CheckBox
1104     state => $CFG->{force_opengl11},
1105     tooltip => "Limit Deliantra to use OpenGL 1.1 features only. This will normally result in "
1106     . "higher memory usage and slower performance. It will, however, help tremendously on "
1107     . "cards that claim to support a feature but fall back to software rendering. "
1108     . "Nvidia Geforce FX cards are known to claim features the hardware doesn't support, "
1109     . "but cards and drivers from other vendors (ATI) are often just as bad. "
1110     . "<b>If you experience extremely low framerates and your card should do better, try this option.</b>",
1111     on_changed => sub { my ($self, $value) = @_; $CFG->{force_opengl11} = $value; 0 }
1112     );
1113    
1114     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Forbid Alpha");
1115     $table->add_at (1, $row++, new DC::UI::CheckBox
1116     state => $CFG->{disable_alpha},
1117     tooltip => "Forbid the use of the alpha channel. This makes Deliantra look a lot worse "
1118     . "by disabling a number of textures and transparency effects. Normally, these "
1119     . "effects do not cost a lot of resources, but some graphics cards might fall "
1120     . "back to extremely slow rendering if this is enabled. If disabling this option "
1121     . "noticably improves the framerate of the client please report this! "
1122     . "<b>If you experience extremely low framerates and your card should do better, try this option.</b>",
1123     on_changed => sub {
1124     my ($self, $value) = @_;
1125     $CFG->{disable_alpha} = $value;
1126     $SDL_REINIT = 1; # SDL_SetVideoMode ignores GL attr changes
1127     0
1128     }
1129     );
1130    
1131     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Compress Textures");
1132     $table->add_at (1, $row++, new DC::UI::CheckBox
1133     state => $CFG->{texture_compression},
1134     tooltip => "Use texture compression. Normally this will not reduce visual quality noticable but "
1135     . "will save a lot of memory and increase performance (and also fall prey to the ever-buggy Mac OS X software renderer). "
1136     . "The compression algorithm can differ form card to card, so your mileage may vary. This setting is ignored in "
1137     . "forced OpenGL 1.1 mode and when using the Apple renderer.",
1138     on_changed => sub { my ($self, $value) = @_; $CFG->{texture_compression} = $value; 0 }
1139     );
1140    
1141     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Fast & Ugly");
1142     $table->add_at (1, $row++, new DC::UI::CheckBox
1143     state => $CFG->{fast},
1144     tooltip => "Lower the visual quality considerably to speed up rendering.",
1145     on_changed => sub { my ($self, $value) = @_; $CFG->{fast} = $value; 0 }
1146     );
1147    
1148     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "GUI Fontsize");
1149     $table->add_at (1, $row++, new DC::UI::Slider
1150     range => [$CFG->{gui_fontsize}, 0.5, 2, 0, 0.1],
1151     tooltip => "The base font size used by most GUI elements that do not have their own setting.",
1152     on_changed => sub { $CFG->{gui_fontsize} = $_[1]; 0 },
1153     );
1154    
1155     $table->add_at (1, $row++, new DC::UI::Button
1156     expand => 1, text => "Apply",
1157     tooltip => "Apply the video settings above.",
1158     on_activate => sub {
1159     video_shutdown ();
1160     video_init ();
1161     0
1162     }
1163     );
1164     }
1165    
1166     {
1167     $vbox->add (my $frame = new DC::UI::FancyFrame expand => 1, label => "Other Settings");
1168    
1169     $frame->add (my $table = new DC::UI::Table expand => 1, col_expand => [0, 1]);
1170    
1171     my $row = 0;
1172     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Smooth Movement");
1173     $table->add_at (1, $row++, new DC::UI::CheckBox
1174     state => $CFG->{smooth_movement},
1175     tooltip => "<b>Smooth Movement</b> tries to make movement, well, smoother, but also increases the framerate. "
1176     . "If you have a very slow system, non-accelerated drivers or plain dislike smooth scrolling, "
1177     . "then disable this option. Changes take effect immdiately.",
1178     on_changed => sub { my ($self, $value) = @_; $CFG->{smooth_movement} = $value; 0 }
1179     );
1180    
1181     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Smooth Transitions");
1182     $table->add_at (1, $row++, new DC::UI::CheckBox
1183     state => $CFG->{smooth_transitions},
1184     tooltip => "<b>Smooth Transitions</b> tries to blend the fog of war and lighting smoothly between updates. "
1185     . "If you have a very slow system, non-accelerated drivers or plain dislike smooth scrolling, "
1186     . "then disable this option. Requires Smooth Movement and OpenGL Multitexturing. Changes take effect immdiately.",
1187     on_changed => sub { my ($self, $value) = @_; $CFG->{smooth_transitions} = $value; 0 }
1188     );
1189    
1190    
1191     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Map Scale");
1192     $table->add_at (1, $row++, new DC::UI::Slider
1193     range => [(log $CFG->{map_scale}) / (log 2), -3, 1, 0, 1],
1194     tooltip => "Enlarge or shrink the displayed map. Changes are instant.",
1195     on_changed => sub { my ($self, $value) = @_; $CFG->{map_scale} = 2 ** $value; 0 }
1196     );
1197    
1198     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Map Smoothing");
1199     $table->add_at (1, $row++, new DC::UI::CheckBox
1200     state => $CFG->{map_smoothing},
1201     tooltip => "<b>Map Smoothing</b> tries to make tile borders less square. "
1202     . "This increases load on the graphics subsystem and works only with TRT servers. "
1203     . "Changes take effect at next login only.",
1204     on_changed => sub { my ($self, $value) = @_; $CFG->{map_smoothing} = $value; 0 }
1205     );
1206    
1207     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Fog of War");
1208     $table->add_at (1, $row++, new DC::UI::CheckBox
1209     state => $CFG->{fow_enable},
1210     tooltip => "<b>Fog-of-War</b> marks areas that cannot be seen by the player. Changes are instant.",
1211     on_changed => sub { my ($self, $value) = @_; $CFG->{fow_enable} = $value; 0 }
1212     );
1213    
1214     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "FoW Pattern");
1215     $table->add_at (1, $row++, new DC::UI::ImageButton
1216     tex => $DC::MapWidget::TEX_HIDDEN[$CFG->{fow_texture}],
1217     bg => [0.3, 0.3, 0.2],
1218     force_w => 64,
1219     force_h => 64,
1220     tooltip => "<b>Fog of War Pattern.</b> The pattern that is overlaid over areas hidden from view. Click to cycle through various alternatives. Changes are instant.",
1221     on_activate => sub {
1222     my ($self) = @_;
1223     $CFG->{fow_texture} = ($CFG->{fow_texture} + 1) % @DC::MapWidget::TEX_HIDDEN;
1224     $self->set_texture ($DC::MapWidget::TEX_HIDDEN[$CFG->{fow_texture}]);
1225     $MAPWIDGET->update;
1226     }
1227     );
1228    
1229     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "FoW Intensity");
1230     $table->add_at (1, $row++, new DC::UI::Slider
1231     range => [$CFG->{fow_intensity}, 0, 1, 0, 1 / 256],
1232     tooltip => "<b>Fog of War Lightness.</b> The higher the intensity, the lighter the Fog-of-War color. Changes are instant.",
1233     on_changed => sub { my ($self, $value) = @_; $CFG->{fow_intensity} = $value; 0 }
1234     );
1235    
1236     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Message Fontsize");
1237     $table->add_at (1, $row++, new DC::UI::Slider
1238     range => [$CFG->{log_fontsize}, 0.5, 2, 0, 0.1],
1239     tooltip => "The font size used by the <b>message/server log</b> window only. Changes are instant, "
1240     . "but you still need to press apply to correctly re-layout the widget.",
1241     on_changed => sub { $MESSAGE_DIST->set_fontsize ($CFG->{log_fontsize} = $_[1]); 0 },
1242     );
1243    
1244     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Gauge fontsize");
1245     $table->add_at (1, $row++, new DC::UI::Slider
1246     range => [$CFG->{gauge_fontsize}, 0.5, 2, 0, 0.1],
1247     tooltip => "Adjusts the fontsize of the gauges at the bottom right. Changes are instant.",
1248     on_changed => sub {
1249     $CFG->{gauge_fontsize} = $_[1];
1250     &set_gauge_window_fontsize;
1251     0
1252     }
1253     );
1254    
1255     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Gauge size");
1256     $table->add_at (1, $row++, new DC::UI::Slider
1257     range => [$CFG->{gauge_size}, 0.2, 0.8],
1258     tooltip => "Adjust the size of the stats gauges at the bottom right. Changes are instant.",
1259     on_changed => sub {
1260     $CFG->{gauge_size} = $_[1];
1261     $GAUGES->{win}->set_size ($WIDTH, int $HEIGHT * $CFG->{gauge_size});
1262     0
1263     }
1264     );
1265     }
1266    
1267     $vbox
1268     }
1269    
1270     our $AUDIO_HW_CHUNKSIZE;
1271     our $AUDIO_INFO;
1272    
1273     sub audio_tab_update {
1274     my ($freq, $format, $chans) = DC::Mix_QuerySpec;
1275    
1276     $AUDIO_HW_CHUNKSIZE->set_options ([
1277     [0, "default", "Use System Default"],
1278     map {
1279     my $ms = sprintf "%dms", 1000 * $_ / ($CFG->{audio_hw_frequency} || 22050);
1280     [$_, $ms, "$ms ($_ samples)"],
1281     } 256, 512, 1024, 2048, 4096, 8192, 16384, 32768
1282     ]);
1283    
1284     my $text = !$freq
1285     ? "audio is off"
1286     : "audio is enabled\n"
1287     . "driver: " . DC::SDL_AudioDriverName . "\n"
1288     . "frequency (Hz): $freq\n"
1289     . "channels: $chans\n"
1290     . "chunk decoders available: " . (join ", ", DC::MixChunk::decoders) . "\n"
1291     . "music decoders available: " . (join ", ", DC::MixMusic::decoders);
1292    
1293     $AUDIO_INFO->set_text ($text);
1294     }
1295    
1296     sub audio_setup {
1297     my $vbox = new DC::UI::VBox;
1298    
1299     $vbox->add (my $table = new DC::UI::Table expand => 1, col_expand => [0, 0, 1]);
1300    
1301     my $row = 0;
1302    
1303     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Audio Enable");
1304     $table->add_at (1, $row, new DC::UI::CheckBox
1305     state => $CFG->{audio_enable},
1306     tooltip => "<b>Master Audio Enable.</b> If enabled, sound effects and music will be played. If disabled, no audio will be used and the soundcard will not be opened.",
1307     on_changed => sub { $CFG->{audio_enable} = $_[1]; 1 }
1308     );
1309     $table->add_at (2, $row++, my $driver = new DC::UI::HBox expand => 1);
1310    
1311     $driver->add (new DC::UI::Label align => 1, text => " Audio driver override");
1312     $driver->add (new DC::UI::Entry
1313     text => $CFG->{audio_driver},
1314     template => "dsound1234",
1315     tooltip => "You can override the audio driver to use here. Leaving it empty will result "
1316     . "in Deliantra picking one automatically. GNU/Linux users often prefer specific "
1317     . "drivers though, and can experiment with <b>alsa</b>, <b>dsp</b>, <b>esd</b>, <b>pulse</b>, <b>arts</b>, <b>nas</b> "
1318 root 1.3 . "or other system-specific drivers. Selecting the wrong driver here will simply result "
1319 root 1.1 . "in no sound.",
1320     on_changed => sub { my ($self, $value) = @_; $CFG->{audio_driver} = $value; 1 }
1321     );
1322    
1323     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Sound Effects");
1324     $table->add_at (1, $row, new DC::UI::CheckBox
1325     expand => 1, state => $CFG->{effects_enable},
1326     tooltip => "If enabled, sound effects are enabled. If disabled, no sound effects will be played.",
1327     on_changed => sub {
1328     $CFG->{effects_enable} = $_[1];
1329     $CONN->update_fx_want if $CONN;
1330     1
1331     }
1332     );
1333     $table->add_at (2, $row++, new DC::UI::Slider
1334     expand => 1, range => [$CFG->{effects_volume}, 0, 1, 0, 1/128],
1335     tooltip => "The relative volume of sound effects. Best audio quality is achieved if this "
1336     . "is set highest (rightmost) and you use your operating system volume setting. Changes are instant.",
1337     on_changed => sub { $CFG->{effects_volume} = $_[1]; 1 }
1338     );
1339    
1340     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Background Music");
1341     $table->add_at (1, $row, new DC::UI::CheckBox
1342     expand => 1, state => $CFG->{bgm_enable},
1343     tooltip => "If enabled, playing of background music is enabled. If disabled, no background music will be played. Needs server reconnect to take effect.",
1344     on_changed => sub {
1345     $CFG->{bgm_enable} = $_[1];
1346     $CONN->update_fx_want if $CONN;
1347     audio_music_push;
1348     1
1349     }
1350     );
1351     $table->add_at (2, $row++, new DC::UI::Slider
1352     expand => 1, range => [$CFG->{bgm_volume}, 0, 1, 0, 1/128],
1353     tooltip => "The volume of the background music. Changes are instant.",
1354     on_changed => sub { $CFG->{bgm_volume} = $_[1]; audio_music_update_volume; 0 }
1355     );
1356    
1357     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Frequency");
1358     $table->add_at (1, $row++, new DC::UI::Selector
1359     c_colspan => 2, expand => 1,
1360     value => $CFG->{audio_hw_frequency},
1361     options => [
1362     [ 0, "default" , "Use System Default"],
1363     [11025, "11 kHz" , "11kHz (low quality)"],
1364     [22050, "22 kHz" , "22kHz (reduced quality, recommended)"],
1365     [44100, "44.1 kHz", "44.1kHz (cd quality)"],
1366     [48000, "48 kHz" , "48kHz (studio quality, not recommended)"],
1367     ],
1368     tooltip => "The sampling frequency to use. Higher sounds better, but also more cpu-intensive and might cause stuttering.",
1369     on_changed => sub {
1370     $CFG->{audio_hw_frequency} = $_[1];
1371     audio_tab_update;
1372     1
1373     }
1374     );
1375    
1376     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Channels");
1377     $table->add_at (1, $row++, new DC::UI::Selector
1378     c_colspan => 2, expand => 1,
1379     value => $CFG->{audio_hw_channels},
1380     options => [
1381     [0, "default" , "Use System Default"],
1382     [1, "Mono" , "Mono (single channel, low quality)"],
1383     [2, "Stereo" , "Stereo (dual channel, standard quality)"],
1384     [4, "4 Ch Surround", "4 Channel Surround Sound (3d sound, high quality)"],
1385     [6, "6 Ch Surround", "6 Channel Surround Sound (3d sound + center + lfe)"],
1386     ],
1387     tooltip => "The number of independent sound channels to use. Higher sounds better, but also more cpu-intensive and might cause stuttering.",
1388     on_changed => sub {
1389     $CFG->{audio_hw_channels} = $_[1];
1390     audio_tab_update;
1391     1
1392     }
1393     );
1394    
1395     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Latency");
1396     $table->add_at (1, $row++, $AUDIO_HW_CHUNKSIZE = new DC::UI::Selector
1397     c_colspan => 2, expand => 1,
1398     value => $CFG->{audio_hw_chunksize},
1399     tooltip => "The guarenteed latency. Lower is better, but also more cpu-intensive and might cause stuttering. If music playback "
1400     . "is stuttering, increase this value. Values of 50-150ms are optimal.",
1401     on_changed => sub {
1402     $CFG->{audio_hw_chunksize} = $_[1];
1403     audio_tab_update;
1404     1
1405     }
1406     );
1407    
1408     # should really be a slider
1409     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Mixer Voices");
1410     $table->add_at (1, $row++, new DC::UI::ValSlider
1411     c_colspan => 2, expand => 1,
1412     tooltip => "The number of simultaneous sound effects possible. Higher is better, but also more cpu-intensive and might cause stuttering.",
1413     range => [$::CFG->{audio_mix_channels}, 4, 32, 0, 1],
1414     template => ">= 99",
1415     on_changed => sub {
1416     my ($slider, $value) = @_;
1417    
1418     $CFG->{audio_mix_channels} = $value
1419     if $value;
1420     1;
1421     }
1422     );
1423    
1424     $table->add_at (1, $row++, new DC::UI::Button
1425     c_colspan => 2, expand => 1, text => "Apply",
1426     tooltip => "Apply the audio settings",
1427     on_activate => sub {
1428     audio_shutdown ();
1429     audio_init ();
1430     0
1431     }
1432     );
1433    
1434     $vbox->add (new DC::UI::FancyFrame
1435     expand => 1,
1436     label => "Audio Info",
1437     child => ($AUDIO_INFO = new DC::UI::Label ellipsise => 0),
1438     );
1439    
1440     audio_tab_update;
1441    
1442     $vbox
1443     }
1444    
1445     sub set_gauge_window_fontsize {
1446     for (map { $GAUGES->{$_} } grep { $_ ne 'win' } keys %{$GAUGES}) {
1447     $_->set_fontsize ($::CFG->{gauge_fontsize});
1448     }
1449     }
1450    
1451     sub make_gauge_window {
1452     my $gh = int $HEIGHT * $CFG->{gauge_size};
1453    
1454     $GAUGES->{win} = my $win = new DC::UI::Frame (
1455     force_x => 0,
1456     force_y => "max",
1457     force_w => $WIDTH,
1458     force_h => $gh,
1459     );
1460    
1461     $win->add (my $hbox = new DC::UI::HBox
1462     children => [
1463     (new DC::UI::HBox expand => 1),
1464     (new DC::UI::VBox children => [
1465     (new DC::UI::Empty expand => 1),
1466     (new DC::UI::Frame bg => [0, 0, 0, 0.4], child => ($FLOORBOX = new DC::UI::Table)),
1467     ]),
1468     (my $vbox = new DC::UI::VBox),
1469     ],
1470     );
1471    
1472     $vbox->add (new DC::UI::HBox
1473     expand => 1,
1474     children => [
1475     (new DC::UI::Empty expand => 1),
1476     (my $hb = new DC::UI::HBox),
1477     ],
1478     );
1479    
1480     $hb->add ($GAUGES->{hp} = new DC::UI::Gauge type => 'hp', tooltip => "#stat_health");
1481     $hb->add ($GAUGES->{mana} = new DC::UI::Gauge type => 'mana', tooltip => "#stat_mana");
1482     $hb->add ($GAUGES->{grace} = new DC::UI::Gauge type => 'grace', tooltip => "#stat_grace");
1483     $hb->add ($GAUGES->{food} = new DC::UI::Gauge type => 'food', tooltip => "#stat_food");
1484    
1485     &set_gauge_window_fontsize;
1486    
1487     $win
1488     }
1489    
1490     our $BW_WATCHER;
1491    
1492     sub debug_toggle($) {
1493     $DELIANTRA_DEBUG ^= $_[0];
1494    
1495     if ($DELIANTRA_DEBUG & 16) {
1496     $BW_WATCHER = EV::periodic 0, 1, 0, sub {
1497     return unless $CONN;
1498     debug sprintf "%8.2gKB/s", $CONN->{octets_in} / 1e3;
1499     $CONN->{octets_in} = 0;
1500     };
1501     } else {
1502     undef $BW_WATCHER;
1503     }
1504    
1505     }
1506    
1507     sub debug_setup {
1508     my $table = new DC::UI::Table;
1509    
1510     $table->add_at (0, 0, new DC::UI::Label text => "Widget Borders");
1511     $table->add_at (1, 0, new DC::UI::CheckBox on_changed => sub { debug_toggle 1; 0 });
1512     $table->add_at (0, 1, new DC::UI::Label text => "Tooltip Widget Info");
1513     $table->add_at (1, 1, new DC::UI::CheckBox on_changed => sub { debug_toggle 2; 0 });
1514     $table->add_at (0, 2, new DC::UI::Label text => "Show FPS");
1515     $table->add_at (1, 2, new DC::UI::CheckBox on_changed => sub { debug_toggle 4; 0 });
1516     $table->add_at (0, 3, new DC::UI::Label text => "Suppress Tooltips");
1517     $table->add_at (1, 3, new DC::UI::CheckBox on_changed => sub { debug_toggle 8; 0 });
1518     $table->add_at (0, 4, new DC::UI::Label text => "Show Bandwidth");
1519     $table->add_at (1, 4, new DC::UI::CheckBox on_changed => sub { debug_toggle 16; 0 });
1520    
1521     $table->add_at (0, 6, new DC::UI::Button text => "die on click(tm)", on_activate => sub { &DC::debug() } );
1522     $table->add_at (0, 7, new DC::UI::TextEdit text => "line1\0152\0153\nµikachu\nづx゙つ゛");#d#
1523    
1524     $table->add_at (7,7, my $t = new DC::UI::Table expand => 0);
1525     $t->add_at (0,0, new DC::UI::Label text => "a a", c_rowspan => 1, c_colspan => 2);
1526     $t->add_at (2,0, new DC::UI::Label text => "b\nb", c_rowspan => 2, c_colspan => 1, ellipsise => 0 );
1527     $t->add_at (1,2, new DC::UI::Label text => "c c", c_rowspan => 1, c_colspan => 2);
1528     $t->add_at (0,1, new DC::UI::Label text => "d\nd", c_rowspan => 2, c_colspan => 1, ellipsise => 0 );
1529     $t->add_at (1,1, new DC::UI::Label text => "e");
1530    
1531     $table->add_at (7, 6, my $c = new DC::UI::Canvas);
1532    
1533     $c->add_items ({
1534     type => "line_loop",
1535     color => [0, 1, 0],
1536     width => 9,
1537     coord_mode => "abs",
1538     coord => [[10, 5], [5, 50], [20, 5], [5, 60]],
1539     });
1540    
1541     $c->add_items ({
1542     type => "lines",
1543     color => [1, 1, 0],
1544     width => 2,
1545     coord_mode => "rel",
1546     coord => [[0,0], [1,1], [1,0], [0,1]],
1547     });
1548    
1549     $c->add_items ({
1550     type => "polygon",
1551     color => [0, 0.43, 0],
1552     width => 2,
1553     coord_mode => "rel",
1554     coord => [[0,0.2], [1,.4], [1,.6], [0,.8]],
1555     });
1556    
1557     $table
1558     }
1559    
1560     sub stats_window {
1561     my $r = new DC::UI::ScrolledWindow (
1562     expand => 1,
1563     scroll_y => 1
1564     );
1565     $r->add (my $vb = new DC::UI::VBox);
1566    
1567     $vb->add (new DC::UI::FancyFrame
1568     label => "Player",
1569     child => (my $pi = new DC::UI::VBox),
1570     );
1571    
1572     $pi->add ($STATWIDS->{title} = new DC::UI::Label text => "Title:", expand => 1, align => 0,
1573     can_hover => 1, can_events => 1,
1574     tooltip => "Your name and title. You can change your title by using the <b>title</b> command, if supported by the server.");
1575     $pi->add ($STATWIDS->{map} = new DC::UI::Label align => 0, text => "Map:", expand => 1,
1576     can_hover => 1, can_events => 1,
1577     tooltip => "The map you are currently on (if supported by the server).");
1578    
1579     $pi->add (my $hb0 = new DC::UI::HBox);
1580     $hb0->add ($STATWIDS->{weight} = new DC::UI::Label text => "Weight:", expand => 1, align => 0,
1581     can_hover => 1, can_events => 1,
1582     tooltip => "The weight of the player including all inventory items.");
1583     $hb0->add ($STATWIDS->{m_weight} = new DC::UI::Label align => 0, text => "Max weight:", expand => 1,
1584     can_hover => 1, can_events => 1,
1585     tooltip => "The weight limit: you cannot carry more than this.");
1586    
1587     $vb->add (new DC::UI::FancyFrame
1588     label => "Primary/Secondary Statistics",
1589     child => (my $hb = new DC::UI::HBox expand => 1),
1590     );
1591     $hb->add (my $tbl = new DC::UI::Table expand => 1);
1592    
1593     my $color2 = [1, 1, 0];
1594    
1595     for (
1596     [0, 0, st_str => "Str", 30],
1597     [0, 1, st_dex => "Dex", 30],
1598     [0, 2, st_con => "Con", 30],
1599     [0, 3, st_int => "Int", 30],
1600     [0, 4, st_wis => "Wis", 30],
1601     [0, 5, st_pow => "Pow", 30],
1602     [0, 6, st_cha => "Cha", 30],
1603    
1604     [2, 0, st_wc => "Wc", -120],
1605     [2, 1, st_ac => "Ac", -120],
1606     [2, 2, st_dam => "Dam", 120],
1607     [2, 3, st_arm => "Arm", 120],
1608     [2, 4, st_spd => "Spd", 10.54],
1609     [2, 5, st_wspd => "WSp", 10.54],
1610     ) {
1611     my ($col, $row, $id, $label, $template) = @$_;
1612    
1613     $tbl->add_at ($col , $row, $STATWIDS->{$id} = new DC::UI::Label
1614     font => $FONT_FIXED, can_hover => 1, can_events => 1,
1615     align => 1, template => $template, tooltip => "#stat_$label");
1616     $tbl->add_at ($col + 1, $row, $STATWIDS->{"$id\_lbl"} = new DC::UI::Label
1617     font => $FONT_FIXED, can_hover => 1, can_events => 1, fg => $color2,
1618     align => 0, text => $label, tooltip => "#stat_$label");
1619     }
1620    
1621     $vb->add (new DC::UI::FancyFrame
1622     label => "Resistancies",
1623     child => (my $tbl2 = new DC::UI::Table expand => 1, col_expand => [1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0]),
1624     );
1625    
1626     my $row = 0;
1627     my $col = 0;
1628    
1629     my %resist_names = (
1630     slow => ["Slow",
1631     "<b>Slow</b> (slows you down when you are hit by the spell. Monsters will have an opportunity to come near you faster and hit you more often.)"],
1632     holyw => ["Holy Word",
1633     "<b>Holy Word</b> (resistance you against getting the fear when someone whose god doesn't like you spells the holy word on you.)"],
1634     conf => ["Confusion",
1635     "<b>Confusion</b> (If you are hit by confusion you will move into random directions, and likely into monsters.)"],
1636     fire => ["Fire",
1637     "<b>Fire</b> (just your resistance to fire spells like burning hands, dragonbreath, meteor swarm fire, ...)"],
1638     depl => ["Depletion",
1639     "<b>Depletion</b> (some monsters and other effects can cause stats depletion)"],
1640     magic => ["Magic",
1641     "<b>Magic</b> (resistance to magic spells like magic missile or similar)"],
1642     drain => ["Draining",
1643     "<b>Draining</b> (some monsters (e.g. vampires) and other effects can steal experience)"],
1644     acid => ["Acid",
1645     "<b>Acid</b> (resistance to acid, acid hurts pretty much and also corrodes your weapons)"],
1646     pois => ["Poison",
1647     "<b>Poison</b> (resistance to getting poisoned)"],
1648     para => ["Paralysation",
1649     "<b>Paralysation</b> (this resistance affects the chance you get paralysed)"],
1650     deat => ["Death",
1651     "<b>Death</b> (resistance against death spells)"],
1652     phys => ["Physical",
1653     "<b>Physical</b> (this is the resistance against physical attacks, like when a monster hit you in melee combat. The value displayed here is also displayed as the 'Arm' secondary stat.)"],
1654     blind => ["Blind",
1655     "<b>Blind</b> (blind resistance affects the chance of a successful blinding attack)"],
1656     fear => ["Fear",
1657     "<b>Fear</b> (this attack will drive you away from monsters who cast this and hit you successfully, being resistant to this helps a lot when fighting those monsters)"],
1658     tund => ["Turn undead",
1659     "<b>Turn undead</b> (affects your resistancy to various forms of 'turn undead' spells. Only relevant when you are, in fact, undead..."],
1660     elec => ["Electricity",
1661     "<b>Electricity</b> (resistance against electricity, spells like large lightning, small lightning, ...)"],
1662     cold => ["Cold",
1663     "<b>Cold</b> (this is your resistance against cold spells like icestorm, snowstorm, ...)"],
1664     ghit => ["Ghost hit",
1665     "<b>Ghost hit</b> (special attack used by ghosts and ghost-like beings)"],
1666     );
1667    
1668     for (qw/slow holyw conf fire depl magic
1669     drain acid pois para deat phys
1670     blind fear tund elec cold ghit/)
1671     {
1672     $tbl2->add_at ($col + 2, $row,
1673     $STATWIDS->{"res_$_"} =
1674     new DC::UI::Label
1675     font => $FONT_FIXED,
1676     template => "-100%",
1677     align => 1,
1678     can_events => 1,
1679     can_hover => 1,
1680     tooltip => $resist_names{$_}->[1],
1681     );
1682     $tbl2->add_at ($col + 1, $row, new DC::UI::Image
1683     font => $FONT_FIXED,
1684     can_hover => 1,
1685     can_events => 1,
1686     path => "ui/resist/resist_$_.png",
1687     tooltip => $resist_names{$_}->[1],
1688     );
1689     $tbl2->add_at ($col + 0, $row, new DC::UI::Label
1690     text => $resist_names{$_}->[0],
1691     font => $FONT_FIXED,
1692     align => 1,
1693     can_hover => 1,
1694     can_events => 1,
1695     tooltip => $resist_names{$_}->[1],
1696     );
1697    
1698     $row++;
1699     if ($row % 6 == 0) {
1700     $col += 4;
1701     $row = 0;
1702     }
1703     }
1704    
1705     #update_stats_window ({});
1706    
1707     $r
1708     }
1709    
1710     sub skill_window {
1711     my $sw = new DC::UI::ScrolledWindow (expand => 1);
1712    
1713     $sw->add ($STATWIDS->{skill_tbl} = new DC::UI::Table expand => 1, col_expand => [0, 0, 1, .1, 0, 0, 1, .1]);
1714    
1715     $sw
1716     }
1717    
1718     sub formsep($) {
1719     scalar reverse join ",", unpack "(A3)*", reverse $_[0] * 1
1720     }
1721    
1722     my $METASERVER_ATIME;
1723    
1724     sub update_metaserver {
1725     my ($metaserver_dialog) = @_;
1726    
1727     $METASERVER = $metaserver_dialog
1728     if defined $metaserver_dialog;
1729    
1730     return if $METASERVER_ATIME > time;
1731     $METASERVER_ATIME = time + 60;
1732    
1733     my $table = $METASERVER->{table};
1734     $table->clear;
1735     $table->add_at (0, 0, my $label = new DC::UI::Label max_w => $WIDTH * 0.8, text => "fetching server list...");
1736    
1737     my $ok = 0;
1738    
1739     DC::background {
1740     my $ua = DC::lwp_useragent;
1741    
1742     DC::background_msg DC::decode_json +(DC::lwp_check $ua->get ($META_SERVER))->decoded_content;
1743     } sub {
1744     my ($msg) = @_;
1745     if ($msg) {
1746     $table->clear;
1747    
1748     my @tip = (
1749     "The current number of users logged in on the server.",
1750     "The hostname of the server.",
1751     "The time this server has been running without being restarted.",
1752     "Short information about this server provided by its admins.",
1753     );
1754     my @col = qw(#Users Host Uptime Version Description);
1755     $table->add_at ($_, 0, new DC::UI::Label
1756     can_hover => 1, can_events => 1, fg => [1, 1, 0],
1757     text => $col[$_], tooltip => $tip[$_])
1758     for 0 .. $#col;
1759    
1760     my @align = qw(1 0.5 1 1 0);
1761    
1762     my $y = 0;
1763     for my $m (@{ $msg->{servers} }) {
1764     my ($ip, $last, $host, $users, $version, $desc, $ibytes, $obytes, $uptime, $highlight) =
1765     @$m{qw(ip age hostname users version description ibytes obytes uptime highlight)};
1766    
1767     for ($desc) {
1768     s/<br>/\n/gi;
1769     s/<li>/\n· /gi;
1770     s/<.*?>//sgi;
1771     s/&amp;/&/g;
1772     s/&lt;/</g;
1773     s/&gt;/>/g;
1774     }
1775    
1776     $uptime = sprintf "%dd %02d:%02d:%02d",
1777     (int $uptime / 86400),
1778     (int $uptime / 3600) % 24,
1779     (int $uptime / 60) % 60,
1780     $uptime % 60;
1781    
1782     $m = [$users, $host, $uptime, $version, $desc];
1783    
1784     $y++;
1785    
1786     $table->add_at (scalar @$m, $y, new DC::UI::VBox children => [
1787     (new DC::UI::Button
1788     text => "Use",
1789     tooltip => "Put this server into the <b>Host:Port</b> field",
1790     on_activate => sub {
1791     $HOST_ENTRY->set_text ($CFG->{profile}{default}{host} = $host);
1792     $METASERVER->hide;
1793     0
1794     },
1795     ),
1796     (new DC::UI::Empty expand => 1),
1797     ]);
1798    
1799     $table->add_at ($_, $y, new DC::UI::Label
1800     max_w => $::WIDTH * 0.4,
1801     ellipsise => 0,
1802     align => $align[$_],
1803     text => $m->[$_],
1804     tooltip => $tip[$_],
1805     fg => ($highlight ? [1, 1, 1] : [.7, .7, .7]),
1806     can_hover => 1,
1807     can_events => 1,
1808     fontsize => 0.8)
1809     for 0 .. $#$m;
1810     }
1811     } else {
1812     $ok or $label->set_text ("error while contacting metaserver");
1813     }
1814     };
1815    
1816     }
1817    
1818     sub metaserver_dialog {
1819     my $vbox = new DC::UI::VBox;
1820     my $table = new DC::UI::Table;
1821     $vbox->add (new DC::UI::ScrolledWindow expand => 1, child => $table);
1822    
1823     my $dialog = new DC::UI::Toplevel
1824     title => "Server List",
1825     name => 'metaserver_dialog',
1826     x => 'center',
1827     y => 'center',
1828     z => 3,
1829     force_w => $::WIDTH * 0.9,
1830     force_h => $::HEIGHT * 0.7,
1831     child => $vbox,
1832     has_close_button => 1,
1833     table => $table,
1834     on_visibility_change => sub {
1835     update_metaserver ($_[0]) if $_[1];
1836     0
1837     },
1838     ;
1839    
1840     $dialog
1841     }
1842    
1843     sub login_setup {
1844     my $vbox = new DC::UI::VBox;
1845    
1846     $vbox->add (new DC::UI::FancyFrame
1847     label => "Login Settings",
1848     child => (my $table = new DC::UI::Table expand => 1, col_expand => [0, 1]),
1849     );
1850    
1851     $table->add_at (0, 4, new DC::UI::Label align => 1, text => "Username");
1852     $table->add_at (1, 4, new DC::UI::Entry
1853     text => $PROFILE->{user},
1854     tooltip => "The name of your character on the server. The name is case-sensitive!",
1855     on_changed => sub { my ($self, $value) = @_; $PROFILE->{user} = $value; 1 }
1856     );
1857    
1858     $table->add_at (0, 5, new DC::UI::Label align => 1, text => "Password");
1859     $table->add_at (1, 5, new DC::UI::Entry
1860     text => $PROFILE->{password},
1861     hidden => 1,
1862     tooltip => "The password for your character.",
1863     on_changed => sub { my ($self, $value) = @_; $PROFILE->{password} = $value; 1 }
1864     );
1865    
1866     $table->add_at (1, 11, $LOGIN_BUTTON = new DC::UI::Button
1867     expand => 1,
1868     text => "Login / Register",
1869     tooltip => "This button will either login to the account configured above or register a new account.",
1870     on_activate => sub {
1871     $CONN ? stop_game
1872     : start_game;
1873     1
1874     },
1875     );
1876    
1877     $vbox->add (new DC::UI::FancyFrame
1878     label => "How to Play",
1879     min_h => 240,
1880     child => (new DC::UI::Label valign => 0, ellipsise => 0,
1881     markup =>
1882     "First select a suitable video resolution in the <b>Graphics</b> tab, above.\n\n"
1883     . "Then register a new account (or use an existing one if you have one). "
1884     . "To register an account, choose a username that hasn't been taken yet (just guess) and "
1885     . "try to log-in. Follow the instructions in the Log tab in the message window.",
1886     ),
1887     );
1888    
1889     $vbox
1890     }
1891    
1892     sub server_setup {
1893     my $vbox = new DC::UI::VBox;
1894    
1895     $vbox->add (new DC::UI::FancyFrame
1896     label => "Connection Settings",
1897     child => (my $table = new DC::UI::Table expand => 1, col_expand => [0, 1]),
1898     );
1899    
1900     my $row = 0;
1901    
1902     $table->add_at (0, ++$row, new DC::UI::Label align => 1, text => "Host:Port");
1903     {
1904     $table->add_at (1, $row, my $vbox = new DC::UI::VBox);
1905    
1906     $vbox->add (
1907     $HOST_ENTRY = new DC::UI::Entry
1908     expand => 1,
1909     text => $PROFILE->{host},
1910     tooltip => "The hostname or ip address of the Deliantra server to connect to (e.g. <b>gameserver.deliantra.net</b>)",
1911     on_changed => sub {
1912     my ($self, $value) = @_;
1913     $PROFILE->{host} = $value;
1914     1
1915     }
1916     );
1917    
1918     if (0) { #d# disabled
1919     $vbox->add (new DC::UI::Button
1920     expand => 1,
1921     text => "Server List",
1922     other => $METASERVER,
1923     tooltip => "Show a list of available Deliantra servers",
1924     on_activate => sub { $METASERVER->toggle_visibility; 0 },
1925     on_visibility_change => sub { $METASERVER->hide unless $_[1]; 1 },
1926     );
1927     }#d#
1928     }
1929    
1930     $table->add_at (0, ++$row, new DC::UI::Label align => 1, text => "Map Size");
1931     $table->add_at (1, $row, new DC::UI::Slider
1932     force_w => 100,
1933     range => [$CFG->{mapsize}, 10, 100, 0, 1],
1934     tooltip => "This is the size of the portion of the map update the server sends you. "
1935     . "If you set this to a high value you will be able to see further, "
1936     . "but you also increase bandwidth requirements and latency. "
1937     . "This option is only used once at log-in.",
1938     on_changed => sub { my ($self, $value) = @_; $CFG->{mapsize} = $self->{range}[0] = $value = int $value; 1 },
1939     );
1940    
1941     $table->add_at (0, ++$row, new DC::UI::Label align => 1, text => "Output-Rate");
1942     $table->add_at (1, $row, new DC::UI::Entry
1943     text => $CFG->{output_rate},
1944     tooltip => "The maximum bandwidth in bytes per second that the server should not exceed "
1945     . "when sending data. When 0 or unset, the server "
1946     . "default will be used, which is usually around 100kb/s. Most servers will "
1947     . "dynamically find an optimal rate, so adjust this only when necessary.",
1948     on_changed => sub { $CFG->{output_rate} = $_[1]; 1 },
1949     );
1950    
1951     $vbox->add (new DC::UI::FancyFrame
1952     label => "Server Info",
1953     child => ($SERVER_INFO = new DC::UI::Label ellipsise => 0),
1954     );
1955    
1956     $vbox
1957     }
1958    
1959     sub client_setup {
1960 root 1.2 my $vbox = new DC::UI::VBox;
1961 root 1.1
1962 root 1.2 $vbox->add (my $top = new DC::UI::FancyFrame expand => 1, label => "Client Settings");
1963     $vbox->add (my $bot = new DC::UI::FancyFrame expand => 1, label => "Client Info");
1964 root 1.1
1965 root 1.2 {
1966     $top->add (my $table = new DC::UI::Table expand => 1, col_expand => [0, 1]);
1967    
1968     my $row = 0;
1969    
1970     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Tip of the day");
1971     $table->add_at (1, $row++, new DC::UI::CheckBox
1972     c_colspan => 2,
1973     state => $CFG->{show_tips},
1974     tooltip => "Show the <b>Tip of the day</b> window at startup?",
1975     on_changed => sub {
1976     my ($self, $value) = @_;
1977     $CFG->{show_tips} = $value;
1978     0
1979     }
1980     );
1981    
1982     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Message Window Size");
1983     $table->add_at (1, $row++, my $saycmd = new DC::UI::Entry
1984     c_colspan => 2,
1985     text => $CFG->{logview_max_par},
1986     tooltip => "This is maximum number of messages remembered in the <b>Message</b> window. If the server "
1987     . "sends more messages than this number, older messages get removed to save memory and "
1988     . "computing time. A value of <b>0</b> disables this feature, but that is not recommended.",
1989     on_changed => sub {
1990     my ($self, $value) = @_;
1991     $MESSAGE_DIST->set_max_par ($CFG->{logview_max_par} = $value*1);
1992     0
1993     },
1994     );
1995    
1996     $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Config Autosave");
1997     $table->add_at (1, $row, new DC::UI::CheckBox
1998     state => $CFG->{config_autosave},
1999     tooltip => "Normally, configuration settings and the user interface layout "
2000     . "are saved on client exit. You can disable this behaviour by "
2001     . "unchecking this checkbox.",
2002     on_changed => sub {
2003     my ($self, $value) = @_;
2004     $CFG->{config_autosave} = $value;
2005     0
2006     }
2007     );
2008     $table->add_at (2, $row++, new DC::UI::Button
2009     text => "Save Now",
2010     tooltip => "Use this to manually save configuration and UI layout when "
2011     . "autosave is disabled.",
2012     on_activate => sub {
2013 root 1.17 DC::save_cfg;
2014 root 1.2 0
2015     }
2016     );
2017     }
2018    
2019     {
2020     $bot->add (my $table = new DC::UI::Table expand => 1, col_expand => [0, 1]);
2021 root 1.1
2022 root 1.2 my $row = 0;
2023 root 1.1
2024 root 1.2 $table->add_at (0, $row , new DC::UI::Label align => 1, text => "Data Directory");
2025     $table->add_at (1, $row++, new DC::UI::Label align => 0, text => $Deliantra::VARDIR, tooltip => "");
2026     $table->add_at (0, $row , new DC::UI::Label align => 1, text => "Database Directory");
2027     $table->add_at (1, $row++, new DC::UI::Label align => 0, text => $DC::DB::DBDIR, tooltip => "");
2028 root 1.5 $table->add_at (0, $row , new DC::UI::Label align => 1, text => "Urlader (Prebuilt)");
2029     $table->add_at (1, $row++, new DC::UI::Label align => 0, text => $ENV{URLADER_VERSION}, tooltip => "");
2030 root 1.2 $table->add_at (0, $row , new DC::UI::Label align => 1, text => "Branch (Prebuilt)");
2031 root 1.5 $table->add_at (1, $row++, new DC::UI::Label align => 0, text => $ENV{URLADER_EXE_ID}, tooltip => "");
2032 root 1.8 $table->add_at (0, $row , new DC::UI::Label align => 1, text => "Revision (Prebuilt)");
2033 root 1.5 $table->add_at (1, $row++, new DC::UI::Label align => 0, text => $ENV{URLADER_EXE_VER}, tooltip => "");
2034 root 1.2 }
2035 root 1.1
2036 root 1.2 $vbox
2037 root 1.1 }
2038    
2039     sub autopickup_setup {
2040     my $r = new DC::UI::ScrolledWindow (
2041     expand => 1,
2042     scroll_y => 1
2043     );
2044     $r->add (my $table = new DC::UI::Table
2045     row_expand => [0],
2046     col_expand => [0, 1, 0, 1],
2047     );
2048    
2049     for (
2050     ["General", 0, 0,
2051     # ["Inhibit autopickup" => PICKUP_INHIBIT],
2052     ["Stop before pickup" => PICKUP_STOP],
2053     ["Debug autopickup" => PICKUP_DEBUG],
2054     ],
2055     ["Weapons", 0, 6,
2056     ["All weapons" => PICKUP_ALLWEAPON],
2057     ["Missile weapons" => PICKUP_MISSILEWEAPON],
2058     ["Bows" => PICKUP_BOW],
2059     ["Arrows" => PICKUP_ARROW],
2060     ],
2061     ["Armour", 0, 12,
2062     ["Helmets" => PICKUP_HELMET],
2063     ["Shields" => PICKUP_SHIELD],
2064     ["Body Armour" => PICKUP_ARMOUR],
2065     ["Boots" => PICKUP_BOOTS],
2066     ["Gloves" => PICKUP_GLOVES],
2067     ["Cloaks" => PICKUP_CLOAK],
2068     ],
2069    
2070     ["Readables", 2, 0,
2071     ["Spellbooks" => PICKUP_SPELLBOOK],
2072     ["Skillscrolls" => PICKUP_SKILLSCROLL],
2073     ["Normal Books/Scrolls" => PICKUP_READABLES],
2074     ],
2075     ["Misc", 2, 5,
2076     ["Food" => PICKUP_FOOD],
2077     ["Drinks" => PICKUP_DRINK],
2078     ["Valuables (Money, Gems)" => PICKUP_VALUABLES],
2079     ["Keys" => PICKUP_KEY],
2080     ["Magical Items" => PICKUP_MAGICAL],
2081     ["Potions" => PICKUP_POTION],
2082     ["Magic Devices" => PICKUP_MAGIC_DEVICE],
2083     ["Ignore cursed" => PICKUP_NOT_CURSED],
2084     ["Jewelery" => PICKUP_JEWELS],
2085     ["Flesh" => PICKUP_FLESH],
2086     ],
2087     ["Value/Weight ratio", 2, 17]
2088     )
2089     {
2090     my ($title, $x, $y, @bits) = @$_;
2091     $table->add_at ($x, $y, new DC::UI::Label text => $title, align => 1, fg => [1, 1, 0]);
2092    
2093     for (@bits) {
2094     ++$y;
2095    
2096     my $mask = $_->[1];
2097     $table->add_at ($x , $y, new DC::UI::Label text => $_->[0], align => 1, expand => 1);
2098     $table->add_at ($x+1, $y, my $checkbox = new DC::UI::CheckBox
2099     state => $::CFG->{pickup} & $mask,
2100     on_changed => sub {
2101     my ($box, $value) = @_;
2102    
2103     if ($value) {
2104     $::CFG->{pickup} |= $mask;
2105     } else {
2106     $::CFG->{pickup} &= ~$mask;
2107     }
2108    
2109     $::CONN->send_pickup ($::CFG->{pickup})
2110     if defined $::CONN;
2111    
2112     0
2113     });
2114    
2115     ${$_->[2]} = $checkbox if $_->[2];
2116     }
2117     }
2118    
2119     $table->add_at (2, 18, new DC::UI::ValSlider
2120     range => [$::CFG->{pickup} & 0xF, 0, 16, 1, 1],
2121     template => ">= 99",
2122     tooltip => "Pick up items whose value/weight (silver/kg) ratio is equal or higher than this setting (which is specified in gold coins).",
2123     to_value => sub { ">= " . 5 * $_[0] },
2124     on_changed => sub {
2125     my ($slider, $value) = @_;
2126    
2127     $::CFG->{pickup} &= ~0xF;
2128     $::CFG->{pickup} |= int $value
2129     if $value;
2130     1;
2131     });
2132    
2133     $table->add_at (3, 18, new DC::UI::Button
2134     text => "set",
2135     on_activate => sub {
2136     $::CONN->send_pickup ($::CFG->{pickup})
2137     if defined $::CONN;
2138     0
2139     });
2140    
2141     $r
2142     }
2143    
2144     my %SORT_ORDER = (
2145     type => sub {
2146     use sort 'stable';
2147     sort { $a->{type} <=> $b->{type} or $a->{name} cmp $b->{name} } @_
2148     },
2149     mtime => sub {
2150     use sort 'stable';
2151     my $NOW = time;
2152     sort {
2153     my $atime = $a->{mtime} - $NOW; $atime = $atime < 5 * 60 ? int $atime / 60 : 6;
2154     my $btime = $b->{mtime} - $NOW; $btime = $btime < 5 * 60 ? int $btime / 60 : 6;
2155    
2156     ($a->{flags} & F_LOCKED) <=> ($b->{flags} & F_LOCKED)
2157     or $btime <=> $atime
2158     or $a->{type} <=> $b->{type}
2159     } @_
2160     },
2161     weight => sub {
2162     use sort 'stable';
2163     sort {
2164     $a->{weight} * ($a->{nrof} || 1) <=> $b->{weight} * ($b->{nrof} || 1)
2165     or $a->{type} <=> $b->{type}
2166     } @_
2167     },
2168     );
2169    
2170     sub inventory_widget {
2171     my $hb = new DC::UI::HBox homogeneous => 1;
2172    
2173     $hb->add (my $vb1 = new DC::UI::VBox);
2174     $vb1->add (new DC::UI::Label text => "Player");
2175    
2176     $vb1->add (my $hb1 = new DC::UI::HBox);
2177    
2178     use sort 'stable';
2179    
2180     $hb1->add (new DC::UI::Selector
2181     value => $::CFG->{inv_sort},
2182     options => [
2183     [type => "Type/Name"],
2184     [mtime => "Recent/Normal/Locked"],
2185     [weight => "Weight/Type"],
2186     ],
2187     on_changed => sub {
2188     $::CFG->{inv_sort} = $_[1];
2189     $INV->set_sort_order ($SORT_ORDER{$_[1]});
2190     },
2191     );
2192     $hb1->add (new DC::UI::Label text => "Weight: ", align => 1, expand => 1);
2193     #TODO# update to weight/maxweight
2194     $hb1->add ($STATWIDS->{i_weight} = new DC::UI::Label align => 0);
2195    
2196     $vb1->add (my $sw1 = new DC::UI::ScrolledWindow expand => 1, scroll_y => 1);
2197     $sw1->add ($INV = new DC::UI::Inventory);
2198     $INV->set_sort_order ($SORT_ORDER{$::CFG->{inv_sort}});
2199    
2200     $hb->add (my $vb2 = new DC::UI::VBox);
2201    
2202     $vb2->add ($INVR_HB = new DC::UI::HBox);
2203    
2204     $vb2->add (my $sw2 = new DC::UI::ScrolledWindow expand => 1, scroll_y => 1);
2205     $sw2->add ($INVR = new DC::UI::Inventory);
2206    
2207     # XXX: Call after $INVR = ... because set_opencont sets the items
2208     DC::Protocol::set_opencont ($::CONN, 0, "Floor");
2209    
2210     $hb
2211     }
2212    
2213     sub media_window {
2214     my $vb = new DC::UI::VBox;
2215    
2216     $vb->add (new DC::UI::FancyFrame
2217 root 1.6 label => "Current background music",
2218 root 1.1 child => new DC::UI::ScrolledWindow scroll_x => 1, scroll_y => 0,
2219     child => ($MUSIC_PLAYING_WIDGET = new DC::UI::Label ellipsise => 0, fontsize => 0.8),
2220     );
2221    
2222     $vb->add (new DC::UI::FancyFrame
2223 root 1.6 label => "Current downloads",
2224     child => ($DOWNLOADS_WIDGET = new DC::UI::Table
2225     expand => 1, fontsize => 0.8, padding_x => 4, padding_y => 4),
2226     );
2227    
2228     $DOWNLOADS_WIDGET->connect (visibility_change => sub {
2229     my ($self) = @_;
2230    
2231     delete $self->{updater};
2232     return unless $_[1];
2233    
2234 root 1.7 $self->{updater} = AE::timer 0, 0.7, sub {
2235 root 1.6 $self->clear;
2236    
2237     return unless $CONN;
2238    
2239     my @nums = sort { $b <=> $a } keys %{ $CONN->{ix_recv_buf} };
2240 root 1.7 return unless @nums;
2241 root 1.6
2242     $self->add_at (0, 0, new DC::UI::Label align => 1, text => "Face");
2243     $self->add_at (1, 0, new DC::UI::Label align => 0, text => "Octets/Total");
2244    
2245     for my $row (0 .. $#nums) {
2246     my $num = $nums[$row];
2247    
2248     my $total = length $CONN->{ix_recv_buf}{$num};
2249     my $got = $total - $CONN->{ix_recv_ofs}{$num};
2250    
2251     $self->add_at (0, $row + 1, new DC::UI::Label align => 1, text => $num, tooltip => "");
2252     $self->add_at (1, $row + 1, new DC::UI::Label align => 0, text => "$got/$total", tooltip => "");
2253     }
2254     };
2255     });
2256    
2257     $vb->add (new DC::UI::FancyFrame
2258 root 1.1 label => "Other media used in this session",
2259     expand => 1,
2260     child => ($LICENSE_WIDGET = new DC::UI::TextScroller
2261 root 1.6 expand => 1, fontsize => 0.8, padding_x => 4, padding_y => 4),
2262 root 1.1 );
2263    
2264     $vb
2265     }
2266    
2267     sub add_license {
2268     my ($meta) = @_;
2269    
2270     $meta = $meta->{data}
2271     or return;
2272    
2273     $meta->{license} || $meta->{author} || $meta->{source}
2274     or return;
2275    
2276     $LICENSE_WIDGET->add_paragraph ({
2277     fg => [1, 1, 1, 1],
2278     markup => "<small>"
2279     . "<b>Name:</b> " . (DC::asxml $meta->{name}) . "\n"
2280     . "<b>Author:</b> " . (DC::asxml $meta->{author}) . "\n"
2281     . "<b>Source:</b> " . (DC::asxml $meta->{source}) . "\n"
2282     . "<b>License:</b> " . (DC::asxml $meta->{license}) . "\n"
2283     . "</small>",
2284     });
2285     $LICENSE_WIDGET->scroll_to_bottom;
2286     }
2287    
2288     sub toggle_player_page {
2289     my ($widget) = @_;
2290    
2291     if ($PL_WINDOW->{visible} && $PL_NOTEBOOK->get_current_page == $widget) {
2292     $PL_WINDOW->hide;
2293     } else {
2294     $PL_NOTEBOOK->set_current_page ($widget);
2295     $PL_WINDOW->show;
2296     }
2297     }
2298    
2299     sub make_playerbook {
2300     my $plwin = $PL_WINDOW = new DC::UI::Toplevel
2301     x => "center",
2302     y => "center",
2303     force_w => $WIDTH * 9/10,
2304     force_h => $HEIGHT * 9/10,
2305     title => "Player",
2306     name => "playerbook",
2307     has_close_button => 1
2308     ;
2309    
2310     my $ntb =
2311     $PL_NOTEBOOK =
2312     new DC::UI::Notebook expand => 1;
2313    
2314     $ntb->add_tab (
2315     "Statistics (F2)" => $STATS_PAGE = stats_window,
2316     "Shows statistics, where all your Stats and Resistances are shown."
2317     );
2318     $ntb->add_tab (
2319     "Skills (F3)" => $SKILL_PAGE = skill_window,
2320     "Shows all your Skills."
2321     );
2322    
2323     my $spellsw = $SPELL_PAGE = new DC::UI::ScrolledWindow (expand => 1, scroll_y => 1);
2324     $spellsw->add ($SPELL_LIST = new DC::UI::SpellList);
2325     $ntb->add_tab (
2326     "Spellbook (F4)" => $spellsw,
2327     "Displays all spells you have and lets you edit keyboard shortcuts for them."
2328     );
2329     $ntb->add_tab (
2330     "Inventory (F5)" => $INVENTORY_PAGE = inventory_widget,
2331     "Toggles the inventory window, where you can manage your loot (or treasures :). "
2332     . "You can also hit the <b>Tab</b>-key to show/hide the Inventory."
2333     );
2334     $ntb->add_tab (Pickup => $PICKUP_PAGE = autopickup_setup,
2335     "Configure autopickup settings, i.e. which items you will pick up automatically when walking (or running) over them.");
2336    
2337     $ntb->add_tab (Media => media_window,
2338     "License, Author and Source info for media sent by the server.");
2339    
2340     $ntb->set_current_page ($INVENTORY_PAGE);
2341    
2342     $plwin->add ($ntb);
2343     }
2344    
2345     sub keyboard_setup {
2346     DC::Macro::keyboard_setup
2347     }
2348    
2349     sub make_help_window {
2350     my $win = new DC::UI::Toplevel
2351     x => 'center',
2352     y => 'center',
2353     z => 4,
2354     name => 'doc_browser',
2355     force_w => int $WIDTH * 7/8,
2356     force_h => int $HEIGHT * 7/8,
2357     title => "Help Browser",
2358     has_close_button => 1;
2359    
2360     $win->add (my $vbox = new DC::UI::VBox);
2361    
2362     $vbox->add (new DC::UI::FancyFrame
2363     label => "Navigation",
2364     child => (my $buttons = new DC::UI::HBox),
2365     );
2366     $vbox->add (my $viewer = new DC::UI::TextScroller
2367     expand => 1, fontsize => 0.8, padding_x => 4, padding_y => 4);
2368    
2369     my @history;
2370     my @future;
2371     my $curnode;
2372    
2373     my $load_node; $load_node = sub {
2374     my ($node, $para) = @_;
2375    
2376     $buttons->clear;
2377    
2378     $buttons->add (new DC::UI::Button
2379     text => "⇤",
2380     tooltip => "back to the starting page",
2381     on_activate => sub {
2382     unshift @future, [$curnode, $viewer->current_paragraph] if $curnode;
2383     unshift @future, @history;
2384     @history = ();
2385     $load_node->(@{shift @future});
2386     },
2387     );
2388    
2389     if (@history) {
2390     $buttons->add (new DC::UI::Button
2391     text => "⋘",
2392     tooltip => "back to <i>" . (DC::asxml DC::Pod::full_path $history[-1][0]) . "</i>",
2393     on_activate => sub {
2394     unshift @future, [$curnode, $viewer->current_paragraph] if $curnode;
2395     $load_node->(@{pop @history});
2396     },
2397     );
2398     }
2399    
2400     if (@future) {
2401     $buttons->add (new DC::UI::Button
2402     text => "⋙",
2403     tooltip => "forward to <i>" . (DC::asxml DC::Pod::full_path $future[0][0]) . "</i>",
2404     on_activate => sub {
2405     push @history, [$curnode, $viewer->current_paragraph];
2406     $load_node->(@{shift @future});
2407     },
2408     );
2409     }
2410    
2411     $buttons->add (new DC::UI::Label text => " ");
2412    
2413     my @path = DC::Pod::full_path_of $node;
2414     pop @path; # drop current node
2415    
2416     for my $node (@path) {
2417     $buttons->add (new DC::UI::Button
2418     text => $node->[DC::Pod::N_KW][0],
2419     tooltip => "go to <i>" . (DC::asxml DC::Pod::full_path $node) . "</i>",
2420     on_activate => sub {
2421     push @history, [$curnode, $viewer->current_paragraph] if $curnode; @future = ();
2422     $load_node->($node);
2423     },
2424     );
2425     $buttons->add (new DC::UI::Label text => "/");
2426     }
2427    
2428     $buttons->add (new DC::UI::Label text => $node->[DC::Pod::N_KW][0], padding_x => 4, padding_y => 4);
2429    
2430     $curnode = $node;
2431    
2432     $viewer->clear;
2433     $viewer->add_paragraph (DC::Pod::as_paragraphs DC::Pod::section_of $curnode);
2434     $viewer->scroll_to ($para);
2435     };
2436    
2437     $load_node->(DC::Pod::find pod => "mainpage");
2438    
2439     $DC::Pod::goto_document = sub {
2440     my (@path) = @_;
2441    
2442     push @history, [$curnode, $viewer->current_paragraph] if $curnode; @future = ();
2443    
2444     $load_node->((DC::Pod::find @path)[0]);
2445     $win->show;
2446     };
2447    
2448     $HELP_WINDOW = $win;
2449     }
2450    
2451     sub open_quit_dialog {
2452     unless ($QUIT_DIALOG) {
2453     $QUIT_DIALOG = new DC::UI::Toplevel
2454     x => "center",
2455     y => "center",
2456 root 1.9 z => 60,
2457 root 1.1 title => "Really Quit?",
2458     on_key_down => sub {
2459     my ($dialog, $ev) = @_;
2460     $ev->{sym} == 27 and $dialog->hide;
2461     }
2462     ;
2463    
2464     $QUIT_DIALOG->add (my $vb = new DC::UI::VBox expand => 1);
2465    
2466     $vb->add (new DC::UI::Label
2467     text => "You should find a savebed and apply it first!",
2468     max_w => $WIDTH * 0.25,
2469     ellipsize => 0,
2470     );
2471     $vb->add (my $hb = new DC::UI::HBox expand => 1);
2472     $hb->add (new DC::UI::Button
2473     text => "Ok",
2474     expand => 1,
2475     on_activate => sub { $QUIT_DIALOG->hide; 0 },
2476     );
2477     $hb->add (new DC::UI::Button
2478     text => "Quit anyway",
2479     expand => 1,
2480     on_activate => sub {
2481     crash "Quit anyway";
2482     EV::break EV::BREAK_ALL;
2483     },
2484     );
2485     }
2486    
2487     $QUIT_DIALOG->show;
2488     $QUIT_DIALOG->grab_focus;
2489     }
2490    
2491     sub make_menubar {
2492     $MENUFRAME = new DC::UI::Toplevel
2493     border => 0,
2494     force_x => 0,
2495     force_y => 0,
2496     force_w => $::WIDTH,
2497     child => ($MENUBAR = new DC::UI::HBox),
2498     ;
2499    
2500     $MENUBAR->add ($BUTTONBAR = new DC::UI::Buttonbar);
2501    
2502     # XXX: this has to be done before make_stats_window as make_stats_window calls update_stats_window which updated the gauges also X-D
2503     make_gauge_window->show;
2504    
2505     # $BUTTONBAR->add (new DC::UI::Flopper text => "Message Window", other => $MESSAGE_WINDOW,
2506     # tooltip => "Toggles the server message log, where the client collects <i>all</i> messages from the server.");
2507    
2508     make_playerbook;
2509    
2510     $MENUPOPUP = DC::UI::Menu->new (items => [
2511     ["Setup…\tF9" , sub { $SETUP_DIALOG->toggle_visibility }],
2512     ["Playerbook…\tTab" , sub { $PL_WINDOW ->toggle_visibility }],
2513     ["…Statistics\tF2" , sub { toggle_player_page ($::STATS_PAGE) }],
2514     ["…Skills\tF3" , sub { toggle_player_page ($::SKILL_PAGE) }],
2515     ["…Spells\tF4" , sub { toggle_player_page ($::SPELL_PAGE) }],
2516     ["…Inventory\tF5" , sub { toggle_player_page ($::INVENTORY_PAGE) }],
2517     ["Help Browser…\tF1" , sub { $HELP_WINDOW ->toggle_visibility }],
2518     ["Quit…" , sub {
2519     if ($CONN) {
2520     open_quit_dialog;
2521     } else {
2522     EV::unloop EV::UNLOOP_ALL;
2523     }
2524     }],
2525     ]);
2526    
2527     $BUTTONBAR->add (new DC::UI::Button text => "Menu…",
2528     tooltip => "Shows the main menu",
2529     on_button_down => sub {
2530     my ($self, $ev) = @_;
2531     local $ev->{x} = 0;
2532     local $ev->{y} = 0;
2533     $MENUPOPUP->popup ($ev);
2534     },
2535     );
2536    
2537     $MENUBAR->add ($GAUGES->{exp} = new DC::UI::ExperienceProgress
2538     padding_x => 6,
2539     padding_y => 3,
2540     tooltip => "This progress bar shows your overall experience and your progress towards the next character level.",
2541     template => " Exp: 888,888,888,888 (lvl 188) ",
2542     );
2543    
2544     $MENUBAR->add ($PICKUP_ENABLE = new DC::UI::CheckBox # checkbox bad, button better?
2545     tooltip => "Automatic Pickup Enable - when this checkbox is enabled, then your character "
2546     . "will automatically pick up items as defined by your item pickup settings "
2547     . "in the playerbook. Often (e.g. in apartments) you want to temporarily "
2548     . "disable autopickup by disabling this checkbox.",
2549     state => $CFG->{pickup} & PICKUP_INHIBIT ? 0 : 1,
2550     on_changed => sub {
2551     my ($self, $value) = @_;
2552     $CFG->{pickup} &= ~PICKUP_INHIBIT;
2553     $CFG->{pickup} |= PICKUP_INHIBIT unless $_[1];
2554     $CONN->send_pickup ($CFG->{pickup})
2555     if $CONN;
2556     },
2557     );
2558    
2559     $MENUBAR->add ($GAUGES->{skillexp} = new DC::UI::ExperienceProgress
2560     c_rescale => 1,
2561     padding_x => 6,
2562     padding_y => 3,
2563     force_w => $::WIDTH * 0.2,
2564     tooltip => "This progress bar shows the currently used skill and your progress towards the next skill level of that skill.",
2565     template => "two handed weapons 99%",
2566     );
2567    
2568     $MENUBAR->add ($GAUGES->{range} = new DC::UI::Label
2569     expand => 1,
2570     align => 1, can_hover => 1, can_events => 1,
2571     text => "Range and Combat Slots",
2572     tooltip => "#stat_ranged",
2573     );
2574    
2575     $MENUFRAME->show;
2576     }
2577    
2578     sub open_string_query {
2579     my ($title, $cb, $txt, $tooltip) = @_;
2580     my $dialog = new DC::UI::Toplevel
2581     x => "center",
2582     y => "center",
2583     z => 50,
2584     force_w => $WIDTH * 4/5,
2585     title => $title;
2586    
2587     $dialog->add (
2588     my $e = new DC::UI::Entry
2589     on_activate => sub { $cb->(@_); $dialog->hide; 0 },
2590     on_key_down => sub { $_[1]->{sym} == 27 and $dialog->hide; 0 },
2591     tooltip => $tooltip
2592     );
2593    
2594     $e->grab_focus;
2595     $e->set_text ($txt) if $txt;
2596     $dialog->show;
2597     }
2598    
2599     sub show_tip_of_the_day {
2600     # find all tips
2601     my @tod = DC::Pod::find tip_of_the_day => "*";
2602    
2603     DC::DB::get state => "tip_of_the_day", sub {
2604     my ($todindex) = @_;
2605     $todindex = 0 if $todindex >= @tod;
2606     DC::DB::put state => tip_of_the_day => $todindex + 1, sub { };
2607    
2608     # create dialog
2609     my $dialog;
2610    
2611     my $close = sub {
2612     $dialog->destroy;
2613     };
2614    
2615     $dialog = new DC::UI::Toplevel
2616     x => "center",
2617     y => "center",
2618     z => 3,
2619     name => 'tip_of_the_day',
2620     force_w => int $WIDTH * 4/9,
2621     force_h => int $WIDTH * 2/9,
2622     title => "Tip of the day #" . (1 + $todindex),
2623     child => my $vbox = new DC::UI::VBox,
2624     has_close_button => 1,
2625     on_delete => $close,
2626     ;
2627    
2628     $vbox->add (my $viewer = new DC::UI::TextScroller
2629     expand => 1, fontsize => 0.8, padding_x => 4, padding_y => 4);
2630     $viewer->add_paragraph (DC::Pod::as_paragraphs DC::Pod::section_of $tod[$todindex]);
2631    
2632     $vbox->add (my $table = new DC::UI::Table col_expand => [0, 1]);
2633    
2634     $table->add_at (0, 0, new DC::UI::Button
2635     text => "Close",
2636     tooltip => "Close the tip of the day window. To never see it again, disable the tip of the day in the <b>Server Setup</b>.",
2637     on_activate => $close,
2638     );
2639    
2640     $table->add_at (2, 0, new DC::UI::Button
2641     text => "Next",
2642     tooltip => "Show the next <b>Tip of the day</b>.",
2643     on_activate => sub {
2644     $close->();
2645     &show_tip_of_the_day;
2646     },
2647     );
2648    
2649     $dialog->show;
2650     };
2651     }
2652    
2653     sub video_init {
2654     DC::set_theme $CFG->{uitheme};
2655    
2656     DC::SDL_InitSubSystem DC::SDL_INIT_VIDEO if $SDL_REINIT;
2657     $SDL_REINIT = 0;
2658    
2659 root 1.21 @SDL_MODES = DC::SDL_ListModes 8, $CFG->{disable_alpha} ? 0 : 2;
2660 root 1.1 @SDL_MODES = DC::SDL_ListModes 8, 8 unless @SDL_MODES;
2661     @SDL_MODES = DC::SDL_ListModes 5, 0 unless @SDL_MODES;
2662     @SDL_MODES or DC::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)";
2663    
2664     @SDL_MODES = sort { $a->[0] * $a->[1] <=> $b->[0] * $b->[1] } @SDL_MODES;
2665    
2666     if (!defined $CFG->{sdl_mode} or $CFG->{sdl_mode} > $#SDL_MODES) {
2667     $CFG->{sdl_mode} = 0; # lowest resolution by default
2668    
2669     # now choose biggest mode <= 1024x768
2670     for (0 .. $#SDL_MODES) {
2671     if ($SDL_MODES[$_][0] * $SDL_MODES[$_][1] <= 1024 * 768) {
2672     $CFG->{sdl_mode} = $_;
2673     }
2674     }
2675     }
2676    
2677     my ($old_w, $old_h) = ($WIDTH, $HEIGHT);
2678    
2679     ($WIDTH, $HEIGHT, my ($rgb, $alpha)) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
2680     $FULLSCREEN = $CFG->{fullscreen};
2681     $FAST = $CFG->{fast};
2682    
2683 root 1.11 DC::SDL_WM_SetCaption "Deliantra MORPG Client $DC::VERSION$Urlader::EXE_VER", "Deliantra"; # must be after SDL_Init
2684    
2685 root 1.1 # due to mac os x braindamage, we simply retry with !fullscreen in case of an error
2686     DC::SDL_SetVideoMode $WIDTH, $HEIGHT, $rgb, $alpha, $FULLSCREEN
2687     or DC::SDL_SetVideoMode $WIDTH, $HEIGHT, $rgb, $alpha, !$FULLSCREEN
2688     or die "SDL_SetVideoMode failed: " . (DC::SDL_GetError) . "\n";
2689    
2690     $SDL_ACTIVE = 1;
2691     $LAST_REFRESH = time - 0.01;
2692    
2693     DC::OpenGL::init;
2694     DC::Macro::init;
2695    
2696     $FONTSIZE = int $HEIGHT / 40 * $CFG->{gui_fontsize};
2697    
2698     $DC::UI::ROOT->configure (0, 0, $WIDTH, $HEIGHT);#d#
2699    
2700     #############################################################################
2701    
2702     if ($DEBUG_STATUS) {
2703     DC::UI::rescale_widgets $WIDTH / $old_w, $HEIGHT / $old_h;
2704     } else {
2705     # create/configure the widgets
2706    
2707     $DC::UI::ROOT->connect (key_down => sub {
2708     my (undef, $ev) = @_;
2709    
2710     if (my @macros = DC::Macro::find $ev) {
2711     DC::Macro::execute $_ for @macros;
2712    
2713     return 1;
2714     }
2715    
2716     0
2717     });
2718    
2719     $DEBUG_STATUS = new DC::UI::Label
2720     padding => 0,
2721     z => 100,
2722     force_x => "max",
2723     force_y => 20;
2724     $DEBUG_STATUS->show;
2725    
2726     $STATUSBOX = new DC::UI::Statusbox;
2727    
2728     $MODBOX = new DC::UI::Label
2729     can_events => 1,
2730     can_hover => 1,
2731     markup => "",
2732     align => 0,
2733     font => $FONT_FIXED,
2734     tooltip => "#modifier_box",
2735     tooltip_width => 0.67,
2736     ;
2737    
2738     update_modbox;
2739    
2740     (new DC::UI::Frame
2741     bg => [0, 0, 0, 0.4],
2742     force_x => 0,
2743     force_y => "max",
2744     child => (my $LL = new DC::UI::VBox),
2745     )->show;
2746    
2747     $LL->add ($STATUSBOX);
2748     $LL->add ($MODBOX);
2749     $LL->add (new DC::UI::Label
2750     align => 0,
2751     markup => "Use <b>Alt-Enter</b> to toggle fullscreen mode",
2752     fontsize => 0.5,
2753     fg => [1, 1, 0, 0.7],
2754     );
2755    
2756     DC::UI::Toplevel->new (
2757     title => "Minimap",
2758     name => "mapmap",
2759     x => 0,
2760     y => $::FONTSIZE + 8,#d# hack to move messages window below the menubar
2761     border_bg => [1, 1, 1, 192/255],
2762     bg => [1, 1, 1, 0],
2763     child => ($MAPMAP = new DC::MapWidget::MapMap
2764     tooltip => "<b>Minimap</b>. This will display an overview of the surrounding areas.",
2765     ),
2766     )->show;
2767    
2768     $MAPWIDGET = new DC::MapWidget;
2769     $MAPWIDGET->connect (activate_console => sub {
2770     my ($mapwidget, $preset) = @_;
2771    
2772     $MESSAGE_DIST->activate_console ($preset)
2773     if $MESSAGE_DIST;
2774     });
2775     $MAPWIDGET->show;
2776     $MAPWIDGET->grab_focus;
2777    
2778     $COMPLETER = new DC::MapWidget::Command::
2779     command => { },
2780     tooltip => "#completer_help",
2781     ;
2782    
2783     $SETUP_DIALOG = new DC::UI::Toplevel
2784     title => "Setup",
2785     name => "setup_dialog",
2786     x => 'center',
2787     y => 'center',
2788     z => 2,
2789     force_w => $::WIDTH * 0.6,
2790     force_h => $::HEIGHT * 0.6,
2791     has_close_button => 1,
2792     ;
2793    
2794     $METASERVER = metaserver_dialog;
2795     # the name is changed to not conflict with the older name as users could have hidden it
2796     $MESSAGE_WINDOW = new DC::UI::Dockbar
2797     name => "message_window2",
2798     title => 'Messages',
2799     y => $::FONTSIZE + 8,#d# hack to move messages window below the menubar
2800     force_w => $::WIDTH * 0.6,
2801     force_h => $::HEIGHT * 0.25,
2802     ;
2803    
2804     $MESSAGE_DIST = new DC::MessageDistributor dockbar => $MESSAGE_WINDOW;
2805    
2806     $SETUP_DIALOG->add ($SETUP_NOTEBOOK = new DC::UI::Notebook expand => 1,
2807     filter => new DC::UI::ScrolledWindow expand => 1, scroll_y => 1);
2808    
2809     $SETUP_NOTEBOOK->add_tab (Login => $SETUP_LOGIN = login_setup,
2810     "Configure the server to play on, your username and password.");
2811     $SETUP_NOTEBOOK->add_tab (Server => $SETUP_SERVER = server_setup,
2812     "Configure other server related options.");
2813     $SETUP_NOTEBOOK->add_tab (Client => client_setup,
2814     "Configure various client-specific settings.");
2815     $SETUP_NOTEBOOK->add_tab (Graphics => graphics_setup,
2816     "Configure the video mode, performance, fonts and other graphical aspects of the game.");
2817     $SETUP_NOTEBOOK->add_tab (Audio => audio_setup,
2818     "Configure the use of audio, sound effects and background music.");
2819     $SETUP_NOTEBOOK->add_tab (Keyboard => $SETUP_KEYBOARD = keyboard_setup,
2820     "Lets you define, edit and delete key bindings."
2821     . "There is a shortcut for making bindings: <b>Control-Insert</b> opens the binding editor "
2822     . "with nothing set and the recording started. After doing the actions you "
2823     . "want to record press <b>Insert</b> and you will be asked to press a key-combo. "
2824     . "After pressing the combo the binding will be saved automatically and the "
2825     . "binding editor closes");
2826     $SETUP_NOTEBOOK->add_tab (Debug => debug_setup,
2827     "Some debuggin' options. Do not ask.");
2828    
2829     make_help_window;
2830     make_menubar;
2831    
2832     $SETUP_DIALOG->show;
2833     $MESSAGE_WINDOW->show;
2834     }
2835    
2836     $MODE_SLIDER->set_range ([$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1, 1]);
2837     $MODE_SLIDER->emit (changed => $CFG->{sdl_mode});
2838    
2839     $CAVEAT_LABEL->set_text ("None :)");
2840     $CAVEAT_LABEL->set_text ("Apple/NVIDIA Texture bug (slow)")
2841     if $DC::OpenGL::APPLE_NVIDIA_BUG;
2842     $CAVEAT_LABEL->set_text ("Software Rendering (very slow)")
2843     unless DC::SDL_GL_GetAttribute DC::SDL_GL_ACCELERATED_VISUAL;
2844    
2845     $STATUSBOX->add ("Set video mode $WIDTH×$HEIGHT", timeout => 10, fg => [1, 1, 1, 0.5]);
2846     }
2847    
2848     sub video_shutdown {
2849     DC::OpenGL::shutdown;
2850     DC::SDL_QuitSubSystem DC::SDL_INIT_VIDEO if $SDL_REINIT;
2851    
2852     undef $SDL_ACTIVE;
2853     }
2854    
2855     my %animate_object;
2856     my $animate_timer;
2857    
2858     my $fps = 9;
2859    
2860     sub force_refresh {
2861     if ($DELIANTRA_DEBUG & 4) {
2862     $fps = $fps * 0.98 + 1 / (($NOW - $LAST_REFRESH) || 0.1) * 0.02;
2863     debug sprintf "%3.2f", $fps;
2864     }
2865    
2866     undef $WANT_REFRESH;
2867     $_[0]->stop;
2868    
2869     $DC::UI::ROOT->draw;
2870     DC::SDL_GL_SwapBuffers;
2871     $LAST_REFRESH = $NOW;
2872     }
2873    
2874     my $want_refresh = EV::prepare_ns \&force_refresh;
2875    
2876     our $INPUT_WATCHER = EV::periodic 0, 1 / $MAX_FPS, undef, sub {
2877     $NOW = EV::now;
2878    
2879     ($SDL_CB[$_->{type}] || sub { warn "unhandled event $_->{type}" })->($_)
2880 root 1.2 for DC::peep_events;
2881 root 1.1
2882     if (%animate_object) {
2883     $_->animate ($LAST_REFRESH - $NOW) for values %animate_object;
2884     $WANT_REFRESH = 1;
2885     }
2886    
2887     $want_refresh->start
2888     if $WANT_REFRESH;
2889     };
2890    
2891     sub animation_start {
2892     my ($widget) = @_;
2893     $animate_object{$widget} = $widget;
2894     }
2895    
2896     sub animation_stop {
2897     my ($widget) = @_;
2898     delete $animate_object{$widget};
2899     }
2900    
2901     $SDL_CB[DC::SDL_QUIT] = sub {
2902     crash "SDL_QUIT";
2903     EV::unloop EV::UNLOOP_ALL;
2904     };
2905     $SDL_CB[DC::SDL_VIDEORESIZE] = sub { };
2906     $SDL_CB[DC::SDL_VIDEOEXPOSE] = sub {
2907     DC::UI::full_refresh;
2908     };
2909     $SDL_CB[DC::SDL_ACTIVEEVENT] = sub {
2910     # not useful, as APPACTIVE includes only iconified state, not unmapped
2911     # printf "active %x %x %x\n", $_[0]{gain}, $_[0]{state}, DC::SDL_GetAppState;#d#
2912     # printf "a %x\n", DC::SDL_GetAppState & DC::SDL_APPACTIVE;#d#
2913     # printf "A\n" if $_[0]{state} & DC::SDL_APPACTIVE;
2914     # printf "K\n" if $_[0]{state} & DC::SDL_APPINPUTFOCUS;
2915     # printf "M\n" if $_[0]{state} & DC::SDL_APPMOUSEFOCUS;
2916     };
2917     $SDL_CB[DC::SDL_KEYDOWN] = sub {
2918     if ($_[0]{mod} & DC::KMOD_ALT && $_[0]{sym} == 13) {
2919     # alt-enter
2920     video_shutdown;
2921     $FULLSCREEN_ENABLE->toggle;
2922     video_init;
2923     } else {
2924     &DC::UI::feed_sdl_key_down_event;
2925     }
2926     update_modbox;
2927     };
2928     $SDL_CB[DC::SDL_KEYUP] = sub {
2929     &DC::UI::feed_sdl_key_up_event;
2930     update_modbox;
2931     };
2932     $SDL_CB[DC::SDL_MOUSEMOTION] = \&DC::UI::feed_sdl_motion_event,
2933     $SDL_CB[DC::SDL_MOUSEBUTTONDOWN] = \&DC::UI::feed_sdl_button_down_event,
2934     $SDL_CB[DC::SDL_MOUSEBUTTONUP] = \&DC::UI::feed_sdl_button_up_event,
2935     $SDL_CB[DC::SDL_USEREVENT] = sub {
2936     if ($_[0]{code} == 1) {
2937     audio_channel_finished $_[0]{data1};
2938     } elsif ($_[0]{code} == 0) {
2939     audio_music_finished;
2940     }
2941     };
2942    
2943     #############################################################################
2944    
2945     $SIG{INT} = $SIG{TERM} = sub {
2946     EV::unloop;
2947     #d# TODO calling exit here hangs the process in some futex
2948     };
2949    
2950     # due to mac os x + sdl combined braindamage, we need this contortion
2951 root 1.18 sub DC::Main::run {
2952     DC::SDL_main_hack {
2953     {
2954     DC::Pod::load_docwiki DC::find_rcfile "docwiki.pst";
2955    
2956     DC::load_cfg;
2957     DC::upgrade_cfg;
2958 root 1.1
2959 root 1.18 DC::Audio::probe;
2960 root 1.15
2961 root 1.18 DC::DB::Server::run;
2962 root 1.1
2963 root 1.18 if ($CFG->{db_schema} < 1) {
2964     warn "INFO: upgrading database schema from 0 to 1, mapcache and tilecache will be lost\n";
2965     DC::DB::nuke_db;
2966     $CFG->{db_schema} = 1;
2967     DC::save_cfg;
2968     }
2969 root 1.1
2970 root 1.18 DC::DB::open_db;
2971 root 1.1
2972 root 1.18 DC::UI::set_layout ($::CFG->{layout});
2973 root 1.1
2974 root 1.18 my @args = @ARGV;
2975 root 1.1
2976 root 1.18 # OS X passes some process serial number of other shit. they
2977     # could have used an env var or any other sane mechanism. but
2978     # would it be os x then? no...
2979     shift @args if $args[0] =~ /^-psn_/;
2980    
2981     my $profile = 'default';
2982    
2983     for (my $i = 0; $i < @args; $i++) {
2984     if ($args[$i] =~ /^--?profile$/) {
2985     $profile = $args[$i + 1];
2986     splice @args, $i, 2, ();
2987     $i = 0;
2988     } elsif ($args[$i] =~ /^--?h/) {
2989     print STDERR "Usage: $0 [--profile name] [host [user [password]]]\n";
2990     exit 0;
2991     }
2992 root 1.1 }
2993    
2994 root 1.18 $CFG->{profile}{$profile} ||= {};
2995     $PROFILE = $CFG->{profile}{$profile};
2996     $PROFILE->{host} ||= "gameserver.deliantra.net";
2997    
2998     $PROFILE->{host} = $args[0] if @args > 0;
2999     $PROFILE->{user} = $args[1] if @args > 1;
3000     $PROFILE->{password} = $args[2] if @args > 2;
3001    
3002     # convert old bindings (only default profile matters)
3003     if (my $bindings = delete $PROFILE->{bindings}) {
3004     while (my ($mod, $syms) = each %$bindings) {
3005     while (my ($sym, $cmds) = each %$syms) {
3006     push @{ $PROFILE->{macro} }, {
3007     accelkey => [$mod*1, $sym*1],
3008     action => $cmds,
3009     };
3010     }
3011 root 1.1 }
3012     }
3013    
3014 root 1.18 # fontconfig doesn't support relative paths anymore, so use abs_path and keep fingers crossed
3015     # these are ignored under windows, for some reason, and thus set in the loader
3016     $ENV{FONTCONFIG_FILE} = "fonts.conf";
3017     $ENV{FONTCONFIG_PATH} = Cwd::abs_path DC::find_rcfile "fonts";
3018     $ENV{FONTCONFIG_DIR} = $ENV{FONTCONFIG_PATH}; # helps with older versions
3019    
3020     {
3021     my @fonts = map DC::find_rcfile "fonts/$_", qw(
3022     DejaVuSans.ttf
3023     DejaVuSansMono.ttf
3024     DejaVuSans-Bold.ttf
3025     DejaVuSansMono-Bold.ttf
3026     DejaVuSans-Oblique.ttf
3027     DejaVuSansMono-Oblique.ttf
3028     DejaVuSans-BoldOblique.ttf
3029     DejaVuSansMono-BoldOblique.ttf
3030     mona.ttf
3031     );
3032 root 1.1
3033 root 1.18 DC::add_font $_ for @fonts;
3034 root 1.1
3035 root 1.18 $FONT_PROP = new_from_file DC::Font $fonts[0];
3036     $FONT_FIXED = new_from_file DC::Font $fonts[1];
3037 root 1.1
3038 root 1.18 $FONT_PROP->make_default;
3039 root 1.1
3040 root 1.18 DC::pango_init;
3041     }
3042 root 1.1
3043     # compare mono (ft) vs. rgba (cairo)
3044     # ft - 1.8s, cairo 3s, even in alpha-only mode
3045     # for my $rgba (0..1) {
3046     # my $t1 = Time::HiRes::time;
3047     # for (1..1000) {
3048     # my $layout = DC::Layout->new ($rgba);
3049     # $layout->set_text ("hallo" x 100);
3050     # $layout->render;
3051     # }
3052     # my $t2 = Time::HiRes::time;
3053     # warn $t2-$t1;
3054     # }
3055    
3056 root 1.18 }
3057    
3058     DC::SDL_Init 0;
3059 root 1.1 DC::IMG_Init; video_init;
3060     DC::Mix_Init; audio_init;
3061    
3062 root 1.18 show_tip_of_the_day if $CFG->{show_tips};
3063 root 1.1
3064 root 1.18 my $STARTUP_CANCEL; $STARTUP_CANCEL = EV::idle sub {
3065     undef $STARTUP_CANCEL;
3066     (pop @::STARTUP_DONE)->()
3067     while @::STARTUP_DONE;
3068     };
3069 root 1.1
3070 root 1.18 debug_toggle 0;
3071 root 1.1
3072 root 1.18 delete $SIG{__DIE__};
3073     EV::loop;
3074 root 1.1
3075 root 1.18 DC::save_cfg if $CFG->{config_autosave};
3076 root 1.1
3077 root 1.18 #video_shutdown;
3078     #audio_shutdown;
3079 root 1.1
3080 root 1.18 DC::OpenGL::quit;
3081     DC::SDL_Quit;
3082     DC::DB::Server::stop;
3083     };
3084 root 1.1 }
3085    
3086     1