… | |
… | |
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 | |
… | |
… | |
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 = shift; |
20 | |
21 | |
21 | my $self = $class->SUPER::new (@_, setup_req => { extmap => 1 }); |
22 | my $self = $class->SUPER::new (@_, |
|
|
23 | setup_req => { |
|
|
24 | extmap => 1, |
|
|
25 | smoothing => 1, |
|
|
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 | |
|
|
51 | $self->connect_ext (event_capabilities => sub { |
|
|
52 | my ($cap) = @_; |
|
|
53 | |
|
|
54 | if (my $ts = $cap->{tileset}) { |
|
|
55 | if (my ($default) = grep $_->[2] & 1, @$ts) { |
|
|
56 | $self->{tileset} = $default; |
|
|
57 | $self->{tilesize} = $default->[3]; |
|
|
58 | $self->setup_req (tileset => $default->[0]); |
|
|
59 | |
|
|
60 | my $w = int $self->{mapw} * 32 / $self->{tilesize}; |
|
|
61 | my $h = int $self->{maph} * 32 / $self->{tilesize}; |
|
|
62 | |
|
|
63 | $self->setup_req (mapsize => "${w}x${h}"); |
|
|
64 | } |
|
|
65 | } |
|
|
66 | }); |
|
|
67 | |
45 | $self->{map_widget}->add_command (@$_) |
68 | $self->{map_widget}->add_command (@$_) |
46 | for @cmd_help; |
69 | for @cmd_help; |
47 | |
|
|
48 | $self->{noface} = new_from_file CFPlus::Texture |
|
|
49 | CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1; |
|
|
50 | |
70 | |
51 | { |
71 | { |
52 | $self->{dialogue} = my $tex = new_from_file CFPlus::Texture |
72 | $self->{dialogue} = my $tex = new_from_file CFPlus::Texture |
53 | CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1; |
73 | CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1; |
54 | $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}}); |
55 | } |
75 | } |
56 | |
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 | |
57 | $self->{open_container} = 0; |
83 | $self->{open_container} = 0; |
58 | |
84 | |
59 | # "global" |
|
|
60 | $self->{tilecache} = CFPlus::db_table "tilecache" |
|
|
61 | or die "tilecache: unable to open database table"; |
|
|
62 | $self->{facemap} = CFPlus::db_table "facemap" |
|
|
63 | or die "facemap: unable to open database table"; |
|
|
64 | |
|
|
65 | # per server |
85 | # per server |
66 | $self->{mapcache} = CFPlus::db_table "mapcache_$self->{host}_$self->{port}" |
86 | $self->{mapcache} = "mapcache_$self->{host}_$self->{port}"; |
67 | or die "mapcache_$self->{host}_$self->{port}: unable to open database table"; |
|
|
68 | |
87 | |
69 | $self |
88 | $self |
70 | } |
89 | } |
71 | |
90 | |
72 | sub logprint { |
91 | sub logprint { |
… | |
… | |
374 | or return; |
393 | or return; |
375 | |
394 | |
376 | my ($hash, $x, $y, $w, $h) = @$map_info; |
395 | my ($hash, $x, $y, $w, $h) = @$map_info; |
377 | |
396 | |
378 | my $data = $self->{map}->get_rect ($x, $y, $w, $h); |
397 | my $data = $self->{map}->get_rect ($x, $y, $w, $h); |
379 | $self->{mapcache}->put ($hash => Compress::LZF::compress $data); |
398 | CFPlus::DB::put $self->{mapcache} => $hash => Compress::LZF::compress $data, sub { }; |
380 | #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# |
399 | #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# |
381 | } |
400 | } |
382 | |
401 | |
383 | sub map_clear { |
402 | sub map_clear { |
384 | my ($self) = @_; |
403 | my ($self) = @_; |
… | |
… | |
388 | |
407 | |
389 | $self->{map}->clear; |
408 | $self->{map}->clear; |
390 | delete $self->{map_widget}{magicmap}; |
409 | delete $self->{map_widget}{magicmap}; |
391 | } |
410 | } |
392 | |
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 | } |
393 | |
433 | |
394 | sub load_map($$$) { |
434 | sub load_map($$$) { |
395 | my ($self, $hash, $x, $y) = @_; |
435 | my ($self, $hash, $x, $y) = @_; |
396 | |
436 | |
397 | if (defined (my $data = $self->{mapcache}->get ($hash))) { |
437 | CFPlus::DB::get $self->{mapcache} => $hash, sub { |
|
|
438 | my ($data) = @_; |
|
|
439 | |
|
|
440 | if (defined $data) { |
398 | $data = Compress::LZF::decompress $data; |
441 | $data = Compress::LZF::decompress $data; |
399 | #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d# |
442 | #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d# |
400 | for my $id ($self->{map}->set_rect ($x, $y, $data)) { |
|
|
401 | my $data = $self->{tilecache}->get ($id) |
|
|
402 | or next; |
|
|
403 | |
443 | |
404 | $self->set_texture ($id => $data); |
444 | my $inprogress = @{ $self->{bg_fetch} || [] }; |
|
|
445 | unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data); |
|
|
446 | $self->bg_fetch unless $inprogress; |
405 | } |
447 | } |
406 | } |
448 | }; |
407 | } |
449 | } |
408 | |
450 | |
409 | # hardcode /world/world_xxx_xxx map names, the savings are enourmous, |
451 | # hardcode /world/world_xxx_xxx map names, the savings are enourmous, |
410 | # (server resource,s latency, bandwidth), so this hack is warranted. |
452 | # (server resource,s latency, bandwidth), so this hack is warranted. |
411 | # the right fix is to make real tiled maps with an overview file |
453 | # the right fix is to make real tiled maps with an overview file |
… | |
… | |
525 | $self->load_map ($hash, $x, $y); |
567 | $self->load_map ($hash, $x, $y); |
526 | $self->flood_fill (0, 0, 0, "", $hash, $flags); |
568 | $self->flood_fill (0, 0, 0, "", $hash, $flags); |
527 | } |
569 | } |
528 | |
570 | |
529 | sub face_find { |
571 | sub face_find { |
530 | my ($self, $facenum, $face) = @_; |
572 | my ($self, $facenum, $face, $cb) = @_; |
531 | |
573 | |
532 | my $hash = "$face->{chksum},$face->{name}"; |
574 | my $hash = "$face->{chksum},$face->{name}"; |
533 | |
575 | |
534 | my $id = $self->{facemap}->get ($hash); |
576 | my $id = CFPlus::DB::get_tile_id_sync $hash; |
535 | |
577 | |
536 | unless ($id) { |
|
|
537 | # create new id for face |
|
|
538 | # I love transactions |
|
|
539 | for (1..100) { |
|
|
540 | my $txn = $CFPlus::DB_ENV->txn_begin; |
|
|
541 | my $status = $self->{facemap}->db_get (id => $id); |
|
|
542 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
|
|
543 | $id = ($id || 64) + 1; |
|
|
544 | if ($self->{facemap}->put (id => $id) == 0 |
|
|
545 | && $self->{facemap}->put ($hash => $id) == 0) { |
|
|
546 | $txn->txn_commit; |
|
|
547 | |
|
|
548 | goto gotid; |
|
|
549 | } |
|
|
550 | } |
|
|
551 | $txn->txn_abort; |
|
|
552 | } |
|
|
553 | |
|
|
554 | CFPlus::fatal "maximum number of transaction retries reached - database problems?"; |
|
|
555 | } |
|
|
556 | |
|
|
557 | gotid: |
|
|
558 | $face->{id} = $id; |
578 | $face->{id} = $id; |
559 | $self->{map}->set_face ($facenum => $id); |
|
|
560 | $self->{faceid}[$facenum] = $id;#d# |
579 | $self->{faceid}[$facenum] = $id; |
561 | |
580 | |
562 | my $face = $self->{tilecache}->get ($id); |
581 | $self->{map}->set_tileid ($facenum => $id); |
563 | |
582 | |
564 | if ($face) { |
583 | CFPlus::DB::get tilecache => $id, $cb; |
565 | #$self->face_prefetch; |
|
|
566 | $face |
|
|
567 | } else { |
|
|
568 | my $tex = $self->{noface}; |
|
|
569 | $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); |
|
|
570 | undef |
|
|
571 | }; |
|
|
572 | } |
584 | } |
573 | |
585 | |
574 | sub face_update { |
586 | sub face_update { |
575 | my ($self, $facenum, $face, $changed) = @_; |
587 | my ($self, $facenum, $face, $changed) = @_; |
576 | |
588 | |
577 | $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed; |
589 | CFPlus::DB::put tilecache => $face->{id} => $face->{image}, sub { } |
|
|
590 | if $changed; |
578 | |
591 | |
579 | $self->set_texture ($face->{id} => delete $face->{image}); |
592 | $self->set_texture ($face->{id} => delete $face->{image}); |
|
|
593 | } |
|
|
594 | |
|
|
595 | sub smooth_update { |
|
|
596 | my ($self, $facenum, $face) = @_; |
|
|
597 | |
|
|
598 | $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel}); |
580 | } |
599 | } |
581 | |
600 | |
582 | sub set_texture { |
601 | sub set_texture { |
583 | my ($self, $id, $data) = @_; |
602 | my ($self, $id, $data) = @_; |
584 | |
603 | |
585 | $self->{texture}[$id] ||= do { |
604 | $self->{texture}[$id] = my $tex = |
586 | my $tex = |
|
|
587 | new_from_image CFPlus::Texture |
605 | new_from_image CFPlus::Texture |
588 | $data, minify => 1, mipmap => 1; |
606 | $data, minify => 1, mipmap => 1; |
589 | |
607 | |
590 | $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); |
608 | $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); |
591 | $self->{map_widget}->update; |
609 | $self->{map_widget}->update; |
592 | |
|
|
593 | $tex |
|
|
594 | }; |
|
|
595 | } |
610 | } |
596 | |
611 | |
597 | sub sound_play { |
612 | sub sound_play { |
598 | my ($self, $x, $y, $soundnum, $type) = @_; |
613 | my ($self, $x, $y, $soundnum, $type) = @_; |
599 | |
614 | |
… | |
… | |
679 | } |
694 | } |
680 | |
695 | |
681 | sub setup { |
696 | sub setup { |
682 | my ($self, $setup) = @_; |
697 | my ($self, $setup) = @_; |
683 | |
698 | |
|
|
699 | $self->{map_widget}->set_tilesize ($self->{tilesize}); |
684 | $::MAP->resize ($self->{mapw}, $self->{maph}); |
700 | $::MAP->resize ($self->{mapw}, $self->{maph}); |
685 | } |
701 | } |
686 | |
702 | |
687 | sub addme_success { |
703 | sub addme_success { |
688 | my ($self) = @_; |
704 | my ($self) = @_; |