… | |
… | |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use Crossfire::Protocol::Constants; |
6 | use Crossfire::Protocol::Constants; |
7 | |
7 | |
8 | use CFPlus; |
8 | use CFPlus; |
|
|
9 | use CFPlus::DB; |
9 | use CFPlus::UI; |
10 | use CFPlus::UI; |
10 | use CFPlus::Pod; |
11 | use CFPlus::Pod; |
11 | use CFPlus::Macro; |
12 | use CFPlus::Macro; |
12 | use CFPlus::Item; |
13 | use CFPlus::Item; |
13 | |
14 | |
14 | use Crossfire::Protocol::Base 0.95; |
15 | use Crossfire::Protocol::Base 0.95; |
15 | |
16 | |
16 | use base 'Crossfire::Protocol::Base'; |
17 | use base 'Crossfire::Protocol::Base'; |
17 | |
18 | |
18 | sub new { |
19 | sub new { |
19 | my $class = shift; |
20 | my ($class, %arg) = @_; |
20 | |
21 | |
21 | my $self = $class->SUPER::new (@_, setup_req => { extmap => 1 }); |
22 | my $self = $class->SUPER::new (%arg, |
|
|
23 | setup_req => { |
|
|
24 | extmap => 1, |
|
|
25 | %{$arg{setup_req} || {}}, |
|
|
26 | }, |
|
|
27 | ); |
22 | |
28 | |
23 | $self->{map_widget}->clr_commands; |
29 | $self->{map_widget}->clr_commands; |
24 | |
30 | |
25 | my @cmd_help = map { |
31 | my @cmd_help = map { |
26 | $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x |
32 | $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x |
… | |
… | |
37 | for @args; |
43 | for @args; |
38 | |
44 | |
39 | map ["$cmd$_", $text], |
45 | map ["$cmd$_", $text], |
40 | sort { (length $a) <=> (length $b) } |
46 | sort { (length $a) <=> (length $b) } |
41 | @args |
47 | @args |
42 | } sort { $a->{par} <=> $b->{par} } |
48 | } sort { $a->{par} <=> $b->{par} } |
43 | CFPlus::Pod::find command => "*"; |
49 | CFPlus::Pod::find command => "*"; |
44 | |
50 | |
45 | $self->connect_ext (event_capabilities => sub { |
51 | $self->connect_ext (event_capabilities => sub { |
46 | my ($cap) = @_; |
52 | my ($cap) = @_; |
47 | |
53 | |
48 | if (my $ts = $cap->{tileset}) { |
54 | if (my $ts = $cap->{tileset}) { |
49 | if (my ($default) = grep $_->[2] & 1, @$ts) { |
55 | if (my ($default) = grep $_->[2] & 1, @$ts) { |
50 | $self->{tileset} = $default; |
56 | $self->{tileset} = $default; |
51 | $self->{tilesize} = $default->[3]; |
57 | $self->{tilesize} = $default->[3]; |
52 | $self->setup_req (tileset => $default->[0]); |
58 | $self->setup_req (tileset => $default->[0]); |
53 | |
59 | |
54 | my $w = int $self->{mapw} * 32 / $self->{tilesize}; |
60 | my $w = int $self->{mapw} * 32 / $self->{tilesize}; |
55 | my $h = int $self->{maph} * 32 / $self->{tilesize}; |
61 | my $h = int $self->{maph} * 32 / $self->{tilesize}; |
56 | |
62 | |
57 | $self->setup_req (mapsize => "${w}x${h}"); |
63 | $self->setup_req (mapsize => "${w}x${h}"); |
58 | } |
64 | } |
59 | } |
65 | } |
60 | }); |
66 | }); |
61 | |
67 | |
62 | $self->{map_widget}->add_command (@$_) |
68 | $self->{map_widget}->add_command (@$_) |
63 | for @cmd_help; |
69 | for @cmd_help; |
64 | |
|
|
65 | $self->{noface} = new_from_file CFPlus::Texture |
|
|
66 | CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1; |
|
|
67 | |
70 | |
68 | { |
71 | { |
69 | $self->{dialogue} = my $tex = new_from_file CFPlus::Texture |
72 | $self->{dialogue} = my $tex = new_from_file CFPlus::Texture |
70 | CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1; |
73 | CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1; |
71 | $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}}); |
74 | $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}}); |
72 | } |
75 | } |
73 | |
76 | |
|
|
77 | { |
|
|
78 | $self->{noface} = my $tex = new_from_file CFPlus::Texture |
|
|
79 | CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1; |
|
|
80 | $self->{map}->set_texture (2, @$tex{qw(name w h s t)}, @{$tex->{minified}}); |
|
|
81 | } |
|
|
82 | |
74 | $self->{open_container} = 0; |
83 | $self->{open_container} = 0; |
75 | |
84 | |
76 | # "global" |
|
|
77 | $self->{tilecache} = CFPlus::db_table "tilecache" |
|
|
78 | or die "tilecache: unable to open database table"; |
|
|
79 | $self->{facemap} = CFPlus::db_table "facemap" |
|
|
80 | or die "facemap: unable to open database table"; |
|
|
81 | |
|
|
82 | # per server |
85 | # per server |
83 | $self->{mapcache} = CFPlus::db_table "mapcache_$self->{host}_$self->{port}" |
86 | $self->{mapcache} = "mapcache_$self->{host}_$self->{port}"; |
84 | or die "mapcache_$self->{host}_$self->{port}: unable to open database table"; |
|
|
85 | |
87 | |
86 | $self |
88 | $self |
87 | } |
89 | } |
88 | |
90 | |
89 | sub logprint { |
91 | sub logprint { |
… | |
… | |
391 | or return; |
393 | or return; |
392 | |
394 | |
393 | my ($hash, $x, $y, $w, $h) = @$map_info; |
395 | my ($hash, $x, $y, $w, $h) = @$map_info; |
394 | |
396 | |
395 | my $data = $self->{map}->get_rect ($x, $y, $w, $h); |
397 | my $data = $self->{map}->get_rect ($x, $y, $w, $h); |
396 | $self->{mapcache}->put ($hash => Compress::LZF::compress $data); |
398 | CFPlus::DB::put $self->{mapcache} => $hash => Compress::LZF::compress $data, sub { }; |
397 | #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# |
399 | #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# |
398 | } |
400 | } |
399 | |
401 | |
400 | sub map_clear { |
402 | sub map_clear { |
401 | my ($self) = @_; |
403 | my ($self) = @_; |
… | |
… | |
405 | |
407 | |
406 | $self->{map}->clear; |
408 | $self->{map}->clear; |
407 | delete $self->{map_widget}{magicmap}; |
409 | delete $self->{map_widget}{magicmap}; |
408 | } |
410 | } |
409 | |
411 | |
|
|
412 | sub bg_fetch { |
|
|
413 | my ($self) = @_; |
|
|
414 | |
|
|
415 | my $id; |
|
|
416 | |
|
|
417 | do { |
|
|
418 | $id = pop @{$self->{bg_fetch}} |
|
|
419 | or return; |
|
|
420 | } while $self->{texture}[$id]; |
|
|
421 | |
|
|
422 | CFPlus::DB::get tilecache => $id, sub { |
|
|
423 | my ($data) = @_; |
|
|
424 | |
|
|
425 | return unless $self->{map}; # stop when destroyed |
|
|
426 | |
|
|
427 | $self->set_texture ($id => $data) |
|
|
428 | if defined $data; |
|
|
429 | |
|
|
430 | $self->bg_fetch; |
|
|
431 | }; |
|
|
432 | } |
410 | |
433 | |
411 | sub load_map($$$) { |
434 | sub load_map($$$) { |
412 | my ($self, $hash, $x, $y) = @_; |
435 | my ($self, $hash, $x, $y) = @_; |
413 | |
436 | |
414 | if (defined (my $data = $self->{mapcache}->get ($hash))) { |
437 | my $gen = $self->{map_change_gen}; |
|
|
438 | |
|
|
439 | CFPlus::DB::get $self->{mapcache} => $hash, sub { |
|
|
440 | return unless $gen == $self->{map_change_gen}; |
|
|
441 | |
|
|
442 | my ($data) = @_; |
|
|
443 | |
|
|
444 | if (defined $data) { |
415 | $data = Compress::LZF::decompress $data; |
445 | $data = Compress::LZF::decompress $data; |
416 | #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d# |
446 | #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d# |
417 | for my $id ($self->{map}->set_rect ($x, $y, $data)) { |
|
|
418 | my $data = $self->{tilecache}->get ($id) |
|
|
419 | or next; |
|
|
420 | |
447 | |
421 | $self->set_texture ($id => $data); |
448 | my $inprogress = @{ $self->{bg_fetch} || [] }; |
|
|
449 | unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data); |
|
|
450 | $self->bg_fetch unless $inprogress; |
422 | } |
451 | } |
423 | } |
452 | }; |
424 | } |
453 | } |
425 | |
454 | |
426 | # hardcode /world/world_xxx_xxx map names, the savings are enourmous, |
455 | # hardcode /world/world_xxx_xxx map names, the savings are enourmous, |
427 | # (server resource,s latency, bandwidth), so this hack is warranted. |
456 | # (server resource,s latency, bandwidth), so this hack is warranted. |
428 | # the right fix is to make real tiled maps with an overview file |
457 | # the right fix is to make real tiled maps with an overview file |
… | |
… | |
492 | |
521 | |
493 | $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags) |
522 | $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags) |
494 | if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1; |
523 | if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1; |
495 | |
524 | |
496 | } else { |
525 | } else { |
|
|
526 | my $gen = $self->{map_change_gen}; |
497 | $self->send_mapinfo ("spatial $path$tile", sub { |
527 | $self->send_mapinfo ("spatial $path$tile", sub { |
|
|
528 | return unless $gen == $self->{map_change_gen}; |
|
|
529 | |
498 | my ($mode, $flags, $x, $y, $w, $h, $hash) = @_; |
530 | my ($mode, $flags, $x, $y, $w, $h, $hash) = @_; |
499 | |
531 | |
500 | return if $mode ne "spatial"; |
532 | return if $mode ne "spatial"; |
501 | |
533 | |
502 | $x += $self->{map}->ox; |
534 | $x += $self->{map}->ox; |
… | |
… | |
516 | |
548 | |
517 | sub map_change { |
549 | sub map_change { |
518 | my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; |
550 | my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; |
519 | |
551 | |
520 | $self->flush_map; |
552 | $self->flush_map; |
|
|
553 | |
|
|
554 | ++$self->{map_change_gen}; |
521 | |
555 | |
522 | my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); |
556 | my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); |
523 | |
557 | |
524 | my $mapmapw = $self->{mapmap}->{w}; |
558 | my $mapmapw = $self->{mapmap}->{w}; |
525 | my $mapmaph = $self->{mapmap}->{h}; |
559 | my $mapmaph = $self->{mapmap}->{h}; |
… | |
… | |
542 | $self->load_map ($hash, $x, $y); |
576 | $self->load_map ($hash, $x, $y); |
543 | $self->flood_fill (0, 0, 0, "", $hash, $flags); |
577 | $self->flood_fill (0, 0, 0, "", $hash, $flags); |
544 | } |
578 | } |
545 | |
579 | |
546 | sub face_find { |
580 | sub face_find { |
547 | my ($self, $facenum, $face) = @_; |
581 | my ($self, $facenum, $face, $cb) = @_; |
548 | |
582 | |
549 | my $hash = "$face->{chksum},$face->{name}"; |
583 | my $hash = "$face->{chksum},$face->{name}"; |
550 | |
584 | |
551 | my $id = $self->{facemap}->get ($hash); |
585 | my $id = CFPlus::DB::get_tile_id_sync $hash; |
552 | |
586 | |
553 | unless ($id) { |
|
|
554 | # create new id for face |
|
|
555 | # I love transactions |
|
|
556 | for (1..100) { |
|
|
557 | my $txn = $CFPlus::DB_ENV->txn_begin; |
|
|
558 | my $status = $self->{facemap}->db_get (id => $id); |
|
|
559 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
|
|
560 | $id = ($id || 64) + 1; |
|
|
561 | if ($self->{facemap}->put (id => $id) == 0 |
|
|
562 | && $self->{facemap}->put ($hash => $id) == 0) { |
|
|
563 | $txn->txn_commit; |
|
|
564 | |
|
|
565 | goto gotid; |
|
|
566 | } |
|
|
567 | } |
|
|
568 | $txn->txn_abort; |
|
|
569 | } |
|
|
570 | |
|
|
571 | CFPlus::fatal "maximum number of transaction retries reached - database problems?"; |
|
|
572 | } |
|
|
573 | |
|
|
574 | gotid: |
|
|
575 | $face->{id} = $id; |
587 | $face->{id} = $id; |
576 | $self->{map}->set_face ($facenum => $id); |
|
|
577 | $self->{faceid}[$facenum] = $id;#d# |
588 | $self->{faceid}[$facenum] = $id; |
578 | |
589 | |
579 | my $face = $self->{tilecache}->get ($id); |
590 | $self->{map}->set_tileid ($facenum => $id); |
580 | |
591 | |
581 | if ($face) { |
592 | CFPlus::DB::get tilecache => $id, $cb; |
582 | #$self->face_prefetch; |
|
|
583 | $face |
|
|
584 | } else { |
|
|
585 | my $tex = $self->{noface}; |
|
|
586 | $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); |
|
|
587 | undef |
|
|
588 | }; |
|
|
589 | } |
593 | } |
590 | |
594 | |
591 | sub face_update { |
595 | sub face_update { |
592 | my ($self, $facenum, $face, $changed) = @_; |
596 | my ($self, $facenum, $face, $changed) = @_; |
593 | |
597 | |
594 | $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed; |
598 | CFPlus::DB::put tilecache => $face->{id} => $face->{image}, sub { } |
|
|
599 | if $changed; |
595 | |
600 | |
596 | $self->set_texture ($face->{id} => delete $face->{image}); |
601 | $self->set_texture ($face->{id} => delete $face->{image}); |
|
|
602 | } |
|
|
603 | |
|
|
604 | sub smooth_update { |
|
|
605 | my ($self, $facenum, $face) = @_; |
|
|
606 | |
|
|
607 | $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel}); |
597 | } |
608 | } |
598 | |
609 | |
599 | sub set_texture { |
610 | sub set_texture { |
600 | my ($self, $id, $data) = @_; |
611 | my ($self, $id, $data) = @_; |
601 | |
612 | |
602 | $self->{texture}[$id] ||= do { |
613 | $self->{texture}[$id] = my $tex = |
603 | my $tex = |
|
|
604 | new_from_image CFPlus::Texture |
614 | new_from_image CFPlus::Texture |
605 | $data, minify => 1, mipmap => 1; |
615 | $data, minify => 1, mipmap => 1; |
606 | |
616 | |
607 | $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); |
617 | $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); |
608 | $self->{map_widget}->update; |
618 | $self->{map_widget}->update; |
609 | |
|
|
610 | $tex |
|
|
611 | }; |
|
|
612 | } |
619 | } |
613 | |
620 | |
614 | sub sound_play { |
621 | sub sound_play { |
615 | my ($self, $x, $y, $soundnum, $type) = @_; |
622 | my ($self, $x, $y, $soundnum, $type) = @_; |
616 | |
623 | |
… | |
… | |
649 | |
656 | |
650 | my $fg = $color[$color % @color]; |
657 | my $fg = $color[$color % @color]; |
651 | |
658 | |
652 | $self->logprint ("info: ", $text); |
659 | $self->logprint ("info: ", $text); |
653 | |
660 | |
654 | # try to create single paragraphs of multiple lines sent by the server |
661 | ## try to create single paragraphs of multiple lines sent by the server |
|
|
662 | # no longer neecssary with TRT servers |
655 | $text =~ s/(?<=\S)\n(?=\w)/ /g; |
663 | #$text =~ s/(?<=\S)\n(?=\w)/ /g; |
656 | |
664 | |
657 | $text = CFPlus::asxml $text; |
665 | $text = CFPlus::asxml $text; |
658 | $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; |
666 | $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; |
659 | $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; |
667 | $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; |
660 | |
668 | |