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