ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
(Generate patch)

Comparing deliantra/Deliantra-Client/bin/pclient (file contents):
Revision 1.74 by root, Tue Apr 11 20:44:49 2006 UTC vs.
Revision 1.173 by root, Mon Apr 24 06:05:36 2006 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3use strict; 3use strict;
4use utf8; 4use utf8;
5 5
6use Glib; 6use Time::HiRes 'time';
7use Gtk2 -init;
8
9use SDL;
10use SDL::App;
11use SDL::Event; 7use Event;
12use SDL::Surface;
13use SDL::OpenGL;
14use SDL::OpenGL::Constants;
15 8
16use Crossfire; 9use Crossfire;
17use Crossfire::Protocol; 10use Crossfire::Protocol;
18 11
12use Compress::LZF;
13
19use CFClient; 14use CFClient;
20use CFClient::UI; 15use CFClient::UI;
16use CFClient::MapWidget;
21 17
22our $VERSION = '0.1'; 18our $VERSION = '0.1';
23 19
24my $MAX_FPS = 30; 20my $MAX_FPS = 60;
25my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame 21my $MIN_FPS = 5; # unused as of yet
26 22
23our $META_SERVER = "crossfire.real-time.com:13326";
24
25our $FACEMAP;
26our $TILECACHE;
27our $FACECACHE; 27our $MAPCACHE;
28
29our $LAST_REFRESH;
30our $NOW;
28 31
29our $CFG; 32our $CFG;
30our $CONN; 33our $CONN;
34our $FAST; # fast, low-quality mode, possibly useful for software-rendering
31 35
36our @SDL_MODES;
32our $WIDTH; 37our $WIDTH;
33our $HEIGHT; 38our $HEIGHT;
34our $FULLSCREEN; 39our $FULLSCREEN;
40our $FONTSIZE;
35 41
36our $NOW; 42our $FONT_PROP;
43our $FONT_FIXED;
37 44
45our $MAP;
38our $MAPWIDGET; 46our $MAPWIDGET;
39our $FONTSIZE; 47our $BUTTONBAR;
48our $LOGVIEW;
49our $CONSOLE;
50our $METASERVER;
40 51
52our $FLOORBOX;
53our $GAUGES;
54our $STATWIDS;
55
41our $SDL_TIMER; 56our $SDL_ACTIVE;
42our $SDL_APP;
43our $SDL_EV;
44our %SDL_CB; 57our %SDL_CB;
58
59our $SDL_MIXER;
60our @SOUNDS; # event => file mapping
61our %AUDIO_CHUNKS; # audio files
45 62
46our $ALT_ENTER_MESSAGE; 63our $ALT_ENTER_MESSAGE;
47our $STATUS_LINE; 64our $STATUS_LINE;
48our $DEBUG_STATUS; 65our $DEBUG_STATUS;
49 66
50my $last_refresh; 67sub status {
51my %ANIMATE; 68 $STATUS_LINE->set_text ($_[0]);
52my $refresh_handler; 69 $STATUS_LINE->move (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $STATUS_LINE->{h});
70}
53 71
54our ($tw, $te); # Test widget #d# 72sub debug {
73 $DEBUG_STATUS->set_text ($_[0]);
74 $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0, $DEBUG_STATUS->{w}, $DEBUG_STATUS->{h});
75}
55 76
56sub init_screen { 77sub start_game {
57 $SDL_APP = new SDL::App 78 status "logging in...";
58 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
59 -title => "Crossfire+ Client",
60 -width => $WIDTH,
61 -height => $HEIGHT,
62 -opengl => 1,
63 -red_size => 5,
64 -green_size => 5,
65 -blue_size => 5,
66 -alpha_size => 0,
67 -double_buffer => 1,
68 -fullscreen => $FULLSCREEN,
69 -resizeable => 0;
70 79
71 $SDL_EV = new SDL::Event; 80 my $mapsize = List::Util::min 32, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
72 $SDL_EV->set_unicode (1);
73 81
74 $SDL_TIMER = add Glib::Timeout 1000/50, sub { 82 $MAPCACHE = CFClient::db_table "mapcache_$CFG->{host}";
75 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
76 while $SDL_EV->poll;
77 83
78 1 84 $MAP = new CFClient::Map $mapsize, $mapsize;
79 };
80 85
81 $last_refresh = SDL::GetTicks; 86 my ($host, $port) = split /:/, $CFG->{host};
82
83 CFClient::gl_init;
84
85 $FONTSIZE = int $HEIGHT / 50;
86
87 #############################################################################
88
89 glClearColor 0.45, 0.45, 0.45, 1;
90
91 glEnable GL_TEXTURE_2D;
92 glEnable GL_COLOR_MATERIAL;
93 glShadeModel GL_FLAT;
94 glDisable GL_DEPTH_TEST;
95 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
96
97 #############################################################################
98
99 $DEBUG_STATUS = new CFClient::UI::Label;
100 $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS);
101 87
102 $STATUS_LINE = new CFClient::UI::Label
103 y => $HEIGHT * 59 / 60 - $FONTSIZE;
104 $CFClient::UI::TOPLEVEL->add ($STATUS_LINE);
105
106 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
107 y => $HEIGHT * 59 / 60,
108 height => $HEIGHT / 60,
109 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
110 $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
111
112 $MAPWIDGET = new CFClient::UI::MapWidget;
113 $CFClient::UI::TOPLEVEL->add ($MAPWIDGET);
114 $MAPWIDGET->focus_in;
115}
116
117sub destroy_screen {
118 remove Glib::Source $SDL_TIMER;
119 undef $SDL_APP;
120 undef $SDL_EV;
121 SDL::Quit;
122}
123
124sub config_dialog {
125 my $dialog = new CFClient::UI::FancyFrame x => 300, y => 100,
126 child => (my $vbox = new CFClient::UI::VBox);
127 $vbox->add (new CFClient::UI::Label align => 0, text => "Setup");
128 $vbox->add (my $table = new CFClient::UI::Table);
129
130 $table->add (0, 0, new CFClient::UI::Label text => "Video Mode");
131 $table->add (0, 1, new CFClient::UI::Label text => "Host");
132 $table->add (0, 2, new CFClient::UI::Label text => "Port");
133 $table->add (0, 3, new CFClient::UI::Label text => "Username");
134 $table->add (0, 4, new CFClient::UI::Label text => "Password", hidden => 1);
135 $table->add (0, 5, new CFClient::UI::Label text => "Server Map Size");
136
137 $CFClient::UI::TOPLEVEL->add ($dialog);
138}
139
140sub start_game {
141 $WIDTH = $CFG->{width};
142 $HEIGHT = $CFG->{height};
143 $FULLSCREEN = 0;
144
145 init_screen;
146
147 config_dialog;
148
149 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
150
151 $CONN = new conn 88 $CONN = new conn
152 host => $CFG->{host}, 89 host => $host,
153 port => $CFG->{port}, 90 port => $port || 13327,
154 user => $CFG->{user}, 91 user => $CFG->{user},
155 pass => $CFG->{password}, 92 pass => $CFG->{password},
156 mapw => $mapsize, 93 mapw => $mapsize,
157 maph => $mapsize, 94 maph => $mapsize,
158 ; 95 ;
159 96
97 status "login successful";
98
160 CFClient::lowdelay fileno $CONN->{fh}; 99 CFClient::lowdelay fileno $CONN->{fh};
161} 100}
162 101
163sub stop_game { 102sub stop_game {
164 remove Glib::Source $refresh_handler if $refresh_handler;
165 undef $refresh_handler;
166
167 undef $CONN; 103 undef $CONN;
168 destroy_screen;
169} 104}
170 105
171sub force_refresh { 106sub client_setup {
172 glViewport 0, 0, $WIDTH, $HEIGHT; 107 my $dialog = new CFClient::UI::FancyFrame
108 title => "Client Setup",
109 child => (my $vbox = new CFClient::UI::VBox);
110 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
173 111
174 glMatrixMode GL_PROJECTION; 112 $table->add (0, 0, new CFClient::UI::Label valign => 0, align => 1, text => "Video Mode");
175 glLoadIdentity; 113 $table->add (1, 0, my $hbox = new CFClient::UI::HBox);
176 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
177 glMatrixMode GL_MODELVIEW;
178 glLoadIdentity;
179 114
180 glClear GL_COLOR_BUFFER_BIT; 115 $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]);
116 $hbox->add (my $mode_label = new CFClient::UI::Label align => 0, valign => 0, height => 0.8, template => "9999x9999");
181 117
182 $CFClient::UI::TOPLEVEL->draw; 118 $mode_slider->connect (changed => sub {
119 my ($self, $value) = @_;
183 120
184 SDL::GLSwapBuffers; 121 $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value;
185} 122 $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]});
123 });
124 $mode_slider->emit (changed => $mode_slider->{range}[0]);
186 125
187sub debug { 126 my $row = 1;
188 $DEBUG_STATUS->set_text ($_[0]);
189 $DEBUG_STATUS->size_allocate ($DEBUG_STATUS->size_request);
190 $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0);
191}
192 127
193my $FPS; 128 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Fullscreen");
129 $table->add (1, $row++, new CFClient::UI::CheckBox
130 state => $CFG->{fullscreen},
131 tooltip => "Bring the client into fullscreen mode",
132 connect_changed => sub {
133 my ($self, $value) = @_;
134 $CFG->{fullscreen} = $value;
135 }
136 );
194 137
195sub refresh { 138 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Fast & Ugly");
196 $refresh_handler ||= add Glib::Idle sub { 139 $table->add (1, $row++, new CFClient::UI::CheckBox
197 if ($SDL_APP) { 140 state => $CFG->{fast},
198 $NOW = SDL::GetTicks; 141 tooltip => "Lower the visual quality considerably to speed up rendering.",
142 connect_changed => sub {
143 my ($self, $value) = @_;
144 $CFG->{fast} = $value;
145 }
146 );
199 147
200 if ($NOW - $last_refresh < $TICKS_PER_FRAME) { 148 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Map Scale");
201 SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh); 149 $table->add (1, $row++, new CFClient::UI::Slider
202 $NOW = SDL::GetTicks; 150 range => [$CFG->{map_scale}, 0.25, 2, 0.05],
151 tooltip => "Enlarge or shrink the displayed map",
152 connect_changed => sub {
153 my ($self, $value) = @_;
154 $CFG->{map_scale} = 0.05 * int $value / 0.05;
155 }
156 );
157
158 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Fog of War");
159 $table->add (1, $row++, new CFClient::UI::CheckBox
160 state => $CFG->{fow_enable},
161 tooltip => "Fog-of-War marks areas that cannot be seen by the player",
162 connect_changed => sub {
163 my ($self, $value) = @_;
164 $CFG->{fow_enable} = $value;
165 }
166 );
167
168 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "FoW Intensity");
169 $table->add (1, $row++, new CFClient::UI::Slider
170 range => [$CFG->{fow_intensity}, 0, 1 + 0.001, 0.001],
171 tooltip => "The higher the intensity, the lighter the Fog-of-War color",
172 connect_changed => sub {
173 my ($self, $value) = @_;
174 $CFG->{fow_intensity} = $value;
175 }
176 );
177
178 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "FoW Smooth");
179 $table->add (1, $row++, new CFClient::UI::CheckBox
180 state => $CFG->{fow_smooth},
181 tooltip => "Smooth the Fog-of-War a bit to make it more realistic",
182 connect_changed => sub {
183 my ($self, $value) = @_;
184 $CFG->{fow_smooth} = $value;
185 status "Fog of War smoothing requires OpenGL 1.2 or higher" if $CFClient::GL_VERSION < 1.2;
186 }
187 );
188
189 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "GUI Fontsize");
190 $table->add (1, $row++, new CFClient::UI::Slider
191 range => [$CFG->{gui_fontsize}, 0.5, 2, 0.1],
192 tooltip => "The font size used by most GUI elements",
193 connect_changed => sub {
194 $CFG->{gui_fontsize} = 0.1 * int $_[1] * 10;
195# $FONTSIZE = int $HEIGHT / 40 * $CFG->{gui_fontsize};
196 }
197 );
198
199 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Server Log Fontsize");
200 $table->add (1, $row++, new CFClient::UI::Slider
201 range => [$CFG->{log_fontsize}, 0.5, 2, 0.1],
202 tooltip => "The font size used by the server log window only",
203 connect_changed => sub {
204 $LOGVIEW->set_fontsize ($CFG->{log_fontsize} = 0.1 * int $_[1] * 10);
205 }
206 );
207
208 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Stats Fontsize");
209
210 $table->add (1, $row++, new CFClient::UI::Slider
211 range => [$CFG->{stat_fontsize}, 0.5, 2, 0.1],
212 tooltip => "The font size used by the statistics window only",
213 connect_changed => sub {
214 $CFG->{stat_fontsize} = 0.1 * int $_[1] * 10;
215 &set_stats_window_fontsize;
216 }
217 );
218
219 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Gauge size");
220 $table->add (1, $row++, new CFClient::UI::Slider
221 range => [$CFG->{gauge_size}, 0.2, 0.8, 0.02],
222 tooltip => "Adjust the size of the stats gauges at the bottom right",
223 connect_changed => sub {
224 $CFG->{gauge_size} = $_[1];
225 my $h = int $HEIGHT * $CFG->{gauge_size};
226 $GAUGES->{win}->set_size ($WIDTH, $h);
227 $GAUGES->{win}->move (0, $HEIGHT - $h);
228 }
229 );
230
231 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Gauge fontsize");
232 $table->add (1, $row++, new CFClient::UI::Slider
233 range => [$CFG->{gauge_fontsize}, 0.5, 2.0, 0.1],
234 tooltip => "Adjusts the fontsize of the gauges at the bottom right",
235 connect_changed => sub {
236 $CFG->{gauge_fontsize} = 0.1 * int $_[1] * 10;
237 &set_gauge_window_fontsize;
238 }
239 );
240
241 $table->add (1, $row++, new CFClient::UI::Button
242 expand => 1, align => 0, text => "Apply",
243 tooltip => "Apply the video settings",
244 connect_activate => sub {
245 video_shutdown ();
246 video_init ();
247 }
248 );
249
250 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Audio Enable");
251 $table->add (1, $row++, new CFClient::UI::CheckBox
252 state => $CFG->{audio_enable},
253 tooltip => "If enabled, sound effects and music will be played. If disabled, no audio will be used and the soundcard will not be opened.",
254 connect_changed => sub {
255 $CFG->{audio_enable} = $_[1];
256 }
257 );
258# $table->add (0, 9, new CFClient::UI::Label valign => 0, align => 1, text => "Effects Volume");
259# $table->add (1, 8, new CFClient::UI::Slider range => [$CFG->{effects_volume}, 0, 128, 1], connect_changed => sub {
260# $CFG->{effects_volume} = $_[1];
261# });
262 $table->add (0, $row, new CFClient::UI::Label valign => 0, align => 1, text => "Background Music");
263 $table->add (1, $row++, my $hbox = new CFClient::UI::HBox);
264 $hbox->add (new CFClient::UI::CheckBox
265 expand => 1, state => $CFG->{bgm_enable},
266 tooltip => "Enable background music playing",
267 connect_changed => sub {
268 $CFG->{bgm_enable} = $_[1];
269 }
270 );
271 $hbox->add (new CFClient::UI::Slider
272 expand => 1, range => [$CFG->{bgm_volume}, 0, 1, 0.1],
273 tooltip => "The volume of the background music",
274 connect_changed => sub {
275 $CFG->{bgm_volume} = $_[1];
276 CFClient::MixMusic::volume $_[1] * 128;
277 }
278 );
279
280 $table->add (1, $row++, new CFClient::UI::Button
281 expand => 1, align => 0, text => "Apply",
282 tooltip => "Apply the audio settings",
283 connect_activate => sub {
284 audio_shutdown ();
285 audio_init ();
286 }
287 );
288
289 $dialog
290}
291
292sub set_stats_window_fontsize {
293 for (values %{$STATWIDS}) {
294 $_->set_fontsize ($::CFG->{stat_fontsize});
295 }
296}
297
298sub set_gauge_window_fontsize {
299 for (map { $GAUGES->{$_} } grep { $_ ne 'win' } keys %{$GAUGES}) {
300 $_->set_fontsize ($::CFG->{gauge_fontsize});
301 }
302
303# local $GAUGES->{win}{parent};#d#
304# use PApp::Util; open D, ">:utf8", "d"; print D PApp::Util::dumpval $GAUGES->{win}; close D;
305}
306
307sub make_gauge_window {
308 my $gh = int ($HEIGHT * $CFG->{gauge_size});
309# my $gw = int ($WIDTH * $CFG->{gauge_w_size});
310
311 my $win = new CFClient::UI::Frame (
312 y => $HEIGHT - $gh, x => 0, user_w => $WIDTH, user_h => $gh
313 );
314 $win->add (my $hbox = new CFClient::UI::HBox
315 children => [
316 (new CFClient::UI::HBox expand => 1),
317 ($FLOORBOX = new CFClient::UI::VBox),
318 (my $vbox = new CFClient::UI::VBox),
319 ],
320 );
321
322 $vbox->add (new CFClient::UI::HBox
323 expand => 1,
324 children => [
325 (new CFClient::UI::Empty expand => 1),
326 (my $hb = new CFClient::UI::HBox),
327 ],
328 );
329
330 $hb->add (my $hg = new CFClient::UI::Gauge type => 'hp',
331 tooltip => "Health points - depletes when you get wounded, refills when you heal or idle");
332 $hb->add (my $mg = new CFClient::UI::Gauge type => 'mana',
333 tooltip => "Spell points - deplete when you cast wizard spells, refills when you idle");
334 $hb->add (my $gg = new CFClient::UI::Gauge type => 'grace',
335 tooltip => "Grace points - deplete when you cast priest spells, refills when you pray");
336 $hb->add (my $fg = new CFClient::UI::Gauge type => 'food',
337 tooltip => "Food - depletes with time, faster when you heal or build mana, refills when you eat healthy food");
338
339 $vbox->add (my $exp = new CFClient::UI::Label valign => 0, align => 1, can_hover => 1, can_events => 1,
340 tooltip => "Experience points and level - increases when you kill monsters or successfully use skills");
341 $vbox->add (my $rng = new CFClient::UI::Label valign => 0, align => 1, can_hover => 1, can_events => 1,
342 tooltip => "Ranged attack - how you attack when you press shift-cursor (spell, skill, weapon etc.)");
343
344 $GAUGES = {
345 exp => $exp, win => $win, range => $rng,
346 food => $fg, mana => $mg, hp => $hg, grace => $gg
347 };
348
349 &set_gauge_window_fontsize;
350
351 $win
352}
353
354sub make_stats_window {
355 my $tgw = new CFClient::UI::FancyFrame (x => $WIDTH * 2/5, y => 0, title => "Stats");
356
357 $tgw->add (my $vb = new CFClient::UI::VBox);
358 $vb->add ($STATWIDS->{title} = new CFClient::UI::Label valign => 0, align => -1, text => "Title:", expand => 1);
359 $vb->add ($STATWIDS->{map} = new CFClient::UI::Label valign => 0, align => -1, text => "Map:", expand => 1);
360
361 $vb->add (my $hb = new CFClient::UI::HBox expand => 1);
362
363 $hb->add (my $tbl = new CFClient::UI::Table expand => 1);
364
365 $tbl->add (0, 0, $STATWIDS->{st_str} = new CFClient::UI::Label valign => 0, align => +1, template => "30");
366 $tbl->add (0, 1, $STATWIDS->{st_dex} = new CFClient::UI::Label valign => 0, align => +1, template => "30");
367 $tbl->add (0, 2, $STATWIDS->{st_con} = new CFClient::UI::Label valign => 0, align => +1, template => "30");
368 $tbl->add (0, 3, $STATWIDS->{st_int} = new CFClient::UI::Label valign => 0, align => +1, template => "30");
369 $tbl->add (0, 4, $STATWIDS->{st_wis} = new CFClient::UI::Label valign => 0, align => +1, template => "30");
370 $tbl->add (0, 5, $STATWIDS->{st_pow} = new CFClient::UI::Label valign => 0, align => +1, template => "30");
371 $tbl->add (0, 6, $STATWIDS->{st_cha} = new CFClient::UI::Label valign => 0, align => +1, template => "30");
372
373 $tbl->add (1, 0, $STATWIDS->{st_str_lbl} = new CFClient::UI::Label valign => 0, align => -1, text => "Str");
374 $tbl->add (1, 1, $STATWIDS->{st_dex_lbl} = new CFClient::UI::Label valign => 0, align => -1, text => "Dex");
375 $tbl->add (1, 2, $STATWIDS->{st_con_lbl} = new CFClient::UI::Label valign => 0, align => -1, text => "Con");
376 $tbl->add (1, 3, $STATWIDS->{st_int_lbl} = new CFClient::UI::Label valign => 0, align => -1, text => "Int");
377 $tbl->add (1, 4, $STATWIDS->{st_wis_lbl} = new CFClient::UI::Label valign => 0, align => -1, text => "Wis");
378 $tbl->add (1, 5, $STATWIDS->{st_pow_lbl} = new CFClient::UI::Label valign => 0, align => -1, text => "Pow");
379 $tbl->add (1, 6, $STATWIDS->{st_cha_lbl} = new CFClient::UI::Label valign => 0, align => -1, text => "Cha");
380
381 $tbl->add (2, 0, $STATWIDS->{st_wc} = new CFClient::UI::Label valign => 0, align => +1, template => "-120");
382 $tbl->add (2, 1, $STATWIDS->{st_ac} = new CFClient::UI::Label valign => 0, align => +1, template => "-120");
383 $tbl->add (2, 2, $STATWIDS->{st_dam} = new CFClient::UI::Label valign => 0, align => +1, template => "120");
384 $tbl->add (2, 3, $STATWIDS->{st_arm} = new CFClient::UI::Label valign => 0, align => +1, template => "120");
385 $tbl->add (2, 4, $STATWIDS->{st_spd} = new CFClient::UI::Label valign => 0, align => +1, template => "10.54");
386 $tbl->add (2, 5, $STATWIDS->{st_wspd} = new CFClient::UI::Label valign => 0, align => +1, template => "9");
387
388 $tbl->add (3, 0, $STATWIDS->{st_wc_lbl} = new CFClient::UI::Label valign => 0, align => -1, text => "Wc");
389 $tbl->add (3, 1, $STATWIDS->{st_ac_lbl} = new CFClient::UI::Label valign => 0, align => -1, text => "Ac");
390 $tbl->add (3, 2, $STATWIDS->{st_dam_lbl} = new CFClient::UI::Label valign => 0, align => -1, text => "Dam");
391 $tbl->add (3, 3, $STATWIDS->{st_arm_lbl} = new CFClient::UI::Label valign => 0, align => -1, text => "Arm");
392 $tbl->add (3, 4, $STATWIDS->{st_spd_lbl} = new CFClient::UI::Label valign => 0, align => -1, text => "Sp");
393 $tbl->add (3, 5, $STATWIDS->{st_wspd_lbl} = new CFClient::UI::Label valign => 0, align => -1, text => "WSp");
394
395 $hb->add (my $tbl2 = new CFClient::UI::Table expand => 1);
396
397 my $row = 0;
398 my $col = 0;
399
400 my %resist_names = (
401 slow => "Slow",
402 holyw => "Holy Word",
403 conf => "Confusion",
404 fire => "Fire",
405 depl => "Depletion",
406 magic => "Magic",
407 drain => "Draining",
408 acid => "Acid",
409 pois => "Poison",
410 para => "Paralysation",
411 deat => "Death",
412 phys => "Physical",
413 blind => "Blind",
414 fear => "Fear",
415 tund => "Turn undead",
416 elec => "Electricity",
417 cold => "Cold",
418 ghit => "Ghost hit",
419 );
420 for (qw/slow holyw conf fire depl magic
421 drain acid pois para deat phys
422 blind fear tund elec cold ghit/)
423 {
424 $tbl2->add ($col, $row,
425 $STATWIDS->{"res_$_"} =
426 new CFClient::UI::Label
427 template => "-100%",
428 align => +1,
429 valign => 0,
430 tooltip => $resist_names{$_}
431 );
432 $tbl2->add ($col + 1, $row, new CFClient::UI::Image
433 can_hover => 1,
434 can_events => 1,
435 image => "ui/resist/resist_$_.png",
436 tooltip => $resist_names{$_}
437 );
438
439 $row++;
440 if ($row % 6 == 0) {
441 $col += 2;
442 $row = 0;
443 }
444 }
445
446 &set_stats_window_fontsize;
447 update_stats_window ({});
448
449 $tgw
450}
451
452sub formsep {
453 reverse join ",", grep length, split /(...)/, reverse $_[0] * 1
454}
455
456sub update_stats_window {
457 my ($stats) = @_;
458
459 # i love text protocols!!!
460 my $hp = $stats->{Crossfire::Protocol::CS_STAT_HP} * 1;
461 my $hp_m = $stats->{Crossfire::Protocol::CS_STAT_MAXHP} * 1;
462 my $sp = $stats->{Crossfire::Protocol::CS_STAT_SP} * 1;
463 my $sp_m = $stats->{Crossfire::Protocol::CS_STAT_MAXSP} * 1;
464 my $fo = $stats->{Crossfire::Protocol::CS_STAT_FOOD} * 1;
465 my $fo_m = 999;
466 my $gr = $stats->{Crossfire::Protocol::CS_STAT_GRACE} * 1;
467 my $gr_m = $stats->{Crossfire::Protocol::CS_STAT_MAXGRACE} * 1;
468
469 $GAUGES->{hp} ->set_value ($hp, $hp_m);
470 $GAUGES->{mana} ->set_value ($sp, $sp_m);
471 $GAUGES->{food} ->set_value ($fo, $fo_m);
472 $GAUGES->{grace} ->set_value ($gr, $gr_m);
473 $GAUGES->{exp} ->set_text ("Exp: " . (formsep $stats->{Crossfire::Protocol::CS_STAT_EXP64})
474 . " (lvl " . ($stats->{Crossfire::Protocol::CS_STAT_LEVEL} * 1) . ")");
475 my $rng = $stats->{Crossfire::Protocol::CS_STAT_RANGE};
476 $rng =~ s/^Range: //; # thank you so much dear server
477 $GAUGES->{range} ->set_text ("Rng: " . $rng);
478 my $title = $stats->{Crossfire::Protocol::CS_STAT_TITLE};
479 $title =~ s/^Player: //;
480 $STATWIDS->{title} ->set_text ("Title: " . $title);
481
482 $STATWIDS->{st_str} ->set_text (sprintf "%d", $stats->{5});
483 $STATWIDS->{st_dex} ->set_text (sprintf "%d", $stats->{8});
484 $STATWIDS->{st_con} ->set_text (sprintf "%d", $stats->{9});
485 $STATWIDS->{st_int} ->set_text (sprintf "%d", $stats->{6});
486 $STATWIDS->{st_wis} ->set_text (sprintf "%d", $stats->{7});
487 $STATWIDS->{st_pow} ->set_text (sprintf "%d", $stats->{22});
488 $STATWIDS->{st_cha} ->set_text (sprintf "%d", $stats->{10});
489 $STATWIDS->{st_wc} ->set_text (sprintf "%d", $stats->{13});
490 $STATWIDS->{st_ac} ->set_text (sprintf "%d", $stats->{14});
491 $STATWIDS->{st_dam} ->set_text (sprintf "%d", $stats->{15});
492 $STATWIDS->{st_arm} ->set_text (sprintf "%d", $stats->{16});
493 $STATWIDS->{st_spd} ->set_text (sprintf "%.1f", $stats->{Crossfire::Protocol::CS_STAT_SPEED});
494 $STATWIDS->{st_wspd}->set_text (sprintf "%.1f", $stats->{Crossfire::Protocol::CS_STAT_WEAP_SP});
495
496 my %tbl = (
497 phys => 100,
498 magic => 101,
499 fire => 102,
500 elec => 103,
501 cold => 104,
502 conf => 105,
503 acid => 106,
504 drain => 107,
505 ghit => 108,
506 pois => 109,
507 slow => 110,
508 para => 111,
509 tund => 112,
510 fear => 113,
511 depl => 113,
512 deat => 115,
513 holyw => 116,
514 blind => 117
515 );
516
517 for (keys %tbl) {
518 $STATWIDS->{"res_$_"}->set_text (sprintf "%d%", $stats->{$tbl{$_}});
519 }
520
521}
522
523sub metaserver_dialog {
524 my $dialog = new CFClient::UI::FancyFrame
525 title => "Metaserver",
526 child => (my $vbox = new CFClient::UI::VBox);
527
528 $vbox->add ($dialog->{table} = new CFClient::UI::Table);
529
530 $dialog
531}
532
533sub update_metaserver {
534 my ($HOST) = @_;
535
536 status "fetching metaserver list...";
537
538 my $buf;
539
540 my $fh = new IO::Socket::INET PeerHost => $META_SERVER, Blocking => 0;
541
542 Event->io (fd => $fh, poll => 'r', cb => sub {
543 my $res = sysread $fh, $buf, 8192, length $buf;
544
545 if (!defined $res) {
546 $_[0]->w->cancel;
547 status "metaserver: $!";
548 } elsif ($res == 0) {
549 $_[0]->w->cancel;
550 status "server list retrieved";
551
552 my $table = $METASERVER->{table};
553
554 $table->clear;
555
556 my @col = qw(Use #Users Host Uptime Version Description);
557 $table->add ($_, 0, new CFClient::UI::Label align => 0, fg => [1, 1, 0], text => $col[$_])
558 for 0 .. $#col;
559
560 my @align = qw(1 0 1 1 -1);
561
562 my $y = 0;
563 for my $m (sort { $b->[3] <=> $a->[3] } map [split /\|/], split /\015?\012/, $buf) {
564 my ($ip, $last, $host, $users, $version, $desc, $ibytes, $obytes, $uptime) = @$m;
565
566 for ($desc) {
567 s/<br>/\n/gi;
568 s/<li>/\n· /gi;
569 s/<.*?>//sgi;
570 s/&/&amp;/g;
571 s/</&lt;/g;
572 s/>/&gt;/g;
573 }
574
575 $uptime = sprintf "%dd %02d:%02d:%02d",
576 (int $m->[8] / 86400),
577 (int $m->[8] / 3600) % 24,
578 (int $m->[8] / 60) % 60,
579 $m->[8] % 60;
580
581 $m = [$users, $host, $uptime, $version, $desc];
582
583 $y++;
584
585 $table->add (0, $y, new CFClient::UI::VBox children => [
586 (new CFClient::UI::Button text => " ", connect_activate => sub {
587 $HOST->set_text ($CFG->{host} = $host);
588 }),
589 (new CFClient::UI::Empty expand => 1),
590 ]);
591
592 $table->add ($_ + 1, $y, new CFClient::UI::Label align => $align[$_], text => $m->[$_], fontsize => 0.8)
593 for 0 .. $#$m;
203 } 594 }
595 }
596 });
597}
598
599sub server_setup {
600 my $dialog = new CFClient::UI::FancyFrame
601 title => "Server Setup",
602 child => (my $vbox = new CFClient::UI::VBox);
603
604 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
605 $table->add (0, 2, new CFClient::UI::Label valign => 0, align => 1, text => "Host:Port");
606
607 {
608 $table->add (1, 2, my $vbox = new CFClient::UI::VBox);
609
610 $vbox->add (
611 my $HOST = new CFClient::UI::Entry
612 expand => 1,
613 text => $CFG->{host},
614 tooltip => "The hostname or ip address of the Crossfire(+) server to connect to",
615 connect_changed => sub {
616 my ($self, $value) = @_;
617 $CFG->{host} = $value;
204 618 }
205 my $interval = ($NOW - $last_refresh) * 0.001; 619 );
206 $last_refresh = $NOW;
207 620
208 if ($interval) { 621 $METASERVER = metaserver_dialog;
209 $FPS ||= 1 / $interval; 622
210 $FPS = $FPS * 0.96 + (1 / $interval) * 0.04; 623 $vbox->add (new CFClient::UI::Flopper
211 debug sprintf "%5.02f", $FPS; 624 expand => 1,
625 text => "Metaserver",
626 other => $METASERVER,
627 tooltip => "Show a list of avaible crossfire servers",
628 connect_open => sub {
629 update_metaserver $HOST;
212 } 630 }
631 );
632 }
213 633
214 force_refresh; 634 $table->add (0, 4, new CFClient::UI::Label valign => 0, align => 1, text => "Username");
215 $_->animate ($interval) for grep $_, values %ANIMATE; 635 $table->add (1, 4, new CFClient::UI::Entry
636 text => $CFG->{user},
637 tooltip => "The name of your character on the server",
638 connect_changed => sub {
639 my ($self, $value) = @_;
640 $CFG->{user} = $value;
641 }
642 );
216 643
217 if (%ANIMATE) { 644 $table->add (0, 5, new CFClient::UI::Label valign => 0, align => 1, text => "Password");
645 $table->add (1, 5, new CFClient::UI::Entry
646 text => $CFG->{password},
647 hidden => 1,
648 tooltip => "The password for your character",
649 connect_changed => sub {
650 my ($self, $value) = @_;
651 $CFG->{password} = $value;
652 }
653 );
654
655 $table->add (0, 6, new CFClient::UI::Label valign => 0, align => 1, text => "Def. say cmd");
656 $table->add (1, 6, my $saycmd = new CFClient::UI::Entry
657 text => $CFG->{say_command},
658 tooltip => "This is the command that will be used if you write a line in the message window entry. "
659 ."Usually you want to enter something like 'say' or 'shout' or 'gsay' here. "
660 ."But you could also set it to 'tell &lt;playername&gt;' to only chat with that user.",
661 connect_changed => sub {
662 my ($self, $value) = @_;
663 $CFG->{say_command} = $value;
664 }
665 );
666
667 $table->add (0, 7, new CFClient::UI::Label valign => 0, align => 1, text => "Map Size");
668 $table->add (1, 7, new CFClient::UI::Slider
669 req_w => 100,
670 range => [$CFG->{mapsize}, 10, 100 + 1, 1],
671 tooltip => "This is the size of the portion of the map update the server sends you. "
672 ."If you set this to a high value you will be able to see further for example.",
673 connect_changed => sub {
674 my ($self, $value) = @_;
675
676 $CFG->{mapsize} = $self->{range}[0] = $value = int $value;
677 },
678 );
679
680 $table->add (1, 8, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub {
681 start_game;
682 });
683
684 $dialog
685}
686
687sub message_window {
688 my $window = new CFClient::UI::FancyFrame
689 title => "Messages",
690 border_bg => [1, 1, 1, 0.5],
691 bg => [0.3, 0.3, 0.3, 0.8],
692 user_w => int $::WIDTH / 3,
693 user_h => int $::HEIGHT / 5,
694 child => (my $vbox = new CFClient::UI::VBox);
695
696 $vbox->add ($LOGVIEW = new CFClient::UI::TextView
697 expand => 1,
698 font => $FONT_FIXED,
699 fontsize => $::CFG->{log_fontsize},
700 );
701
702 $vbox->add (my $input = new CFClient::UI::Entry
703 connect_focus_in => sub {
704 my ($input, $prev_focus) = @_;
705
706 delete $input->{refocus_map};
707
708 if ($prev_focus == $MAPWIDGET && $input->{auto_activated}) {
709 $input->{refocus_map} = 1;
218 1 710 }
711 delete $input->{auto_activated};
712 },
713 connect_activate => sub {
714 my ($input, $text) = @_;
715 $input->set_text ('');
716
717 if ($text =~ /^\/(.*)/) {
718 $::CONN->user_send ($1);
219 } else { 719 } else {
220 undef $refresh_handler; 720 my $say_cmd = $::CFG->{say_command} || 'say';
721 $::CONN->user_send ("$say_cmd $text");
722 }
723 if ($input->{refocus_map}) {
724 delete $input->{refocus_map};
725 $MAPWIDGET->focus_in
726 }
727 },
728 connect_escape => sub {
729 $MAPWIDGET->focus_in
730 },
731 );
732
733 $CONSOLE = {
734 window => $window,
735 input => $input
736 };
737
738 $window
739}
740
741sub sdl_init {
742 CFClient::SDL_Init
743 and die "SDL::Init failed!\n";
744}
745
746sub video_init {
747 sdl_init;
748
749 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
750 $FULLSCREEN = $CFG->{fullscreen};
751 $FAST = $CFG->{fast};
752
753 CFClient::SDL_SetVideoMode $WIDTH, $HEIGHT, $FULLSCREEN
754 or die "SDL_SetVideoMode failed!\n";
755
756 $SDL_ACTIVE = 1;
757
758 $LAST_REFRESH = time - 0.01;
759
760 CFClient::gl_init;
761
762 $FONTSIZE = int $HEIGHT / 40 * $CFG->{gui_fontsize};
763
764 #############################################################################
765
766 $DEBUG_STATUS = new CFClient::UI::Label padding => 0, z => 100;
767 $DEBUG_STATUS->show;
768
769 $STATUS_LINE = new CFClient::UI::Label
770 padding => 0,
771 y => $HEIGHT - $FONTSIZE * 1.8;
772 $STATUS_LINE->show;
773
774 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
775 padding => 0,
776 fontsize => 0.8,
777 markup => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
778 $ALT_ENTER_MESSAGE->show;
779 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h});
780
781 $CFClient::UI::ROOT->add ($MAPWIDGET = new CFClient::MapWidget);
782 $MAPWIDGET->focus_in;
783 $MAPWIDGET->connect (activate_console => sub {
784 my ($mapwidget, $preset) = @_;
785
786 if ($CONSOLE) {
787 $CONSOLE->{input}->{auto_activated} = 1;
788 $CONSOLE->{input}->focus_in;
789
790 if ($preset && $CONSOLE->{input}->get_text eq '') {
791 $CONSOLE->{input}->set_text ($preset);
792 }
793 }
794 });
795
796 $CFClient::UI::ROOT->add ($BUTTONBAR = new CFClient::UI::HBox);
797
798 $BUTTONBAR->add (new CFClient::UI::Flopper text => "Client Setup", other => client_setup);
799 $BUTTONBAR->add (new CFClient::UI::Flopper text => "Server Setup", other => server_setup);
800 $BUTTONBAR->add (new CFClient::UI::Flopper text => "Message Window", other => message_window);
801
802 $CFClient::UI::ROOT->add (make_gauge_window); # XXX: this has to be set before make_stats_window as make_stats_window calls update_stats_window which updated the gauges also X-D
803 $BUTTONBAR->add (new CFClient::UI::Flopper text => "Stats Window", other => make_stats_window);
804
805 $BUTTONBAR->add (new CFClient::UI::Button text => "Save Config", connect_activate => sub {
806 CFClient::write_cfg "$Crossfire::VARDIR/pclientrc";
807 status "Configuration Saved";
808 });
809
810 $BUTTONBAR->{children}[1]->emit ("activate"); # pop up server setup
811}
812
813sub video_shutdown {
814 $CFClient::UI::ROOT->{children} = [];
815 undef $SDL_ACTIVE;
816}
817
818my @bgmusic = qw(game1.ogg game2.ogg game3.ogg game5.ogg game6.ogg ross1.ogg ross2.ogg ross3.ogg ross4.ogg ross5.ogg); #d#
819my $bgmusic;#TODO#hack#d#
820
821sub audio_music_finished {
822 return unless $CFG->{bgm_enable};
823
824 # TODO: hack, do play loop and mood music
825 $bgmusic = new_from_file CFClient::MixMusic CFClient::find_rcfile "music/$bgmusic[0]";
826 $bgmusic->play (0);
827
828 push @bgmusic, shift @bgmusic;
829}
830
831sub audio_init {
832 if ($CFG->{audio_enable}) {
833 if (open my $fh, "<:utf8", CFClient::find_rcfile "sounds/config") {
834 $SDL_MIXER = !CFClient::Mix_OpenAudio;
835 CFClient::Mix_AllocateChannels 8;
836 CFClient::MixMusic::volume $CFG->{bgm_volume} * 128;
837
838 audio_music_finished;
839
840 while (<$fh>) {
841 next if /^\s*#/;
842 next if /^\s*$/;
843
844 my ($file, $volume, $event) = split /\s+/, $_, 3;
845
846 push @SOUNDS, "$volume,$file";
847
848 $AUDIO_CHUNKS{"$volume,$file"} ||= do {
849 my $chunk = new_from_file CFClient::MixChunk CFClient::find_rcfile "sounds/$file";
850 $chunk->volume ($volume * 128 / 100);
851 $chunk
221 0 852 };
222 } 853 }
223 } else { 854 } else {
224 undef $refresh_handler; 855 status "unable to open sound config: $!";
225 0
226 } 856 }
227 }; 857 }
858}
859
860sub audio_shutdown {
861 CFClient::Mix_CloseAudio if $SDL_MIXER;
862 undef $SDL_MIXER;
863 @SOUNDS = ();
864 %AUDIO_CHUNKS = ();
865}
866
867my %animate_object;
868my $animate_timer;
869
870my $want_refresh;
871my $can_refresh;
872
873my $fps = 9;
874
875sub force_refresh {
876 $fps = $fps * 0.95 + 1 / ($NOW - $LAST_REFRESH) * 0.05;
877 debug sprintf "%3.2f", $fps;
878
879 $want_refresh = 0;
880 $can_refresh = 0;
881
882 $CFClient::UI::ROOT->draw;
883
884 CFClient::SDL_GL_SwapBuffers;
885
886 $LAST_REFRESH = $NOW;
887}
888
889my $refresh_watcher = Event->timer (after => 0, hard => 1, interval => 1 / $MAX_FPS, cb => sub {
890 $NOW = time;
891
892 ($SDL_CB{$_->{type}} || sub { warn "unhandled event $_->{type}" })->($_)
893 for CFClient::SDL_PollEvent;
894
895 if (%animate_object) {
896 $_->animate ($LAST_REFRESH - $NOW) for values %animate_object;
897 $want_refresh++;
898 }
899
900 if ($want_refresh) {
901 force_refresh;
902 } else {
903 $can_refresh = 1;
904 }
905});
906
907sub refresh {
908 $want_refresh++;
228} 909}
229 910
230sub animation_start { 911sub animation_start {
231 my ($widget) = @_; 912 my ($widget) = @_;
232 $ANIMATE{$widget} = $widget; 913 $animate_object{$widget} = $widget;
233 Scalar::Util::weaken $ANIMATE{$widget};
234
235 refresh;
236} 914}
237 915
238sub animation_stop { 916sub animation_stop {
239 my ($widget) = @_; 917 my ($widget) = @_;
240 delete $ANIMATE{$widget}; 918 delete $animate_object{$widget};
919}
920
921@conn::ISA = Crossfire::Protocol::;
922
923sub conn::stats_update {
924 my ($self, $stats) = @_;
925
926 update_stats_window ($stats);
927}
928
929sub conn::user_send {
930 my ($self, $command) = @_;
931
932 $self->send_command ($command);
933 status $command;
934}
935
936sub conn::map_scroll {
937 my ($self, $dx, $dy) = @_;
938
939 $MAP->scroll ($dx, $dy);
940}
941
942sub conn::feed_map1a {
943 my ($self, $data) = @_;
944
945# $self->Crossfire::Protocol::feed_map1a ($data);
946
947 $MAP->map1a_update ($data);
948 $MAPWIDGET->update;
949}
950
951sub conn::flush_map {
952 my ($self) = @_;
953
954 my $map_info = delete $self->{map_info}
955 or return;
956
957 my ($hash, $x, $y, $w, $h) = @$map_info;
958
959 my $data = $MAP->get_rect ($x, $y, $w, $h);
960 $MAPCACHE->put ($hash => Compress::LZF::compress $data);
961 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
962}
963
964sub conn::map_clear {
965 my ($self) = @_;
966
967 $self->flush_map;
968 delete $self->{neigh_map};
969
970 $MAP->clear;
971}
972
973
974sub conn::load_map($$$) {
975 my ($self, $hash, $x, $y) = @_;
976
977 if (defined (my $data = $MAPCACHE->get ($hash))) {
978 $data = Compress::LZF::decompress $data;
979 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
980 for my $id ($MAP->set_rect ($x, $y, $data)) {
981 my $data = $TILECACHE->get ($id)
982 or next;
983
984 $self->set_texture ($id => $data);
985 }
986 }
987}
988
989# this method does a "flood fill" into every tile direction
990# it assumes that tiles are arranged in a rectangular grid,
991# i.e. a map is the same as the left of the right map etc.
992# failure to comply are harmless and result in display errors
993# at worst.
994sub conn::flood_fill {
995 my ($self, $gx, $gy, $path, $hash, $flags) = @_;
996
997 # the server does not allow map paths > 6
998 return if 6 <= length $path;
999
1000 my ($x0, $y0, $x1, $y1) = @{$self->{neigh_rect}};
1001
1002 for (
1003 [1, 0, -1],
1004 [2, 1, 0],
1005 [3, 0, 1],
1006 [4, -1, 0],
1007 ) {
1008 my ($tile, $dx, $dy) = @$_;
1009
1010 my $gx = $gx + $dx;
1011 my $gy = $gy + $dy;
1012
1013 next unless $flags & (1 << ($tile - 1));
1014 next if $self->{neigh_grid}{$gx, $gy}++;
1015
1016 my $neigh = $self->{neigh_map}{$hash} ||= [];
1017 if (my $info = $neigh->[$tile]) {
1018 my ($flags, $x, $y, $w, $h, $hash) = @$info;
1019
1020 $self->flood_fill ($gx, $gy, "$path$tile", $hash, $flags)
1021 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
1022
1023 } else {
1024 $self->send_mapinfo ("spatial $path$tile", sub {
1025 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
1026
1027 return if $mode ne "spatial";
1028
1029 $x += $MAP->ox;
1030 $y += $MAP->oy;
1031
1032 $self->load_map ($hash, $x, $y)
1033 unless $self->{neigh_map}{$hash}[5]++;#d#
1034
1035 $neigh->[$tile] = [$flags, $x, $y, $w, $h, $hash];
1036
1037 $self->flood_fill ($gx, $gy, "$path$tile", $hash, $flags)
1038 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
1039 });
1040 }
1041 }
1042}
1043
1044sub conn::map_change {
1045 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
1046
1047 $self->flush_map;
1048
1049 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
1050
1051 my $mapmapw = 250;
1052 my $mapmaph = 250;
1053
1054 $self->{neigh_rect} = [
1055 $ox - $mapmapw * 0.5, $oy - $mapmapw * 0.5,
1056 $ox + $mapmapw * 0.5 + $w, $oy + $mapmapw * 0.5 + $h,
1057 ];
1058
1059 delete $self->{neigh_grid};
1060 $self->flood_fill (0, 0, "", $hash, $flags);
1061
1062 $x += $ox;
1063 $y += $oy;
1064
1065 $self->{map_info} = [$hash, $x, $y, $w, $h];
1066
1067 my $map = $self->{map_info}[0];
1068 $map =~ s/^.*?\/([^\/]+)$/\1/;
1069 $STATWIDS->{map}->set_text ("Map: " . $map);
1070
1071 $self->load_map ($hash, $x, $y);
1072}
1073
1074sub conn::face_find {
1075 my ($self, $facenum, $face) = @_;
1076
1077 my $hash = "$face->{chksum},$face->{name}";
1078
1079 my $id = $FACEMAP->get ($hash);
1080
1081 unless ($id) {
1082 # create new id for face
1083 # i love transactions
1084 for (1..100) {
1085 my $txn = $CFClient::DB_ENV->txn_begin;
1086 my $status = $FACEMAP->db_get (id => $id, BerkeleyDB::DB_RMW);
1087 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
1088 $id++;
1089 if ($FACEMAP->put (id => $id) == 0
1090 && $FACEMAP->put ($hash => $id) == 0) {
1091 $txn->txn_commit;
1092
1093 goto gotid;
1094 }
1095 }
1096 $txn->abort;
1097 }
1098
1099 CFClient::fatal "maximum number of transaction retries reached - database problems?";
1100 }
1101
1102gotid:
1103 $face->{id} = $id;
1104 $MAP->set_face ($facenum => $id);
1105 $self->{faceid}[$facenum] = $id;#d#
1106 $TILECACHE->get ($id)
1107}
1108
1109sub conn::face_update {
1110 my ($self, $facenum, $face) = @_;
1111
1112 $TILECACHE->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes
1113
1114 $self->set_texture ($face->{id} => delete $face->{image});
1115}
1116
1117sub conn::set_texture {
1118 my ($self, $id, $data) = @_;
1119
1120 $self->{texture}[$id] ||= do {
1121 my $tex =
1122 new_from_image CFClient::Texture
1123 $data, minify => 1, mipmap => 1;
1124
1125 $MAP->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
1126 $MAPWIDGET->update;
1127
1128 $tex
1129 };
1130}
1131
1132sub conn::sound_play {
1133 my ($self, $x, $y, $soundnum, $type) = @_;
1134
1135 $SDL_MIXER
1136 or return;
1137
1138 my $chunk = $AUDIO_CHUNKS{$SOUNDS[$soundnum]}
1139 or return;
1140
1141 $chunk->play;
1142# warn "sound $x,$y,$soundnum,$type\n";#d#
1143}
1144
1145my $LAST_QUERY; # server is stupid, stupid, stupid
1146
1147sub conn::query {
1148 my ($self, $flags, $prompt) = @_;
1149
1150 $prompt = $LAST_QUERY unless length $prompt;
1151 $LAST_QUERY = $prompt;
1152
1153 my $dialog = new CFClient::UI::FancyFrame
1154 title => "Query",
1155 child => my $vbox = new CFClient::UI::VBox;
1156
1157 $vbox->add (new CFClient::UI::Label
1158 max_w => $::WIDTH * 0.4,
1159 text => $prompt);
1160
1161 if ($flags & Crossfire::Protocol::CS_QUERY_YESNO) {
1162 $vbox->add (my $hbox = new CFClient::HBox);
1163 $hbox->add (new CFClient::Button
1164 text => "No",
1165 connect_activate => sub {
1166 $self->send ("reply n");
1167 $dialog->destroy;
1168 $MAPWIDGET->focus_in;
1169 }
1170 );
1171 $hbox->add (new CFClient::Button
1172 text => "Yes",
1173 connect_activate => sub {
1174 $self->send ("reply y");
1175 $dialog->destroy;
1176 $MAPWIDGET->focus_in;
1177 },
1178 );
1179
1180 $dialog->focus_in;
1181
1182 } elsif ($flags & Crossfire::Protocol::CS_QUERY_SINGLECHAR) {
1183 $dialog->{tooltip} = "Press a key (click on the entry to make sure it has keyboard focus)";
1184 $vbox->add (my $entry = new CFClient::UI::Entry
1185 connect_changed => sub {
1186 $self->send ("reply $_[1]");
1187 $dialog->destroy;
1188 $MAPWIDGET->focus_in;
1189 },
1190 );
1191
1192 $entry->focus_in;
1193
1194 } else {
1195 $dialog->{tooltip} = "Enter the reply and press return (click on the entry to make sure it has keyboard focus)";
1196
1197 $vbox->add (my $entry = new CFClient::UI::Entry
1198 $flags & Crossfire::Protocol::CS_QUERY_HIDEINPUT ? (hiddenchar => "*") : (),
1199 connect_activate => sub {
1200 $self->send ("reply $_[1]");
1201 $dialog->destroy;
1202 $MAPWIDGET->focus_in;
1203 },
1204 );
1205
1206 $entry->focus_in;
1207 }
1208
1209 $dialog->show;
1210}
1211
1212sub conn::drawinfo {
1213 my ($self, $color, $text) = @_;
1214
1215 my @color = (
1216 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
1217 [1.00, 1.00, 1.00],
1218 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
1219 [1.00, 0.00, 0.00],
1220 [1.00, 0.54, 0.00],
1221 [0.11, 0.56, 1.00],
1222 [0.93, 0.46, 0.00],
1223 [0.18, 0.54, 0.34],
1224 [0.56, 0.73, 0.56],
1225 [0.80, 0.80, 0.80],
1226 [0.55, 0.41, 0.13],
1227 [0.99, 0.77, 0.26],
1228 [0.74, 0.65, 0.41],
1229 );
1230
1231 $LOGVIEW->add_paragraph ($color[$color], $text);
1232}
1233
1234sub conn::spell_add {
1235 my ($self, $spell) = @_;
1236
1237 # TODO
1238 # create a widget dynamically, using spell face (CF::Protocol downloads them)
1239 $MAPWIDGET->add_command ("invoke $spell->{name}", $spell->{message});
1240 $MAPWIDGET->add_command ("cast $spell->{name}", $spell->{message});
1241}
1242
1243sub conn::spell_delete {
1244 my ($self, $spell) = @_;
1245}
1246
1247sub conn::addme_success {
1248 my ($self) = @_;
1249
1250 for my $skill (values %{$self->{skill_info}}) {
1251 $MAPWIDGET->add_command ("ready_skill $skill", "Ready the skill '$skill'");
1252 $MAPWIDGET->add_command ("use_skill $skill", "Immediately use the skill '$skill'");
1253 }
1254}
1255
1256sub update_floorbox {
1257 $CFClient::UI::ROOT->on_refresh ($FLOORBOX => sub {
1258 $FLOORBOX->clear;
1259 $FLOORBOX->add (new CFClient::UI::Empty expand => 1);
1260
1261 my @items = values %{ $CONN->{container}{0} };
1262
1263 # we basically have to use the same sorting as everybody else
1264 @items = sort { $a->{type} <=> $b->{type} } @items;
1265
1266 for my $item (reverse @items) {
1267 my $desc = $item->{nrof} < 2
1268 ? $item->{name}
1269 : "$item->{nrof} $item->{name_pl}";
1270 # todo: animation widget, face widget, weight(?) etc.
1271 $FLOORBOX->add (my $hbox = new CFClient::UI::HBox
1272 tooltip => (CFClient::UI::Label->escape ($desc)
1273 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
1274 can_hover => 1,
1275 can_events => 1,
1276 );
1277
1278 $hbox->add (new CFClient::UI::Face
1279 face => $item->{face},
1280 anim => $item->{anim},
1281 animspeed => $item->{animspeed},
1282 );
1283
1284 $hbox->add (new CFClient::UI::Label
1285 text => $desc,
1286 );
1287 }
1288 });
1289 refresh;
1290}
1291
1292sub conn::container_add {
1293 my ($self, $id, $items) = @_;
1294
1295 update_floorbox if $id == 0;
1296 # $self-<{player}{tag} => player inv
1297 #use PApp::Util; warn PApp::Util::dumpval $self->{container}{$self->{player}{tag}};
1298}
1299
1300sub conn::container_clear {
1301 my ($self, $id) = @_;
1302
1303 update_floorbox if $id == 0;
1304# use PApp::Util; warn PApp::Util::dumpval $self->{container}{0};
1305}
1306
1307sub conn::item_delete {
1308 my ($self, @items) = @_;
1309
1310 for (@items) {
1311 update_floorbox if $_->{container} == 0;
1312 }
1313}
1314
1315sub conn::item_update {
1316 my ($self, $item) = @_;
1317
1318 update_floorbox if $item->{container} == 0;
241} 1319}
242 1320
243%SDL_CB = ( 1321%SDL_CB = (
244 SDL_QUIT() => sub { 1322 CFClient::SDL_QUIT => sub {
245 main_quit Gtk2; 1323 Event::unloop -1;
246 }, 1324 },
247 SDL_VIDEORESIZE() => sub { 1325 CFClient::SDL_VIDEORESIZE => sub {
248 }, 1326 },
249 SDL_VIDEOEXPOSE() => sub { 1327 CFClient::SDL_VIDEOEXPOSE => \&refresh,
250 refresh;
251 },
252 SDL_KEYDOWN() => sub {
253 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
254 # alt-enter
255 $FULLSCREEN = !$FULLSCREEN;
256 init_screen;
257 } else {
258 CFClient::UI::feed_sdl_key_down_event ($SDL_EV);
259 }
260 },
261 SDL_KEYUP() => sub {
262 CFClient::UI::feed_sdl_key_up_event ($SDL_EV);
263 },
264 SDL_MOUSEMOTION() => sub {
265 CFClient::UI::feed_sdl_motion_event ($SDL_EV);
266 },
267 SDL_MOUSEBUTTONDOWN() => sub {
268 CFClient::UI::feed_sdl_button_down_event ($SDL_EV);
269 },
270 SDL_MOUSEBUTTONUP() => sub {
271 CFClient::UI::feed_sdl_button_up_event ($SDL_EV);
272 },
273 SDL_ACTIVEEVENT() => sub { 1328 CFClient::SDL_ACTIVEEVENT => sub {
274# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# 1329# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
275 }, 1330 },
1331 CFClient::SDL_KEYDOWN => sub {
1332 if ($_[0]{mod} & CFClient::KMOD_ALT && $_[0]{sym} == 13) {
1333 # alt-enter
1334 video_shutdown;
1335 $CFG->{fullscreen} = !$CFG->{fullscreen};
1336 video_init;
1337 } else {
1338 CFClient::UI::feed_sdl_key_down_event ($_[0]);
1339 }
1340 },
1341 CFClient::SDL_KEYUP => \&CFClient::UI::feed_sdl_key_up_event,
1342 CFClient::SDL_MOUSEMOTION => \&CFClient::UI::feed_sdl_motion_event,
1343 CFClient::SDL_MOUSEBUTTONDOWN => \&CFClient::UI::feed_sdl_button_down_event,
1344 CFClient::SDL_MOUSEBUTTONUP => \&CFClient::UI::feed_sdl_button_up_event,
1345 CFClient::SDL_USEREVENT => \&audio_music_finished,
276); 1346);
277 1347
278@conn::ISA = Crossfire::Protocol::;
279
280sub conn::map_update {
281 my ($self, $dirty) = @_;
282
283 $MAPWIDGET->update;
284}
285
286sub conn::map_scroll {
287 my ($self, $dx, $dy) = @_;
288
289# refresh;
290}
291
292sub conn::map_clear {
293 my ($self) = @_;
294
295# refresh;
296}
297
298sub conn::face_find {
299 my ($self, $face) = @_;
300
301 $FACECACHE->{"$face->{chksum},$face->{name}"}
302}
303
304sub conn::face_update {
305 my ($self, $face) = @_;
306
307 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
308
309 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
310}
311
312sub conn::query {
313 my ($self, $flags, $prompt) = @_;
314
315 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
316}
317
318sub gtk_add_cfg_field {
319 my ($tbl, $cfg, $klbl, $key, $value) = @_;
320 my $i = $cfg->{_i}++;
321 $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
322 $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
323 if ($key eq 'password') {
324 $ent->set_invisible_char ("*");
325 $ent->set (visibility => 0)
326 }
327 $ent->set_text ($value);
328 $ent->signal_connect (changed => sub {
329 my ($ent) = @_;
330 $cfg->{$key} = $ent->get_text;
331 # TODO: Mapsize should be a slider in the game gui
332 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
333 $cfg->{$key} = 100;
334 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
335 $cfg->{$key} = 50;
336 }
337 });
338}
339
340sub run_config_dialog {
341 my (%events) = @_;
342
343 my $w = Gtk2::Window->new;
344
345 my @cfg = (
346 [qw/Host host/],
347 [qw/Port port/],
348 [qw/Mapsize% mapsize/],
349 [qw/Username user/],
350 [qw/Password password/],
351 );
352
353 my $cfg = {};
354
355 my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE);
356 my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a;
357
358 $w->add (my $vb = Gtk2::VBox->new);
359 $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
360 my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
361 $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
362 $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
363 my $i = 0;
364 my $act = 0;
365 for (map { "$_->[0]x$_->[1]" } reverse @modes) {
366 if ($_ eq $selmode) { $act = $i }
367 $cb->append_text ($_);
368 $i++;
369 }
370 $cb->set_active ($act);
371 $cb->signal_connect (changed => sub {
372 my ($cb) = @_;
373 my $txt = $cb->get_active_text;
374 if ($txt =~ m/(\d+)x(\d+)/) {
375 $::CFG->{width} = $1;
376 $::CFG->{height} = $2;
377 }
378 });
379
380 $cfg->{_i} = 1;
381 for (@cfg) {
382 gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
383 }
384
385 $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
386 $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
387 $cb->signal_connect (clicked => sub {
388 for (keys %$cfg) {
389 $::CFG->{$_} = $cfg->{$_}
390 if $_ ne '_i';
391 }
392 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
393 });
394 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
395 $cb->signal_connect (clicked => sub {
396 for (keys %$cfg) {
397 $::CFG->{$_} = $cfg->{$_}
398 if $_ ne '_i';
399 }
400 my $cb = $events{login} || sub {};
401 $cb->($::CFG->{user}, $::CFG->{password});
402 });
403 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
404 $cb->signal_connect (clicked => sub {
405 my $cb = $events{logout} || sub {};
406 $cb->();
407 });
408 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
409 $cb->signal_connect (clicked => sub { $w->destroy });
410
411 $w->show_all;
412
413 $w->signal_connect (destroy => sub { Gtk2->main_quit });
414}
415
416
417############################################################################# 1348#############################################################################
418 1349
419SDL::Init SDL_INIT_EVERYTHING; 1350$SIG{INT} = $SIG{TERM} = sub { exit };
1351
1352$TILECACHE = CFClient::db_table "tilecache";
1353$FACEMAP = CFClient::db_table "facemap";
420 1354
421CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; 1355CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
422 1356
423$CFG ||= { 1357my %DEF_CFG = (
1358 sdl_mode => 0,
424 width => 640, 1359 width => 640,
425 height => 480, 1360 height => 480,
1361 fullscreen => 0,
1362 fast => 0,
1363 map_scale => 0.5,
1364 fow_enable => 1,
1365 fow_intensity => 0.45,
1366 fow_smooth => 0,
1367 gui_fontsize => 1,
1368 log_fontsize => 1,
1369 gauge_fontsize => 1,
1370 gauge_size => 0.35,
1371 stat_fontsize => 1,
426 mapsize => 100, 1372 mapsize => 100,
427 fullscreen => 0,
428 host => "crossfire.schmorp.de", 1373 host => "crossfire.schmorp.de",
429 port => 13327, 1374 say_command => 'say',
430}; 1375 audio_enable => 1,
1376 bgm_enable => 1,
1377 bgm_volume => 0.25,
1378);
1379
1380while (my ($k, $v) = each %DEF_CFG) {
1381 $CFG->{$k} = $v unless exists $CFG->{$k};
1382}
1383
1384sdl_init;
1385
1386@SDL_MODES = reverse
1387 grep $_->[0] >= 640 && $_->[1] >= 480,
1388 CFClient::SDL_ListModes;
1389
1390@SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)";
1391
1392$CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES;
431 1393
432{ 1394{
433 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); 1395 my @fonts = map CFClient::find_rcfile "fonts/$_", qw(
1396 DejaVuSans.ttf
1397 DejaVuSansMono.ttf
1398 DejaVuSans-Bold.ttf
1399 DejaVuSansMono-Bold.ttf
1400 DejaVuSans-Oblique.ttf
1401 DejaVuSansMono-Oblique.ttf
1402 DejaVuSans-BoldOblique.ttf
1403 DejaVuSansMono-BoldOblique.ttf
1404 );
434 1405
435 CFClient::add_font $_ for @fonts; 1406 CFClient::add_font $_ for @fonts;
436 CFClient::set_font $fonts[0]; 1407
437} 1408 $FONT_PROP = new_from_file CFClient::Font $fonts[0];
1409 $FONT_FIXED = new_from_file CFClient::Font $fonts[1];
438 1410
439$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 1411 $FONT_PROP->make_default;
1412}
440 1413
441run_config_dialog 1414video_init;
442 login => sub { start_game }, 1415audio_init;
443 logout => sub { stop_game };
444 1416
445main Gtk2; 1417Event::loop;
446 1418
447Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; 1419END { CFClient::SDL_Quit }
1420
1421

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines