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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines