ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
(Generate patch)

Comparing deliantra/Deliantra-Client/bin/pclient (file contents):
Revision 1.108 by root, Sat Apr 15 01:13:47 2006 UTC vs.
Revision 1.143 by root, Wed Apr 19 08:05:07 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines