tmud-2.9.0/benchmark/
tmud-2.9.0/cmd/
tmud-2.9.0/cmd/objects/
tmud-2.9.0/cmd/tiny/
tmud-2.9.0/db/
tmud-2.9.0/doc/classes/Acceptor.src/
tmud-2.9.0/doc/classes/BoolExpParser.src/
tmud-2.9.0/doc/classes/CacheStats.src/
tmud-2.9.0/doc/classes/Character.src/
tmud-2.9.0/doc/classes/Client.src/
tmud-2.9.0/doc/classes/ColorFilter.src/
tmud-2.9.0/doc/classes/Command.src/
tmud-2.9.0/doc/classes/Configuration.src/
tmud-2.9.0/doc/classes/Connector.src/
tmud-2.9.0/doc/classes/ConsoleClient.src/
tmud-2.9.0/doc/classes/CursesClient.src/
tmud-2.9.0/doc/classes/DebugFilter.src/
tmud-2.9.0/doc/classes/Dumper.src/
tmud-2.9.0/doc/classes/Engine.src/
tmud-2.9.0/doc/classes/Event.src/
tmud-2.9.0/doc/classes/EventManager.src/
tmud-2.9.0/doc/classes/Farts/AttributeSyntaxNode.src/
tmud-2.9.0/doc/classes/Farts/CallSyntaxNode.src/
tmud-2.9.0/doc/classes/Farts/CommandSyntaxNode.src/
tmud-2.9.0/doc/classes/Farts/CommentSyntaxNode.src/
tmud-2.9.0/doc/classes/Farts/EndSyntaxNode.src/
tmud-2.9.0/doc/classes/Farts/IfSyntaxNode.src/
tmud-2.9.0/doc/classes/Farts/Interpreter.src/
tmud-2.9.0/doc/classes/Farts/Lexer.src/
tmud-2.9.0/doc/classes/Farts/Lib.src/
tmud-2.9.0/doc/classes/Farts/LiteralSyntaxNode.src/
tmud-2.9.0/doc/classes/Farts/LocalVarSyntaxNode.src/
tmud-2.9.0/doc/classes/Farts/Parser.src/
tmud-2.9.0/doc/classes/Farts/ProgramSyntaxNode.src/
tmud-2.9.0/doc/classes/Farts/SyntaxNode.src/
tmud-2.9.0/doc/classes/Filter.src/
tmud-2.9.0/doc/classes/LineIO.src/
tmud-2.9.0/doc/classes/Loader.src/
tmud-2.9.0/doc/classes/Log.src/
tmud-2.9.0/doc/classes/Module.src/
tmud-2.9.0/doc/classes/ObjCmd.src/
tmud-2.9.0/doc/classes/PacketIO.src/
tmud-2.9.0/doc/classes/ProtocolStack.src/
tmud-2.9.0/doc/classes/Publisher.src/
tmud-2.9.0/doc/classes/Reactor.src/
tmud-2.9.0/doc/classes/Room.src/
tmud-2.9.0/doc/classes/SQLite/
tmud-2.9.0/doc/classes/SQLite/Database.src/
tmud-2.9.0/doc/classes/SQLite3/
tmud-2.9.0/doc/classes/SQLite3/Database.src/
tmud-2.9.0/doc/classes/Script.src/
tmud-2.9.0/doc/classes/SockIO.src/
tmud-2.9.0/doc/classes/String.src/
tmud-2.9.0/doc/classes/TerminalFilter.src/
tmud-2.9.0/doc/classes/TernaryTrie.src/
tmud-2.9.0/doc/classes/TernaryTrie/
tmud-2.9.0/doc/classes/TernaryTrie/TNode.src/
tmud-2.9.0/doc/classes/Timer.src/
tmud-2.9.0/doc/classes/Utility.src/
tmud-2.9.0/doc/classes/XmlStore.src/
tmud-2.9.0/doc/classes/YamlStore.src/
tmud-2.9.0/doc/dot/
tmud-2.9.0/doc/files/cmd/objects/
tmud-2.9.0/doc/files/cmd/tiny/
tmud-2.9.0/doc/files/lib/
tmud-2.9.0/doc/files/lib/engine/
tmud-2.9.0/doc/files/lib/farts/
tmud-2.9.0/doc/files/tclient_rb.src/
tmud-2.9.0/doc/files/tmud_rb.src/
tmud-2.9.0/farts/
tmud-2.9.0/lib/
tmud-2.9.0/lib/core/
tmud-2.9.0/lib/engine/
tmud-2.9.0/lib/farts/
tmud-2.9.0/logs/
#
# file::    character.rb
# author::  Jon A. Lambert
# version:: 2.9.0
# date::    03/15/2006
#
# This source code copyright (C) 2005, 2006 by Jon A. Lambert
# All rights reserved.
#
# Released under the terms of the TeensyMUD Public License
# See LICENSE file for additional information.
#
$:.unshift "lib" if !$:.include? "lib"

require 'core/gameobject'

# The Character class is the mother of all characters.
# Who's their daddy?
#
class Character < GameObject
  logger 'INFO'

  # The acctid object this character is associated with.
  property :acctid
  attr_accessor :account # The reference to the account object
                         # (nil if not logged in)

  # Create a new Character object
  # IMPORTANT :Character objects must be marked nonswappable while connected!!!
  #       Otherwise we risk losing the contants of @account
  # [+name+]    The displayed name of the character.
  # [+acctid+]  The account id this character belongs to.
  # [+return+]  A handle to the new Character.
  def initialize(name,acctid)
    super(name, nil, options['home'] || 1)
    self.acctid = acctid
    @account = nil              # reference to the Account.  If nil this
                                # character is not logged in.
                                # We could use get_object(acctid) but
                                # holding the reference is faster
  end

  # Sends a message to the character if they are connected.
  # [+s+]      The message string
  # [+return+] Undefined.
  def sendto(s)
    @account.sendmsg(s+"\n") if @account
  end

  # All command input routed through here and parsed.
  # [+m+]      The input message to be parsed
  # [+return+] Undefined.
  def parse(m)
    @account.prompt
    # handle edit mode
    if @mode == :edit
      edit_parser m
      return
    end

    # match legal command
    m=~/([A-Za-z0-9_@?"'#!\]\[]+)(.*)/
    cmd=$1
    arg=$2
    arg.strip! if arg
    if !cmd
      sendto("Huh?")
      return
    end

    # look for a command in our spanking new table
    c = world.cmds.find(cmd)


    # add any exits to our command list
    # escape certain characters in cmd
    check = cmd.gsub(/\?/,"\\?")
    check.gsub!(/\#/,"\\#")
    check.gsub!(/\[/,"\\[")
    check.gsub!(/\]/,"\\]")
    get_object(location).exits.keys.grep(/^#{check}/).each do |ex|
      c << Command.new(:cmd_go,"go #{ex}",nil)
      arg = ex
    end
    log.debug "parse commands - '#{c.inspect}', arguments - '#{arg}'"

    # there are three possibilities here
    case c.size
    when 0   # no commands found
      sendto("Huh?")
    when 1   # command found
      self.send(c[0].cmd, arg)
    else     # ambiguous command - tell luser about them.
      ln = "Which did you mean, "
      c.each do |x|
        ln += "\'" + x.name + "\'"
        x.name == c.last.name ? ln += "?" : ln += " or "
      end
      sendto(ln)
    end
  rescue Exception
    # keep character alive after exceptions
    log.fatal $!
  end

  # Event :describe
  # [+e+]      The event
  # [+return+] Undefined
  def describe(e)
    msg = "[COLOR Cyan]#{name} is here.[/COLOR]"
    add_event(id,e.from,:show,msg)
  end

  # Event :show
  # [+e+]      The event
  # [+return+] Undefined
  def show(e)
    sendto(e.msg)
  end

end