ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.103
Committed: Mon Apr 23 19:17:41 2007 UTC (17 years, 1 month ago) by root
Branch: MAIN
Changes since 1.102: +18 -17 lines
Log Message:
implement support for ex cmd, retrieveing item descriptions (immediately right now, and again and again - might want to improve on this, but not sure how)

File Contents

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