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