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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines