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.88 by root, Wed Apr 12 21:01:47 2006 UTC vs.
Revision 1.150 by root, Thu Apr 20 07:12:58 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines