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.176 by root, Mon Apr 24 10:19:40 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines