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 |
|
|
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, |
36 |
|
|
face => $marker, |
37 |
|
|
); |
38 |
|
|
my $children = [$label, $marker]; |
39 |
|
|
|
40 |
|
|
$ws->{canvas}->add (my $vbox = $ws->new (VBox => |
41 |
|
|
children => $children, |
42 |
root |
1.5 |
c_halign => -.5, |
43 |
|
|
c_valign => -1, |
44 |
root |
1.9 |
)); |
45 |
|
|
$vbox->{children} = $children; |
46 |
|
|
|
47 |
|
|
$vbox |
48 |
root |
1.7 |
}; |
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; |
57 |
root |
1.5 |
} |
58 |
root |
1.4 |
} |
59 |
root |
1.7 |
|
60 |
|
|
$ws->{labels} = $new; |
61 |
root |
1.5 |
} |
62 |
root |
1.1 |
|
63 |
|
|
sub create_widgets { |
64 |
|
|
my ($ns) = @_; |
65 |
|
|
|
66 |
root |
1.5 |
my $ws = $ns->new_widgetset; |
67 |
root |
1.1 |
|
68 |
root |
1.5 |
$ws->{toplevel} = my $w = $ws->new (Toplevel => |
69 |
root |
1.2 |
title => "Worldmap", |
70 |
|
|
name => "server_item_worldmap", |
71 |
root |
1.1 |
force_w => 400, |
72 |
|
|
force_h => 400, |
73 |
|
|
x => "center", |
74 |
|
|
y => "center", |
75 |
|
|
has_close_button => 1, |
76 |
|
|
on_delete => sub { shift->hide }, |
77 |
root |
1.5 |
on_visibility_change => sub { |
78 |
root |
1.9 |
warn "VCHANGE <@_>\n";#d# |
79 |
root |
1.5 |
$_[0]{visibility} = $_[1]; |
80 |
|
|
update_worldmap $_[0]{ws} if $_[1]; |
81 |
|
|
}, |
82 |
root |
1.1 |
); |
83 |
|
|
|
84 |
|
|
my $face = cf::face::find "res/worldmap.jpg"; |
85 |
|
|
$ns->send_face ($face); |
86 |
root |
1.3 |
$ns->flush_fx; |
87 |
root |
1.1 |
|
88 |
root |
1.7 |
$w->add (my $sw = $ws->{window} = $ws->new (ScrolledWindow => scroll_x => 1, scroll_y => 1)); |
89 |
root |
1.5 |
$sw->add (my $canvas = $ws->{canvas} = $ws->new (Canvas => expand => 1)); |
90 |
root |
1.1 |
|
91 |
root |
1.5 |
$ws->{mapface} = $ws->new (Face => |
92 |
|
|
expand => 1, |
93 |
|
|
size_w => undef, |
94 |
|
|
size_h => undef, |
95 |
|
|
face => $face, |
96 |
|
|
); |
97 |
root |
1.1 |
|
98 |
root |
1.5 |
$ws->{canvas}->add ($ws->{mapface}); |
99 |
root |
1.4 |
|
100 |
root |
1.5 |
$ws |
101 |
root |
1.4 |
} |
102 |
root |
1.1 |
|
103 |
|
|
cf::object::attachment item_worldmap => |
104 |
|
|
on_apply => sub { |
105 |
|
|
my ($self, $who) = @_; |
106 |
|
|
|
107 |
|
|
my $ns = $who->contr->ns; |
108 |
|
|
|
109 |
root |
1.2 |
if ($ns->{can_widget}) { |
110 |
|
|
my $ws = $ns->{ws_worldmap} ||= create_widgets $ns; |
111 |
root |
1.4 |
$ws->{toplevel}->toggle_visibility; |
112 |
root |
1.2 |
} else { |
113 |
|
|
$ns->send_msg ("log", "Your client doesn't support the (required) widget extension. Try CFPlus at http://crossfire.schmorp.de/.", cf::NDI_RED); |
114 |
|
|
} |
115 |
root |
1.1 |
|
116 |
|
|
cf::override 1; |
117 |
|
|
}, |
118 |
|
|
; |
119 |
|
|
|
120 |
root |
1.5 |
cf::async_ext { |
121 |
|
|
my $schedule_interval = Coro::Event->timer (after => 1); |
122 |
|
|
|
123 |
|
|
while () { |
124 |
|
|
$schedule_interval->interval ($WORLDMAP_UPDATE_INTERVAL); |
125 |
|
|
$schedule_interval->next; |
126 |
|
|
|
127 |
root |
1.7 |
cf::get_slot 0.01, -50, "worldmap update"; |
128 |
root |
1.6 |
|
129 |
|
|
++$GENCOUNT; |
130 |
|
|
|
131 |
root |
1.5 |
# recalculate player info |
132 |
root |
1.6 |
my %new; |
133 |
root |
1.5 |
for (values %cf::PLAYER) { |
134 |
|
|
my $map = $_->ob->map |
135 |
|
|
or next; |
136 |
|
|
$map =~ /^\/world\/world_(\d\d\d)_(\d\d\d)/ |
137 |
|
|
or next; |
138 |
root |
1.6 |
|
139 |
root |
1.5 |
my $ob = $_->ob; |
140 |
|
|
my $x = ($1 - 100) * 50 + $ob->x; |
141 |
|
|
my $y = ($2 - 100) * 50 + $ob->y; |
142 |
|
|
|
143 |
|
|
0 <= $x && 0 <= $y && $x < 1500 && $y < 1500 |
144 |
|
|
or next; |
145 |
|
|
|
146 |
root |
1.7 |
$x = int $x * $MAPW / 1500; |
147 |
|
|
$y = int $y * $MAPH / 1500; |
148 |
root |
1.6 |
|
149 |
|
|
my $name = $ob->name; |
150 |
|
|
|
151 |
|
|
if (my $pi = delete $PLAYERINFO{$name}) { |
152 |
|
|
if ($pi->[0] == $x && $pi->[1] == $y) { |
153 |
|
|
$new{$name} = $pi; |
154 |
|
|
next; |
155 |
|
|
} |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
$new{$name} = [$x, $y]; |
159 |
root |
1.5 |
} |
160 |
|
|
|
161 |
root |
1.7 |
*PLAYERINFO = \%new; |
162 |
|
|
|
163 |
|
|
cf::get_slot 0.03, -50, "worldmap socket update"; |
164 |
root |
1.5 |
for (values %cf::PLAYER) { |
165 |
|
|
my $ns = $_->ns |
166 |
|
|
or next; |
167 |
root |
1.6 |
|
168 |
root |
1.5 |
update_worldmap $ns->{ws_worldmap} |
169 |
|
|
if $ns->{ws_worldmap} && $ns->{ws_worldmap}{toplevel}{visibility}; |
170 |
|
|
} |
171 |
|
|
} |
172 |
|
|
}; |
173 |
|
|
|