Not signed in (Sign In)

Discussion Tag Cloud

Categories

Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthorR3bel1
    • CommentTimeJul 19th 2016
     
    I have an object that has five total lights, the default lightscript only affects the first light listed as "thelight" Is there a way to make the script repeat four more times so I don't have to edit new scripts for "thelight2" ...3..4....5...so on? Help would be greatly appreciated.
    •  
      CommentAuthorbestdani
    • CommentTimeJul 19th 2016 edited
     
    Yes you can make the script detect all lights that follow the "thelightX" name scheme (thelight0, thelight1, thelight2, ...) it's a rather simple task if you have some programming experience, this is a possible solution:

    (Save as LightScriptExtended.nlvm)

    import com.nolimitscoaster.*;
    import nlvm.math3d.*;
    import nlvm.util.Vector;

    /**
    * This script simulates simple lights, that switches on when the sun sets
    */
    public class LightScriptExtended extends Script
    {
    private static final String c_aLightSourceName = "thelight";
    private static final double c_fSwitchOnAngle = 18.0; // degrees
    private static final float c_fMaxInitialDelay = 0.6f; // seconds
    private static final float c_fSwitchOnTime = 10.0f; // seconds
    private static final float c_fSwitchOffTime = 1.3f; // seconds
    private static final float c_fStartBrightness = 0.2f;

    private Vector m_vLights;
    private SceneObject m_aSCO;
    private float m_fInitialDelayTime;
    private float m_fCurBrightness;
    private float m_fNewBrightness;
    private float m_fElevationLimit;
    private Vector3f m_aOrgColor;
    private Vector4f m_aEntCol;
    private bool m_bEnabled;

    public bool onInit()
    {
    m_aSCO = sim.getSceneObjectForEntityId(getParentEntityId());
    m_vLights = new Vector();
    int iLight = 0;
    SceneObjectLight aLight, aLastLight = null;
    while((aLight = m_aSCO.getLightForName(c_aLightSourceName+iLight++)) != null)
    {
    aLight.setEnabled(false);
    m_vLights.addElement(aLight);
    aLastLight = aLight;
    }
    if (aLastLight == null)
    {
    System.err.println("Scene object has no light with name scheme '" + c_aLightSourceName + "X', X = 0, 1, 2, ...");
    return false;
    }

    m_fCurBrightness = 0;
    m_fNewBrightness = 0;
    m_bEnabled = false;

    m_aOrgColor = new Vector3f();
    aLastLight.getColor(m_aOrgColor);

    m_aEntCol = new Vector4f(1,1,1,0);

    m_fElevationLimit = (float)Math.toRadians(c_fSwitchOnAngle);
    m_fInitialDelayTime = (float)Math.random() * c_fMaxInitialDelay;

    return true;
    }

    public void onNextFrame(float tick)
    {
    bool bEnable = sim.getCurSunElevation() <= m_fElevationLimit;

    if (bEnable != m_bEnabled)
    {
    if (bEnable)
    {
    // switching on
    m_fCurBrightness = c_fStartBrightness;
    m_fNewBrightness = 1.0f;
    m_bEnabled = true;
    }
    else
    {
    // switching off
    m_fNewBrightness = 0.0f;
    m_bEnabled = false;
    }
    }

    if (m_fCurBrightness < m_fNewBrightness)
    {
    // switching on
    m_fCurBrightness += tick / c_fSwitchOnTime;
    if (m_fCurBrightness > m_fNewBrightness)
    {
    m_fCurBrightness = m_fNewBrightness;
    }
    }
    else if (m_fCurBrightness > m_fNewBrightness)
    {
    // switching off
    m_fCurBrightness -= tick / c_fSwitchOffTime;
    if (m_fCurBrightness < m_fNewBrightness)
    {
    m_fCurBrightness = m_fNewBrightness;
    }
    }

    bEnable = m_fCurBrightness > 0;


    if (bEnable)
    {
    SceneObjectLight aLight;
    int n = m_vLights.size();
    for(int i = n - 1; i >= 0; --i)
    {
    aLight = (SceneObjectLight) m_vLights.elementAt(i);
    aLight.setEnabled(true);
    aLight.setColor(m_fCurBrightness * m_aOrgColor.x, m_fCurBrightness * m_aOrgColor.y, m_fCurBrightness * m_aOrgColor.z);
    }

    }else{
    SceneObjectLight aLight;
    int n = m_vLights.size();
    for(int i = n - 1; i >= 0; --i)
    {
    aLight = (SceneObjectLight) m_vLights.elementAt(i);
    aLight.setEnabled(false);
    }
    }

    // This controls the fading between normal object color and completely white, using a special material (fade_to_white)
    m_aEntCol.w = m_fCurBrightness;
    m_aSCO.setEntityColor(m_aEntCol);
    }
    }
    • CommentAuthorR3bel1
    • CommentTimeJul 21st 2016
     
    Just tested this script when I got off work. Thanks!