ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.86
Committed: Sun Oct 8 21:22:30 2006 UTC (17 years, 7 months ago) by root
Branch: MAIN
CVS Tags: rel-0_53
Changes since 1.85: +4 -10 lines
Log Message:
*** empty log message ***

File Contents

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