1 |
#! perl # mandatory |
2 |
|
3 |
# the setup command |
4 |
|
5 |
use JSON::XS (); |
6 |
use List::Util qw(min max); |
7 |
|
8 |
sub send_capabilities { |
9 |
my ($ns) = @_; |
10 |
|
11 |
return unless $ns->extcmd; |
12 |
|
13 |
$ns->ext_msg (capabilities => |
14 |
# id, name, flags (1 == 2d), edge length |
15 |
tileset => [[1, "default 64x64 faceset", 1, 64], [0, "default 32x32 faceset", 1, 32], [2, "default text faceset", 2, 1]], |
16 |
); |
17 |
} |
18 |
|
19 |
sub do_setup { |
20 |
my ($ns, $setup) = @_; |
21 |
|
22 |
my %orig = %$setup; |
23 |
|
24 |
while (my ($k, $v) = each %$setup) { |
25 |
if ($k eq "sound") { |
26 |
$ns->sound ($v); |
27 |
|
28 |
} elsif ($k eq "spellmon") { |
29 |
$ns->monitor_spells ($v); |
30 |
|
31 |
} elsif ($k eq "mapinfocmd") { |
32 |
$ns->mapinfocmd ($v); |
33 |
|
34 |
} elsif ($k eq "extcmd") { |
35 |
$ns->extcmd (min 2, $v); |
36 |
send_capabilities $ns; |
37 |
|
38 |
} elsif ($k eq "faceset") { |
39 |
$ns->faceset (0); |
40 |
$setup->{$k} = 0; |
41 |
# $ns->image2 (1) |
42 |
|
43 |
} elsif ($k eq "tileset") { |
44 |
$setup->{$k} = $ns->faceset (int cf::clamp $v, 0, 2); |
45 |
|
46 |
} elsif ($k eq "itemcmd") { |
47 |
# Version of the item protocol command to use. Currently, |
48 |
# only supported versions are 1 and 2. Using a numeric |
49 |
# value will make it very easy to extend this in the future. |
50 |
$ns->itemcmd ($v) if $v >= 1 && $v <= 2; |
51 |
|
52 |
$setup->{$k} = $ns->itemcmd; |
53 |
|
54 |
} elsif ($k eq "mapsize") { |
55 |
my ($x, $y) = split /x/, $v; |
56 |
|
57 |
# we *need* to make sure we use an odd map size, as the remaining |
58 |
# code relies on this. |
59 |
$ns->mapx ($x = max 9, min +(cf::MAP_CLIENT_X - 1) | 1, ($x - 1) | 1); |
60 |
$ns->mapy ($y = max 9, min +(cf::MAP_CLIENT_Y - 1) | 1, ($y - 1) | 1); |
61 |
|
62 |
$setup->{$k} = "${x}x${y}"; |
63 |
|
64 |
} elsif ($k eq "extendedTextInfos") { |
65 |
$ns->has_readable_type ($v); |
66 |
|
67 |
} elsif ($k eq "smoothing") { # cfplus-style smoothing |
68 |
$ns->smoothing ($v); |
69 |
|
70 |
} elsif ($k eq "widget") { |
71 |
# server-side widgets |
72 |
$v = $v > 1; |
73 |
$ns->{can_widget} = $v; |
74 |
$ns->fx_want (6 => 1); # need support for RSRC |
75 |
$setup->{$k} = $v ? 2 : 0; |
76 |
|
77 |
} elsif ($k eq "lzf") { |
78 |
# the lzf packet simply contains an lzf-compressed packet as argument |
79 |
$ns->{can_lzf} = $v == 1; |
80 |
|
81 |
} elsif ($k eq "frag") { |
82 |
# the frag packet contains data which gets appended to the existing packet buffer. |
83 |
# empty frag packet means end of packet. |
84 |
$ns->{can_frag} = $v == 1; |
85 |
|
86 |
} elsif ($k eq "excmd") { |
87 |
# we support it |
88 |
|
89 |
} else { |
90 |
# other commands: |
91 |
# sexp: no idea, probably for oudated servers |
92 |
# tick: more stupidity, server should send a tick per tick |
93 |
|
94 |
$setup->{$k} = "FALSE"; |
95 |
} |
96 |
} |
97 |
|
98 |
# force some mandatory protocol options, most of these |
99 |
# are for obsolete clients only |
100 |
$setup->{darkness} = 1; |
101 |
$setup->{exp64} = 1; |
102 |
$setup->{extmap} = 1; |
103 |
$setup->{facecache} = 1; |
104 |
$setup->{fxix} = 3; |
105 |
$setup->{map1acmd} = 1; |
106 |
$setup->{map1cmd} = 0; |
107 |
$setup->{msg} = 1; |
108 |
|
109 |
cf::datalog setup => |
110 |
request => \%orig, |
111 |
reply => $setup, |
112 |
; |
113 |
} |
114 |
|
115 |
cf::client->attach (on_setup => sub { |
116 |
my ($ns, $args) = @_; |
117 |
|
118 |
# run through the cmds of setup |
119 |
# syntax is setup <cmdname1> <parameter> <cmdname2> <parameter> ... |
120 |
# or setup json-object |
121 |
# |
122 |
# we send the status of the cmd back, or a FALSE is the cmd if the server unknown |
123 |
# the client then must sort this out |
124 |
|
125 |
if ($args =~ /^\s*\{/) { |
126 |
my $setup = eval { JSON::XS::decode_json $args } || {}; |
127 |
do_setup $ns, $setup; |
128 |
$ns->send_packet ("setup " . JSON::XS::encode_json $setup); |
129 |
} else { |
130 |
my %setup = split / +/, $args; |
131 |
do_setup $ns, \%setup; |
132 |
$ns->send_packet (join " ", setup => %setup); |
133 |
} |
134 |
}); |
135 |
|