Esempio n. 1
0
void AvHBaseBuildable::ConstructUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
	bool theSuccess = false;
	bool theIsBuilding = false;
	bool theIsResearching = false;
	float thePercentage = 0.0f;

	AvHSHUGetBuildResearchState(this->pev->iuser3, this->pev->iuser4, this->pev->fuser1, theIsBuilding, theIsResearching, thePercentage);
	
	// Only allow players to help along building, not researching
	if(theIsBuilding)
	{
		// Only allow users from same team as turret deployer
		float thePercentage = this->GetNormalizedBuildPercentage();
		if(pActivator->pev->team == this->pev->team && (thePercentage < 1.0f))
		{
			AvHPlayer* thePlayer = dynamic_cast<AvHPlayer*>(pActivator);
			ASSERT(thePlayer);
	
			// Only soldiers and builders can build
			if(thePlayer->GetIsAbleToAct() && ((thePlayer->pev->iuser3 == AVH_USER3_MARINE_PLAYER) || (thePlayer->pev->iuser3 == AVH_USER3_ALIEN_PLAYER2)))
			{
                AvHBasePlayerWeapon* theWeapon = dynamic_cast<AvHBasePlayerWeapon*>(thePlayer->m_pActiveItem);
                if(!theWeapon || theWeapon->CanHolster())
                {
				    thePlayer->PlayerConstructUse();
	    
				    bool thePlaySound = false;
				    
				    // Ensure that buildings are never absolutely painful to create
				    int theBuildTime = max(GetGameRules()->GetBuildTimeForMessageID(this->mMessageID), 1);
	    
				    if((GetGameRules()->GetIsTesting() || GetGameRules()->GetCheatsEnabled()) && !GetGameRules()->GetIsCheatEnabled(kcSlowResearch))
				    {
					    theBuildTime = 2;
				    }
	    
				    // Make non-frame-rate dependent
				    const float kDefaultInterval = .1f;
				    float theTimeOfLastConstructUse = thePlayer->GetTimeOfLastConstructUse();
				    
				    float theInterval = min(max(gpGlobals->time - theTimeOfLastConstructUse, 0.0f), kDefaultInterval);
				    thePercentage += (theInterval/(float)theBuildTime);

				    thePlayer->SetTimeOfLastConstructUse(gpGlobals->time);
	    
				    if(gpGlobals->time > (this->mLastTimePlayedSound + this->mAverageUseSoundLength))
				    {
					    AvHSUPlayRandomConstructionEffect(thePlayer, this);
					    this->mLastTimePlayedSound = gpGlobals->time;
				    }
				    
				    // Given the number of constructors, what's chance of starting a new sound?
				    float theChanceForNewSound = (gpGlobals->frametime/(this->mAverageUseSoundLength));//  /2.0f));
				    float theRandomFloat = RANDOM_FLOAT(0.0f, 1.0f);
				    if(theRandomFloat < theChanceForNewSound)
				    {
					    AvHSUPlayRandomConstructionEffect(thePlayer, this);
				    }
				    
				    //if(RANDOM_LONG(0, 20) == 0)
				    //{
				    //	char theMessage[128];
				    //	sprintf(theMessage, "Time passed: %f, ticks: %d, rate: %f\n", theTimePassed, this->mPreThinkTicks, this->mPreThinkFrameRate);
				    //	UTIL_SayText(theMessage, this);
				    //}
					    
				    this->SetNormalizedBuildPercentage(thePercentage);

				    theSuccess = true;

					// GHOSTBUILD: Manifest structure.
					pev->renderamt = 255;
					pev->rendermode = kRenderNormal;
					pev->solid = SOLID_BBOX;
					this->mGhost = false;

                }
			}
		}
	}

	// Clear out +use sound when ineffective
	if(!theSuccess)
	{
		EMIT_SOUND(pActivator->edict(), CHAN_ITEM, "common/null.wav", 1.0, ATTN_NORM);
	}
}