ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/item-worldmap.ext
(Generate patch)

Comparing deliantra/server/ext/item-worldmap.ext (file contents):
Revision 1.6 by root, Mon Jul 23 16:53:15 2007 UTC vs.
Revision 1.9 by root, Mon Jul 23 23:38:17 2007 UTC

1#! perl # depends=widget mandatory 1#! perl # depends=widget mandatory
2 2
3# this module implements a rather fancy worldmap 3# this module implements a rather fancy worldmap
4 4
5our $WORLDMAP_UPDATE_INTERVAL = $cf::CFG{worldmap_update_interval} || 10; 5our $WORLDMAP_UPDATE_INTERVAL = $cf::CFG{worldmap_update_interval} || 2;
6 6
7our $GENCOUNT = 0; 7our $GENCOUNT = 0;
8our %PLAYERINFO; 8our %PLAYERINFO;
9 9
10our ($MAPW, $MAPH) = (1024, 1024); # it's useful to know the map width/height in pixels
11
10sub update_worldmap { 12sub update_worldmap {
11 my ($ws) = @_; 13 my ($ws) = @_;
12 14
15 return if $GENCOUNT == $ws->{gencount};
16 $ws->{gencount} = $GENCOUNT;
17
13 my $labels = delete $ws->{labels}; 18 my $old = delete $ws->{labels};
19 my $new;
20
21 my $name = $ws->{ns}->pl->ob->name;
14 22
15 while (my ($k, $v) = each %PLAYERINFO) { 23 while (my ($k, $v) = each %PLAYERINFO) {
16 if (my $old = $labels->{$k}) { 24 my $label = (delete $old->{$k}) || do {
17 $ws->{labels}{$k} = $old;
18 } else {
19 $ws->{labels}{$k} = $ws->new (Label => 25 my $label = $ws->new (Label =>
20 text => $k, 26 text => $k,
21 fontsize => 0.2, 27 fontsize => 0.2,
28 );
29
30 my $marker = cf::face::find "res/map-arrow.png";
31 $ws->{ns}->send_face ($marker);
32 $ws->{ns}->flush_fx;
33 $marker = $ws->new (Face =>
34 size_w => undef,
35 size_h => undef,
22 c_rel => 1, 36 face => $marker,
37 );
38 my $children = [$label, $marker];
39
40 $ws->{canvas}->add (my $vbox = $ws->new (VBox =>
41 children => $children,
23 c_halign => -.5, 42 c_halign => -.5,
24 c_valign => -1, 43 c_valign => -1,
25 ); 44 ));
26 $ws->{canvas}->add ($ws->{labels}{$k}); 45 $vbox->{children} = $children;
46
47 $vbox
48 };
49
50 $new->{$k} = $label;
51
52 if ($v != $label->{prevpos}) {
53 $label->set (c_x => $v->[0], c_y => $v->[1]);
54 $label->{prevpos} = $v;
55 $ws->{window}->make_visible ($v->[0], $v->[1], .2)
56 if $k eq $name;
27 } 57 }
28 $ws->{labels}{$k}->set (c_x => $v->[0], c_y => $v->[1]);
29 } 58 }
59
60 $ws->{labels} = $new;
30} 61}
31 62
32sub create_widgets { 63sub create_widgets {
33 my ($ns) = @_; 64 my ($ns) = @_;
34 65
42 x => "center", 73 x => "center",
43 y => "center", 74 y => "center",
44 has_close_button => 1, 75 has_close_button => 1,
45 on_delete => sub { shift->hide }, 76 on_delete => sub { shift->hide },
46 on_visibility_change => sub { 77 on_visibility_change => sub {
78 warn "VCHANGE <@_>\n";#d#
47 $_[0]{visibility} = $_[1]; 79 $_[0]{visibility} = $_[1];
48 update_worldmap $_[0]{ws} if $_[1]; 80 update_worldmap $_[0]{ws} if $_[1];
49 }, 81 },
50 ); 82 );
51 83
52 my $face = cf::face::find "res/worldmap.jpg"; 84 my $face = cf::face::find "res/worldmap.jpg";
53 $ns->send_face ($face); 85 $ns->send_face ($face);
54 $ns->flush_fx; 86 $ns->flush_fx;
55 87
56 $w->add (my $sw = $ws->new (ScrolledWindow => scroll_x => 1, scroll_y => 1)); 88 $w->add (my $sw = $ws->{window} = $ws->new (ScrolledWindow => scroll_x => 1, scroll_y => 1));
57 $sw->add (my $canvas = $ws->{canvas} = $ws->new (Canvas => expand => 1)); 89 $sw->add (my $canvas = $ws->{canvas} = $ws->new (Canvas => expand => 1));
58 90
59 $ws->{mapface} = $ws->new (Face => 91 $ws->{mapface} = $ws->new (Face =>
60 expand => 1, 92 expand => 1,
61 size_w => undef, 93 size_w => undef,
90 122
91 while () { 123 while () {
92 $schedule_interval->interval ($WORLDMAP_UPDATE_INTERVAL); 124 $schedule_interval->interval ($WORLDMAP_UPDATE_INTERVAL);
93 $schedule_interval->next; 125 $schedule_interval->next;
94 126
95 cf::get_slot 0.05; 127 cf::get_slot 0.01, -50, "worldmap update";
96 128
97 ++$GENCOUNT; 129 ++$GENCOUNT;
98 130
99 # recalculate player info 131 # recalculate player info
100 my %new; 132 my %new;
109 my $y = ($2 - 100) * 50 + $ob->y; 141 my $y = ($2 - 100) * 50 + $ob->y;
110 142
111 0 <= $x && 0 <= $y && $x < 1500 && $y < 1500 143 0 <= $x && 0 <= $y && $x < 1500 && $y < 1500
112 or next; 144 or next;
113 145
114 # rounding saves network bandwidth... 146 $x = int $x * $MAPW / 1500;
115 $x = sprintf "%.3f", $x / 1500; 147 $y = int $y * $MAPH / 1500;
116 $y = sprintf "%.3f", $y / 1500;
117 148
118 my $name = $ob->name; 149 my $name = $ob->name;
119 150
120 if (my $pi = delete $PLAYERINFO{$name}) { 151 if (my $pi = delete $PLAYERINFO{$name}) {
121 if ($pi->[0] == $x && $pi->[1] == $y) { 152 if ($pi->[0] == $x && $pi->[1] == $y) {
125 } 156 }
126 157
127 $new{$name} = [$x, $y]; 158 $new{$name} = [$x, $y];
128 } 159 }
129 160
161 *PLAYERINFO = \%new;
162
163 cf::get_slot 0.03, -50, "worldmap socket update";
130 for (values %cf::PLAYER) { 164 for (values %cf::PLAYER) {
131 my $ns = $_->ns 165 my $ns = $_->ns
132 or next; 166 or next;
133 167
134 update_worldmap $ns->{ws_worldmap} 168 update_worldmap $ns->{ws_worldmap}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines