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.85 by root, Thu Jun 1 05:04:34 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;
243} 316}
244 317
245sub do_n_dialog { 318sub do_n_dialog {
246 my ($cb) = @_; 319 my ($cb) = @_;
247 320
248 my $w = new CFClient::UI::FancyFrame; 321 my $w = new CFClient::UI::FancyFrame
322 on_delete => sub { $_[0]->destroy; 1 },
323 has_close_button => 1,
324 ;
325
249 $w->add (my $vb = new CFClient::UI::VBox x => "center", y => "center"); 326 $w->add (my $vb = new CFClient::UI::VBox x => "center", y => "center");
250 $vb->add (new CFClient::UI::Label text => "Enter item count:"); 327 $vb->add (new CFClient::UI::Label text => "Enter item count:");
251 $vb->add (my $entry = new CFClient::UI::Entry 328 $vb->add (my $entry = new CFClient::UI::Entry
252 text => $last_enter_count, 329 text => $last_enter_count,
253 on_activate => sub { 330 on_activate => sub {
254 my ($entry) = @_; 331 my ($entry) = @_;
255 $last_enter_count = $entry->get_text; 332 $last_enter_count = $entry->get_text;
256 $cb->($last_enter_count); 333 $cb->($last_enter_count);
257 $w->hide; 334 $w->hide;
258 $w = undef; 335 $w->destroy;
336
337 0
259 } 338 },
339 on_escape => sub { $w->destroy; 1 },
260 ); 340 );
261 $entry->focus_in; 341 $entry->grab_focus;
262 $w->show; 342 $w->show;
263
264} 343}
265 344
266sub update_widgets { 345sub update_widgets {
267 my ($self) = @_; 346 my ($self) = @_;
347
348 # necessary to avoid cyclic references
349 Scalar::Util::weaken $self;
268 350
269 my $button_cb = sub { 351 my $button_cb = sub {
270 my (undef, $ev, $x, $y) = @_; 352 my (undef, $ev, $x, $y) = @_;
271 353
272 my $targ = $::CONN->{player}{tag}; 354 my $targ = $::CONN->{player}{tag};
276 } 358 }
277 359
278 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) { 360 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
279 $::CONN->send ("move $targ $self->{tag} 0") 361 $::CONN->send ("move $targ $self->{tag} 0")
280 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})
281 } elsif ($ev->{button} == 1) { 367 } elsif ($ev->{button} == 1) {
282 $::CONN->send ("examine $self->{tag}"); 368 $::CONN->send ("examine $self->{tag}");
283 } elsif ($ev->{button} == 2) { 369 } elsif ($ev->{button} == 2) {
284 $::CONN->send ("apply $self->{tag}"); 370 $::CONN->send ("apply $self->{tag}");
285 } 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
286 my @menu_items = ( 377 my @menu_items = (
287 ["examine", sub { $::CONN->send ("examine $self->{tag}") }], 378 ["examine", sub { $::CONN->send ("examine $self->{tag}") }],
288 ["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 ],
289 ["apply", sub { $::CONN->send ("apply $self->{tag}") }], 386 ["apply", sub { $::CONN->send ("apply $self->{tag}") }],
290 ( 387 (
291 $self->{flags} & F_LOCKED 388 $self->{flags} & F_LOCKED
292 ? ( 389 ? (
293 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }], 390 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }],
294 ) 391 )
295 : ( 392 : (
296 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }], 393 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }],
297 ["drop", sub { $::CONN->send ("move $::CONN->{open_container} $self->{tag} 0") }], 394 ["$move_prefix all", sub { $::CONN->send ("move $targ $self->{tag} 0") }],
298 ["move n", 395 ["$move_prefix <n>",
299 sub { 396 sub {
300 do_n_dialog (sub { $::CONN->send ("move $targ $self->{tag} $_[0]") }) 397 do_n_dialog (sub { $::CONN->send ("move $targ $self->{tag} $_[0]") })
301 } 398 }
302 ] 399 ]
303 ) 400 )
312 409
313 my $tooltip_std = "<small>" 410 my $tooltip_std = "<small>"
314 . "Left click - examine item\n" 411 . "Left click - examine item\n"
315 . "Shift-Left click - " . ($self->{container} ? "move or drop" : "take") . " item\n" 412 . "Shift-Left click - " . ($self->{container} ? "move or drop" : "take") . " item\n"
316 . "Middle click - apply\n" 413 . "Middle click - apply\n"
414 . "Shift-Middle click - lock/unlock\n"
317 . "Right click - further options" 415 . "Right click - further options"
318 . "</small>\n"; 416 . "</small>\n";
319 417
320 $self->{face_widget} ||= new CFClient::UI::Face 418 $self->{face_widget} ||= new CFClient::UI::Face
321 can_events => 1, 419 can_events => 1,
360 . ($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. ")
361 . "\n\n$tooltip_std" 459 . "\n\n$tooltip_std"
362 ); 460 );
363} 461}
364 462
365package CFClient::Binder;
366
367my @ALLOWED_MODIFIER_KEYS = (
368 CFClient::SDLK_LSHIFT,
369 CFClient::SDLK_LCTRL ,
370 CFClient::SDLK_LALT ,
371 CFClient::SDLK_LMETA ,
372
373 CFClient::SDLK_RSHIFT,
374 CFClient::SDLK_RCTRL ,
375 CFClient::SDLK_RALT ,
376 CFClient::SDLK_RMETA ,
377);
378
379my %ALLOWED_MODIFIERS = (
380 CFClient::KMOD_LSHIFT => "LSHIFT",
381 CFClient::KMOD_LCTRL => "LCTRL",
382 CFClient::KMOD_LALT => "LALT",
383 CFClient::KMOD_LMETA => "LMETA",
384
385 CFClient::KMOD_RSHIFT => "RSHIFT",
386 CFClient::KMOD_RCTRL => "RCTRL",
387 CFClient::KMOD_RALT => "RALT",
388 CFClient::KMOD_RMETA => "RMETA",
389);
390
391my %DIRECT_BIND_CHARS = map { $_ => 1 } qw/0 1 2 3 4 5 6 7 8 9/;
392my @DIRECT_BIND_KEYS = (
393 CFClient::SDLK_F1,
394 CFClient::SDLK_F2,
395 CFClient::SDLK_F3,
396 CFClient::SDLK_F4,
397 CFClient::SDLK_F5,
398 CFClient::SDLK_F6,
399 CFClient::SDLK_F7,
400 CFClient::SDLK_F8,
401 CFClient::SDLK_F9,
402 CFClient::SDLK_F10,
403 CFClient::SDLK_F11,
404 CFClient::SDLK_F12,
405 CFClient::SDLK_F13,
406 CFClient::SDLK_F14,
407 CFClient::SDLK_F15,
408);
409
410# this binding dialog asks for a key-combo to be pressed
411# and if successful it calls the $cb with $mod and $sym as args.
412sub open_binding_dialog {
413 my ($cb) = @_;
414
415 my $w = new CFClient::UI::FancyFrame
416 title => "Bind Action",
417 x => "center",
418 y => "center";
419
420 $w->add (my $vb = new CFClient::UI::VBox);
421 $vb->add (new CFClient::UI::Label
422 text => "Press a modifier (CTRL, ALT and/or SHIFT) and a key."
423 ."You can only bind 0-9 and F1-F15 without modifiers."
424 );
425 $vb->add (my $entry = new CFClient::UI::Entry
426 text => "",
427 on_key_down => sub {
428 my ($entry, $ev) = @_;
429
430 my $mod = $ev->{mod};
431 my $sym = $ev->{sym};
432
433 # XXX: This seems a little bit hackisch to me, but i have to ignore them
434 if (grep { $_ == $sym } @ALLOWED_MODIFIER_KEYS) {
435 return;
436 }
437
438 if ($mod == CFClient::KMOD_NONE
439 and not $DIRECT_BIND_CHARS{chr ($ev->{unicode})}
440 and not grep { $sym == $_ } @DIRECT_BIND_KEYS)
441 {
442 $::STATUSBOX->add (
443 "Can't bind key ".CFClient::SDL_GetKeyName ($sym)
444 ." directly without modifier! It would damage the completer handling."
445 );
446 return;
447 }
448
449 $entry->focus_out;
450
451 $cb->($mod, $sym);
452
453 $w->destroy
454 });
455
456 $entry->focus_in;
457 $w->show;
458}
459
460sub keycombo_to_name {
461 my ($mod, $sym) = @_;
462
463 my $mods = join '+',
464 map { $ALLOWED_MODIFIERS{$_} }
465 grep { ($_ + 0) & ($mod + 0) }
466 keys %ALLOWED_MODIFIERS;
467 $mods .= "+" if $mods ne '';
468
469 return $mods . CFClient::SDL_GetKeyName ($sym);
470}
471
4721; 4631;
473 464
474=back 465=back
475 466
476=head1 AUTHOR 467=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines