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