ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/item-worldmap.ext
Revision: 1.19
Committed: Fri Sep 19 01:39:45 2008 UTC (15 years, 8 months ago) by root
Branch: MAIN
Changes since 1.18: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.5 #! perl # depends=widget mandatory
2 root 1.4
3     # this module implements a rather fancy worldmap
4    
5 root 1.7 our $WORLDMAP_UPDATE_INTERVAL = $cf::CFG{worldmap_update_interval} || 2;
6 root 1.4
7 root 1.6 our $GENCOUNT = 0;
8 root 1.5 our %PLAYERINFO;
9    
10 root 1.7 our ($MAPW, $MAPH) = (1024, 1024); # it's useful to know the map width/height in pixels
11    
12 root 1.5 sub update_worldmap {
13     my ($ws) = @_;
14    
15 root 1.7 return if $GENCOUNT == $ws->{gencount};
16     $ws->{gencount} = $GENCOUNT;
17    
18     my $old = delete $ws->{labels};
19     my $new;
20    
21     my $name = $ws->{ns}->pl->ob->name;
22 root 1.4
23 root 1.5 while (my ($k, $v) = each %PLAYERINFO) {
24 root 1.8 my $label = (delete $old->{$k}) || do {
25 root 1.7 my $label = $ws->new (Label =>
26 root 1.5 text => $k,
27     fontsize => 0.2,
28 root 1.9 );
29    
30 root 1.10 my $marker = $ws->new (Face =>
31 root 1.14 face => $ws->{ns}->need_face ("res/map-arrow"),
32 root 1.9 );
33     my $children = [$label, $marker];
34    
35     $ws->{canvas}->add (my $vbox = $ws->new (VBox =>
36     children => $children,
37 root 1.5 c_halign => -.5,
38     c_valign => -1,
39 root 1.9 ));
40     $vbox->{children} = $children;
41    
42     $vbox
43 root 1.7 };
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;
52 root 1.5 }
53 root 1.4 }
54 root 1.7
55     $ws->{labels} = $new;
56 root 1.5 }
57 root 1.1
58     sub create_widgets {
59     my ($ns) = @_;
60    
61 root 1.5 my $ws = $ns->new_widgetset;
62 root 1.1
63 root 1.5 $ws->{toplevel} = my $w = $ws->new (Toplevel =>
64 root 1.2 title => "Worldmap",
65     name => "server_item_worldmap",
66 root 1.1 force_w => 400,
67     force_h => 400,
68     x => "center",
69     y => "center",
70     has_close_button => 1,
71     on_delete => sub { shift->hide },
72 root 1.5 on_visibility_change => sub {
73     $_[0]{visibility} = $_[1];
74     update_worldmap $_[0]{ws} if $_[1];
75     },
76 root 1.1 );
77    
78 root 1.7 $w->add (my $sw = $ws->{window} = $ws->new (ScrolledWindow => scroll_x => 1, scroll_y => 1));
79 root 1.5 $sw->add (my $canvas = $ws->{canvas} = $ws->new (Canvas => expand => 1));
80 root 1.1
81 root 1.5 $ws->{mapface} = $ws->new (Face =>
82 root 1.12 size_w => undef,
83     size_h => undef,
84 root 1.14 face => $ws->{ns}->need_face ("res/worldmap"),
85 root 1.5 );
86 root 1.1
87 root 1.5 $ws->{canvas}->add ($ws->{mapface});
88 root 1.4
89 root 1.5 $ws
90 root 1.4 }
91 root 1.1
92     cf::object::attachment item_worldmap =>
93     on_apply => sub {
94     my ($self, $who) = @_;
95    
96     my $ns = $who->contr->ns;
97    
98 root 1.2 if ($ns->{can_widget}) {
99     my $ws = $ns->{ws_worldmap} ||= create_widgets $ns;
100 root 1.4 $ws->{toplevel}->toggle_visibility;
101 root 1.2 } else {
102 root 1.19 $ns->send_msg ("log", "Your client doesn't support the (required) widget extension. Try the deliantra client at http://www.deliantra.net/.", cf::NDI_RED);
103 root 1.2 }
104 root 1.1
105     cf::override 1;
106     },
107     ;
108    
109 root 1.5 cf::async_ext {
110 root 1.16 $Coro::current->{desc} = "worldmap updater";
111 root 1.5
112     while () {
113 root 1.18 Coro::EV::timer_once $WORLDMAP_UPDATE_INTERVAL;
114 root 1.5
115 root 1.7 cf::get_slot 0.01, -50, "worldmap update";
116 root 1.6
117     ++$GENCOUNT;
118    
119 root 1.5 # recalculate player info
120 root 1.6 my %new;
121 root 1.5 for (values %cf::PLAYER) {
122     my $map = $_->ob->map
123     or next;
124     $map =~ /^\/world\/world_(\d\d\d)_(\d\d\d)/
125     or next;
126 root 1.6
127 root 1.5 my $ob = $_->ob;
128     my $x = ($1 - 100) * 50 + $ob->x;
129     my $y = ($2 - 100) * 50 + $ob->y;
130    
131     0 <= $x && 0 <= $y && $x < 1500 && $y < 1500
132     or next;
133    
134 root 1.7 $x = int $x * $MAPW / 1500;
135     $y = int $y * $MAPH / 1500;
136 root 1.6
137     my $name = $ob->name;
138    
139     if (my $pi = delete $PLAYERINFO{$name}) {
140     if ($pi->[0] == $x && $pi->[1] == $y) {
141     $new{$name} = $pi;
142     next;
143     }
144     }
145    
146     $new{$name} = [$x, $y];
147 root 1.5 }
148    
149 root 1.7 *PLAYERINFO = \%new;
150    
151     cf::get_slot 0.03, -50, "worldmap socket update";
152 root 1.5 for (values %cf::PLAYER) {
153     my $ns = $_->ns
154     or next;
155 root 1.6
156 root 1.5 update_worldmap $ns->{ws_worldmap}
157     if $ns->{ws_worldmap} && $ns->{ws_worldmap}{toplevel}{visibility};
158     }
159     }
160     };
161