1 | #! perl # MANDATORY |
1 | #! perl # mandatory |
2 | |
2 | |
3 | # login handling |
3 | # login handling |
4 | |
4 | |
5 | use Fcntl; |
5 | use Fcntl; |
6 | use Coro::AIO; |
6 | use Coro::AIO; |
7 | use List::Util qw(min max); |
|
|
8 | |
7 | |
9 | # paranoia function to overwrite a string-in-place |
8 | # paranoia function to overwrite a string-in-place |
10 | sub nuke_str { |
9 | sub nuke_str { |
11 | substr $_[0], 0, (length $_[0]), "x" x length $_[0] |
10 | substr $_[0], 0, (length $_[0]), "x" x length $_[0] |
12 | } |
11 | } |
… | |
… | |
39 | return unless cf::player::find_active $user; |
38 | return unless cf::player::find_active $user; |
40 | |
39 | |
41 | $ns->send_drawinfo ( |
40 | $ns->send_drawinfo ( |
42 | "That player is already logged in on this server. " |
41 | "That player is already logged in on this server. " |
43 | . "If you want to create a new player, choose another name. " |
42 | . "If you want to create a new player, choose another name. " |
44 | . "If you are already a registered player, make sure nobody " |
43 | . "If you have already a registered, make sure nobody " |
45 | . "else is using your account at this time. If you lost your conenction " |
44 | . "else is using your account at this time. If you lost your connection " |
46 | . "then the server will likely timeout within a minute. If you still " |
45 | . "then the server will likely timeout within a minute. If you still " |
47 | . "cannot log-in after a minute, you are still logged in. Make sure " |
46 | . "cannot log-in after a minute, you are still logged in. Make sure " |
48 | . "you do not have another client running. If you use windows, reboot, " |
47 | . "you do not have another client running. If you use windows, reboot, " |
49 | . "this will fix anything.", |
48 | . "this will fix anything.", |
50 | cf::NDI_RED |
49 | cf::NDI_RED |
… | |
… | |
54 | } |
53 | } |
55 | |
54 | |
56 | sub enter_map { |
55 | sub enter_map { |
57 | my ($pl) = @_; |
56 | my ($pl) = @_; |
58 | |
57 | |
|
|
58 | my $ob = $pl->ob; |
|
|
59 | |
59 | my ($map, $x, $y) |
60 | my ($map, $x, $y) |
60 | = $pl->ob->{_link_pos} |
61 | = $ob->{_link_pos} |
61 | ? @{delete $pl->ob->{_link_pos}} |
62 | ? @{delete $ob->{_link_pos}} |
62 | : ($pl->maplevel, $pl->ob->x, $pl->ob->y); |
63 | : ($pl->maplevel, $ob->x, $ob->y); |
63 | |
64 | |
64 | $pl->ob->enter_link; |
65 | $ob->enter_link; |
65 | |
66 | |
66 | if (my $time = delete $pl->{unclean_save}) { |
67 | if (my $time = delete $pl->{unclean_save}) { |
67 | if (my $m = cf::map::find $map) { |
68 | if (my $m = cf::map::find $map) { |
68 | if ($time < $m->{instantiate_time}) { |
69 | if ($time < $m->{instantiate_time} || 1) {#d# |
69 | # the map was reset in the meantime |
70 | # the map was reset in the meantime |
70 | my $age = $cf::RUNTIME - $time; |
71 | my $age = $cf::RUNTIME - $time; |
71 | warn $pl->ob->name, " map reset after logout, logout age $age\n";#d# |
72 | warn $ob->name, " map reset after logout, logout age $age\n";#d# |
72 | |
73 | |
73 | # for now, just go back to savebed |
74 | # for now, just go back to savebed |
74 | ($map, $x, $y) = $pl->savebed; |
75 | ($map, $x, $y) = $pl->savebed; |
75 | |
76 | |
76 | $pl->ns->send_drawinfo ( |
77 | $pl->ns->send_drawinfo ( |
… | |
… | |
88 | ); |
89 | ); |
89 | } |
90 | } |
90 | } |
91 | } |
91 | } |
92 | } |
92 | |
93 | |
93 | $pl->ob->goto ($map, $x, $y); |
94 | $ob->goto ($map, $x, $y); |
94 | } |
95 | } |
95 | |
96 | |
96 | # delete a player directory, be non-blocking AND synchronous... |
97 | # delete a player directory, be non-blocking AND synchronous... |
97 | # (thats hard, so we crap out and fork). |
98 | # (thats hard, so we crap out and fork). |
98 | sub nuke_playerdir { |
99 | sub nuke_playerdir { |
… | |
… | |
102 | system "cd \Q$PLAYERDIR\E " |
103 | system "cd \Q$PLAYERDIR\E " |
103 | . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " |
104 | . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " |
104 | . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; |
105 | . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; |
105 | } |
106 | } |
106 | |
107 | |
107 | sub send_capabilities { |
108 | cf::client->attach (on_addme => sub { |
108 | my ($ns) = @_; |
109 | my ($ns) = @_; |
109 | |
110 | |
110 | return unless $ns->extcmd; |
|
|
111 | |
|
|
112 | $ns->ext_event (capabilities => |
|
|
113 | # id, name, flags (1 == 2d), edge length |
|
|
114 | tileset => [[1, "default 64x64 faceset", 1, 64], [0, "default 32x32 faceset", 1, 32]], |
|
|
115 | ); |
|
|
116 | } |
|
|
117 | |
|
|
118 | sub setup { |
|
|
119 | my ($ns, $args) = @_; |
|
|
120 | |
|
|
121 | # run through the cmds of setup |
|
|
122 | # syntax is setup <cmdname1> <parameter> <cmdname2> <parameter> ... |
|
|
123 | # |
|
|
124 | # we send the status of the cmd back, or a FALSE is the cmd is the server unknown |
|
|
125 | # The client then must sort this out |
|
|
126 | |
|
|
127 | my %setup = split / +/, $args; |
|
|
128 | while (my ($k, $v) = each %setup) { |
|
|
129 | if ($k eq "sound") { |
|
|
130 | $ns->sound ($v); |
|
|
131 | |
|
|
132 | } elsif ($k eq "exp64") { |
|
|
133 | $setup{$k} = 1; |
|
|
134 | |
|
|
135 | } elsif ($k eq "spellmon") { |
|
|
136 | $ns->monitor_spells ($v); |
|
|
137 | |
|
|
138 | } elsif ($k eq "darkness") { |
|
|
139 | $ns->darkness ($v); |
|
|
140 | |
|
|
141 | } elsif ($k eq "map1cmd") { |
|
|
142 | $ns->mapmode (cf::Map1Cmd) if $v > 0; |
|
|
143 | |
|
|
144 | } elsif ($k eq "map1acmd") { |
|
|
145 | $ns->mapmode (cf::Map1aCmd) if $v > 0; |
|
|
146 | |
|
|
147 | } elsif ($k eq "map2cmd") { |
|
|
148 | # gcfclient bug, map1acmd is sent too late |
|
|
149 | $ns->mapmode (cf::Map1aCmd); |
|
|
150 | $setup{$k} = "FALSE"; |
|
|
151 | |
|
|
152 | } elsif ($k eq "newmapcmd") { |
|
|
153 | $ns->newmapcmd ($v); |
|
|
154 | |
|
|
155 | } elsif ($k eq "mapinfocmd") { |
|
|
156 | $ns->mapinfocmd ($v); |
|
|
157 | |
|
|
158 | } elsif ($k eq "extcmd") { |
|
|
159 | $ns->extcmd ($v > 0); |
|
|
160 | send_capabilities $ns; |
|
|
161 | |
|
|
162 | } elsif ($k eq "extmap") { |
|
|
163 | $ns->extmap ($v); |
|
|
164 | |
|
|
165 | } elsif ($k eq "facecache") { |
|
|
166 | if (!$v) { |
|
|
167 | $v = 1; |
|
|
168 | $setup{$k} = $v; |
|
|
169 | $ns->send_drawinfo ("(trying to forcefully enable facecaching)", cf::NDI_RED); |
|
|
170 | } |
|
|
171 | |
|
|
172 | $ns->facecache ($v); |
|
|
173 | |
|
|
174 | } elsif ($k eq "faceset") { |
|
|
175 | $ns->faceset (0); |
|
|
176 | $setup{$k} = 0; |
|
|
177 | # $ns->image2 (1) |
|
|
178 | |
|
|
179 | } elsif ($k eq "tileset") { |
|
|
180 | $setup{$k} = $ns->faceset ($v & 1); |
|
|
181 | |
|
|
182 | } elsif ($k eq "itemcmd") { |
|
|
183 | # Version of the item protocol command to use. Currently, |
|
|
184 | # only supported versions are 1 and 2. Using a numeric |
|
|
185 | # value will make it very easy to extend this in the future. |
|
|
186 | $ns->itemcmd ($v) if $v >= 1 && $v <= 2; |
|
|
187 | |
|
|
188 | $setup{$k} = $ns->itemcmd; |
|
|
189 | |
|
|
190 | } elsif ($k eq "mapsize") { |
|
|
191 | my ($x, $y) = split /x/, $v; |
|
|
192 | |
|
|
193 | $ns->mapx ($x = max 9, min cf::MAP_CLIENT_X, ($x - 1) | 1); |
|
|
194 | $ns->mapy ($y = max 9, min cf::MAP_CLIENT_Y, ($y - 1) | 1); |
|
|
195 | |
|
|
196 | $setup{$k} = "${x}x${y}"; |
|
|
197 | |
|
|
198 | } elsif ($k eq "extendedMapInfos") { |
|
|
199 | $ns->ext_mapinfos ($v); |
|
|
200 | |
|
|
201 | } elsif ($k eq "extendedTextInfos") { |
|
|
202 | $ns->has_readable_type ($v); |
|
|
203 | |
|
|
204 | } elsif ($k eq "smoothing") { # cfplus-style smoothing |
|
|
205 | $ns->smoothing ($v); |
|
|
206 | |
|
|
207 | } elsif ($k eq "fxix") { |
|
|
208 | $ns->fxix ($v); |
|
|
209 | |
|
|
210 | } elsif ($k eq "msg") { |
|
|
211 | $ns->can_msg ($v); |
|
|
212 | |
|
|
213 | } elsif ($k eq "excmd") { |
|
|
214 | # we support it |
|
|
215 | |
|
|
216 | } else { |
|
|
217 | # other commands: |
|
|
218 | # sexp: no idea, probably for oudated servers |
|
|
219 | # tick: more stupidity, server should send a tick per tick |
|
|
220 | |
|
|
221 | $setup{$k} = "FALSE"; |
|
|
222 | } |
|
|
223 | } |
|
|
224 | |
|
|
225 | $ns->send_packet (join " ", setup => %setup); |
|
|
226 | |
|
|
227 | cf::datalog setup => |
|
|
228 | request => $args, |
|
|
229 | reply => \%setup, |
|
|
230 | client => $ns->version, |
|
|
231 | ; |
|
|
232 | } |
|
|
233 | |
|
|
234 | sub addme { |
|
|
235 | my ($ns) = @_; |
|
|
236 | |
|
|
237 | if (!$ns->facecache) |
111 | if (!$ns->facecache) |
238 | { |
112 | { |
239 | $ns->send_drawinfo (<<EOF, cf::NDI_RED); |
113 | $ns->send_drawinfo (<<EOF, cf::NDI_RED); |
240 | |
114 | |
241 | |
115 | |
242 | *** |
116 | *** |
243 | *** WARNING: |
117 | *** WARNING: |
244 | *** Your client does not support face/image caching, |
118 | *** Your client does not support face/image caching, |
… | |
… | |
312 | . "(only a-z, A-Z and 0-9 are allowed), " |
186 | . "(only a-z, A-Z and 0-9 are allowed), " |
313 | . "or is not between 3 and 18 characters in length.", |
187 | . "or is not between 3 and 18 characters in length.", |
314 | cf::NDI_RED |
188 | cf::NDI_RED |
315 | ); |
189 | ); |
316 | } |
190 | } |
|
|
191 | Coro::Timer::sleep 0.4; |
317 | } |
192 | } |
318 | |
193 | |
319 | check_playing $ns, $user and next; |
194 | check_playing $ns, $user and next; |
320 | |
195 | |
321 | $ns->send_drawinfo ( |
196 | $ns->send_drawinfo ( |
… | |
… | |
332 | $ns->send_drawinfo ( |
207 | $ns->send_drawinfo ( |
333 | "Try to use at least three characters as your password please, " |
208 | "Try to use at least three characters as your password please, " |
334 | . "that cannot be too much to ask for :)", |
209 | . "that cannot be too much to ask for :)", |
335 | cf::NDI_RED |
210 | cf::NDI_RED |
336 | ); |
211 | ); |
|
|
212 | Coro::Timer::sleep 0.4; |
337 | } |
213 | } |
338 | |
214 | |
339 | # lock this username for the remainder of this login session |
215 | # lock this username for the remainder of this login session |
340 | if ($cf::LOGIN_LOCK{$user}) { |
216 | if ($cf::LOGIN_LOCK{$user}) { |
341 | $ns->send_drawinfo ( |
217 | $ns->send_drawinfo ( |
… | |
… | |
422 | nuke_str $pass2; |
298 | nuke_str $pass2; |
423 | $ns->send_drawinfo ( |
299 | $ns->send_drawinfo ( |
424 | "The passwords do not match, please try again.", |
300 | "The passwords do not match, please try again.", |
425 | cf::NDI_RED |
301 | cf::NDI_RED |
426 | ); |
302 | ); |
|
|
303 | Coro::Timer::sleep 0.5; |
427 | next; |
304 | next; |
428 | } |
305 | } |
429 | |
306 | |
430 | nuke_str $pass2; |
307 | nuke_str $pass2; |
431 | |
308 | |
… | |
… | |
453 | $ob->swap_stats ($res - 1, $swap - 1); |
330 | $ob->swap_stats ($res - 1, $swap - 1); |
454 | } |
331 | } |
455 | } else { |
332 | } else { |
456 | $ob->roll_stats; |
333 | $ob->roll_stats; |
457 | } |
334 | } |
|
|
335 | |
|
|
336 | Coro::Timer::sleep 0.2; |
458 | } |
337 | } |
459 | |
338 | |
460 | $ob->set_animation (2); |
339 | $ob->set_animation (2); |
461 | $ob->add_statbonus; |
340 | $ob->add_statbonus; |
462 | |
341 | |
… | |
… | |
470 | "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"; |
349 | "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"; |
471 | |
350 | |
472 | last if $res =~ /[dD]/; |
351 | last if $res =~ /[dD]/; |
473 | |
352 | |
474 | $pl->chargen_race_next; |
353 | $pl->chargen_race_next; |
|
|
354 | Coro::Timer::sleep 0.2; |
475 | } |
355 | } |
476 | |
356 | |
477 | $pl->chargen_race_done; |
357 | $pl->chargen_race_done; |
|
|
358 | |
|
|
359 | while () { |
|
|
360 | my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
|
|
361 | "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n"; |
|
|
362 | |
|
|
363 | if ($res =~ /^[fF]/) { |
|
|
364 | $pl->gender (1); |
|
|
365 | last; |
|
|
366 | } elsif ($res =~ /^[mM]/) { |
|
|
367 | $pl->gender (0); |
|
|
368 | last; |
|
|
369 | } |
|
|
370 | Coro::Timer::sleep 0.2; |
|
|
371 | } |
|
|
372 | |
|
|
373 | $ob->reply (undef, "Welcome to Crossfire!"); |
478 | |
374 | |
479 | delete $pl->{deny_save}; |
375 | delete $pl->{deny_save}; |
480 | |
376 | |
481 | last; |
377 | last; |
482 | } |
378 | } |
483 | }); |
379 | }); |
484 | } |
380 | }); |
485 | |
381 | |
486 | cf::register_command quit => sub { |
382 | cf::register_command quit => sub { |
487 | my ($ob, $arg) = @_; |
383 | my ($ob, $arg) = @_; |
488 | |
384 | |
489 | $ob->reply (undef, |
385 | $ob->reply (undef, |
… | |
… | |
560 | $pl->{unclean_save} = $cf::RUNTIME; |
456 | $pl->{unclean_save} = $cf::RUNTIME; |
561 | } |
457 | } |
562 | }, |
458 | }, |
563 | ); |
459 | ); |
564 | |
460 | |
565 | cf::client->attach ( |
|
|
566 | on_addme => \&addme, |
|
|
567 | on_setup => \&setup, |
|
|
568 | ); |
|
|
569 | |
|
|
570 | ############################################################################# |
461 | ############################################################################# |
571 | |
462 | |
572 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
463 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
573 | our $SAVE_TIMEOUT = 20; # save players every n seconds |
464 | our $SAVE_TIMEOUT = 20; # save players every n seconds |
574 | |
465 | |
… | |
… | |
593 | $pl->save; |
484 | $pl->save; |
594 | |
485 | |
595 | unless ($pl->active || $pl->ns) { |
486 | unless ($pl->active || $pl->ns) { |
596 | # check refcounts, this is tricky and needs to be adjusted to fit server internals |
487 | # check refcounts, this is tricky and needs to be adjusted to fit server internals |
597 | my $ob = $pl->ob; |
488 | my $ob = $pl->ob; |
598 | Scalar::Util::weaken $pl; |
489 | |
599 | Scalar::Util::weaken $ob; |
|
|
600 | my $a_ = $pl->refcnt;#d# |
|
|
601 | my $b_ = $ob->refcnt;#d# |
|
|
602 | my $pl_ref = $pl->refcnt_cnt; |
490 | my $pl_ref = $pl->refcnt_cnt; |
603 | my $ob_ref = $ob->refcnt_cnt; |
491 | my $ob_ref = $ob->refcnt_cnt; |
604 | |
492 | |
605 | ## pl_ref == one from object + one from cf::PLAYER |
493 | ## pl_ref == $pl + ob->contr + %cf::PLAYER |
606 | ## ob_ref == one from simply being an object + one from pl->observe |
494 | ## ob_ref == $ob + pl->observe + simply being an object |
607 | if ($pl_ref == 2 && $ob_ref == 2) { |
495 | if ($pl_ref == 3 && $ob_ref == 3) { |
608 | warn "player-scheduler destroy ", $ob->name;#d# |
496 | warn "player-scheduler destroy ", $ob->name;#d# |
609 | |
497 | |
610 | # remove from sight and get fresh "copies" |
498 | # remove from sight and get fresh "copies" |
611 | $pl = delete $cf::PLAYER{$ob->name}; |
499 | $pl = delete $cf::PLAYER{$ob->name}; |
612 | $ob = $pl->ob; |
500 | $ob = $pl->ob; |
613 | |
501 | |
614 | $ob->destroy; |
|
|
615 | $pl->destroy; |
502 | $pl->destroy; # destroys $ob |
616 | } else { |
503 | } else { |
|
|
504 | my $a_ = $pl->refcnt;#d# |
|
|
505 | my $b_ = $ob->refcnt;#d# |
|
|
506 | |
617 | warn "player-scheduler refcnt ", $ob->name, " pp$pl_ref,pc$a_ op$ob_ref,oc$b_\n";#d# |
507 | warn "player-scheduler refcnt ", $ob->name, " pl $pl_ref ob $ob_ref (C pl $a_ ob $b_)\n";#d# |
618 | } |
508 | } |
619 | } |
509 | } |
620 | } |
510 | } |
621 | }; |
511 | }; |
622 | warn $@ if $@; |
512 | warn $@ if $@; |