… | |
… | |
7 | |
7 | |
8 | use CFClient; |
8 | use CFClient; |
9 | use CFClient::UI; |
9 | use CFClient::UI; |
10 | |
10 | |
11 | use base 'Crossfire::Protocol::Base'; |
11 | use base 'Crossfire::Protocol::Base'; |
12 | |
|
|
13 | our %open_logs; |
|
|
14 | |
12 | |
15 | sub new { |
13 | sub 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 | |
66 | sub logprint { |
64 | sub 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 | |
|
|
86 | sub _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 | |
|
|
97 | sub _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 |
|
|
115 | my @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 | |
90 | sub stats_update { |
155 | sub 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 | |
|
|
185 | my %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 | |
|
|
206 | sub 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 | |
103 | sub user_send { |
290 | sub 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 | |
115 | sub start_record { |
302 | sub start_record { |
116 | my ($self) = @_; |
303 | my ($self) = @_; |
117 | |
304 | |
… | |
… | |
160 | |
347 | |
161 | $self->flush_map; |
348 | $self->flush_map; |
162 | delete $self->{neigh_map}; |
349 | delete $self->{neigh_map}; |
163 | |
350 | |
164 | $self->{map}->clear; |
351 | $self->{map}->clear; |
|
|
352 | delete $self->{map_widget}{magicmap}; |
165 | } |
353 | } |
166 | |
354 | |
167 | |
355 | |
168 | sub load_map($$$) { |
356 | sub load_map($$$) { |
169 | my ($self, $hash, $x, $y) = @_; |
357 | my ($self, $hash, $x, $y) = @_; |
… | |
… | |
344 | undef |
532 | undef |
345 | }; |
533 | }; |
346 | } |
534 | } |
347 | |
535 | |
348 | sub face_update { |
536 | sub face_update { |
349 | my ($self, $facenum, $face) = @_; |
537 | my ($self, $facenum, $face, $changed) = @_; |
350 | |
538 | |
351 | $self->{tilecache}->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes |
539 | $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed; |
352 | |
540 | |
353 | $self->set_texture ($face->{id} => delete $face->{image}); |
541 | $self->set_texture ($face->{id} => delete $face->{image}); |
354 | } |
542 | } |
355 | |
543 | |
356 | sub set_texture { |
544 | sub set_texture { |
… | |
… | |
413 | |
601 | |
414 | $text = CFClient::UI::Label::escape $text; |
602 | $text = CFClient::UI::Label::escape $text; |
415 | $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; |
603 | $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; |
416 | $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; |
604 | $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; |
417 | |
605 | |
418 | $self->{logview}->add_paragraph ($color[$color], |
606 | $self->{logview}->add_paragraph ($color[$color], $_) |
419 | join "\n", map "$time $_", split /\n/, $text); |
607 | for map "$time $_", split /\n/, $text; |
|
|
608 | $self->{logview}->scroll_to_bottom; |
420 | |
609 | |
421 | $self->{statusbox}->add ($text, |
610 | $self->{statusbox}->add ($text, |
422 | group => $text, |
611 | group => $text, |
423 | fg => $color[$color], |
612 | fg => $color[$color], |
424 | timeout => $color >= 2 ? 60 : 10, |
613 | timeout => $color >= 2 ? 60 : 10, |
… | |
… | |
438 | # 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 |
439 | $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g; |
628 | $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g; |
440 | $spell->{message} =~ s/\n+$//; |
629 | $spell->{message} =~ s/\n+$//; |
441 | $spell->{message} ||= "Server did not provide a description for this spell."; |
630 | $spell->{message} ||= "Server did not provide a description for this spell."; |
442 | |
631 | |
443 | $::SETUP_SPELLS->add_spell ($spell); |
632 | $::SPELL_PAGE->add_spell ($spell); |
444 | |
633 | |
445 | $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}); |
446 | $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}); |
447 | } |
636 | } |
448 | |
637 | |
449 | sub spell_delete { |
638 | sub spell_delete { |
450 | my ($self, $spell) = @_; |
639 | my ($self, $spell) = @_; |
|
|
640 | |
451 | $::SETUP_SPELLS->remove_spell ($spell); |
641 | $::SPELL_PAGE->remove_spell ($spell); |
452 | } |
642 | } |
453 | |
643 | |
454 | sub addme_success { |
644 | sub addme_success { |
455 | my ($self) = @_; |
645 | my ($self) = @_; |
456 | |
646 | |
… | |
… | |
523 | return unless $::CONN; |
713 | return unless $::CONN; |
524 | |
714 | |
525 | $::FLOORBOX->clear; |
715 | $::FLOORBOX->clear; |
526 | |
716 | |
527 | my $row; |
717 | my $row; |
528 | for (@{ $::CONN->{container}{0} }) { |
718 | for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{0} }) { |
529 | if ($row < 6) { |
719 | if ($row < 6) { |
530 | local $_->{face_widget}; # hack to force recreation of widget |
720 | local $_->{face_widget}; # hack to force recreation of widget |
531 | local $_->{desc_widget}; # hack to force recreation of widget |
721 | local $_->{desc_widget}; # hack to force recreation of widget |
532 | CFClient::Item::update_widgets $_; |
722 | CFClient::Item::update_widgets $_; |
533 | |
723 | |
… | |
… | |
536 | |
726 | |
537 | $row++; |
727 | $row++; |
538 | } else { |
728 | } else { |
539 | $::FLOORBOX->add (1, $row, new CFClient::UI::Button |
729 | $::FLOORBOX->add (1, $row, new CFClient::UI::Button |
540 | text => "More...", |
730 | text => "More...", |
541 | on_activate => sub { $::INV_WINDOW->toggle_visibility }, |
731 | on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 }, |
542 | ); |
732 | ); |
543 | last; |
733 | last; |
544 | } |
734 | } |
545 | } |
735 | } |
546 | }); |
736 | }); |
… | |
… | |
561 | tooltip => "Close the currently open container (if one is open)", |
751 | tooltip => "Close the currently open container (if one is open)", |
562 | on_activate => sub { |
752 | on_activate => sub { |
563 | $::CONN->send ("apply $tag") # $::CONN->{open_container}") |
753 | $::CONN->send ("apply $tag") # $::CONN->{open_container}") |
564 | if $tag != 0; |
754 | if $tag != 0; |
565 | #if $CONN->{open_container} != 0; |
755 | #if $CONN->{open_container} != 0; |
|
|
756 | 0 |
566 | }, |
757 | }, |
567 | ); |
758 | ); |
568 | } |
759 | } |
569 | |
760 | |
570 | $::INVR->set_items ($conn->{container}{$tag}); |
761 | $::INVR->set_items ($conn->{container}{$tag}); |
571 | } |
762 | } |
572 | |
763 | |
573 | sub update_container { |
764 | sub update_containers { |
574 | my ($tag) = @_; |
765 | my ($self) = @_; |
575 | |
766 | |
576 | $::INVR->set_items ($::CONN->{container}{$::CONN->{open_container}}) |
767 | $CFClient::UI::ROOT->on_refresh ("update_containers_$self" => sub { |
|
|
768 | my $todo = delete $self->{update_container} |
|
|
769 | or return; |
|
|
770 | |
|
|
771 | for my $tag (keys %$todo) { |
|
|
772 | if ($tag == 0) { |
|
|
773 | update_floorbox; |
|
|
774 | $::INVR->set_items ($self->{container}{0}) |
577 | if $tag == $::CONN->{open_container}; |
775 | if $tag == $self->{open_container}; |
|
|
776 | } elsif ($tag == $self->{player}{tag}) { |
|
|
777 | $::INV->set_items ($self->{container}{$tag}) |
|
|
778 | } else { |
|
|
779 | $::INVR->set_items ($self->{container}{$tag}) |
|
|
780 | if $tag == $self->{open_container}; |
|
|
781 | } |
|
|
782 | } |
|
|
783 | }); |
578 | } |
784 | } |
579 | |
785 | |
580 | sub container_add { |
786 | sub container_add { |
581 | my ($self, $tag, $items) = @_; |
787 | my ($self, $tag, $items) = @_; |
582 | |
788 | |
583 | #d# print "container_add: container $tag ($self->{player}{tag})\n"; |
789 | $self->{update_container}{$tag}++; |
584 | |
790 | $self->update_containers; |
585 | if ($tag == 0) { |
|
|
586 | update_floorbox; |
|
|
587 | update_container (0); |
|
|
588 | } elsif ($tag == $self->{player}{tag}) { |
|
|
589 | $::INV->set_items ($self->{container}{$self->{player}{tag}}) |
|
|
590 | } else { |
|
|
591 | update_container ($tag); |
|
|
592 | } |
|
|
593 | |
|
|
594 | # $self-<{player}{tag} => player inv |
|
|
595 | #use PApp::Util; warn PApp::Util::dumpval $self->{container}{$self->{player}{tag}}; |
|
|
596 | } |
791 | } |
597 | |
792 | |
598 | sub container_clear { |
793 | sub container_clear { |
599 | my ($self, $tag) = @_; |
794 | my ($self, $tag) = @_; |
600 | |
795 | |
601 | #d# print "container_clear: container $tag ($self->{player}{tag})\n"; |
796 | $self->{update_container}{$tag}++; |
602 | |
797 | $self->update_containers; |
603 | if ($tag == 0) { |
|
|
604 | update_floorbox; |
|
|
605 | update_container (0); |
|
|
606 | } elsif ($tag == $self->{player}{tag}) { |
|
|
607 | $::INV->set_items ($self->{container}{$tag}) |
|
|
608 | } else { |
|
|
609 | update_container ($tag); |
|
|
610 | } |
|
|
611 | |
|
|
612 | # use PApp::Util; warn PApp::Util::dumpval $self->{container}{0}; |
|
|
613 | } |
798 | } |
614 | |
799 | |
615 | sub item_delete { |
800 | sub item_delete { |
616 | my ($self, @items) = @_; |
801 | my ($self, @items) = @_; |
617 | |
802 | |
|
|
803 | $self->{update_container}{$_->{container}}++ |
618 | for (@items) { |
804 | for @items; |
619 | #d# print "item_delete: $_->{tag} from $_->{container} ($self->{player}{tag})\n"; |
|
|
620 | |
|
|
621 | if ($_->{container} == 0) { |
|
|
622 | update_floorbox; |
|
|
623 | update_container ($_->{tag}); |
|
|
624 | } elsif ($_->{container} == $self->{player}{tag}) { |
|
|
625 | $::INV->set_items ($self->{container}{$self->{player}{tag}}) |
|
|
626 | } else { |
|
|
627 | update_container ($_->{container}); |
|
|
628 | } |
|
|
629 | } |
805 | |
|
|
806 | $self->update_containers; |
630 | } |
807 | } |
631 | |
808 | |
632 | sub item_update { |
809 | sub item_update { |
633 | my ($self, $item) = @_; |
810 | my ($self, $item) = @_; |
634 | |
811 | |
635 | #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n"; |
812 | #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n"; |
636 | |
813 | |
637 | if ($item->{tag} == $self->{player}{tag}) { |
|
|
638 | $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $item->{weight} / 1000); |
|
|
639 | return; |
|
|
640 | } |
|
|
641 | |
|
|
642 | CFClient::Item::update_widgets $item; |
814 | CFClient::Item::update_widgets $item; |
643 | |
815 | |
644 | if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) { |
816 | if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) { |
645 | set_opencont ($::CONN, 0, "Floor"); |
817 | set_opencont ($::CONN, 0, "Floor"); |
646 | |
818 | |
647 | } elsif ($item->{flags} & F_OPEN) { |
819 | } elsif ($item->{flags} & F_OPEN) { |
648 | set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item); |
820 | set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item); |
|
|
821 | |
649 | } else { |
822 | } else { |
650 | if ($item->{container} == 0) { |
823 | $self->{update_container}{$item->{container}}++; |
651 | update_floorbox; |
|
|
652 | update_container (0); |
824 | $self->update_containers; |
653 | } elsif ($item->{container} == $self->{player}{tag}) { |
|
|
654 | $::INV->set_items ($self->{container}{$item->{container}}) |
|
|
655 | } |
|
|
656 | } |
825 | } |
657 | } |
826 | } |
658 | |
827 | |
659 | sub player_update { |
828 | sub player_update { |
660 | my ($self, $player) = @_; |
829 | my ($self, $player) = @_; |
661 | |
|
|
662 | $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000); |
830 | $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000); |
663 | } |
831 | } |
664 | |
832 | |
665 | sub update_server_info { |
833 | sub update_server_info { |
666 | my ($self) = @_; |
834 | my ($self) = @_; |
… | |
… | |
762 | |
930 | |
763 | return unless $text =~ /\S/; |
931 | return unless $text =~ /\S/; |
764 | |
932 | |
765 | $entry->set_text (""); |
933 | $entry->set_text (""); |
766 | $this->send ($text); |
934 | $this->send ($text); |
|
|
935 | |
|
|
936 | 0 |
767 | }, |
937 | }, |
768 | ); |
938 | ); |
769 | |
939 | |
770 | $vbox->add ($self->{options} = new CFClient::UI::VBox); |
940 | $vbox->add ($self->{options} = new CFClient::UI::VBox); |
771 | |
941 | |
772 | $self->{close_button} = new CFClient::UI::Button |
942 | $self->{bye_button} = new CFClient::UI::Button |
773 | text => "Bye (close)", |
943 | text => "Bye (close)", |
774 | tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.", |
944 | tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.", |
775 | on_activate => sub { $this->destroy }, |
945 | on_activate => sub { $this->destroy; 0 }, |
776 | ; |
946 | ; |
777 | |
947 | |
778 | $self->update_options; |
948 | $self->update_options; |
779 | |
949 | |
780 | $self->{token} = $self->{conn}->ext_token; |
950 | $self->{token} = $self->{conn}->ext_token; |
781 | $self->{conn}->connect_ext ($self->{token} => sub { $this->feed (@_) }); |
951 | $self->{conn}->connect_ext ($self->{token} => sub { $this->feed (@_) }); |
782 | $self->{conn}->send ("ext npc_dialog_begin $self->{token} $self->{dx} $self->{dy}"); |
952 | $self->{conn}->send ("ext npc_dialog_begin $self->{token} $self->{dx} $self->{dy}"); |
783 | |
953 | |
784 | $self->{entry}->focus_in; |
954 | $self->{entry}->grab_focus; |
785 | |
955 | |
786 | $self->{textview}->add_paragraph ([1, 1, 0, 1], "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n"); |
956 | $self->{textview}->add_paragraph ([1, 1, 0, 1], "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n"); |
787 | |
957 | |
788 | $self->show; |
958 | $self->show; |
789 | $self |
959 | $self |
… | |
… | |
793 | my ($self) = @_; |
963 | my ($self) = @_; |
794 | |
964 | |
795 | Scalar::Util::weaken $self; |
965 | Scalar::Util::weaken $self; |
796 | |
966 | |
797 | $self->{options}->clear; |
967 | $self->{options}->clear; |
798 | $self->{options}->add ($self->{close_button}); |
968 | $self->{options}->add ($self->{bye_button}); |
799 | |
969 | |
800 | for my $kw (sort keys %{ $self->{kw} }) { |
970 | for my $kw (sort keys %{ $self->{kw} }) { |
801 | $self->{options}->add (new CFClient::UI::Button |
971 | $self->{options}->add (new CFClient::UI::Button |
802 | text => $kw, |
972 | text => $kw, |
803 | on_activate => sub { |
973 | on_activate => sub { |
804 | $self->send ($kw); |
974 | $self->send ($kw); |
|
|
975 | 0 |
805 | }, |
976 | }, |
806 | ); |
977 | ); |
807 | } |
978 | } |
808 | } |
979 | } |
809 | |
980 | |
810 | sub feed { |
981 | sub feed { |
811 | my ($self, $data) = @_; |
982 | my ($self, $data) = @_; |
|
|
983 | |
|
|
984 | Scalar::Util::weaken $self; |
812 | |
985 | |
813 | my ($type, $msg) = split / /, $data, 2; |
986 | my ($type, $msg) = split / /, $data, 2; |
814 | |
987 | |
815 | if ($type eq "msg") { |
988 | if ($type eq "msg") { |
816 | my ($msg, @kw) = split /\x00/, $msg; |
989 | my ($msg, @kw) = split /\x00/, $msg; |
817 | $self->{kw}{$_} = 1 for @kw; |
990 | $self->{kw}{$_} = 1 for @kw; |
818 | |
991 | |
819 | $msg = CFClient::UI::Label::escape $msg; |
992 | $msg = "\n" . CFClient::UI::Label::escape $msg; |
820 | my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} }; |
993 | my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} }; |
821 | $msg =~ s/($match)/<span foreground='#c0c0ff' underline='none'>$1<\/span>/gi; # underline when http-ready, huh. |
994 | my @link; |
|
|
995 | $msg =~ s{ |
|
|
996 | ($match) |
|
|
997 | }{ |
|
|
998 | my $kw = $1; |
|
|
999 | |
|
|
1000 | push @link, new CFClient::UI::Label |
|
|
1001 | markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>", |
|
|
1002 | can_hover => 1, |
|
|
1003 | can_events => 1, |
|
|
1004 | padding_x => 0, |
|
|
1005 | padding_y => 0, |
|
|
1006 | on_button_up => sub { |
|
|
1007 | $self->send ($kw); |
|
|
1008 | }; |
|
|
1009 | |
|
|
1010 | chr 0xfffc |
|
|
1011 | }giex; |
822 | |
1012 | |
823 | $self->{textview}->add_paragraph ([1, 1, 1, 1], "\n$msg"); |
1013 | $self->{textview}->add_paragraph ([1, 1, 1, 1], [$msg, @link]); |
|
|
1014 | $self->{textview}->scroll_to_bottom; |
824 | $self->update_options; |
1015 | $self->update_options; |
825 | } else { |
1016 | } else { |
826 | $self->destroy; |
1017 | $self->destroy; |
827 | } |
1018 | } |
828 | |
1019 | |
… | |
… | |
832 | sub send { |
1023 | sub send { |
833 | my ($self, $msg) = @_; |
1024 | my ($self, $msg) = @_; |
834 | |
1025 | |
835 | $self->{conn}->send ("ext npc_dialog_tell $self->{token} $msg"); |
1026 | $self->{conn}->send ("ext npc_dialog_tell $self->{token} $msg"); |
836 | $self->{textview}->add_paragraph ([1, 1, 0, 1], "\n" . CFClient::UI::Label::escape $msg); |
1027 | $self->{textview}->add_paragraph ([1, 1, 0, 1], "\n" . CFClient::UI::Label::escape $msg); |
|
|
1028 | $self->{textview}->scroll_to_bottom; |
837 | } |
1029 | } |
838 | |
1030 | |
839 | sub destroy { |
1031 | sub destroy { |
840 | my ($self) = @_; |
1032 | my ($self) = @_; |
841 | |
1033 | |
842 | #Carp::cluck "debug\n";#d# #todo# enable: destroyx gets called twice because scalar keys {} is 1 |
1034 | #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1 |
843 | |
1035 | |
|
|
1036 | $self->{conn}->send ("ext npc_dialog_end $self->{token}") if $self->{token}; |
844 | delete $self->{conn}{npc_dialog}; |
1037 | delete $self->{conn}{npc_dialog}; |
845 | $self->{conn}->disconnect_ext ($self->{token}); |
1038 | $self->{conn}->disconnect_ext ($self->{token}); |
846 | |
1039 | |
847 | $self->SUPER::destroy; |
1040 | $self->SUPER::destroy; |
848 | } |
1041 | } |