ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.78
Committed: Mon Aug 14 14:35:31 2006 UTC (17 years, 10 months ago) by elmex
Branch: MAIN
Changes since 1.77: +1 -0 lines
Log Message:
further documentation

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