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.112 by root, Sat Apr 15 13:56:26 2006 UTC vs.
Revision 1.141 by root, Wed Apr 19 06:21: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
26our $META_SERVER = "crossfire.real-time.com:13326"; 34our $META_SERVER = "crossfire.real-time.com:13326";
27 35
36our $FACEMAP;
37our $TILECACHE;
28our $FACECACHE; 38our $MAPCACHE;
29 39
30our $LAST_REFRESH; 40our $LAST_REFRESH;
31our $NOW; 41our $NOW;
32 42
33our $CFG; 43our $CFG;
45our $BUTTONBAR; 55our $BUTTONBAR;
46our $LOGVIEW; 56our $LOGVIEW;
47our $CONSOLE; 57our $CONSOLE;
48our $METASERVER; 58our $METASERVER;
49 59
60our $GAUGES;
61
50our $SDL_ACTIVE; 62our $SDL_ACTIVE;
51our $SDL_EV; 63our $SDL_EV;
52our %SDL_CB; 64our %SDL_CB;
53 65
66our $SDL_MIXER;
67our @SOUNDS; # event => file mapping
68our %AUDIO_CHUNKS; # audio files
69
54our $ALT_ENTER_MESSAGE; 70our $ALT_ENTER_MESSAGE;
55our $STATUS_LINE; 71our $STATUS_LINE;
56our $DEBUG_STATUS; 72our $DEBUG_STATUS;
57 73
58sub status { 74sub status {
59 $STATUS_LINE->set_text ($_[0]); 75 $STATUS_LINE->set_text ($_[0]);
60 my ($w, $h) = $STATUS_LINE->size_request;
61 $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});
62} 77}
63 78
64sub debug { 79sub debug {
65 $DEBUG_STATUS->set_text ($_[0]); 80 $DEBUG_STATUS->set_text ($_[0]);
66 my ($w, $h) = $DEBUG_STATUS->size_request; 81 $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0, $DEBUG_STATUS->{w}, $DEBUG_STATUS->{h});
67 $DEBUG_STATUS->size_allocate ($WIDTH - $w, 0, $w, $h);
68} 82}
69 83
70sub start_game { 84sub start_game {
71 status "logging in..."; 85 status "logging in...";
72 86
73 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;
88
89 $MAPCACHE = CFClient::db_table "mapcache_$CFG->{host}";
74 90
75 $MAP = new CFClient::Map $mapsize, $mapsize; 91 $MAP = new CFClient::Map $mapsize, $mapsize;
76 92
77 my ($host, $port) = split /:/, $CFG->{host}; 93 my ($host, $port) = split /:/, $CFG->{host};
78 94
95} 111}
96 112
97sub client_setup { 113sub client_setup {
98 my $dialog = new CFClient::UI::FancyFrame 114 my $dialog = new CFClient::UI::FancyFrame
99 child => (my $vbox = new CFClient::UI::VBox); 115 child => (my $vbox = new CFClient::UI::VBox);
100 $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");
101 $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]);
102 118
103 $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");
104 $table->add (1, 0, my $hbox = new CFClient::UI::HBox); 120 $table->add (1, 0, my $hbox = new CFClient::UI::HBox);
105 121
106 $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]);
107 $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);
108 124
109 $mode_slider->connect (changed => sub { 125 $mode_slider->connect (changed => sub {
110 my ($self, $value) = @_; 126 my ($self, $value) = @_;
111 127
112 $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value; 128 $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value;
113 $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]}); 129 $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]});
114 }); 130 });
115 $mode_slider->emit (changed => $mode_slider->{range}[0]); 131 $mode_slider->emit (changed => $mode_slider->{range}[0]);
116 132
117 $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");
118 $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 {
119 my ($self, $value) = @_; 135 my ($self, $value) = @_;
120 $CFG->{fullscreen} = $value; 136 $CFG->{fullscreen} = $value;
121 }); 137 });
122 138
123 $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");
124 $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 {
125 my ($self, $value) = @_; 141 my ($self, $value) = @_;
126 $CFG->{fast} = $value; 142 $CFG->{fast} = $value;
127 }); 143 });
128 144
129 $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");
130 $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 {
131 my ($self, $value) = @_; 147 my ($self, $value) = @_;
132 $CFG->{fow_enable} = $value; 148 $CFG->{fow_enable} = $value;
133 }); 149 });
134 150
135 $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");
136 $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 {
137 my ($self, $value) = @_; 153 my ($self, $value) = @_;
138 $CFG->{fow_intensity} = $value; 154 $CFG->{fow_intensity} = $value;
139 }); 155 });
140 156
141 $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");
142 $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 {
143 my ($self, $value) = @_; 159 my ($self, $value) = @_;
144 $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;
145 }); 162 });
146 163
147 $table->add (0, 6, 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");
148 $table->add (1, 6, 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 {
149 my ($self, $value) = @_;
150 $LOGVIEW->set_fontsize ($CFG->{log_fontsize} = int $value); 172 $LOGVIEW->set_fontsize ($CFG->{log_fontsize} = 0.1 * int $_[1] * 10);
151 }); 173 });
152 174
153 $table->add (1, 7, 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 {
154 destroy_screen (); 176 video_shutdown ();
155 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];
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 ();
156 }); 201 });
157 202
158 $dialog 203 $dialog
159} 204}
160 205
166 211
167 $dialog 212 $dialog
168} 213}
169 214
170sub update_metaserver { 215sub update_metaserver {
216 my ($HOST) = @_;
217
171 status "fetching metaserver list..."; 218 status "fetching metaserver list...";
172 219
173 my $buf; 220 my $buf;
174 221
175 my $fh = new IO::Socket::INET PeerHost => $META_SERVER, Blocking => 0; 222 my $fh = new IO::Socket::INET PeerHost => $META_SERVER, Blocking => 0;
181 $_[0]->w->cancel; 228 $_[0]->w->cancel;
182 status "metaserver: $!"; 229 status "metaserver: $!";
183 } elsif ($res == 0) { 230 } elsif ($res == 0) {
184 $_[0]->w->cancel; 231 $_[0]->w->cancel;
185 status "server list retrieved"; 232 status "server list retrieved";
186 warn $buf; 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 }
187 } 277 }
188 }); 278 });
189} 279}
190 280
191sub server_setup { 281sub server_setup {
192 my $dialog = new CFClient::UI::FancyFrame 282 my $dialog = new CFClient::UI::FancyFrame
193 child => (my $vbox = new CFClient::UI::VBox); 283 child => (my $vbox = new CFClient::UI::VBox);
194 284
195 $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");
196 $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]);
197 $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Host:Port"); 287 $table->add (0, 2, new CFClient::UI::Label valign => 0, align => 1, text => "Host:Port");
198 288
199 { 289 {
200 $table->add (1, 2, my $vbox = new CFClient::UI::VBox); 290 $table->add (1, 2, my $vbox = new CFClient::UI::VBox);
201 291
202 $vbox->add (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 {
203 my ($self, $value) = @_; 293 my ($self, $value) = @_;
204 $CFG->{host} = $value; 294 $CFG->{host} = $value;
205 }); 295 });
206 296
207 $METASERVER = metaserver_dialog; 297 $METASERVER = metaserver_dialog;
208 298
209 $vbox->add (new CFClient::UI::Flopper text => "Metaserver", other => $METASERVER, connect_open => sub { 299 $vbox->add (new CFClient::UI::Flopper expand => 1, text => "Metaserver", other => $METASERVER, connect_open => sub {
210 update_metaserver; 300 update_metaserver $HOST;
211 }); 301 });
212 } 302 }
213 303
214 $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");
215 $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 {
216 my ($self, $value) = @_; 306 my ($self, $value) = @_;
217 $CFG->{user} = $value; 307 $CFG->{user} = $value;
218 }); 308 });
219 309
220 $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");
221 $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 {
222 my ($self, $value) = @_; 312 my ($self, $value) = @_;
223 $CFG->{password} = $value; 313 $CFG->{password} = $value;
224 }); 314 });
225 315
226 $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");
227 $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 {
228 my ($self, $value) = @_; 318 my ($self, $value) = @_;
229 $CFG->{say_command} = $value; 319 $CFG->{say_command} = $value;
230 }); 320 });
231 321
232 $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");
233 $table->add (1, 7, new CFClient::UI::Slider 323 $table->add (1, 7, new CFClient::UI::Slider
234 req_w => 100, 324 req_w => 100,
235 range => [$CFG->{mapsize}, 10, 100 + 1, 1], 325 range => [$CFG->{mapsize}, 10, 100 + 1, 1],
236 connect_changed => sub { 326 connect_changed => sub {
237 my ($self, $value) = @_; 327 my ($self, $value) = @_;
249 339
250sub message_window { 340sub message_window {
251 my $window = new CFClient::UI::FancyFrame 341 my $window = new CFClient::UI::FancyFrame
252 border_bg => [1, 1, 1, 0.5], 342 border_bg => [1, 1, 1, 0.5],
253 bg => [0.3, 0.3, 0.3, 0.8], 343 bg => [0.3, 0.3, 0.3, 0.8],
254 user_w => $::WIDTH/4, 344 user_w => int $::WIDTH / 3,
255 user_h => $::HEIGHT, 345 user_h => int $::HEIGHT / 5,
256 child => (my $vbox = new CFClient::UI::VBox); 346 child => (my $vbox = new CFClient::UI::VBox);
257 347
258 $vbox->add ($LOGVIEW = new CFClient::UI::TextView 348 $vbox->add ($LOGVIEW = new CFClient::UI::TextView
259 expand => 1, 349 expand => 1,
260 fontsize => $::CFG->{log_fontsize}, 350 fontsize => $::CFG->{log_fontsize},
261 ); 351 );
262 352
263 $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 },
264 $input->connect (activate => sub { 364 connect_activate => sub {
265 my ($input, $text) = @_; 365 my ($input, $text) = @_;
266 $input->set_text (''); 366 $input->set_text ('');
267 367
268 if ($text =~ /^\/(.*)/) { 368 if ($text =~ /^\/(.*)/) {
269 $::CONN->user_send ("command $1"); 369 $::CONN->user_send ($1);
270 } else { 370 } else {
271 my $say_cmd = $::CFG->{say_command} || 'say'; 371 my $say_cmd = $::CFG->{say_command} || 'say';
272 $::CONN->user_send ("command $say_cmd $text"); 372 $::CONN->user_send ("$say_cmd $text");
273 } 373 }
374 if ($input->{refocus_map}) {
375 delete $input->{refocus_map};
376 $MAPWIDGET->focus_in
274 1 377 }
275 }); 378 },
276 $input->connect (escape => sub { 379 connect_escape => sub {
277 $MAPWIDGET->focus_in 380 $MAPWIDGET->focus_in
278 }); 381 },
279 $input->focus_in; 382 );
280 383
281 $CONSOLE = { 384 $CONSOLE = {
282 window => $window, 385 window => $window,
283 input => $input 386 input => $input
284 }; 387 };
290 #SDL::Init SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE 393 #SDL::Init SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE
291 SDL::Init SDL_INIT_AUDIO | SDL_INIT_VIDEO 394 SDL::Init SDL_INIT_AUDIO | SDL_INIT_VIDEO
292 and die "SDL::Init failed!\n"; 395 and die "SDL::Init failed!\n";
293} 396}
294 397
295sub init_screen { 398sub video_init {
296 sdl_init; 399 sdl_init;
297 400
298 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] }; 401 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
299 $FULLSCREEN = $CFG->{fullscreen}; 402 $FULLSCREEN = $CFG->{fullscreen};
300 $FAST = $CFG->{fast}; 403 $FAST = $CFG->{fast};
301 404
302 SDL::GLSetAttribute SDL_GL_RED_SIZE, 5; 405 SDL::GLSetAttribute SDL_GL_RED_SIZE, 5;
303 SDL::GLSetAttribute SDL_GL_GREEN_SIZE, 5; 406 SDL::GLSetAttribute SDL_GL_GREEN_SIZE, 5;
304 SDL::GLSetAttribute SDL_GL_BLUE_SIZE, 5; 407 SDL::GLSetAttribute SDL_GL_BLUE_SIZE, 5;
305 SDL::GLSetAttribute SDL_GL_ALPHA_SIZE, 0; 408 SDL::GLSetAttribute SDL_GL_ALPHA_SIZE, 1;
306 409
307 SDL::GLSetAttribute SDL_GL_ACCUM_RED_SIZE, 0; 410 SDL::GLSetAttribute SDL_GL_ACCUM_RED_SIZE, 0;
308 SDL::GLSetAttribute SDL_GL_ACCUM_GREEN_SIZE, 0; 411 SDL::GLSetAttribute SDL_GL_ACCUM_GREEN_SIZE, 0;
309 SDL::GLSetAttribute SDL_GL_ACCUM_BLUE_SIZE, 0; 412 SDL::GLSetAttribute SDL_GL_ACCUM_BLUE_SIZE, 0;
310 SDL::GLSetAttribute SDL_GL_ACCUM_ALPHA_SIZE, 0; 413 SDL::GLSetAttribute SDL_GL_ACCUM_ALPHA_SIZE, 0;
327 430
328 $LAST_REFRESH = time - 0.01; 431 $LAST_REFRESH = time - 0.01;
329 432
330 CFClient::gl_init; 433 CFClient::gl_init;
331 434
332 $FONTSIZE = int $HEIGHT / 40; 435 $FONTSIZE = int $HEIGHT / 40 * $CFG->{gui_fontsize};
333 436
334 ############################################################################# 437 #############################################################################
335 438
336 $DEBUG_STATUS = new CFClient::UI::Label padding => 0, z => 100; 439 $DEBUG_STATUS = new CFClient::UI::Label padding => 0, z => 100;
337 $CFClient::UI::ROOT->add ($DEBUG_STATUS); 440 $DEBUG_STATUS->show;
338 441
339 $STATUS_LINE = new CFClient::UI::Label 442 $STATUS_LINE = new CFClient::UI::Label
340 padding => 0, 443 padding => 0,
341 y => $HEIGHT * 44 / 45 - $FONTSIZE; 444 y => $HEIGHT - $FONTSIZE * 1.8;
342 $CFClient::UI::ROOT->add ($STATUS_LINE); 445 $STATUS_LINE->show;
343 446
344 $ALT_ENTER_MESSAGE = new CFClient::UI::Label 447 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
345 padding => 0, 448 padding => 0,
346 y => $HEIGHT * 44 / 45, 449 fontsize => 0.8,
347 height => $HEIGHT / 45,
348 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode"; 450 markup => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
349 $CFClient::UI::ROOT->add ($ALT_ENTER_MESSAGE); 451 $ALT_ENTER_MESSAGE->show;
452 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h});
350 453
351 $CFClient::UI::ROOT->add ($MAPWIDGET = new CFClient::UI::MapWidget); 454 $CFClient::UI::ROOT->add ($MAPWIDGET = new CFClient::MapWidget);
352 $MAPWIDGET->focus_in; 455 $MAPWIDGET->focus_in;
353 $MAPWIDGET->connect (activate_console => sub { 456 $MAPWIDGET->connect (activate_console => sub {
354 my ($mapwidget, $preset) = @_; 457 my ($mapwidget, $preset) = @_;
355 458
356 if ($CONSOLE) { 459 if ($CONSOLE) {
460 $CONSOLE->{input}->{auto_activated} = 1;
357 $CONSOLE->{input}->focus_in; 461 $CONSOLE->{input}->focus_in;
358 462
359 if ($preset && $CONSOLE->{input}->get_text eq '') { 463 if ($preset && $CONSOLE->{input}->get_text eq '') {
360 $CONSOLE->{input}->set_text ($preset); 464 $CONSOLE->{input}->set_text ($preset);
361 } 465 }
371 $BUTTONBAR->add (new CFClient::UI::Button text => "Save Config", connect_activate => sub { 475 $BUTTONBAR->add (new CFClient::UI::Button text => "Save Config", connect_activate => sub {
372 CFClient::write_cfg "$Crossfire::VARDIR/pclientrc"; 476 CFClient::write_cfg "$Crossfire::VARDIR/pclientrc";
373 status "Configuration Saved"; 477 status "Configuration Saved";
374 }); 478 });
375 479
376 $BUTTONBAR->{children}[0]->emit ("activate"); 480 $BUTTONBAR->{children}[1]->emit ("activate"); # pop up server setup
377}
378 481
379sub destroy_screen { 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 {
380 $CFClient::UI::ROOT->{children} = []; 495 $CFClient::UI::ROOT->{children} = [];
381 undef $SDL_ACTIVE; 496 undef $SDL_ACTIVE;
382 undef $SDL_EV; 497 undef $SDL_EV;
383 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 $SDL_MIXER = new SDL::Mixer;
506 $SDL_MIXER->allocate_channels (8);
507
508 # TODO: hack, do play loop and mood music
509 if ($CFG->{bgm_enable}) {
510 $bgmusic = new SDL::Music CFClient::find_rcfile "music/game3.ogg";
511 $SDL_MIXER->play_music ($bgmusic, -1);
512 $SDL_MIXER->music_volume ($CFG->{bgm_volume});
513 }
514
515 while (<$fh>) {
516 next if /^\s*#/;
517 next if /^\s*$/;
518
519 my ($file, $volume, $event) = split /\s+/, $_, 3;
520
521 push @SOUNDS, "$volume,$file";
522
523 $AUDIO_CHUNKS{"$volume,$file"} ||= do {
524 my $chunk = new SDL::Sound CFClient::find_rcfile "sounds/$file";
525 $chunk->volume ($volume * 128 / 100);
526 $chunk
527 };
528 }
529 } else {
530 status "unable to open sound config: $!";
531 }
532 }
533}
534
535sub audio_shutdown {
536 undef $SDL_MIXER;
537 @SOUNDS = ();
538 %AUDIO_CHUNKS = ();
384} 539}
385 540
386my %animate_object; 541my %animate_object;
387my $animate_timer; 542my $animate_timer;
388 543
437 delete $animate_object{$widget}; 592 delete $animate_object{$widget};
438} 593}
439 594
440@conn::ISA = Crossfire::Protocol::; 595@conn::ISA = Crossfire::Protocol::;
441 596
597sub conn::stats_update {
598 my ($self, $stats) = @_;
599
600 # i love text protocols!!!
601 # FIXME: the stats are somehow weird
602 my $hp = $stats->{1};
603 my $hp_m = $stats->{2};
604 my $sp = $stats->{3};
605 my $sp_m = $stats->{4};
606 my $fo = $stats->{18};
607 my $fo_m = 1000;
608 my $gr = $stats->{23};
609 my $gr_m = $stats->{24};
610
611 #d# warn "DATA $hp $hp_m $sp $sp_m $fo $fo_m $gr $gr_m\n";
612 $GAUGES->{hp}->set_value ($hp, $hp_m);
613 $GAUGES->{mana}->set_value ($sp, $sp_m);
614 $GAUGES->{food}->set_value ($fo, $fo_m);
615 $GAUGES->{grace}->set_value ($gr, $gr_m);
616}
617
442sub conn::user_send { 618sub conn::user_send {
443 my ($self, $command) = @_; 619 my ($self, $command) = @_;
444 620
445 $self->send ($command); 621 $self->send_command ($command);
446 status $command; 622 status $command;
623}
624
625sub conn::map_scroll {
626 my ($self, $dx, $dy) = @_;
627
628 $MAP->scroll ($dx, $dy);
447} 629}
448 630
449sub conn::feed_map1a { 631sub conn::feed_map1a {
450 my ($self, $data) = @_; 632 my ($self, $data) = @_;
451 633
452# $self->Crossfire::Protocol::feed_map1a ($data); 634# $self->Crossfire::Protocol::feed_map1a ($data);
453 635
454 $MAP->scroll (delete $self->{delayed_scroll_x}, delete $self->{delayed_scroll_y});
455 $MAP->map1a_update ($data); 636 $MAP->map1a_update ($data);
456 $MAPWIDGET->update; 637 $MAPWIDGET->update;
457} 638}
458 639
459#sub conn::map_update { 640sub conn::flush_map {
460# my ($self, $dirty) = @_; 641 my ($self) = @_;
461# 642
462# $MAPWIDGET->update; 643 my $map_info = delete $self->{map_info}
463#} 644 or return;
645
646 my ($hash, $x, $y, $w, $h) = @$map_info;
647
648 my $data = $MAP->get_rect ($x, $y, $w, $h);
649 $MAPCACHE->put ($hash => Compress::LZF::compress $data);
650
651 warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
652
653}
464 654
465sub conn::map_clear { 655sub conn::map_clear {
466 my ($self) = @_; 656 my ($self) = @_;
467 657
658 $self->flush_map;
659 delete $self->{neigh};
660
468 $MAP->clear; 661 $MAP->clear;
662}
469 663
470# refresh; 664
665sub conn::load_map($$$) {
666 my ($self, $hash, $x, $y) = @_;
667
668 if (defined (my $data = $MAPCACHE->get ($hash))) {
669 $data = Compress::LZF::decompress $data;
670 warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
671 for my $id ($MAP->set_rect ($x, $y, $data)) {
672 my $data = $TILECACHE->get ($id)
673 or next;
674
675 $self->set_texture ($id => $data);
676 }
677 }
678}
679
680sub conn::flood_fill {
681 my ($self, $path, $hash, $flags, $x0, $y0, $x1, $y1) = @_;
682
683 # the server does not allow map paths > 6
684 return if 6 <= length $path;
685
686 for my $tile (1..4) {
687 next if $self->{neigh}{$hash}[$tile];
688 next unless $flags & (1 << ($tile - 1));
689
690 my $neigh = $self->{neigh}{$hash} ||= [];
691
692 $self->send_mapinfo ("spatial $path$tile", sub {
693 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
694
695 #warn "map<$path>_$tile=<$mode,$x,$y,$w,$h,$hash>\n";#d#
696 return if $mode ne "spatial";
697
698 $x += $MAP->ox;
699 $y += $MAP->oy;
700
701 $self->load_map ($hash, $x, $y)
702 unless $self->{neigh}{$hash}[5]++;#d#
703
704 $neigh->[$tile] = [$x, $y, $w, $h];
705
706 $self->flood_fill ("$path$tile", $hash, $flags, $x0, $y0, $x1, $y1)
707 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
708 });
709 }
710}
711
712sub conn::map_change {
713 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
714
715 $self->flush_map;
716
717 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
718
719 my $mapmapw = 250;
720 my $mapmaph = 250;
721
722 $self->flood_fill ("", $hash, $flags,
723 $ox - $mapmapw * 0.5, $oy - $mapmapw * 0.5,
724 $ox + $mapmapw * 0.5, $oy + $mapmapw * 0.5);
725
726 $x += $ox;
727 $y += $oy;
728
729 $self->{map_info} = [$hash, $x, $y, $w, $h];
730
731 $self->load_map ($hash, $x, $y);
471} 732}
472 733
473sub conn::face_find { 734sub conn::face_find {
474 my ($self, $face) = @_; 735 my ($self, $facenum, $face) = @_;
475 736
476 $FACECACHE->{"$face->{chksum},$face->{name}"} 737 my $hash = "$face->{chksum},$face->{name}";
738
739 my $id = $FACEMAP->get ($hash);
740
741 unless ($id) {
742 # create new id for face
743 # i love transactions
744 for (1..100) {
745 my $txn = $CFClient::DB_ENV->txn_begin;
746 my $status = $FACEMAP->db_get (id => $id, BerkeleyDB::DB_RMW);
747 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
748 $id++;
749 if ($FACEMAP->put (id => $id) == 0
750 && $FACEMAP->put ($hash => $id) == 0) {
751 $txn->txn_commit;
752
753 goto gotid;
754 }
755 }
756 $txn->abort;
757 }
758
759 CFClient::fatal "maximum number of transaction retries reached - database problems?";
760 }
761
762gotid:
763 $face->{id} = $id;
764 $MAP->set_face ($facenum => $id);
765 $TILECACHE->get ($id)
477} 766}
478 767
479sub conn::face_update { 768sub conn::face_update {
480 my ($self, $facenum, $face) = @_; 769 my ($self, $facenum, $face) = @_;
481 770
482 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; 771 $TILECACHE->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes
483 772
484 my $tex = $face->{texture} = 773 $self->set_texture ($face->{id} => delete $face->{image});
774}
775
776sub conn::set_texture {
777 my ($self, $id, $data) = @_;
778
779 $self->{texture}[$id] ||= do {
780 my $tex =
485 new_from_image CFClient::Texture 781 new_from_image CFClient::Texture
486 delete $face->{image}, minify => 1; 782 $data, minify => 1;
487 783
488 $MAP->set_texture ($facenum, @$tex{qw(name w h s t)}, @{$tex->{minified}}); 784 $MAP->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
489 $MAPWIDGET->update; 785 $MAPWIDGET->update;
786
787 $tex
788 };
789}
790
791sub conn::sound_play {
792 my ($self, $x, $y, $soundnum, $type) = @_;
793
794 $SDL_MIXER
795 or return;
796
797 my $chunk = $AUDIO_CHUNKS{$SOUNDS[$soundnum]}
798 or return;
799
800 $SDL_MIXER->play_channel (-1, $chunk);
801 warn "sound $x,$y,$soundnum,$type\n";#d#
490} 802}
491 803
492sub conn::query { 804sub conn::query {
493 my ($self, $flags, $prompt) = @_; 805 my ($self, $flags, $prompt) = @_;
494 806
500 my ($self, $color, $text) = @_; 812 my ($self, $color, $text) = @_;
501 813
502 my @color = ( 814 my @color = (
503 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00], 815 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
504 [1.00, 1.00, 1.00], 816 [1.00, 1.00, 1.00],
505 [0.00, 0.00, 0.55], 817 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
506 [1.00, 0.00, 0.00], 818 [1.00, 0.00, 0.00],
507 [1.00, 0.54, 0.00], 819 [1.00, 0.54, 0.00],
508 [0.11, 0.56, 1.00], 820 [0.11, 0.56, 1.00],
509 [0.93, 0.46, 0.00], 821 [0.93, 0.46, 0.00],
510 [0.18, 0.54, 0.34], 822 [0.18, 0.54, 0.34],
528 refresh; 840 refresh;
529 }, 841 },
530 SDL_KEYDOWN() => sub { 842 SDL_KEYDOWN() => sub {
531 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { 843 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
532 # alt-enter 844 # alt-enter
533 destroy_screen; 845 video_shutdown;
534 $CFG->{fullscreen} = !$CFG->{fullscreen}; 846 $CFG->{fullscreen} = !$CFG->{fullscreen};
535 init_screen; 847 video_init;
536 } else { 848 } else {
537 CFClient::UI::feed_sdl_key_down_event ($SDL_EV); 849 CFClient::UI::feed_sdl_key_down_event ($SDL_EV);
538 } 850 }
539 }, 851 },
540 SDL_KEYUP() => sub { 852 SDL_KEYUP() => sub {
554 }, 866 },
555); 867);
556 868
557############################################################################# 869#############################################################################
558 870
871$SIG{INT} = $SIG{TERM} = sub { exit };
872
873$TILECACHE = CFClient::db_table "tilecache";
874$FACEMAP = CFClient::db_table "facemap";
875
559CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; 876CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
560 877
561my %DEF_CFG = ( 878my %DEF_CFG = (
562 sdl_mode => 0, 879 sdl_mode => 0,
563 width => 640, 880 width => 640,
565 fullscreen => 0, 882 fullscreen => 0,
566 fast => 0, 883 fast => 0,
567 fow_enable => 1, 884 fow_enable => 1,
568 fow_intensity => 0.45, 885 fow_intensity => 0.45,
569 fow_smooth => 0, 886 fow_smooth => 0,
887 gui_fontsize => 1,
570 log_fontsize => 14, 888 log_fontsize => 14,
571 mapsize => 100, 889 mapsize => 100,
572 host => "crossfire.schmorp.de", 890 host => "crossfire.schmorp.de",
573 say_command => 'say', 891 say_command => 'say',
892 audio_enable => 1,
893 bgm_enable => 1,
894 bgm_volume => 64,
574); 895);
575 896
576while (my ($k, $v) = each %DEF_CFG) { 897while (my ($k, $v) = each %DEF_CFG) {
577 $CFG->{$k} = $v unless exists $CFG->{$k}; 898 $CFG->{$k} = $v unless exists $CFG->{$k};
578} 899}
586 907
587@SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)"; 908@SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)";
588 909
589$CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES; 910$CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES;
590 911
591init_screen;
592
593{ 912{
594 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); 913 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
595 914
596 CFClient::add_font $_ for @fonts; 915 CFClient::add_font $_ for @fonts;
597 CFClient::set_font $fonts[0]; 916 CFClient::set_font $fonts[0];
598} 917}
599 918
600$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 919video_init;
920audio_init;
601 921
602Event::loop; 922Event::loop;
603 923
604Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; 924END { SDL::Quit }
605 925
926

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines