size_t CommBus::Send(int32_t entity_id, zmq::message_t &msg) { zmq::socket_t *sock; if (IsLocalEntity(entity_id)) { sock = thr_info_->inproc_sock_.get(); } else { sock = thr_info_->interproc_sock_.get(); } int32_t recv_id = ZMQUtil::EntityID2ZmqID(entity_id); size_t nbytes = ZMQUtil::ZMQSend(sock, recv_id, msg, 0); return nbytes; }
size_t CommBus::Send(int32_t entity_id, const void *data, size_t len) { zmq::socket_t *sock; if (IsLocalEntity(entity_id)) { sock = thr_info_->inproc_sock_.get(); } else { sock = thr_info_->interproc_sock_.get(); } int32_t recv_id = ZMQUtil::EntityID2ZmqID(entity_id); size_t nbytes = ZMQUtil::ZMQSend(sock, recv_id, data, len, 0); return nbytes; }
void CommBus::ConnectTo(int32_t entity_id, void *connect_msg, size_t size) { CHECK(IsLocalEntity(entity_id)) << "Not local entity " << entity_id; zmq::socket_t *sock = thr_info_->inproc_sock_.get(); if (sock == NULL) { try { thr_info_->inproc_sock_.reset(new zmq::socket_t(*zmq_ctx_, ZMQ_ROUTER)); } catch (...) { LOG(FATAL) << "Failed creating router socket"; } sock = thr_info_->inproc_sock_.get(); SetUpRouterSocket(sock, thr_info_->entity_id_, thr_info_->num_bytes_inproc_send_buff_, thr_info_->num_bytes_inproc_recv_buff_); } std::string connect_addr; MakeInProcAddr(entity_id, &connect_addr); int32_t zmq_id = ZMQUtil::EntityID2ZmqID(entity_id); ZMQUtil::ZMQConnectSend(sock, connect_addr, zmq_id, connect_msg, size); }
void CClientWeapon::DoPulse ( void ) { /*if ( m_bHasTargetDirection ) { CVector vecDirection = m_vecLastDirection; CVector vecOffset = m_vecTargetDirection - vecDirection; vecOffset /= CVector ( 10, 10, 10 ); vecDirection += vecOffset; vecDirection.Normalize (); m_vecLastDirection = vecDirection; SetDirection ( vecDirection ); }*/ if ( m_targetType != TARGET_TYPE_FIXED ) { CVector vecTargetPos; if ( m_targetType == TARGET_TYPE_ENTITY ) { if ( m_pTarget ) { if ( m_pTarget->GetType ( ) == CCLIENTPED || m_pTarget->GetType ( ) == CCLIENTPLAYER ) { CClientPed * pPed = (CClientPed *) (CClientEntity *)(m_pTarget); pPed->GetBonePosition( m_targetBone, vecTargetPos ); } else { m_pTarget->GetPosition( vecTargetPos ); } } else { ResetWeaponTarget ( ); } } else if ( m_targetType == TARGET_TYPE_VECTOR ) { vecTargetPos = m_vecTarget; } if ( !m_pAttachedToEntity ) { // Calculate direction to target CVector vecPosition; GetPosition ( vecPosition ); CVector vecDirection = vecTargetPos - vecPosition; // Convert direction to rotation CVector vecRotation = vecDirection.ToRotation (); SetRotationRadians ( vecRotation ); } else { // Transform target position into local (AttachedToEntity) space CMatrix attachedToMatrix; m_pAttachedToEntity->GetMatrix ( attachedToMatrix ); CVector vecLocalTargetPos = attachedToMatrix.Inverse ().TransformVector ( vecTargetPos ); // Calculate local direction CVector vecDirection = vecLocalTargetPos - m_vecAttachedPosition; // Convert local direction to local rotation CVector vecRotation = vecDirection.ToRotation (); // Apply local rotation SetAttachedOffsets ( m_vecAttachedPosition, vecRotation ); } } if ( m_nAmmoInClip <= 0 ) { if ( m_weaponConfig.bInstantReload == false ) { if ( m_nAmmoTotal >= m_pWeaponStat->GetMaximumClipAmmo() && m_State != WEAPONSTATE_RELOADING ) { m_PreviousState = m_State; m_State = WEAPONSTATE_RELOADING; m_reloadTimer.Reset(); } else if ( m_State == WEAPONSTATE_RELOADING && m_reloadTimer.Get() >= m_pWeapon->GetWeaponReloadTime ( m_pWeaponStat ) ) { m_State = m_PreviousState; m_nAmmoInClip = m_pWeaponStat->GetMaximumClipAmmo(); m_nAmmoTotal -= m_pWeaponStat->GetMaximumClipAmmo(); } else return; } else { if ( m_nAmmoTotal >= m_pWeaponStat->GetMaximumClipAmmo() && m_State != WEAPONSTATE_RELOADING ) { m_nAmmoInClip = m_pWeaponStat->GetMaximumClipAmmo(); m_nAmmoTotal -= m_pWeaponStat->GetMaximumClipAmmo(); } } } if ( m_nAmmoInClip > 0 && ( IsLocalEntity ( ) || m_pOwner == g_pClientGame->GetLocalPlayer ( ) ) ) { if ( m_State == WEAPONSTATE_FIRING && m_fireTimer.Get() >= m_iWeaponFireRate ) { Fire (); } } }
void CClientWeapon::FireInstantHit ( CVector vecOrigin, CVector vecTarget, bool bServerFire, bool bRemote ) #endif { CVector vecDirection = vecTarget - vecOrigin; vecDirection.Normalize (); CClientEntity * pAttachedTo = GetAttachedTo (); CVector vecOriginalTarget = vecTarget; CEntity * pColEntity = NULL; CColPoint * pColPoint = NULL; SLineOfSightBuildingResult pBuildingResult; CEntitySAInterface * pEntity = NULL; if ( m_Type != WEAPONTYPE_SHOTGUN ) { CVector vecWeaponFirePosition; if ( !IsLocalEntity ( ) && m_pOwner ) { CClientPlayer * pPlayer = m_pOwner; CClientPed * pLocalPlayer = g_pClientGame->GetLocalPlayer(); if ( pLocalPlayer && pPlayer ) { CClientVehicle* pVehicle = pLocalPlayer->GetRealOccupiedVehicle (); // Move both players to where they should be for shot compensation if ( pPlayer && !pPlayer->IsLocalPlayer () ) { if ( !pVehicle || pLocalPlayer->GetOccupiedVehicleSeat() == 0 ) { // Warp back in time to where we were when this player shot (their latency) // We don't account for interpolation here, +250ms seems to work better // ** Changed ajustment to +125ms as the position of this clients player on the firers screen // has been changed. See CClientPed::UpdateTargetPosition() ** CVector vecPosition; unsigned short usLatency = ( pPlayer->GetLatency () + 125 ); g_pClientGame->GetNetAPI()->GetInterpolation ( vecPosition, usLatency ); // Move the entity back if ( pVehicle ) { pVehicle->GetPosition ( vecWeaponFirePosition ); pVehicle->SetPosition ( vecPosition, false, false ); } else { pLocalPlayer->GetPosition ( vecWeaponFirePosition ); pLocalPlayer->SetPosition ( vecPosition, false, false ); } } } } } //if ( pAttachedTo ) pAttachedTo->WorldIgnore ( true ); if ( m_pWeapon->ProcessLineOfSight ( &vecOrigin, &vecTarget, &pColPoint, &pColEntity, m_weaponConfig.flags, &pBuildingResult, m_Type, &pEntity ) ) { vecTarget = pColPoint->GetPosition (); } // Don't continue without a valid colpoint if ( !pColPoint ) return; //if ( pAttachedTo ) pAttachedTo->WorldIgnore ( false ); // return if shoot if target is blocked is false and we aren't pointing at our target if ( ( m_pTarget != NULL && m_pTarget->GetGameEntity ( ) != NULL && m_pTarget->GetGameEntity()->GetInterface ( ) != pEntity ) && m_weaponConfig.bShootIfTargetBlocked == false && bRemote == false ) { if ( pColPoint ) pColPoint->Destroy (); return; } // Execute our weapon fire event CClientEntity * pClientEntity = m_pManager->FindEntitySafe ( pColEntity ); CLuaArguments Arguments; if ( pClientEntity ) Arguments.PushElement ( pClientEntity ); // entity that got hit else Arguments.PushNil ( ); // Probably a building. Arguments.PushNumber ( pColPoint->GetPosition ( ).fX ); // pos x Arguments.PushNumber ( pColPoint->GetPosition ( ).fY ); // pos y Arguments.PushNumber ( pColPoint->GetPosition ( ).fZ ); // pos z Arguments.PushNumber ( pColPoint->GetNormal ( ).fX ); // Normal x Arguments.PushNumber ( pColPoint->GetNormal ( ).fY ); // Normal y Arguments.PushNumber ( pColPoint->GetNormal ( ).fZ ); // Normal z Arguments.PushNumber ( pColPoint->GetSurfaceTypeB ( ) ); // Surface type "B" Arguments.PushNumber ( pColPoint->GetLightingForTimeOfDay ( ) ); // Lighting Arguments.PushNumber ( pColPoint->GetPieceTypeB ( ) ); // Piece if ( !CallEvent ( "onClientWeaponFire", Arguments, true ) ) { if ( pColPoint ) pColPoint->Destroy (); return; } DoGunShells ( vecOrigin, vecDirection ); CVector vecCollision; if ( g_pGame->GetWaterManager ()->TestLineAgainstWater ( vecOrigin, vecTarget, &vecCollision ) ) { g_pGame->GetFx ()->TriggerBulletSplash ( vecCollision ); g_pGame->GetAudioEngine ()->ReportBulletHit ( NULL, SURFACE_TYPE_WATER_SHALLOW, &vecCollision, 0.0f ); } #ifdef MARKER_DEBUG m_pMarker2->SetPosition ( vecTarget ); #endif m_pWeapon->DoBulletImpact ( m_pObject, pEntity, &vecOrigin, &vecTarget, pColPoint, 0 ); if ( !IsLocalEntity ( ) && m_pOwner ) { CClientPed * pPed = m_pOwner; CClientPed * pLocalPlayer = g_pClientGame->GetLocalPlayer(); if ( pPed->GetType () == CCLIENTPLAYER ) { // Restore compensated positions if ( !pPed->IsLocalPlayer () ) { CClientVehicle* pVehicle = pLocalPlayer->GetRealOccupiedVehicle (); if ( !pVehicle ) { pLocalPlayer->SetPosition ( vecWeaponFirePosition, false, false ); } else if ( pLocalPlayer->GetOccupiedVehicleSeat() == 0 ) { pVehicle->SetPosition ( vecWeaponFirePosition, false, false ); } } } } if ( !IsLocalEntity ( ) && GetOwner ( ) == g_pClientGame->GetLocalPlayer ( ) && bServerFire == false ) { g_pClientGame->GetNetAPI ( )->SendBulletSyncCustomWeaponFire ( this, vecOrigin, vecOriginalTarget ); } } #ifdef SHOTGUN_TEST else { //if ( pAttachedTo ) pAttachedTo->WorldIgnore ( true ); //if ( pAttachedTo ) pAttachedTo->WorldIgnore ( false ); // Fire instant hit is off by a few degrees FireShotgun ( m_pObject, vecOrigin, vecTarget, vecRotation ); } #endif if ( pColPoint ) pColPoint->Destroy (); }
void CClientWeapon::Fire ( bool bServerFire ) { if ( !m_pWeapon || !m_pObject || ( !IsLocalEntity ( ) && m_pOwner != g_pClientGame->GetLocalPlayer ( ) && bServerFire == false ) ) return; switch ( m_Type ) { case WEAPONTYPE_PISTOL: case WEAPONTYPE_PISTOL_SILENCED: case WEAPONTYPE_DESERT_EAGLE: #ifdef SHOTGUN_TEST case WEAPONTYPE_SHOTGUN: case WEAPONTYPE_SAWNOFF_SHOTGUN: case WEAPONTYPE_SPAS12_SHOTGUN: #endif case WEAPONTYPE_MICRO_UZI: case WEAPONTYPE_MP5: case WEAPONTYPE_AK47: case WEAPONTYPE_M4: case WEAPONTYPE_TEC9: case WEAPONTYPE_COUNTRYRIFLE: case WEAPONTYPE_SNIPERRIFLE: case WEAPONTYPE_MINIGUN: { CMatrix matOrigin; GetMatrix ( matOrigin ); CVector vecTarget; float fDistance = m_pWeaponInfo->GetWeaponRange (); if ( m_targetType == TARGET_TYPE_ENTITY ) { if ( m_pTarget ) { if ( m_pTarget->GetType ( ) == CCLIENTPED || m_pTarget->GetType ( ) == CCLIENTPLAYER ) { CClientPed * pPed = (CClientPed *) (CClientEntity *) m_pTarget; pPed->GetBonePosition( m_targetBone, vecTarget ); } else { if ( m_pTarget->GetType() == CCLIENTVEHICLE ) { if ( m_itargetWheel <= MAX_WHEELS ) { CClientVehicle * pTarget = (CClientVehicle*)(CClientEntity *)m_pTarget; vecTarget = pTarget->GetGameVehicle()->GetWheelPosition ( (eWheels)m_itargetWheel ); } else m_pTarget->GetPosition( vecTarget ); } else m_pTarget->GetPosition( vecTarget ); } if ( m_weaponConfig.bShootIfTargetOutOfRange == false && (matOrigin.GetPosition() - vecTarget).Length() >= fDistance ) { return; } } else { ResetWeaponTarget ( ); } } else if ( m_targetType == TARGET_TYPE_VECTOR ) { vecTarget = m_vecTarget; if ( m_weaponConfig.bShootIfTargetOutOfRange == false && (matOrigin.GetPosition() - vecTarget).Length() >= fDistance ) { return; } } else { #ifndef SHOTGUN_TEST CVector vecFireOffset = *m_pWeaponInfo->GetFireOffset (); matOrigin = CMatrix( vecFireOffset, m_vecFireRotationNoTarget ) * matOrigin; #endif CVector vecDirection ( 1, 0, 0 ); vecDirection *= fDistance; vecTarget = matOrigin.TransformVector( vecDirection ); } // Save short sDamage = m_pWeaponInfo->GetDamagePerHit ( ); float fAccuracy = m_pWeaponInfo->GetAccuracy ( ); float fTargetRange = m_pWeaponInfo->GetTargetRange ( ); float fRange = m_pWeaponInfo->GetWeaponRange ( ); // Set new m_pWeaponInfo->SetDamagePerHit ( m_pWeaponStat->GetDamagePerHit ( ) ); m_pWeaponInfo->SetAccuracy ( m_pWeaponStat->GetAccuracy ( ) ); m_pWeaponInfo->SetTargetRange ( m_pWeaponStat->GetTargetRange ( ) ); m_pWeaponInfo->SetWeaponRange ( m_pWeaponStat->GetWeaponRange ( ) ); #ifdef SHOTGUN_TEST CVector vecFireOffset = *m_pWeaponInfo->GetFireOffset (); CMatrix matTemp = CMatrix( vecFireOffset ) * matOrigin; #ifdef MARKER_DEBUG // Process m_pMarker->SetPosition ( matOrigin.GetPosition() ); #endif CVector vecTemp2; GetRotationDegrees(vecTemp2); vecTemp2.fZ -= 84.6f; SetRotationDegrees(vecTemp2); FireInstantHit ( matOrigin.GetPosition(), vecTarget-matOrigin.GetPosition(), matTemp.GetPosition() ); vecTemp2.fZ += 84.6f; SetRotationDegrees(vecTemp2); #else FireInstantHit ( matOrigin.GetPosition(), vecTarget, bServerFire ); #endif // Restore m_pWeaponInfo->SetDamagePerHit ( sDamage ); m_pWeaponInfo->SetAccuracy ( fAccuracy ); m_pWeaponInfo->SetTargetRange ( fTargetRange ); m_pWeaponInfo->SetWeaponRange ( fRange ); m_nAmmoInClip--; m_fireTimer.Reset(); break; } default: break; } }