ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.83
Committed: Tue Sep 19 22:02:38 2006 UTC (17 years, 8 months ago) by root
Branch: MAIN
CVS Tags: rel-0_52
Changes since 1.82: +6 -0 lines
Log Message:
add dialogue indicator support

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