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.10 by root, Mon Jul 23 23:46:03 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,
22 c_rel => 1, 28 );
29
30 my $marker = $ws->new (Face =>
31 size_w => undef,
32 size_h => undef,
33 face => $ws->{ns}->find_face ("res/map-arrow.png"),
34 );
35 my $children = [$label, $marker];
36
37 $ws->{canvas}->add (my $vbox = $ws->new (VBox =>
38 children => $children,
23 c_halign => -.5, 39 c_halign => -.5,
24 c_valign => -1, 40 c_valign => -1,
25 ); 41 ));
26 $ws->{canvas}->add ($ws->{labels}{$k}); 42 $vbox->{children} = $children;
43
44 $vbox
45 };
46
47 $new->{$k} = $label;
48
49 if ($v != $label->{prevpos}) {
50 $label->set (c_x => $v->[0], c_y => $v->[1]);
51 $label->{prevpos} = $v;
52 $ws->{window}->make_visible ($v->[0], $v->[1], .2)
53 if $k eq $name;
27 } 54 }
28 $ws->{labels}{$k}->set (c_x => $v->[0], c_y => $v->[1]);
29 } 55 }
56
57 $ws->{labels} = $new;
30} 58}
31 59
32sub create_widgets { 60sub create_widgets {
33 my ($ns) = @_; 61 my ($ns) = @_;
34 62
42 x => "center", 70 x => "center",
43 y => "center", 71 y => "center",
44 has_close_button => 1, 72 has_close_button => 1,
45 on_delete => sub { shift->hide }, 73 on_delete => sub { shift->hide },
46 on_visibility_change => sub { 74 on_visibility_change => sub {
75 warn "VCHANGE <@_>\n";#d#
47 $_[0]{visibility} = $_[1]; 76 $_[0]{visibility} = $_[1];
48 update_worldmap $_[0]{ws} if $_[1]; 77 update_worldmap $_[0]{ws} if $_[1];
49 }, 78 },
50 ); 79 );
51 80
52 my $face = cf::face::find "res/worldmap.jpg";
53 $ns->send_face ($face);
54 $ns->flush_fx;
55
56 $w->add (my $sw = $ws->new (ScrolledWindow => scroll_x => 1, scroll_y => 1)); 81 $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)); 82 $sw->add (my $canvas = $ws->{canvas} = $ws->new (Canvas => expand => 1));
58 83
59 $ws->{mapface} = $ws->new (Face => 84 $ws->{mapface} = $ws->new (Face =>
60 expand => 1, 85 expand => 1,
61 size_w => undef, 86 size_w => undef,
62 size_h => undef, 87 size_h => undef,
63 face => $face, 88 face => $ws->{ns}->find_face ("res/worldmap.jpg"),
64 ); 89 );
65 90
66 $ws->{canvas}->add ($ws->{mapface}); 91 $ws->{canvas}->add ($ws->{mapface});
67 92
68 $ws 93 $ws
90 115
91 while () { 116 while () {
92 $schedule_interval->interval ($WORLDMAP_UPDATE_INTERVAL); 117 $schedule_interval->interval ($WORLDMAP_UPDATE_INTERVAL);
93 $schedule_interval->next; 118 $schedule_interval->next;
94 119
95 cf::get_slot 0.05; 120 cf::get_slot 0.01, -50, "worldmap update";
96 121
97 ++$GENCOUNT; 122 ++$GENCOUNT;
98 123
99 # recalculate player info 124 # recalculate player info
100 my %new; 125 my %new;
109 my $y = ($2 - 100) * 50 + $ob->y; 134 my $y = ($2 - 100) * 50 + $ob->y;
110 135
111 0 <= $x && 0 <= $y && $x < 1500 && $y < 1500 136 0 <= $x && 0 <= $y && $x < 1500 && $y < 1500
112 or next; 137 or next;
113 138
114 # rounding saves network bandwidth... 139 $x = int $x * $MAPW / 1500;
115 $x = sprintf "%.3f", $x / 1500; 140 $y = int $y * $MAPH / 1500;
116 $y = sprintf "%.3f", $y / 1500;
117 141
118 my $name = $ob->name; 142 my $name = $ob->name;
119 143
120 if (my $pi = delete $PLAYERINFO{$name}) { 144 if (my $pi = delete $PLAYERINFO{$name}) {
121 if ($pi->[0] == $x && $pi->[1] == $y) { 145 if ($pi->[0] == $x && $pi->[1] == $y) {
125 } 149 }
126 150
127 $new{$name} = [$x, $y]; 151 $new{$name} = [$x, $y];
128 } 152 }
129 153
154 *PLAYERINFO = \%new;
155
156 cf::get_slot 0.03, -50, "worldmap socket update";
130 for (values %cf::PLAYER) { 157 for (values %cf::PLAYER) {
131 my $ns = $_->ns 158 my $ns = $_->ns
132 or next; 159 or next;
133 160
134 update_worldmap $ns->{ws_worldmap} 161 update_worldmap $ns->{ws_worldmap}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines