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

📄 game.cpp

📁 MUD游戏编程光盘代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
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( (int)(100*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( (int)(100 * 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 )
{
    Player& p = *m_player;

    int i = p.GetItemIndex( p_item );

    if( i == -1 )
    {
        p.SendString( red + bold + "Could not find that item!" );
        return false;
    }

    Item& itm = *p.GetItem( i );
    

    switch( itm.Type() )
    {
    case WEAPON:
        p.UseWeapon( i );
        return true;

    case ARMOR:
        p.UseArmor( i );
        return true;

    case HEALING:
        p.AddBonuses( itm.ID() );
        p.AddHitpoints( BasicLib::RandomInt( itm.Min(), itm.Max() ) );
        p.DropItem( i );
        return true;
    }
    return false;
}

// ------------------------------------------------------------------------
//  This removes your weapon or your armor
// ------------------------------------------------------------------------
bool Game::RemoveItem( std::string p_item )
{
    Player& p = *m_player;

    p_item = BasicLib::LowerCase( p_item );

    if( p_item == "weapon" && p.Weapon() != 0 )
    {
        p.RemoveWeapon();
        return true;
    }

    if( p_item == "armor" && p.Armor() != 0 )
    {
        p.RemoveArmor();
        return true;
    }

    p.SendString( red + bold + "Could not Remove item!" );
    return false;
}





void Game::GotoTrain()
{
    Player& p = *m_player;
    LogoutMessage( p.Name() + " leaves to edit stats" );
    m_connection->AddHandler( new Train( *m_connection, p.ID() ) );
}




}   // end namespace SimpleMUD

⌨️ 快捷键说明

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