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

Comparing deliantra/Deliantra-Client/DC.pm (file contents):
Revision 1.81 by elmex, Tue May 30 14:35:08 2006 UTC vs.
Revision 1.103 by root, Sun Jul 23 08:58:43 2006 UTC

24use utf8; 24use utf8;
25 25
26use Carp (); 26use Carp ();
27use AnyEvent (); 27use AnyEvent ();
28use BerkeleyDB; 28use BerkeleyDB;
29use Pod::POM ();
30use Scalar::Util ();
31use Storable (); # finally
32
33our %STAT_TOOLTIP = (
34 Str => "<b>Physical Strength</b>, determines damage dealt with weapons, how much you can carry, and how often you can attack",
35 Dex => "<b>Dexterity</b>, your physical agility. Determines chance of being hit and affects armor class and speed",
36 Con => "<b>Constitution</b>, physical health and toughness. Determines how many healthpoints you can have",
37 Int => "<b>Intelligence</b>, your ability to learn and use skills and incantations (both prayers and magic) and determines how much spell points you can have",
38 Wis => "<b>Wisdom</b>, the ability to learn and use divine magic (prayers). Determines how many grace points you can have",
39 Pow => "<b>Power</b>, your magical potential. Influences the strength of spell effects, and also how much your spell and grace points increase when leveling up",
40 Cha => "<b>Charisma</b>, how well you are received by NPCs. Affects buying and selling prices in shops.",
41
42 Wc => "<b>Weapon Class</b>, effectiveness of melee/missile attacks. Lower is more potent. Current weapon, level and Str are some things which effect the value of Wc. The value of Wc may range between 25 and -72.",
43 Ac => "<b>Armour Class</b>, how protected you are from being hit by any attack. Lower values are better. Ac is based on your race and is modified by the Dex and current armour worn. For characters that cannot wear armour, Ac improves as their level increases.",
44 Dam => "<b>Damage</b>, how much damage your melee/missile attack inflicts. Higher values indicate a greater amount of damage will be inflicted with each attack.",
45 Arm => "<b>Armour</b>, how much damage (from physical attacks) will be subtracted from successful hits made upon you. This value ranges between 0 to 99%. Current armour worn primarily determines Arm value. This is the same as the physical resistance.",
46 Spd => "<b>Speed</b>, how fast you can move. The value of speed may range between nearly 0 (\"very slow\") to higher than 5 (\"lightning fast\"). Base speed is determined from the Dex and modified downward proportionally by the amount of weight carried which exceeds the Max Carry limit. The armour worn also sets the upper limit on speed.",
47 WSp => "<b>Weapon Speed</b>, how many attacks you may make per unit of time (0.120s). Higher values indicate faster attack speed. Current weapon and Dex effect the value of weapon speed.",
48);
49
50=item guard { BLOCK }
51
52Returns an object that executes the given block as soon as it is destroyed.
53
54=cut
55
56sub guard(&) {
57 bless \(my $cb = $_[0]), "CFClient::Guard"
58}
59
60sub CFClient::Guard::DESTROY {
61 ${$_[0]}->()
62}
63
64package CFClient::PodToPango;
65
66use base Pod::POM::View::Text;
67
68our $VERSION = 1; # bump if resultant formatting changes
69
70our $indent = 0;
71
72*view_seq_code =
73*view_seq_bold = sub { "<b>$_[1]</b>" };
74*view_seq_italic = sub { "<i>$_[1]</i>" };
75*view_seq_space =
76*view_seq_link =
77*view_seq_index = sub { CFClient::UI::Label::escape ($_[1]) };
78
79sub view_seq_text {
80 my $text = $_[1];
81 $text =~ s/\s+/ /g;
82 CFClient::UI::Label::escape ($text)
83}
84
85sub view_item {
86 ("\t" x ($indent / 4))
87 . $_[1]->title->present ($_[0])
88 . "\n\n"
89 . $_[1]->content->present ($_[0])
90}
91
92sub view_verbatim {
93 (join "",
94 map +("\t" x ($indent / 2)) . "<tt>$_</tt>\n",
95 split /\n/, CFClient::UI::Label::escape ($_[1]))
96 . "\n"
97}
98
99sub view_textblock {
100 ("\t" x ($indent / 2)) . "$_[1]\n\n"
101}
102
103sub view_head1 {
104 "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
105 . $_[1]->content->present ($_[0])
106};
107
108sub view_head2 {
109 "\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
110 . $_[1]->content->present ($_[0])
111};
112
113sub view_head3 {
114 "\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
115 . $_[1]->content->present ($_[0])
116};
117
118sub view_over {
119 local $indent = $indent + $_[1]->indent;
120 $_[1]->content->present ($_[0])
121}
122
123package CFClient::Database;
124
125our @ISA = BerkeleyDB::Btree::;
126
127sub get($$) {
128 my $data;
129
130 $_[0]->db_get ($_[1], $data) == 0
131 ? $data
132 : ()
133}
134
135my %DB_SYNC;
136
137sub put($$$) {
138 my ($db, $key, $data) = @_;
139
140 $DB_SYNC{$db} = AnyEvent->timer (after => 5, cb => sub { $db->db_sync });
141
142 $db->db_put ($key => $data)
143}
144
145package CFClient;
29 146
30sub find_rcfile($) { 147sub find_rcfile($) {
31 my $path; 148 my $path;
32 149
33 for (grep !ref, @INC) { 150 for (grep !ref, @INC) {
68 } 185 }
69 186
70 close CFG; 187 close CFG;
71} 188}
72 189
73mkdir "$Crossfire::VARDIR/cfplus", 0777;
74
75our $DB_ENV; 190our $DB_ENV;
76 191
77{ 192{
78 use strict; 193 use strict;
79 194
195 mkdir "$Crossfire::VARDIR/cfplus", 0777;
80 my $recover = $BerkeleyDB::db_version >= 4.4 196 my $recover = $BerkeleyDB::db_version >= 4.4
81 ? eval "DB_REGISTER | DB_RECOVER" 197 ? eval "DB_REGISTER | DB_RECOVER"
82 : 0; 198 : 0;
83 199
84 $DB_ENV = new BerkeleyDB::Env 200 $DB_ENV = new BerkeleyDB::Env
105 -Property => DB_CHKSUM, 221 -Property => DB_CHKSUM,
106 -Flags => DB_CREATE | DB_UPGRADE, 222 -Flags => DB_CREATE | DB_UPGRADE,
107 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" 223 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
108} 224}
109 225
226my $pod_cache = db_table "pod_cache";
227
228sub load_pod($$$$) {
229 my ($path, $filtertype, $filterversion, $filtercb) = @_;
230
231 stat $path
232 or die "$path: $!";
233
234 my $phash = join ",", $filterversion, $CFClient::PodToPango::VERSION, (stat _)[7,9];
235
236 my ($chash, $pom) = eval { @{ Storable::thaw $pod_cache->get ("$path/$filtertype") } };
237
238 return $pom if $chash eq $phash;
239
240 my $pod = do {
241 local $/;
242 open my $pod, "<:utf8", $_[0]
243 or die "$_[0]: $!";
244 <$pod>
245 };
246
247 #utf8::downgrade $pod;
248
249 $pom = $filtercb-> (Pod::POM->new->parse_text ($pod));
250
251 $pod_cache->put ("$path/$filtertype" => Storable::nfreeze [$phash, $pom]);
252
253 $pom
254}
255
110sub pod_to_pango($) { 256sub pod_to_pango($) {
111 my ($pom) = @_; 257 my ($pom) = @_;
112 258
113 $pom->present ("CFClient::PodToPango") 259 $pom->present ("CFClient::PodToPango")
114} 260}
120 map s/^(\s*)// && [40 * length $1, length $_ ? $_ : " "], 266 map s/^(\s*)// && [40 * length $1, length $_ ? $_ : " "],
121 split /\n/, $pom->present ("CFClient::PodToPango") 267 split /\n/, $pom->present ("CFClient::PodToPango")
122 ] 268 ]
123} 269}
124 270
125package CFClient::PodToPango; 271package CFClient::Layout;
126 272
127use base Pod::POM::View::Text; 273$CFClient::OpenGL::SHUTDOWN_HOOK{"CFClient::Layout"} = sub {
128 274 reset_glyph_cache;
129our $indent = 0;
130
131*view_seq_code =
132*view_seq_bold = sub { "<b>$_[1]</b>" };
133*view_seq_italic = sub { "<i>$_[1]</i>" };
134*view_seq_space =
135*view_seq_link =
136*view_seq_index = sub { CFClient::UI::Label::escape ($_[1]) };
137
138sub view_seq_text {
139 my $text = $_[1];
140 $text =~ s/\s+/ /g;
141 CFClient::UI::Label::escape ($text)
142}
143
144sub view_item {
145 ("\t" x ($indent / 4))
146 . $_[1]->title->present ($_[0])
147 . "\n"
148 . $_[1]->content->present ($_[0])
149}
150
151sub view_verbatim {
152 (join "",
153 map +("\t" x ($indent / 2)) . "<tt>$_</tt>\n",
154 split /\n/, CFClient::UI::Label::escape ($_[1]))
155 . "\n"
156}
157
158sub view_textblock {
159 ("\t" x ($indent / 2)) . "$_[1]\n\n"
160}
161
162sub view_head1 {
163 "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
164 . $_[1]->content->present ($_[0])
165}; 275};
166
167sub view_head2 {
168 "\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
169 . $_[1]->content->present ($_[0])
170};
171
172sub view_head3 {
173 "\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
174 . $_[1]->content->present ($_[0])
175};
176
177sub view_over {
178 local $indent = $indent + $_[1]->indent;
179 $_[1]->content->present ($_[0])
180}
181
182package CFClient::Database;
183
184our @ISA = BerkeleyDB::Btree::;
185
186sub get($$) {
187 my $data;
188
189 $_[0]->db_get ($_[1], $data) == 0
190 ? $data
191 : ()
192}
193
194my %DB_SYNC;
195
196sub put($$$) {
197 my ($db, $key, $data) = @_;
198
199 $DB_SYNC{$db} = AnyEvent->timer (after => 5, cb => sub { $db->db_sync });
200
201 $db->db_put ($key => $data)
202}
203 276
204package CFClient::Item; 277package CFClient::Item;
205 278
206use strict; 279use strict;
207use Crossfire::Protocol::Constants; 280use Crossfire::Protocol::Constants;
281
282my $last_enter_count = 1;
208 283
209sub desc_string { 284sub desc_string {
210 my ($self) = @_; 285 my ($self) = @_;
211 286
212 my $desc = 287 my $desc =
238 my $weight = ($self->{nrof} || 1) * $self->{weight}; 313 my $weight = ($self->{nrof} || 1) * $self->{weight};
239 314
240 $weight < 0 ? "?" : $weight * 0.001 315 $weight < 0 ? "?" : $weight * 0.001
241} 316}
242 317
318sub do_n_dialog {
319 my ($cb) = @_;
320
321 my $w = new CFClient::UI::FancyFrame
322 on_delete => sub { $_[0]->destroy; 1 },
323 has_close_button => 1,
324 ;
325
326 $w->add (my $vb = new CFClient::UI::VBox x => "center", y => "center");
327 $vb->add (new CFClient::UI::Label text => "Enter item count:");
328 $vb->add (my $entry = new CFClient::UI::Entry
329 text => $last_enter_count,
330 on_activate => sub {
331 my ($entry) = @_;
332 $last_enter_count = $entry->get_text;
333 $cb->($last_enter_count);
334 $w->hide;
335 $w->destroy;
336
337 0
338 },
339 on_escape => sub { $w->destroy; 1 },
340 );
341 $entry->grab_focus;
342 $w->show;
343}
344
243sub update_widgets { 345sub update_widgets {
244 my ($self) = @_; 346 my ($self) = @_;
245 347
348 # necessary to avoid cyclic references
349 Scalar::Util::weaken $self;
350
246 my $button_cb = sub { 351 my $button_cb = sub {
247 my (undef, $ev, $x, $y) = @_; 352 my (undef, $ev, $x, $y) = @_;
248 353
354 my $targ = $::CONN->{player}{tag};
355
356 if ($self->{container} == $::CONN->{player}{tag}) {
357 $targ = $::CONN->{open_container};
358 }
359
249 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) { 360 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
250 my $targ = $::CONN->{player}{tag};
251
252 if ($self->{container} == $::CONN->{player}{tag}) {
253 $targ = $::CONN->{open_container};
254 }
255
256 $::CONN->send ("move $targ $self->{tag} 0") 361 $::CONN->send ("move $targ $self->{tag} 0")
257 if $targ || !($self->{flags} & F_LOCKED); 362 if $targ || !($self->{flags} & F_LOCKED);
363 } elsif (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 2) {
364 $self->{flags} & F_LOCKED
365 ? $::CONN->send ("lock " . pack "CN", 0, $self->{tag})
366 : $::CONN->send ("lock " . pack "CN", 1, $self->{tag})
258 } elsif ($ev->{button} == 1) { 367 } elsif ($ev->{button} == 1) {
259 $::CONN->send ("examine $self->{tag}"); 368 $::CONN->send ("examine $self->{tag}");
260 } elsif ($ev->{button} == 2) { 369 } elsif ($ev->{button} == 2) {
261 $::CONN->send ("apply $self->{tag}"); 370 $::CONN->send ("apply $self->{tag}");
262 } elsif ($ev->{button} == 3) { 371 } elsif ($ev->{button} == 3) {
372 my $move_prefix = $::CONN->{open_container} ? 'put' : 'drop';
373 if ($self->{container} == $::CONN->{open_container}) {
374 $move_prefix = "take";
375 }
376
263 my @menu_items = ( 377 my @menu_items = (
264 ["examine", sub { $::CONN->send ("examine $self->{tag}") }], 378 ["examine", sub { $::CONN->send ("examine $self->{tag}") }],
265 ["mark", sub { $::CONN->send ("mark ". pack "N", $self->{tag}) }], 379 ["mark", sub { $::CONN->send ("mark ". pack "N", $self->{tag}) }],
380 ["ignite/thaw", # first try of an easier use of flint&steel
381 sub {
382 $::CONN->send ("mark ". pack "N", $self->{tag});
383 $::CONN->send ("command apply flint and steel");
384 }
385 ],
266 ["apply", sub { $::CONN->send ("apply $self->{tag}") }], 386 ["apply", sub { $::CONN->send ("apply $self->{tag}") }],
267 ( 387 (
268 $self->{flags} & F_LOCKED 388 $self->{flags} & F_LOCKED
269 ? ( 389 ? (
270 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }], 390 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }],
271 ) 391 )
272 : ( 392 : (
273 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }], 393 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }],
274 ["drop", sub { $::CONN->send ("move $::CONN->{open_container} $self->{tag} 0") }], 394 ["$move_prefix all", sub { $::CONN->send ("move $targ $self->{tag} 0") }],
395 ["$move_prefix <n>",
396 sub {
397 do_n_dialog (sub { $::CONN->send ("move $targ $self->{tag} $_[0]") })
398 }
399 ]
275 ) 400 )
276 ), 401 ),
277 ); 402 );
278 403
279 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); 404 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
284 409
285 my $tooltip_std = "<small>" 410 my $tooltip_std = "<small>"
286 . "Left click - examine item\n" 411 . "Left click - examine item\n"
287 . "Shift-Left click - " . ($self->{container} ? "move or drop" : "take") . " item\n" 412 . "Shift-Left click - " . ($self->{container} ? "move or drop" : "take") . " item\n"
288 . "Middle click - apply\n" 413 . "Middle click - apply\n"
414 . "Shift-Middle click - lock/unlock\n"
289 . "Right click - further options" 415 . "Right click - further options"
290 . "</small>\n"; 416 . "</small>\n";
291 417
292 $self->{face_widget} ||= new CFClient::UI::Face 418 $self->{face_widget} ||= new CFClient::UI::Face
293 can_events => 1, 419 can_events => 1,
332 . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ") 458 . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ")
333 . "\n\n$tooltip_std" 459 . "\n\n$tooltip_std"
334 ); 460 );
335} 461}
336 462
337package CFClient::Binder;
338
339my @ALLOWED_MODIFIER_KEYS = (
340 (CFClient::SDLK_LSHIFT),
341 (CFClient::SDLK_LCTRL ),
342 (CFClient::SDLK_LALT ),
343 (CFClient::SDLK_LMETA ),
344
345 (CFClient::SDLK_RSHIFT),
346 (CFClient::SDLK_RCTRL ),
347 (CFClient::SDLK_RALT ),
348 (CFClient::SDLK_RMETA ),
349);
350
351my %ALLOWED_MODIFIERS = (
352 (CFClient::KMOD_LSHIFT) => "LSHIFT",
353 (CFClient::KMOD_LCTRL ) => "LCTRL",
354 (CFClient::KMOD_LALT ) => "LALT",
355 (CFClient::KMOD_LMETA ) => "LMETA",
356
357 (CFClient::KMOD_RSHIFT) => "RSHIFT",
358 (CFClient::KMOD_RCTRL ) => "RCTRL",
359 (CFClient::KMOD_RALT ) => "RALT",
360 (CFClient::KMOD_RMETA ) => "RMETA",
361);
362
363my %DIRECT_BIND_CHARS = map { $_ => 1 } qw/0 1 2 3 4 5 6 7 8 9/;
364my @DIRECT_BIND_KEYS = (
365 CFClient::SDLK_F1,
366 CFClient::SDLK_F2,
367 CFClient::SDLK_F3,
368 CFClient::SDLK_F4,
369 CFClient::SDLK_F5,
370 CFClient::SDLK_F6,
371 CFClient::SDLK_F7,
372 CFClient::SDLK_F8,
373 CFClient::SDLK_F9,
374 CFClient::SDLK_F10,
375 CFClient::SDLK_F11,
376 CFClient::SDLK_F12,
377 CFClient::SDLK_F13,
378 CFClient::SDLK_F14,
379 CFClient::SDLK_F15,
380);
381
382# this binding dialog asks for a key-combo to be pressed
383# and if successful it calls the $cb with $mod and $sym as args.
384sub open_binding_dialog {
385 my ($cb) = @_;
386
387 my $w = new CFClient::UI::FancyFrame
388 title => "Bind Action",
389 x => "center",
390 y => "center";
391
392 $w->add (my $vb = new CFClient::UI::VBox);
393 $vb->add (new CFClient::UI::Label
394 text => "Press a modifier (CTRL, ALT and/or SHIFT) and a key."
395 ."You can only bind 0-9 and F1-F15 without modifiers."
396 );
397 $vb->add (my $entry = new CFClient::UI::Entry
398 text => "",
399 on_key_down => sub {
400 my ($entry, $ev) = @_;
401
402 my $mod = $ev->{mod};
403 my $sym = $ev->{sym};
404
405 # XXX: This seems a little bit hackisch to me, but i have to ignore them
406 if (grep { $_ == $sym } @ALLOWED_MODIFIER_KEYS) {
407 return;
408 }
409
410 if ($mod == CFClient::KMOD_NONE
411 and not $DIRECT_BIND_CHARS{chr ($ev->{unicode})}
412 and not grep { $sym == $_ } @DIRECT_BIND_KEYS)
413 {
414 $::STATUSBOX->add (
415 "Can't bind key ".CFClient::SDL_GetKeyName ($sym)
416 ." directly without modifier! It would damage the completer handling."
417 );
418 return;
419 }
420
421 $entry->focus_out;
422
423 $cb->($mod, $sym);
424
425 $w->destroy
426 });
427
428 $entry->focus_in;
429 $w->show;
430}
431
432sub keycombo_to_name {
433 my ($mod, $sym) = @_;
434
435 my $mods = join '+',
436 map { $ALLOWED_MODIFIERS{$_} }
437 grep { ($_ + 0) & ($mod + 0) }
438 keys %ALLOWED_MODIFIERS;
439 $mods .= "+" if $mods ne '';
440
441 return $mods . CFClient::SDL_GetKeyName ($sym);
442}
443
4441; 4631;
445 464
446=back 465=back
447 466
448=head1 AUTHOR 467=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines