📄 game.cpp
字号:
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";
}
return desc;
}
void Game::SendRoom( string p_text, room p_room )
{
std::for_each( p_room->Players().begin(),
p_room->Players().end(),
playersend( p_text ) );
}
void Game::Move( int p_direction )
{
Player& p = *m_player;
room next = p.CurrentRoom()->Adjacent( p_direction );
room previous = p.CurrentRoom();
if( next == 0 )
{
SendRoom( red + p.Name() + " bumps into the wall to the " +
DIRECTIONSTRINGS[p_direction] + "!!!",
p.CurrentRoom() );
return;
}
previous->RemovePlayer( p.ID() );
SendRoom( green + p.Name() + " leaves to the " +
DIRECTIONSTRINGS[p_direction] + ".",
previous );
SendRoom( green + p.Name() + " enters from the " +
DIRECTIONSTRINGS[OppositeDirection(p_direction)] + ".",
next );
p.SendString( green + "You walk " + DIRECTIONSTRINGS[p_direction] + "." );
p.CurrentRoom() = next;
next->AddPlayer( p.ID() );
p.SendString( PrintRoom( next ) );
}
void Game::GetItem( string p_item )
{
Player& p = *m_player;
if( p_item[0] == '$' )
{
// clear off the '$', and convert the result into a number.
p_item.erase( 0, 1 );
money m = BasicLib::totype<money>( p_item );
// make sure there's enough money in the room
if( m > p.CurrentRoom()->Money() )
{
p.SendString( red + bold + "There isn't that much here!" );
}
else
{
p.Money() += m;
p.CurrentRoom()->Money() -= m;
SendRoom( cyan + bold + p.Name() + " picks up $" +
tostring( m ) + ".", p.CurrentRoom() );
}
return;
}
item i = p.CurrentRoom()->FindItem( p_item );
if( i == 0 )
{
p.SendString( red + bold + "You don't see that here!" );
return;
}
if( !p.PickUpItem( i ) )
{
p.SendString( red + bold + "You can't carry that much!" );
return;
}
p.CurrentRoom()->RemoveItem( i );
SendRoom( cyan + bold + p.Name() + " picks up " + i->Name() + ".",
p.CurrentRoom() );
}
void Game::DropItem( string p_item )
{
Player& p = *m_player;
if( p_item[0] == '$' )
{
// clear off the '$', and convert the result into a number.
p_item.erase( 0, 1 );
money m = BasicLib::totype<money>( p_item );
// make sure there's enough money in the inventory
if( m > p.Money() )
{
p.SendString( red + bold + "You don't have that much!" );
}
else
{
p.Money() -= m;
p.CurrentRoom()->Money() += m;
SendRoom( cyan + bold + p.Name() + " drops $" +
tostring( m ) + ".", p.CurrentRoom() );
}
return;
}
int i = p.GetItemIndex( p_item );
if( i == -1 )
{
p.SendString( red + bold + "You don't have that!" );
return;
}
SendRoom( cyan + bold + p.Name() + " drops " +
p.GetItem( i )->Name() + ".", p.CurrentRoom() );
p.CurrentRoom()->AddItem( p.GetItem( i ) );
p.DropItem( i );
}
void Game::GotoTrain()
{
Player& p = *m_player;
p.Active() = false;
p.Conn()->AddHandler( new Train( *m_connection, p.ID() ) );
LogoutMessage( p.Name() + " leaves to edit stats" );
}
string Game::StoreList( entityid p_store )
{
Store& s = StoreDatabase::get( p_store );
string output = white + bold +
"--------------------------------------------------------------------------------\r\n";
output += " Welcome to " + s.Name() + "!\r\n";
output += "--------------------------------------------------------------------------------\r\n";
output += " Item | Price\r\n";
output += "--------------------------------------------------------------------------------\r\n";
Store::iterator itr = s.begin();
while( itr != s.end() )
{
output += " " + tostring( (*itr)->Name(), 31 ) + "| ";
output += tostring( (*itr)->Price() ) + "\r\n";
++itr;
}
output += bold +
"--------------------------------------------------------------------------------\r\n";
return output;
}
void Game::Buy( const string& p_item )
{
Player& p = *m_player;
Store& s = StoreDatabase::get( p.CurrentRoom()->Data() );
item i = s.find( p_item );
if( i == 0 )
{
p.SendString( red + bold + "Sorry, we don't have that item!" );
return;
}
if( p.Money() < i->Price() )
{
p.SendString( red + bold + "Sorry, but you can't afford that!" );
return;
}
if( !p.PickUpItem( i ) )
{
p.SendString( red + bold + "Sorry, but you can't carry that much!" );
return;
}
p.Money() -= i->Price();
SendRoom( cyan + bold + p.Name() + " buys a " + i->Name(),
p.CurrentRoom() );
}
void Game::Sell( const string& p_item )
{
Player& p = *m_player;
Store& s = StoreDatabase::get( p.CurrentRoom()->Data() );
int index = p.GetItemIndex( p_item );
if( index == -1 )
{
p.SendString( red + bold + "Sorry, you don't have that!" );
return;
}
item i = p.GetItem( index );
if( !s.has( i ) )
{
p.SendString( red + bold + "Sorry, we don't want that item!" );
return;
}
p.DropItem( index );
p.Money() += i->Price();
SendRoom( cyan + bold + p.Name() + " sells a " + i->Name(),
p.CurrentRoom() );
}
} // end namespace SimpleMUD
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -