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