ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/Protocol.pm (file contents):
Revision 1.35 by root, Thu Jun 15 13:40:02 2006 UTC vs.
Revision 1.63 by root, Sun Jul 23 04:46:02 2006 UTC

7 7
8use CFClient; 8use CFClient;
9use CFClient::UI; 9use CFClient::UI;
10 10
11use base 'Crossfire::Protocol::Base'; 11use base 'Crossfire::Protocol::Base';
12
13our %open_logs;
14 12
15sub new { 13sub new {
16 my $class = shift; 14 my $class = shift;
17 15
18 my $self = $class->SUPER::new (@_); 16 my $self = $class->SUPER::new (@_);
63 $self 61 $self
64} 62}
65 63
66sub logprint { 64sub logprint {
67 my ($self, @a) = @_; 65 my ($self, @a) = @_;
66
67 $self->{log_fh} ||= do {
68 my $filename = "$Crossfire::VARDIR/log.$self->{host}"; 68 my $path = "$Crossfire::VARDIR/log.$self->{host}";
69 69
70 my $fh = $open_logs{$filename}; 70 open my $fh, ">>:utf8", $path
71 unless ($fh) {
72 # FIXME: handle this more gracefully?
73 open $fh, ">>", $filename
74 or die "Couldn't open logfile: log.$self->{host}: $!"; 71 or die "Couldn't open logfile $path: $!";
75 72
76 $open_logs{$filename} = $fh; 73 $fh->autoflush (1);
74
75 $fh;
77 } 76 };
78 77
79 my ($sec, $min, $hour, $mday, $mon, $year) = localtime (time); 78 my ($sec, $min, $hour, $mday, $mon, $year) = localtime time;
80 79
81 my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d", 80 my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
82 $year + 1900, $mon + 1, $mday, $hour, $min, $sec; 81 $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
83 82
84 print $fh "$ts ", @a, "\n"; 83 print {$self->{log_fh}} "$ts ", @a, "\n";
85 $fh->flush;
86} 84}
87 85
86sub _stat_numdiff {
87 my ($self, $name, $old, $new) = @_;
88 88
89 my $diff = $new - $old;
90
91 $diff = 0.01 * int $diff * 100;
92
93 0.1 >= abs $diff ? ()
94 : $diff < 0 ? "$name$diff" : "$name+$diff"
95}
96
97sub _stat_skillmaskdiff {
98 my ($self, $name, $old, $new) = @_;
99
100 my $diff = $old ^ $new
101 or return;
102
103 my @diff = map
104 {
105 $diff & $_
106 ? (($new & $_ ? "+" : "-") . $self->{spell_paths}{$_})
107 : ()
108 }
109 sort { $a <=> $b } keys %{$self->{spell_paths}};
110
111 join "", @diff
112}
113
114# all stats that are chacked against changes
115my @statchange = (
116 [&CS_STAT_STR => \&_stat_numdiff, "Str"],
117 [&CS_STAT_INT => \&_stat_numdiff, "Int"],
118 [&CS_STAT_WIS => \&_stat_numdiff, "Wis"],
119 [&CS_STAT_DEX => \&_stat_numdiff, "Dex"],
120 [&CS_STAT_CON => \&_stat_numdiff, "Con"],
121 [&CS_STAT_CHA => \&_stat_numdiff, "Cha"],
122 [&CS_STAT_POW => \&_stat_numdiff, "Pow"],
123 [&CS_STAT_WC => \&_stat_numdiff, "Wc"],
124 [&CS_STAT_AC => \&_stat_numdiff, "Ac"],
125 [&CS_STAT_DAM => \&_stat_numdiff, "Dam"],
126 [&CS_STAT_SPEED => \&_stat_numdiff, "Speed"],
127 [&CS_STAT_WEAP_SP => \&_stat_numdiff, "WSp"],
128 [&CS_STAT_MAXHP => \&_stat_numdiff, "HP"],
129 [&CS_STAT_MAXSP => \&_stat_numdiff, "Mana"],
130 [&CS_STAT_MAXGRACE => \&_stat_numdiff, "Grace"],
131 [&CS_STAT_WEIGHT_LIM => \&_stat_numdiff, "Weight"],
132 [&CS_STAT_SPELL_ATTUNE => \&_stat_skillmaskdiff, "attuned"],
133 [&CS_STAT_SPELL_REPEL => \&_stat_skillmaskdiff, "repelled"],
134 [&CS_STAT_SPELL_DENY => \&_stat_skillmaskdiff, "denied"],
135 [&CS_STAT_RES_PHYS => \&_stat_numdiff, "phys"],
136 [&CS_STAT_RES_MAG => \&_stat_numdiff, "magic"],
137 [&CS_STAT_RES_FIRE => \&_stat_numdiff, "fire"],
138 [&CS_STAT_RES_ELEC => \&_stat_numdiff, "electricity"],
139 [&CS_STAT_RES_COLD => \&_stat_numdiff, "cold"],
140 [&CS_STAT_RES_CONF => \&_stat_numdiff, "confusion"],
141 [&CS_STAT_RES_ACID => \&_stat_numdiff, "acid"],
142 [&CS_STAT_RES_DRAIN => \&_stat_numdiff, "drain"],
143 [&CS_STAT_RES_GHOSTHIT => \&_stat_numdiff, "ghosthit"],
144 [&CS_STAT_RES_POISON => \&_stat_numdiff, "poison"],
145 [&CS_STAT_RES_SLOW => \&_stat_numdiff, "slow"],
146 [&CS_STAT_RES_PARA => \&_stat_numdiff, "paralyse"],
147 [&CS_STAT_TURN_UNDEAD => \&_stat_numdiff, "turnundead"],
148 [&CS_STAT_RES_FEAR => \&_stat_numdiff, "fear"],
149 [&CS_STAT_RES_DEPLETE => \&_stat_numdiff, "depletion"],
150 [&CS_STAT_RES_DEATH => \&_stat_numdiff, "death"],
151 [&CS_STAT_RES_HOLYWORD => \&_stat_numdiff, "godpower"],
152 [&CS_STAT_RES_BLIND => \&_stat_numdiff, "blind"],
153);
89 154
90sub stats_update { 155sub stats_update {
91 my ($self, $stats) = @_; 156 my ($self, $stats) = @_;
92 157
93 if (my $exp = $stats->{+CS_STAT_EXP64}) { 158 my $prev = $self->{prev_stats} || { };
94 my $diff = $exp - $self->{prev_exp}; 159
160 if (my @diffs =
161 (
162 ($stats->{+CS_STAT_EXP64} > $prev->{+CS_STAT_EXP64} ? ($stats->{+CS_STAT_EXP64} - $prev->{+CS_STAT_EXP64}) . " experience gained" : ()),
163 map {
164 $stats->{$_} && $prev->{$_}
165 && $stats->{$_}[1] > $prev->{$_}[1] ? "($self->{skill_info}{$_}+" . ($stats->{$_}[1] - $prev->{$_}[1]) . ")" : ()
166 } sort { $a <=> $b } keys %{$self->{skill_info}}
167 )
168 ) {
169 my $msg = join " ", @diffs;
95 $self->{statusbox}->add ("$diff experience gained", group => "experience $diff", fg => [0.5, 1, 0.5, 0.8], timeout => 5) 170 $self->{statusbox}->add ($msg, group => "experience $msg", fg => [0.5, 1, 0.5, 0.8], timeout => 5);
96 if exists $self->{prev_exp} && $diff;
97 $self->{prev_exp} = $exp;
98 } 171 }
99 172
173 if (
174 my @diffs = map $_->[1]->($self, $_->[2], $prev->{$_->[0]}, $stats->{$_->[0]}), @statchange
175 ) {
176 my $msg = "<b>stat change</b>: " . (join " ", @diffs);
177 $self->{statusbox}->add ($msg, group => "stat $msg", fg => [0.8, 1, 0.2, 1], timeout => 10);
178 }
179
100 ::update_stats_window ($stats); 180 $self->update_stats_window ($stats, $prev);
181
182 $self->{prev_stats} = { %$stats };
183}
184
185my %RES_TBL = (
186 phys => CS_STAT_RES_PHYS,
187 magic => CS_STAT_RES_MAG,
188 fire => CS_STAT_RES_FIRE,
189 elec => CS_STAT_RES_ELEC,
190 cold => CS_STAT_RES_COLD,
191 conf => CS_STAT_RES_CONF,
192 acid => CS_STAT_RES_ACID,
193 drain => CS_STAT_RES_DRAIN,
194 ghit => CS_STAT_RES_GHOSTHIT,
195 pois => CS_STAT_RES_POISON,
196 slow => CS_STAT_RES_SLOW,
197 para => CS_STAT_RES_PARA,
198 tund => CS_STAT_TURN_UNDEAD,
199 fear => CS_STAT_RES_FEAR,
200 depl => CS_STAT_RES_DEPLETE,
201 deat => CS_STAT_RES_DEATH,
202 holyw => CS_STAT_RES_HOLYWORD,
203 blind => CS_STAT_RES_BLIND,
204);
205
206sub update_stats_window {
207 my ($self, $stats, $prev) = @_;
208
209 # I love text protocols...
210
211 my $hp = $stats->{+CS_STAT_HP} * 1;
212 my $hp_m = $stats->{+CS_STAT_MAXHP} * 1;
213 my $sp = $stats->{+CS_STAT_SP} * 1;
214 my $sp_m = $stats->{+CS_STAT_MAXSP} * 1;
215 my $fo = $stats->{+CS_STAT_FOOD} * 1;
216 my $fo_m = 999;
217 my $gr = $stats->{+CS_STAT_GRACE} * 1;
218 my $gr_m = $stats->{+CS_STAT_MAXGRACE} * 1;
219
220 $::GAUGES->{hp} ->set_value ($hp, $hp_m);
221 $::GAUGES->{mana} ->set_value ($sp, $sp_m);
222 $::GAUGES->{food} ->set_value ($fo, $fo_m);
223 $::GAUGES->{grace} ->set_value ($gr, $gr_m);
224 $::GAUGES->{exp} ->set_text ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64}))
225 . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")");
226 my $rng = $stats->{+CS_STAT_RANGE};
227 $rng =~ s/^Range: //; # thank you so much dear server
228 $::GAUGES->{range} ->set_text ("Rng: " . $rng);
229 my $title = $stats->{+CS_STAT_TITLE};
230 $title =~ s/^Player: //;
231 $::STATWIDS->{title} ->set_text ("Title: " . $title);
232
233 $::STATWIDS->{st_str} ->set_text (sprintf "%d" , $stats->{+CS_STAT_STR});
234 $::STATWIDS->{st_dex} ->set_text (sprintf "%d" , $stats->{+CS_STAT_DEX});
235 $::STATWIDS->{st_con} ->set_text (sprintf "%d" , $stats->{+CS_STAT_CON});
236 $::STATWIDS->{st_int} ->set_text (sprintf "%d" , $stats->{+CS_STAT_INT});
237 $::STATWIDS->{st_wis} ->set_text (sprintf "%d" , $stats->{+CS_STAT_WIS});
238 $::STATWIDS->{st_pow} ->set_text (sprintf "%d" , $stats->{+CS_STAT_POW});
239 $::STATWIDS->{st_cha} ->set_text (sprintf "%d" , $stats->{+CS_STAT_CHA});
240 $::STATWIDS->{st_wc} ->set_text (sprintf "%d" , $stats->{+CS_STAT_WC});
241 $::STATWIDS->{st_ac} ->set_text (sprintf "%d" , $stats->{+CS_STAT_AC});
242 $::STATWIDS->{st_dam} ->set_text (sprintf "%d" , $stats->{+CS_STAT_DAM});
243 $::STATWIDS->{st_arm} ->set_text (sprintf "%d" , $stats->{+CS_STAT_RES_PHYS});
244 $::STATWIDS->{st_spd} ->set_text (sprintf "%.1f", $stats->{+CS_STAT_SPEED});
245 $::STATWIDS->{st_wspd}->set_text (sprintf "%.1f", $stats->{+CS_STAT_WEAP_SP});
246
247 $::STATWIDS->{m_weight}->set_text (sprintf "Max weight: %.1fkg", $stats->{+CS_STAT_WEIGHT_LIM} / 1000);
248
249 $::STATWIDS->{"res_$_"}->set_text (sprintf "%d%", $stats->{$RES_TBL{$_}})
250 for keys %RES_TBL;
251
252 my $sktbl = $::STATWIDS->{skill_tbl};
253 my @skills = keys %{ $self->{skill_info} };
254
255 if (grep +(exists $stats->{$_}) != (exists $prev->{$_}), @skills) {
256 $sktbl->clear;
257
258 $sktbl->add (0, 0, new CFClient::UI::Label text => "Experience", align => 1);
259 $sktbl->add (1, 0, new CFClient::UI::Label text => "Lvl.", align => 1);
260 $sktbl->add (2, 0, new CFClient::UI::Label text => "Skill", expand => 1);
261 $sktbl->add (3, 0, new CFClient::UI::Label text => "Experience", align => 1);
262 $sktbl->add (4, 0, new CFClient::UI::Label text => "Lvl.", align => 1);
263 $sktbl->add (5, 0, new CFClient::UI::Label text => "Skill", expand => 1);
264
265 my ($x, $y) = (1, 0);
266 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 ) {
272 my ($idx, $name) = @$_;
273
274 $sktbl->add ($x * 3 + 0, $y, $self->{stat_widget_exp}{$idx} = new CFClient::UI::Label
275 text => "0", align => 1, font => $::FONT_FIXED, fg => [1, 1, 0]);
276 $sktbl->add ($x * 3 + 1, $y, $self->{stat_widget_lvl}{$idx} = new CFClient::UI::Label
277 text => "0", align => 1, font => $::FONT_FIXED, fg => [0, 1, 0], padding_x => 4);
278 $sktbl->add ($x * 3 + 2, $y, new CFClient::UI::Label text => $name);
279
280 $x++ and ($x, $y) = (0, $y + 1);
281 }
282 }
283
284 for (grep exists $stats->{$_}, @skills) {
285 $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1]));
286 $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1);
287 }
101} 288}
102 289
103sub user_send { 290sub user_send {
104 my ($self, $command) = @_; 291 my ($self, $command) = @_;
105 292
107 push @{$self->{record}}, $command; 294 push @{$self->{record}}, $command;
108 } 295 }
109 296
110 $self->logprint ("send: ", $command); 297 $self->logprint ("send: ", $command);
111 $self->send_command ($command); 298 $self->send_command ($command);
112 ::status $command; 299 ::status ($command);
113} 300}
114 301
115sub start_record { 302sub start_record {
116 my ($self) = @_; 303 my ($self) = @_;
117 304
132sub feed_map1a { 319sub feed_map1a {
133 my ($self, $data) = @_; 320 my ($self, $data) = @_;
134 321
135 $self->{map}->map1a_update ($data); 322 $self->{map}->map1a_update ($data);
136 $self->{map_widget}->update; 323 $self->{map_widget}->update;
324}
325
326sub magicmap {
327 my ($self, $w, $h, $x, $y, $data) = @_;
328
329 $self->{map_widget}->set_magicmap ($w, $h, $x, $y, $data);
137} 330}
138 331
139sub flush_map { 332sub flush_map {
140 my ($self) = @_; 333 my ($self) = @_;
141 334
154 347
155 $self->flush_map; 348 $self->flush_map;
156 delete $self->{neigh_map}; 349 delete $self->{neigh_map};
157 350
158 $self->{map}->clear; 351 $self->{map}->clear;
352 delete $self->{map_widget}{magicmap};
159} 353}
160 354
161 355
162sub load_map($$$) { 356sub load_map($$$) {
163 my ($self, $hash, $x, $y) = @_; 357 my ($self, $hash, $x, $y) = @_;
338 undef 532 undef
339 }; 533 };
340} 534}
341 535
342sub face_update { 536sub face_update {
343 my ($self, $facenum, $face) = @_; 537 my ($self, $facenum, $face, $changed) = @_;
344 538
345 $self->{tilecache}->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes 539 $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed;
346 540
347 $self->set_texture ($face->{id} => delete $face->{image}); 541 $self->set_texture ($face->{id} => delete $face->{image});
348} 542}
349 543
350sub set_texture { 544sub set_texture {
391 [0.11, 0.56, 1.00], 585 [0.11, 0.56, 1.00],
392 [0.93, 0.46, 0.00], 586 [0.93, 0.46, 0.00],
393 [0.18, 0.54, 0.34], 587 [0.18, 0.54, 0.34],
394 [0.56, 0.73, 0.56], 588 [0.56, 0.73, 0.56],
395 [0.80, 0.80, 0.80], 589 [0.80, 0.80, 0.80],
396 [0.55, 0.41, 0.13], 590 [0.75, 0.61, 0.20],
397 [0.99, 0.77, 0.26], 591 [0.99, 0.77, 0.26],
398 [0.74, 0.65, 0.41], 592 [0.74, 0.65, 0.41],
399 ); 593 );
400 594
401 $self->logprint ("info: ", $text); 595 $self->logprint ("info: ", $text);
407 601
408 $text = CFClient::UI::Label::escape $text; 602 $text = CFClient::UI::Label::escape $text;
409 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; 603 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g;
410 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; 604 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
411 605
412 $self->{logview}->add_paragraph ($color[$color], 606 $self->{logview}->add_paragraph ($color[$color], $_)
413 join "\n", map "$time $_", split /\n/, $text); 607 for map "<span foreground='#ffffff'>$time</span> $_", split /\n/, $text;
608 $self->{logview}->scroll_to_bottom;
414 609
415 $self->{statusbox}->add ($text, 610 $self->{statusbox}->add ($text,
416 group => $text, 611 group => $text,
417 fg => $color[$color], 612 fg => $color[$color],
418 timeout => $color >= 2 ? 60 : 10, 613 timeout => $color >= 2 ? 60 : 10,
432 # try to create single paragraphs of multiple lines sent by the server 627 # try to create single paragraphs of multiple lines sent by the server
433 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g; 628 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g;
434 $spell->{message} =~ s/\n+$//; 629 $spell->{message} =~ s/\n+$//;
435 $spell->{message} ||= "Server did not provide a description for this spell."; 630 $spell->{message} ||= "Server did not provide a description for this spell.";
436 631
437 $::SETUP_SPELLS->add_spell ($spell); 632 $::SPELL_PAGE->add_spell ($spell);
438 633
439 $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message}); 634 $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message});
440 $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message}); 635 $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message});
441} 636}
442 637
443sub spell_delete { 638sub spell_delete {
444 my ($self, $spell) = @_; 639 my ($self, $spell) = @_;
640
445 $::SETUP_SPELLS->remove_spell ($spell); 641 $::SPELL_PAGE->remove_spell ($spell);
446} 642}
447 643
448sub addme_success { 644sub addme_success {
449 my ($self) = @_; 645 my ($self) = @_;
450 646
513} 709}
514 710
515sub update_floorbox { 711sub update_floorbox {
516 $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub { 712 $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub {
517 return unless $::CONN; 713 return unless $::CONN;
518 714
519 $::FLOORBOX->clear; 715 $::FLOORBOX->clear;
520 716
521 my $row; 717 my $row;
522 for (@{ $::CONN->{container}{0} }) { 718 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) {
523 if ($row < 6) { 719 if ($row < 6) {
524 local $_->{face_widget}; # hack to force recreation of widget 720 local $_->{face_widget}; # hack to force recreation of widget
525 local $_->{desc_widget}; # hack to force recreation of widget 721 local $_->{desc_widget}; # hack to force recreation of widget
526 CFClient::Item::update_widgets $_; 722 CFClient::Item::update_widgets $_;
527 723
530 726
531 $row++; 727 $row++;
532 } else { 728 } else {
533 $::FLOORBOX->add (1, $row, new CFClient::UI::Button 729 $::FLOORBOX->add (1, $row, new CFClient::UI::Button
534 text => "More...", 730 text => "More...",
535 on_activate => sub { $::INV_WINDOW->toggle_visibility }, 731 on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 },
536 ); 732 );
537 last; 733 last;
538 } 734 }
539 } 735 }
736 if ($::CONN->{open_container}) {
737 $::FLOORBOX->add (1, $row++, new CFClient::UI::Button
738 text => "Close container",
739 on_activate => sub { $::CONN->send ("apply $::CONN->{open_container}") }
740 );
741 }
540 }); 742 });
541 743
542 $::WANT_REFRESH++; 744 $::WANT_REFRESH++;
543} 745}
544 746
545sub set_opencont { 747sub set_opencont {
546 my ($conn, $tag, $name) = @_; 748 my ($conn, $tag, $name) = @_;
547 $conn->{open_container} = $tag; 749 $conn->{open_container} = $tag;
750 update_floorbox;
548 751
549 $::INV_RIGHT_HB->clear (); 752 $::INV_RIGHT_HB->clear ();
550 $::INV_RIGHT_HB->add (new CFClient::UI::Label align => 0, expand => 1, text => $name); 753 $::INV_RIGHT_HB->add (new CFClient::UI::Label align => 0, expand => 1, text => $name);
551 754
552 if ($tag != 0) { # Floor isn't closable, is it? 755 if ($tag != 0) { # Floor isn't closable, is it?
555 tooltip => "Close the currently open container (if one is open)", 758 tooltip => "Close the currently open container (if one is open)",
556 on_activate => sub { 759 on_activate => sub {
557 $::CONN->send ("apply $tag") # $::CONN->{open_container}") 760 $::CONN->send ("apply $tag") # $::CONN->{open_container}")
558 if $tag != 0; 761 if $tag != 0;
559 #if $CONN->{open_container} != 0; 762 #if $CONN->{open_container} != 0;
763 0
560 }, 764 },
561 ); 765 );
562 } 766 }
563 767
564 $::INVR->set_items ($conn->{container}{$tag}); 768 $::INVR->set_items ($conn->{container}{$tag});
565} 769}
566 770
567sub update_container { 771sub update_containers {
568 my ($tag) = @_; 772 my ($self) = @_;
569 773
570 $::INVR->set_items ($::CONN->{container}{$::CONN->{open_container}}) 774 $CFClient::UI::ROOT->on_refresh ("update_containers_$self" => sub {
775 my $todo = delete $self->{update_container}
776 or return;
777
778 for my $tag (keys %$todo) {
779 update_floorbox if $tag == 0 or $tag == $self->{open_container};
780 if ($tag == 0) {
781 $::INVR->set_items ($self->{container}{0})
571 if $tag == $::CONN->{open_container}; 782 if $tag == $self->{open_container};
783 } elsif ($tag == $self->{player}{tag}) {
784 $::INV->set_items ($self->{container}{$tag})
785 } else {
786 $::INVR->set_items ($self->{container}{$tag})
787 if $tag == $self->{open_container};
788 }
789 }
790 });
572} 791}
573 792
574sub container_add { 793sub container_add {
575 my ($self, $tag, $items) = @_; 794 my ($self, $tag, $items) = @_;
576 795
577 #d# print "container_add: container $tag ($self->{player}{tag})\n"; 796 $self->{update_container}{$tag}++;
578 797 $self->update_containers;
579 if ($tag == 0) {
580 update_floorbox;
581 update_container (0);
582 } elsif ($tag == $self->{player}{tag}) {
583 $::INV->set_items ($self->{container}{$self->{player}{tag}})
584 } else {
585 update_container ($tag);
586 }
587
588 # $self-<{player}{tag} => player inv
589 #use PApp::Util; warn PApp::Util::dumpval $self->{container}{$self->{player}{tag}};
590} 798}
591 799
592sub container_clear { 800sub container_clear {
593 my ($self, $tag) = @_; 801 my ($self, $tag) = @_;
594 802
595 #d# print "container_clear: container $tag ($self->{player}{tag})\n"; 803 $self->{update_container}{$tag}++;
596 804 $self->update_containers;
597 if ($tag == 0) {
598 update_floorbox;
599 update_container (0);
600 } elsif ($tag == $self->{player}{tag}) {
601 $::INV->set_items ($self->{container}{$tag})
602 } else {
603 update_container ($tag);
604 }
605
606# use PApp::Util; warn PApp::Util::dumpval $self->{container}{0};
607} 805}
608 806
609sub item_delete { 807sub item_delete {
610 my ($self, @items) = @_; 808 my ($self, @items) = @_;
611 809
810 $self->{update_container}{$_->{container}}++
612 for (@items) { 811 for @items;
613 #d# print "item_delete: $_->{tag} from $_->{container} ($self->{player}{tag})\n";
614
615 if ($_->{container} == 0) {
616 update_floorbox;
617 update_container ($_->{tag});
618 } elsif ($_->{container} == $self->{player}{tag}) {
619 $::INV->set_items ($self->{container}{$self->{player}{tag}})
620 } else {
621 update_container ($_->{container});
622 }
623 } 812
813 $self->update_containers;
624} 814}
625 815
626sub item_update { 816sub item_update {
627 my ($self, $item) = @_; 817 my ($self, $item) = @_;
628 818
629 #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n"; 819 #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n";
630 820
631 if ($item->{tag} == $self->{player}{tag}) {
632 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $item->{weight} / 1000);
633 return;
634 }
635
636 CFClient::Item::update_widgets $item; 821 CFClient::Item::update_widgets $item;
637 822
638 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) { 823 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) {
639 set_opencont ($::CONN, 0, "Floor"); 824 set_opencont ($::CONN, 0, "Floor");
640 825
641 } elsif ($item->{flags} & F_OPEN) { 826 } elsif ($item->{flags} & F_OPEN) {
642 set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item); 827 set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item);
828
643 } else { 829 } else {
644 if ($item->{container} == 0) { 830 $self->{update_container}{$item->{container}}++;
645 update_floorbox;
646 update_container (0); 831 $self->update_containers;
647 } elsif ($item->{container} == $self->{player}{tag}) {
648 $::INV->set_items ($self->{container}{$item->{container}})
649 }
650 } 832 }
651} 833}
652 834
653sub player_update { 835sub player_update {
654 my ($self, $player) = @_; 836 my ($self, $player) = @_;
655
656 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000); 837 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000);
657} 838}
658 839
659sub update_server_info { 840sub update_server_info {
660 my ($self) = @_; 841 my ($self) = @_;
756 937
757 return unless $text =~ /\S/; 938 return unless $text =~ /\S/;
758 939
759 $entry->set_text (""); 940 $entry->set_text ("");
760 $this->send ($text); 941 $this->send ($text);
942
943 0
761 }, 944 },
762 ); 945 );
763 946
764 $vbox->add ($self->{options} = new CFClient::UI::VBox); 947 $vbox->add ($self->{options} = new CFClient::UI::VBox);
765 948
766 $self->{close_button} = new CFClient::UI::Button 949 $self->{bye_button} = new CFClient::UI::Button
767 text => "Bye (close)", 950 text => "Bye (close)",
768 tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.", 951 tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.",
769 on_activate => sub { $this->destroy }, 952 on_activate => sub { $this->destroy; 0 },
770 ; 953 ;
771 954
772 $self->update_options; 955 $self->update_options;
773 956
774 $self->{token} = $self->{conn}->ext_token; 957 $self->{token} = $self->{conn}->ext_token;
775 $self->{conn}->connect_ext ($self->{token} => sub { $this->feed (@_) }); 958 $self->{conn}->connect_ext ($self->{token} => sub { $this->feed (@_) });
776 $self->{conn}->send ("ext npc_dialog_begin $self->{token} $self->{dx} $self->{dy}"); 959 $self->{conn}->send ("ext npc_dialog_begin $self->{token} $self->{dx} $self->{dy}");
777 960
778 $self->{entry}->focus_in; 961 $self->{entry}->grab_focus;
779 962
780 $self->{textview}->add_paragraph ([1, 1, 0, 1], "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n"); 963 $self->{textview}->add_paragraph ([1, 1, 0, 1], "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n");
781 964
782 $self->show; 965 $self->show;
783 $self 966 $self
787 my ($self) = @_; 970 my ($self) = @_;
788 971
789 Scalar::Util::weaken $self; 972 Scalar::Util::weaken $self;
790 973
791 $self->{options}->clear; 974 $self->{options}->clear;
792 $self->{options}->add ($self->{close_button}); 975 $self->{options}->add ($self->{bye_button});
793 976
794 for my $kw (sort keys %{ $self->{kw} }) { 977 for my $kw (sort keys %{ $self->{kw} }) {
795 $self->{options}->add (new CFClient::UI::Button 978 $self->{options}->add (new CFClient::UI::Button
796 text => $kw, 979 text => $kw,
797 on_activate => sub { 980 on_activate => sub {
798 $self->send ($kw); 981 $self->send ($kw);
982 0
799 }, 983 },
800 ); 984 );
801 } 985 }
802} 986}
803 987
804sub feed { 988sub feed {
805 my ($self, $data) = @_; 989 my ($self, $data) = @_;
806 990
991 Scalar::Util::weaken $self;
992
807 my ($type, $msg) = split / /, $data, 2; 993 my ($type, $msg) = split / /, $data, 2;
808 994
809 if ($type eq "msg") { 995 if ($type eq "msg") {
810 my ($msg, @kw) = split /\x00/, $msg; 996 my ($msg, @kw) = split /\x00/, $msg;
997
998 utf8::decode $_ for ($msg, @kw);
999
811 $self->{kw}{$_} = 1 for @kw; 1000 $self->{kw}{$_} = 1 for @kw;
812 1001
813 $msg = CFClient::UI::Label::escape $msg; 1002 $msg = "\n" . CFClient::UI::Label::escape $msg;
814 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} }; 1003 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} };
815 $msg =~ s/($match)/<span foreground='#c0c0ff' underline='none'>$1<\/span>/gi; # underline when http-ready, huh. 1004 my @link;
1005 $msg =~ s{
1006 ($match)
1007 }{
1008 my $kw = $1;
1009
1010 push @link, new CFClient::UI::Label
1011 markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>",
1012 can_hover => 1,
1013 can_events => 1,
1014 padding_x => 0,
1015 padding_y => 0,
1016 on_button_up => sub {
1017 $self->send ($kw);
1018 };
1019
1020 chr 0xfffc
1021 }giex;
816 1022
817 $self->{textview}->add_paragraph ([1, 1, 1, 1], "\n$msg"); 1023 $self->{textview}->add_paragraph ([1, 1, 1, 1], [$msg, @link]);
1024 $self->{textview}->scroll_to_bottom;
818 $self->update_options; 1025 $self->update_options;
819 } else { 1026 } else {
820 $self->destroy; 1027 $self->destroy;
821 } 1028 }
822 1029
824} 1031}
825 1032
826sub send { 1033sub send {
827 my ($self, $msg) = @_; 1034 my ($self, $msg) = @_;
828 1035
1036 $self->{textview}->add_paragraph ([1, 1, 0, 1], "\n" . CFClient::UI::Label::escape $msg);
1037 $self->{textview}->scroll_to_bottom;
1038
1039 utf8::encode $msg;
829 $self->{conn}->send ("ext npc_dialog_tell $self->{token} $msg"); 1040 $self->{conn}->send ("ext npc_dialog_tell $self->{token} $msg");
830 $self->{textview}->add_paragraph ([1, 1, 0, 1], "\n" . CFClient::UI::Label::escape $msg);
831} 1041}
832 1042
833sub destroy { 1043sub destroy {
834 my ($self) = @_; 1044 my ($self) = @_;
835 1045
836 #Carp::cluck "debug\n";#d# #todo# enable: destroyx gets called twice because scalar keys {} is 1 1046 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1
837 1047
1048 $self->{conn}->send ("ext npc_dialog_end $self->{token}") if $self->{token};
838 delete $self->{conn}{npc_dialog}; 1049 delete $self->{conn}{npc_dialog};
839 $self->{conn}->disconnect_ext ($self->{token}); 1050 $self->{conn}->disconnect_ext ($self->{token});
840 1051
841 $self->SUPER::destroy; 1052 $self->SUPER::destroy;
842} 1053}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines