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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines