📄 sentient.cpp
字号:
{
return;
}
newWeapon = NULL;
if ( currentWeapon )
{
currentWeapon->DetachFromOwner();
}
SetCurrentWeapon( weapon );
}
qboolean Sentient::CanChangeWeapons
(
void
)
{
return !(
( newWeapon != NULL ) ||
( currentWeapon && !currentWeapon->ReadyToChange() ) ||
( flags & FL_MUTANT )
);
}
void Sentient::SetCurrentWeapon
(
Weapon *weapon
)
{
const char *name;
newWeapon = NULL;
currentWeapon = weapon;
if ( currentWeapon )
{
currentWeapon->ReadyWeapon();
// get the name of the group that the gun bone belongs to
name = gi.GetBoneGroupName( edict->s.modelindex, "gun" );
if ( name )
{
gun_bone_group_name = name;
}
else
{
gun_bone_group_name = "";
}
}
}
Weapon *Sentient::CurrentWeapon
(
void
)
{
return currentWeapon;
}
Weapon *Sentient::BestWeapon
(
Weapon *ignore
)
{
Item *next;
int n;
int j;
int bestrank;
Weapon *bestweapon;
n = inventory.NumObjects();
// Search forewards until we find a weapon
bestweapon = NULL;
bestrank = -999999;
for( j = 1; j <= n; j++ )
{
next = ( Item * )G_GetEntity( inventory.ObjectAt( j ) );
assert( next );
if ( ( next != ignore ) && next->isSubclassOf( Weapon ) && ( ( ( Weapon * )next )->Rank() > bestrank ) &&
( ( ( Weapon * )next )->HasAmmo() ) )
{
bestweapon = ( Weapon * )next;
bestrank = bestweapon->Rank();
}
}
return bestweapon;
}
Weapon *Sentient::NextWeapon
(
Weapon *weapon
)
{
Item *item;
int i;
int n;
int weaponorder;
Weapon *choice;
int choiceorder;
Weapon *bestchoice;
int bestorder;
Weapon *worstchoice;
int worstorder;
if ( !inventory.ObjectInList( weapon->entnum ) )
{
error( "NextWeapon", "Weapon not in list" );
}
weaponorder = weapon->Order();
bestchoice = weapon;
bestorder = 65535;
worstchoice = weapon;
worstorder = weaponorder;
n = inventory.NumObjects();
for( i = 1; i <= n; i++ )
{
item = ( Item * )G_GetEntity( inventory.ObjectAt( i ) );
assert( item );
if ( item->isSubclassOf( Weapon ) )
{
choice = ( Weapon * )item;
if ( !choice->HasAmmo() || !choice->AutoChange() )
{
continue;
}
choiceorder = choice->Order();
if ( ( choiceorder > weaponorder ) && ( choiceorder < bestorder ) )
{
bestorder = choiceorder;
bestchoice = choice;
}
if ( choiceorder < worstorder )
{
worstorder = choiceorder;
worstchoice = choice;
}
}
}
if ( bestchoice == weapon )
{
return worstchoice;
}
return bestchoice;
}
Weapon *Sentient::PreviousWeapon
(
Weapon *weapon
)
{
Item *item;
int i;
int n;
int weaponorder;
Weapon *choice;
int choiceorder;
Weapon *bestchoice;
int bestorder;
Weapon *worstchoice;
int worstorder;
if ( !inventory.ObjectInList( weapon->entnum ) )
{
error( "PreviousWeapon", "Weapon not in list" );
}
weaponorder = weapon->Order();
bestchoice = weapon;
bestorder = -65535;
worstchoice = weapon;
worstorder = weaponorder;
n = inventory.NumObjects();
for( i = 1; i <= n; i++ )
{
item = ( Item * )G_GetEntity( inventory.ObjectAt( i ) );
assert( item );
if ( item->isSubclassOf( Weapon ) )
{
choice = ( Weapon * )item;
if ( !choice->HasAmmo() || !choice->AutoChange() )
{
continue;
}
choiceorder = choice->Order();
if ( ( choiceorder < weaponorder ) && ( choiceorder > bestorder ) )
{
bestorder = choiceorder;
bestchoice = choice;
}
if ( choiceorder > worstorder )
{
worstorder = choiceorder;
worstchoice = choice;
}
}
}
if ( bestchoice == weapon )
{
return worstchoice;
}
return bestchoice;
}
qboolean Sentient::WeaponReady
(
void
)
{
Weapon *best;
if ( !currentWeapon )
{
return false;
}
if (!currentWeapon->ReadyToUse() )
{
best = BestWeapon();
if ( best )
{
ChangeWeapon( best );
}
return false;
}
if ( !currentWeapon->ReadyToFire() )
{
return false;
}
if ( !currentWeapon->HasAmmo() )
{
best = BestWeapon();
if ( best )
{
ChangeWeapon( best );
}
return false;
}
return true;
}
void Sentient::DropWeapon
(
Weapon *weapon
)
{
if ( weapon == currentWeapon )
{
if ( deadflag )
{
SetCurrentWeapon( NULL );
}
else
{
// Choose the best weapon that's not the current
SetCurrentWeapon( BestWeapon( currentWeapon ) );
}
}
// check if the sentient can drop the weapon
if ( dropweapon )
{
// This one checks if the weapon is droppable
if ( !weapon->Drop() && deadflag )
{
// can't drop it, probably because there's no worldmodel
// since we're dead, just delete the weapon.
delete weapon;
}
}
else
{
delete weapon;
}
}
void Sentient::DropCurrentWeapon
(
void
)
{
if ( currentWeapon )
DropWeapon( currentWeapon );
}
void Sentient::takeWeapon
(
const char *weaponname
)
{
Weapon *weapon;
assert( weaponname );
weapon = ( Weapon * )FindItem( weaponname );
if ( !weapon )
{
return;
}
if ( weapon == currentWeapon )
{
SetCurrentWeapon( BestWeapon( weapon ) );
}
weapon->DetachFromOwner();
weapon->PostEvent( EV_Remove, 0 );
}
void Sentient::EventGiveTargetname
(
Event *ev
)
{
str name;
Item *item=NULL;
edict_t *from;
name = ev->GetString( 1 );
for ( from = &g_edicts[ 1 ]; from < &g_edicts[ globals.num_edicts ] ; from++ )
{
if ( !from->inuse )
continue;
if ( from->entity->isSubclassOf( Item ) )
{
item = ( Item * ) from->entity;
if ( item->itemname == name )
break;
}
}
// Item should be found
assert( item );
if ( item )
{
item->SetOwner( this );
item->ProcessPendingEvents();
AddItem( item );
}
else
{
ev->Error( "Could not give item %s to %s.\n", name, targetname );
}
}
Item *Sentient::giveItem
(
const char * itemname,
int amount,
int icon_index
)
{
ClassDef *cls;
Item *item;
int index;
item = ( Item * )FindItem( itemname );
if ( item )
{
item->Add( amount );
}
else
{
cls = getClass( itemname );
if ( !cls )
{
gi.dprintf( "No item named '%s'\n", itemname );
return NULL;
}
item = ( Item * )cls->newInstance();
item->SetOwner( this );
item->ProcessPendingEvents();
item->Set( amount );
AddItem( item );
}
// Send icon to the player
if ( this->isSubclassOf( Player ) )
{
if ( icon_index >= 0 )
index = icon_index;
else
index = item->Icon();
if ( index >= 0 )
{
gi.WriteByte( svc_console_command );
gi.WriteString( va( "pl %d %d", index, amount ) );
gi.unicast( edict, NULL );
}
}
return item;
}
void Sentient::takeItem
(
const char *itemname,
int amount
)
{
Item *item;
item = ( Item * )FindItem( itemname );
if ( item )
{
item->Remove( amount );
if ( !item->Amount() )
item->ProcessEvent( EV_Remove );
}
}
void Sentient::EventTakeItem
(
Event *ev
)
{
int amount;
if ( ev->NumArgs() > 1 )
amount = ev->GetInteger( 2 );
else
amount = 1;
takeItem( ev->GetString( 1 ), amount );
}
Weapon *Sentient::giveWeapon
(
const char *weaponname
)
{
Weapon *weapon;
assert( weaponname );
if ( !checkInheritance( &Weapon::ClassInfo, weaponname ) )
{
gi.dprintf( "A %s is not usable as a weapon\n", weaponname );
return NULL;
}
weapon = ( Weapon * )giveItem( weaponname, 1 );
if ( !currentWeapon || ( weapon->Rank() > currentWeapon->Rank() ) )
{
ChangeWeapon( weapon );
}
return weapon;
}
Weapon *Sentient::useWeapon
(
const char *weaponname
)
{
Weapon *weapon;
assert( weaponname );
weapon = ( Weapon * )FindItem( weaponname );
if ( weapon )
{
ChangeWeapon( weapon );
}
return currentWeapon;
}
void Sentient::EventGiveWeapon
(
Event *ev
)
{
ChangeWeapon( giveWeapon( ev->GetString( 1 ) ) );
}
void Sentient::EventTakeWeapon
(
Event *ev
)
{
takeWeapon( ev->GetString( 1 ) );
}
void Sentient::EventTakeAmmo
(
Event *ev
)
{
int amount;
const char *type;
Ammo *ammo;
Weapon *best;
type = ev->GetString( 1 );
amount = ev->GetInteger( 2 );
ammo = ( Ammo * )FindItem( type );
if ( ammo )
{
ammo->Remove( amount );
}
if ( currentWeapon && !currentWeapon->HasAmmo() )
{
best = BestWeapon();
if ( best )
{
ChangeWeapon( best );
}
}
}
void Sentient::EventGiveAmmo
(
Event *ev
)
{
int amount;
const char *type;
type = ev->GetString( 1 );
amount = ev->GetInteger( 2 );
if ( !checkInheritance( &Ammo::ClassInfo, type ) )
{
gi.dprintf( "%s is not usable as ammo\n", type );
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -