ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/item-worldmap.ext
Revision: 1.8
Committed: Mon Jul 23 21:02:50 2007 UTC (16 years, 10 months ago) by root
Branch: MAIN
Changes since 1.7: +3 -1 lines
Log Message:
- new extcmd version, much streamlined
- new npc dialogue protocol
- older cfplus clients are supported, without cfplus_support (no dialogue),
  for some time.
- improved worldmap support.

File Contents

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