ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.77
Committed: Mon Aug 14 04:15:04 2006 UTC (17 years, 9 months ago) by root
Branch: MAIN
Changes since 1.76: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.69 package CFPlus::Protocol;
2 root 1.1
3     use utf8;
4     use strict;
5    
6 root 1.2 use Crossfire::Protocol::Constants;
7    
8 root 1.69 use CFPlus;
9     use CFPlus::UI;
10     use CFPlus::Pod;
11 root 1.1
12     use base 'Crossfire::Protocol::Base';
13    
14     sub new {
15     my $class = shift;
16    
17     my $self = $class->SUPER::new (@_);
18    
19     $self->{map_widget}->clr_commands;
20    
21 root 1.72 my @cmd_help = map {
22     $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x
23     or die "unparseable command help: $_->{kw}[0]";
24    
25     my $cmd = $1;
26     my @args = split /\|/, $2;
27     @args = (".*") unless @args;
28    
29     my (undef, @par) = CFPlus::Pod::section_of $_;
30     my $text = CFPlus::Pod::as_label @par;
31    
32     $_ = $_ eq ".*" ? "" : " $_"
33     for @args;
34    
35     map ["$cmd$_", $text],
36     sort { (length $a) <=> (length $b) }
37     @args
38 root 1.74 } sort { $a->{par} <=> $b->{par} }
39 root 1.77 CFPlus::Pod::find command => "*";
40 root 1.1
41 root 1.72 $self->{map_widget}->add_command (@$_)
42     for @cmd_help;
43 root 1.1
44 root 1.69 $self->{noface} = new_from_file CFPlus::Texture
45     CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1;
46 root 1.1
47     $self->{open_container} = 0;
48    
49     # "global"
50 root 1.69 $self->{tilecache} = CFPlus::db_table "tilecache";
51     $self->{facemap} = CFPlus::db_table "facemap";
52 root 1.1
53     # per server
54 root 1.69 $self->{mapcache} = CFPlus::db_table "mapcache_$self->{host}_$self->{port}";
55 root 1.1
56     $self
57     }
58    
59 elmex 1.34 sub logprint {
60     my ($self, @a) = @_;
61    
62 root 1.59 $self->{log_fh} ||= do {
63     my $path = "$Crossfire::VARDIR/log.$self->{host}";
64    
65     open my $fh, ">>:utf8", $path
66     or die "Couldn't open logfile $path: $!";
67 elmex 1.34
68 root 1.59 $fh->autoflush (1);
69    
70     $fh;
71     };
72 elmex 1.34
73 root 1.59 my ($sec, $min, $hour, $mday, $mon, $year) = localtime time;
74 elmex 1.34
75     my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
76     $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
77    
78 root 1.59 print {$self->{log_fh}} "$ts ", @a, "\n";
79 elmex 1.34 }
80    
81 root 1.52 sub _stat_numdiff {
82 root 1.54 my ($self, $name, $old, $new) = @_;
83 root 1.52
84     my $diff = $new - $old;
85 root 1.54
86     $diff = 0.01 * int $diff * 100;
87    
88     0.1 >= abs $diff ? ()
89     : $diff < 0 ? "$name$diff" : "$name+$diff"
90 root 1.52 }
91    
92     sub _stat_skillmaskdiff {
93 root 1.54 my ($self, $name, $old, $new) = @_;
94 root 1.52
95 root 1.54 my $diff = $old ^ $new
96     or return;
97 root 1.52
98 root 1.53 my @diff = map
99     {
100     $diff & $_
101     ? (($new & $_ ? "+" : "-") . $self->{spell_paths}{$_})
102     : ()
103     }
104     sort { $a <=> $b } keys %{$self->{spell_paths}};
105 root 1.52
106     join "", @diff
107     }
108    
109     # all stats that are chacked against changes
110     my @statchange = (
111     [&CS_STAT_STR => \&_stat_numdiff, "Str"],
112     [&CS_STAT_INT => \&_stat_numdiff, "Int"],
113     [&CS_STAT_WIS => \&_stat_numdiff, "Wis"],
114     [&CS_STAT_DEX => \&_stat_numdiff, "Dex"],
115     [&CS_STAT_CON => \&_stat_numdiff, "Con"],
116     [&CS_STAT_CHA => \&_stat_numdiff, "Cha"],
117     [&CS_STAT_POW => \&_stat_numdiff, "Pow"],
118     [&CS_STAT_WC => \&_stat_numdiff, "Wc"],
119     [&CS_STAT_AC => \&_stat_numdiff, "Ac"],
120     [&CS_STAT_DAM => \&_stat_numdiff, "Dam"],
121     [&CS_STAT_SPEED => \&_stat_numdiff, "Speed"],
122     [&CS_STAT_WEAP_SP => \&_stat_numdiff, "WSp"],
123     [&CS_STAT_MAXHP => \&_stat_numdiff, "HP"],
124     [&CS_STAT_MAXSP => \&_stat_numdiff, "Mana"],
125     [&CS_STAT_MAXGRACE => \&_stat_numdiff, "Grace"],
126     [&CS_STAT_WEIGHT_LIM => \&_stat_numdiff, "Weight"],
127     [&CS_STAT_SPELL_ATTUNE => \&_stat_skillmaskdiff, "attuned"],
128     [&CS_STAT_SPELL_REPEL => \&_stat_skillmaskdiff, "repelled"],
129     [&CS_STAT_SPELL_DENY => \&_stat_skillmaskdiff, "denied"],
130     [&CS_STAT_RES_PHYS => \&_stat_numdiff, "phys"],
131     [&CS_STAT_RES_MAG => \&_stat_numdiff, "magic"],
132     [&CS_STAT_RES_FIRE => \&_stat_numdiff, "fire"],
133     [&CS_STAT_RES_ELEC => \&_stat_numdiff, "electricity"],
134     [&CS_STAT_RES_COLD => \&_stat_numdiff, "cold"],
135     [&CS_STAT_RES_CONF => \&_stat_numdiff, "confusion"],
136     [&CS_STAT_RES_ACID => \&_stat_numdiff, "acid"],
137     [&CS_STAT_RES_DRAIN => \&_stat_numdiff, "drain"],
138     [&CS_STAT_RES_GHOSTHIT => \&_stat_numdiff, "ghosthit"],
139     [&CS_STAT_RES_POISON => \&_stat_numdiff, "poison"],
140     [&CS_STAT_RES_SLOW => \&_stat_numdiff, "slow"],
141     [&CS_STAT_RES_PARA => \&_stat_numdiff, "paralyse"],
142     [&CS_STAT_TURN_UNDEAD => \&_stat_numdiff, "turnundead"],
143     [&CS_STAT_RES_FEAR => \&_stat_numdiff, "fear"],
144     [&CS_STAT_RES_DEPLETE => \&_stat_numdiff, "depletion"],
145     [&CS_STAT_RES_DEATH => \&_stat_numdiff, "death"],
146     [&CS_STAT_RES_HOLYWORD => \&_stat_numdiff, "godpower"],
147     [&CS_STAT_RES_BLIND => \&_stat_numdiff, "blind"],
148     );
149    
150 root 1.1 sub stats_update {
151     my ($self, $stats) = @_;
152    
153 root 1.59 my $prev = $self->{prev_stats} || { };
154    
155     if (my @diffs =
156     (
157     ($stats->{+CS_STAT_EXP64} > $prev->{+CS_STAT_EXP64} ? ($stats->{+CS_STAT_EXP64} - $prev->{+CS_STAT_EXP64}) . " experience gained" : ()),
158     map {
159     $stats->{$_} && $prev->{$_}
160     && $stats->{$_}[1] > $prev->{$_}[1] ? "($self->{skill_info}{$_}+" . ($stats->{$_}[1] - $prev->{$_}[1]) . ")" : ()
161     } sort { $a <=> $b } keys %{$self->{skill_info}}
162     )
163     ) {
164     my $msg = join " ", @diffs;
165     $self->{statusbox}->add ($msg, group => "experience $msg", fg => [0.5, 1, 0.5, 0.8], timeout => 5);
166     }
167    
168     if (
169     my @diffs = map $_->[1]->($self, $_->[2], $prev->{$_->[0]}, $stats->{$_->[0]}), @statchange
170     ) {
171     my $msg = "<b>stat change</b>: " . (join " ", @diffs);
172     $self->{statusbox}->add ($msg, group => "stat $msg", fg => [0.8, 1, 0.2, 1], timeout => 10);
173     }
174    
175     $self->update_stats_window ($stats, $prev);
176    
177     $self->{prev_stats} = { %$stats };
178     }
179    
180     my %RES_TBL = (
181     phys => CS_STAT_RES_PHYS,
182     magic => CS_STAT_RES_MAG,
183     fire => CS_STAT_RES_FIRE,
184     elec => CS_STAT_RES_ELEC,
185     cold => CS_STAT_RES_COLD,
186     conf => CS_STAT_RES_CONF,
187     acid => CS_STAT_RES_ACID,
188     drain => CS_STAT_RES_DRAIN,
189     ghit => CS_STAT_RES_GHOSTHIT,
190     pois => CS_STAT_RES_POISON,
191     slow => CS_STAT_RES_SLOW,
192     para => CS_STAT_RES_PARA,
193     tund => CS_STAT_TURN_UNDEAD,
194     fear => CS_STAT_RES_FEAR,
195     depl => CS_STAT_RES_DEPLETE,
196     deat => CS_STAT_RES_DEATH,
197     holyw => CS_STAT_RES_HOLYWORD,
198     blind => CS_STAT_RES_BLIND,
199     );
200    
201     sub update_stats_window {
202     my ($self, $stats, $prev) = @_;
203    
204     # I love text protocols...
205 root 1.52
206 root 1.59 my $hp = $stats->{+CS_STAT_HP} * 1;
207     my $hp_m = $stats->{+CS_STAT_MAXHP} * 1;
208     my $sp = $stats->{+CS_STAT_SP} * 1;
209     my $sp_m = $stats->{+CS_STAT_MAXSP} * 1;
210     my $fo = $stats->{+CS_STAT_FOOD} * 1;
211     my $fo_m = 999;
212     my $gr = $stats->{+CS_STAT_GRACE} * 1;
213     my $gr_m = $stats->{+CS_STAT_MAXGRACE} * 1;
214    
215     $::GAUGES->{hp} ->set_value ($hp, $hp_m);
216     $::GAUGES->{mana} ->set_value ($sp, $sp_m);
217     $::GAUGES->{food} ->set_value ($fo, $fo_m);
218     $::GAUGES->{grace} ->set_value ($gr, $gr_m);
219     $::GAUGES->{exp} ->set_text ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64}))
220     . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")");
221     my $rng = $stats->{+CS_STAT_RANGE};
222     $rng =~ s/^Range: //; # thank you so much dear server
223     $::GAUGES->{range} ->set_text ("Rng: " . $rng);
224     my $title = $stats->{+CS_STAT_TITLE};
225     $title =~ s/^Player: //;
226     $::STATWIDS->{title} ->set_text ("Title: " . $title);
227    
228     $::STATWIDS->{st_str} ->set_text (sprintf "%d" , $stats->{+CS_STAT_STR});
229     $::STATWIDS->{st_dex} ->set_text (sprintf "%d" , $stats->{+CS_STAT_DEX});
230     $::STATWIDS->{st_con} ->set_text (sprintf "%d" , $stats->{+CS_STAT_CON});
231     $::STATWIDS->{st_int} ->set_text (sprintf "%d" , $stats->{+CS_STAT_INT});
232     $::STATWIDS->{st_wis} ->set_text (sprintf "%d" , $stats->{+CS_STAT_WIS});
233     $::STATWIDS->{st_pow} ->set_text (sprintf "%d" , $stats->{+CS_STAT_POW});
234     $::STATWIDS->{st_cha} ->set_text (sprintf "%d" , $stats->{+CS_STAT_CHA});
235     $::STATWIDS->{st_wc} ->set_text (sprintf "%d" , $stats->{+CS_STAT_WC});
236     $::STATWIDS->{st_ac} ->set_text (sprintf "%d" , $stats->{+CS_STAT_AC});
237     $::STATWIDS->{st_dam} ->set_text (sprintf "%d" , $stats->{+CS_STAT_DAM});
238     $::STATWIDS->{st_arm} ->set_text (sprintf "%d" , $stats->{+CS_STAT_RES_PHYS});
239     $::STATWIDS->{st_spd} ->set_text (sprintf "%.1f", $stats->{+CS_STAT_SPEED});
240     $::STATWIDS->{st_wspd}->set_text (sprintf "%.1f", $stats->{+CS_STAT_WEAP_SP});
241    
242 root 1.64 $self->update_weight;
243 root 1.59
244     $::STATWIDS->{"res_$_"}->set_text (sprintf "%d%", $stats->{$RES_TBL{$_}})
245     for keys %RES_TBL;
246    
247     my $sktbl = $::STATWIDS->{skill_tbl};
248     my @skills = keys %{ $self->{skill_info} };
249    
250     if (grep +(exists $stats->{$_}) != (exists $prev->{$_}), @skills) {
251     $sktbl->clear;
252    
253 root 1.69 $sktbl->add (0, 0, new CFPlus::UI::Label text => "Experience", align => 1);
254     $sktbl->add (1, 0, new CFPlus::UI::Label text => "Lvl.", align => 1);
255     $sktbl->add (2, 0, new CFPlus::UI::Label text => "Skill", expand => 1);
256     $sktbl->add (3, 0, new CFPlus::UI::Label text => "Experience", align => 1);
257     $sktbl->add (4, 0, new CFPlus::UI::Label text => "Lvl.", align => 1);
258     $sktbl->add (5, 0, new CFPlus::UI::Label text => "Skill", expand => 1);
259 root 1.59
260 elmex 1.68 my $TOOLTIP_ALL = "\n\n<small>Left click - ready skill\nMiddle click - use spell\nRight click - further options</small>";
261    
262     my @TOOLTIP_LVL = (tooltip => "<b>Level</b>. The level of the skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
263     my @TOOLTIP_EXP = (tooltip => "<b>Experience</b>. The experience points you have in this skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
264    
265 root 1.65 my ($x, $y) = (0, 1);
266 root 1.59 for (
267     sort { $stats->{$b->[0]}[1] <=> $stats->{$a->[0]}[1] or $a->[1] cmp $b->[1] }
268     map [$_, $self->{skill_info}{$_}],
269     grep exists $stats->{$_},
270     @skills
271 root 1.52 ) {
272 root 1.59 my ($idx, $name) = @$_;
273    
274 elmex 1.68 my $spell_cb = sub {
275     my ($widget, $ev) = @_;
276    
277     if ($ev->{button} == 1) {
278     $::CONN->user_send ("ready_skill $name");
279     } elsif ($ev->{button} == 2) {
280     $::CONN->user_send ("use_skill $name");
281     } elsif ($ev->{button} == 3) {
282 root 1.69 (new CFPlus::UI::Menu
283 elmex 1.68 items => [
284     ["bind <i>ready_skill $name</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["ready_skill $name"]) }],
285     ["bind <i>use_skill $name</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["use_skill $name"]) }],
286     ],
287     )->popup ($ev);
288     } else {
289     return 0;
290     }
291    
292     1
293     };
294    
295 root 1.69 $sktbl->add ($x * 3 + 0, $y, $self->{stat_widget_exp}{$idx} = new CFPlus::UI::Label
296 elmex 1.68 text => "0", align => 1, font => $::FONT_FIXED, fg => [1, 1, 0], on_button_down => $spell_cb, @TOOLTIP_EXP);
297 root 1.69 $sktbl->add ($x * 3 + 1, $y, $self->{stat_widget_lvl}{$idx} = new CFPlus::UI::Label
298 elmex 1.68 text => "0", align => 1, font => $::FONT_FIXED, fg => [0, 1, 0], padding_x => 4, on_button_down => $spell_cb, @TOOLTIP_LVL);
299 root 1.74 $sktbl->add ($x * 3 + 2, $y, new CFPlus::UI::Label text => $name, on_button_down => $spell_cb,
300     can_events => 1, can_hover => 1, tooltip => (CFPlus::Pod::section_label skill_description => $name) . $TOOLTIP_ALL);
301 root 1.59
302     $x++ and ($x, $y) = (0, $y + 1);
303 root 1.52 }
304 root 1.1 }
305    
306 root 1.59 for (grep exists $stats->{$_}, @skills) {
307     $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1]));
308     $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1);
309     }
310 root 1.1 }
311    
312     sub user_send {
313     my ($self, $command) = @_;
314    
315 elmex 1.9 if ($self->{record}) {
316     push @{$self->{record}}, $command;
317     }
318    
319 elmex 1.34 $self->logprint ("send: ", $command);
320 root 1.1 $self->send_command ($command);
321 root 1.45 ::status ($command);
322 root 1.1 }
323    
324 elmex 1.9 sub start_record {
325     my ($self) = @_;
326    
327     $self->{record} = [];
328     }
329    
330     sub stop_record {
331     my ($self) = @_;
332     return delete $self->{record};
333     }
334    
335 root 1.1 sub map_scroll {
336     my ($self, $dx, $dy) = @_;
337    
338     $self->{map}->scroll ($dx, $dy);
339     }
340    
341     sub feed_map1a {
342     my ($self, $data) = @_;
343    
344     $self->{map}->map1a_update ($data);
345     $self->{map_widget}->update;
346     }
347    
348 root 1.36 sub magicmap {
349     my ($self, $w, $h, $x, $y, $data) = @_;
350    
351     $self->{map_widget}->set_magicmap ($w, $h, $x, $y, $data);
352     }
353    
354 root 1.1 sub flush_map {
355     my ($self) = @_;
356    
357     my $map_info = delete $self->{map_info}
358     or return;
359    
360     my ($hash, $x, $y, $w, $h) = @$map_info;
361    
362     my $data = $self->{map}->get_rect ($x, $y, $w, $h);
363     $self->{mapcache}->put ($hash => Compress::LZF::compress $data);
364     #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
365     }
366    
367     sub map_clear {
368     my ($self) = @_;
369    
370     $self->flush_map;
371     delete $self->{neigh_map};
372    
373     $self->{map}->clear;
374 root 1.37 delete $self->{map_widget}{magicmap};
375 root 1.1 }
376    
377    
378     sub load_map($$$) {
379     my ($self, $hash, $x, $y) = @_;
380    
381     if (defined (my $data = $self->{mapcache}->get ($hash))) {
382     $data = Compress::LZF::decompress $data;
383     #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
384     for my $id ($self->{map}->set_rect ($x, $y, $data)) {
385     my $data = $self->{tilecache}->get ($id)
386     or next;
387    
388     $self->set_texture ($id => $data);
389     }
390     }
391     }
392    
393     # hardcode /world/world_xxx_xxx map names, the savings are enourmous,
394     # (server resource,s latency, bandwidth), so this hack is warranted.
395     # the right fix is to make real tiled maps with an overview file
396     sub send_mapinfo {
397     my ($self, $data, $cb) = @_;
398    
399     if ($self->{map_info}[0] =~ m%^/world/world_(\d\d\d)_(\d\d\d)$%) {
400     my ($wx, $wy) = ($1, $2);
401    
402     if ($data =~ /^spatial ([1-4]+)$/) {
403     my @dx = (0, 0, 1, 0, -1);
404     my @dy = (0, -1, 0, 1, 0);
405     my ($dx, $dy);
406    
407     for (split //, $1) {
408     $dx += $dx[$_];
409     $dy += $dy[$_];
410     }
411    
412     $cb->(spatial => 15,
413     $self->{map_info}[1] - $self->{map}->ox + $dx * 50,
414     $self->{map_info}[2] - $self->{map}->oy + $dy * 50,
415     50, 50,
416     sprintf "/world/world_%03d_%03d", $wx + $dx, $wy + $dy
417     );
418    
419     return;
420     }
421     }
422    
423     $self->SUPER::send_mapinfo ($data, $cb);
424     }
425    
426     # this method does a "flood fill" into every tile direction
427     # it assumes that tiles are arranged in a rectangular grid,
428     # i.e. a map is the same as the left of the right map etc.
429     # failure to comply are harmless and result in display errors
430     # at worst.
431     sub flood_fill {
432     my ($self, $block, $gx, $gy, $path, $hash, $flags) = @_;
433    
434     # the server does not allow map paths > 6
435     return if 7 <= length $path;
436    
437     my ($x0, $y0, $x1, $y1) = @{$self->{neigh_rect}};
438    
439     for (
440     [1, 3, 0, -1],
441     [2, 4, 1, 0],
442     [3, 1, 0, 1],
443     [4, 2, -1, 0],
444     ) {
445     my ($tile, $tile2, $dx, $dy) = @$_;
446    
447     next if $block & (1 << $tile);
448     my $block = $block | (1 << $tile2);
449    
450     my $gx = $gx + $dx;
451     my $gy = $gy + $dy;
452    
453     next unless $flags & (1 << ($tile - 1));
454     next if $self->{neigh_grid}{$gx, $gy}++;
455    
456     my $neigh = $self->{neigh_map}{$hash} ||= [];
457     if (my $info = $neigh->[$tile]) {
458     my ($flags, $x, $y, $w, $h, $hash) = @$info;
459    
460     $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
461     if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
462    
463     } else {
464     $self->send_mapinfo ("spatial $path$tile", sub {
465     my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
466    
467     return if $mode ne "spatial";
468    
469     $x += $self->{map}->ox;
470     $y += $self->{map}->oy;
471    
472     $self->load_map ($hash, $x, $y)
473     unless $self->{neigh_map}{$hash}[5]++;#d#
474    
475     $neigh->[$tile] = [$flags, $x, $y, $w, $h, $hash];
476    
477     $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
478     if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
479     });
480     }
481     }
482     }
483    
484     sub map_change {
485     my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
486    
487     $self->flush_map;
488    
489     my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
490    
491     my $mapmapw = $self->{mapmap}->{w};
492     my $mapmaph = $self->{mapmap}->{h};
493    
494     $self->{neigh_rect} = [
495     $ox - $mapmapw * 0.5, $oy - $mapmapw * 0.5,
496     $ox + $mapmapw * 0.5 + $w, $oy + $mapmapw * 0.5 + $h,
497     ];
498    
499     delete $self->{neigh_grid};
500    
501     $x += $ox;
502     $y += $oy;
503    
504     $self->{map_info} = [$hash, $x, $y, $w, $h];
505    
506     (my $map = $hash) =~ s/^.*?\/([^\/]+)$/\1/;
507     $::STATWIDS->{map}->set_text ("Map: " . $map);
508    
509     $self->load_map ($hash, $x, $y);
510     $self->flood_fill (0, 0, 0, "", $hash, $flags);
511     }
512    
513     sub face_find {
514     my ($self, $facenum, $face) = @_;
515    
516     my $hash = "$face->{chksum},$face->{name}";
517    
518     my $id = $self->{facemap}->get ($hash);
519    
520     unless ($id) {
521     # create new id for face
522     # I love transactions
523     for (1..100) {
524 root 1.69 my $txn = $CFPlus::DB_ENV->txn_begin;
525 root 1.15 my $status = $self->{facemap}->db_get (id => $id);
526 root 1.1 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
527 root 1.35 $id = ($id || 64) + 1;
528 root 1.1 if ($self->{facemap}->put (id => $id) == 0
529     && $self->{facemap}->put ($hash => $id) == 0) {
530     $txn->txn_commit;
531    
532     goto gotid;
533     }
534     }
535 root 1.15 $txn->txn_abort;
536 root 1.1 }
537    
538 root 1.69 CFPlus::fatal "maximum number of transaction retries reached - database problems?";
539 root 1.1 }
540    
541     gotid:
542     $face->{id} = $id;
543     $self->{map}->set_face ($facenum => $id);
544     $self->{faceid}[$facenum] = $id;#d#
545    
546     my $face = $self->{tilecache}->get ($id);
547    
548     if ($face) {
549     #$self->face_prefetch;
550     $face
551     } else {
552     my $tex = $self->{noface};
553     $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
554     undef
555     };
556     }
557    
558     sub face_update {
559 root 1.58 my ($self, $facenum, $face, $changed) = @_;
560 root 1.1
561 root 1.58 $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed;
562 root 1.1
563     $self->set_texture ($face->{id} => delete $face->{image});
564     }
565    
566     sub set_texture {
567     my ($self, $id, $data) = @_;
568    
569     $self->{texture}[$id] ||= do {
570     my $tex =
571 root 1.69 new_from_image CFPlus::Texture
572 root 1.1 $data, minify => 1, mipmap => 1;
573    
574     $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
575     $self->{map_widget}->update;
576    
577     $tex
578     };
579     }
580    
581     sub sound_play {
582     my ($self, $x, $y, $soundnum, $type) = @_;
583    
584     $self->{sound_play}->($x, $y, $soundnum, $type);
585     }
586    
587     my $LAST_QUERY; # server is stupid, stupid, stupid
588    
589     sub query {
590     my ($self, $flags, $prompt) = @_;
591    
592     $prompt = $LAST_QUERY unless length $prompt;
593     $LAST_QUERY = $prompt;
594    
595 root 1.22 $self->{query}-> ($self, $flags, $prompt);
596 root 1.1 }
597    
598     sub drawinfo {
599     my ($self, $color, $text) = @_;
600    
601     my @color = (
602     [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
603     [1.00, 1.00, 1.00],
604     [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
605     [1.00, 0.00, 0.00],
606     [1.00, 0.54, 0.00],
607     [0.11, 0.56, 1.00],
608     [0.93, 0.46, 0.00],
609     [0.18, 0.54, 0.34],
610     [0.56, 0.73, 0.56],
611     [0.80, 0.80, 0.80],
612 root 1.61 [0.75, 0.61, 0.20],
613 root 1.1 [0.99, 0.77, 0.26],
614     [0.74, 0.65, 0.41],
615     );
616    
617 elmex 1.34 $self->logprint ("info: ", $text);
618    
619 root 1.1 my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0];
620    
621 root 1.23 # try to create single paragraphs of multiple lines sent by the server
622 root 1.24 $text =~ s/(?<=\S)\n(?=\w)/ /g;
623 root 1.23
624 root 1.69 $text = CFPlus::asxml $text;
625 root 1.1 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g;
626     $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
627    
628 root 1.67 $self->{logview}->add_paragraph ({ fg => $color[$color], markup => $_ })
629 root 1.63 for map "<span foreground='#ffffff'>$time</span> $_", split /\n/, $text;
630 root 1.40 $self->{logview}->scroll_to_bottom;
631 root 1.1
632 elmex 1.5 $self->{statusbox}->add ($text,
633 root 1.1 group => $text,
634     fg => $color[$color],
635 root 1.71 timeout => $color >= 2 ? 180 : 10,
636 root 1.1 tooltip_font => $::FONT_FIXED,
637     );
638     }
639    
640     sub drawextinfo {
641     my ($self, $color, $type, $subtype, $message) = @_;
642    
643     $self->drawinfo ($color, $message);
644     }
645    
646     sub spell_add {
647     my ($self, $spell) = @_;
648    
649 root 1.66 # try to create single paragraphs out of the multiple lines sent by the server
650 root 1.27 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g;
651     $spell->{message} =~ s/\n+$//;
652     $spell->{message} ||= "Server did not provide a description for this spell.";
653    
654 root 1.51 $::SPELL_PAGE->add_spell ($spell);
655 elmex 1.14
656 root 1.69 $self->{map_widget}->add_command ("invoke $spell->{name}", CFPlus::asxml $spell->{message});
657     $self->{map_widget}->add_command ("cast $spell->{name}", CFPlus::asxml $spell->{message});
658 root 1.1 }
659    
660     sub spell_delete {
661     my ($self, $spell) = @_;
662 root 1.51
663     $::SPELL_PAGE->remove_spell ($spell);
664 root 1.1 }
665    
666     sub addme_success {
667     my ($self) = @_;
668    
669 root 1.72 my %skill_help;
670 root 1.21
671 root 1.72 for my $node (CFPlus::Pod::find skill_description => "*") {
672     my (undef, @par) = CFPlus::Pod::section_of $node;
673     $skill_help{$node->{kw}[0]} = CFPlus::Pod::as_label @par;
674 root 1.21 };
675 root 1.72
676 root 1.1 for my $skill (values %{$self->{skill_info}}) {
677     $self->{map_widget}->add_command ("ready_skill $skill",
678 root 1.69 (CFPlus::asxml "Ready the skill '$skill'\n\n")
679 root 1.72 . $skill_help{$skill});
680 root 1.1 $self->{map_widget}->add_command ("use_skill $skill",
681 root 1.69 (CFPlus::asxml "Immediately use the skill '$skill'\n\n")
682 root 1.72 . $skill_help{$skill});
683 root 1.1 }
684     }
685    
686     sub eof {
687     my ($self) = @_;
688    
689     $self->{map_widget}->clr_commands;
690    
691     ::stop_game ();
692     }
693    
694     sub image_info {
695     my ($self, $numfaces) = @_;
696    
697     $self->{num_faces} = $numfaces;
698     $self->{face_prefetch} = [1 .. $numfaces];
699     $self->face_prefetch;
700     }
701    
702     sub face_prefetch {
703     my ($self) = @_;
704    
705     return unless $::CFG->{face_prefetch};
706    
707     if ($self->{num_faces}) {
708     return if @{ $self->{send_queue} || [] };
709     my $todo = @{ $self->{face_prefetch} }
710     or return;
711    
712     my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, ();
713    
714     $self->send ("requestinfo image_sums $face $face");
715    
716 root 1.69 $self->{statusbox}->add (CFPlus::asxml "prefetching $todo",
717 root 1.19 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
718 root 1.1 } elsif (!exists $self->{num_faces}) {
719     $self->send ("requestinfo image_info");
720    
721     $self->{num_faces} = 0;
722    
723 root 1.69 $self->{statusbox}->add (CFPlus::asxml "starting to prefetch",
724 root 1.19 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
725 root 1.1 }
726     }
727    
728     sub update_floorbox {
729 root 1.69 $CFPlus::UI::ROOT->on_refresh ($::FLOORBOX => sub {
730 root 1.1 return unless $::CONN;
731 elmex 1.62
732 root 1.1 $::FLOORBOX->clear;
733    
734     my $row;
735 elmex 1.62 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) {
736 root 1.25 if ($row < 6) {
737 root 1.1 local $_->{face_widget}; # hack to force recreation of widget
738     local $_->{desc_widget}; # hack to force recreation of widget
739 root 1.69 CFPlus::Item::update_widgets $_;
740 root 1.1
741     $::FLOORBOX->add (0, $row, $_->{face_widget});
742     $::FLOORBOX->add (1, $row, $_->{desc_widget});
743    
744     $row++;
745     } else {
746 root 1.69 $::FLOORBOX->add (1, $row, new CFPlus::UI::Button
747 root 1.30 text => "More...",
748 root 1.51 on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 },
749 root 1.25 );
750 root 1.1 last;
751     }
752     }
753 elmex 1.62 if ($::CONN->{open_container}) {
754 root 1.69 $::FLOORBOX->add (1, $row++, new CFPlus::UI::Button
755 elmex 1.62 text => "Close container",
756     on_activate => sub { $::CONN->send ("apply $::CONN->{open_container}") }
757     );
758     }
759 root 1.1 });
760 elmex 1.62
761 root 1.1 $::WANT_REFRESH++;
762     }
763    
764     sub set_opencont {
765     my ($conn, $tag, $name) = @_;
766     $conn->{open_container} = $tag;
767 elmex 1.62 update_floorbox;
768 elmex 1.10
769     $::INV_RIGHT_HB->clear ();
770 root 1.69 $::INV_RIGHT_HB->add (new CFPlus::UI::Label align => 0, expand => 1, text => $name);
771 elmex 1.10
772     if ($tag != 0) { # Floor isn't closable, is it?
773 root 1.69 $::INV_RIGHT_HB->add (new CFPlus::UI::Button
774 elmex 1.10 text => "Close container",
775     tooltip => "Close the currently open container (if one is open)",
776     on_activate => sub {
777     $::CONN->send ("apply $tag") # $::CONN->{open_container}")
778     if $tag != 0;
779     #if $CONN->{open_container} != 0;
780 root 1.38 0
781 elmex 1.10 },
782     );
783     }
784    
785 root 1.1 $::INVR->set_items ($conn->{container}{$tag});
786     }
787    
788 root 1.47 sub update_containers {
789     my ($self) = @_;
790 root 1.1
791 root 1.69 $CFPlus::UI::ROOT->on_refresh ("update_containers_$self" => sub {
792 root 1.55 my $todo = delete $self->{update_container}
793     or return;
794    
795     for my $tag (keys %$todo) {
796 elmex 1.62 update_floorbox if $tag == 0 or $tag == $self->{open_container};
797 root 1.47 if ($tag == 0) {
798 root 1.48 $::INVR->set_items ($self->{container}{0})
799     if $tag == $self->{open_container};
800 root 1.47 } elsif ($tag == $self->{player}{tag}) {
801     $::INV->set_items ($self->{container}{$tag})
802     } else {
803 root 1.48 $::INVR->set_items ($self->{container}{$tag})
804     if $tag == $self->{open_container};
805 root 1.47 }
806     }
807     });
808 root 1.1 }
809    
810 root 1.48 sub container_add {
811     my ($self, $tag, $items) = @_;
812    
813     $self->{update_container}{$tag}++;
814     $self->update_containers;
815     }
816    
817 root 1.1 sub container_clear {
818     my ($self, $tag) = @_;
819    
820 root 1.47 $self->{update_container}{$tag}++;
821     $self->update_containers;
822 root 1.1 }
823    
824     sub item_delete {
825     my ($self, @items) = @_;
826    
827 root 1.49 $self->{update_container}{$_->{container}}++
828 root 1.47 for @items;
829    
830     $self->update_containers;
831 root 1.1 }
832    
833     sub item_update {
834     my ($self, $item) = @_;
835    
836     #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n";
837    
838 root 1.69 CFPlus::Item::update_widgets $item;
839 root 1.1
840 root 1.2 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) {
841 root 1.1 set_opencont ($::CONN, 0, "Floor");
842    
843 root 1.2 } elsif ($item->{flags} & F_OPEN) {
844 root 1.69 set_opencont ($::CONN, $item->{tag}, CFPlus::Item::desc_string $item);
845 root 1.47
846 root 1.1 } else {
847 root 1.47 $self->{update_container}{$item->{container}}++;
848     $self->update_containers;
849 root 1.1 }
850     }
851    
852 elmex 1.3 sub player_update {
853     my ($self, $player) = @_;
854 root 1.64
855     $self->update_weight;
856     }
857    
858     sub update_weight {
859     my ($self) = @_;
860    
861     my $weight = .001 * $self->{player}{weight};
862     my $limit = .001 * $self->{stat}{+CS_STAT_WEIGHT_LIM};
863    
864     $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $weight);
865     $::STATWIDS->{m_weight}->set_text (sprintf "%.1fkg", $limit);
866     $::STATWIDS->{i_weight}->set_text (sprintf "%.1f/%.1fkg", $weight, $limit);
867 root 1.28 }
868    
869 root 1.32 sub update_server_info {
870     my ($self) = @_;
871    
872     my @yesno = ("<span foreground='red'>no</span>", "<span foreground='green'>yes</span>");
873    
874     $::SERVER_INFO->set_markup (
875     "server <tt>$self->{host}:$self->{port}</tt>\n"
876     . "protocol version <tt>$self->{version}</tt>\n"
877     . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n"
878     . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n"
879     . "cfplus support $yesno[$self->{cfplus_ext} > 0]"
880     . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
881     . "map size $self->{mapw}×$self->{maph}\n"
882     );
883     }
884    
885 root 1.28 sub logged_in {
886     my ($self) = @_;
887    
888 root 1.70 $self->send_ext_req (cfplus_support => version => 1, sub {
889     $self->{cfplus_ext} = $_[0]{version};
890 root 1.32 $self->update_server_info;
891 root 1.70
892     0
893 root 1.32 });
894 root 1.31
895 root 1.32 $self->update_server_info;
896 root 1.13
897     $self->send_command ("output-sync $::CFG->{output_sync}");
898     $self->send_command ("output-count $::CFG->{output_count}");
899 root 1.29 $self->send_command ("pickup $::CFG->{pickup}");
900 elmex 1.3 }
901    
902 root 1.32 sub lookat {
903     my ($self, $x, $y) = @_;
904    
905     if ($self->{cfplus_ext}) {
906 root 1.70 $self->send_ext_req (lookat => dx => $x, dy => $y, sub {
907     my ($msg) = @_;
908 root 1.32
909 root 1.70 if (exists $msg->{npc_dialog}) {
910 root 1.32 # start npc chat dialog
911 root 1.69 $self->{npc_dialog} = new CFPlus::NPCDialog::
912 root 1.32 dx => $x,
913     dy => $y,
914 root 1.70 title => "$msg->{npc_dialog} (NPC)",
915 root 1.32 conn => $self,
916     ;
917     }
918     });
919     }
920    
921     $self->send ("lookat $x $y");
922     }
923    
924     sub destroy {
925     my ($self) = @_;
926    
927     $self->{npc_dialog}->destroy
928     if $self->{npc_dialog};
929    
930     $self->SUPER::destroy;
931     }
932    
933 root 1.69 package CFPlus::NPCDialog;
934 root 1.32
935 root 1.76 our @ISA = 'CFPlus::UI::Toplevel';
936 root 1.32
937     sub new {
938     my $class = shift;
939    
940     my $self = $class->SUPER::new (
941     x => 'center',
942     y => 'center',
943     name => "npc_dialog",
944     force_w => $::WIDTH * 0.7,
945     force_h => $::HEIGHT * 0.7,
946     title => "NPC Dialog",
947     kw => { hi => 0, yes => 0, no => 0 },
948 root 1.64 has_close_button => 1,
949 root 1.32 @_,
950     );
951    
952     Scalar::Util::weaken (my $this = $self);
953    
954 root 1.64 $self->connect (delete => sub { $this->destroy; 1 });
955    
956 root 1.32 # better use a pane...
957 root 1.69 $self->add (my $hbox = new CFPlus::UI::HBox);
958     $hbox->add ($self->{textview} = new CFPlus::UI::TextScroller expand => 1);
959 root 1.32
960 root 1.69 $hbox->add (my $vbox = new CFPlus::UI::VBox);
961 root 1.32
962 root 1.69 $vbox->add (new CFPlus::UI::Label text => "Message Entry:");
963     $vbox->add ($self->{entry} = new CFPlus::UI::Entry
964 root 1.73 tooltip => "#npc_message_entry",
965 root 1.32 on_activate => sub {
966     my ($entry, $text) = @_;
967    
968     return unless $text =~ /\S/;
969    
970     $entry->set_text ("");
971     $this->send ($text);
972 root 1.38
973     0
974 root 1.32 },
975     );
976    
977 root 1.69 $vbox->add ($self->{options} = new CFPlus::UI::VBox);
978 root 1.32
979 root 1.69 $self->{bye_button} = new CFPlus::UI::Button
980 root 1.32 text => "Bye (close)",
981     tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.",
982 root 1.64 on_activate => sub { $this->destroy; 1 },
983 root 1.32 ;
984    
985     $self->update_options;
986    
987 root 1.70 $self->{id} = $self->{conn}->send_ext_req (
988     npc_dialog_begin => dx => $self->{dx}, dy => $self->{dy},
989     sub { $this && $this->feed (@_) }
990     );
991 root 1.32
992 root 1.38 $self->{entry}->grab_focus;
993 root 1.32
994 root 1.67 $self->{textview}->add_paragraph ({
995     fg => [1, 1, 0, 1],
996     markup => "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n",
997     });
998 root 1.32
999     $self->show;
1000     $self
1001     };
1002    
1003     sub update_options {
1004     my ($self) = @_;
1005    
1006     Scalar::Util::weaken $self;
1007    
1008     $self->{options}->clear;
1009 root 1.39 $self->{options}->add ($self->{bye_button});
1010 root 1.32
1011     for my $kw (sort keys %{ $self->{kw} }) {
1012 root 1.69 $self->{options}->add (new CFPlus::UI::Button
1013 root 1.32 text => $kw,
1014     on_activate => sub {
1015     $self->send ($kw);
1016 root 1.38 0
1017 root 1.32 },
1018     );
1019     }
1020     }
1021    
1022     sub feed {
1023 root 1.70 my ($self, $msg) = @_;
1024 root 1.32
1025 root 1.42 Scalar::Util::weaken $self;
1026    
1027 root 1.70 if ($msg->{msgtype} eq "reply") {
1028     $self->{kw}{$_} = 1 for @{$msg->{add_topics} || []};
1029     $self->{kw}{$_} = 0 for @{$msg->{del_topics} || []};
1030 root 1.61
1031 root 1.70 my $text = "\n" . CFPlus::asxml $msg->{msg};
1032 root 1.32 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} };
1033 root 1.41 my @link;
1034 root 1.70 $text =~ s{
1035 root 1.41 ($match)
1036     }{
1037 root 1.42 my $kw = $1;
1038    
1039 root 1.69 push @link, new CFPlus::UI::Label
1040 root 1.42 markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>",
1041 root 1.41 can_hover => 1,
1042 root 1.42 can_events => 1,
1043 root 1.43 padding_x => 0,
1044     padding_y => 0,
1045 root 1.42 on_button_up => sub {
1046     $self->send ($kw);
1047     };
1048 root 1.41
1049 root 1.75 "\x{fffc}"
1050 root 1.41 }giex;
1051 root 1.32
1052 root 1.70 $self->{textview}->add_paragraph ({ markup => $text, widget => \@link });
1053 root 1.42 $self->{textview}->scroll_to_bottom;
1054 root 1.32 $self->update_options;
1055     } else {
1056     $self->destroy;
1057     }
1058    
1059     1
1060     }
1061    
1062     sub send {
1063     my ($self, $msg) = @_;
1064    
1065 root 1.69 $self->{textview}->add_paragraph ({ markup => "\n" . CFPlus::asxml $msg });
1066 root 1.42 $self->{textview}->scroll_to_bottom;
1067 root 1.61
1068 root 1.70 $self->{conn}->send_ext_msg (npc_dialog_tell => msgid => $self->{id}, msg => $msg);
1069 root 1.32 }
1070    
1071     sub destroy {
1072     my ($self) = @_;
1073    
1074 root 1.39 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1
1075 root 1.32
1076 root 1.70 if ($self->{conn}) {
1077     $self->{conn}->send_ext_msg (npc_dialog_end => msgid => $self->{id}) if $self->{id};
1078     delete $self->{conn}{npc_dialog};
1079     $self->{conn}->disconnect_ext ($self->{id});
1080     }
1081 root 1.32
1082     $self->SUPER::destroy;
1083     }
1084    
1085 root 1.67 1