--- deliantra/server/ext/commands.ext 2007/03/02 11:07:59 1.24 +++ deliantra/server/ext/commands.ext 2010/04/14 21:36:32 1.78 @@ -1,47 +1,11 @@ -#! perl +#! perl # mandatory depends=irc use POSIX (); # miscellaneous commands -sub rename_to($$$) { - my ($ob, $from, $to) = @_; - - $to =~ /^[a-zA-Z0-9.,=#\/%$!^ ]*$/ - or return $ob->message ("rename: name must consist only of letters, digits, spaces and a few other things."); - - 127 >= length $to - or return $ob->message ("rename: new name must be <= 127 characters."); - - my $item; - - if (length $from) { - $item = $ob->find_best_object_match ($from) - or return $ob->message ("rename: could not find a matching item to rename."); - } else { - $item = $ob->find_marked_object () - or return $ob->message ("rename: no from name and no marked item found to rename."); - } - - $item->custom_name (length $to ? $to : undef); - - if (length $to) { - $item->custom_name ($to); - $ob->message ("Your " . $item->base_name . " will now be called $to."); - } else { - $item->custom_name (undef); - $ob->message ("You stop calling your " . $item->base_name . " with weird names."); - } - - $ob->esrv_update_item (cf::UPD_NAME, $item); - - 1 -} - -sub ext::schmorp_irc::users; # HACK: TODO: replace by signal - -sub who_listing(;$) { - my ($privileged) = @_; +sub who_listing(;$$) { + my ($privileged, $select) = @_; my ($numwiz, $numafk) = (0, 0); my @pl; @@ -61,108 +25,221 @@ ( "Total Players in The World. (" . (scalar @pl) . ") -- WIZ($numwiz) AFK($numafk) BOT(0)", - ( - map { - my ($pl, $ob, $ns) = ($_, $_->ob, $_->ns); - - "* " . $ob->name . "/" . $ob->level . " " . (length $pl->own_title ? $pl->own_title : "the " . $pl->title) - . ($pl->peaceful ? " [peaceful]" : " [HOSTILE]") - . ($ns->afk ? " [AFK]" : "") - . ($ob->flag (cf::FLAG_WIZ) ? " [WIZ]" : "") - . " [" . $pl->ns->version . "]" - . " [" . ($pl->peaceful || $privileged ? $ob->map->visible_name : $ob->region->name) . "]" - . (sprintf " [rtt %.3fs]", $pl->ns->rtt * 1e-6) - . ($privileged ? " " . $pl->ns->host : "") - } sort { (lc $a->ob->name) cmp (lc $b->ob->name) } @pl + (grep /$select/, + map { + my ($pl, $ob, $ns) = ($_, $_->ob, $_->ns); + + "* " . $ob->name . "/" . $ob->level . " " . (length $pl->own_title ? $pl->own_title : "the " . $pl->title) + . ($pl->gender ? " [f]" : " [m]") + . ($pl->peaceful ? " [peaceful]" : " [HOSTILE]") + . ($ns->afk ? " [AFK]" : "") + . ($ob->flag (cf::FLAG_WIZ) ? " [WIZ]" : "") + . " [" . $pl->ns->{who_version} . "]" + . " [" . ($pl->peaceful || $privileged ? $ob->map->visible_name : $ob->region->name) . "]" + . (sprintf " [rtt %.3fs]", $pl->ns->tcpi_rtt * 1e-6) + . ($privileged ? " " . $pl->ns->host : "") + } sort { (lc $a->ob->name) cmp (lc $b->ob->name) } @pl ), - eval { "* IRC: " . join ", ", ext::schmorp_irc::users }, + eval { "* IRC: " . join ", ", ext::irc::users }, ) } cf::register_command who => sub { my ($ob, $arg) = @_; - $ob->speed_left ($ob->speed_left - 0.25); - - $ob->reply (undef, (join "\n", who_listing $ob->may ("extended_who")), cf::NDI_UNIQUE | cf::NDI_DK_ORANGE); + $ob->speed_left ($ob->speed_left - 4); - 1 + $ob->send_msg ("c/who" => (join "\r", who_listing $ob->may ("extended_who"), $arg), cf::NDI_DK_ORANGE | cf::NDI_REPLY | cf::NDI_CLEAR | cf::NDI_DEF); }; -cf::register_command applymode => sub { - my ($ob, $arg) = @_; - my @types = ("nochoice", "never", "always"); - my $mapping = { - nochoice => 1, - never => 2, - always => 3, +cf::register_command seen => sub { + my ($pl, $args) = @_; + + cf::async { + if (my ($login) = $args =~ /(\S+)/) { + if ($login eq $pl->name) { + $pl->message ("Very funny, $login. Ha. Ha.", cf::NDI_REPLY); + } elsif (cf::player::find_active $login) { + $pl->message ("$login is right here on this server!", cf::NDI_REPLY); + } elsif (cf::player::exists $login + and stat cf::player::path $login) { + my $time = (stat _)[9]; + + $pl->message ("$login was last seen here " + . (POSIX::strftime "%Y-%m-%d %H:%M:%S +0000", gmtime $time) + . " which was " . (int +(time - $time) / 3600) . " hours ago.", cf::NDI_REPLY); + } else { + $pl->message ("No player named $login is known to me.", cf::NDI_REPLY); + } + } else { + $pl->message ("Usage: seen ", cf::NDI_REPLY); + } }; +}; - my $oldmode = $ob->contr->unapply; - my $oldmode_name = $types[$oldmode]; +cf::register_command body => sub { + my ($ob) = @_; - return $ob->reply (undef, "applymode is set to $oldmode_name") - unless $arg; + my $observe = $ob->contr->observe; + + # Too hard to try and make a header that lines everything up, so just + # give a description. (comment from C++) + my $reply = + "The first column is the name of the body location.\r" + . "The second column is how many of those locations your body has.\r" + . "The third column is how many slots in that location are available.\r" + . "The last column shows the items currently using the slot\n\n"; + + # first process all applied items and hash them into their slots + my @slot; + + for my $item (grep $_->flag (cf::FLAG_APPLIED), $observe->inv) { + $item->slot_info ($_) + and push @{ $slot[$_] }, $item + for 0 .. cf::NUM_BODY_LOCATIONS-1; + } + + $reply .= sprintf " %-20s %3s %5s %s\n", "Location", "You", "Avail", "What"; + for (0 .. cf::NUM_BODY_LOCATIONS - 1) { + my $msg = cf::object::slot_nonuse_name $_; + $msg =~ s/^.*? a //; + $reply .= sprintf " %-20s %3d %5d %s\n", + $msg, + $observe->slot_info ($_), + $observe->slot_used ($_), + join ", ", map $_->query_short_name, @{ $slot[$_] } + if $observe->slot_info ($_) || $observe->slot_used ($_); + } + + $reply .= "You are not allowed to wear armor\r" + unless $observe->flag (cf::FLAG_USE_ARMOUR); + $reply .= "You are not allowed to use weapons\r" + unless $observe->flag (cf::FLAG_USE_WEAPON); + + $ob->send_msg ("c/body" => $reply, cf::NDI_REPLY | cf::NDI_CLEAR); +}; + +#cf::register_command mark => sub { +# my ($pl, $arg) = @_; +# +# if (length $arg) { +# my $ob = $pl->find_best_object_match ($arg); +# +# return $pl->reply (undef, "Could not find an object that matches $arg") +# unless $ob; +# +# $pl->contr->mark ($ob); +# $pl->reply (undef, (sprintf "Marked item %s", $ob->name, $ob->title)); +# } else { +# my $ob = $pl->mark; +# +# $pl->reply (undef, $ob +# ? (sprintf "%s %s * is marked.", $ob->name, $ob->title) +# : "You have no marked object."); +# } +#}; + +cf::register_command mapinfo => sub { + my ($ob) = @_; + + my $observe = $ob->contr->observe; + + my $map = $observe->map + or return; + + my $msg = ''; + + if ($map->name ne '') { + $msg .= sprintf "%s [%s] ", $map->name, $map->visible_name + } else { + $msg .= sprintf "%s ", $map->visible_name + } + + if ($map->visible_name ne $map->path) { + $msg .= sprintf "(%s) ", $map->path; + } - return $ob->reply (undef, "applymode: Unknown options '$arg', valid options are @types") - unless $mapping->{$arg}; + $msg .= sprintf "\r%s", $observe->region->longname; - $ob->contr->unapply ($mapping->{$arg} - 1); # HACK: because of the $mapping->{$arg} check before, where $arg should not be 0 - # but $arg would be 0 if a user enters an incorrect value - $ob->reply (undef, "applymode" . ($oldmode == $ob->contr->unapply ? "" : " now") . " set to " . $types[$ob->contr->unapply]); + $msg .= sprintf "\rplayers: %d difficulty: %d" + . "\rsize: %dx%d start: %dx%d position: (%d|%d) timeout: %d", + (scalar $map->players), + $map->difficulty, + $map->width, $map->height, + $map->enter_x, $map->enter_y, + $ob->x, $ob->y, + $map->timeout + if $ob->flag (cf::FLAG_WIZ); - 1 + $ob->send_msg ("c/mapinfo" => $msg, cf::NDI_REPLY | cf::NDI_CLEAR); }; -cf::register_command petmode => sub { +cf::register_command whereami => sub { + my ($ob) = @_; + + my $reg = $ob->contr->observe->region; + $ob->send_msg ("c/who" => (sprintf "You are %s.\n\n%s", $reg->longname, $reg->msg), cf::NDI_REPLY | cf::NDI_CLEAR); +}; + +cf::register_command whereabouts => sub { my ($ob, $arg) = @_; - my @types = ("normal", "sad", "defend", "arena"); - my $mapping = { - normal => 1, - sad => 2, - defend => 3, - arena => 4, - }; - my $oldtype = $ob->contr->petmode; - my $oldtype_name = $types[$oldtype]; + my %count; + + for my $pl (cf::player::list) { + ++$count{$pl->ob->region->longname}; + } + + my $msg = "T\n\n" + . join "", map { sprintf " C<%3d >player(s) %s\r", $count{$_}, $_ } sort keys %count; + + $ob->send_msg ("c/who" => $msg, cf::NDI_REPLY | cf::NDI_CLEAR); +}; + +cf::register_command hiscore => sub { + my ($ob, $arg) = @_; - return $ob->reply (undef, "petmode is set to $oldtype_name") + my $url = $cf::CFG{hiscore_url}; + $ob->send_msg (log => "See $url", cf::NDI_REPLY); +}; + +sub _set_mode($$$@) { + my ($name, $ob, $arg, $slot, @choices) = @_; + + my $oldmode = $ob->contr->$slot; + + return $ob->reply (undef, "$name is set to $choices[$oldmode]") unless $arg; - return $ob->reply (undef, "petmode: Unknown options '$arg', valid options are @types") - unless $mapping->{$arg}; + my ($idx) = grep $choices[$_] eq $arg, 0 .. $#choices + or return $ob->reply (undef, "$name: Unknown options '$arg', valid options are @choices"), 1; - $ob->contr->petmode ($mapping->{$arg} - 1); # HACK: because of the $mapping->{$arg} check before, where $arg should not be 0 - # but $arg would be 0 if a user enters an incorrect value - $ob->reply (undef, "petmode" . ($oldtype == $ob->contr->petmode ? "" : " now") . " set to " . $types[$ob->contr->petmode]); + $ob->contr->$slot ($idx); + $ob->reply (undef, "$name" . ($oldmode == $ob->contr->unapply ? "" : " now") . " set to " . $choices[$ob->contr->$slot]); +} + +cf::register_command applymode => sub { + my ($ob, $arg) = @_; - 1 + _set_mode "applymode", $ob, $arg, unapply => qw(nochoice never always); }; -cf::register_command usekeys => sub { +cf::register_command petmode => sub { my ($ob, $arg) = @_; - my @types = ("inventory", "keyrings", "containers"); - my $mapping = { - inventory => 1, - keyrings => 2, - containers => 3, - }; - my $oldtype = $ob->contr->usekeys; - my $oldtype_name = $types[$oldtype]; + _set_mode "petmode", $ob, $arg, petmode => qw(normal sad defend arena); +}; - return $ob->reply (undef, "usekeys is set to $oldtype_name") - unless $arg; +cf::register_command usekeys => sub { + my ($ob, $arg) = @_; - return $ob->reply (undef, "usekeys: Unknown options '$arg', valid options are @types") - unless $mapping->{$arg}; + _set_mode "usekeys", $ob, $arg, usekeys => qw(inventory keyrings containers); +}; - $ob->contr->usekeys ($mapping->{$arg} - 1); # HACK: because of the $mapping->{$arg} check before, where $arg should not be 0 - # but $arg would be 0 if a user enters an incorrect value - $ob->reply (undef, "usekeys" . ($oldtype == $ob->contr->usekeys ? "" : " now") . " set to " . $types[$ob->contr->usekeys]); +cf::register_command hintmode => sub { + my ($ob, $arg) = @_; - 1 + _set_mode "hintmode", $ob, $arg, hintmode => qw(show mark hide); }; cf::register_command afk => sub { @@ -170,8 +247,6 @@ $ob->contr->ns->afk ($ob->contr->ns->afk ? 0 : 1); $ob->reply (undef, $ob->contr->ns->afk ? "You are now AFK" : "You are no longer AFK"); - - 1 }; cf::register_command sound => sub { @@ -179,8 +254,6 @@ $ob->contr->ns->sound ($ob->contr->ns->sound ? 0 : 1); $ob->reply (undef, $ob->contr->ns->sound ? "The sounds are enabled." : "Silence is golden..."); - - 1 }; cf::register_command brace => sub { @@ -188,8 +261,16 @@ $ob->contr->braced ($ob->contr->braced ? 0 : 1); $ob->reply (undef, $ob->contr->braced ? "You are braced." : "Not braced."); +}; + +cf::register_command 'output-rate' => sub { + my ($ob, $arg) = @_; + + return $ob->reply (undef, sprintf "Output rate is presently %dbps.", $ob->contr->ns->max_rate / $cf::TICK) + unless $arg > 0; - 1 + $ob->contr->ns->max_rate ((cf::clamp $arg, $OUTPUT_RATE_MIN, $OUTPUT_RATE_MAX) * $TICK); + $ob->reply (undef, sprintf "Output rate now set to %dbps.", $ob->contr->ns->max_rate / $TICK); }; cf::register_command 'output-count' => sub { @@ -198,22 +279,22 @@ return $ob->reply (undef, "Output count is presently " . $ob->contr->outputs_count) unless $arg > 0; + $arg = 4 if $arg < 4; + $ob->contr->outputs_count ($arg); $ob->reply (undef, "Output count now set to " . $ob->contr->outputs_count); - - 1 }; cf::register_command 'output-sync' => sub { my ($ob, $arg) = @_; - return $ob->reply (undef, "Output sync time is presently " . $ob->contr->outputs_sync) - unless $arg > 0; + return $ob->reply (undef, sprintf "Output sync time is presently %.1fs", $ob->contr->outputs_sync * $cf::TICK) + unless length $arg; - $ob->contr->outputs_sync ($arg); - $ob->reply (undef, "Output sync time now set to " . $ob->contr->outputs_sync); + $arg = 0.5 if $arg < 0.5; - 1 + $ob->contr->outputs_sync ($arg / $cf::TICK); + $ob->reply (undef, sprintf "Output sync time now set to %.1fs", $ob->contr->outputs_sync * $cf::TICK); }; # XXX: This has a bug. After one sets his wimpy level to 0 and resets it to @@ -230,8 +311,6 @@ if $arg =~ /^\d+$/ and $arg <= 100; $ob->reply (undef, "Incorrect parameters for wimpy: $arg"); - - 1 }; cf::register_command peaceful => sub { @@ -244,10 +323,42 @@ #$ob->contr->peaceful ($ob->contr->peaceful ? 0 : 1); #$ob->reply (undef, $ob->contr->peaceful ? "You will attack other players." : "You will not attack other players."); - - 1 }; +sub rename_to($$$) { + my ($ob, $from, $to) = @_; + + $to =~ /^[a-zA-Z0-9.,=#\/%$!^ ]*$/ + or return $ob->message ("rename: name must consist only of letters, digits, spaces and a few other things."); + + 127 >= length $to + or return $ob->message ("rename: new name must be <= 127 characters."); + + my $item; + + if (length $from) { + $item = $ob->find_best_object_match ($from) + or return $ob->message ("rename: could not find a matching item to rename."); + } else { + $item = $ob->mark + or return $ob->message ("rename: no from name and no marked item found to rename."); + } + + $item->custom_name (length $to ? $to : undef); + + if (length $to) { + $item->custom_name ($to); + $ob->message ("Your " . $item->base_name . " will now be called $to."); + } else { + $item->custom_name (undef); + $ob->message ("You stop calling your " . $item->base_name . " with weird names."); + } + + $ob->esrv_update_item (cf::UPD_NAME, $item); + + 1 +} + cf::register_command rename => sub { my ($ob, $arg) = @_; @@ -270,8 +381,6 @@ } else { $ob->reply (undef, 'Syntax error. Rename usage: rename ["oldname"] to "newname"'); } - - 1 }; cf::register_command uptime => sub { @@ -279,13 +388,11 @@ my $startup = POSIX::strftime "%Y-%m-%d %H:%M:%S %Z", localtime $cf::UPTIME; my $runtime = sprintf "%.1f", (time - $cf::UPTIME) / 86400; - $ob->reply (undef, "server started $startup, uptime ${runtime}\d"); - - 1 + $ob->send_msg ("c/uptime" => "server started $startup, uptime ${runtime}\d", cf::NDI_REPLY | cf::NDI_CLEAR); }; my %IN_MEMORY = ( - cf::MAP_IN_MEMORY => "I", + cf::MAP_ACTIVE => "I", cf::MAP_SWAPPED => "S", cf::MAP_LOADING => "L", ); @@ -295,9 +402,9 @@ no re 'eval'; $arg = qr<$arg>; - my $format = "%2s %1s %3s %5s %.60s\n"; + my $format = " %2s %1s %3s %5s %.60s\n"; - $ob->reply (undef, (sprintf $format, "Pl", "I", "Svd", "Reset", "Name"), cf::NDI_BLACK | cf::NDI_UNIQUE); + my $msg = "\n" . sprintf $format, "Pl", "I", "Svd", "Reset", "Name"; for (sort keys %cf::MAP) { my $map = $cf::MAP{$_} @@ -309,16 +416,14 @@ my $svd = int $cf::RUNTIME - $map->{last_save}; $svd = "++" if $svd > 99; - $ob->reply (undef, - (sprintf $format, - (scalar $map->players), - $IN_MEMORY{$map->in_memory} || "?", - $svd, - (int $map->reset_at - $cf::RUNTIME), - $map->visible_name), - cf::NDI_BLACK | cf::NDI_UNIQUE); + $msg .= sprintf $format, + (scalar $map->players), + $IN_MEMORY{$map->in_memory} || "?", + $svd, + (int $map->reset_at - $cf::RUNTIME), + $map->visible_name; } - 1 + $ob->send_msg ("c/mapinfo" => $msg, cf::NDI_REPLY | cf::NDI_CLEAR); };