… | |
… | |
48 | sort { (length $a) <=> (length $b) } |
48 | sort { (length $a) <=> (length $b) } |
49 | @args |
49 | @args |
50 | } sort { $a->{par} <=> $b->{par} } |
50 | } sort { $a->{par} <=> $b->{par} } |
51 | CFPlus::Pod::find command => "*"; |
51 | CFPlus::Pod::find command => "*"; |
52 | |
52 | |
53 | $self->connect_ext (event_music => sub { |
53 | $self->connect_ext (music => sub { |
54 | my ($ev) = @_; |
54 | my (%ev) = @_; |
55 | |
55 | |
56 | return unless $::CFG->{bgm_enable}; |
56 | return unless $::CFG->{bgm_enable}; |
57 | |
57 | |
58 | my $faces = $ev->{faces}; |
58 | my $faces = $ev{faces}; |
59 | my @songs; |
59 | my @songs; |
60 | |
60 | |
61 | # request music from server if appropriate |
61 | # request music from server if appropriate |
62 | if (my $faces = $ev->{faces}) { |
62 | if (my $faces = $ev{faces}) { |
63 | for (@$faces) { |
63 | for (@$faces) { |
64 | my ($face, $pri, $chksum) = @$_; |
64 | my ($face, $pri, $chksum) = @$_; |
65 | utf8::downgrade $chksum; |
65 | utf8::downgrade $chksum; |
66 | |
66 | |
67 | $chksum = unpack "H*", $chksum; |
67 | $chksum = unpack "H*", $chksum; |
… | |
… | |
97 | } |
97 | } |
98 | } |
98 | } |
99 | } |
99 | } |
100 | } |
100 | } |
101 | |
101 | |
102 | if (my $play = $ev->{play}) { |
102 | if (my $play = $ev{play}) { |
103 | &::audio_music_set ($self->{songs} = [map $self->{music_map}{$_}, @$play]); |
103 | &::audio_music_set ($self->{songs} = [map $self->{music_map}{$_}, @$play]); |
104 | } |
104 | } |
105 | }); |
105 | }); |
106 | |
106 | |
107 | $self->connect_ext (event_capabilities => sub { |
107 | $self->connect_ext (capabilities => sub { |
108 | my ($cap) = @_; |
108 | my (%cap) = @_; |
109 | |
109 | |
110 | if (my $ts = $cap->{tileset}) { |
110 | if (my $ts = $cap{tileset}) { |
111 | if (my ($default) = grep $_->[2] & 1, @$ts) { |
111 | if (my ($default) = grep $_->[2] & 1, @$ts) { |
112 | $self->{tileset} = $default; |
112 | $self->{tileset} = $default; |
113 | $self->{tilesize} = $default->[3]; |
113 | $self->{tilesize} = $default->[3]; |
114 | $self->setup_req (tileset => $default->[0]); |
114 | $self->setup_req (tileset => $default->[0]); |
115 | |
115 | |
… | |
… | |
209 | my ($arg) = @_; |
209 | my ($arg) = @_; |
210 | |
210 | |
211 | my $w = $self->{widget}{$arg->{id}} |
211 | my $w = $self->{widget}{$arg->{id}} |
212 | or return; |
212 | or return; |
213 | |
213 | |
214 | $w->{$arg->{name}} = $arg->{value}; |
214 | my $attr = $arg->{attr}; |
|
|
215 | |
|
|
216 | for (my $i = 0; $i < $#$attr; $i += 2) { |
|
|
217 | my ($member, $value) = @$attr[$i, $i+1]; |
|
|
218 | if (defined $value) { |
|
|
219 | $w->{$member} = $value; |
|
|
220 | } else { |
|
|
221 | delete $w->{$member}; |
|
|
222 | } |
|
|
223 | $w->{parent}->realloc if $member =~ /^c_/ && $w->{visible}; |
|
|
224 | } |
215 | }); |
225 | }); |
216 | |
226 | |
217 | # widget get |
227 | # widget get |
218 | $self->connect_ext (w_g => sub { |
228 | $self->connect_ext (w_g => sub { |
219 | my ($arg) = @_; |
229 | my ($arg) = @_; |
220 | |
230 | |
221 | my $w = $self->{widget}{$arg->{id}} |
231 | my $w = $self->{widget}{$arg->{id}} |
222 | or return; |
232 | or return; |
223 | |
233 | |
224 | $self->send_exti_msg (w_r => rid => $arg->{rid}, res => [$w->{$arg->{name}}]); |
234 | $self->send_exti_msg (w_r => rid => $arg->{rid}, res => [map $w->{$_}, @{$arg->{attr}}]); |
225 | }); |
235 | }); |
226 | |
236 | |
227 | # destroy widgets on logout |
237 | # destroy widgets on logout |
228 | $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub { |
238 | $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub { |
229 | for my $ws (values %{delete $self->{widgetset} || {}}) { |
239 | for my $ws (values %{delete $self->{widgetset} || {}}) { |
… | |
… | |
1161 | } |
1171 | } |
1162 | |
1172 | |
1163 | sub logged_in { |
1173 | sub logged_in { |
1164 | my ($self) = @_; |
1174 | my ($self) = @_; |
1165 | |
1175 | |
1166 | $self->send_ext_req (cfplus_support => version => 1, sub { |
1176 | $self->send_ext_req (cfplus_support => version => 2, sub { |
|
|
1177 | my (%msg) = @_; |
|
|
1178 | |
1167 | $self->{cfplus_ext} = $_[0]{version}; |
1179 | $self->{cfplus_ext} = $msg{version}; |
1168 | $self->update_server_info; |
1180 | $self->update_server_info; |
1169 | |
1181 | |
1170 | if ($self->{cfplus_ext} >= 2) { |
1182 | if ($self->{cfplus_ext} >= 2) { |
1171 | $self->send_ext_req ("editor_support", sub { |
1183 | $self->send_ext_req ("editor_support", sub { |
1172 | $self->{editor_support} = $_[0]; |
1184 | $self->{editor_support} = { @_ }; |
1173 | $self->update_server_info; |
1185 | $self->update_server_info; |
1174 | |
1186 | |
1175 | 0 |
1187 | 0 |
1176 | }); |
1188 | }); |
1177 | } |
1189 | } |
… | |
… | |
1197 | |
1209 | |
1198 | sub lookat { |
1210 | sub lookat { |
1199 | my ($self, $x, $y) = @_; |
1211 | my ($self, $x, $y) = @_; |
1200 | |
1212 | |
1201 | if ($self->{cfplus_ext}) { |
1213 | if ($self->{cfplus_ext}) { |
1202 | $self->send_ext_req (lookat => dx => $x, dy => $y, sub { |
1214 | $self->send_ext_req (lookat => $x, $y, sub { |
1203 | my ($msg) = @_; |
1215 | my (%msg) = @_; |
1204 | |
1216 | |
1205 | if (exists $msg->{npc_dialog}) { |
1217 | if (exists $msg{npc_dialog}) { |
1206 | # start npc chat dialog |
1218 | # start npc chat dialog |
1207 | $self->{npc_dialog} = new CFPlus::NPCDialog:: |
1219 | $self->{npc_dialog} = new CFPlus::NPCDialog:: |
1208 | dx => $x, |
1220 | token => $msg{npc_dialog}, |
1209 | dy => $y, |
|
|
1210 | title => "$msg->{npc_dialog} (NPC)", |
1221 | title => "$msg{npc_dialog}[0] (NPC)", |
1211 | conn => $self, |
1222 | conn => $self, |
1212 | ; |
1223 | ; |
1213 | } |
1224 | } |
1214 | }); |
1225 | }); |
1215 | } |
1226 | } |
… | |
… | |
1280 | on_activate => sub { $this->destroy; 1 }, |
1291 | on_activate => sub { $this->destroy; 1 }, |
1281 | ; |
1292 | ; |
1282 | |
1293 | |
1283 | $self->update_options; |
1294 | $self->update_options; |
1284 | |
1295 | |
1285 | $self->{id} = $self->{conn}->send_ext_req ( |
1296 | $self->{id} = "npc-channel-" . $self->{conn}->token; |
1286 | npc_dialog_begin => dx => $self->{dx}, dy => $self->{dy}, |
1297 | $self->{conn}->connect_ext ($self->{id} => sub { |
1287 | sub { $this && $this->feed (@_) } |
1298 | $this->feed (@_) if $this; |
1288 | ); |
1299 | }); |
|
|
1300 | |
|
|
1301 | $self->{conn}->send_ext_msg (npc_dialog_begin => $self->{id}, $self->{token}); |
1289 | |
1302 | |
1290 | $self->{entry}->grab_focus; |
1303 | $self->{entry}->grab_focus; |
1291 | |
1304 | |
1292 | $self->{textview}->add_paragraph ({ |
1305 | $self->{textview}->add_paragraph ({ |
1293 | fg => [1, 1, 0, 1], |
1306 | fg => [1, 1, 0, 1], |
… | |
… | |
1316 | ); |
1329 | ); |
1317 | } |
1330 | } |
1318 | } |
1331 | } |
1319 | |
1332 | |
1320 | sub feed { |
1333 | sub feed { |
1321 | my ($self, $msg) = @_; |
1334 | my ($self, $type, @arg) = @_; |
1322 | |
1335 | |
1323 | CFPlus::weaken $self; |
1336 | CFPlus::weaken $self; |
1324 | |
1337 | |
1325 | if ($msg->{msgtype} eq "reply") { |
1338 | if ($type eq "update") { |
|
|
1339 | my (%info) = @arg; |
|
|
1340 | |
1326 | $self->{kw}{$_} = 1 for @{$msg->{add_topics} || []}; |
1341 | $self->{kw}{$_} = 1 for @{$info{add_topics} || []}; |
1327 | $self->{kw}{$_} = 0 for @{$msg->{del_topics} || []}; |
1342 | $self->{kw}{$_} = 0 for @{$info{del_topics} || []}; |
1328 | |
|
|
1329 | my $text = "\n" . CFPlus::Protocol::sanitise_xml $msg->{msg}; |
|
|
1330 | my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} }; |
|
|
1331 | my @link; |
|
|
1332 | $text =~ s{ |
|
|
1333 | ($match) |
|
|
1334 | }{ |
|
|
1335 | my $kw = $1; |
|
|
1336 | |
|
|
1337 | push @link, new CFPlus::UI::Label |
|
|
1338 | markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>", |
|
|
1339 | can_hover => 1, |
|
|
1340 | can_events => 1, |
|
|
1341 | padding_x => 0, |
|
|
1342 | padding_y => 0, |
|
|
1343 | on_button_up => sub { |
|
|
1344 | $self->send ($kw); |
|
|
1345 | }; |
|
|
1346 | |
|
|
1347 | "\x{fffc}" |
|
|
1348 | }giex; |
|
|
1349 | |
1343 | |
|
|
1344 | if (exists $info{msg}) { |
|
|
1345 | my $text = "\n" . CFPlus::Protocol::sanitise_xml $info{msg}; |
|
|
1346 | my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} }; |
|
|
1347 | my @link; |
|
|
1348 | $text =~ s{ |
|
|
1349 | ($match) |
|
|
1350 | }{ |
|
|
1351 | my $kw = $1; |
|
|
1352 | |
|
|
1353 | push @link, new CFPlus::UI::Label |
|
|
1354 | markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>", |
|
|
1355 | can_hover => 1, |
|
|
1356 | can_events => 1, |
|
|
1357 | padding_x => 0, |
|
|
1358 | padding_y => 0, |
|
|
1359 | on_button_up => sub { |
|
|
1360 | $self->send ($kw); |
|
|
1361 | }; |
|
|
1362 | |
|
|
1363 | "\x{fffc}" |
|
|
1364 | }giex; |
|
|
1365 | |
1350 | $self->{textview}->add_paragraph ({ markup => $text, widget => \@link }); |
1366 | $self->{textview}->add_paragraph ({ markup => $text, widget => \@link }); |
1351 | $self->{textview}->scroll_to_bottom; |
1367 | $self->{textview}->scroll_to_bottom; |
|
|
1368 | } |
|
|
1369 | |
1352 | $self->update_options; |
1370 | $self->update_options; |
1353 | } else { |
1371 | } else { |
1354 | $self->destroy; |
1372 | $self->destroy; |
1355 | } |
1373 | } |
1356 | |
1374 | |
… | |
… | |
1361 | my ($self, $msg) = @_; |
1379 | my ($self, $msg) = @_; |
1362 | |
1380 | |
1363 | $self->{textview}->add_paragraph ({ markup => "\n" . CFPlus::asxml $msg }); |
1381 | $self->{textview}->add_paragraph ({ markup => "\n" . CFPlus::asxml $msg }); |
1364 | $self->{textview}->scroll_to_bottom; |
1382 | $self->{textview}->scroll_to_bottom; |
1365 | |
1383 | |
1366 | $self->{conn}->send_ext_msg (npc_dialog_tell => msgid => $self->{id}, msg => $msg); |
1384 | $self->{conn}->send_ext_msg (npc_dialog_tell => $self->{id}, $msg); |
1367 | } |
1385 | } |
1368 | |
1386 | |
1369 | sub destroy { |
1387 | sub destroy { |
1370 | my ($self) = @_; |
1388 | my ($self) = @_; |
1371 | |
1389 | |
1372 | #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1 |
1390 | #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1 |
1373 | |
1391 | |
1374 | if ($self->{conn}) { |
1392 | if ($self->{conn}) { |
1375 | $self->{conn}->send_ext_msg (npc_dialog_end => msgid => $self->{id}) if $self->{id}; |
1393 | $self->{conn}->send_ext_msg (npc_dialog_end => $self->{id}) if $self->{id}; |
1376 | delete $self->{conn}{npc_dialog}; |
1394 | delete $self->{conn}{npc_dialog}; |
1377 | $self->{conn}->disconnect_ext ($self->{id}); |
1395 | $self->{conn}->disconnect_ext ($self->{id}); |
1378 | } |
1396 | } |
1379 | |
1397 | |
1380 | $self->SUPER::destroy; |
1398 | $self->SUPER::destroy; |