bool CVisibleObjectsHelper::CheckObjectViewDist(const Agent& agent, const SVisibleObject &visibleObject) const { FUNCTION_PROFILER(GetISystem(), PROFILE_GAME); assert(agent.IsValid()); bool bInViewDist = true; IEntitySystem *pEntitySystem = gEnv->pEntitySystem; assert(pEntitySystem); IEntity *pAIEntity = pEntitySystem->GetEntity(agent.GetEntityID()); IEntity *pObjectEntity = pEntitySystem->GetEntity(visibleObject.entityId); IComponentRender *pObjectRenderProxy = (pAIEntity != NULL && pObjectEntity ? static_cast<IComponentRender *>(pObjectEntity->GetComponent<IComponentRender>().get()) : NULL); if (pObjectRenderProxy != NULL) { IRenderNode *pObjectRenderNode = pObjectRenderProxy->GetRenderNode(); if (pObjectRenderNode != NULL) { const float fDistanceSq = pAIEntity->GetWorldPos().GetSquaredDistance(pObjectEntity->GetWorldPos()); const float fMaxViewDistSq = sqr(pObjectRenderNode->GetMaxViewDist()); bInViewDist = (fDistanceSq <= fMaxViewDistSq); } } return bInViewDist; }
IEntity* CLaserBeam::CreateLaserEntity() { IEntity* pLaserEntity = gEnv->pEntitySystem->GetEntity(m_laserEntityId); if (pLaserEntity) return pLaserEntity; IEntity* pOwnerEntity = gEnv->pEntitySystem->GetEntity(m_ownerEntityId); if (!pOwnerEntity) return 0; SEntitySpawnParams spawnParams; spawnParams.pClass = gEnv->pEntitySystem->GetClassRegistry()->GetDefaultClass(); spawnParams.sName = "LaserBeam"; spawnParams.nFlags = (pOwnerEntity->GetFlags() | ENTITY_FLAG_NO_SAVE) & ~ENTITY_FLAG_CASTSHADOW; IEntity* pNewEntity = gEnv->pEntitySystem->SpawnEntity(spawnParams); if(pNewEntity) { m_laserEntityId = pNewEntity->GetId(); IEntityRenderProxy *pRenderProxy = (IEntityRenderProxy*)pNewEntity->GetProxy(ENTITY_PROXY_RENDER); IRenderNode * pRenderNode = pRenderProxy?pRenderProxy->GetRenderNode():NULL; if(pRenderNode) pRenderNode->SetRndFlags(ERF_RENDER_ALWAYS,true); SetLaserEntitySlots(false); } return pNewEntity; }
void CCameraFlight::DetectCollisions() { primitives::sphere sph; sph.r = 0.15f; sph.center = m_vTargetFadePos + m_vLookingDirection * RAY_SCAN_DISTANCE; CCameraRayScan *pRayScan = g_pGame->GetCameraManager()->GetCamView()->GetCamRayScan(); if (m_RayId == INVALID_RAY_ID) { m_RayId = pRayScan->ShootRay(sph.center, -m_vLookingDirection * RAY_SCAN_DISTANCE, ent_all & ~(ent_living | ent_independent | ent_rigid), geom_colltype0); } const RayCastResult* pRayRes = pRayScan->GetExternalHit(m_RayId); const ray_hit *pHit = pRayRes->hitCount > 0 ? &pRayRes->hits[0] : NULL; static int iNumHits = 0; if(pHit && pHit->dist > 0.0f) { iNumHits++; if(iNumHits > 2) { bool bIgnore = false; IPhysicalEntity *pPhysEntity = pHit->pCollider; if(pPhysEntity) { int iForeignData = pPhysEntity->GetiForeignData(); if (iForeignData == PHYS_FOREIGN_ID_STATIC) { //check whether the hit rendernode is "vegetation" void *pForeignData = pPhysEntity->GetForeignData(PHYS_FOREIGN_ID_STATIC); IRenderNode * pRN = (IRenderNode*)pForeignData; if(pRN && pRN->GetRenderNodeType() == eERType_Vegetation) bIgnore = true; } else if(iForeignData == PHYS_FOREIGN_ID_ENTITY) { IEntity *pEntity = gEnv->pEntitySystem->GetEntityFromPhysics(pPhysEntity); if(pEntity && (pEntity == m_pRefEnt || pEntity->GetId() == LOCAL_PLAYER_ENTITY_ID)) bIgnore = true; } else if(iForeignData == -1) bIgnore = true; } if(!bIgnore) m_vTargetFadePos = m_vTargetFadePos + m_vLookingDirection * max(0.0f, (RAY_SCAN_DISTANCE - pHit->dist)); else iNumHits = 0; } } else iNumHits = 0; if (pRayRes) { pRayScan->RemoveExternalHit(m_RayId); m_RayId = INVALID_RAY_ID; } }
//------------------------------------------------------------------------ void CItem::ResetRenderFlags() { if (!GetRenderProxy()) return; IRenderNode *pRenderNode = GetRenderProxy()->GetRenderNode(); if (pRenderNode) { pRenderNode->SetViewDistRatio(127); pRenderNode->SetLodRatio(127); GetEntity()->SetFlags(GetEntity()->GetFlags()|ENTITY_FLAG_CASTSHADOW); } }
//------------------------------------------------------------------------ void CProjectile::HandleEvent(const SGameObjectEvent &event) { if (m_destroying) return; FUNCTION_PROFILER(GetISystem(), PROFILE_GAME); if (event.event == eGFE_OnCollision) { EventPhysCollision *pCollision = (EventPhysCollision *)event.ptr; IEntity *pCollidee = pCollision->iForeignData[0]==PHYS_FOREIGN_ID_ENTITY ? (IEntity*)pCollision->pForeignData[0]:0; const SCollisionParams* pCollisionParams = m_pAmmoParams->pCollision; if (pCollisionParams) { if (pCollisionParams->pParticleEffect) pCollisionParams->pParticleEffect->Spawn(true, IParticleEffect::ParticleLoc(pCollision->pt, pCollision->n, pCollisionParams->scale)); if (pCollisionParams->sound) { _smart_ptr<ISound> pSound = gEnv->pSoundSystem->CreateSound(pCollisionParams->sound, FLAG_SOUND_DEFAULT_3D); pSound->SetSemantic(eSoundSemantic_Projectile); pSound->SetPosition(pCollision->pt); pSound->Play(); } IStatObj *statObj = 0; if (pCollision->iForeignData[1] == PHYS_FOREIGN_ID_STATIC) { IRenderNode * pRN = (IRenderNode*)pCollision->pForeignData[1]; if (pRN && pRN->GetEntityStatObj(0)) statObj = pRN->GetEntityStatObj(0); } } // add battledust for bulletimpact if(gEnv->bServer && g_pGame->GetGameRules()) { if(CBattleDust* pBD = g_pGame->GetGameRules()->GetBattleDust()) { pBD->RecordEvent(eBDET_ShotImpact, pCollision->pt, GetEntity()->GetClass()); } } Ricochet(pCollision); } }
void CUIEntityDynTexTag::OnAddTaggedEntity(EntityId entityId, const char *uiElementName, const char *entityClass, const char *materialTemplate, const Vec3 &offset, const char *idx) { OnRemoveTaggedEntity(entityId, idx); IEntityClass *pEntClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass(entityClass); if(pEntClass) { SEntitySpawnParams params; params.nFlags = ENTITY_FLAG_CLIENT_ONLY; params.pClass = pEntClass; IEntity *pTagEntity = gEnv->pEntitySystem->SpawnEntity(params); IUIElement *pElement = gEnv->pFlashUI->GetUIElement(uiElementName); if(pTagEntity && pElement) { IMaterial *pMatTemplate = gEnv->p3DEngine->GetMaterialManager()->LoadMaterial(materialTemplate, false); if(pMatTemplate && pMatTemplate->GetShaderItem().m_pShaderResources->GetTexture(0)) { pMatTemplate->GetShaderItem().m_pShaderResources->GetTexture(0)->m_Name.Format("%s@%d.ui", uiElementName, entityId); IMaterial *pMat = gEnv->p3DEngine->GetMaterialManager()->CloneMaterial(pMatTemplate); pTagEntity->SetMaterial(pMat); } IEntityRenderProxy *pRenderProxy = (IEntityRenderProxy *) pTagEntity->GetProxy(ENTITY_PROXY_RENDER); if(pRenderProxy) { IRenderNode *pRenderNode = pRenderProxy->GetRenderNode(); if(pRenderNode) { pRenderNode->SetViewDistRatio(256); } } pElement->RemoveEventListener(this); // first remove to avoid assert if already registered! pElement->AddEventListener(this, "CUIEntityDynTexTag"); gEnv->pEntitySystem->AddEntityEventListener(entityId, ENTITY_EVENT_DONE, this); m_Tags.push_back(STagInfo(entityId, pTagEntity->GetId(), idx, offset, pElement->GetInstance((uint)entityId))); } } }
//--------------------------------------------------------------------------- void CLam::CreateLaserEntity() { if(m_pLaserEntityId) { //Check if entity is valid IEntity *pEntity = m_pEntitySystem->GetEntity(m_pLaserEntityId); if(!pEntity) m_pLaserEntityId = 0; } if (!m_pLaserEntityId) { SEntitySpawnParams spawnParams; spawnParams.pClass = gEnv->pEntitySystem->GetClassRegistry()->GetDefaultClass(); spawnParams.sName = "LAMLaser"; spawnParams.nFlags = (GetEntity()->GetFlags() | ENTITY_FLAG_NO_SAVE) & ~ENTITY_FLAG_CASTSHADOW; IEntity *pNewEntity =gEnv->pEntitySystem->SpawnEntity(spawnParams); //assert(pNewEntity && "Laser entity could no be spawned!!"); if(pNewEntity) { pNewEntity->FreeSlot(0); pNewEntity->FreeSlot(1); m_pLaserEntityId = pNewEntity->GetId(); if(IEntity* pEntity = GetEntity()) pEntity->AttachChild(pNewEntity); IEntityRenderProxy *pRenderProxy = (IEntityRenderProxy*)pNewEntity->GetProxy(ENTITY_PROXY_RENDER); IRenderNode * pRenderNode = pRenderProxy?pRenderProxy->GetRenderNode():NULL; if(pRenderNode) pRenderNode->SetRndFlags(ERF_RENDER_ALWAYS,true); } } }
//------------------------------------------------------------------------ void CItem::CopyRenderFlags(IEntity *pOwner) { if (!pOwner || !GetRenderProxy()) return; IRenderNode *pRenderNode = GetRenderProxy()->GetRenderNode(); if (pRenderNode) { IEntityRenderProxy *pOwnerRenderProxy = (IEntityRenderProxy *)pOwner->GetProxy(ENTITY_PROXY_RENDER); IRenderNode *pOwnerRenderNode = pOwnerRenderProxy?pOwnerRenderProxy->GetRenderNode():NULL; if (pOwnerRenderNode) { pRenderNode->SetViewDistRatio(pOwnerRenderNode->GetViewDistRatio()); pRenderNode->SetLodRatio(pOwnerRenderNode->GetLodRatio()); uint32 flags = pOwner->GetFlags()&(ENTITY_FLAG_CASTSHADOW); uint32 mflags = GetEntity()->GetFlags()&(~(ENTITY_FLAG_CASTSHADOW)); GetEntity()->SetFlags(mflags|flags); } } }
//-------------------------------------------------------------------------------------------------- // Name: ExtractPhysDataFromEvent // Desc: Extracts collider's physical data from an event // Note 1: Ideally *ALL* of this should be calculated offline and the minimal data loaded // Note 2: We're currently duplicating some work done in CryAction, so should be reading that in //-------------------------------------------------------------------------------------------------- bool CBreakableGlassSystem::ExtractPhysDataFromEvent(const EventPhysCollision& physEvent, SBreakableGlassPhysData& data, SBreakableGlassInitParams& initParams) { if (IPhysicalEntity* pPhysEntity = physEvent.pEntity[PHYSEVENT_COLLIDEE]) { // Get collider entity data const int entType = pPhysEntity->GetiForeignData(); const int entPart = physEvent.partid[PHYSEVENT_COLLIDEE]; // Local output data IStatObj* pStatObj = NULL; IMaterial* pRenderMat = NULL; phys_geometry* pPhysGeom = NULL; uint renderFlags = 0; Matrix34A entityMat; entityMat.SetIdentity(); // Only handling simple objects at the moment const pe_type physType = pPhysEntity->GetType(); if (physType == PE_STATIC || physType == PE_RIGID) { // Entity or static object? if (entType == PHYS_FOREIGN_ID_ENTITY) { IEntity* pEntity = (IEntity*)pPhysEntity->GetForeignData(PHYS_FOREIGN_ID_ENTITY); pStatObj = pEntity->GetStatObj(entPart); entityMat = pEntity->GetSlotWorldTM(entPart); if (IEntityRenderProxy* pRenderProxy = (IEntityRenderProxy*)pEntity->GetProxy(ENTITY_PROXY_RENDER)) { pRenderMat = pRenderProxy->GetRenderMaterial(entPart); IRenderNode* pRenderNode = pRenderProxy->GetRenderNode(); renderFlags = pRenderNode ? pRenderNode->GetRndFlags() : 0; // Fall back to top level material if sub-object fails to find it if (!pRenderMat) { pRenderMat = pRenderProxy->GetRenderMaterial(); if (!pRenderMat && pStatObj) { pRenderMat = pStatObj->GetMaterial(); } } } } else if (entType == PHYS_FOREIGN_ID_STATIC) { if (IRenderNode* pBrush = (IRenderNode*)physEvent.pForeignData[PHYSEVENT_COLLIDEE]) { pStatObj = pBrush->GetEntityStatObj(0, 0, &entityMat); pRenderMat = pBrush->GetMaterial(); renderFlags = pBrush->GetRndFlags(); // May need to get sub-object and it's material if (pStatObj && pStatObj->GetFlags() & STATIC_OBJECT_COMPOUND) { if (IStatObj::SSubObject* pSubObj = pStatObj->GetSubObject(entPart)) { pStatObj = pSubObj->pStatObj; if (!pSubObj->bIdentityMatrix) { entityMat = entityMat * pSubObj->tm; } // Find the correct sub-material // Note: We loop as the slots don't always line up const int subMtlCount = pRenderMat->GetSubMtlCount(); for (int i = 0; i < subMtlCount; ++i) { if (IMaterial* pSubMat = pRenderMat->GetSubMtl(i)) { if (pSubMat->GetSurfaceTypeId() == initParams.surfaceTypeId) { pRenderMat = pSubMat; break; } } } } } } } } // Validate geometry of collided object pPhysGeom = pStatObj ? pStatObj->GetPhysGeom() : NULL; IGeometry* pGeom = pPhysGeom ? pPhysGeom->pGeom : NULL; bool validGeom = false; primitives::box bbox; int thinAxis; if (pGeom) { // Determine thin geometry axis for glass alignment pGeom->GetBBox(&bbox); thinAxis = idxmin3((float*)&bbox.size); // Handle geometry mesh type switch (pGeom->GetType()) { case GEOM_TRIMESH: // Perform full mesh analysis and extraction if (mesh_data* pPhysMeshData = (mesh_data*)pGeom->GetData()) { if (ValidatePhysMesh(pPhysMeshData, thinAxis) && ExtractPhysMesh(pPhysMeshData, thinAxis, bbox, data.defaultFrag)) { validGeom = true; } } break; case GEOM_BOX: // Simple box, so assume valid validGeom = true; break; default: // Only support boxes and tri-meshes break; } } // Invalid geometry, so can't continue if (!validGeom) { pPhysGeom = NULL; } // Attempt UV coord extraction from render mesh else { ExtractUVCoords(pStatObj, bbox, thinAxis, data); } // Copy final data data.pStatObj = pStatObj; data.pPhysGeom = pPhysGeom; data.renderFlags = renderFlags; data.entityMat = entityMat; initParams.pGlassMaterial = pRenderMat; } return data.pStatObj && data.pPhysGeom && initParams.pGlassMaterial; }//-------------------------------------------------------------------------------------------------
virtual void ProcessEvent( EFlowEvent event, SActivationInfo *pActInfo ) { switch (event) { case eFE_Initialize: break; case eFE_Activate: IGameFramework* pGameFramework = gEnv->pGame->GetIGameFramework(); if(IsPortActive(pActInfo, EIP_Cast)) { // setup ray + optionally skip 1 entity ray_hit rayHit; static const float maxRayDist = 100.f; const unsigned int flags = rwi_stop_at_pierceable|rwi_colltype_any; IPhysicalEntity *skipList[1]; int skipCount = 0; IEntity* skipEntity = gEnv->pEntitySystem->GetEntity(GetPortEntityId(pActInfo, EIP_SkipEntity)); if(skipEntity) { skipList[0] = skipEntity->GetPhysics(); skipCount = 1; } Vec3 rayPos = GetPortVec3(pActInfo, EIP_RayPos); Vec3 rayDir = GetPortVec3(pActInfo, EIP_RayDir); // Check if the ray hits an entity if(gEnv->pSystem->GetIPhysicalWorld()->RayWorldIntersection(rayPos, rayDir * 100, ent_all, flags, &rayHit, 1, skipList, skipCount)) { int type = rayHit.pCollider->GetiForeignData(); if (type == PHYS_FOREIGN_ID_ENTITY) { IEntity* pEntity = (IEntity*)rayHit.pCollider->GetForeignData(PHYS_FOREIGN_ID_ENTITY); IEntityRenderProxy* pRenderProxy = pEntity ? (IEntityRenderProxy*)pEntity->GetProxy(ENTITY_PROXY_RENDER) : 0; // Get the renderproxy, and use it to check if the material is a DynTex, and get the UIElement if so if(pRenderProxy) { IRenderNode *pRenderNode = pRenderProxy->GetRenderNode(); IMaterial* pMaterial = pRenderProxy->GetRenderMaterial(); SEfResTexture* texture = 0; if(pMaterial && pMaterial->GetShaderItem().m_pShaderResources) texture= pMaterial->GetShaderItem().m_pShaderResources->GetTexture(EFTT_DIFFUSE); IUIElement* pElement = texture ? gEnv->pFlashUI->GetUIElementByInstanceStr(texture->m_Name) : 0; if(pElement && pRenderNode) { int m_dynTexGeomSlot = 0; IStatObj* pObj = pRenderNode->GetEntityStatObj(m_dynTexGeomSlot); // result bool hasHit = false; Vec2 uv0, uv1, uv2; Vec3 p0, p1, p2; Vec3 hitpos; // calculate ray dir CCamera cam = gEnv->pRenderer->GetCamera(); if (pEntity->GetSlotFlags(m_dynTexGeomSlot) & ENTITY_SLOT_RENDER_NEAREST) { ICVar *r_drawnearfov = gEnv->pConsole->GetCVar("r_DrawNearFoV"); assert(r_drawnearfov); cam.SetFrustum(cam.GetViewSurfaceX(),cam.GetViewSurfaceZ(),DEG2RAD(r_drawnearfov->GetFVal()),cam.GetNearPlane(),cam.GetFarPlane(), cam.GetPixelAspectRatio()); } Vec3 vPos0 = rayPos; Vec3 vPos1 = rayPos + rayDir; // translate into object space const Matrix34 m = pEntity->GetWorldTM().GetInverted(); vPos0 = m * vPos0; vPos1 = m * vPos1; // walk through all sub objects const int objCount = pObj->GetSubObjectCount(); for (int obj = 0; obj <= objCount && !hasHit; ++obj) { Vec3 vP0, vP1; IStatObj* pSubObj = NULL; if (obj == objCount) { vP0 = vPos0; vP1 = vPos1; pSubObj = pObj; } else { IStatObj::SSubObject* pSub = pObj->GetSubObject(obj); const Matrix34 mm = pSub->tm.GetInverted(); vP0 = mm * vPos0; vP1 = mm * vPos1; pSubObj = pSub->pStatObj; } IRenderMesh* pMesh = pSubObj ? pSubObj->GetRenderMesh() : NULL; if (pMesh) { const Ray ray(vP0, (vP1-vP0).GetNormalized() * maxRayDist); hasHit = RayIntersectMesh(pMesh, pMaterial, pElement, ray, hitpos, p0, p1, p2, uv0, uv1, uv2); } } // skip if not hit if (!hasHit) { ActivateOutput(pActInfo, EOP_Failed, 1); return; } // calculate vectors from hitpos to vertices p0, p1 and p2: const Vec3 v0 = p0-hitpos; const Vec3 v1 = p1-hitpos; const Vec3 v2 = p2-hitpos; // calculate factors const float h = (p0-p1).Cross(p0-p2).GetLength(); const float f0 = v1.Cross(v2).GetLength() / h; const float f1 = v2.Cross(v0).GetLength() / h; const float f2 = v0.Cross(v1).GetLength() / h; // find the uv corresponding to hitpos Vec3 uv = uv0 * f0 + uv1 * f1 + uv2 * f2; // translate to flash space int x, y, width, height; float aspect; pElement->GetFlashPlayer()->GetViewport(x, y, width, height, aspect); int iX = int_round(uv.x * (float)width); int iY = int_round(uv.y * (float)height); // call the function provided if it is present in the UIElement description string funcName = GetPortString(pActInfo, EIP_CallFunction); const SUIEventDesc* eventDesc = pElement->GetFunctionDesc(funcName); if(eventDesc) { SUIArguments arg; arg.AddArgument(iX); arg.AddArgument(iY); pElement->CallFunction(eventDesc->sName, arg); } ActivateOutput(pActInfo, EOP_Success, 1); } } } } ActivateOutput(pActInfo, EOP_Failed, 1); } break; } }