… | |
… | |
3 | use utf8; |
3 | use utf8; |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use Crossfire::Protocol::Constants; |
6 | use Crossfire::Protocol::Constants; |
7 | |
7 | |
|
|
8 | use CFClient; |
8 | use CFClient::UI; |
9 | use CFClient::UI; |
9 | |
10 | |
10 | use base 'Crossfire::Protocol::Base'; |
11 | use base 'Crossfire::Protocol::Base'; |
11 | |
12 | |
12 | sub new { |
13 | sub new { |
… | |
… | |
14 | |
15 | |
15 | my $self = $class->SUPER::new (@_); |
16 | my $self = $class->SUPER::new (@_); |
16 | |
17 | |
17 | $self->{map_widget}->clr_commands; |
18 | $self->{map_widget}->clr_commands; |
18 | |
19 | |
19 | my $parser = new Pod::POM; |
20 | my $cmd_help = CFClient::load_pod CFClient::find_rcfile "pod/command_help.pod", command_help => 1, sub { |
20 | my $pod = $parser->parse_file (CFClient::find_rcfile "pod/command_help.pod"); |
21 | my ($pom) = @_; |
21 | |
22 | |
|
|
23 | my @cmd_help; |
|
|
24 | |
22 | for my $head2 ($pod->head1->[-2]->head2) { |
25 | for my $head2 ($pom->head1->[-2]->head2) { |
23 | $head2->title =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x |
26 | $head2->title =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x |
24 | or next; |
27 | or next; |
25 | |
28 | |
26 | my $cmd = $1; |
29 | my $cmd = $1; |
27 | my @args = split /\|/, $2; |
30 | my @args = split /\|/, $2; |
28 | @args = (".*") unless @args; |
31 | @args = (".*") unless @args; |
29 | |
32 | |
30 | $_ = $_ eq ".*" ? "" : " $_" |
33 | $_ = $_ eq ".*" ? "" : " $_" |
31 | for @args; |
34 | for @args; |
32 | |
35 | |
33 | my $text = CFClient::pod_to_pango $head2->content; |
36 | my $text = CFClient::pod_to_pango $head2->content; |
34 | |
37 | |
35 | $self->{map_widget}->add_command ("$cmd$_", $text) |
38 | push @cmd_help, ["$cmd$_", $text] |
36 | for sort { (length $a) <=> (length $b) } |
39 | for sort { (length $a) <=> (length $b) } |
37 | @args; |
40 | @args; |
|
|
41 | } |
|
|
42 | |
|
|
43 | \@cmd_help |
38 | } |
44 | }; |
|
|
45 | |
|
|
46 | $self->{map_widget}->add_command (@$_) |
|
|
47 | for @$cmd_help; |
39 | |
48 | |
40 | $self->{noface} = new_from_file CFClient::Texture |
49 | $self->{noface} = new_from_file CFClient::Texture |
41 | CFClient::find_rcfile "noface.png", minify => 1, mipmap => 1; |
50 | CFClient::find_rcfile "noface.png", minify => 1, mipmap => 1; |
42 | |
51 | |
43 | $self->{open_container} = 0; |
52 | $self->{open_container} = 0; |
… | |
… | |
50 | $self->{mapcache} = CFClient::db_table "mapcache_$self->{host}_$self->{port}"; |
59 | $self->{mapcache} = CFClient::db_table "mapcache_$self->{host}_$self->{port}"; |
51 | |
60 | |
52 | $self |
61 | $self |
53 | } |
62 | } |
54 | |
63 | |
|
|
64 | sub logprint { |
|
|
65 | my ($self, @a) = @_; |
|
|
66 | |
|
|
67 | $self->{log_fh} ||= do { |
|
|
68 | my $path = "$Crossfire::VARDIR/log.$self->{host}"; |
|
|
69 | |
|
|
70 | open my $fh, ">>:utf8", $path |
|
|
71 | or die "Couldn't open logfile $path: $!"; |
|
|
72 | |
|
|
73 | $fh->autoflush (1); |
|
|
74 | |
|
|
75 | $fh; |
|
|
76 | }; |
|
|
77 | |
|
|
78 | my ($sec, $min, $hour, $mday, $mon, $year) = localtime time; |
|
|
79 | |
|
|
80 | my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d", |
|
|
81 | $year + 1900, $mon + 1, $mday, $hour, $min, $sec; |
|
|
82 | |
|
|
83 | print {$self->{log_fh}} "$ts ", @a, "\n"; |
|
|
84 | } |
|
|
85 | |
|
|
86 | sub _stat_numdiff { |
|
|
87 | my ($self, $name, $old, $new) = @_; |
|
|
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 | ); |
|
|
154 | |
55 | sub stats_update { |
155 | sub stats_update { |
56 | my ($self, $stats) = @_; |
156 | my ($self, $stats) = @_; |
57 | |
157 | |
58 | if (my $exp = $stats->{+CS_STAT_EXP64}) { |
158 | my $prev = $self->{prev_stats} || { }; |
59 | 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; |
60 | $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); |
61 | if exists $self->{prev_exp} && $diff; |
|
|
62 | $self->{prev_exp} = $exp; |
|
|
63 | } |
171 | } |
64 | |
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 | |
65 | ::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 | } |
66 | } |
288 | } |
67 | |
289 | |
68 | sub user_send { |
290 | sub user_send { |
69 | my ($self, $command) = @_; |
291 | my ($self, $command) = @_; |
70 | |
292 | |
71 | if ($self->{record}) { |
293 | if ($self->{record}) { |
72 | push @{$self->{record}}, $command; |
294 | push @{$self->{record}}, $command; |
73 | } |
295 | } |
74 | |
296 | |
|
|
297 | $self->logprint ("send: ", $command); |
75 | $self->send_command ($command); |
298 | $self->send_command ($command); |
76 | ::status $command; |
299 | ::status ($command); |
77 | } |
300 | } |
78 | |
301 | |
79 | sub start_record { |
302 | sub start_record { |
80 | my ($self) = @_; |
303 | my ($self) = @_; |
81 | |
304 | |
… | |
… | |
96 | sub feed_map1a { |
319 | sub feed_map1a { |
97 | my ($self, $data) = @_; |
320 | my ($self, $data) = @_; |
98 | |
321 | |
99 | $self->{map}->map1a_update ($data); |
322 | $self->{map}->map1a_update ($data); |
100 | $self->{map_widget}->update; |
323 | $self->{map_widget}->update; |
|
|
324 | } |
|
|
325 | |
|
|
326 | sub magicmap { |
|
|
327 | my ($self, $w, $h, $x, $y, $data) = @_; |
|
|
328 | |
|
|
329 | $self->{map_widget}->set_magicmap ($w, $h, $x, $y, $data); |
101 | } |
330 | } |
102 | |
331 | |
103 | sub flush_map { |
332 | sub flush_map { |
104 | my ($self) = @_; |
333 | my ($self) = @_; |
105 | |
334 | |
… | |
… | |
118 | |
347 | |
119 | $self->flush_map; |
348 | $self->flush_map; |
120 | delete $self->{neigh_map}; |
349 | delete $self->{neigh_map}; |
121 | |
350 | |
122 | $self->{map}->clear; |
351 | $self->{map}->clear; |
|
|
352 | delete $self->{map_widget}{magicmap}; |
123 | } |
353 | } |
124 | |
354 | |
125 | |
355 | |
126 | sub load_map($$$) { |
356 | sub load_map($$$) { |
127 | my ($self, $hash, $x, $y) = @_; |
357 | my ($self, $hash, $x, $y) = @_; |
… | |
… | |
270 | # I love transactions |
500 | # I love transactions |
271 | for (1..100) { |
501 | for (1..100) { |
272 | my $txn = $CFClient::DB_ENV->txn_begin; |
502 | my $txn = $CFClient::DB_ENV->txn_begin; |
273 | my $status = $self->{facemap}->db_get (id => $id); |
503 | my $status = $self->{facemap}->db_get (id => $id); |
274 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
504 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
275 | $id = ($id || 16) + 1; |
505 | $id = ($id || 64) + 1; |
276 | if ($self->{facemap}->put (id => $id) == 0 |
506 | if ($self->{facemap}->put (id => $id) == 0 |
277 | && $self->{facemap}->put ($hash => $id) == 0) { |
507 | && $self->{facemap}->put ($hash => $id) == 0) { |
278 | $txn->txn_commit; |
508 | $txn->txn_commit; |
279 | |
509 | |
280 | goto gotid; |
510 | goto gotid; |
… | |
… | |
302 | undef |
532 | undef |
303 | }; |
533 | }; |
304 | } |
534 | } |
305 | |
535 | |
306 | sub face_update { |
536 | sub face_update { |
307 | my ($self, $facenum, $face) = @_; |
537 | my ($self, $facenum, $face, $changed) = @_; |
308 | |
538 | |
309 | $self->{tilecache}->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes |
539 | $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed; |
310 | |
540 | |
311 | $self->set_texture ($face->{id} => delete $face->{image}); |
541 | $self->set_texture ($face->{id} => delete $face->{image}); |
312 | } |
542 | } |
313 | |
543 | |
314 | sub set_texture { |
544 | sub set_texture { |
… | |
… | |
338 | my ($self, $flags, $prompt) = @_; |
568 | my ($self, $flags, $prompt) = @_; |
339 | |
569 | |
340 | $prompt = $LAST_QUERY unless length $prompt; |
570 | $prompt = $LAST_QUERY unless length $prompt; |
341 | $LAST_QUERY = $prompt; |
571 | $LAST_QUERY = $prompt; |
342 | |
572 | |
343 | my $dialog = new CFClient::UI::FancyFrame |
573 | $self->{query}-> ($self, $flags, $prompt); |
344 | x => "center", |
|
|
345 | y => "center", |
|
|
346 | title => "Query", |
|
|
347 | child => my $vbox = new CFClient::UI::VBox, |
|
|
348 | ; |
|
|
349 | |
|
|
350 | $vbox->add (new CFClient::UI::Label |
|
|
351 | max_w => $::WIDTH * 0.4, |
|
|
352 | ellipsise => 0, |
|
|
353 | text => $prompt); |
|
|
354 | |
|
|
355 | if ($flags & CS_QUERY_YESNO) { |
|
|
356 | $vbox->add (my $hbox = new CFClient::UI::HBox); |
|
|
357 | $hbox->add (new CFClient::UI::Button |
|
|
358 | text => "No", |
|
|
359 | on_activate => sub { |
|
|
360 | $self->send ("reply n"); |
|
|
361 | $dialog->destroy; |
|
|
362 | $self->{map_widget}->focus_in; |
|
|
363 | } |
|
|
364 | ); |
|
|
365 | $hbox->add (new CFClient::UI::Button |
|
|
366 | text => "Yes", |
|
|
367 | on_activate => sub { |
|
|
368 | $self->send ("reply y"); |
|
|
369 | $dialog->destroy; |
|
|
370 | }, |
|
|
371 | ); |
|
|
372 | |
|
|
373 | $dialog->focus_in; |
|
|
374 | |
|
|
375 | } elsif ($flags & CS_QUERY_SINGLECHAR) { |
|
|
376 | $dialog->{tooltip} = "Press a key (click on the entry to make sure it has keyboard focus)"; |
|
|
377 | $vbox->add (my $entry = new CFClient::UI::Entry |
|
|
378 | on_changed => sub { |
|
|
379 | $self->send ("reply $_[1]"); |
|
|
380 | $dialog->destroy; |
|
|
381 | }, |
|
|
382 | ); |
|
|
383 | |
|
|
384 | $entry->focus_in; |
|
|
385 | |
|
|
386 | } else { |
|
|
387 | $dialog->{tooltip} = "Enter the reply and press return (click on the entry to make sure it has keyboard focus)"; |
|
|
388 | |
|
|
389 | $vbox->add (my $entry = new CFClient::UI::Entry |
|
|
390 | $flags & CS_QUERY_HIDEINPUT ? (hiddenchar => "*") : (), |
|
|
391 | on_activate => sub { |
|
|
392 | $self->send ("reply $_[1]"); |
|
|
393 | $dialog->destroy; |
|
|
394 | }, |
|
|
395 | ); |
|
|
396 | |
|
|
397 | $entry->focus_in; |
|
|
398 | } |
|
|
399 | |
|
|
400 | $dialog->show; |
|
|
401 | } |
574 | } |
402 | |
575 | |
403 | sub drawinfo { |
576 | sub drawinfo { |
404 | my ($self, $color, $text) = @_; |
577 | my ($self, $color, $text) = @_; |
405 | |
578 | |
… | |
… | |
412 | [0.11, 0.56, 1.00], |
585 | [0.11, 0.56, 1.00], |
413 | [0.93, 0.46, 0.00], |
586 | [0.93, 0.46, 0.00], |
414 | [0.18, 0.54, 0.34], |
587 | [0.18, 0.54, 0.34], |
415 | [0.56, 0.73, 0.56], |
588 | [0.56, 0.73, 0.56], |
416 | [0.80, 0.80, 0.80], |
589 | [0.80, 0.80, 0.80], |
417 | [0.55, 0.41, 0.13], |
590 | [0.75, 0.61, 0.20], |
418 | [0.99, 0.77, 0.26], |
591 | [0.99, 0.77, 0.26], |
419 | [0.74, 0.65, 0.41], |
592 | [0.74, 0.65, 0.41], |
420 | ); |
593 | ); |
421 | |
594 | |
|
|
595 | $self->logprint ("info: ", $text); |
|
|
596 | |
422 | my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0]; |
597 | my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0]; |
|
|
598 | |
|
|
599 | # try to create single paragraphs of multiple lines sent by the server |
|
|
600 | $text =~ s/(?<=\S)\n(?=\w)/ /g; |
423 | |
601 | |
424 | $text = CFClient::UI::Label::escape $text; |
602 | $text = CFClient::UI::Label::escape $text; |
425 | $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; |
603 | $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; |
426 | $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; |
604 | $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; |
427 | |
605 | |
428 | $self->{logview}->add_paragraph ($color[$color], |
606 | $self->{logview}->add_paragraph ($color[$color], $_) |
429 | join "\n", map "$time $_", split /\n/, $text); |
607 | for map "$time $_", split /\n/, $text; |
|
|
608 | $self->{logview}->scroll_to_bottom; |
430 | |
609 | |
431 | $self->{statusbox}->add ($text, |
610 | $self->{statusbox}->add ($text, |
432 | group => $text, |
611 | group => $text, |
433 | fg => $color[$color], |
612 | fg => $color[$color], |
434 | timeout => 10, |
613 | timeout => $color >= 2 ? 60 : 10, |
435 | tooltip_font => $::FONT_FIXED, |
614 | tooltip_font => $::FONT_FIXED, |
436 | ); |
615 | ); |
437 | } |
616 | } |
438 | |
617 | |
439 | sub drawextinfo { |
618 | sub drawextinfo { |
… | |
… | |
443 | } |
622 | } |
444 | |
623 | |
445 | sub spell_add { |
624 | sub spell_add { |
446 | my ($self, $spell) = @_; |
625 | my ($self, $spell) = @_; |
447 | |
626 | |
448 | # TODO |
627 | # try to create single paragraphs of multiple lines sent by the server |
449 | # create a widget dynamically, using spell face (CF::Protocol downloads them) |
628 | $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g; |
|
|
629 | $spell->{message} =~ s/\n+$//; |
|
|
630 | $spell->{message} ||= "Server did not provide a description for this spell."; |
|
|
631 | |
450 | $::SETUP_SPELLS->add_spell ($spell); |
632 | $::SPELL_PAGE->add_spell ($spell); |
451 | |
633 | |
452 | $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}); |
453 | $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}); |
454 | } |
636 | } |
455 | |
637 | |
456 | sub spell_delete { |
638 | sub spell_delete { |
457 | my ($self, $spell) = @_; |
639 | my ($self, $spell) = @_; |
|
|
640 | |
458 | $::SETUP_SPELLS->remove_spell ($spell); |
641 | $::SPELL_PAGE->remove_spell ($spell); |
459 | } |
642 | } |
460 | |
643 | |
461 | sub addme_success { |
644 | sub addme_success { |
462 | my ($self) = @_; |
645 | my ($self) = @_; |
463 | |
646 | |
464 | my $parser = new Pod::POM; |
647 | my $skill_help = CFClient::load_pod CFClient::find_rcfile "pod/skill_help.pod", skill_help => 1, sub { |
465 | my $pod = $parser->parse_file (CFClient::find_rcfile "pod/skill_help.pod"); |
648 | my ($pom) = @_; |
466 | |
649 | |
467 | my %skill_tooltip; |
650 | my %skill_help; |
468 | |
651 | |
469 | for my $head2 ($pod->head1->[-2]->head2) { |
652 | for my $head2 ($pom->head1->[3]->head2) { |
470 | $skill_tooltip{$head2->title} = CFClient::pod_to_pango $head2->content; |
653 | $skill_help{$head2->title} = CFClient::pod_to_pango $head2->content; |
|
|
654 | } |
|
|
655 | |
|
|
656 | \%skill_help |
471 | } |
657 | }; |
472 | |
658 | |
473 | for my $skill (values %{$self->{skill_info}}) { |
659 | for my $skill (values %{$self->{skill_info}}) { |
474 | $self->{map_widget}->add_command ("ready_skill $skill", |
660 | $self->{map_widget}->add_command ("ready_skill $skill", |
475 | (CFClient::UI::Label::escape "Ready the skill '$skill'\n\n") |
661 | (CFClient::UI::Label::escape "Ready the skill '$skill'\n\n") |
476 | . $skill_tooltip{$skill}); |
662 | . $skill_help->{$skill}); |
477 | $self->{map_widget}->add_command ("use_skill $skill", |
663 | $self->{map_widget}->add_command ("use_skill $skill", |
478 | (CFClient::UI::Label::escape "Immediately use the skill '$skill'\n\n") |
664 | (CFClient::UI::Label::escape "Immediately use the skill '$skill'\n\n") |
479 | . $skill_tooltip{$skill}); |
665 | . $skill_help->{$skill}); |
480 | } |
666 | } |
481 | } |
667 | } |
482 | |
668 | |
483 | sub eof { |
669 | sub eof { |
484 | my ($self) = @_; |
670 | my ($self) = @_; |
… | |
… | |
509 | my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, (); |
695 | my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, (); |
510 | |
696 | |
511 | $self->send ("requestinfo image_sums $face $face"); |
697 | $self->send ("requestinfo image_sums $face $face"); |
512 | |
698 | |
513 | $self->{statusbox}->add (CFClient::UI::Label::escape "prefetching $todo", |
699 | $self->{statusbox}->add (CFClient::UI::Label::escape "prefetching $todo", |
514 | group => "prefetch", timeout => 2, fg => [1, 1, 0, 0.5]); |
700 | group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]); |
515 | } elsif (!exists $self->{num_faces}) { |
701 | } elsif (!exists $self->{num_faces}) { |
516 | $self->send ("requestinfo image_info"); |
702 | $self->send ("requestinfo image_info"); |
517 | |
703 | |
518 | $self->{num_faces} = 0; |
704 | $self->{num_faces} = 0; |
519 | |
705 | |
520 | $self->{statusbox}->add (CFClient::UI::Label::escape "starting to prefetch", |
706 | $self->{statusbox}->add (CFClient::UI::Label::escape "starting to prefetch", |
521 | group => "prefetch", timeout => 2, fg => [1, 1, 0, 0.5]); |
707 | group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]); |
522 | } |
708 | } |
523 | } |
709 | } |
524 | |
710 | |
525 | sub update_floorbox { |
711 | sub update_floorbox { |
526 | $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub { |
712 | $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub { |
527 | return unless $::CONN; |
713 | return unless $::CONN; |
528 | |
714 | |
529 | $::FLOORBOX->clear; |
715 | $::FLOORBOX->clear; |
530 | |
716 | |
531 | my $row; |
717 | my $row; |
532 | for (@{ $::CONN->{container}{0} }) { |
718 | for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{0} }) { |
533 | if ($row < 7) { |
719 | if ($row < 6) { |
534 | local $_->{face_widget}; # hack to force recreation of widget |
720 | local $_->{face_widget}; # hack to force recreation of widget |
535 | local $_->{desc_widget}; # hack to force recreation of widget |
721 | local $_->{desc_widget}; # hack to force recreation of widget |
536 | CFClient::Item::update_widgets $_; |
722 | CFClient::Item::update_widgets $_; |
537 | |
723 | |
538 | $::FLOORBOX->add (0, $row, $_->{face_widget}); |
724 | $::FLOORBOX->add (0, $row, $_->{face_widget}); |
539 | $::FLOORBOX->add (1, $row, $_->{desc_widget}); |
725 | $::FLOORBOX->add (1, $row, $_->{desc_widget}); |
540 | |
726 | |
541 | $row++; |
727 | $row++; |
542 | } else { |
728 | } else { |
543 | $::FLOORBOX->add (1, $row, new CFClient::UI::Label text => "More..."); |
729 | $::FLOORBOX->add (1, $row, new CFClient::UI::Button |
|
|
730 | text => "More...", |
|
|
731 | on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 }, |
|
|
732 | ); |
544 | last; |
733 | last; |
545 | } |
734 | } |
546 | } |
735 | } |
547 | }); |
736 | }); |
548 | |
737 | |
… | |
… | |
562 | tooltip => "Close the currently open container (if one is open)", |
751 | tooltip => "Close the currently open container (if one is open)", |
563 | on_activate => sub { |
752 | on_activate => sub { |
564 | $::CONN->send ("apply $tag") # $::CONN->{open_container}") |
753 | $::CONN->send ("apply $tag") # $::CONN->{open_container}") |
565 | if $tag != 0; |
754 | if $tag != 0; |
566 | #if $CONN->{open_container} != 0; |
755 | #if $CONN->{open_container} != 0; |
|
|
756 | 0 |
567 | }, |
757 | }, |
568 | ); |
758 | ); |
569 | } |
759 | } |
570 | |
760 | |
571 | $::INVR->set_items ($conn->{container}{$tag}); |
761 | $::INVR->set_items ($conn->{container}{$tag}); |
572 | } |
762 | } |
573 | |
763 | |
574 | sub update_container { |
764 | sub update_containers { |
575 | my ($tag) = @_; |
765 | my ($self) = @_; |
576 | |
766 | |
577 | $::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}) |
578 | 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 | }); |
579 | } |
784 | } |
580 | |
785 | |
581 | sub container_add { |
786 | sub container_add { |
582 | my ($self, $tag, $items) = @_; |
787 | my ($self, $tag, $items) = @_; |
583 | |
788 | |
584 | #d# print "container_add: container $tag ($self->{player}{tag})\n"; |
789 | $self->{update_container}{$tag}++; |
585 | |
790 | $self->update_containers; |
586 | if ($tag == 0) { |
|
|
587 | update_floorbox; |
|
|
588 | update_container (0); |
|
|
589 | } elsif ($tag == $self->{player}{tag}) { |
|
|
590 | $::INV->set_items ($self->{container}{$self->{player}{tag}}) |
|
|
591 | } else { |
|
|
592 | update_container ($tag); |
|
|
593 | } |
|
|
594 | |
|
|
595 | # $self-<{player}{tag} => player inv |
|
|
596 | #use PApp::Util; warn PApp::Util::dumpval $self->{container}{$self->{player}{tag}}; |
|
|
597 | } |
791 | } |
598 | |
792 | |
599 | sub container_clear { |
793 | sub container_clear { |
600 | my ($self, $tag) = @_; |
794 | my ($self, $tag) = @_; |
601 | |
795 | |
602 | #d# print "container_clear: container $tag ($self->{player}{tag})\n"; |
796 | $self->{update_container}{$tag}++; |
603 | |
797 | $self->update_containers; |
604 | if ($tag == 0) { |
|
|
605 | update_floorbox; |
|
|
606 | update_container (0); |
|
|
607 | } elsif ($tag == $self->{player}{tag}) { |
|
|
608 | $::INV->set_items ($self->{container}{$tag}) |
|
|
609 | } else { |
|
|
610 | update_container ($tag); |
|
|
611 | } |
|
|
612 | |
|
|
613 | # use PApp::Util; warn PApp::Util::dumpval $self->{container}{0}; |
|
|
614 | } |
798 | } |
615 | |
799 | |
616 | sub item_delete { |
800 | sub item_delete { |
617 | my ($self, @items) = @_; |
801 | my ($self, @items) = @_; |
618 | |
802 | |
|
|
803 | $self->{update_container}{$_->{container}}++ |
619 | for (@items) { |
804 | for @items; |
620 | #d# print "item_delete: $_->{tag} from $_->{container} ($self->{player}{tag})\n"; |
|
|
621 | |
|
|
622 | if ($_->{container} == 0) { |
|
|
623 | update_floorbox; |
|
|
624 | update_container ($_->{tag}); |
|
|
625 | } elsif ($_->{container} == $self->{player}{tag}) { |
|
|
626 | $::INV->set_items ($self->{container}{$self->{player}{tag}}) |
|
|
627 | } else { |
|
|
628 | update_container ($_->{container}); |
|
|
629 | } |
|
|
630 | } |
805 | |
|
|
806 | $self->update_containers; |
631 | } |
807 | } |
632 | |
808 | |
633 | sub item_update { |
809 | sub item_update { |
634 | my ($self, $item) = @_; |
810 | my ($self, $item) = @_; |
635 | |
811 | |
636 | #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"; |
637 | |
813 | |
638 | if ($item->{tag} == $self->{player}{tag}) { |
|
|
639 | $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $item->{weight} / 1000); |
|
|
640 | return; |
|
|
641 | } |
|
|
642 | |
|
|
643 | CFClient::Item::update_widgets $item; |
814 | CFClient::Item::update_widgets $item; |
644 | |
815 | |
645 | if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) { |
816 | if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) { |
646 | set_opencont ($::CONN, 0, "Floor"); |
817 | set_opencont ($::CONN, 0, "Floor"); |
647 | |
818 | |
648 | } elsif ($item->{flags} & F_OPEN) { |
819 | } elsif ($item->{flags} & F_OPEN) { |
649 | set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item); |
820 | set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item); |
|
|
821 | |
650 | } else { |
822 | } else { |
651 | if ($item->{container} == 0) { |
823 | $self->{update_container}{$item->{container}}++; |
652 | update_floorbox; |
|
|
653 | update_container (0); |
824 | $self->update_containers; |
654 | } elsif ($item->{container} == $self->{player}{tag}) { |
|
|
655 | $::INV->set_items ($self->{container}{$item->{container}}) |
|
|
656 | } |
|
|
657 | } |
825 | } |
658 | } |
826 | } |
659 | |
827 | |
660 | sub player_update { |
828 | sub player_update { |
661 | my ($self, $player) = @_; |
829 | my ($self, $player) = @_; |
662 | $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000); |
830 | $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000); |
|
|
831 | } |
663 | |
832 | |
664 | # do it here because it is ignored earlier, and there is no "login" event |
833 | sub update_server_info { |
|
|
834 | my ($self) = @_; |
|
|
835 | |
|
|
836 | my @yesno = ("<span foreground='red'>no</span>", "<span foreground='green'>yes</span>"); |
|
|
837 | |
|
|
838 | $::SERVER_INFO->set_markup ( |
|
|
839 | "server <tt>$self->{host}:$self->{port}</tt>\n" |
|
|
840 | . "protocol version <tt>$self->{version}</tt>\n" |
|
|
841 | . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n" |
|
|
842 | . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n" |
|
|
843 | . "cfplus support $yesno[$self->{cfplus_ext} > 0]" |
|
|
844 | . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n" |
|
|
845 | . "map size $self->{mapw}×$self->{maph}\n" |
|
|
846 | ); |
|
|
847 | } |
|
|
848 | |
|
|
849 | sub logged_in { |
|
|
850 | my ($self) = @_; |
|
|
851 | |
|
|
852 | $self->send_ext_req (cfplus_support => "1", sub { |
|
|
853 | $self->{cfplus_ext} = $_[0]; |
|
|
854 | $self->update_server_info; |
|
|
855 | }); |
|
|
856 | |
|
|
857 | $self->update_server_info; |
|
|
858 | |
665 | $self->send_command ("output-sync $::CFG->{output_sync}"); |
859 | $self->send_command ("output-sync $::CFG->{output_sync}"); |
666 | $self->send_command ("output-count $::CFG->{output_count}"); |
860 | $self->send_command ("output-count $::CFG->{output_count}"); |
|
|
861 | $self->send_command ("pickup $::CFG->{pickup}"); |
|
|
862 | } |
|
|
863 | |
|
|
864 | sub lookat { |
|
|
865 | my ($self, $x, $y) = @_; |
|
|
866 | |
|
|
867 | if ($self->{cfplus_ext}) { |
|
|
868 | $self->send_ext_req (lookat => "$x $y", sub { |
|
|
869 | my %res = split /\x00/, $_[0]; |
|
|
870 | |
|
|
871 | if (exists $res{npc_dialog}) { |
|
|
872 | # start npc chat dialog |
|
|
873 | $self->{npc_dialog} = new CFClient::NPCDialog:: |
|
|
874 | dx => $x, |
|
|
875 | dy => $y, |
|
|
876 | title => "$res{npc_dialog} (NPC)", |
|
|
877 | conn => $self, |
|
|
878 | ; |
|
|
879 | } |
|
|
880 | }); |
|
|
881 | } |
|
|
882 | |
|
|
883 | $self->send ("lookat $x $y"); |
|
|
884 | } |
|
|
885 | |
|
|
886 | sub destroy { |
|
|
887 | my ($self) = @_; |
|
|
888 | |
|
|
889 | $self->{npc_dialog}->destroy |
|
|
890 | if $self->{npc_dialog}; |
|
|
891 | |
|
|
892 | $self->SUPER::destroy; |
|
|
893 | } |
|
|
894 | |
|
|
895 | package CFClient::NPCDialog; |
|
|
896 | |
|
|
897 | our @ISA = 'CFClient::UI::FancyFrame'; |
|
|
898 | |
|
|
899 | sub new { |
|
|
900 | my $class = shift; |
|
|
901 | |
|
|
902 | my $self = $class->SUPER::new ( |
|
|
903 | x => 'center', |
|
|
904 | y => 'center', |
|
|
905 | name => "npc_dialog", |
|
|
906 | force_w => $::WIDTH * 0.7, |
|
|
907 | force_h => $::HEIGHT * 0.7, |
|
|
908 | title => "NPC Dialog", |
|
|
909 | kw => { hi => 0, yes => 0, no => 0 }, |
|
|
910 | @_, |
|
|
911 | ); |
|
|
912 | |
|
|
913 | Scalar::Util::weaken (my $this = $self); |
|
|
914 | |
|
|
915 | # better use a pane... |
|
|
916 | $self->add (my $hbox = new CFClient::UI::HBox); |
|
|
917 | $hbox->add ($self->{textview} = new CFClient::UI::TextScroller expand => 1); |
|
|
918 | |
|
|
919 | $hbox->add (my $vbox = new CFClient::UI::VBox); |
|
|
920 | |
|
|
921 | $vbox->add (new CFClient::UI::Label text => "Message Entry:"); |
|
|
922 | $vbox->add ($self->{entry} = new CFClient::UI::Entry |
|
|
923 | tooltip => "Enter a message you want to tell the NPC and press <b>return</b>.\n\n" |
|
|
924 | . "Sometimes you have to tell an NPC something you cannot find out during " |
|
|
925 | . "a normal conversation (such as a password). In those cases you have to use " |
|
|
926 | . "this text entry. You can also enter responses manually instead of using the response " |
|
|
927 | . "buttons below.", |
|
|
928 | on_activate => sub { |
|
|
929 | my ($entry, $text) = @_; |
|
|
930 | |
|
|
931 | return unless $text =~ /\S/; |
|
|
932 | |
|
|
933 | $entry->set_text (""); |
|
|
934 | $this->send ($text); |
|
|
935 | |
|
|
936 | 0 |
|
|
937 | }, |
|
|
938 | ); |
|
|
939 | |
|
|
940 | $vbox->add ($self->{options} = new CFClient::UI::VBox); |
|
|
941 | |
|
|
942 | $self->{bye_button} = new CFClient::UI::Button |
|
|
943 | text => "Bye (close)", |
|
|
944 | tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.", |
|
|
945 | on_activate => sub { $this->destroy; 0 }, |
|
|
946 | ; |
|
|
947 | |
|
|
948 | $self->update_options; |
|
|
949 | |
|
|
950 | $self->{token} = $self->{conn}->ext_token; |
|
|
951 | $self->{conn}->connect_ext ($self->{token} => sub { $this->feed (@_) }); |
|
|
952 | $self->{conn}->send ("ext npc_dialog_begin $self->{token} $self->{dx} $self->{dy}"); |
|
|
953 | |
|
|
954 | $self->{entry}->grab_focus; |
|
|
955 | |
|
|
956 | $self->{textview}->add_paragraph ([1, 1, 0, 1], "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n"); |
|
|
957 | |
|
|
958 | $self->show; |
|
|
959 | $self |
|
|
960 | }; |
|
|
961 | |
|
|
962 | sub update_options { |
|
|
963 | my ($self) = @_; |
|
|
964 | |
|
|
965 | Scalar::Util::weaken $self; |
|
|
966 | |
|
|
967 | $self->{options}->clear; |
|
|
968 | $self->{options}->add ($self->{bye_button}); |
|
|
969 | |
|
|
970 | for my $kw (sort keys %{ $self->{kw} }) { |
|
|
971 | $self->{options}->add (new CFClient::UI::Button |
|
|
972 | text => $kw, |
|
|
973 | on_activate => sub { |
|
|
974 | $self->send ($kw); |
|
|
975 | 0 |
|
|
976 | }, |
|
|
977 | ); |
|
|
978 | } |
|
|
979 | } |
|
|
980 | |
|
|
981 | sub feed { |
|
|
982 | my ($self, $data) = @_; |
|
|
983 | |
|
|
984 | Scalar::Util::weaken $self; |
|
|
985 | |
|
|
986 | my ($type, $msg) = split / /, $data, 2; |
|
|
987 | |
|
|
988 | if ($type eq "msg") { |
|
|
989 | my ($msg, @kw) = split /\x00/, $msg; |
|
|
990 | |
|
|
991 | utf8::decode $_ for ($msg, @kw); |
|
|
992 | |
|
|
993 | $self->{kw}{$_} = 1 for @kw; |
|
|
994 | |
|
|
995 | $msg = "\n" . CFClient::UI::Label::escape $msg; |
|
|
996 | my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} }; |
|
|
997 | my @link; |
|
|
998 | $msg =~ s{ |
|
|
999 | ($match) |
|
|
1000 | }{ |
|
|
1001 | my $kw = $1; |
|
|
1002 | |
|
|
1003 | push @link, new CFClient::UI::Label |
|
|
1004 | markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>", |
|
|
1005 | can_hover => 1, |
|
|
1006 | can_events => 1, |
|
|
1007 | padding_x => 0, |
|
|
1008 | padding_y => 0, |
|
|
1009 | on_button_up => sub { |
|
|
1010 | $self->send ($kw); |
|
|
1011 | }; |
|
|
1012 | |
|
|
1013 | chr 0xfffc |
|
|
1014 | }giex; |
|
|
1015 | |
|
|
1016 | $self->{textview}->add_paragraph ([1, 1, 1, 1], [$msg, @link]); |
|
|
1017 | $self->{textview}->scroll_to_bottom; |
|
|
1018 | $self->update_options; |
|
|
1019 | } else { |
|
|
1020 | $self->destroy; |
|
|
1021 | } |
|
|
1022 | |
|
|
1023 | 1 |
|
|
1024 | } |
|
|
1025 | |
|
|
1026 | sub send { |
|
|
1027 | my ($self, $msg) = @_; |
|
|
1028 | |
|
|
1029 | $self->{textview}->add_paragraph ([1, 1, 0, 1], "\n" . CFClient::UI::Label::escape $msg); |
|
|
1030 | $self->{textview}->scroll_to_bottom; |
|
|
1031 | |
|
|
1032 | utf8::encode $msg; |
|
|
1033 | $self->{conn}->send ("ext npc_dialog_tell $self->{token} $msg"); |
|
|
1034 | } |
|
|
1035 | |
|
|
1036 | sub destroy { |
|
|
1037 | my ($self) = @_; |
|
|
1038 | |
|
|
1039 | #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1 |
|
|
1040 | |
|
|
1041 | $self->{conn}->send ("ext npc_dialog_end $self->{token}") if $self->{token}; |
|
|
1042 | delete $self->{conn}{npc_dialog}; |
|
|
1043 | $self->{conn}->disconnect_ext ($self->{token}); |
|
|
1044 | |
|
|
1045 | $self->SUPER::destroy; |
667 | } |
1046 | } |
668 | |
1047 | |
669 | 1; |
1048 | 1; |