Example #1
0
//------------------------------------------------------------------------
bool CItem::IsCurrentItem()
{
	CActor *pOwner = GetOwnerActor();
	if (!pOwner)
		return false;

	if (pOwner->GetCurrentItem() == this)
		return true;

	return false;
}
bool CMelee::SwitchToMeleeWeaponAndAttack()
{
	// Checking for the equipped MeleeWeapon for each melee is hateful, but there's nowhere to check the inventory when a weapon is equipped.
	CActor* pOwner = m_pWeapon->GetOwnerActor();
	if( pOwner && (pOwner->GetInventory()->GetItemByClass( CItem::sWeaponMeleeClass ) != 0) )
	{
		m_pWeapon->SetPlaySelectAction( false );
		pOwner->SelectItemByName( "WeaponMelee", true );

		// trigger the attack - should be good without ptr checks, we validated that the weapon exists in the inventory.
		pOwner->GetCurrentItem()->GetIWeapon()->MeleeAttack();

		return true;
	}

	return false;
}
Example #3
0
void CGameStateRecorder::OnGameplayEvent(IEntity *pEntity, const GameplayEvent &event)
{
	EntityId id;
	if(!pEntity || !(id = pEntity->GetId()))
	{
		GameWarning("TimeDemo:GameState::OnGamePlayEvent: Entity not found");
		return;
	}
	CActor *pActor = (CActor*)(gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(id));
	if(!pActor)
	{
		GameWarning("TimeDemo:GameState::OnGamePlayEvent: Entity %s has no actor", pEntity->GetName());
		return;
	}

	GameplayEvent  event2 = event;
	event2.extra = 0;// event2 is the forwarded event, extra either will be not used by the listener or re-set as a string
	uint8 eType = event.event;

	bool bPlayer = (pActor->IsPlayer() && m_mode);
	if(bPlayer || m_mode==GPM_AllActors)
	{
		//items
		switch(eType)
		{
			case eGE_ItemPickedUp:
				{
					CheckInventory(pActor,0);//,*m_pRecordGameEventFtor);
				}
				break;

			case eGE_ItemDropped:
				{
					TItemName itemName = GetItemName(EntityId(event.extra));
					if(!itemName ) //if(itemIdx < 0)
						break;
					event2.description = itemName;
					SendGamePlayEvent(pEntity,event2);
					IEntity* pItemEntity = gEnv->pEntitySystem->FindEntityByName(itemName);
					if(!pItemEntity)
						break;
					IItem* pItem = g_pGame->GetIGameFramework()->GetIItemSystem()->GetItem(pItemEntity->GetId());
					if(!pItem)
						break;

					IEntityClass* pItemClass = pItem->GetEntity()->GetClass();
					if(pItemClass && !strcmpi(pItemClass->GetName(),"SOCOM"))
					{
						IItem* pCurrentItem = pActor->GetCurrentItem();
						if(pCurrentItem)
						{
							IEntityClass* pCurrentItemClass = pCurrentItem->GetEntity()->GetClass();
							if(pCurrentItemClass && !strcmpi(pCurrentItemClass->GetName(),"SOCOM"))
							{
								GameplayEvent event3;
								event3.event = eGE_ItemSelected;
								TItemName itemName = GetItemName(pCurrentItem->GetEntity()->GetId());
								if(!itemName)
									break;
								event3.value = 0;
								event3.description = (const char*)itemName;
								SendGamePlayEvent(pEntity,event3);
							}
						}
					}
				}
				break;

			case eGE_WeaponFireModeChanged:
				{
					TItemName itemIdx = GetItemName(EntityId(event.extra));
					if(!itemIdx)//if(itemIdx < 0)
						break;
					event2.description = (const char*)itemIdx;
					SendGamePlayEvent(pEntity,event2);
				}
				break;

			case eGE_ItemSelected:
				{
					EntityId itemId = EntityId(event.extra);
					TItemName itemIdx = GetItemName(itemId);
					if(itemId && !itemIdx)
						break;
					event2.value = 0;
					event2.description = (const char*)itemIdx;
					SendGamePlayEvent(pEntity,event2);
				}
				break;

			case eGE_AttachedAccessory:
				{
					if(!IsGrenade(event.description)) // NOT OffHandGrenade
						SendGamePlayEvent(pEntity,event2);
				}
				break;

			case eGE_AmmoCount:
				{
					const char* itemIdx = event.description;
					if(!itemIdx)
						break;

					TGameStates::iterator itGS;
					/*if(pActor->IsPlayer())
						itGS = m_itSingleActorGameState;
					else */if(pActor->GetEntity())
						itGS = m_GameStates.find(pActor->GetEntity()->GetId());
					else
						break;

					if(itGS == m_GameStates.end())
						break;

					SActorGameState& gstate = itGS->second;

					IEntity* pItemEntity = gEnv->pEntitySystem->FindEntityByName(itemIdx);
					if(!pItemEntity)
						break;
					IItem* pItem = g_pGame->GetIGameFramework()->GetIItemSystem()->GetItem(pItemEntity->GetId());
					if(!pItem)
						break;

					CWeapon* pWeapon = (CWeapon*)(pItem->GetIWeapon());
					if(pWeapon && pWeapon->GetEntity())
					{
						TItemContainer::iterator it = gstate.Items.find(itemIdx);
						if(it==gstate.Items.end())
							break;
						SItemProperties& recItem = it->second;
						
						SWeaponAmmo weaponAmmo = pWeapon->GetFirstAmmo();
						bool bGrenade = false;
						if(!weaponAmmo.pAmmoClass)
						{
							// special case for grenades
							if(IsAmmoGrenade((const char*)(event.extra)))
							{
								weaponAmmo.pAmmoClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass((const char*)event.extra);
								weaponAmmo.count = (int)event.value;
								bGrenade = true;
							}
						}
						
						for(; weaponAmmo.pAmmoClass ; weaponAmmo = pWeapon->GetNextAmmo())
						{
							int ammoCount = weaponAmmo.count;
							const char* ammoClass;
							if(weaponAmmo.pAmmoClass && (ammoClass = weaponAmmo.pAmmoClass->GetName()))
							{
								TAmmoContainer& recAmmo = bGrenade? gstate.AmmoMags : recItem.Ammo;
								if(recAmmo.find(ammoClass) == recAmmo.end())
									recAmmo.insert(std::make_pair(ammoClass,0));
								if(ammoCount != recAmmo[ammoClass])
								{
									event2.event = eGE_AmmoCount;
									event2.value = (float)ammoCount;
									if(event2.value < 0)
										event2.value = 0;
									event2.extra = (void*)ammoClass;
									event2.description = (const char*)itemIdx;
									SendGamePlayEvent(pEntity,event2);
								}
							}
						}
					}

				}
				break;

			case eGE_EntityGrabbed:
				{
					EntityId entityId = EntityId(event.extra);
					IEntity * pGrabbedEntity = gEnv->pEntitySystem->GetEntity(entityId);
					if(pGrabbedEntity)
					{
						event2.description = pGrabbedEntity->GetName();
						SendGamePlayEvent(pEntity,event2);
					}
				}
				break;

			case eGE_WeaponReload:
			case eGE_ZoomedIn:
			case eGE_ZoomedOut:			
			case eGE_HealthChanged:
			case eGE_ItemExchanged:
				SendGamePlayEvent(pEntity,event2);
				break;

			default:
				break;
		}

	}
}
Example #4
0
void CGameStateRecorder::OnRecordedGameplayEvent(IEntity *pEntity, const GameplayEvent &event, int currentFrame, bool bRecording)
{
	EntityId id;
	m_currentFrame = currentFrame;

	int demo_forceGameState = 0;
	if(!bRecording)
	{
		ICVar *pVar = gEnv->pConsole->GetCVar( "demo_force_game_state" );
		if(pVar)
			demo_forceGameState = pVar->GetIVal();
	}
	
	if(!pEntity || !(id = pEntity->GetId()))
		return;

	if(m_IgnoredEvents.size())
		if(event.event == m_IgnoredEvents[0])
		{
			m_IgnoredEvents.erase(m_IgnoredEvents.begin());
			return;
		}

		TGameStates::iterator itActor = m_GameStates.find(id);
	if(itActor == m_GameStates.end())
	{
		m_GameStates.insert(std::make_pair(id,SActorGameState()));
		itActor = m_GameStates.find(id);
	}
	if(itActor == m_GameStates.end())
	{
		GameWarning("TimeDemo:GameState: actor %s not found in records",pEntity->GetName());
		return;
	}

	SActorGameState& gstate = itActor->second;

	switch(event.event)
	{
		case eGE_HealthChanged: 
			{
				gstate.health = event.value;

				if(!m_bRecording)
				{
					CActor *pActor = (CActor*)(gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(id));
					if(pActor)
					{
						if(m_bLogWarning)
						{
							if(CHECK_MISMATCH(pActor->GetHealth(), gstate.health,10))
							{
								if(!gstate.bHealthDifferent)
								{
									GameWarning("TimeDemo:GameState: Frame %d - Actor %s - HEALTH mismatch (%d, %d)",m_currentFrame,pEntity->GetName(), static_cast<int>(pActor->GetHealth()), static_cast<int>(gstate.health));
									gstate.bHealthDifferent = true;
								}
							}
							else
								gstate.bHealthDifferent = false;
						}

						if( demo_forceGameState)
							pActor->SetHealth(gstate.health);
					}
				}
			}
			break;

		case eGE_WeaponFireModeChanged:
			{
				TItemName sel = (event.description);
				if(sel)
				{
					TItemContainer& Items = gstate.Items;
					TItemContainer::iterator iti = Items.find(sel);
					uint8 recFireModeIdx = uint8(event.value);
					if(iti != Items.end())
						iti->second.fireMode = recFireModeIdx;

					CActor *pActor = (CActor*)(gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(id));
					if(pActor && pActor->GetInventory())
					{
						IItem* pItem = pActor->GetInventory()->GetItemByName(sel);
						CWeapon* pWeapon;
						if(pItem && (pWeapon = (CWeapon*)(pItem->GetIWeapon())))
						{

						int fireModeIdx = pWeapon->GetCurrentFireMode();
						if(m_bLogWarning)
							{
									CheckDifference(recFireModeIdx,fireModeIdx,"TimeDemo:GameState: Frame %d - Actor %s - FIRE MODE mismatch for weapon %s (rec:%d, cur:%d)",pEntity,pItem->GetEntity() ? pItem->GetEntity()->GetName() : "(null)");
							}

							if(demo_forceGameState==2 && fireModeIdx!= recFireModeIdx)
								pWeapon->SetCurrentFireMode(recFireModeIdx);
						}
					}
				}
			}
			break;

		case eGE_WeaponReload:
			{
				const char* ammoType = event.description;
				TAmmoContainer& ammoMags = gstate.AmmoMags;
				TAmmoContainer::iterator it = ammoMags.find(ammoType);
				if(it!=ammoMags.end())
				{
					it->second -= (uint16)event.value;
				
					CActor *pActor = (CActor*)(gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(id));
					if(pActor)
					{
						CInventory* pInventory = (CInventory*)(pActor->GetInventory());
						if(pInventory)
						{
							{
								IEntityClass* pAmmoClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass(ammoType);
								if(pAmmoClass)
								{
									if(m_bLogWarning)
										CheckDifference(it->second,pInventory->GetAmmoCount(pAmmoClass),"TimeDemo:GameState: Frame %d - Actor %s - WEAPON RELOAD, ammo count mismatch for ammo class %s (rec:%d, cur:%d)",pEntity,ammoType);
		
									if(demo_forceGameState == 2)
										pInventory->SetAmmoCount(pAmmoClass,it->second);
								}
							}
						}
					}
				}
			}
			break;

		case eGE_ItemSelected:
			{
				TItemName itemName = event.description;
				gstate.itemSelected = itemName;
				if(itemName)
				{
					if( !bRecording && (event.value > 0 || demo_forceGameState==2)) // EVENT.VALUE > 0 means initialization
					{
						CActor *pActor = (CActor*)(gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(id));
						if(pActor && pActor->GetInventory())
						{
							IItem* pItem = pActor->GetInventory()->GetItemByName(itemName);
							if(pItem)
								pActor->SelectItem(pItem->GetEntityId(),false);
						}
					}
				}

				if(m_bLogWarning)
				{	
					CActor *pActor = (CActor*)(gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(id));
					if(pActor)
					{
						IItem* pItem = pActor->GetCurrentItem();
						const char* curItemName= pItem && pItem->GetEntity() ? pItem->GetEntity()->GetName(): NULL;
						CheckDifferenceString(itemName,curItemName,"TimeDemo:GameState: Frame %d - Actor %s - SELECTED ITEM mismatch (rec:%s, cur:%s)",pEntity);
					}
				}
				break;
			}
			break;

		case eGE_ItemExchanged:
			{
				// prevent unwanted record/play mismatch error with current item during item exchanging
				// two unneeded game events are sent (selecting fists)
				m_IgnoredEvents.push_back(eGE_ItemSelected);
				m_IgnoredEvents.push_back(eGE_ItemSelected);
			}
			break;

		case eGE_ItemPickedUp:
			{
				TItemName sel = (TItemName)event.description;
//				gstate.itemSelected = sel;
				TItemContainer& Items = gstate.Items;
				TItemContainer::iterator it = Items.find(sel);
				if(it == Items.end())
				{
					Items.insert(std::make_pair(sel,SItemProperties()));
					it = Items.find(sel);
				}

				if(it != Items.end())
				{
					it->second.count++;

					CActor *pActor = (CActor*)(gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(id));
					if(pActor && !m_bRecording)
					{
						CInventory* pInventory = (CInventory*)(pActor->GetInventory());
						if(pInventory)
						{
							// just check if the item is the inventory
							if(m_bLogWarning && !pInventory->GetItemByName(sel))
								GameWarning("TimeDemo:GameState: Frame %d - Actor %s - Recorded PICKED UP ITEM class '%s' not found in current inventory",m_currentFrame,pEntity->GetName(),sel);

							if(demo_forceGameState == 2)
							{
								IEntity* pItemEntity = gEnv->pEntitySystem->FindEntityByName(sel);
								if(pItemEntity)
									pInventory->AddItem(pItemEntity->GetId());
								else
									GameWarning("TimeDemo:GameState: Frame %d - Actor %s - PICKED UP ITEM entity %s not found in level",m_currentFrame,pEntity->GetName(),sel);
							}
						}
					}
				}
				else if(m_bLogWarning)
				{
					if(!sel)
						sel = "(null)";
					GameWarning("TimeDemo:GameState: Frame %d - Actor %s - PICKED UP ITEM %s not found in recorded inventory",m_currentFrame,pEntity->GetName(),sel);
				}

			}
			break;

		case eGE_AmmoPickedUp:
			{
				uint16 ammoCount = (uint16)(event.value);
				TItemName sel = (TItemName)event.description;
				TAmmoContainer& Ammo = gstate.AmmoMags;

				TAmmoContainer::iterator it = Ammo.find(sel);
				if(it == Ammo.end())
					Ammo.insert(std::make_pair(sel,ammoCount));
				else
					it->second = ammoCount;

				if( !m_bRecording)
				{
					CActor *pActor = (CActor*)(gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(id));
					if(pActor)
					{
						CInventory* pInventory = (CInventory*)(pActor->GetInventory());
						if(pInventory)
						{
							IEntityClass* pClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass(sel);
							if(m_bLogWarning)
								CheckDifference(ammoCount,pInventory->GetAmmoCount(pClass),"TimeDemo:GameState: Frame %d - Actor %s - AMMO PICKEDUP: count mismatch for ammo class %s (rec:%d, cur:%d)", pEntity,sel);

							if(demo_forceGameState == 2)
								pInventory->SetAmmoCount(pClass,ammoCount);
						}
					}
				}
			}
			break;

		case eGE_AccessoryPickedUp:
			{
				TItemName sel = (TItemName)event.description;
				//				gstate.itemSelected = sel;
				TAccessoryContainer& Accessories = gstate.Accessories;
				TAccessoryContainer::iterator it = Accessories.find(sel);
				if(it == Accessories.end())
				{
					Accessories.insert(std::make_pair(sel,1));
				}
				else
					it->second++;

				CActor *pActor = (CActor*)(gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(id));
				if(pActor)
				{
					CInventory* pInventory = (CInventory*)(pActor->GetInventory());
					if(pInventory)
					{
						IEntityClass* pClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass(sel);
						
						if(m_bLogWarning && !pInventory->HasAccessory(pClass))
							GameWarning("TimeDemo:GameState: Frame %d - Actor %s - ACCESSORY PICKEDUP %s not found in current inventory", m_currentFrame, pEntity->GetName(),sel ? sel:"(null)");

						if(demo_forceGameState == 2 && pClass)					
							pInventory->AddAccessory(pClass);// doesn't actually add it if it's there already

					}
				}
			}
			break;

		case eGE_ItemDropped:
			{
				TItemName sel = (TItemName)event.description;
				//gstate.itemSelected = sel;
				TItemContainer& Items = gstate.Items;
				TItemContainer::iterator it = Items.find(sel);
				if(it != Items.end())
				{
					it->second.count--;
					if(it->second.count<=0)
						Items.erase(it);

					if(demo_forceGameState == 2)
					{
						CActor *pActor = (CActor*)(gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(id));
						if(pActor)
						{
							CInventory* pInventory = (CInventory*)(pActor->GetInventory());
							if(pInventory)
							{
								IEntityClass* pClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass(sel);
								if(pClass)
								{
									EntityId itemId = pInventory->GetItemByClass(pClass);
									if(itemId)
										pInventory->RemoveItem(itemId);
								}
							}
						}
					}
				}
				else
					GameWarning("TimeDemo:GameState: Frame %d - Actor %s - ITEM DROPPED, wrong item selected (%s)",m_currentFrame,pEntity->GetName(),sel);
			}
			break;

		case eGE_AmmoCount: 
			{
				TItemContainer& Items = gstate.Items;
				TItemName itemClassDesc = event.description;
				TItemContainer::iterator it = Items.find(itemClassDesc);
				if(it != Items.end())
				{
					SItemProperties& item = it->second;
					const char* ammoClassDesc = (const char*)(event.extra);
					if(ammoClassDesc)
					{
						string szAmmoClassDesc(ammoClassDesc);
						bool bAmmoMag = IsAmmoGrenade(ammoClassDesc);
						TAmmoContainer& itemAmmo = bAmmoMag ? gstate.AmmoMags : item.Ammo;

						if(itemAmmo.find(szAmmoClassDesc)==itemAmmo.end())
							itemAmmo.insert(std::make_pair(szAmmoClassDesc,int(event.value)));
						else
							itemAmmo[szAmmoClassDesc] = (uint16)event.value;

						if(!bRecording && (m_bLogWarning || demo_forceGameState==2))
						{
							CActor *pActor = (CActor*)(gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(id));
							if(pActor)
							{
								CInventory* pInventory = (CInventory*)(pActor->GetInventory());
								if(pInventory)
								{
									IItem* pItem = pInventory->GetItemByName(itemClassDesc);

									if(pItem)
									{
										CWeapon* pWeapon = (CWeapon*)(pItem->GetIWeapon());
										if(pWeapon)
										{
											IEntityClass* pAmmoClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass(ammoClassDesc);
											if(pAmmoClass)
											{
												if(m_bLogWarning)
												{
													int curAmmoCount = (bAmmoMag ? pInventory->GetAmmoCount(pAmmoClass) : 
														pWeapon->GetAmmoCount(pAmmoClass));
													CheckDifference( int(event.value),curAmmoCount,"TimeDemo:GameState: Frame %d - Actor %s - AMMO COUNT mismatch for ammo class %s (rec:%d, cur:%d)",pEntity,ammoClassDesc);
												}

												if(demo_forceGameState==2)
													pWeapon->SetAmmoCount(pAmmoClass,int(event.value));

											}
										}
									}
								}
							}
						}
					}
					else
						GameWarning("TimeDemo:GameState: Frame %d - Actor %s - AMMO COUNT null ammoClass descriptor",m_currentFrame,pEntity->GetName());
				}
				else
					GameWarning("TimeDemo:GameState: Frame %d - Actor %s - AMMO COUNT wrong item selected (%s)",m_currentFrame,pEntity->GetName(),itemClassDesc);
			}
			break;

		case eGE_AttachedAccessory: 
			{
				// always force attachment of accessory to the current weapon
				// kind of a workaround, the HUD weapon modifier menu is spawned during timedemo playback 
				// but it doesn't use recorded input events
				if(!bRecording)
				{
					CActor *pActor = (CActor*)(gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(id));
					if(pActor)
					{
						CInventory* pInventory = (CInventory*)(pActor->GetInventory());
						if(pInventory)
						{
							const char* sel = event.description;
							IEntityClass* pClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass(sel);
							if(!sel)
								sel = "(null)";
							if(!pInventory->HasAccessory(pClass))
							{
								if(m_bLogWarning)
									GameWarning("TimeDemo:GameState: Frame %d - Actor %s - ATTACHED ACCESSORY %s not found in current inventory", m_currentFrame,pEntity->GetName(),sel);
							}
							else
							{
								CItem* pCurrentItem = (CItem*)(pActor->GetCurrentItem());
								if(pCurrentItem)
									pCurrentItem->SwitchAccessory(sel);
							}
						}
					}
				}
			}
			break;

		case eGE_EntityGrabbed:
			{
				if(demo_forceGameState==2)
				{
					TItemName itemName = event.description;
					if(itemName)
					{
						IEntity * pGrabbedEntity = gEnv->pEntitySystem->FindEntityByName(itemName);
						if(pGrabbedEntity)
						{
							CActor *pActor = (CActor*)(gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(id));
							if(pActor)
							{
								IItem* pItem = GetItemOfName(pActor,itemName);
								if(!pItem)
								{
									// it's a pickable entity, won't end up in the inventory
									//(otherwise it would be managed by eGE_ItemPickedUp)
									COffHand* pOffHand = static_cast<COffHand*>(pActor->GetWeaponByClass(CItem::sOffHandClass));
									if(pOffHand && !pOffHand->GetPreHeldEntityId())
										pOffHand->ForcePickUp(pGrabbedEntity->GetId());
								}
							}
						}
					}	
				}
			}
			break;

		default:
			break;
	}
}
Example #5
0
//------------------------------------------------------------------------
int CScriptBind_Item::OnUsed(IFunctionHandler *pH, ScriptHandle userId)
{
	CItem *pItem = GetItem(pH);
	if (!pItem)
		return pH->EndFunction();

	CActor *pActor = GetActor((EntityId)userId.n);
	if (!pActor)
		return pH->EndFunction();

	if (pItem->CanUse((EntityId)userId.n))
	{
		if(IEntity* pParent = pItem->GetEntity()->GetParent())
		{
			IVehicle* pVehicle = gEnv->pGame->GetIGameFramework()->GetIVehicleSystem()->GetVehicle(pParent->GetId());
			if(pVehicle)
			{
				CPlayer* pPlayer = static_cast<CPlayer*>(pActor);
				IInteractor* pInteractor = pPlayer->GetInteractor();
				return pH->EndFunction( pVehicle->OnUsed((EntityId)userId.n, pInteractor->GetOverSlotIdx()) );
			}
		}

		pActor->UseItem(pItem->GetEntityId());
		return pH->EndFunction(true);
	}
	else if (pItem->CanPickUp((EntityId)userId.n))
	{
		//Should be always the client...
		if (pActor->IsClient())
		{
			CPlayer* pClientPlayer = static_cast<CPlayer*>(pActor);
			const SInteractionInfo& interactionInfo = pClientPlayer->GetCurrentInteractionInfo();
			bool expectedItem = (interactionInfo.interactiveEntityId == pItem->GetEntityId());
			bool expectedInteraction =	(interactionInfo.interactionType == eInteraction_PickupItem) || 
																	(interactionInfo.interactionType == eInteraction_ExchangeItem);
			if (!expectedItem || !expectedInteraction)
			{
				return pH->EndFunction();
			}

			if (interactionInfo.interactionType == eInteraction_ExchangeItem)
			{
				IItemSystem* pItemSystem = g_pGame->GetIGameFramework()->GetIItemSystem();
				CItem* pCurrentItem = static_cast<CItem*>(pActor->GetCurrentItem());
				CItem* pExchangeItem = static_cast<CItem*>(pItemSystem->GetItem(interactionInfo.swapEntityId));
				if (pExchangeItem && pCurrentItem)
					pExchangeItem->ScheduleExchangeToNextItem(pActor, pItem, pCurrentItem);
			}
			else
			{
				pActor->PickUpItem(pItem->GetEntityId(), true, true);
			}
		}
		else
		{
			pActor->PickUpItem(pItem->GetEntityId(), true, true);
		}
		return pH->EndFunction(true);
	}

	return pH->EndFunction();
}
Example #6
0
void CPlayerView::ViewSpectatorTarget(SViewParams &viewParams)
{
	CActor* pTarget = (CActor*)g_pGame->GetIGameFramework()->GetIActorSystem()->GetActor(m_in.stats_spectatorTarget);
	if(!pTarget)
		return;

	IVehicle* pVehicle = pTarget->GetLinkedVehicle();

	static float defaultOffset = 0.3f;
	static float viewHeight = 1.8f;

	Matrix34 worldTM = pTarget->GetEntity()->GetWorldTM();
	Vec3 worldPos = worldTM.GetTranslation();
	if(!pVehicle)
	{
		const SStanceInfo* stanceInfo = pTarget->GetStanceInfo(pTarget->GetStance());
		if(stanceInfo)
		{
			Interpolate(viewHeight, stanceInfo->viewOffset.z, 5.0f, viewParams.frameTime);
			worldPos.z += viewHeight + defaultOffset;
		}
		else
		{
			worldPos.z += 1.8f;
		}
	}
	else
	{
		// use vehicle pos/ori
		worldTM = pVehicle->GetEntity()->GetWorldTM();
		worldPos = pVehicle->GetEntity()->GetWorldPos();
		worldPos.z += 1.5f;
	}
	
	Ang3 worldAngles = Ang3::GetAnglesXYZ(Matrix33(worldTM));
	float distance = 3;

	// if freelook allowed, get orientation and distance from player entity
	if(g_pGameCVars->g_spectate_FixedOrientation == 0)
	{
		CPlayer* pThisPlayer = static_cast<CPlayer*>(g_pGame->GetIGameFramework()->GetIActorSystem()->GetActor(m_in.entityId));
		if(!pThisPlayer)
			return;
		Matrix34 ownOrientation = pThisPlayer->GetEntity()->GetWorldTM();

		worldAngles += Ang3::GetAnglesXYZ(Matrix33(ownOrientation));
		distance = pThisPlayer->GetSpectatorZoom();
	}

	if(pVehicle)
	{
		distance *= 4.0f;

		// air vehicles need bigger distance
		if(pVehicle->GetMovement() && pVehicle->GetMovement()->GetMovementType() == IVehicleMovement::eVMT_Air)
			distance *= 2.0f;
	}

	Vec3 goal;
	goal.x = distance * cos(worldAngles.z + gf_PI*1.5f) + worldPos.x;
	goal.y = distance * sin(worldAngles.z - gf_PI/2.0f) + worldPos.y;

	AABB targetBounds;
	pTarget->GetEntity()->GetLocalBounds(targetBounds);
	goal.z = targetBounds.max.z;
	float offset = defaultOffset;
	if(pVehicle)
	{
		if(pVehicle->GetMovement() && pVehicle->GetMovement()->GetMovementType() == IVehicleMovement::eVMT_Air)
			offset = 3.0f;
		else
			offset = 1.0f;
	}
	goal.z += pTarget->GetEntity()->GetWorldPos().z + offset;

	// store / interpolate the offset, not the world pos (reduces percieved jitter in vehicles)
	static Vec3 viewOffset(goal-worldPos);
	static Vec3 camPos(goal);
	static Vec3 entPos(worldPos);
	static EntityId lastSpectatorTarget(m_in.stats_spectatorTarget);

	// do a ray cast to check for camera intersection
	static ray_hit hit;	
	IPhysicalEntity* pSkipEntities[10];
	int nSkip = 0;
	if(pVehicle)
	{
		// vehicle drivers don't seem to have current items, so need to add the vehicle itself here
		nSkip = pVehicle->GetSkipEntities(pSkipEntities, 10);
	}
	else
	{
		IItem* pItem = pTarget->GetCurrentItem();
		if (pItem)
		{
			CWeapon* pWeapon = (CWeapon*)pItem->GetIWeapon();
			if (pWeapon)
				nSkip = CSingle::GetSkipEntities(pWeapon, pSkipEntities, 10);
		}
	}

	static float minDist = 0.4f;	// how close we're allowed to get to the target
	static float wallSafeDistance = 0.3f; // how far to keep camera from walls

	Vec3 dir = goal - worldPos;
	primitives::sphere sphere;
	sphere.center = worldPos;
	sphere.r = wallSafeDistance;

	geom_contact *pContact = 0;          
	float hitDist = gEnv->pPhysicalWorld->PrimitiveWorldIntersection(sphere.type, &sphere, dir, ent_static|ent_terrain|ent_rigid|ent_sleeping_rigid,
		&pContact, 0, (geom_colltype_player<<rwi_colltype_bit) | rwi_stop_at_pierceable, 0, 0, 0, pSkipEntities, nSkip);

	// even when we have contact, keep the camera the same height above the target
	float minHeightDiff = dir.z;

	if(hitDist > 0 && pContact)
	{
		goal = worldPos + (hitDist * dir.GetNormalizedSafe());
		
		if(goal.z - worldPos.z < minHeightDiff)
		{
			// can't move the camera far enough away from the player in this direction. Try moving it directly up a bit
			int numHits = 0;
			sphere.center = goal;

			// (move back just slightly to avoid colliding with the wall we've already found...)
			sphere.center -= dir.GetNormalizedSafe() * 0.05f;

			float newHitDist = gEnv->pPhysicalWorld->PrimitiveWorldIntersection(sphere.type, &sphere, Vec3(0,0,minHeightDiff), ent_static|ent_terrain|ent_rigid|ent_sleeping_rigid,
				&pContact, 0, (geom_colltype_player<<rwi_colltype_bit) | rwi_stop_at_pierceable, 0, 0, 0, pSkipEntities, nSkip);

			float raiseDist = minHeightDiff - (goal.z - worldPos.z) - wallSafeDistance;
			if(newHitDist != 0)
			{
				raiseDist = MIN(minHeightDiff, newHitDist);
			}
			
			raiseDist = MAX(0.0f, raiseDist);
			
			goal.z += raiseDist;
			worldPos.z += raiseDist*0.8f;
		}
	}

	int thisFrameId = gEnv->pRenderer->GetFrameID();
	static int frameNo(thisFrameId);
	if(thisFrameId - frameNo > 5)
	{
		// reset positions
		viewOffset = goal - worldPos;
		entPos = worldPos;
		camPos = goal;
	}
	if(lastSpectatorTarget != m_in.stats_spectatorTarget)
	{
		viewOffset = goal - worldPos;
		entPos = worldPos;
		camPos = goal;
		lastSpectatorTarget = m_in.stats_spectatorTarget;
	}
	frameNo = thisFrameId;

	static float interpSpeed = 5.0f;
	static float interpSpeed2 = 5.0f;
	static float interpSpeed3 = 8.0f;

	if(pVehicle)
	{
		Interpolate(viewOffset, goal-worldPos, interpSpeed, viewParams.frameTime);
		entPos = worldPos;
		viewParams.position = worldPos + viewOffset;
		camPos = viewParams.position;
	}
	else
	{
		Vec3 camPosChange = goal - camPos;
		Vec3 entPosChange = worldPos - entPos;

		if(camPosChange.GetLengthSquared() > 100.0f)
			camPos = goal;
		if(entPosChange.GetLengthSquared() > 100.0f)
			entPos = worldPos;

		Interpolate(camPos, goal, interpSpeed2, viewParams.frameTime);
		Interpolate(entPos, worldPos, interpSpeed3, viewParams.frameTime);
		viewParams.position = camPos;
	}

	Matrix33 rotation = Matrix33::CreateRotationVDir((entPos - viewParams.position).GetNormalizedSafe());
	viewParams.rotation = GetQuatFromMat33(rotation);	
	m_io.bUsePivot = true;
	m_io.stats_bobCycle = 0.0;
}
void CPlayerView::ViewSpectatorTarget(SViewParams &viewParams)
{
	CActor *pTarget = (CActor *)g_pGame->GetIGameFramework()->GetIActorSystem()->GetActor(m_in.stats_spectatorTarget);

	if(!pTarget)
	{
		return;
	}

	Matrix34 worldTM = pTarget->GetEntity()->GetWorldTM();
	Vec3 worldPos = worldTM.GetTranslation();
	worldPos.z += 1.5f;
	Ang3 worldAngles = Ang3::GetAnglesXYZ(Matrix33(worldTM));
	float rot = worldAngles.z;// + m_rot;
	float distance = 3;//(m_defaultDistance != 0) ? m_defaultDistance : m_distance;

	if(IVehicle *pVehicle = pTarget->GetLinkedVehicle())
	{
		AABB vehicleBox;
		pVehicle->GetEntity()->GetLocalBounds(vehicleBox);
		distance = 2.0f * vehicleBox.GetRadius();
	}

	Vec3 goal;
	float zoom = 1.0f;
	goal.x = distance * zoom * cosf(rot + gf_PI * 1.5f) + worldPos.x;
	goal.y = distance * zoom * sinf(rot - gf_PI / 2.0f) + worldPos.y;
	AABB targetBounds;
	pTarget->GetEntity()->GetLocalBounds(targetBounds);
	goal.z = targetBounds.max.z;
	static float defaultOffset = 0.75f;
	float offset = defaultOffset;

	if(pTarget->GetLinkedVehicle())
	{
		offset = 2.0f;
	}

	goal.z += pTarget->GetEntity()->GetWorldPos().z + offset;
	// store / interpolate the offset, not the world pos (reduces percieved jitter in vehicles)
	static Vec3 viewOffset(goal - worldPos);
	static Vec3 position(goal);
	static Vec3 entPos(worldPos);
	static EntityId lastSpectatorTarget(m_in.stats_spectatorTarget);
	// do a ray cast to check for camera intersection
	static ray_hit hit;
	IPhysicalEntity *pSkipEntities[10];
	int nSkip = 0;
	IItem *pItem = pTarget->GetCurrentItem();

	if (pItem)
	{
		CWeapon *pWeapon = (CWeapon *)pItem->GetIWeapon();

		if (pWeapon)
		{
			nSkip = CSingle::GetSkipEntities(pWeapon, pSkipEntities, 10);
		}
	}
	else if(IVehicle *pVehicle = pTarget->GetLinkedVehicle())
	{
		// vehicle drivers don't seem to have current items, so need to add the vehicle itself here
		nSkip = pVehicle->GetSkipEntities(pSkipEntities, 10);
	}

	const float wallSafeDistance = 0.2f; // how far to keep camera from walls
	Vec3 dir = goal - worldPos;
	primitives::sphere sphere;
	sphere.center = worldPos;
	sphere.r = wallSafeDistance;
	geom_contact *pContact = 0;
	float hitDist = gEnv->pPhysicalWorld->PrimitiveWorldIntersection(sphere.type, &sphere, dir, ent_static | ent_terrain | ent_rigid | ent_sleeping_rigid,
					&pContact, 0, geom_colltype_player, 0, 0, 0, pSkipEntities, nSkip);
	// even when we have contact, keep the camera the same height above the target
	float minHeightDiff = dir.z;

	if(hitDist > 0 && pContact)
	{
		goal = worldPos + (hitDist * dir.GetNormalizedSafe());

		if(goal.z - worldPos.z < minHeightDiff)
		{
			// can't move the camera far enough away from the player in this direction. Try moving it directly up a bit
			sphere.center = goal;
			// (move back just slightly to avoid colliding with the wall we've already found...)
			sphere.center -= dir.GetNormalizedSafe() * 0.05f;
			float newHitDist = gEnv->pPhysicalWorld->PrimitiveWorldIntersection(sphere.type, &sphere, Vec3(0, 0, minHeightDiff), ent_static | ent_terrain | ent_rigid | ent_sleeping_rigid,
							   &pContact, 0, geom_colltype_player, 0, 0, 0, pSkipEntities, nSkip);
			float raiseDist = minHeightDiff - (goal.z - worldPos.z) - wallSafeDistance;

			if(newHitDist != 0)
			{
				raiseDist = MIN(minHeightDiff, newHitDist);
			}

			raiseDist = MAX(0.0f, raiseDist);
			goal.z += raiseDist;
			worldPos.z += raiseDist * 0.8f;
		}
	}

	int thisFrameId = gEnv->pRenderer->GetFrameID();
	static int frameNo(thisFrameId);

	if(thisFrameId - frameNo > 5)
	{
		// reset positions
		viewOffset = goal - worldPos;
		entPos = worldPos;
		position = goal;
	}

	if(lastSpectatorTarget != m_in.stats_spectatorTarget)
	{
		viewOffset = goal - worldPos;
		entPos = worldPos;
		position = goal;
		lastSpectatorTarget = m_in.stats_spectatorTarget;
	}

	frameNo = thisFrameId;

	if(pTarget->GetLinkedVehicle())
	{
		Interpolate(viewOffset, goal - worldPos, 5.0f, viewParams.frameTime);
		entPos = worldPos;
		viewParams.position = worldPos + viewOffset;
		position = viewParams.position;
	}
	else
	{
		Vec3 camPosChange = goal - position;
		Vec3 entPosChange = worldPos - entPos;

		if(camPosChange.GetLengthSquared() > 100.0f)
		{
			position = goal;
		}

		if(entPosChange.GetLengthSquared() > 100.0f)
		{
			entPos = worldPos;
		}

		Interpolate(position, goal, 5.0f, viewParams.frameTime);
		Interpolate(entPos, worldPos, 5.0f, viewParams.frameTime);
		viewParams.position = position;
	}

	Matrix33 rotation = Matrix33::CreateRotationVDir((entPos - viewParams.position).GetNormalizedSafe());
	viewParams.rotation = Quat(rotation);
	m_io.bUsePivot = true;
	m_io.stats_bobCycle = 0.0;
}
Example #8
0
void CHeavyWeapon::StartUse(EntityId userId)
{
    // holster user item here
    SetOwnerId(userId);

    CActor* pOwner = GetOwnerActor();
    if (!pOwner)
        return;

    HighlightWeapon(false);
    Physicalize(false, false);

    if(gEnv->bMultiplayer)
        {
            m_properties.usable &= strlen(g_pGameCVars->g_forceHeavyWeapon->GetString()) == 0;

            CHANGED_NETWORK_STATE(pOwner, CPlayer::ASPECT_CURRENT_ITEM);
        }

    if(IItem* pCurrentItem = pOwner->GetCurrentItem())
        {
            //Don't keep history if we're switching from Pick & Throw otherwsie we'll switch back to it when we're done with the heavy weapon
            static IEntityClass* pPickAndThrowWeaponClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass("PickAndThrowWeapon");
            m_pItemSystem->SetActorItem(pOwner, GetEntityId(), pCurrentItem->GetEntity()->GetClass() != pPickAndThrowWeaponClass);
        }
    else
        {
            m_pItemSystem->SetActorItem(pOwner, GetEntityId(), true);
        }

    TriggerRespawn();

    EnableUpdate(true, eIUS_General);
    RequireUpdate(eIUS_General);

    RegisterAsUser();

    HandleHeavyWeaponPro(*pOwner);

    m_stats.brandnew = false;
    m_stats.used = true;
    m_stats.detached = false;

    if(IsClient() && gEnv->pGame->GetIGameFramework()->GetClientActorId()==userId)
        {
            if(IEntity* pEntity = GetEntity())
                {
                    const char* collectibleId = pEntity->GetClass()->GetName();
                    CPersistantStats* pStats = g_pGame->GetPersistantStats();
                    if(pStats && pStats->GetStat(collectibleId, EMPS_SPWeaponByName) == 0)
                        {
                            pStats->SetMapStat(EMPS_SPWeaponByName, collectibleId, eDatabaseStatValueFlag_Available);

                            if(!gEnv->bMultiplayer)
                                {
                                    // Show hud unlock msg
                                    SHUDEventWrapper::DisplayWeaponUnlockMsg(collectibleId);
                                }
                        }
                }
        }

    if (IsServer())
        {
            pOwner->GetGameObject()->InvokeRMIWithDependentObject(CActor::ClStartUse(), CActor::ItemIdParam(GetEntityId()), eRMI_ToAllClients|eRMI_NoLocalCalls, GetEntityId());
            g_pGame->GetGameRules()->AbortEntityRemoval(GetEntityId());
        }

    OnStartUsing();
}