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 | |
5 | our $WORLDMAP_UPDATE_INTERVAL = $cf::CFG{worldmap_update_interval} || 10; |
5 | our $WORLDMAP_UPDATE_INTERVAL = $cf::CFG{worldmap_update_interval} || 2; |
6 | |
6 | |
7 | our $GENCOUNT = 0; |
7 | our $GENCOUNT = 0; |
8 | our %PLAYERINFO; |
8 | our %PLAYERINFO; |
9 | |
9 | |
|
|
10 | our ($MAPW, $MAPH) = (1024, 1024); # it's useful to know the map width/height in pixels |
|
|
11 | |
10 | sub update_worldmap { |
12 | sub 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"), |
|
|
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 | |
32 | sub create_widgets { |
58 | sub create_widgets { |
33 | my ($ns) = @_; |
59 | my ($ns) = @_; |
34 | |
60 | |
… | |
… | |
47 | $_[0]{visibility} = $_[1]; |
73 | $_[0]{visibility} = $_[1]; |
48 | update_worldmap $_[0]{ws} if $_[1]; |
74 | update_worldmap $_[0]{ws} if $_[1]; |
49 | }, |
75 | }, |
50 | ); |
76 | ); |
51 | |
77 | |
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)); |
78 | $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)); |
79 | $sw->add (my $canvas = $ws->{canvas} = $ws->new (Canvas => expand => 1)); |
58 | |
80 | |
59 | $ws->{mapface} = $ws->new (Face => |
81 | $ws->{mapface} = $ws->new (Face => |
60 | expand => 1, |
|
|
61 | size_w => undef, |
82 | size_w => undef, |
62 | size_h => undef, |
83 | size_h => undef, |
63 | face => $face, |
84 | face => $ws->{ns}->need_face ("res/worldmap"), |
64 | ); |
85 | ); |
65 | |
86 | |
66 | $ws->{canvas}->add ($ws->{mapface}); |
87 | $ws->{canvas}->add ($ws->{mapface}); |
67 | |
88 | |
68 | $ws |
89 | $ws |
… | |
… | |
84 | cf::override 1; |
105 | cf::override 1; |
85 | }, |
106 | }, |
86 | ; |
107 | ; |
87 | |
108 | |
88 | cf::async_ext { |
109 | cf::async_ext { |
|
|
110 | $Coro::current->{desc} = "worldmap updater"; |
89 | my $schedule_interval = Coro::Event->timer (after => 1); |
111 | my $schedule_interval = Coro::Event->timer (after => 1, data => cf::WF_AUTOCANCEL); |
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} |