… | |
… | |
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::Mixer; |
|
|
15 | use SDL::Sound; |
|
|
16 | use SDL::Music; |
|
|
17 | |
13 | use SDL::OpenGL; |
18 | use SDL::OpenGL; |
14 | |
19 | |
15 | use Crossfire; |
20 | use Crossfire; |
16 | use Crossfire::Protocol; |
21 | use Crossfire::Protocol; |
17 | |
22 | |
|
|
23 | use Compress::LZF; |
|
|
24 | |
18 | use CFClient; |
25 | use CFClient; |
19 | use CFClient::UI; |
26 | use CFClient::UI; |
20 | |
27 | |
21 | our $VERSION = '0.1'; |
28 | our $VERSION = '0.1'; |
22 | |
29 | |
23 | my $MAX_FPS = 60; |
30 | my $MAX_FPS = 60; |
24 | my $MIN_FPS = 5; # unused as of yet |
31 | my $MIN_FPS = 5; # unused as of yet |
25 | |
32 | |
|
|
33 | our $META_SERVER = "crossfire.real-time.com:13326"; |
|
|
34 | |
|
|
35 | our $FACEMAP; |
|
|
36 | our $TILECACHE; |
26 | our $FACECACHE; |
37 | our $MAPCACHE; |
27 | |
38 | |
28 | our $LAST_REFRESH; |
39 | our $LAST_REFRESH; |
29 | our $NOW; |
40 | our $NOW; |
30 | |
41 | |
31 | our $CFG; |
42 | our $CFG; |
… | |
… | |
38 | our $FULLSCREEN; |
49 | our $FULLSCREEN; |
39 | our $FONTSIZE; |
50 | our $FONTSIZE; |
40 | |
51 | |
41 | our $MAP; |
52 | our $MAP; |
42 | our $MAPWIDGET; |
53 | our $MAPWIDGET; |
|
|
54 | our $BUTTONBAR; |
|
|
55 | our $LOGVIEW; |
|
|
56 | our $CONSOLE; |
|
|
57 | our $METASERVER; |
|
|
58 | |
|
|
59 | our $GAUGES; |
43 | |
60 | |
44 | our $SDL_ACTIVE; |
61 | our $SDL_ACTIVE; |
45 | our $SDL_EV; |
62 | our $SDL_EV; |
46 | our %SDL_CB; |
63 | our %SDL_CB; |
47 | |
64 | |
|
|
65 | our $SDL_MIXER; |
|
|
66 | our @SOUNDS; # event => file mapping |
|
|
67 | our %AUDIO_CHUNKS; # audio files |
|
|
68 | |
48 | our $ALT_ENTER_MESSAGE; |
69 | our $ALT_ENTER_MESSAGE; |
49 | our $STATUS_LINE; |
70 | our $STATUS_LINE; |
50 | our $DEBUG_STATUS; |
71 | our $DEBUG_STATUS; |
51 | our $BUTTONBAR; |
|
|
52 | our $LOGVIEW; |
|
|
53 | |
72 | |
54 | sub status { |
73 | sub status { |
55 | $STATUS_LINE->set_text ($_[0]); |
74 | $STATUS_LINE->set_text ($_[0]); |
56 | my ($w, $h) = $STATUS_LINE->size_request; |
|
|
57 | $STATUS_LINE->size_allocate (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $h, $w, $h); |
75 | $STATUS_LINE->move (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $STATUS_LINE->{h}); |
58 | } |
76 | } |
59 | |
77 | |
60 | sub debug { |
78 | sub debug { |
61 | $DEBUG_STATUS->set_text ($_[0]); |
79 | $DEBUG_STATUS->set_text ($_[0]); |
62 | my ($w, $h) = $DEBUG_STATUS->size_request; |
80 | $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0, $DEBUG_STATUS->{w}, $DEBUG_STATUS->{h}); |
63 | $DEBUG_STATUS->size_allocate ($WIDTH - $w, 0, $w, $h); |
|
|
64 | } |
81 | } |
65 | |
82 | |
66 | sub start_game { |
83 | sub start_game { |
67 | status "logging in..."; |
84 | status "logging in..."; |
68 | |
85 | |
69 | my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32; |
86 | my $mapsize = List::Util::min 32, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32; |
|
|
87 | |
|
|
88 | $MAPCACHE = CFClient::db_table "mapcache_$CFG->{host}"; |
70 | |
89 | |
71 | $MAP = new CFClient::Map $mapsize, $mapsize; |
90 | $MAP = new CFClient::Map $mapsize, $mapsize; |
|
|
91 | |
|
|
92 | my ($host, $port) = split /:/, $CFG->{host}; |
72 | |
93 | |
73 | $CONN = new conn |
94 | $CONN = new conn |
74 | host => $CFG->{host}, |
95 | host => $host, |
75 | port => $CFG->{port}, |
96 | port => $port || 13327, |
76 | user => $CFG->{user}, |
97 | user => $CFG->{user}, |
77 | pass => $CFG->{password}, |
98 | pass => $CFG->{password}, |
78 | mapw => $mapsize, |
99 | mapw => $mapsize, |
79 | maph => $mapsize, |
100 | maph => $mapsize, |
80 | ; |
101 | ; |
… | |
… | |
86 | |
107 | |
87 | sub stop_game { |
108 | sub stop_game { |
88 | undef $CONN; |
109 | undef $CONN; |
89 | } |
110 | } |
90 | |
111 | |
91 | sub config_dialog { |
112 | sub client_setup { |
92 | my $dialog = new CFClient::UI::FancyFrame |
113 | my $dialog = new CFClient::UI::FancyFrame |
93 | child => (my $vbox = new CFClient::UI::VBox); |
114 | child => (my $vbox = new CFClient::UI::VBox); |
94 | $vbox->add (new CFClient::UI::Label align => 0, text => "Client Setup"); |
115 | $vbox->add (new CFClient::UI::Label align => 0, text => "Client Setup"); |
95 | $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]); |
116 | $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]); |
96 | |
117 | |
… | |
… | |
134 | |
155 | |
135 | $table->add (0, 5, new CFClient::UI::Label align => 1, text => "FoW Smooth"); |
156 | $table->add (0, 5, new CFClient::UI::Label align => 1, text => "FoW Smooth"); |
136 | $table->add (1, 5, new CFClient::UI::CheckBox state => $CFG->{fow_smooth}, connect_changed => sub { |
157 | $table->add (1, 5, new CFClient::UI::CheckBox state => $CFG->{fow_smooth}, connect_changed => sub { |
137 | my ($self, $value) = @_; |
158 | my ($self, $value) = @_; |
138 | $CFG->{fow_smooth} = $value; |
159 | $CFG->{fow_smooth} = $value; |
|
|
160 | status "Fog of War smoothing requires OpenGL 1.2 or higher" if $CFClient::GL_VERSION < 1.2; |
139 | }); |
161 | }); |
140 | |
162 | |
|
|
163 | $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Log Fontsize"); |
|
|
164 | $table->add (1, 6, new CFClient::UI::Slider range => [$CFG->{log_fontsize}, 8, 30, 1], connect_changed => sub { |
|
|
165 | my ($self, $value) = @_; |
|
|
166 | $LOGVIEW->set_fontsize ($CFG->{log_fontsize} = int $value); |
|
|
167 | }); |
|
|
168 | |
141 | $table->add (1, 6, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub { |
169 | $table->add (1, 7, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub { |
142 | destroy_screen (); |
170 | video_shutdown (); |
143 | init_screen (); |
171 | video_init (); |
144 | }); |
172 | }); |
|
|
173 | |
|
|
174 | $dialog |
|
|
175 | } |
|
|
176 | |
|
|
177 | sub metaserver_dialog { |
|
|
178 | my $dialog = new CFClient::UI::FancyFrame |
|
|
179 | child => (my $vbox = new CFClient::UI::VBox); |
|
|
180 | |
|
|
181 | $vbox->add ($dialog->{table} = new CFClient::UI::Table); |
|
|
182 | |
|
|
183 | $dialog |
|
|
184 | } |
|
|
185 | |
|
|
186 | sub update_metaserver { |
|
|
187 | my ($HOST) = @_; |
|
|
188 | |
|
|
189 | status "fetching metaserver list..."; |
|
|
190 | |
|
|
191 | my $buf; |
|
|
192 | |
|
|
193 | my $fh = new IO::Socket::INET PeerHost => $META_SERVER, Blocking => 0; |
|
|
194 | |
|
|
195 | Event->io (fd => $fh, poll => 'r', cb => sub { |
|
|
196 | my $res = sysread $fh, $buf, 8192, length $buf; |
|
|
197 | |
|
|
198 | if (!defined $res) { |
|
|
199 | $_[0]->w->cancel; |
|
|
200 | status "metaserver: $!"; |
|
|
201 | } elsif ($res == 0) { |
|
|
202 | $_[0]->w->cancel; |
|
|
203 | status "server list retrieved"; |
|
|
204 | |
|
|
205 | my $table = $METASERVER->{table}; |
|
|
206 | |
|
|
207 | $table->clear; |
|
|
208 | |
|
|
209 | my @col = qw(Use #Users Host Uptime Version Description); |
|
|
210 | $table->add ($_, 0, new CFClient::UI::Label align => 0, fg => [1, 1, 0], text => $col[$_]) |
|
|
211 | for 0 .. $#col; |
|
|
212 | |
|
|
213 | my @align = qw(1 0 1 1 -1); |
|
|
214 | |
|
|
215 | my $y = 0; |
|
|
216 | for my $m (sort { $b->[3] <=> $a->[3] } map [split /\|/], split /\015?\012/, $buf) { |
|
|
217 | my ($ip, $last, $host, $users, $version, $desc, $ibytes, $obytes, $uptime) = @$m; |
|
|
218 | |
|
|
219 | for ($desc) { |
|
|
220 | s/<br>/\n/gi; |
|
|
221 | s/<li>/\n· /gi; |
|
|
222 | s/<.*?>//sgi; |
|
|
223 | s/&/&/g; |
|
|
224 | s/</</g; |
|
|
225 | s/>/>/g; |
|
|
226 | } |
|
|
227 | |
|
|
228 | $uptime = sprintf "%dd %02d:%02d:%02d", |
|
|
229 | (int $m->[8] / 86400), |
|
|
230 | (int $m->[8] / 3600) % 24, |
|
|
231 | (int $m->[8] / 60) % 60, |
|
|
232 | $m->[8] % 60; |
|
|
233 | |
|
|
234 | $m = [$users, $host, $uptime, $version, $desc]; |
|
|
235 | |
|
|
236 | $y++; |
|
|
237 | |
|
|
238 | $table->add (0, $y, new CFClient::UI::VBox children => [ |
|
|
239 | (new CFClient::UI::Button text => " ", connect_activate => sub { |
|
|
240 | $HOST->set_text ($CFG->{host} = $host); |
|
|
241 | }), |
|
|
242 | (new CFClient::UI::Empty expand => 1), |
|
|
243 | ]); |
|
|
244 | |
|
|
245 | $table->add ($_ + 1, $y, new CFClient::UI::Label align => $align[$_], text => $m->[$_], fontsize => $FONTSIZE * 0.8) |
|
|
246 | for 0 .. $#$m; |
|
|
247 | } |
|
|
248 | } |
|
|
249 | }); |
|
|
250 | } |
|
|
251 | |
|
|
252 | sub server_setup { |
|
|
253 | my $dialog = new CFClient::UI::FancyFrame |
|
|
254 | child => (my $vbox = new CFClient::UI::VBox); |
145 | |
255 | |
146 | $vbox->add (new CFClient::UI::Label align => 0, text => "Server Setup"); |
256 | $vbox->add (new CFClient::UI::Label align => 0, text => "Server Setup"); |
147 | $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]); |
257 | $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]); |
148 | $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Host"); |
258 | $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Host:Port"); |
149 | $table->add (1, 2, my $host = new CFClient::UI::Entry text => $CFG->{host}); |
259 | |
150 | |
260 | { |
151 | $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Port"); |
261 | $table->add (1, 2, my $vbox = new CFClient::UI::VBox); |
152 | $table->add (1, 3, my $port = new CFClient::UI::Entry text => $CFG->{port}); |
262 | |
|
|
263 | $vbox->add (my $HOST = new CFClient::UI::Entry text => $CFG->{host}, connect_changed => sub { |
|
|
264 | my ($self, $value) = @_; |
|
|
265 | $CFG->{host} = $value; |
|
|
266 | }); |
|
|
267 | |
|
|
268 | $METASERVER = metaserver_dialog; |
|
|
269 | |
|
|
270 | $vbox->add (new CFClient::UI::Flopper text => "Metaserver", other => $METASERVER, connect_open => sub { |
|
|
271 | update_metaserver $HOST; |
|
|
272 | }); |
|
|
273 | } |
153 | |
274 | |
154 | $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Username"); |
275 | $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Username"); |
155 | $table->add (1, 4, my $user = new CFClient::UI::Entry text => $CFG->{user}); |
276 | $table->add (1, 4, new CFClient::UI::Entry text => $CFG->{user}, connect_changed => sub { |
|
|
277 | my ($self, $value) = @_; |
|
|
278 | $CFG->{user} = $value; |
|
|
279 | }); |
156 | |
280 | |
157 | $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Password"); |
281 | $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Password"); |
158 | $table->add (1, 5, my $pass = new CFClient::UI::Entry text => $CFG->{password}, hidden => 1); |
282 | $table->add (1, 5, new CFClient::UI::Entry text => $CFG->{password}, hidden => 1, connect_changed => sub { |
|
|
283 | my ($self, $value) = @_; |
|
|
284 | $CFG->{password} = $value; |
|
|
285 | }); |
159 | |
286 | |
|
|
287 | $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Def. say cmd"); |
|
|
288 | $table->add (1, 6, my $saycmd = new CFClient::UI::Entry text => $CFG->{say_command}, connect_changed => sub { |
|
|
289 | my ($self, $value) = @_; |
|
|
290 | $CFG->{say_command} = $value; |
|
|
291 | }); |
|
|
292 | |
160 | $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Map Size"); |
293 | $table->add (0, 7, new CFClient::UI::Label align => 1, text => "Map Size"); |
161 | $table->add (1, 6, new CFClient::UI::Slider |
294 | $table->add (1, 7, new CFClient::UI::Slider |
162 | req_w => 100, |
295 | req_w => 100, |
163 | range => [$CFG->{mapsize}, 10, 100 + 1, 1], |
296 | range => [$CFG->{mapsize}, 10, 100 + 1, 1], |
164 | connect_changed => sub { |
297 | connect_changed => sub { |
165 | my ($self, $value) = @_; |
298 | my ($self, $value) = @_; |
166 | |
299 | |
167 | $CFG->{mapsize} = $self->{range}[0] = $value = int $value; |
300 | $CFG->{mapsize} = $self->{range}[0] = $value = int $value; |
168 | }, |
301 | }, |
169 | ); |
302 | ); |
170 | |
303 | |
171 | $table->add (1, 7, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub { |
304 | $table->add (1, 8, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub { |
172 | start_game; |
305 | start_game; |
173 | }); |
306 | }); |
174 | |
307 | |
175 | $vbox->add (my $hbox = new CFClient::UI::HBox); |
|
|
176 | |
|
|
177 | $hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Save", connect_activate => sub { |
|
|
178 | CFClient::write_cfg "$Crossfire::VARDIR/pclientrc"; |
|
|
179 | status "Configuration Saved"; |
|
|
180 | }); |
|
|
181 | |
|
|
182 | $dialog |
308 | $dialog |
183 | } |
309 | } |
184 | |
310 | |
185 | sub console_window { |
311 | sub message_window { |
186 | my $window = new CFClient::UI::FancyFrame |
312 | my $window = new CFClient::UI::FancyFrame |
187 | border_bg => [1, 1, 1, 0.5], |
313 | border_bg => [1, 1, 1, 0.5], |
188 | bg => [0.3, 0.3, 0.3, 0.8], |
314 | bg => [0.3, 0.3, 0.3, 0.8], |
|
|
315 | user_w => int $::WIDTH / 3, |
|
|
316 | user_h => int $::HEIGHT / 5, |
189 | child => (my $vbox = new CFClient::UI::VBox); |
317 | child => (my $vbox = new CFClient::UI::VBox); |
190 | |
318 | |
191 | $vbox->add ($LOGVIEW = new CFClient::UI::TextView expand => 1); |
319 | $vbox->add ($LOGVIEW = new CFClient::UI::TextView |
|
|
320 | expand => 1, |
|
|
321 | fontsize => $::CFG->{log_fontsize}, |
|
|
322 | ); |
|
|
323 | |
192 | $vbox->add (my $input = new CFClient::UI::Entry); |
324 | $vbox->add (my $input = new CFClient::UI::Entry |
|
|
325 | connect_focus_in => sub { |
|
|
326 | my ($input, $prev_focus) = @_; |
|
|
327 | |
|
|
328 | delete $input->{refocus_map}; |
|
|
329 | |
|
|
330 | if ($prev_focus == $MAPWIDGET && $input->{auto_activated}) { |
|
|
331 | $input->{refocus_map} = 1; |
|
|
332 | } |
|
|
333 | delete $input->{auto_activated}; |
|
|
334 | }, |
|
|
335 | connect_activate => sub { |
|
|
336 | my ($input, $text) = @_; |
|
|
337 | $input->set_text (''); |
|
|
338 | |
|
|
339 | if ($text =~ /^\/(.*)/) { |
|
|
340 | $::CONN->user_send ($1); |
|
|
341 | } else { |
|
|
342 | my $say_cmd = $::CFG->{say_command} || 'say'; |
|
|
343 | $::CONN->user_send ("$say_cmd $text"); |
|
|
344 | } |
|
|
345 | if ($input->{refocus_map}) { |
|
|
346 | delete $input->{refocus_map}; |
|
|
347 | $MAPWIDGET->focus_in |
|
|
348 | } |
|
|
349 | }, |
|
|
350 | connect_escape => sub { |
|
|
351 | $MAPWIDGET->focus_in |
|
|
352 | }, |
|
|
353 | ); |
|
|
354 | |
|
|
355 | $CONSOLE = { |
|
|
356 | window => $window, |
|
|
357 | input => $input |
|
|
358 | }; |
193 | |
359 | |
194 | $window |
360 | $window |
195 | } |
361 | } |
196 | |
362 | |
197 | sub sdl_init { |
363 | sub sdl_init { |
|
|
364 | #SDL::Init SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE |
198 | SDL::Init SDL_INIT_AUDIO | SDL_INIT_VIDEO |
365 | SDL::Init SDL_INIT_AUDIO | SDL_INIT_VIDEO |
199 | and die "SDL::Init failed!\n"; |
366 | and die "SDL::Init failed!\n"; |
200 | } |
367 | } |
201 | |
368 | |
202 | sub init_screen { |
369 | sub video_init { |
203 | sdl_init; |
370 | sdl_init; |
204 | |
371 | |
205 | ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] }; |
372 | ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] }; |
206 | $FULLSCREEN = $CFG->{fullscreen}; |
373 | $FULLSCREEN = $CFG->{fullscreen}; |
207 | $FAST = $CFG->{fast}; |
374 | $FAST = $CFG->{fast}; |
208 | |
375 | |
209 | SDL::GLSetAttribute SDL_GL_RED_SIZE, 5; |
376 | SDL::GLSetAttribute SDL_GL_RED_SIZE, 5; |
210 | SDL::GLSetAttribute SDL_GL_GREEN_SIZE, 5; |
377 | SDL::GLSetAttribute SDL_GL_GREEN_SIZE, 5; |
211 | SDL::GLSetAttribute SDL_GL_BLUE_SIZE, 5; |
378 | SDL::GLSetAttribute SDL_GL_BLUE_SIZE, 5; |
212 | SDL::GLSetAttribute SDL_GL_ALPHA_SIZE, 0; |
379 | SDL::GLSetAttribute SDL_GL_ALPHA_SIZE, 1; |
213 | |
380 | |
214 | SDL::GLSetAttribute SDL_GL_ACCUM_RED_SIZE, 0; |
381 | SDL::GLSetAttribute SDL_GL_ACCUM_RED_SIZE, 0; |
215 | SDL::GLSetAttribute SDL_GL_ACCUM_GREEN_SIZE, 0; |
382 | SDL::GLSetAttribute SDL_GL_ACCUM_GREEN_SIZE, 0; |
216 | SDL::GLSetAttribute SDL_GL_ACCUM_BLUE_SIZE, 0; |
383 | SDL::GLSetAttribute SDL_GL_ACCUM_BLUE_SIZE, 0; |
217 | SDL::GLSetAttribute SDL_GL_ACCUM_ALPHA_SIZE, 0; |
384 | SDL::GLSetAttribute SDL_GL_ACCUM_ALPHA_SIZE, 0; |
… | |
… | |
239 | $FONTSIZE = int $HEIGHT / 40; |
406 | $FONTSIZE = int $HEIGHT / 40; |
240 | |
407 | |
241 | ############################################################################# |
408 | ############################################################################# |
242 | |
409 | |
243 | $DEBUG_STATUS = new CFClient::UI::Label padding => 0, z => 100; |
410 | $DEBUG_STATUS = new CFClient::UI::Label padding => 0, z => 100; |
244 | $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS); |
411 | $CFClient::UI::ROOT->add ($DEBUG_STATUS); |
245 | |
412 | |
246 | $STATUS_LINE = new CFClient::UI::Label |
413 | $STATUS_LINE = new CFClient::UI::Label |
247 | padding => 0, |
414 | padding => 0, |
248 | y => $HEIGHT * 44 / 45 - $FONTSIZE; |
415 | y => $HEIGHT * 44 / 45 - $FONTSIZE; |
249 | $CFClient::UI::TOPLEVEL->add ($STATUS_LINE); |
416 | $CFClient::UI::ROOT->add ($STATUS_LINE); |
250 | |
417 | |
251 | $ALT_ENTER_MESSAGE = new CFClient::UI::Label |
418 | $ALT_ENTER_MESSAGE = new CFClient::UI::Label |
252 | padding => 0, |
419 | padding => 0, |
253 | y => $HEIGHT * 44 / 45, |
420 | y => $HEIGHT * 44 / 45, |
254 | height => $HEIGHT / 45, |
421 | fontsize => $HEIGHT / 45, |
255 | text => "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
422 | markup => "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
256 | $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
423 | $CFClient::UI::ROOT->add ($ALT_ENTER_MESSAGE); |
257 | |
424 | |
258 | $CFClient::UI::TOPLEVEL->add ($MAPWIDGET = new CFClient::UI::MapWidget); |
425 | $CFClient::UI::ROOT->add ($MAPWIDGET = new CFClient::UI::MapWidget); |
259 | $MAPWIDGET->focus_in; |
426 | $MAPWIDGET->focus_in; |
|
|
427 | $MAPWIDGET->connect (activate_console => sub { |
|
|
428 | my ($mapwidget, $preset) = @_; |
260 | |
429 | |
|
|
430 | if ($CONSOLE) { |
|
|
431 | $CONSOLE->{input}->{auto_activated} = 1; |
|
|
432 | $CONSOLE->{input}->focus_in; |
|
|
433 | |
|
|
434 | if ($preset && $CONSOLE->{input}->get_text eq '') { |
|
|
435 | $CONSOLE->{input}->set_text ($preset); |
|
|
436 | } |
|
|
437 | } |
|
|
438 | }); |
|
|
439 | |
261 | $CFClient::UI::TOPLEVEL->add ($BUTTONBAR = new CFClient::UI::HBox); |
440 | $CFClient::UI::ROOT->add ($BUTTONBAR = new CFClient::UI::HBox); |
262 | |
441 | |
263 | $BUTTONBAR->add (my $setup = new CFClient::UI::Flopper x => 0, y => 0, text => "Setup", other => config_dialog, state => 1); |
442 | $BUTTONBAR->add (new CFClient::UI::Flopper text => "Client Setup", other => client_setup); |
264 | $BUTTONBAR->add (my $setup = new CFClient::UI::Flopper x => 0, y => 0, text => "Console", other => console_window); |
443 | $BUTTONBAR->add (new CFClient::UI::Flopper text => "Server Setup", other => server_setup); |
265 | } |
444 | $BUTTONBAR->add (new CFClient::UI::Flopper text => "Message Window", other => message_window); |
266 | |
445 | |
267 | sub destroy_screen { |
446 | $BUTTONBAR->add (new CFClient::UI::Button text => "Save Config", connect_activate => sub { |
|
|
447 | CFClient::write_cfg "$Crossfire::VARDIR/pclientrc"; |
|
|
448 | status "Configuration Saved"; |
|
|
449 | }); |
|
|
450 | |
|
|
451 | $BUTTONBAR->{children}[1]->emit ("activate"); # pop up server setup |
|
|
452 | |
|
|
453 | my $tgw = new CFClient::UI::FancyFrame (x => $WIDTH - 300, y => 0); |
|
|
454 | $tgw->add (my $hbox = new CFClient::UI::HBox ()); |
|
|
455 | |
|
|
456 | $hbox->add (my $hg = new CFClient::UI::VGauge (gauge => 'hp')); |
|
|
457 | $hbox->add (my $mg = new CFClient::UI::VGauge (gauge => 'mana')); |
|
|
458 | $hbox->add (my $gg = new CFClient::UI::VGauge (gauge => 'grace')); |
|
|
459 | $hbox->add (my $fg = new CFClient::UI::VGauge (gauge => 'food')); |
|
|
460 | |
|
|
461 | $GAUGES = { food => $fg, mana => $mg, hp => $hg, grace => $gg }; |
|
|
462 | $CFClient::UI::ROOT->add ($tgw); |
|
|
463 | } |
|
|
464 | |
|
|
465 | sub video_shutdown { |
268 | $CFClient::UI::TOPLEVEL->{children} = []; |
466 | $CFClient::UI::ROOT->{children} = []; |
269 | undef $SDL_ACTIVE; |
467 | undef $SDL_ACTIVE; |
270 | undef $SDL_EV; |
468 | undef $SDL_EV; |
271 | SDL::Quit; |
469 | } |
|
|
470 | |
|
|
471 | my $bgmusic;#TODO#hack#d# |
|
|
472 | |
|
|
473 | sub audio_init { |
|
|
474 | if ($CFG->{sound} || 1) { |
|
|
475 | if (open my $fh, "<:utf8", CFClient::find_rcfile "sounds/config") { |
|
|
476 | $SDL_MIXER = new SDL::Mixer; |
|
|
477 | $SDL_MIXER->allocate_channels (8); |
|
|
478 | |
|
|
479 | $bgmusic = new SDL::Music CFClient::find_rcfile "music/game3.ogg"; |
|
|
480 | $SDL_MIXER->play_music ($bgmusic, -1); |
|
|
481 | |
|
|
482 | while (<$fh>) { |
|
|
483 | next if /^\s*#/; |
|
|
484 | next if /^\s*$/; |
|
|
485 | |
|
|
486 | my ($file, $volume, $event) = split /\s+/, $_, 3; |
|
|
487 | |
|
|
488 | push @SOUNDS, "$volume,$file"; |
|
|
489 | |
|
|
490 | $AUDIO_CHUNKS{"$volume,$file"} ||= do { |
|
|
491 | my $chunk = new SDL::Sound CFClient::find_rcfile "sounds/$file"; |
|
|
492 | $chunk->volume ($volume * 128 / 100); |
|
|
493 | $chunk |
|
|
494 | }; |
|
|
495 | } |
|
|
496 | } else { |
|
|
497 | status "unable to open sound config: $!"; |
|
|
498 | } |
|
|
499 | } |
|
|
500 | } |
|
|
501 | |
|
|
502 | sub audio_shutdown { |
|
|
503 | undef $SDL_MIXER; |
|
|
504 | @SOUNDS = (); |
|
|
505 | %AUDIO_CHUNKS = (); |
272 | } |
506 | } |
273 | |
507 | |
274 | my %animate_object; |
508 | my %animate_object; |
275 | my $animate_timer; |
509 | my $animate_timer; |
276 | |
510 | |
… | |
… | |
284 | debug sprintf "%3.2f", $fps; |
518 | debug sprintf "%3.2f", $fps; |
285 | |
519 | |
286 | $want_refresh = 0; |
520 | $want_refresh = 0; |
287 | $can_refresh = 0; |
521 | $can_refresh = 0; |
288 | |
522 | |
289 | glViewport 0, 0, $WIDTH, $HEIGHT; |
|
|
290 | |
|
|
291 | glMatrixMode GL_PROJECTION; |
|
|
292 | glLoadIdentity; |
|
|
293 | glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; |
|
|
294 | glMatrixMode GL_MODELVIEW; |
|
|
295 | glLoadIdentity; |
|
|
296 | |
|
|
297 | glClearColor +($CFG->{fow_intensity}) x 3, 1; |
|
|
298 | glClear GL_COLOR_BUFFER_BIT; |
|
|
299 | |
|
|
300 | $CFClient::UI::TOPLEVEL->draw; |
523 | $CFClient::UI::ROOT->draw; |
301 | |
524 | |
302 | SDL::GLSwapBuffers; |
525 | SDL::GLSwapBuffers; |
303 | |
526 | |
304 | $LAST_REFRESH = $NOW; |
527 | $LAST_REFRESH = $NOW; |
305 | } |
528 | } |
… | |
… | |
336 | delete $animate_object{$widget}; |
559 | delete $animate_object{$widget}; |
337 | } |
560 | } |
338 | |
561 | |
339 | @conn::ISA = Crossfire::Protocol::; |
562 | @conn::ISA = Crossfire::Protocol::; |
340 | |
563 | |
|
|
564 | sub 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 | |
341 | sub conn::user_send { |
585 | sub conn::user_send { |
342 | my ($self, $command) = @_; |
586 | my ($self, $command) = @_; |
343 | |
587 | |
344 | $self->send ($command); |
588 | $self->send_command ($command); |
345 | status $command; |
589 | status $command; |
|
|
590 | } |
|
|
591 | |
|
|
592 | sub conn::map_scroll { |
|
|
593 | my ($self, $dx, $dy) = @_; |
|
|
594 | |
|
|
595 | $MAP->scroll ($dx, $dy); |
346 | } |
596 | } |
347 | |
597 | |
348 | sub conn::feed_map1a { |
598 | sub conn::feed_map1a { |
349 | my ($self, $data) = @_; |
599 | my ($self, $data) = @_; |
350 | |
600 | |
351 | # $self->Crossfire::Protocol::feed_map1a ($data); |
601 | # $self->Crossfire::Protocol::feed_map1a ($data); |
352 | |
602 | |
353 | $MAP->scroll (delete $self->{delayed_scroll_x}, delete $self->{delayed_scroll_y}); |
|
|
354 | $MAP->map1a_update ($data); |
603 | $MAP->map1a_update ($data); |
355 | $MAPWIDGET->update; |
604 | $MAPWIDGET->update; |
356 | } |
605 | } |
357 | |
606 | |
358 | #sub conn::map_update { |
607 | sub conn::flush_map { |
359 | # my ($self, $dirty) = @_; |
608 | my ($self) = @_; |
360 | # |
609 | |
361 | # $MAPWIDGET->update; |
610 | my $map_info = delete $self->{map_info} |
362 | #} |
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 | } |
363 | |
621 | |
364 | sub conn::map_clear { |
622 | sub conn::map_clear { |
365 | my ($self) = @_; |
623 | my ($self) = @_; |
366 | |
624 | |
|
|
625 | $self->flush_map; |
|
|
626 | delete $self->{neigh}; |
|
|
627 | |
367 | $MAP->clear; |
628 | $MAP->clear; |
|
|
629 | } |
368 | |
630 | |
369 | # refresh; |
631 | |
|
|
632 | sub 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 | |
|
|
647 | sub 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 | |
|
|
679 | sub 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); |
370 | } |
699 | } |
371 | |
700 | |
372 | sub conn::face_find { |
701 | sub conn::face_find { |
373 | my ($self, $face) = @_; |
702 | my ($self, $facenum, $face) = @_; |
374 | |
703 | |
375 | $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 | |
|
|
729 | gotid: |
|
|
730 | $face->{id} = $id; |
|
|
731 | $MAP->set_face ($facenum => $id); |
|
|
732 | $TILECACHE->get ($id) |
376 | } |
733 | } |
377 | |
734 | |
378 | sub conn::face_update { |
735 | sub conn::face_update { |
379 | my ($self, $facenum, $face) = @_; |
736 | my ($self, $facenum, $face) = @_; |
380 | |
737 | |
381 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
738 | $TILECACHE->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes |
382 | |
739 | |
383 | my $tex = $face->{texture} = new_from_image CFClient::Texture delete $face->{image}; |
740 | $self->set_texture ($face->{id} => delete $face->{image}); |
|
|
741 | } |
384 | |
742 | |
|
|
743 | sub 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 | |
385 | $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}}); |
386 | $MAPWIDGET->update; |
752 | $MAPWIDGET->update; |
|
|
753 | |
|
|
754 | $tex |
|
|
755 | }; |
|
|
756 | } |
|
|
757 | |
|
|
758 | sub conn::sound_play { |
|
|
759 | my ($self, $x, $y, $soundnum, $type) = @_; |
|
|
760 | |
|
|
761 | my $chunk = $AUDIO_CHUNKS{$SOUNDS[$soundnum]} |
|
|
762 | or return; |
|
|
763 | |
|
|
764 | $SDL_MIXER->play_channel (-1, $chunk); |
|
|
765 | warn "sound $x,$y,$soundnum,$type\n";#d# |
387 | } |
766 | } |
388 | |
767 | |
389 | sub conn::query { |
768 | sub conn::query { |
390 | my ($self, $flags, $prompt) = @_; |
769 | my ($self, $flags, $prompt) = @_; |
391 | |
770 | |
… | |
… | |
397 | my ($self, $color, $text) = @_; |
776 | my ($self, $color, $text) = @_; |
398 | |
777 | |
399 | my @color = ( |
778 | my @color = ( |
400 | [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00], |
779 | [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00], |
401 | [1.00, 1.00, 1.00], |
780 | [1.00, 1.00, 1.00], |
402 | [0.00, 0.00, 0.55], |
781 | [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55] |
403 | [1.00, 0.00, 0.00], |
782 | [1.00, 0.00, 0.00], |
404 | [1.00, 0.54, 0.00], |
783 | [1.00, 0.54, 0.00], |
405 | [0.11, 0.56, 1.00], |
784 | [0.11, 0.56, 1.00], |
406 | [0.93, 0.46, 0.00], |
785 | [0.93, 0.46, 0.00], |
407 | [0.18, 0.54, 0.34], |
786 | [0.18, 0.54, 0.34], |
… | |
… | |
425 | refresh; |
804 | refresh; |
426 | }, |
805 | }, |
427 | SDL_KEYDOWN() => sub { |
806 | SDL_KEYDOWN() => sub { |
428 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
807 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
429 | # alt-enter |
808 | # alt-enter |
430 | destroy_screen; |
809 | video_shutdown; |
431 | $CFG->{fullscreen} = !$CFG->{fullscreen}; |
810 | $CFG->{fullscreen} = !$CFG->{fullscreen}; |
432 | init_screen; |
811 | video_init; |
433 | } else { |
812 | } else { |
434 | CFClient::UI::feed_sdl_key_down_event ($SDL_EV); |
813 | CFClient::UI::feed_sdl_key_down_event ($SDL_EV); |
435 | } |
814 | } |
436 | }, |
815 | }, |
437 | SDL_KEYUP() => sub { |
816 | SDL_KEYUP() => sub { |
… | |
… | |
451 | }, |
830 | }, |
452 | ); |
831 | ); |
453 | |
832 | |
454 | ############################################################################# |
833 | ############################################################################# |
455 | |
834 | |
|
|
835 | $SIG{INT} = $SIG{TERM} = sub { exit }; |
|
|
836 | |
|
|
837 | $TILECACHE = CFClient::db_table "tilecache"; |
|
|
838 | $FACEMAP = CFClient::db_table "facemap"; |
|
|
839 | |
456 | CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; |
840 | CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; |
457 | |
841 | |
458 | my %DEF_CFG = ( |
842 | my %DEF_CFG = ( |
|
|
843 | sdl_mode => 0, |
459 | width => 640, |
844 | width => 640, |
460 | height => 480, |
845 | height => 480, |
|
|
846 | fullscreen => 0, |
461 | fast => 0, |
847 | fast => 0, |
462 | fow_enable => 1, |
848 | fow_enable => 1, |
463 | fow_intensity => 0.45, |
849 | fow_intensity => 0.45, |
464 | fow_smooth => 0, |
850 | fow_smooth => 0, |
465 | fullscreen => 0, |
851 | log_fontsize => 14, |
466 | sdl_mode => 0, |
|
|
467 | mapsize => 100, |
852 | mapsize => 100, |
468 | host => "crossfire.schmorp.de", |
853 | host => "crossfire.schmorp.de", |
469 | port => 13327, |
854 | say_command => 'say', |
470 | ); |
855 | ); |
471 | |
856 | |
472 | while (my ($k, $v) = each %DEF_CFG) { |
857 | while (my ($k, $v) = each %DEF_CFG) { |
473 | $CFG->{$k} = $v unless exists $CFG->{$k}; |
858 | $CFG->{$k} = $v unless exists $CFG->{$k}; |
474 | } |
859 | } |
… | |
… | |
482 | |
867 | |
483 | @SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)"; |
868 | @SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)"; |
484 | |
869 | |
485 | $CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES; |
870 | $CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES; |
486 | |
871 | |
487 | init_screen; |
|
|
488 | |
|
|
489 | { |
872 | { |
490 | my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); |
873 | my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); |
491 | |
874 | |
492 | CFClient::add_font $_ for @fonts; |
875 | CFClient::add_font $_ for @fonts; |
493 | CFClient::set_font $fonts[0]; |
876 | CFClient::set_font $fonts[0]; |
494 | } |
877 | } |
495 | |
878 | |
496 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
879 | video_init; |
|
|
880 | audio_init; |
497 | |
881 | |
498 | Event::loop; |
882 | Event::loop; |
499 | |
883 | |
500 | Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; |
884 | END { SDL::Quit } |
501 | |
885 | |
|
|
886 | |