Ejemplo n.º 1
0
void CElement::GetChildrenByType(const char* szType, lua_State* pLua)
{
    assert(szType);
    assert(pLua);

    // Add all our children to the table on top of the given lua main's stack
    unsigned int                    uiIndex = 0;
    unsigned int                    uiTypeHash = GetTypeHashFromString(szType);
    CChildListType ::const_iterator iter = m_Children.begin();
    for (; iter != m_Children.end(); iter++)
    {
        // Name matches?
        if ((*iter)->GetTypeHash() == uiTypeHash)
        {
            // Add it to the table
            lua_pushnumber(pLua, ++uiIndex);
            lua_pushelement(pLua, *iter);
            lua_settable(pLua, -3);
        }
    }
}
Ejemplo n.º 2
0
void lua_pushuserdata ( lua_State* luaVM, void* pData )
{
    if ( CClientEntity* pEntity = UserDataCast < CClientEntity > ( ( CClientEntity* ) NULL, pData, luaVM ) )
        return lua_pushelement ( luaVM, pEntity );
    else if ( CResource* pResource = UserDataCast < CResource > ( ( CResource* ) NULL, pData, luaVM ) )
        return lua_pushresource ( luaVM, pResource );
    else if ( CXMLNode* pNode = UserDataCast < CXMLNode > ( ( CXMLNode* ) NULL, pData, luaVM ) )
        return lua_pushxmlnode ( luaVM, pNode );
    else if ( CLuaTimer* pTimer = UserDataCast < CLuaTimer > ( ( CLuaTimer* ) NULL, pData, luaVM ) )
        return lua_pushtimer ( luaVM, pTimer );
    else if ( CLuaVector2D* pVector = UserDataCast < CLuaVector2D > ( (CLuaVector2D*) NULL, pData, luaVM ) )
        return lua_pushvector ( luaVM,* pVector );
    else if ( CLuaVector3D* pVector = UserDataCast < CLuaVector3D > ( (CLuaVector3D*) NULL, pData, luaVM ) )
        return lua_pushvector ( luaVM, *pVector );
    else if ( CLuaVector4D* pVector = UserDataCast < CLuaVector4D > ( (CLuaVector4D*) NULL, pData, luaVM ) )
        return lua_pushvector ( luaVM, *pVector );
    else if ( CLuaMatrix* pMatrix = UserDataCast < CLuaMatrix > ( (CLuaMatrix*) NULL, pData, luaVM ) )
        return lua_pushmatrix ( luaVM, *pMatrix );

    lua_pushobject ( luaVM, NULL, pData );
}
Ejemplo n.º 3
0
int CLuaSearchLightDefs::CreateSearchLight(lua_State* luaVM)
{
    //  searchlight createSearchLight ( float startX, float startY, float startZ, float endX, float endY, float endZ, float startRadius, float endRadius [, bool
    //  renderSpot = true ] )
    CVector vecStart, vecEnd;
    float   startRadius, endRadius;
    bool    renderSpot;

    CScriptArgReader argStream(luaVM);
    argStream.ReadVector3D(vecStart);
    argStream.ReadVector3D(vecEnd);
    argStream.ReadNumber(startRadius);
    argStream.ReadNumber(endRadius);
    argStream.ReadBool(renderSpot, true);

    if (!argStream.HasErrors())
    {
        CLuaMain*  pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
        CResource* pResource = pLuaMain ? pLuaMain->GetResource() : nullptr;

        if (pResource)
        {
            auto pLight = CStaticFunctionDefinitions::CreateSearchLight(*pResource, vecStart, vecEnd, startRadius, endRadius, renderSpot);
            if (pLight)
            {
                CElementGroup* pGroup = pResource->GetElementGroup();
                if (pGroup)
                    pGroup->Add(pLight);

                lua_pushelement(luaVM, pLight);
                return 1;
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());

    lua_pushboolean(luaVM, false);
    return 1;
}
Ejemplo n.º 4
0
int CLuaPedDefs::GetPedTarget ( lua_State* luaVM )
{
    CPed* pPed;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadUserData ( pPed );

    if ( !argStream.HasErrors () )
    {
        CElement *pTarget = CStaticFunctionDefinitions::GetPedTarget ( pPed );
        if ( pTarget )
        {
            lua_pushelement ( luaVM, pTarget );
            return 1;
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Ejemplo n.º 5
0
int CLuaFxDefs::CreateEffect ( lua_State* luaVM )
{
    // bool createEffect ( string fxName, float posX, float posY, float posZ[, float rotX, float rotY, float rotZ] )

    CVector vecPosition;
    CVector vecRotation;
    SString strFxName;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( strFxName );
    argStream.ReadVector3D ( vecPosition );
    argStream.ReadVector3D ( vecRotation, CVector(0, 0, 0) );

    if ( !argStream.HasErrors ( ) )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResource* pResource = pLuaMain->GetResource();
            if ( pResource )
            {
                // Create it and return it
                CClientEffect * pFx = CStaticFunctionDefinitions::CreateEffect ( *pResource, strFxName, vecPosition );
                if ( pFx != NULL )
                {
                    pFx->SetRotationDegrees ( vecRotation );
                    lua_pushelement ( luaVM, pFx );
                    return 1;
                }
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() );

    // Failed
    lua_pushboolean ( luaVM, false );
    return 1;
}
int CLuaFunctionDefs::CreateWeapon ( lua_State* luaVM )
{
    CVector vecPos;
    eWeaponType weaponType;
    CScriptArgReader argStream ( luaVM );
    argStream.ReadEnumStringOrNumber ( weaponType );
    argStream.ReadNumber ( vecPos.fX );
    argStream.ReadNumber ( vecPos.fY );
    argStream.ReadNumber ( vecPos.fZ );

    if ( !argStream.HasErrors () )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResource* pResource = pLuaMain->GetResource ();
            if ( pResource )
            {
                CClientWeapon * pWeapon = CStaticFunctionDefinitions::CreateWeapon ( *pResource, weaponType, vecPos );
                if ( pWeapon )
                {
                    CElementGroup * pGroup = pResource->GetElementGroup();
                    if ( pGroup )
                    {
                        pGroup->Add ( ( CClientEntity* ) pWeapon );
                    }

                    lua_pushelement ( luaVM, pWeapon );
                    return 1;
                }
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Ejemplo n.º 7
0
int CLuaFunctionDefs::GetTeamFromName ( lua_State* luaVM )
{
    SString strName;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( strName );

    if ( !argStream.HasErrors () )
    {
        CTeam* pTeam = CStaticFunctionDefinitions::GetTeamFromName ( strName );
        if ( pTeam )
        {
            lua_pushelement ( luaVM, pTeam );
            return 1;
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Ejemplo n.º 8
0
int CLuaCameraDefs::getCameraTarget ( lua_State* luaVM )
{
//  element getCameraTarget ( player thePlayer )
    CPlayer* pPlayer;
    
    CScriptArgReader argStream ( luaVM );
    argStream.ReadUserData ( pPlayer );
    
    if ( !argStream.HasErrors () )
    {
        CElement* pTarget = CStaticFunctionDefinitions::GetCameraTarget ( pPlayer );
        if ( pTarget )
        {
            lua_pushelement ( luaVM, pTarget );
            return 1;
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
int CLuaFunctionDefs::GUIGetBrowser ( lua_State* luaVM ) // Or rather guiGetBrowserBrowser?
{
//  webbrowser guiGetBrowser ( gui-webbrowser browser )
    CClientGUIElement* pGUIElement;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadUserData < CGUIWebBrowser > ( pGUIElement );

    if ( !argStream.HasErrors () )
    {
        if ( IS_GUI ( pGUIElement ) && pGUIElement->GetCGUIType () == CGUI_WEBBROWSER )
        {
            CClientGUIWebBrowser* pGUIBrowser = static_cast < CClientGUIWebBrowser* > ( pGUIElement );
            lua_pushelement ( luaVM, pGUIBrowser->GetBrowser () );
            return 1;
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Ejemplo n.º 10
0
int CLuaFunctionDefs::GetAccountPlayer ( lua_State* luaVM )
{
    //  player getAccountPlayer ( account theAccount )
    CAccount* pAccount;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadUserData ( pAccount );

    if ( !argStream.HasErrors () )
    {
        CClient* pClient = CStaticFunctionDefinitions::GetAccountPlayer ( pAccount );
        if ( pClient )
        {
            lua_pushelement ( luaVM, pClient->GetElement () );
            return 1;
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
int CLuaFunctionDefs::GUICreateBrowser ( lua_State* luaVM )
{
//  element guiCreateBrowser ( float x, float y, float width, float height, bool isLocal, bool isTransparent, bool relative, [element parent = nil] )
    float x; float y; float width; float height; bool bIsLocal; bool bIsTransparent; bool bIsRelative; CClientGUIElement* parent;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadNumber ( x );
    argStream.ReadNumber ( y );
    argStream.ReadNumber ( width );
    argStream.ReadNumber ( height );
    argStream.ReadBool ( bIsLocal );
    argStream.ReadBool ( bIsTransparent );
    argStream.ReadBool ( bIsRelative );
    argStream.ReadUserData ( parent, nullptr );

    if ( !argStream.HasErrors () )
    {
        if ( !bIsLocal && !g_pCore->GetWebCore()->GetRemotePagesEnabled () )
        {
            lua_pushboolean ( luaVM, false );
            return 1;
        }

        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CClientGUIElement* pGUIElement = CStaticFunctionDefinitions::GUICreateBrowser ( *pLuaMain, x, y, width, height, bIsLocal, bIsTransparent, bIsRelative, parent );
            lua_pushelement ( luaVM, pGUIElement );
            return 1;
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Ejemplo n.º 12
0
int CLuaFunctionDefs::PlaySFX3D ( lua_State* luaVM )
{
//  sound playSFX3D ( string audioContainer, int bankIndex, int audioIndex, float posX, float posY, float posZ [, loop = false ] )
    eAudioLookupIndex containerIndex; int iBankIndex; int iAudioIndex; CVector vecPosition; bool bLoop;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadEnumString ( containerIndex );
    argStream.ReadNumber ( iBankIndex );
    argStream.ReadNumber ( iAudioIndex );
    argStream.ReadNumber ( vecPosition.fX );
    argStream.ReadNumber ( vecPosition.fY );
    argStream.ReadNumber ( vecPosition.fZ );
    argStream.ReadBool ( bLoop, false );

    if ( !argStream.HasErrors () )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResource* pResource = pLuaMain->GetResource ();
            if ( pResource )
            {
                CClientSound* pSound;
                if ( CStaticFunctionDefinitions::PlaySFX3D ( pResource, containerIndex, iBankIndex, iAudioIndex, vecPosition, bLoop, pSound ) )
                {
                    lua_pushelement ( luaVM, pSound );
                    return 1;
                }
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Ejemplo n.º 13
0
void CElement::GetEntitiesFromRoot(unsigned int uiTypeHash, lua_State* pLua)
{
#if CHECK_ENTITIES_FROM_ROOT
    _CheckEntitiesFromRoot(uiTypeHash);
#endif

    t_mapEntitiesFromRoot::iterator find = ms_mapEntitiesFromRoot.find(uiTypeHash);
    if (find != ms_mapEntitiesFromRoot.end())
    {
        CFromRootListType& listEntities = find->second;
        CElement*          pEntity;
        unsigned int       uiIndex = 0;

        for (CChildListType::const_reverse_iterator i = listEntities.rbegin(); i != listEntities.rend(); ++i)
        {
            pEntity = *i;

            // Add it to the table
            lua_pushnumber(pLua, ++uiIndex);
            lua_pushelement(pLua, pEntity);
            lua_settable(pLua, -3);
        }
    }
}
int CLuaFunctionDefs::GetProjectileTarget ( lua_State* luaVM )
{
    // Verify the argument
    if ( lua_type ( luaVM, 1 ) == LUA_TLIGHTUSERDATA )
    {
        CClientProjectile* pProjectile = lua_toprojectile ( luaVM, 1 );
        if ( pProjectile )
        {
            unsigned char ucWeapon = pProjectile->GetWeaponType();
			if (ucWeapon == WEAPONTYPE_ROCKET_HS)
			{
				lua_pushelement ( luaVM, pProjectile->GetTargetEntity() );
                return 1;
			}
        }
        else
            m_pScriptDebugging->LogBadPointer ( luaVM, "projectile", 1 );
    }
    else
        m_pScriptDebugging->LogBadType ( luaVM );

    lua_pushboolean ( luaVM, false );
    return 1;
}
int CLuaFunctionDefs::CreateProjectile ( lua_State* luaVM )
{
    if ( ( lua_istype ( luaVM, 1, LUA_TLIGHTUSERDATA ) ) &&
        ( lua_istype ( luaVM, 2, LUA_TNUMBER ) || lua_istype ( luaVM, 2, LUA_TSTRING ) ) )
    {
        CClientEntity* pCreator = lua_toelement ( luaVM, 1 );
        if ( pCreator )
        {
            unsigned char ucWeaponType = static_cast < unsigned char > ( lua_tonumber ( luaVM, 2 ) );
            CVector vecOrigin;
            pCreator->GetPosition ( vecOrigin );
            float fForce = 1.0f;
            CClientEntity* pTarget = NULL;
            CVector *pvecRotation = NULL, *pvecMoveSpeed = NULL;
            unsigned short usModel = 0;
            if ( ( lua_istype ( luaVM, 3, LUA_TNUMBER ) || lua_istype ( luaVM, 3, LUA_TSTRING ) ) &&
                ( lua_istype ( luaVM, 4, LUA_TNUMBER ) || lua_istype ( luaVM, 4, LUA_TSTRING ) ) &&
                ( lua_istype ( luaVM, 5, LUA_TNUMBER ) || lua_istype ( luaVM, 5, LUA_TSTRING ) ) )
            {
                vecOrigin = CVector ( static_cast < float > ( lua_tonumber ( luaVM, 3 ) ),
                    static_cast < float > ( lua_tonumber ( luaVM, 4 ) ),
                    static_cast < float > ( lua_tonumber ( luaVM, 5 ) ) );

                if ( lua_istype ( luaVM, 6, LUA_TNUMBER ) || lua_istype ( luaVM, 6, LUA_TSTRING ) )
                {
                    fForce = static_cast < float > ( lua_tonumber ( luaVM, 6 ) );

                    if ( lua_istype ( luaVM, 7, LUA_TLIGHTUSERDATA ) )
                    {
                        CClientEntity* pTemp = lua_toelement ( luaVM, 7 );
                        if ( pTemp )
                        {
                            pTarget = pTemp;
                        }
                        else
                            m_pScriptDebugging->LogBadPointer ( luaVM, "createProjectile", "element", 7 );
                    }

                    int iArgument8 = lua_type ( luaVM, 8 );
                    int iArgument9 = lua_type ( luaVM, 9 );
                    int iArgument10 = lua_type ( luaVM, 10 );
                    if ( ( iArgument8 == LUA_TSTRING || iArgument8 == LUA_TNUMBER ) &&
                        ( iArgument9 == LUA_TSTRING || iArgument9 == LUA_TNUMBER ) &&
                        ( iArgument10 == LUA_TSTRING || iArgument10 == LUA_TNUMBER ) )
                    {
                        pvecRotation = new CVector ( static_cast < float > ( lua_tonumber ( luaVM, 8 ) ),
                            static_cast < float > ( lua_tonumber ( luaVM, 9 ) ),
                            static_cast < float > ( lua_tonumber ( luaVM, 10 ) ) );
                    }
                    int iArgument11 = lua_type ( luaVM, 11 );
                    int iArgument12 = lua_type ( luaVM, 12 );
                    int iArgument13 = lua_type ( luaVM, 13 );
                    if ( ( iArgument11 == LUA_TSTRING || iArgument11 == LUA_TNUMBER ) &&
                        ( iArgument12 == LUA_TSTRING || iArgument12 == LUA_TNUMBER ) &&
                        ( iArgument13 == LUA_TSTRING || iArgument13 == LUA_TNUMBER ) )
                    {
                        pvecMoveSpeed = new CVector ( static_cast < float > ( lua_tonumber ( luaVM, 11 ) ),
                            static_cast < float > ( lua_tonumber ( luaVM, 12 ) ),
                            static_cast < float > ( lua_tonumber ( luaVM, 13 ) ) );

                        int iArgument14 = lua_type ( luaVM, 14 );
                        if ( iArgument14 == LUA_TSTRING || iArgument14 == LUA_TNUMBER )
                        {
                            usModel = static_cast < unsigned short > ( lua_tonumber ( luaVM, 14 ) );
                        }
                    }
                }
            }

            CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
            if ( pLuaMain )
            {
                CResource * pResource = pLuaMain->GetResource();
                if ( pResource )
                {
                    CClientProjectile * pProjectile = CStaticFunctionDefinitions::CreateProjectile ( *pResource, *pCreator, ucWeaponType, vecOrigin, fForce, pTarget, pvecRotation, pvecMoveSpeed, usModel );
                    if ( pProjectile )
                    {
                        CElementGroup * pGroup = pResource->GetElementGroup();
                        if ( pGroup )
                        {
                            pGroup->Add ( ( CClientEntity* ) pProjectile );
                        }

                        if ( pvecRotation )
                        {
                            delete pvecRotation;
                            pvecRotation = NULL;
                        }
                        if ( pvecMoveSpeed )
                        {
                            delete pvecMoveSpeed;
                            pvecMoveSpeed = NULL;
                        }

                        lua_pushelement ( luaVM, pProjectile );
                        return 1;
                    }
                }
            }
            if ( pvecRotation )
            {
                delete pvecRotation;
                pvecRotation = NULL;
            }
            if ( pvecMoveSpeed )
            {
                delete pvecMoveSpeed;
                pvecMoveSpeed = NULL;
            }
        }
        else
            m_pScriptDebugging->LogBadPointer ( luaVM, "createProjectile", "element", 1 );
    }
    else
        m_pScriptDebugging->LogBadType ( luaVM, "createProjectile" );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Ejemplo n.º 16
0
int CLuaFileDefs::fileCreate ( lua_State* luaVM )
{
//  file fileCreate ( string filePath )
    SString filePath;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( filePath );

    if ( !argStream.HasErrors () )
    {
        // Grab our lua VM
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );

        if ( !g_pNet->ValidateBinaryFileName ( filePath ) )
        {
            argStream.SetCustomError ( SString ( "Filename not allowed %s", *filePath ), "File error" );
        }
        else
        if ( pLuaMain )
        {
            SString strAbsPath;
            SString strMetaPath;
            CResource* pThisResource = pLuaMain->GetResource ();
            CResource* pResource = pThisResource;
            if ( CResourceManager::ParseResourcePathInput ( filePath, pResource, strAbsPath, strMetaPath ) )
            {
                // Inform file verifier
                g_pClientGame->GetResourceManager()->FileModifedByScript( strAbsPath );

                // Make sure the destination folder exist so we can create the file
                MakeSureDirExists ( strAbsPath.c_str () );

                // Create the file to create
                eAccessType accessType = filePath[0] == '@' ? eAccessType::ACCESS_PRIVATE : eAccessType::ACCESS_PUBLIC;
                CScriptFile* pFile = new CScriptFile( pThisResource->GetScriptID( ), strMetaPath.c_str(), DEFAULT_MAX_FILESIZE, accessType);
                assert ( pFile );

                // Try to load it
                if ( pFile->Load ( pResource, CScriptFile::MODE_CREATE ) )
                {
                    // Make it a child of the resource's file root
                    pFile->SetParent ( pResource->GetResourceDynamicEntity () );

                    // Add it to the scrpt resource element group
                    CElementGroup* pGroup = pThisResource->GetElementGroup ();
                    if ( pGroup )
                    {
                        pGroup->Add ( pFile );
                    }

                    // Success. Return the file.
                    lua_pushelement ( luaVM, pFile );
                    return 1;
                }
                else
                {
                    // Delete the file again
                    delete pFile;

                    // Output error
                    argStream.SetCustomError ( SString ( "Unable to create %s", *filePath ), "File error" );
                }
            }
        }
    }

    if ( argStream.HasErrors () )
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    // Failed
    lua_pushboolean ( luaVM, false );
    return 1;
}
Ejemplo n.º 17
0
bool CMapEventManager::Call ( const char* szName, const CLuaArguments& Arguments, class CClientEntity* pSource, class CClientEntity* pThis )
{
    // Check if no events
    if ( !m_bHasEvents )
        return false;

    // Check if no events with a name match
    EventsIterPair itPair = m_EventsMap.equal_range ( szName );
    if ( itPair.first == itPair.second )
        return false;

    TIMEUS startTimeCall = GetTimeUs ();
    SString strStatus;

    // Check for multi-threading slipups
    assert ( IsMainThread () );

    // Call all the events with matching names
    bool bCalled = false;
    bool bIsAlreadyIterating = m_bIteratingList;
    m_bIteratingList = true;

    // Copy the results into a array in case m_EventsMap is modified during the call
    std::vector< CMapEvent* > matchingEvents;
    for ( EventsIter iter = itPair.first ; iter != itPair.second ; ++iter )
        matchingEvents.push_back(iter->second);

    for ( std::vector< CMapEvent* >::iterator iter = matchingEvents.begin() ; iter != matchingEvents.end() ; ++iter )
    {
        CMapEvent* pMapEvent = *iter;

        // If it's not being destroyed
        if ( !pMapEvent->IsBeingDestroyed () )
        {
            // Compare the names
            dassert ( strcmp ( pMapEvent->GetName (), szName ) == 0 );
            {
                // Call if propagated?
                if ( pSource == pThis || pMapEvent->IsPropagated () )
                {
                    // Grab the current VM
                    lua_State* pState = pMapEvent->GetVM ()->GetVM ();

                    LUA_CHECKSTACK ( pState, 1 );   // Ensure some room

                    #if MTA_DEBUG
                        int luaStackPointer = lua_gettop ( pState );
                    #endif

                    TIMEUS startTime = GetTimeUs();

                    // Aspect ratio adjustment bodges
                    if ( pMapEvent->ShouldAllowAspectRatioAdjustment() )
                    {
                        g_bAllowAspectRatioAdjustment = true;
                        if ( pMapEvent->ShouldForceAspectRatioAdjustment() )
                            g_pCore->GetGraphics()->SetAspectRatioAdjustmentEnabled( true );
                    }

                    // Record event for the crash dump writer
                    static bool bEnabled = ( g_pCore->GetDiagnosticDebug () == EDiagnosticDebug::LUA_TRACE_0000 );
                    if ( bEnabled )
                        g_pCore->LogEvent ( 0, "Lua Event", pMapEvent->GetVM ()->GetScriptName (), szName );

                    // Store the current values of the globals
                    lua_getglobal ( pState, "source" );
                    CLuaArgument OldSource ( pState, -1 );
                    lua_pop( pState, 1 );

                    lua_getglobal ( pState, "this" );
                    CLuaArgument OldThis ( pState, -1 );
                    lua_pop( pState, 1 );

                    lua_getglobal ( pState, "sourceResource" );
                    CLuaArgument OldResource ( pState, -1 );
                    lua_pop( pState, 1 );

                    lua_getglobal ( pState, "sourceResourceRoot" );
                    CLuaArgument OldResourceRoot ( pState, -1 );
                    lua_pop( pState, 1 );

                    lua_getglobal ( pState, "eventName" );
                    CLuaArgument OldEventName ( pState, -1 );
                    lua_pop( pState, 1 );

                    // Set the "source", "this", "sourceResource" and the "sourceResourceRoot" globals on that VM
                    lua_pushelement ( pState, pSource );
                    lua_setglobal ( pState, "source" );

                    lua_pushelement ( pState, pThis );
                    lua_setglobal ( pState, "this" );

                    CLuaMain* pLuaMain = g_pClientGame->GetScriptDebugging()->GetTopLuaMain();
                    CResource* pSourceResource = pLuaMain ? pLuaMain->GetResource() : NULL;
                    if ( pSourceResource )
                    {
                        lua_pushresource ( pState, pSourceResource );
                        lua_setglobal ( pState, "sourceResource" );

                        lua_pushelement ( pState, pSourceResource->GetResourceDynamicEntity() );
                        lua_setglobal ( pState, "sourceResourceRoot" );
                    }
                    else
                    {
                        lua_pushnil ( pState );
                        lua_setglobal ( pState, "sourceResource" );

                        lua_pushnil ( pState );
                        lua_setglobal ( pState, "sourceResourceRoot" );
                    }

                    lua_pushstring ( pState, szName );
                    lua_setglobal ( pState, "eventName" );
                    
                    // Call it
                    pMapEvent->Call ( Arguments );
                    bCalled = true;

                    // Reset the globals on that VM
                    OldSource.Push ( pState );
                    lua_setglobal ( pState, "source" );

                    OldThis.Push ( pState );
                    lua_setglobal ( pState, "this" );                

                    OldResource.Push ( pState );
                    lua_setglobal ( pState, "sourceResource" );

                    OldResourceRoot.Push ( pState );
                    lua_setglobal ( pState, "sourceResourceRoot" );

                    OldEventName.Push ( pState );
                    lua_setglobal ( pState, "eventName" );

                    #if MTA_DEBUG
                        assert ( lua_gettop ( pState ) == luaStackPointer );
                    #endif

                    // Aspect ratio adjustment bodges
                    if ( pMapEvent->ShouldAllowAspectRatioAdjustment() )
                    {
                        g_pCore->GetGraphics()->SetAspectRatioAdjustmentEnabled( false );
                        g_bAllowAspectRatioAdjustment = false;
                    }

                    TIMEUS deltaTimeUs = GetTimeUs() - startTime;

                    if ( deltaTimeUs > 3000 ) 
                        if ( IS_TIMING_CHECKPOINTS() )
                            strStatus += SString ( " (%s %d ms)", pMapEvent->GetVM ()->GetScriptName (), deltaTimeUs / 1000 );

                    CClientPerfStatLuaTiming::GetSingleton ()->UpdateLuaTiming ( pMapEvent->GetVM (), szName, deltaTimeUs );
                }
            }
        }
    }

    // Clean out the trash if we're no longer calling events.
    if ( !bIsAlreadyIterating )
    {
        TakeOutTheTrash ();

        // We're no longer iterating the list
        m_bIteratingList = false;
    }

    if ( IS_TIMING_CHECKPOINTS() )
    {
        TIMEUS deltaTimeUs = GetTimeUs() - startTimeCall;
        if ( deltaTimeUs > 5000 )
            TIMING_DETAIL( SString ( "CMapEventManager::Call ( %s, ... ) took %d ms ( %s )", szName, deltaTimeUs / 1000, *strStatus ) );
    }

    // Return whether we called atleast one func or not
    return bCalled;
}
Ejemplo n.º 18
0
int CLuaFunctionDefs::CreateWater ( lua_State* luaVM )
{
    CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
    if ( pLuaMain )
    {
        CResource* pResource = pLuaMain->GetResource ();
        if ( pResource )
        {
            int iArgument1  = lua_type ( luaVM, 1 );
            int iArgument2  = lua_type ( luaVM, 2 );
            int iArgument3  = lua_type ( luaVM, 3 );
            int iArgument4  = lua_type ( luaVM, 4 );
            int iArgument5  = lua_type ( luaVM, 5 );
            int iArgument6  = lua_type ( luaVM, 6 );
            int iArgument7  = lua_type ( luaVM, 7 );
            int iArgument8  = lua_type ( luaVM, 8 );
            int iArgument9  = lua_type ( luaVM, 9 );
            int iArgument10 = lua_type ( luaVM, 10 );
            int iArgument11 = lua_type ( luaVM, 11 );
            int iArgument12 = lua_type ( luaVM, 12 );
            if ( ( iArgument1 == LUA_TNUMBER || iArgument1 == LUA_TSTRING ) &&
                ( iArgument2 == LUA_TNUMBER || iArgument2 == LUA_TSTRING ) &&
                ( iArgument3 == LUA_TNUMBER || iArgument3 == LUA_TSTRING ) &&
                ( iArgument4 == LUA_TNUMBER || iArgument4 == LUA_TSTRING ) &&
                ( iArgument5 == LUA_TNUMBER || iArgument5 == LUA_TSTRING ) &&
                ( iArgument6 == LUA_TNUMBER || iArgument6 == LUA_TSTRING ) &&
                ( iArgument7 == LUA_TNUMBER || iArgument7 == LUA_TSTRING ) &&
                ( iArgument8 == LUA_TNUMBER || iArgument8 == LUA_TSTRING ) &&
                ( iArgument9 == LUA_TNUMBER || iArgument9 == LUA_TSTRING ) )
            {
                CVector v1 ( (float)lua_tonumber(luaVM, 1), (float)lua_tonumber(luaVM, 2), (float)lua_tonumber(luaVM, 3) );
                CVector v2 ( (float)lua_tonumber(luaVM, 4), (float)lua_tonumber(luaVM, 5), (float)lua_tonumber(luaVM, 6) );
                CVector v3 ( (float)lua_tonumber(luaVM, 7), (float)lua_tonumber(luaVM, 8), (float)lua_tonumber(luaVM, 9) );
                if ( ( iArgument10 == LUA_TNUMBER || iArgument10 == LUA_TSTRING ) &&
                    ( iArgument11 == LUA_TNUMBER || iArgument11 == LUA_TSTRING ) &&
                    ( iArgument12 == LUA_TNUMBER || iArgument12 == LUA_TSTRING ) )
                {
                    CVector v4 ( (float)lua_tonumber(luaVM, 10), (float)lua_tonumber(luaVM, 11), (float)lua_tonumber(luaVM, 12) );
                    bool bShallow = false;
                    if ( lua_type ( luaVM, 13 ) == LUA_TBOOLEAN && lua_toboolean ( luaVM, 13 ) )
                        bShallow = true;
                    lua_pushelement ( luaVM, CStaticFunctionDefinitions::CreateWater (
                        *pResource, &v1, &v2, &v3, &v4, bShallow ) );
                    return 1;
                }
                else
                {
                    bool bShallow = false;
                    if ( lua_type ( luaVM, 10 ) == LUA_TBOOLEAN && lua_toboolean ( luaVM, 10 ) )
                        bShallow = true;
                    lua_pushelement ( luaVM, CStaticFunctionDefinitions::CreateWater (
                        *pResource, &v1, &v2, &v3, NULL, bShallow ) );
                    return 1;
                }
            }
            else
                m_pScriptDebugging->LogBadType ( luaVM, "createWater" );
        }
    }

    lua_pushboolean ( luaVM, false );
    return 1;
}
Ejemplo n.º 19
0
int CLuaFileDefs::fileOpen ( lua_State* luaVM )
{
//  file fileOpen ( string filePath [, bool readOnly = false ] )
    SString filePath; bool readOnly;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( filePath );
    argStream.ReadBool ( readOnly, false );

    if ( !argStream.HasErrors () )
    {
        // Grab our lua VM
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            SString strAbsPath;
            SString strMetaPath;
            CResource* pThisResource = pLuaMain->GetResource ();
            CResource* pResource = pThisResource;
            if ( CResourceManager::ParseResourcePathInput ( filePath, pResource, strAbsPath, strMetaPath ) )
            {
                // Inform file verifier
                if ( !readOnly )
                    g_pClientGame->GetResourceManager()->FileModifedByScript( strAbsPath );

                // Create the file to create
                eAccessType accessType = filePath[0] == '@' ? eAccessType::ACCESS_PRIVATE : eAccessType::ACCESS_PUBLIC;
                CScriptFile* pFile = new CScriptFile( pThisResource->GetScriptID( ), strMetaPath.c_str( ), DEFAULT_MAX_FILESIZE, accessType );
                assert ( pFile );

                // Try to load it
                if ( pFile->Load ( pResource, readOnly ? CScriptFile::MODE_READ : CScriptFile::MODE_READWRITE ) )
                {
                    // Make it a child of the resource's file root
                    pFile->SetParent ( pResource->GetResourceDynamicEntity () );

                    // Grab its owner resource
                    CResource* pParentResource = pLuaMain->GetResource ();
                    if ( pParentResource )
                    {
                        // Add it to the scrpt resource element group
                        CElementGroup* pGroup = pParentResource->GetElementGroup ();
                        if ( pGroup )
                        {
                            pGroup->Add ( pFile );
                        }
                    }

                    // Success. Return the file.
                    lua_pushelement ( luaVM, pFile );
                    return 1;
                }
                else
                {
                    // Delete the file again
                    delete pFile;

                    // Output error
                    argStream.SetCustomError( SString( "unable to load file '%s'", *filePath ) );
                }
            }
        }
    }
    if ( argStream.HasErrors () )
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() );

    // Failed
    lua_pushboolean ( luaVM, false );
    return 1;
}
Ejemplo n.º 20
0
int CLuaPickupDefs::createPickup ( lua_State* luaVM )
{
    // Grab all the argument types
    int iArgument1 = lua_type ( luaVM, 1 );
    int iArgument2 = lua_type ( luaVM, 2 );
    int iArgument3 = lua_type ( luaVM, 3 );
    int iArgument4 = lua_type ( luaVM, 4 );
    int iArgument5 = lua_type ( luaVM, 5 );
    int iArgument6 = lua_type ( luaVM, 6 );
    int iArgument7 = lua_type ( luaVM, 7 );

    // The first 6 are always numeric saying position, type and weapon/health/armor
    // TODO: Check argument 7 incase type is weapon
    if ( ( iArgument1 == LUA_TNUMBER || iArgument1 == LUA_TSTRING ) &&
         ( iArgument2 == LUA_TNUMBER || iArgument2 == LUA_TSTRING ) &&
         ( iArgument3 == LUA_TNUMBER || iArgument3 == LUA_TSTRING ) &&
         ( iArgument4 == LUA_TNUMBER || iArgument4 == LUA_TSTRING ) &&
         ( iArgument5 == LUA_TNUMBER || iArgument5 == LUA_TSTRING ) &&
         ( iArgument6 == LUA_TNUMBER || iArgument6 == LUA_TSTRING || iArgument6 == LUA_TNONE ) &&
         ( iArgument7 == LUA_TNUMBER || iArgument7 == LUA_TSTRING || iArgument7 == LUA_TNONE ) )
    {
        // Populate a position vector for it
        CVector vecPosition = CVector ( static_cast < float > ( lua_tonumber ( luaVM, 1 ) ),
                                        static_cast < float > ( lua_tonumber ( luaVM, 2 ) ),
                                        static_cast < float > ( lua_tonumber ( luaVM, 3 ) ) );

        // Is the type health or armor?
        unsigned long ulRespawnInterval = 30000;
        double dblAmmo = 50.0;
        if ( iArgument6 == LUA_TNUMBER || iArgument6 == LUA_TSTRING )
            ulRespawnInterval = static_cast < unsigned long > ( lua_tonumber ( luaVM, 6 ) );

        if ( iArgument7 == LUA_TNUMBER || iArgument7 == LUA_TSTRING )
            dblAmmo = lua_tonumber ( luaVM, 7 );

		CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
		if ( pLuaMain )
		{
			CResource* pResource = pLuaMain->GetResource();
			if ( pResource )
			{
				CPickup* pPickup = CStaticFunctionDefinitions::CreatePickup ( 
										pResource,
										vecPosition, 
										static_cast < unsigned char > ( lua_tonumber ( luaVM, 4 ) ), 
										lua_tonumber ( luaVM, 5 ), 
										ulRespawnInterval, 
										dblAmmo );
		        if ( pPickup )
		        {
					CElementGroup * pGroup = pResource->GetElementGroup();
					if ( pGroup )
					{
						pGroup->Add ( pPickup );
					}
					// Return the handle
					lua_pushelement ( luaVM, pPickup );
					return 1;
				}
			}
        }
    }
    else
        m_pScriptDebugging->LogBadType ( luaVM, "createPickup" );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Ejemplo n.º 21
0
int CLuaFileDefs::fileCreate ( lua_State* luaVM )
{
//  file fileCreate ( string filePath )
    SString strFile;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( strFile );

    if ( argStream.NextIsUserData () )
        m_pScriptDebugging->LogCustom ( luaVM, "fileCreate may be using an outdated syntax. Please check and update." );

    if ( !argStream.HasErrors () )
    {
        // Grab our lua VM
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            // Grab the filename
            std::string strAbsPath;
            std::string strSubPath;

            // We have a resource argument?
            CResource* pThisResource = pLuaMain->GetResource ();
            CResource* pResource = pThisResource;
            if ( CResourceManager::ParseResourcePathInput ( strFile, pResource, &strAbsPath, &strSubPath ) )
            {
                // Do we have permissions?
                if ( pResource == pThisResource ||
                     m_pACLManager->CanObjectUseRight ( pThisResource->GetName ().c_str (),
                                                        CAccessControlListGroupObject::OBJECT_TYPE_RESOURCE,
                                                        "ModifyOtherObjects",
                                                        CAccessControlListRight::RIGHT_TYPE_GENERAL,
                                                        false ) )
                {
                    // Make sure the destination folder exist so we can create the file
                    MakeSureDirExists ( strAbsPath.c_str () );
                    
                    // Create the file to create
                    CScriptFile* pFile = new CScriptFile ( pThisResource->GetScriptID(), strSubPath.c_str (), DEFAULT_MAX_FILESIZE );
                    assert ( pFile );

                    // Try to load it
                    if ( pFile->Load ( pResource, CScriptFile::MODE_CREATE ) )
                    {
                        // Add it to the scrpt resource element group
                        CElementGroup* pGroup = pThisResource->GetElementGroup ();
                        if ( pGroup )
                        {
                            pGroup->Add ( pFile );
                        }

                        // Success. Return the file.
                        lua_pushelement ( luaVM, pFile );
                        return 1;
                    }
                    else
                    {
                        // Delete the file again
                        delete pFile;

                        // Output error
                        m_pScriptDebugging->LogWarning ( luaVM, "%s; unable to load file", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ) );
                    }
                }
                else
                    m_pScriptDebugging->LogError ( luaVM, "%s failed; ModifyOtherObjects in ACL denied resource %s to access %s", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ), pThisResource->GetName ().c_str (), pResource->GetName ().c_str () );
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    // Failed
    lua_pushboolean ( luaVM, false );
    return 1;
}
Ejemplo n.º 22
0
int CLuaDatabaseDefs::DbConnect ( lua_State* luaVM )
{
    //  element dbConnect ( string type, string host, string username, string password, string options )
    SString strType; SString strHost; SString strUsername; SString strPassword; SString strOptions;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( strType );
    argStream.ReadString ( strHost );
    argStream.ReadString ( strUsername, "" );
    argStream.ReadString ( strPassword, "" );
    argStream.ReadString ( strOptions, "" );

    if ( !argStream.HasErrors () )
    {
        CResource* pThisResource = m_pLuaManager->GetVirtualMachineResource ( luaVM );
        if ( pThisResource )
        {
            // If type is sqlite, and has a host, try to resolve path
            if ( strType == "sqlite" && !strHost.empty () )
            {
                // If path starts with :/ then use global database directory
                if ( strHost.BeginsWith ( ":/" ) )
                {
                    strHost = strHost.SubStr ( 1 );
                    if ( !IsValidFilePath ( strHost ) )
                    {
                        argStream.SetCustomError( SString( "host path %s not valid", *strHost ) );
                    }
                    else
                    {
                        strHost = PathJoin ( g_pGame->GetConfig ()->GetGlobalDatabasesPath (), strHost );
                    }
                }
                else
                {
                    std::string strAbsPath;

                    // Parse path
                    CResource* pPathResource = pThisResource;
                    if ( CResourceManager::ParseResourcePathInput ( strHost, pPathResource, &strAbsPath ) )
                    {
                        strHost = strAbsPath;
                        CheckCanModifyOtherResource( argStream, pThisResource, pPathResource );
                    }
                    else
                    {
                        argStream.SetCustomError( SString( "host path %s not found", *strHost ) );
                    }
                }
            }

            if ( !argStream.HasErrors() )
            {
                if ( strType == "mysql" )
                    pThisResource->SetUsingDbConnectMysql( true );

                // Add logging options
                bool bLoggingEnabled;
                SString strLogTag;
                SString strQueueName;
                // Set default values if required
                GetOption < CDbOptionsMap > ( strOptions, "log", bLoggingEnabled, 1 );
                GetOption < CDbOptionsMap > ( strOptions, "tag", strLogTag, "script" );
                GetOption < CDbOptionsMap > ( strOptions, "queue", strQueueName, (strType == "mysql") ? strHost : DB_SQLITE_QUEUE_NAME_DEFAULT );
                SetOption < CDbOptionsMap > ( strOptions, "log", bLoggingEnabled );
                SetOption < CDbOptionsMap > ( strOptions, "tag", strLogTag );
                SetOption < CDbOptionsMap > ( strOptions, "queue", strQueueName );
                // Do connect
                SConnectionHandle connection = g_pGame->GetDatabaseManager ()->Connect ( strType, strHost, strUsername, strPassword, strOptions );
                if ( connection == INVALID_DB_HANDLE )
                {
                    argStream.SetCustomError( g_pGame->GetDatabaseManager()->GetLastErrorMessage() );
                }
                else
                {
                    // Use an element to wrap the connection for auto disconnected when the resource stops
                    // Don't set a parent because the element should not be accessible from other resources
                    CDatabaseConnectionElement* pElement = new CDatabaseConnectionElement ( NULL, connection );
                    CElementGroup * pGroup = pThisResource->GetElementGroup ();
                    if ( pGroup )
                    {
                        pGroup->Add ( pElement );
                    }

                    lua_pushelement ( luaVM, pElement );
                    return 1;
                }
            }
        }
    }

    if ( argStream.HasErrors() )
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Ejemplo n.º 23
0
int CLuaFileDefs::fileOpen ( lua_State* luaVM )
{
    //  file fileOpen ( string filePath [, bool readOnly = false ] )
    SString strInputPath; bool bReadOnly;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( strInputPath );
    argStream.ReadBool ( bReadOnly, false );

    if ( argStream.NextIsUserData () )
        m_pScriptDebugging->LogCustom ( luaVM, "fileOpen may be using an outdated syntax. Please check and update." );

    if ( !argStream.HasErrors () )
    {
        // Grab our lua VM
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            SString strAbsPath;
            SString strMetaPath;
            CResource* pThisResource = pLuaMain->GetResource ();
            CResource* pResource = pThisResource;
            if ( CResourceManager::ParseResourcePathInput ( strInputPath, pResource, &strAbsPath, &strMetaPath ) )
            {
                CheckCanModifyOtherResource( argStream, pThisResource, pResource );
                CheckCanAccessOtherResourceFile( argStream, pThisResource, pResource, strAbsPath, &bReadOnly );
                if ( !argStream.HasErrors() )
                {
#ifndef MTA_CLIENT // IF SERVER
                    // Create the file to create
                    CScriptFile* pFile = new CScriptFile ( pThisResource->GetScriptID (), strMetaPath, DEFAULT_MAX_FILESIZE );
#else
                    eAccessType accessType = strInputPath[0] == '@' ? eAccessType::ACCESS_PRIVATE : eAccessType::ACCESS_PUBLIC;
                    CScriptFile* pFile = new CScriptFile ( pThisResource->GetScriptID (), strMetaPath, DEFAULT_MAX_FILESIZE, accessType );                
#endif
                    // Try to load it
                    if ( pFile->Load ( pResource, bReadOnly ? CScriptFile::MODE_READ : CScriptFile::MODE_READWRITE ) )
                    {
#ifdef MTA_CLIENT
                        // Make it a child of the resource's file root
                        pFile->SetParent ( pResource->GetResourceDynamicEntity () );
#endif
                        // Grab its owner resource
                        CResource* pParentResource = pLuaMain->GetResource ();
                        if ( pParentResource )
                        {
                            // Add it to the scrpt resource element group
                            CElementGroup* pGroup = pParentResource->GetElementGroup ();
                            if ( pGroup )
                            {
                                pGroup->Add ( pFile );
                            }
                        }

                        // Success. Return the file.
                        lua_pushelement ( luaVM, pFile );
                        return 1;
                    }
                    else
                    {
                        // Delete the file again
                        delete pFile;

                        // Output error
                        argStream.SetCustomError ( SString ( "unable to load file '%s'", *strInputPath ) );
                    }
                }
            }
        }
    }

    if ( argStream.HasErrors () )
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    // Failed
    lua_pushboolean ( luaVM, false );
    return 1;
}
Ejemplo n.º 24
0
int CLuaFunctionDefs::CreateRadarArea ( lua_State* luaVM )
{
    int iArgument1 = lua_type ( luaVM, 1 );
    int iArgument2 = lua_type ( luaVM, 2 );
    int iArgument3 = lua_type ( luaVM, 3 );
    int iArgument4 = lua_type ( luaVM, 4 );
    int iArgument5 = lua_type ( luaVM, 5 );
    int iArgument6 = lua_type ( luaVM, 6 );
    int iArgument7 = lua_type ( luaVM, 7 );
    int iArgument8 = lua_type ( luaVM, 8 );

    if ( ( iArgument1 == LUA_TNUMBER || iArgument1 == LUA_TSTRING ) &&
        ( iArgument2 == LUA_TNUMBER || iArgument2 == LUA_TSTRING ) &&
        ( iArgument3 == LUA_TNUMBER || iArgument3 == LUA_TSTRING ) &&
        ( iArgument4 == LUA_TNUMBER || iArgument4 == LUA_TSTRING ) &&
        ( iArgument5 == LUA_TNUMBER || iArgument5 == LUA_TSTRING || iArgument5 == LUA_TNONE ) &&
        ( iArgument6 == LUA_TNUMBER || iArgument6 == LUA_TSTRING || iArgument6 == LUA_TNONE ) &&
        ( iArgument7 == LUA_TNUMBER || iArgument7 == LUA_TSTRING || iArgument7 == LUA_TNONE ) &&
        ( iArgument8 == LUA_TNUMBER || iArgument8 == LUA_TSTRING || iArgument8 == LUA_TNONE ) )
    {
        CVector2D vecPosition = CVector2D ( static_cast < float > ( lua_tonumber ( luaVM, 1 ) ), static_cast < float > ( lua_tonumber ( luaVM, 2 ) ) );
        CVector2D vecSize = CVector2D ( static_cast < float > ( lua_tonumber ( luaVM, 3 ) ), static_cast < float > ( lua_tonumber ( luaVM, 4 ) ) );

        double dRed = 255;
        double dGreen = 0;
        double dBlue = 0;
        double dAlpha = 255;
        if ( iArgument5 != LUA_TNONE && iArgument6 != LUA_TNONE && iArgument7 != LUA_TNONE && iArgument8 != LUA_TNONE )
        {
            dRed = lua_tonumber ( luaVM, 5 );
            dGreen = lua_tonumber ( luaVM, 6 );
            dBlue = lua_tonumber ( luaVM, 7 );
            dAlpha = lua_tonumber ( luaVM, 8 );
        }


        if ( dRed >= 0 && dRed <= 255 &&
            dGreen >= 0 && dGreen <= 255 &&
            dBlue >= 0 && dBlue <= 255 &&
            dAlpha >= 0 && dAlpha <= 255 )
        {     
            SColor color;
            color.R = static_cast < unsigned char > ( dRed );
            color.G = static_cast < unsigned char > ( dGreen );
            color.B = static_cast < unsigned char > ( dBlue );
            color.A = static_cast < unsigned char > ( dAlpha );

            CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
            if ( pLuaMain )
            {
                CResource* pResource = pLuaMain->GetResource ();
                if ( pResource )
                {
                    // Create it
                    CClientRadarArea* pRadarArea = CStaticFunctionDefinitions::CreateRadarArea ( *pResource, vecPosition, vecSize, color );
                    if ( pRadarArea )
                    {
                        CElementGroup * pGroup = pResource->GetElementGroup();
                        if ( pGroup )
                        {
                            pGroup->Add ( ( CClientEntity* ) pRadarArea );
                        }
                        lua_pushelement ( luaVM, pRadarArea );
                        return 1;
                    }
                }
            }
        }
        else
            m_pScriptDebugging->LogWarning ( luaVM, "Bad color number sent to createRadarArea (0-255)" );
    }
    else
        m_pScriptDebugging->LogBadType ( luaVM, "createRadarArea" );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Ejemplo n.º 25
0
int CLuaFunctionDefs::ProcessLineOfSight ( lua_State * luaVM )
{
    int iArgument1 = lua_type ( luaVM, 1 );
    int iArgument2 = lua_type ( luaVM, 2 );
    int iArgument3 = lua_type ( luaVM, 3 );
    int iArgument4 = lua_type ( luaVM, 4 );
    int iArgument5 = lua_type ( luaVM, 5 );
    int iArgument6 = lua_type ( luaVM, 6 );
    if ( ( iArgument1 == LUA_TNUMBER || iArgument1 == LUA_TSTRING ) &&
        ( iArgument2 == LUA_TNUMBER || iArgument2 == LUA_TSTRING ) &&
        ( iArgument3 == LUA_TNUMBER || iArgument3 == LUA_TSTRING ) &&
        ( iArgument4 == LUA_TNUMBER || iArgument4 == LUA_TSTRING ) &&
        ( iArgument5 == LUA_TNUMBER || iArgument5 == LUA_TSTRING ) &&
        ( iArgument6 == LUA_TNUMBER || iArgument6 == LUA_TSTRING ) )
    {
        CVector vecStart ( static_cast < float > ( lua_tonumber ( luaVM, 1 ) ),
            static_cast < float > ( lua_tonumber ( luaVM, 2 ) ),
            static_cast < float > ( lua_tonumber ( luaVM, 3 ) ) );
        CVector vecEnd (   static_cast < float > ( lua_tonumber ( luaVM, 4 ) ),
            static_cast < float > ( lua_tonumber ( luaVM, 5 ) ),
            static_cast < float > ( lua_tonumber ( luaVM, 6 ) ) );

        CColPoint* pColPoint = NULL;
        CClientEntity* pColEntity = NULL;
        bool bCheckBuildings = true, bCheckVehicles = true, bCheckPeds = true, bCheckObjects = true, bCheckDummies = true, bSeeThroughStuff = false, bIgnoreSomeObjectsForCamera = false, bShootThroughStuff = false;
        CEntity* pIgnoredEntity = NULL;

        if ( lua_type ( luaVM, 7 ) == LUA_TBOOLEAN )
        {
            bCheckBuildings = ( lua_toboolean ( luaVM, 7 ) ) ? true:false;
            if ( lua_type ( luaVM, 8 ) == LUA_TBOOLEAN )
            {
                bCheckVehicles = ( lua_toboolean ( luaVM, 8 ) ) ? true:false;
                if ( lua_type ( luaVM, 9 ) == LUA_TBOOLEAN )
                {
                    bCheckPeds = ( lua_toboolean ( luaVM, 9 ) ) ? true:false;
                    if ( lua_type ( luaVM, 10 ) == LUA_TBOOLEAN )
                    {
                        bCheckObjects = ( lua_toboolean ( luaVM, 10 ) ) ? true:false;
                        if ( lua_type ( luaVM, 11 ) == LUA_TBOOLEAN )
                        {
                            bCheckDummies = ( lua_toboolean ( luaVM, 11 ) ) ? true:false;
                            if ( lua_type ( luaVM, 12 ) == LUA_TBOOLEAN )
                            {
                                bSeeThroughStuff = ( lua_toboolean ( luaVM, 12 ) ) ? true:false;
                                if ( lua_type ( luaVM, 13 ) == LUA_TBOOLEAN )
                                {
                                    bIgnoreSomeObjectsForCamera = ( lua_toboolean ( luaVM, 13 ) ) ? true:false;
                                    if ( lua_type ( luaVM, 14 ) == LUA_TBOOLEAN )
                                    {
                                        bShootThroughStuff = ( lua_toboolean ( luaVM, 14 ) ) ? true:false;

                                        if ( lua_type ( luaVM, 15 ) == LUA_TLIGHTUSERDATA )
                                        {
                                            CClientEntity* pEntity = lua_toelement ( luaVM, 15 );
                                            if ( pEntity )
                                            {
                                                switch ( pEntity->GetType () )
                                                {
                                                case CCLIENTPED:
                                                case CCLIENTPLAYER:
                                                    pIgnoredEntity = static_cast < CClientPed* > ( pEntity )->GetGamePlayer ();
                                                    break;
                                                case CCLIENTVEHICLE:
                                                    pIgnoredEntity = static_cast < CClientVehicle* > ( pEntity )->GetGameVehicle ();
                                                    break;
                                                case CCLIENTOBJECT:
                                                    pIgnoredEntity = static_cast < CClientObject* > ( pEntity )->GetGameObject ();
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        bool bCollision;
        if ( CStaticFunctionDefinitions::ProcessLineOfSight ( vecStart, vecEnd, bCollision, &pColPoint, &pColEntity, bCheckBuildings, bCheckVehicles, bCheckPeds, bCheckObjects, bCheckDummies, bSeeThroughStuff, bIgnoreSomeObjectsForCamera, bShootThroughStuff, pIgnoredEntity ) )
        {    
            // Got a collision?
            CVector vecColPosition;
            if ( pColPoint )
            {
                // Get the collision position
                vecColPosition = *pColPoint->GetPosition ();

                // Delete the colpoint
                pColPoint->Destroy ();
            }

            lua_pushboolean ( luaVM, bCollision );
            if ( bCollision )
            {
                lua_pushnumber ( luaVM, vecColPosition.fX );
                lua_pushnumber ( luaVM, vecColPosition.fY );
                lua_pushnumber ( luaVM, vecColPosition.fZ );
                if ( pColEntity )
                    lua_pushelement ( luaVM, pColEntity );
                else
                    lua_pushnil ( luaVM );
                return 5;
            }
            return 1;
        }
    }
    else
        m_pScriptDebugging->LogBadType ( luaVM, "processLineOfSight" );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Ejemplo n.º 26
0
int CLuaBlipDefs::CreateBlip(lua_State* luaVM)
{
    CVector          vecPosition;
    unsigned char    ucIcon = 0;
    int              iSize = 2;
    SColorRGBA       color(255, 0, 0, 255);
    int              iOrdering = 0;
    int              iVisibleDistance = 16383;
    CScriptArgReader argStream(luaVM);
    argStream.ReadVector3D(vecPosition);
    argStream.ReadNumber(ucIcon, 0);
    argStream.ReadNumber(iSize, 2);
    argStream.ReadNumber(color.R, 255);
    argStream.ReadNumber(color.G, 0);
    argStream.ReadNumber(color.B, 0);
    argStream.ReadNumber(color.A, 255);
    argStream.ReadNumber(iOrdering, 0);
    argStream.ReadNumber(iVisibleDistance, 16383);

    if (!CClientRadarMarkerManager::IsValidIcon(ucIcon))
    {
        argStream.SetCustomError("Invalid icon");
    }

    if (iSize < 0 || iSize > 25)
        argStream.SetCustomWarning(SString("Blip size beyond 25 is no longer supported (got %i). It will be clamped between 0 and 25.", iSize));

    if (!argStream.HasErrors())
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
        if (pLuaMain)
        {
            CResource* pResource = pLuaMain->GetResource();
            if (pResource)
            {
                unsigned char  ucSize = Clamp(0, iSize, 25);
                short          sOrdering = Clamp(-32768, iOrdering, 32767);
                unsigned short usVisibleDistance = Clamp(0, iVisibleDistance, 65535);

                // Create the blip
                CClientRadarMarker* pMarker =
                    CStaticFunctionDefinitions::CreateBlip(*pResource, vecPosition, ucIcon, ucSize, color, sOrdering, usVisibleDistance);
                if (pMarker)
                {
                    CElementGroup* pGroup = pResource->GetElementGroup();
                    if (pGroup)
                    {
                        pGroup->Add(pMarker);
                    }

                    lua_pushelement(luaVM, pMarker);
                    return 1;
                }
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());

    lua_pushboolean(luaVM, false);
    return 1;
}