?? game.cpp
字號:
// flooding the server.
// ------------------------------------------------------------------------
void Game::Flooded()
{
USERLOG.Log(
GetIPString( m_connection->GetRemoteAddress() ) +
" - User " + m_player->Name() +
" 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 )
{
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 );
SendRoom( green + bold + p.Name() + " arms a " + itm.Name(),
p.CurrentRoom() );
return true;
case ARMOR:
p.UseArmor( i );
SendRoom( green + bold + p.Name() + " puts on a " + itm.Name(),
p.CurrentRoom() );
return true;
case HEALING:
p.AddBonuses( itm.ID() );
p.AddHitpoints( BasicLib::RandomInt( itm.Min(), itm.Max() ) );
p.DropItem( i );
SendRoom( green + bold + p.Name() + " uses a " + itm.Name(),
p.CurrentRoom() );
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 )
{
SendRoom( green + bold + p.Name() + " puts away a " +
p.Weapon()->Name(), p.CurrentRoom() );
p.RemoveWeapon();
return true;
}
if( p_item == "armor" && p.Armor() != 0 )
{
SendRoom( green + bold + p.Name() + " takes off a " +
p.Armor()->Name(), p.CurrentRoom() );
p.RemoveArmor();
return true;
}
p.SendString( red + bold + "Could not Remove item!" );
return false;
}
string Game::PrintRoom( room p_room )
{
string desc = "\r\n" + bold + white + p_room->Name() + "\r\n";
string temp;
int count;
desc += bold + magenta + p_room->Description() + "\r\n";
desc += bold + green + "exits: ";
for( int d = 0; d < NUMDIRECTIONS; d++ )
{
if( p_room->Adjacent( d ) != 0 )
desc += DIRECTIONSTRINGS[d] + " ";
}
desc += "\r\n";
// ---------------------------------
// ITEMS
// ---------------------------------
temp = bold + yellow + "You see: ";
count = 0;
if( p_room->Money() > 0 )
{
count++;
temp += "$" + tostring( p_room->Money() ) + ", ";
}
std::list<item>::iterator itemitr = p_room->Items().begin();
while( itemitr != p_room->Items().end() )
{
count++;
temp += (*itemitr)->Name() + ", ";
++itemitr;
}
if( count > 0 )
{
// chop off the trailing ", "
temp.erase( temp.size() - 2, 2 );
// add a newline
desc += temp + "\r\n";
}
// ---------------------------------
// PEOPLE
// ---------------------------------
temp = bold + cyan + "People: ";
count = 0;
std::list<player>::iterator playeritr = p_room->Players().begin();
while( playeritr != p_room->Players().end() )
{
temp += (*playeritr)->Name() + ", ";
count++;
++playeritr;
}
if( count > 0 )
{
temp.erase( temp.size() - 2, 2 );
desc += temp + "\r\n";
}
// ---------------------------------
// ENEMIES
// ---------------------------------
temp = bold + red + "Enemies: ";
count = 0;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -