ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Main.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/Main.pm (file contents):
Revision 1.22 by root, Sun Nov 4 02:13:53 2012 UTC vs.
Revision 1.31 by root, Mon Nov 19 00:56:08 2018 UTC

25BEGIN { *time = \&EV::time } 25BEGIN { *time = \&EV::time }
26 26
27use List::Util qw(max min); 27use List::Util qw(max min);
28 28
29use Deliantra; 29use Deliantra;
30use Deliantra::Util;
30use Deliantra::Protocol::Constants; 31use Deliantra::Protocol::Constants;
31 32
32use AnyEvent::Util (); 33use AnyEvent::Util ();
33use AnyEvent::Socket (); 34use AnyEvent::Socket ();
34use AnyEvent::DNS (); 35use AnyEvent::DNS ();
73use DC::MessageDistributor; 74use DC::MessageDistributor;
74use DC::Pod; 75use DC::Pod;
75use DC::MapWidget; 76use DC::MapWidget;
76use DC::Macro; 77use DC::Macro;
77 78
79# printf "%5d %5d %5d %5d\n", $_, DC::minpot $_, DC::ld32 $_, 2 << ((DC::ld32 $_ - 1));
80
78$SIG{QUIT} = sub { Carp::cluck "QUIT" }; 81$SIG{QUIT} = sub { Carp::cluck "QUIT" };
79$SIG{PIPE} = 'IGNORE'; 82$SIG{PIPE} = 'IGNORE';
80 83
81$EV::DIED = sub { 84$EV::DIED = sub {
82 crash "CRASH/EV::DIED: $@" => 0; 85 crash "CRASH/EV::DIED: $@" => 0;
128our $MENUBAR; # the hbox at the top 131our $MENUBAR; # the hbox at the top
129our $MENUPOPUP; 132our $MENUPOPUP;
130our $BUTTONBAR; # the menu buttons 133our $BUTTONBAR; # the menu buttons
131our $METASERVER; 134our $METASERVER;
132our $LOGIN_BUTTON; 135our $LOGIN_BUTTON;
136our $LOGIN_ERROR;
133our $QUIT_DIALOG; 137our $QUIT_DIALOG;
134our $HOST_ENTRY; 138our $HOST_ENTRY;
135our $FULLSCREEN_ENABLE; 139our $FULLSCREEN_ENABLE;
136our $PICKUP_ENABLE; 140our $PICKUP_ENABLE;
137our $SERVER_INFO; 141our $SERVER_INFO;
251 : "[<span foreground='#888'>num</span>]"; 255 : "[<span foreground='#888'>num</span>]";
252 256
253 # <tt> around next statement works around some bug that keeps the 257 # <tt> around next statement works around some bug that keeps the
254 # "font =>" from being used on windows 258 # "font =>" from being used on windows
255 $MODBOX->set_markup ("<tt>$markup</tt>"); 259 $MODBOX->set_markup ("<tt>$markup</tt>");
260}
261
262sub errorbox {
263 my ($msg) = @_;
264
265 status $msg;
266
267 my $dialog = new DC::UI::Toplevel
268 x => "center",
269 y => "center",
270 z => 200,
271 title => "Error",
272 child => my $vbox = new DC::UI::VBox,
273 has_close_button => 1,
274 on_delete => sub {
275 $_[0]->destroy;
276 },
277 ;
278
279 add $vbox new DC::UI::Label
280 align => 0.5,
281 ellipsise => 0,
282 text => $msg;
283
284 add $vbox new DC::UI::Button
285 expand => 1,
286 text => "OK",
287 on_activate => sub {
288 $dialog->destroy;
289 0
290 }
291 ;
292
293 $dialog->show;
256} 294}
257 295
258############################################################################# 296#############################################################################
259#TODO: maybe move into own audio module... 297#TODO: maybe move into own audio module...
260 298
475 audio_music_push; 513 audio_music_push;
476} 514}
477 515
478sub audio_init { 516sub audio_init {
479 if ($CFG->{audio_enable}) { 517 if ($CFG->{audio_enable}) {
480 DC::Audio::init $CFG->{audio_driver}; 518 DC::Audio::init;
481 519
482 if ($SDL_MIXER) { 520 if ($SDL_MIXER) {
483 audio_music_finished; 521 audio_music_finished;
484 } else { 522 } else {
485 status "Unable to open sound device: there will be no sound"; 523 status "Unable to open sound device: there will be no sound";
928 $vbox->add (@dialog); 966 $vbox->add (@dialog);
929 $dialog->show; 967 $dialog->show;
930} 968}
931 969
932sub dc_connect { 970sub dc_connect {
933 my ($host, $port) = @_; 971 my ($host, $port, $create) = @_;
934 972
935 my $mapw = List::Util::min 48, List::Util::max 11, int 1.5 + $WIDTH * $CFG->{mapsize} * 0.01 / 32; 973 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; 974 my $maph = List::Util::min 48, List::Util::max 11, int 1.5 + $HEIGHT * $CFG->{mapsize} * 0.01 / 32;
937 975
938 $CONN = 976 $CONN =
939 new DC::Protocol 977 new DC::Protocol
940 host => $host, 978 host => $host,
941 port => $port, 979 port => $port,
980 create_login => $create,
942 user => $PROFILE->{user}, 981 user => $PROFILE->{user},
943 pass => $PROFILE->{password}, 982 pass => (pack "H*", $PROFILE->{password}),
944 mapw => $mapw, 983 mapw => $mapw,
945 maph => $maph, 984 maph => $maph,
946 985
947 c_version => { 986 c_version => {
948 client => "deliantra", 987 client => "deliantra",
968 ota_update_check; 1007 ota_update_check;
969 1008
970 status "successfully connected to the server"; 1009 status "successfully connected to the server";
971 } else { 1010 } else {
972 undef $CONN; 1011 undef $CONN;
973 status "unable to connect: $!"; 1012 $LOGIN_ERROR->{fg} = [1, 0, 0];
1013 $LOGIN_ERROR->set_text ("Unable to connect to server: $!");
974 stop_game(); 1014 stop_game();
975 } 1015 }
976 }, 1016 },
1017
1018 on_addme => sub {
1019 my ($ok, $msg) = @_;
1020
1021 $LOGIN_ERROR->{fg} = $ok ? [0, 1, 0] : [1, 0, 0];
1022 $LOGIN_ERROR->set_text ($msg);
1023 },
977 ; 1024 ;
978} 1025}
979 1026
980sub start_game { 1027sub start_game($) {
1028 my ($create) = @_;
1029
981 status "logging in..."; 1030 status "logging in...";
982 1031
983 my $server = $PROFILE->{host} || $DEFAULT_SERVER; 1032 my $server = $PROFILE->{host} || $DEFAULT_SERVER;
984 my ($host, $port) = AnyEvent::Socket::parse_hostport $server, "deliantra=13327" 1033 my ($host, $port) = AnyEvent::Socket::parse_hostport $server, "deliantra=13327"
985 or return status "$server: unable to parse server address, try an empty field."; 1034 or return status "$server: unable to parse server address, try an empty field.";
1000 status "dns failure, using hardcoded address"; 1049 status "dns failure, using hardcoded address";
1001 $host = "194.126.175.154"; 1050 $host = "194.126.175.154";
1002 } 1051 }
1003 } 1052 }
1004 1053
1005 dc_connect $host, $port; 1054 dc_connect $host, $port, $create;
1006 }; 1055 };
1007 } else { 1056 } else {
1008 dc_connect $host, $port; 1057 dc_connect $host, $port, $create;
1009 } 1058 }
1010} 1059}
1011 1060
1012sub stop_game { 1061sub stop_game {
1013 crash "stop_game"; 1062 crash "stop_game";
1131 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Compress Textures"); 1180 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Compress Textures");
1132 $table->add_at (1, $row++, new DC::UI::CheckBox 1181 $table->add_at (1, $row++, new DC::UI::CheckBox
1133 state => $CFG->{texture_compression}, 1182 state => $CFG->{texture_compression},
1134 tooltip => "Use texture compression. Normally this will not reduce visual quality noticable but " 1183 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). " 1184 . "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 " 1185 . "The compression algorithm can differ from 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.", 1186 . "forced OpenGL 1.1 mode and when using the Apple renderer.",
1138 on_changed => sub { my ($self, $value) = @_; $CFG->{texture_compression} = $value; 0 } 1187 on_changed => sub { my ($self, $value) = @_; $CFG->{texture_compression} = $value; 0 }
1139 ); 1188 );
1140 1189
1141 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Fast & Ugly"); 1190 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Fast & Ugly");
1493 $DELIANTRA_DEBUG ^= $_[0]; 1542 $DELIANTRA_DEBUG ^= $_[0];
1494 1543
1495 if ($DELIANTRA_DEBUG & 16) { 1544 if ($DELIANTRA_DEBUG & 16) {
1496 $BW_WATCHER = EV::periodic 0, 1, 0, sub { 1545 $BW_WATCHER = EV::periodic 0, 1, 0, sub {
1497 return unless $CONN; 1546 return unless $CONN;
1498 debug sprintf "%8.2gKB/s", $CONN->{octets_in} / 1e3; 1547 debug sprintf "%8.2gkB/s", $CONN->{octets_in} / 1e3;
1499 $CONN->{octets_in} = 0; 1548 $CONN->{octets_in} = 0;
1500 }; 1549 };
1501 } else { 1550 } else {
1502 undef $BW_WATCHER; 1551 undef $BW_WATCHER;
1503 } 1552 }
1846 $vbox->add (new DC::UI::FancyFrame 1895 $vbox->add (new DC::UI::FancyFrame
1847 label => "Login Settings", 1896 label => "Login Settings",
1848 child => (my $table = new DC::UI::Table expand => 1, col_expand => [0, 1]), 1897 child => (my $table = new DC::UI::Table expand => 1, col_expand => [0, 1]),
1849 ); 1898 );
1850 1899
1900 my $nullpw = "\x00" x 16;
1901
1851 $table->add_at (0, 4, new DC::UI::Label align => 1, text => "Username"); 1902 $table->add_at (0, 0, new DC::UI::Label align => 1, text => "Username");
1852 $table->add_at (1, 4, new DC::UI::Entry 1903 $table->add_at (1, 0, new DC::UI::Entry
1853 text => $PROFILE->{user}, 1904 text => $PROFILE->{user},
1854 tooltip => "The name of your character on the server. The name is case-sensitive!", 1905 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 } 1906 on_changed => sub { my ($self, $value) = @_; $PROFILE->{user} = $value; 1 }
1856 ); 1907 );
1857 1908
1858 $table->add_at (0, 5, new DC::UI::Label align => 1, text => "Password"); 1909 $table->add_at (0, 1, new DC::UI::Label align => 1, text => "Password");
1859 $table->add_at (1, 5, new DC::UI::Entry 1910 $table->add_at (1, 1, my $pw1 = new DC::UI::Entry
1860 text => $PROFILE->{password}, 1911 text => $PROFILE->{password} ? $nullpw : "",
1861 hidden => 1, 1912 hidden => 1,
1862 tooltip => "The password for your character.", 1913 tooltip => "The password for your character.",
1863 on_changed => sub { my ($self, $value) = @_; $PROFILE->{password} = $value; 1 } 1914 on_focus_in => sub {
1915 my ($self) = @_;
1916 $self->set_text ("")
1917 if $self->{text} eq $nullpw;
1918 0
1919 },
1920 on_changed => sub {
1921 my ($self, $value) = @_;
1922 $PROFILE->{password} = unpack "H*", Deliantra::Util::hash_pw $value
1923 if length $value && $value ne $nullpw;
1924 1
1925 },
1864 ); 1926 );
1865 1927
1866 $table->add_at (1, 11, $LOGIN_BUTTON = new DC::UI::Button 1928 $table->add_at (1, 2, $LOGIN_BUTTON = new DC::UI::Button
1867 expand => 1, 1929 expand => 1,
1868 text => "Login / Register", 1930 text => "Login to Existing Account",
1869 tooltip => "This button will either login to the account configured above or register a new account.", 1931 tooltip => "This button will login to the account given by the <b>Username</b> and <b>Password</b> fields above.",
1870 on_activate => sub { 1932 on_activate => sub {
1871 $CONN ? stop_game 1933 $CONN ? stop_game
1872 : start_game; 1934 : start_game 0;
1873 1 1935 1
1874 }, 1936 },
1875 ); 1937 );
1876 1938
1939 $table->add_at (0, 3, new DC::UI::Label align => 1, text => "Password");
1940 $table->add_at (1, 3, my $pw2 = new DC::UI::Entry
1941 hidden => 1,
1942 tooltip => "The new password for your character",
1943 );
1944
1945 $table->add_at (1, 4, new DC::UI::Button
1946 expand => 1,
1947 text => "Create New Account",
1948 tooltip => "This button will try to create a new account - you need to fill out the <b>Username</b> and the two <b>Password</b> fields above, using the same password for both <b>Password</b> fields.",
1949 on_activate => sub {
1950 if ($pw1->{text} ne $pw2->{text}) {
1951 $LOGIN_ERROR->{fg} = [1, 0, 0];
1952 $LOGIN_ERROR->set_text ("The passwords do not match - try to enter them again.");
1953 } else {
1954 $CONN or start_game 1;
1955 }
1956 1
1957 },
1958 );
1959
1960 $vbox->add (new DC::UI::FancyFrame
1961 label => "Server Message",
1962 tooltip => "The last message, or error, from the server.",
1963 child => ($LOGIN_ERROR = new DC::UI::Label valign => 0, ellipsise => 0),
1964 );
1965
1877 $vbox->add (new DC::UI::FancyFrame 1966 $vbox->add (new DC::UI::FancyFrame
1878 label => "How to Play", 1967 label => "How to Play",
1879 min_h => 240, 1968 min_h => 240,#d# should not be necessary - widget bug
1880 child => (new DC::UI::Label valign => 0, ellipsise => 0, 1969 child => (new DC::UI::Label valign => 0, ellipsise => 0,
1881 markup => 1970 markup =>
1882 "First select a suitable video resolution in the <b>Graphics</b> tab, above.\n\n" 1971 "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). " 1972 . "Then create a new account (or use an existing one if you have one).\n\n"
1884 . "To register an account, choose a username that hasn't been taken yet (just guess) and " 1973 . "To create 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.", 1974 . "fill out the top two <b>Password</b> fields using the same password, then press <b>Create New Account</b>.",
1886 ), 1975 ),
1887 ); 1976 );
1888 1977
1889 $vbox 1978 $vbox
1890} 1979}
1941 $table->add_at (0, ++$row, new DC::UI::Label align => 1, text => "Output-Rate"); 2030 $table->add_at (0, ++$row, new DC::UI::Label align => 1, text => "Output-Rate");
1942 $table->add_at (1, $row, new DC::UI::Entry 2031 $table->add_at (1, $row, new DC::UI::Entry
1943 text => $CFG->{output_rate}, 2032 text => $CFG->{output_rate},
1944 tooltip => "The maximum bandwidth in bytes per second that the server should not exceed " 2033 tooltip => "The maximum bandwidth in bytes per second that the server should not exceed "
1945 . "when sending data. When 0 or unset, the server " 2034 . "when sending data. When 0 or unset, the server "
1946 . "default will be used, which is usually around 100kb/s. Most servers will " 2035 . "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.", 2036 . "dynamically find an optimal rate, so adjust this only when necessary.",
1948 on_changed => sub { $CFG->{output_rate} = $_[1]; 1 }, 2037 on_changed => sub { $CFG->{output_rate} = $_[1]; 1 },
1949 ); 2038 );
1950 2039
1951 $vbox->add (new DC::UI::FancyFrame 2040 $vbox->add (new DC::UI::FancyFrame
2508 make_playerbook; 2597 make_playerbook;
2509 2598
2510 $MENUPOPUP = DC::UI::Menu->new (items => [ 2599 $MENUPOPUP = DC::UI::Menu->new (items => [
2511 ["Setup…\tF9" , sub { $SETUP_DIALOG->toggle_visibility }], 2600 ["Setup…\tF9" , sub { $SETUP_DIALOG->toggle_visibility }],
2512 ["Playerbook…\tTab" , sub { $PL_WINDOW ->toggle_visibility }], 2601 ["Playerbook…\tTab" , sub { $PL_WINDOW ->toggle_visibility }],
2513 ["…Statistics\tF2" , sub { toggle_player_page ($::STATS_PAGE) }], 2602 ["…Statistics\tF2" , sub { toggle_player_page ($::STATS_PAGE) }],
2514 ["…Skills\tF3" , sub { toggle_player_page ($::SKILL_PAGE) }], 2603 ["…Skills\tF3" , sub { toggle_player_page ($::SKILL_PAGE) }],
2515 ["…Spells\tF4" , sub { toggle_player_page ($::SPELL_PAGE) }], 2604 ["…Spells\tF4" , sub { toggle_player_page ($::SPELL_PAGE) }],
2516 ["…Inventory\tF5" , sub { toggle_player_page ($::INVENTORY_PAGE) }], 2605 ["…Inventory\tF5" , sub { toggle_player_page ($::INVENTORY_PAGE) }],
2517 ["Help Browser…\tF1" , sub { $HELP_WINDOW ->toggle_visibility }], 2606 ["Help Browser…\tF1" , sub { $HELP_WINDOW ->toggle_visibility }],
2518 ["Quit…" , sub { 2607 ["Quit…" , sub {
2519 if ($CONN) { 2608 if ($CONN) {
2520 open_quit_dialog; 2609 open_quit_dialog;
2521 } else { 2610 } else {
2522 EV::unloop EV::UNLOOP_ALL; 2611 EV::break EV::BREAK_ALL;
2523 } 2612 }
2524 }], 2613 }],
2525 ]); 2614 ]);
2526 2615
2527 $BUTTONBAR->add (new DC::UI::Button text => "Menu…", 2616 $BUTTONBAR->add (new DC::UI::Button text => "Menu…",
2898 delete $animate_object{$widget}; 2987 delete $animate_object{$widget};
2899} 2988}
2900 2989
2901$SDL_CB[DC::SDL_QUIT] = sub { 2990$SDL_CB[DC::SDL_QUIT] = sub {
2902 crash "SDL_QUIT"; 2991 crash "SDL_QUIT";
2903 EV::unloop EV::UNLOOP_ALL; 2992 EV::break EV::BREAK_ALL;
2904}; 2993};
2905$SDL_CB[DC::SDL_VIDEORESIZE] = sub { }; 2994$SDL_CB[DC::SDL_VIDEORESIZE] = sub { };
2906$SDL_CB[DC::SDL_VIDEOEXPOSE] = sub { 2995$SDL_CB[DC::SDL_VIDEOEXPOSE] = sub {
2907 DC::UI::full_refresh; 2996 DC::UI::full_refresh;
2908}; 2997};
2941}; 3030};
2942 3031
2943############################################################################# 3032#############################################################################
2944 3033
2945$SIG{INT} = $SIG{TERM} = sub { 3034$SIG{INT} = $SIG{TERM} = sub {
2946 EV::unloop; 3035 EV::break;
2947 #d# TODO calling exit here hangs the process in some futex 3036 #d# TODO calling exit here hangs the process in some futex
2948}; 3037};
2949 3038
2950# due to mac os x + sdl combined braindamage, we need this contortion 3039# due to mac os x + sdl combined braindamage, we need this contortion
2951sub DC::Main::run { 3040sub DC::Main::run {
2993 3082
2994 $CFG->{profile}{$profile} ||= {}; 3083 $CFG->{profile}{$profile} ||= {};
2995 $PROFILE = $CFG->{profile}{$profile}; 3084 $PROFILE = $CFG->{profile}{$profile};
2996 $PROFILE->{host} ||= "gameserver.deliantra.net"; 3085 $PROFILE->{host} ||= "gameserver.deliantra.net";
2997 3086
2998 $PROFILE->{host} = $args[0] if @args > 0; 3087 $PROFILE->{host} = $args[0] if @args > 0;
2999 $PROFILE->{user} = $args[1] if @args > 1; 3088 $PROFILE->{user} = $args[1] if @args > 1;
3000 $PROFILE->{password} = $args[2] if @args > 2; 3089 $PROFILE->{password} = unpack "H*", Deliantra::Util::hash_pw $args[2] if @args > 2;
3001 3090
3002 # convert old bindings (only default profile matters) 3091 # convert old bindings (only default profile matters)
3003 if (my $bindings = delete $PROFILE->{bindings}) { 3092 if (my $bindings = delete $PROFILE->{bindings}) {
3004 while (my ($mod, $syms) = each %$bindings) { 3093 while (my ($mod, $syms) = each %$bindings) {
3005 while (my ($sym, $cmds) = each %$syms) { 3094 while (my ($sym, $cmds) = each %$syms) {
3068 }; 3157 };
3069 3158
3070 debug_toggle 0; 3159 debug_toggle 0;
3071 3160
3072 delete $SIG{__DIE__}; 3161 delete $SIG{__DIE__};
3073 EV::loop; 3162 EV::run;
3074 3163
3075 DC::save_cfg if $CFG->{config_autosave}; 3164 DC::save_cfg if $CFG->{config_autosave};
3076 3165
3077 #video_shutdown; 3166 #video_shutdown;
3078 #audio_shutdown; 3167 #audio_shutdown;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines