⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game.cpp

📁 MUD游戏编程光盘代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:

    // ------------------------------------------------------------------------
    //  Command not recognized, send to room
    // ------------------------------------------------------------------------
    SendRoom( bold + p.Name() + " says: " + dim + p_data, p.CurrentRoom() );
}


// ------------------------------------------------------------------------
//  This notifies the handler that there is a new connection
// ------------------------------------------------------------------------
void Game::Enter()
{
    USERLOG.Log(  
        GetIPString( m_connection->GetRemoteAddress() ) + 
        " - User " + m_player->Name() + 
        " entering Game state." );

    m_lastcommand = "";

    Player& p = *m_player;
    p.CurrentRoom()->AddPlayer( p.ID() );
    p.Active() = true;
    p.LoggedIn() = true;

    SendGame( bold + green + p.Name() + " has entered the realm." );

    if( p.Newbie() )
        GotoTrain();
    else
        p.SendString( PrintRoom( p.CurrentRoom() ) );
}

void Game::Leave()
{
    USERLOG.Log(  
        GetIPString( m_connection->GetRemoteAddress() ) + 
        " - User " + m_player->Name() + 
        " leaving Game state." );

    // remove the player from his room
    m_player->CurrentRoom()->RemovePlayer( m_player );
    m_player->Active() = false;

    // log out the player from the database if the connection has been closed
    if( m_connection->Closed() )
    {
        PlayerDatabase::Logout( m_player );
    }
}

// ------------------------------------------------------------------------
//  This notifies the handler that a connection has unexpectedly hung up.
// ------------------------------------------------------------------------
void Game::Hungup()
{
    Player& p = *m_player;
    LogoutMessage( p.Name() + " has suddenly disappeared from the realm." );
}

// ------------------------------------------------------------------------
//  This notifies the handler that a connection is being kicked due to 
//  flooding the server.
// ------------------------------------------------------------------------
void Game::Flooded()
{
    Player& p = *m_player;
    LogoutMessage( p.Name() + " has been kicked for flooding!" );
}

// ------------------------------------------------------------------------
//  Sends a string to everyone connected.
// ------------------------------------------------------------------------
void Game::SendGlobal( const string& p_str )
{
    operate_on_if( PlayerDatabase::begin(),
                   PlayerDatabase::end(),
                   playersend( p_str ),
                   playerloggedin() );
}

// ------------------------------------------------------------------------
//  Sends a string to everyone "within the game"
// ------------------------------------------------------------------------
void Game::SendGame( const std::string& p_str )
{
    operate_on_if( PlayerDatabase::begin(),
                   PlayerDatabase::end(),
                   playersend( p_str ),
                   playeractive() );
}

// ------------------------------------------------------------------------
//  Sends a logout message
// ------------------------------------------------------------------------
void Game::LogoutMessage( const string& p_reason )
{
    SendGame( SocketLib::red + SocketLib::bold + p_reason );
}

// ------------------------------------------------------------------------
//  Sends a system announcement
// ------------------------------------------------------------------------
void Game::Announce( const string& p_announcement )
{
    SendGlobal( SocketLib::cyan + SocketLib::bold + 
                "System Announcement: " + p_announcement );
}

// ------------------------------------------------------------------------
//  Sends a whisper string to the requested player
// ------------------------------------------------------------------------
void Game::Whisper( std::string p_str, std::string p_player )
{
    // find the player
    PlayerDatabase::iterator itr = PlayerDatabase::findactive( p_player );
    
    // if no match was found
    if( itr == PlayerDatabase::end() )
    {
        m_player->SendString( red + bold + "Error, cannot find user." );
    }
    else
    {
        itr->SendString( yellow + m_player->Name() + " whispers to you: " + 
                         reset + p_str );

        m_player->SendString( yellow + "You whisper to " + itr->Name() + 
                              ": " + reset + p_str );
    }
}

// ------------------------------------------------------------------------
//  Functor that generates a who-listing for a single player
// ------------------------------------------------------------------------
struct wholist
{
    string str;

    void operator() ( Player& p )
    {
        str += " " + tostring( p.Name(), 17 ) + "| ";
        str += tostring( p.Level(), 10 ) + "| ";
        
        
        if( p.Active() )
            str += green + "Online  " + white;
        else if( p.LoggedIn() )
            str += yellow + "Inactive" + white;
        else
            str += red + "Offline " + white;

        str += " | ";
        switch( p.Rank() )
        {
            case REGULAR:   str += white;   break;
            case GOD:       str += yellow;  break;
            case ADMIN:     str += green;   break;
        }

        str += GetRankString( p.Rank() );

        str += white + "\r\n";
    }
};

// ------------------------------------------------------------------------
//  This prints up the who-list for the realm.
// ------------------------------------------------------------------------
string Game::WhoList( const string& p_who )
{
    using namespace BasicLib;

    string str = white + bold +
        "--------------------------------------------------------------------------------\r\n" + 
        " Name             | Level     | Activity | Rank\r\n" + 
        "--------------------------------------------------------------------------------\r\n";

    wholist who;
    if( p_who == "all" )
    {
        who = BasicLib::operate_on_if( 
                        PlayerDatabase::begin(),
                        PlayerDatabase::end(),
                        wholist(),
                        always<Player>() );
    }
    else
    {
        who = BasicLib::operate_on_if( 
                        PlayerDatabase::begin(),
                        PlayerDatabase::end(),
                        wholist(),
                        playerloggedin() );
    }

    str += who.str;

    
    str +=
        "--------------------------------------------------------------------------------";

    return str;
}

// ------------------------------------------------------------------------
//  Prints out a help listing based on a user's rank.
// ------------------------------------------------------------------------
string Game::PrintHelp( PlayerRank p_rank )
{
    static string help = white + bold + 
        "--------------------------------- Command List ---------------------------------\r\n" + 
        " /                          - Repeats your last command exactly.\r\n" +
        " chat <mesg>                - Sends message to everyone in the game\r\n" +
        " experience                 - Shows your experience statistics\r\n" +
        " help                       - Shows this menu\r\n" +
        " inventory                  - Shows a list of your items\r\n" + 
        " quit                       - Allows you to leave the realm.\r\n" + 
        " remove <'weapon'/'armor'>  - removes your weapon or armor\r\n" + 
        " stats                      - Shows all of your statistics\r\n" + 
        " time                       - shows the current system time.\r\n" +
        " use <item>                 - use an item in your inventory\r\n" +
        " whisper <who> <msg>        - Sends message to one person\r\n" +
        " who                        - Shows a list of everyone online\r\n" +
        " who all                    - Shows a list of everyone\r\n" +
        " look                       - Shows you the contents of a room\r\n" +
        " north/east/south/west      - Moves in a direction\r\n" +
        " get/drop <item>            - Picks up or drops an item on the ground\r\n" +
        " train                      - Train to the next level (TR)\r\n" +
        " editstats                  - Edit your statistics (TR)\r\n" +
        " list                       - Lists items in a store (ST)\r\n" +
        " buy/sell <item>            - Buy or Sell an item in a store (ST)\r\n" +
        " attack <enemy>             - Attack an enemy\r\n";


    static string god = yellow + bold +
        "--------------------------------- God Commands ---------------------------------\r\n" + 
        " kick <who>                 - kicks a user from the realm\r\n";

    static string admin = green + bold +
        "-------------------------------- Admin Commands --------------------------------\r\n" + 
        " announce <msg>             - Makes a global system announcement\r\n" + 
        " changerank <who> <rank>    - Changes the rank of a player\r\n" + 
        " reload <db>                - Reloads the requested database\r\n" + 
        " shutdown                   - Shuts the server down\r\n";

    static string end = white + bold + 
        "--------------------------------------------------------------------------------";


    if( p_rank == REGULAR )
        return help + end;
    else if( p_rank == GOD )
        return help + god + end;
    else if( p_rank == ADMIN )
        return help + god + admin + end;
    else return "ERROR";

}

// ------------------------------------------------------------------------
//  This prints up the stats of the player
// ------------------------------------------------------------------------
string Game::PrintStats()
{
    using namespace BasicLib;
    Player& p = *m_player;

    return white + bold +
        "---------------------------------- Your Stats ----------------------------------\r\n" + 
        " Name:          " + p.Name() + "\r\n" +
        " Rank:          " + GetRankString( p.Rank() ) + "\r\n" + 
        " HP/Max:        " + tostring( p.HitPoints() ) + "/" + tostring( p.GetAttr( MAXHITPOINTS ) ) +
        "  (" + tostring( percent( p.HitPoints(), p.GetAttr( MAXHITPOINTS )) ) + "%)\r\n" +
        PrintExperience() + "\r\n" + 
        " Strength:      " + tostring( p.GetAttr( STRENGTH ), 16 ) + 
        " Accuracy:      " + tostring( p.GetAttr( ACCURACY ) ) + "\r\n" +
        " Health:        " + tostring( p.GetAttr( HEALTH ), 16 ) + 
        " Dodging:       " + tostring( p.GetAttr( DODGING ) ) + "\r\n" +
        " Agility:       " + tostring( p.GetAttr( AGILITY ), 16 ) + 
        " Strike Damage: " + tostring( p.GetAttr( STRIKEDAMAGE ) ) + "\r\n" +
        " StatPoints:    " + tostring( p.StatPoints(), 16 ) + 
        " Damage Absorb: " + tostring( p.GetAttr( DAMAGEABSORB ) ) + "\r\n" +
        "--------------------------------------------------------------------------------";
}

// ------------------------------------------------------------------------
//  This prints up the experience of the player
// ------------------------------------------------------------------------
string Game::PrintExperience()
{
    using namespace BasicLib;
    Player& p = *m_player;

    return white + bold +
        " Level:         " + tostring( p.Level() ) + "\r\n" + 
        " Experience:    " + tostring( p.Experience() ) + "/" +
        tostring( p.NeedForLevel( p.Level() + 1 ) ) + " (" + 
        tostring( percent( p.Experience(), p.NeedForLevel( p.Level() + 1 ) ) ) + 
        "%)";
}

// ------------------------------------------------------------------------
//  This prints up the inventory-list of the player
// ------------------------------------------------------------------------
string Game::PrintInventory()
{
    using namespace BasicLib;
    Player& p = *m_player;

    // Inventory
    string itemlist = white + bold +
        "-------------------------------- Your Inventory --------------------------------\r\n" +
        " Items:  ";

    for( int i = 0; i < PLAYERITEMS; i++ )
    {
        if( p.GetItem( i ) != 0 )
        {
            itemlist += p.GetItem( i )->Name() + ", ";
        }
    }

    // chop off the extraneous comma, and add a newline.
    itemlist.erase( itemlist.size() - 2, 2 );
    itemlist += "\r\n";

    // Weapon/Armor
    itemlist += " Weapon: ";
    if( p.Weapon() == 0 )
        itemlist += "NONE!";
    else
        itemlist += p.Weapon()->Name();

    itemlist += "\r\n Armor:  ";
    if( p.Armor() == 0 )
        itemlist += "NONE!";
    else
        itemlist += p.Armor()->Name();

    // Money
    itemlist += "\r\n Money:  $" + tostring( p.Money() );

    itemlist +=
        "\r\n--------------------------------------------------------------------------------";
    
    return itemlist;
}

// ------------------------------------------------------------------------
//  This finds and attempts to "use" an item in your inventory.
// ------------------------------------------------------------------------
bool Game::UseItem( const std::string& p_item )
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -