void CRegisteredCommands::CallCommandHandler(CLuaMain* pLuaMain, const CLuaFunctionRef& iLuaFunction, const char* szKey, const char* szArguments, CClient* pClient) { assert(pLuaMain); assert(szKey); CLuaArguments Arguments; // First, try to call a handler with the same number of arguments if (pClient) { switch (pClient->GetClientType()) { case CClient::CLIENT_PLAYER: { Arguments.PushElement(static_cast<CPlayer*>(pClient)); break; } case CClient::CLIENT_CONSOLE: { Arguments.PushElement(static_cast<CConsoleClient*>(pClient)); break; } default: { Arguments.PushBoolean(false); break; } } } else Arguments.PushBoolean(false); Arguments.PushString(szKey); if (szArguments) { // Create a copy and strtok modifies the string char* szTempArguments = new char[strlen(szArguments) + 1]; strcpy(szTempArguments, szArguments); char* arg; arg = strtok(szTempArguments, " "); while (arg) { Arguments.PushString(arg); arg = strtok(NULL, " "); } delete[] szTempArguments; } // Call the handler with the arguments we pushed Arguments.Call(pLuaMain, iLuaFunction); }
// // Handle the changing state of collision between one colshape and one entity // void CClientColManager::HandleHitDetectionResult ( bool bHit, CClientColShape* pShape, CClientEntity* pEntity ) { if ( bHit ) { // If they havn't collided yet if ( !pEntity->CollisionExists ( pShape ) ) { // Add the collision and the collider pShape->AddCollider ( pEntity ); pEntity->AddCollision ( pShape ); // Can we call the event? if ( pShape->GetAutoCallEvent () ) { // Call the event CLuaArguments Arguments; Arguments.PushElement ( pEntity ); Arguments.PushBoolean ( ( pShape->GetDimension () == pEntity->GetDimension () ) ); pShape->CallEvent ( "onClientColShapeHit", Arguments, true ); CLuaArguments Arguments2; Arguments2.PushElement ( pShape ); Arguments2.PushBoolean ( ( pShape->GetDimension () == pEntity->GetDimension () ) ); pEntity->CallEvent ( "onClientElementColShapeHit", Arguments2, true ); } // Run whatever callback the collision item might have attached pShape->CallHitCallback ( *pEntity ); } } else { // If they collided before if ( pEntity->CollisionExists ( pShape ) ) { // Remove the collision and the collider pShape->RemoveCollider ( pEntity ); pEntity->RemoveCollision ( pShape ); // Call the event CLuaArguments Arguments; Arguments.PushElement ( pEntity ); Arguments.PushBoolean ( ( pShape->GetDimension () == pEntity->GetDimension () ) ); pShape->CallEvent ( "onClientColShapeLeave", Arguments, true ); CLuaArguments Arguments2; Arguments2.PushElement ( pShape ); Arguments2.PushBoolean ( ( pShape->GetDimension () == pEntity->GetDimension () ) ); pEntity->CallEvent ( "onClientElementColShapeLeave", Arguments2, true ); pShape->CallLeaveCallback ( *pEntity ); } } }
void CPickup::Callback_OnLeave ( CColShape& Shape, CElement& Element ) { if ( IS_PLAYER ( &Element ) ) { CPlayer& Player = static_cast < CPlayer& > ( Element ); // Matching interior if ( GetInterior () == Element.GetInterior () ) { // Matching dimension if ( GetDimension () == Element.GetDimension () ) { // Is he alive? if ( !Player.IsDead () ) { // Call the onPickupHit event CLuaArguments Arguments; Arguments.PushElement ( &Player ); CallEvent ( "onPickupLeave", Arguments ); CLuaArguments Arguments2; Arguments2.PushElement ( this ); // pickup Element.CallEvent ( "onPlayerPickupLeave", Arguments2 ); } } } } }
void CClientMarker::Callback_OnLeave ( CClientColShape& Shape, CClientEntity& Entity ) { if ( IS_PLAYER ( &Entity ) ) { // Call the marker hit event CLuaArguments Arguments; Arguments.PushElement ( &Entity ); // player that hit it Arguments.PushBoolean ( ( Shape.GetDimension () == Entity.GetDimension () ) ); // matching dimension? CallEvent ( "onClientMarkerLeave", Arguments, true ); } }
bool CClientGUIElement::_CallbackEvent2 ( CGUIElement* pCGUIElement ) { CLuaArguments Arg; if ( pCGUIElement ) { CClientGUIElement* pElement = m_pGUIManager->Get ( pCGUIElement ); if ( pElement ) Arg.PushElement ( pElement ); } CallEvent ( _szCallbackFunc2, Arg, true ); return true; }
void CKeyBinds::Call ( CKeyBind* pKeyBind ) { if ( pKeyBind && !pKeyBind->IsBeingDeleted () ) { switch ( pKeyBind->GetType () ) { case KEY_BIND_FUNCTION: { CKeyFunctionBind* pBind = static_cast < CKeyFunctionBind* > ( pKeyBind ); if ( pBind->luaMain && VERIFY_FUNCTION ( pBind->m_iLuaFunction ) ) { CLuaArguments Arguments; Arguments.PushElement ( m_pPlayer ); Arguments.PushString ( pBind->boundKey->szKey ); Arguments.PushString ( ( pBind->bHitState ) ? "down" : "up" ); Arguments.PushArguments ( pBind->m_Arguments ); Arguments.Call ( pBind->luaMain, pBind->m_iLuaFunction ); } break; } case KEY_BIND_CONTROL_FUNCTION: { CControlFunctionBind* pBind = static_cast < CControlFunctionBind* > ( pKeyBind ); if ( pBind->luaMain && VERIFY_FUNCTION ( pBind->m_iLuaFunction ) ) { CLuaArguments Arguments; Arguments.PushElement ( m_pPlayer ); Arguments.PushString ( pBind->boundControl->szControl ); Arguments.PushString ( ( pBind->bHitState ) ? "down" : "up" ); Arguments.PushArguments ( pBind->m_Arguments ); Arguments.Call ( pBind->luaMain, pBind->m_iLuaFunction ); } break; } default: break; } } }
void CObjectSync::StopSync ( CObject* pObject ) { // Tell the player that used to sync it CPlayer* pSyncer = pObject->GetSyncer (); pSyncer->Send ( CObjectStopSyncPacket ( pObject ) ); // Unmark him as the syncing player pObject->SetSyncer ( NULL ); // Call the onElementStopSync event CLuaArguments Arguments; Arguments.PushElement ( pSyncer ); // Old syncer pObject->CallEvent ( "onElementStopSync", Arguments ); }
bool CClientGUIElement::_CallbackEvent1 ( CGUIElement* pCGUIElement ) { CLuaArguments Arg; if ( pCGUIElement ) { CClientGUIElement* pElement = m_pGUIManager->Get ( pCGUIElement ); if ( pElement ) { Arg.PushElement ( pElement ); pElement->CallEvent ( _strCallbackFunc1, Arg, true ); return true; } } return false; }
void CObjectSync::StartSync ( CPlayer* pPlayer, CObject* pObject ) { if ( !pObject->IsSyncable () ) return; // Tell the player pPlayer->Send ( CObjectStartSyncPacket ( pObject ) ); // Mark him as the syncing player pObject->SetSyncer ( pPlayer ); // Call the onElementStartSync event CLuaArguments Arguments; Arguments.PushElement ( pPlayer ); // New syncer pObject->CallEvent ( "onElementStartSync", Arguments ); }
void CClientPickup::Callback_OnLeave ( CClientColShape& Shape, CClientEntity& Entity ) { if ( IS_PLAYER ( &Entity ) ) { bool bMatchingDimensions = (GetDimension () == Entity.GetDimension ()); // Matching dimensions? // Call the pickup leave event (source = the pickup that was left) CLuaArguments Arguments; Arguments.PushElement ( &Entity ); // The element that left the pickup Arguments.PushBoolean ( bMatchingDimensions ); CallEvent ( "onClientPickupLeave", Arguments, true); // Call the player pickup leave event (source = the player that left the pickup) CLuaArguments Arguments2; Arguments2.PushElement ( this ); // The pickup that was left (this) Arguments2.PushBoolean ( bMatchingDimensions ); Entity.CallEvent ( "onClientPlayerPickupLeave", Arguments2, true ); } }
void CRPCFunctions::PlayerTarget ( NetBitStreamInterface & bitStream ) { if ( m_pSourcePlayer->IsJoined () ) { ElementID TargetID; bitStream.ReadCompressed ( TargetID ); CElement* pTarget = NULL; if ( TargetID != INVALID_ELEMENT_ID ) pTarget = CElementIDs::GetElement ( TargetID ); m_pSourcePlayer->SetTargetedElement ( pTarget ); // Call our script event CLuaArguments Arguments; if ( pTarget ) Arguments.PushElement ( pTarget ); else Arguments.PushBoolean ( false ); m_pSourcePlayer->CallEvent ( "onPlayerTarget", Arguments ); } }
void CPickup::Callback_OnCollision ( CColShape& Shape, CElement& Element ) { if ( IS_PLAYER ( &Element ) ) { CPlayer& Player = static_cast < CPlayer& > ( Element ); // Is he alive? if ( !Player.IsDead () ) { // Matching interior if ( GetInterior () == Element.GetInterior () ) { // Matching dimension if ( GetDimension () == Element.GetDimension () ) { // Call the onPickupHit event CLuaArguments Arguments; Arguments.PushElement ( &Player ); bool bContinue1 = CallEvent ( "onPickupHit", Arguments ); CLuaArguments Arguments2; Arguments2.PushElement ( this ); // pickup bool bContinue2 = Element.CallEvent ( "onPlayerPickupHit", Arguments2 ); if ( bContinue1 && bContinue2 ) { // Does it still exist? if ( !IsBeingDeleted () ) { // Can we USE the pickup? if ( CanUse ( Player ) ) { // USE the pickup Use ( Player ); } } } } } } } }
void CRPCFunctions::RequestStealthKill ( NetBitStreamInterface & bitStream ) { ElementID ID; bitStream.ReadCompressed ( ID ); CElement * pElement = CElementIDs::GetElement ( ID ); if ( pElement ) { int elementType = pElement->GetType (); if ( elementType == CElement::PLAYER || elementType == CElement::PED ) { CPed * pTarget = static_cast < CPed * > ( pElement ); // Are they both alive? if ( !m_pSourcePlayer->IsDead () && !pTarget->IsDead () ) { //Do we have any record of the killer currently having a knife? if ( m_pSourcePlayer->GetWeaponType( 1 ) == 4 ) { // Are they close enough? if ( DistanceBetweenPoints3D ( m_pSourcePlayer->GetPosition (), pTarget->GetPosition () ) <= STEALTH_KILL_RANGE ) { CLuaArguments Arguments; Arguments.PushElement ( pTarget ); if ( m_pSourcePlayer->CallEvent ( "onPlayerStealthKill", Arguments, false ) ) { // Start the stealth kill CStaticFunctionDefinitions::KillPed ( pTarget, m_pSourcePlayer, 4 /*WEAPONTYPE_KNIFE*/, 9/*BODYPART_HEAD*/, true ); } } } else { //You shouldn't be able to get here without cheating to get a knife. if ( !g_pGame->GetConfig ()->IsDisableAC ( "2" ) ) { CStaticFunctionDefinitions::KickPlayer ( m_pSourcePlayer, NULL, "AC #2: You were kicked from the game" ); } } } } } }
void CMarker::Callback_OnLeave ( CColShape& Shape, CElement& Element ) { // Matching interior? if ( GetInterior () == Element.GetInterior () ) { // Call the marker hit event CLuaArguments Arguments; Arguments.PushElement ( &Element ); // Hit element Arguments.PushBoolean ( GetDimension () == Element.GetDimension () ); // Matching dimension? CallEvent ( "onMarkerLeave", Arguments ); if ( IS_PLAYER ( &Element ) ) { CLuaArguments Arguments2; Arguments2.PushElement ( this ); // marker Arguments2.PushBoolean ( GetDimension () == Element.GetDimension () ); // Matching dimension? Element.CallEvent ( "onPlayerMarkerLeave", Arguments2 ); } } }
void CClientWeapon::FireInstantHit ( CVector & vecOrigin, CVector & vecTarget ) #endif { CVector vecDirection = vecTarget - vecOrigin; vecDirection.Normalize (); CClientEntity * pAttachedTo = GetAttachedTo (); CEntity * pColEntity = NULL; CColPoint * pColPoint = NULL; SLineOfSightBuildingResult pBuildingResult; CEntitySAInterface * pEntity = NULL; if ( m_Type != WEAPONTYPE_SHOTGUN ) { //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 ) { 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 ) ) { 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 ( pColEntity && pColEntity->GetEntityType () == ENTITY_TYPE_PED ) { ePedPieceTypes hitZone = ( ePedPieceTypes ) pColPoint->GetPieceTypeB (); short sDamage = m_pWeaponInfo->GetDamagePerHit (); m_pWeapon->GenerateDamageEvent ( dynamic_cast < CPed * > ( pColEntity ), m_pObject, m_Type, sDamage, hitZone, 0 ); } } #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::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 (); }
bool CVehiclePuresyncPacket::Read ( NetBitStreamInterface& BitStream ) { // Got a player to read? if ( m_pSourceElement ) { CPlayer * pSourcePlayer = static_cast < CPlayer * > ( m_pSourceElement ); // Player is in a vehicle? CVehicle* pVehicle = pSourcePlayer->GetOccupiedVehicle (); if ( pVehicle ) { // Read out the time context unsigned char ucTimeContext = 0; if ( !BitStream.Read ( ucTimeContext ) ) return false; // Only read this packet if it matches the current time context that // player is in. if ( !pSourcePlayer->CanUpdateSync ( ucTimeContext ) ) { return false; } // Read out the keysync data CControllerState ControllerState; if ( !ReadFullKeysync ( ControllerState, BitStream ) ) return false; // Read out its position SPositionSync position ( false ); if ( !BitStream.Read ( &position ) ) return false; pSourcePlayer->SetPosition ( position.data.vecPosition ); // Jax: don't allow any outdated packets through unsigned char ucSeat; if ( !BitStream.Read ( ucSeat ) ) return false; if ( ucSeat != pSourcePlayer->GetOccupiedVehicleSeat () ) { // Mis-matching seats can happen when we warp into a different one, // which will screw up the whole packet return false; } // Read out the vehicle matrix only if he's the driver unsigned int uiSeat = pSourcePlayer->GetOccupiedVehicleSeat (); if ( uiSeat == 0 ) { // Read out the vehicle rotation in degrees SRotationDegreesSync rotation; if( !BitStream.Read ( &rotation ) ) return false; // Set it pVehicle->SetPosition ( position.data.vecPosition ); pVehicle->SetRotationDegrees ( rotation.data.vecRotation ); // Move speed vector SVelocitySync velocity; if ( !BitStream.Read ( &velocity ) ) return false; pVehicle->SetVelocity ( velocity.data.vecVelocity ); pSourcePlayer->SetVelocity ( velocity.data.vecVelocity ); // Turn speed vector SVelocitySync turnSpeed; if ( !BitStream.Read ( &turnSpeed ) ) return false; pVehicle->SetTurnSpeed ( turnSpeed.data.vecVelocity ); // Health SVehicleHealthSync health; if ( !BitStream.Read ( &health ) ) return false; float fPreviousHealth = pVehicle->GetHealth (); float fHealth = health.data.fValue; // Less than last time? if ( fHealth < fPreviousHealth ) { // Grab the delta health float fDeltaHealth = fPreviousHealth - fHealth; if ( fDeltaHealth > 0.0f ) { // Call the onVehicleDamage event CLuaArguments Arguments; Arguments.PushNumber ( fDeltaHealth ); pVehicle->CallEvent ( "onVehicleDamage", Arguments ); } } pVehicle->SetHealth ( fHealth ); // Trailer chain CVehicle* pTowedByVehicle = pVehicle; CVehicle* pTrailer = NULL; ElementID TrailerID; bool bHasTrailer; if ( !BitStream.ReadBit ( bHasTrailer ) ) return false; while ( bHasTrailer ) { BitStream.ReadCompressed ( TrailerID ); CElement* pElement = CElementIDs::GetElement ( TrailerID ); if ( pElement ) pTrailer = static_cast < CVehicle* > ( pElement ); // Read out the trailer position and rotation SPositionSync trailerPosition ( false ); if ( !BitStream.Read ( &trailerPosition ) ) return false; SRotationDegreesSync trailerRotation; if ( !BitStream.Read ( &trailerRotation ) ) return false; // If we found the trailer if ( pTrailer ) { // Set its position and rotation pTrailer->SetPosition ( trailerPosition.data.vecPosition ); pTrailer->SetRotationDegrees ( trailerRotation.data.vecRotation ); // Is this a new trailer, attached? CVehicle* pCurrentTrailer = pTowedByVehicle->GetTowedVehicle (); if ( pCurrentTrailer != pTrailer ) { // If theres a trailer already attached if ( pCurrentTrailer ) { pTowedByVehicle->SetTowedVehicle ( NULL ); pCurrentTrailer->SetTowedByVehicle ( NULL ); // Tell everyone to detach them CVehicleTrailerPacket AttachPacket ( pTowedByVehicle, pCurrentTrailer, false ); g_pGame->GetPlayerManager ()->BroadcastOnlyJoined ( AttachPacket ); // Execute the attach trailer script function CLuaArguments Arguments; Arguments.PushElement ( pTowedByVehicle ); pCurrentTrailer->CallEvent ( "onTrailerDetach", Arguments ); } // If something else is towing this trailer CVehicle* pCurrentVehicle = pTrailer->GetTowedByVehicle (); if ( pCurrentVehicle ) { pCurrentVehicle->SetTowedVehicle ( NULL ); pTrailer->SetTowedByVehicle ( NULL ); // Tell everyone to detach them CVehicleTrailerPacket AttachPacket ( pCurrentVehicle, pTrailer, false ); g_pGame->GetPlayerManager ()->BroadcastOnlyJoined ( AttachPacket ); // Execute the attach trailer script function CLuaArguments Arguments; Arguments.PushElement ( pCurrentVehicle ); pTrailer->CallEvent ( "onTrailerDetach", Arguments ); } pTowedByVehicle->SetTowedVehicle ( pTrailer ); pTrailer->SetTowedByVehicle ( pTowedByVehicle ); // Execute the attach trailer script function CLuaArguments Arguments; Arguments.PushElement ( pTowedByVehicle ); bool bContinue = pTrailer->CallEvent ( "onTrailerAttach", Arguments ); // Attach or detach trailers depending on the event outcome CVehicleTrailerPacket TrailerPacket ( pTowedByVehicle, pTrailer, bContinue ); g_pGame->GetPlayerManager ()->BroadcastOnlyJoined ( TrailerPacket ); } } else break; pTowedByVehicle = pTrailer; if ( BitStream.ReadBit ( bHasTrailer ) == false ) return false; } // If there was a trailer before CVehicle* pCurrentTrailer = pTowedByVehicle->GetTowedVehicle (); if ( pCurrentTrailer ) { pTowedByVehicle->SetTowedVehicle ( NULL ); pCurrentTrailer->SetTowedByVehicle ( NULL ); // Tell everyone else to detach them CVehicleTrailerPacket AttachPacket ( pTowedByVehicle, pCurrentTrailer, false ); g_pGame->GetPlayerManager ()->BroadcastOnlyJoined ( AttachPacket ); // Execute the detach trailer script function CLuaArguments Arguments; Arguments.PushElement ( pTowedByVehicle ); pCurrentTrailer->CallEvent ( "onTrailerDetach", Arguments ); } } // Player health SPlayerHealthSync health; if ( !BitStream.Read ( &health ) ) return false; float fHealth = health.data.fValue; float fOldHealth = pSourcePlayer->GetHealth (); float fHealthLoss = fOldHealth - fHealth; // Less than last packet's frame? if ( fHealth < fOldHealth && fHealthLoss > 0 ) { // Call the onPlayerDamage event CLuaArguments Arguments; Arguments.PushNil (); Arguments.PushNumber ( false ); Arguments.PushNumber ( false ); Arguments.PushNumber ( fHealthLoss ); pSourcePlayer->CallEvent ( "onPlayerDamage", Arguments ); } pSourcePlayer->SetHealth ( fHealth ); // Armor SPlayerArmorSync armor; if ( !BitStream.Read ( &armor ) ) return false; float fArmor = armor.data.fValue; float fOldArmor = pSourcePlayer->GetArmor (); float fArmorLoss = fOldArmor - fArmor; // Less than last packet's frame? if ( fArmor < fOldArmor && fArmorLoss > 0 ) { // Call the onPlayerDamage event CLuaArguments Arguments; Arguments.PushNil (); Arguments.PushNumber ( false ); Arguments.PushNumber ( false ); Arguments.PushNumber ( fArmorLoss ); pSourcePlayer->CallEvent ( "onPlayerDamage", Arguments ); } pSourcePlayer->SetArmor ( fArmor ); // Flags SVehiclePuresyncFlags flags; if ( !BitStream.Read ( &flags ) ) return false; pSourcePlayer->SetWearingGoggles ( flags.data.bIsWearingGoggles ); pSourcePlayer->SetDoingGangDriveby ( flags.data.bIsDoingGangDriveby ); // Weapon sync if ( flags.data.bHasAWeapon ) { SWeaponSlotSync slot; if ( !BitStream.Read ( &slot ) ) return false; pSourcePlayer->SetWeaponSlot ( slot.data.uiSlot ); if ( flags.data.bIsDoingGangDriveby && CWeaponNames::DoesSlotHaveAmmo ( slot.data.uiSlot ) ) { // Read the ammo states SWeaponAmmoSync ammo ( pSourcePlayer->GetWeaponType (), false, true ); if ( !BitStream.Read ( &ammo ) ) return false; pSourcePlayer->SetWeaponAmmoInClip ( ammo.data.usAmmoInClip ); // Read aim data SWeaponAimSync aim ( pSourcePlayer->GetWeaponRange (), true ); if ( !BitStream.Read ( &aim ) ) return false; pSourcePlayer->SetAimDirection ( aim.data.fArm ); pSourcePlayer->SetSniperSourceVector ( aim.data.vecOrigin ); pSourcePlayer->SetTargettingVector ( aim.data.vecTarget ); // Read the driveby direction SDrivebyDirectionSync driveby; if ( !BitStream.Read ( &driveby ) ) return false; pSourcePlayer->SetDriveByDirection ( driveby.data.ucDirection ); } } else pSourcePlayer->SetWeaponSlot ( 0 ); // Vehicle specific data if he's the driver if ( uiSeat == 0 ) { ReadVehicleSpecific ( pVehicle, BitStream ); // Set vehicle specific stuff if he's the driver pVehicle->SetSirenActive ( flags.data.bIsSirenOrAlarmActive ); pVehicle->SetSmokeTrailEnabled ( flags.data.bIsSmokeTrailEnabled ); pVehicle->SetLandingGearDown ( flags.data.bIsLandingGearDown ); pVehicle->SetOnGround ( flags.data.bIsOnGround ); pVehicle->SetInWater ( flags.data.bIsInWater ); pVehicle->SetDerailed ( flags.data.bIsDerailed ); pVehicle->SetHeliSearchLightVisible ( flags.data.bIsHeliSearchLightVisible ); } // Read the vehicle_look_left and vehicle_look_right control states // if it's an aircraft. if ( flags.data.bIsAircraft ) { ControllerState.LeftShoulder2 = BitStream.ReadBit () * 255; ControllerState.RightShoulder2 = BitStream.ReadBit () * 255; } pSourcePlayer->GetPad ()->NewControllerState ( ControllerState ); // Success return true; } } return false; }
void CMapManager::SpawnPlayer ( CPlayer& Player, const CVector& vecPosition, float fRotation, unsigned short usModel, unsigned char ucInterior, unsigned short usDimension, CTeam* pTeam ) { // Don't force them off their team if the spawnpoint doesn't have one if ( pTeam == NULL ) pTeam = Player.GetTeam (); ElementID TeamID = ( pTeam ) ? pTeam->GetID () : INVALID_ELEMENT_ID; // Change the time context to avoid old sync packets arriving causing players // to slide from previous location to the new one. unsigned char ucTimeContext = Player.GenerateSyncTimeContext (); // Tell everyone where he spawns m_pPlayerManager->BroadcastOnlyJoined ( CPlayerSpawnPacket ( Player.GetID (), vecPosition, fRotation, usModel, ucInterior, usDimension, TeamID, ucTimeContext ) ); // Remove him from any occupied vehicle CVehicle* pVehicle = Player.GetOccupiedVehicle (); if ( pVehicle ) { pVehicle->SetOccupant ( NULL, Player.GetOccupiedVehicleSeat () ); Player.SetOccupiedVehicle ( NULL, 0 ); } // If this guy was jacking someone, make sure its aborted pVehicle = Player.GetJackingVehicle (); if ( pVehicle ) { if ( Player.GetVehicleAction () == CPlayer::VEHICLEACTION_JACKING ) { CPed * pOccupant = pVehicle->GetOccupant ( 0 ); if ( pOccupant ) { pOccupant->SetVehicleAction ( CPlayer::VEHICLEACTION_NONE ); // Tell everyone CVehicleInOutPacket Reply ( pVehicle->GetID (), 0, CGame::VEHICLE_NOTIFY_JACK_RETURN, pOccupant->GetID (), Player.GetID () ); Reply.SetSourceElement ( &Player ); m_pPlayerManager->BroadcastOnlyJoined ( Reply ); } } if ( pVehicle->GetJackingPlayer () == &Player ) pVehicle->SetJackingPlayer ( NULL ); } // Update the player data Player.SetSpawned ( true ); Player.SetHealth ( Player.GetMaxHealth () ); Player.SetIsDead ( false ); Player.SetWearingGoggles ( false ); Player.SetHasJetPack ( false ); Player.SetPosition ( vecPosition ); Player.SetRotation ( fRotation ); Player.SetModel ( usModel ); Player.SetVehicleAction ( CPlayer::VEHICLEACTION_NONE ); Player.SetTeam ( pTeam, true ); Player.SetInterior ( ucInterior ); Player.SetDimension ( usDimension ); Player.AttachTo ( NULL ); // Call onPlayerSpawn CLuaArguments OnPlayerSpawnArguments; OnPlayerSpawnArguments.PushNumber ( vecPosition.fX ); OnPlayerSpawnArguments.PushNumber ( vecPosition.fY ); OnPlayerSpawnArguments.PushNumber ( vecPosition.fZ ); OnPlayerSpawnArguments.PushNumber ( fRotation ); OnPlayerSpawnArguments.PushElement ( pTeam ); OnPlayerSpawnArguments.PushNumber ( usModel ); OnPlayerSpawnArguments.PushNumber ( ucInterior ); OnPlayerSpawnArguments.PushNumber ( usDimension ); Player.CallEvent ( "onPlayerSpawn", OnPlayerSpawnArguments ); }
void CRPCFunctions::CursorEvent ( NetBitStreamInterface & bitStream ) { SMouseButtonSync button; unsigned char ucButton; CVector2D vecCursorPosition; unsigned short usX; unsigned short usY; SPositionSync position ( false ); CVector vecPosition; bool bHasCollisionElement; ElementID elementID; if ( bitStream.Read ( &button ) && bitStream.ReadCompressed ( usX ) && bitStream.ReadCompressed ( usY ) && bitStream.Read ( &position ) && bitStream.ReadBit ( bHasCollisionElement ) && ( !bHasCollisionElement || bitStream.ReadCompressed ( elementID ) ) ) { ucButton = button.data.ucButton; vecCursorPosition.fX = static_cast < float > ( usX ); vecCursorPosition.fY = static_cast < float > ( usY ); vecPosition = position.data.vecPosition; if ( !bHasCollisionElement ) elementID = INVALID_ELEMENT_ID; } else return; if ( m_pSourcePlayer->IsJoined () ) { // Get the button and state const char* szButton = NULL; const char* szState = NULL; switch ( ucButton ) { case 0: szButton = "left"; szState = "down"; break; case 1: szButton = "left"; szState = "up"; break; case 2: szButton = "middle"; szState = "down"; break; case 3: szButton = "middle"; szState = "up"; break; case 4: szButton = "right"; szState = "down"; break; case 5: szButton = "right"; szState = "up"; break; } if ( szButton && szState ) { CElement* pElement = CElementIDs::GetElement ( elementID ); if ( pElement ) { // Call the onElementClicked event CLuaArguments Arguments; Arguments.PushString ( szButton ); Arguments.PushString ( szState ); Arguments.PushElement ( m_pSourcePlayer ); Arguments.PushNumber ( vecPosition.fX ); Arguments.PushNumber ( vecPosition.fY ); Arguments.PushNumber ( vecPosition.fZ ); pElement->CallEvent ( "onElementClicked", Arguments ); } // Call the onPlayerClick event CLuaArguments Arguments; Arguments.PushString ( szButton ); Arguments.PushString ( szState ); if ( pElement ) Arguments.PushElement ( pElement ); else Arguments.PushNil (); Arguments.PushNumber ( vecPosition.fX ); Arguments.PushNumber ( vecPosition.fY ); Arguments.PushNumber ( vecPosition.fZ ); Arguments.PushNumber ( vecCursorPosition.fX ); Arguments.PushNumber ( vecCursorPosition.fY ); m_pSourcePlayer->CallEvent ( "onPlayerClick", Arguments ); // TODO: iterate server-side element managers for the click events, eg: colshapes } } }
void CPickup::Use ( CPlayer& Player ) { // Call the onPickupUse event CLuaArguments Arguments; Arguments.PushElement ( &Player ); if ( !CallEvent ( "onPickupUse", Arguments ) ) { CLuaArguments Arguments2; Arguments2.PushElement ( this ); // pickup Player.CallEvent ( "onPlayerPickupUse", Arguments2 ); } else { CLuaArguments Arguments2; Arguments2.PushElement ( this ); // pickup if ( Player.CallEvent ( "onPlayerPickupUse", Arguments2 ) ) { // Tell all the other players to hide it if the respawn intervals are bigger than 0 if ( m_ulRespawnIntervals > 0 ) { // Save our last used time m_ulLastUsedTime = GetTime (); // Mark us as not spawned m_bSpawned = false; // Mark us as hidden SetVisible ( false ); } // Tell him to play the sound and hide/show it Player.Send ( CPickupHitConfirmPacket ( this, true ) ); // Tell everyone else to hide/show it as neccessary g_pGame->GetPlayerManager ()->BroadcastOnlyJoined ( CPickupHitConfirmPacket ( this, false ), &Player ); // Handle it depending on the type switch ( m_ucType ) { // Health pickup? case CPickup::HEALTH: { float fHealth = Player.GetHealth (); float fNewHealth = fHealth + m_fAmount; if ( fNewHealth > 200.0f ) fNewHealth = 200.0f; CStaticFunctionDefinitions::SetElementHealth ( &Player, fNewHealth ); break; } // Armor pickup? case CPickup::ARMOR: { float fArmor = Player.GetArmor (); float fNewArmor = fArmor + m_fAmount; if ( fNewArmor > 100.0f ) fNewArmor = 100.0f; CStaticFunctionDefinitions::SetPedArmor ( &Player, fNewArmor ); break; } // Weapon pickup? case CPickup::WEAPON: { // Give him the weapon CStaticFunctionDefinitions::GiveWeapon ( &Player, m_ucWeaponType, m_usAmmo ); break; } default: break; } } } }
bool CPlayerPuresyncPacket::Read ( NetBitStreamInterface& BitStream ) { if ( m_pSourceElement ) { CPlayer * pSourcePlayer = static_cast < CPlayer * > ( m_pSourceElement ); // Read out the time context unsigned char ucTimeContext = 0; if ( !BitStream.Read ( ucTimeContext ) ) return false; // Only read this packet if it matches the current time context that // player is in. if ( !pSourcePlayer->CanUpdateSync ( ucTimeContext ) ) { return false; } // Read out keys CControllerState ControllerState; ReadFullKeysync ( ControllerState, BitStream ); pSourcePlayer->GetPad ()->NewControllerState ( ControllerState ); // Read the flags SPlayerPuresyncFlags flags; if ( !BitStream.Read ( &flags ) ) return false; pSourcePlayer->SetInWater ( flags.data.bIsInWater ); pSourcePlayer->SetOnGround ( flags.data.bIsOnGround ); pSourcePlayer->SetHasJetPack ( flags.data.bHasJetPack ); pSourcePlayer->SetDucked ( flags.data.bIsDucked ); pSourcePlayer->SetWearingGoggles ( flags.data.bWearsGoogles ); pSourcePlayer->SetChoking ( flags.data.bIsChoking ); pSourcePlayer->SetAkimboArmUp ( flags.data.bAkimboTargetUp ); pSourcePlayer->SetOnFire ( flags.data.bIsOnFire ); pSourcePlayer->SetStealthAiming ( flags.data.bStealthAiming ); // Contact element CElement* pContactElement = NULL; if ( flags.data.bHasContact ) { ElementID Temp; if ( !BitStream.ReadCompressed ( Temp ) ) return false; pContactElement = CElementIDs::GetElement ( Temp ); } CElement * pPreviousContactElement = pSourcePlayer->GetContactElement (); pSourcePlayer->SetContactElement ( pContactElement ); if ( pPreviousContactElement != pContactElement ) { // Call our onPlayerContact event CLuaArguments Arguments; if ( pPreviousContactElement ) Arguments.PushElement ( pPreviousContactElement ); else Arguments.PushNil (); if ( pContactElement ) Arguments.PushElement ( pContactElement ); else Arguments.PushNil (); pSourcePlayer->CallEvent ( "onPlayerContact", Arguments ); } // Player position SPositionSync position ( false ); if ( !BitStream.Read ( &position ) ) return false; if ( pContactElement ) { pSourcePlayer->SetContactPosition ( position.data.vecPosition ); // Get the true position CVector vecTempPos = pContactElement->GetPosition (); position.data.vecPosition += vecTempPos; } pSourcePlayer->SetPosition ( position.data.vecPosition ); // Player rotation SPedRotationSync rotation; if ( !BitStream.Read ( &rotation ) ) return false; pSourcePlayer->SetRotation ( rotation.data.fRotation ); // Move speed vector if ( flags.data.bSyncingVelocity ) { SVelocitySync velocity; if ( !BitStream.Read ( &velocity ) ) return false; pSourcePlayer->SetVelocity ( velocity.data.vecVelocity ); } // Health ( stored with damage ) SPlayerHealthSync health; if ( !BitStream.Read ( &health ) ) return false; float fHealth = health.data.fValue; // Armor SPlayerArmorSync armor; if ( !BitStream.Read ( &armor ) ) return false; float fArmor = armor.data.fValue; float fOldArmor = pSourcePlayer->GetArmor (); float fArmorLoss = fOldArmor - fArmor; pSourcePlayer->SetArmor ( fArmor ); // Read out and set the camera rotation float fCameraRotation; if ( !BitStream.Read ( fCameraRotation ) ) return false; pSourcePlayer->SetCameraRotation ( fCameraRotation ); if ( flags.data.bHasAWeapon ) { if ( BitStream.Version () >= 0x0d ) { // Check client has the weapon we think he has unsigned char ucWeaponType; if ( !BitStream.Read ( ucWeaponType ) ) return false; if ( pSourcePlayer->GetWeaponType () != ucWeaponType ) return false; } // Current weapon slot SWeaponSlotSync slot; if ( !BitStream.Read ( &slot ) ) return false; unsigned int uiSlot = slot.data.uiSlot; pSourcePlayer->SetWeaponSlot ( uiSlot ); if ( CWeaponNames::DoesSlotHaveAmmo ( uiSlot ) ) { // Read out the ammo states SWeaponAmmoSync ammo ( pSourcePlayer->GetWeaponType (), true, true ); if ( !BitStream.Read ( &ammo ) ) return false; pSourcePlayer->SetWeaponAmmoInClip ( ammo.data.usAmmoInClip ); pSourcePlayer->SetWeaponTotalAmmo ( ammo.data.usTotalAmmo ); // Read out the aim data SWeaponAimSync sync ( pSourcePlayer->GetWeaponRange (), ( ControllerState.RightShoulder1 || ControllerState.ButtonCircle ) ); if ( !BitStream.Read ( &sync ) ) return false; // Set the arm directions and whether or not arms are up pSourcePlayer->SetAimDirection ( sync.data.fArm ); // Read the aim data only if he's shooting or aiming if ( sync.isFull() ) { pSourcePlayer->SetSniperSourceVector ( sync.data.vecOrigin ); pSourcePlayer->SetTargettingVector ( sync.data.vecTarget ); } } else { pSourcePlayer->SetWeaponAmmoInClip ( 1 ); pSourcePlayer->SetWeaponTotalAmmo ( 1 ); } } else { pSourcePlayer->SetWeaponSlot ( 0 ); pSourcePlayer->SetWeaponAmmoInClip ( 1 ); pSourcePlayer->SetWeaponTotalAmmo ( 1 ); } // Read out damage info if changed if ( BitStream.ReadBit () == true ) { ElementID DamagerID; if ( !BitStream.ReadCompressed ( DamagerID ) ) return false; SWeaponTypeSync weaponType; if ( !BitStream.Read ( &weaponType ) ) return false; SBodypartSync bodyPart; if ( !BitStream.Read ( &bodyPart ) ) return false; pSourcePlayer->SetDamageInfo ( DamagerID, weaponType.data.ucWeaponType, bodyPart.data.uiBodypart ); } // If we know the player's dead, make sure the health we send on is 0 if ( pSourcePlayer->IsDead () ) fHealth = 0.0f; float fOldHealth = pSourcePlayer->GetHealth (); float fHealthLoss = fOldHealth - fHealth; pSourcePlayer->SetHealth ( fHealth ); // Less than last packet's frame? if ( fHealthLoss > 0 || fArmorLoss > 0 ) { float fDamage = 0.0f; if ( fHealthLoss > 0 ) fDamage += fHealthLoss; if ( fArmorLoss > 0 ) fDamage += fArmorLoss; // Call the onPlayerDamage event CLuaArguments Arguments; CElement* pKillerElement = CElementIDs::GetElement ( pSourcePlayer->GetPlayerAttacker () ); if ( pKillerElement ) Arguments.PushElement ( pKillerElement ); else Arguments.PushNil (); Arguments.PushNumber ( pSourcePlayer->GetAttackWeapon () ); Arguments.PushNumber ( pSourcePlayer->GetAttackBodyPart () ); Arguments.PushNumber ( fDamage ); pSourcePlayer->CallEvent ( "onPlayerDamage", Arguments ); } // Success return true; } return false; }