Script on Class 91/Mk3 DVT

Locos, DMUs, EMUs, Carriages etc.
Post Reply
User avatar
PFX
Forum Veteran
Posts: 2086
Joined: Mon Dec 13, 2010 3:12 pm
Trainz Version: TRS22
Trainz Build: 119450
Location: Béal Feirste

Script on Class 91/Mk3 DVT

Post by PFX »

I was wondering if anyone would know how to correct the lighting on Peter Hicks' Mk3 DVT, or indeed if it is possible at all?

Currently both the headlights, marker lights and tail lights are all on regardless of direction. I don't want to start messing with script as I don't know how. Is it simply a matter of a change to script or is there something more complicated required?

The other issue appears to be on the Class 91 itself. The pantograph doesn't raise or lower though the spark effect appears when running. Is there a possible fix or do I need to put up with it?

Cheers,
Innis
Image
User avatar
ScottAS2
Past 100!
Posts: 125
Joined: Sun Nov 24, 2013 12:44 am
Trainz Version: TANE
Trainz Build: 95078
Author KUID: 306203

Re: Script on Class 91/Mk3 DVT

Post by ScottAS2 »

PFX wrote:Peter Hicks' Mk3 DVT
I'm assuming from the reference to Peter Hicks and the Class 91 you mean his Mk4 DVT. Ignore at least half of below if you weren't!
PFX wrote:Currently both the headlights, marker lights and tail lights are all on regardless of direction.

[...]

The other issue appears to be on the Class 91 itself. The pantograph doesn't raise or lower though the spark effect appears when running.
I remember having both of these problems when I first tried the Intercity 225 in TANE, but now I have neither, so it's fixable. Unfortunately, I don't remember what I did that fixed it. :oops: With the DVT, a whole host of strange behaviours disappear when you set the "engine" tag to "1", so try that first if you haven't already done it. I also changed the enginespec to <kuid:45317:102635> (Nexusdj's Mk3 DVT enginespec, rather than a class 89(!) one) and changed "*.tga" to "*.texture" to stop content manager moaning at me.

For the Class 91, the only change I've made to the config file was the ".tga" to ".texture" replacement. I think it's unlikely that'll fix the pantograph on its own, but it's the only change I have found so far. I didn't change any of the scripts, or make any changes to the pantograph asset itself.

There are the changes I've found by running around diff-ing the most likely files from my current version of Trainz to the originals, but if none of these work, I'll try to go through more systematically when I have some more time.
User avatar
Briggsy
Forum Veteran
Posts: 1523
Joined: Thu Feb 16, 2012 1:02 am
Trainz Version: TRS2019 + TANE SP3
Trainz Build: 105096
Author KUID: 630773
Location: Coventry (West Midlands)

Re: Script on Class 91/Mk3 DVT

Post by Briggsy »

PFX wrote:I was wondering if anyone would know how to correct the lighting on Peter Hicks' Mk3 DVT, or indeed if it is possible at all?

Currently both the headlights, marker lights and tail lights are all on regardless of direction. I don't want to start messing with script as I don't know how. Is it simply a matter of a change to script or is there something more complicated required?

The other issue appears to be on the Class 91 itself. The pantograph doesn't raise or lower though the spark effect appears when running. Is there a possible fix or do I need to put up with it?

Cheers,
Innis
It's definitely fixable because I repaired mine a long time ago.

I can't remember what I did now to repair it - but if you can hang on until the weekend when I have free time, I'll have a look for you. It certainly wasn't anything too drastic because I'm useless with scripts, etc.

From memory, I think I imported it back into TS12, ran it through one of the Pevsoft tools that cleaned up the coding, shifted it back into TANE - and then most of it had corrected itself.

I'll have a look at the weekend for you and report back.
User avatar
PFX
Forum Veteran
Posts: 2086
Joined: Mon Dec 13, 2010 3:12 pm
Trainz Version: TRS22
Trainz Build: 119450
Location: Béal Feirste

Re: Script on Class 91/Mk3 DVT

Post by PFX »

Scott, thanks for the reply and yes, I did mean the Mk4 DVT.

Briggsy, any help is appreciated and there isn't any rush.

Cheers,
Innis
Image
User avatar
Briggsy
Forum Veteran
Posts: 1523
Joined: Thu Feb 16, 2012 1:02 am
Trainz Version: TRS2019 + TANE SP3
Trainz Build: 105096
Author KUID: 630773
Location: Coventry (West Midlands)

Re: Script on Class 91/Mk3 DVT

Post by Briggsy »

Just remembered I completely forgot about this.

Will take a look for you and get back to you this weekend. I've written a reminder down this time so that I won't forget again.
User avatar
cyberdonblue
Forum Veteran
Posts: 1578
Joined: Mon Mar 31, 2014 6:41 pm
Trainz Version: 2006 2012 T:ANE SP3
Trainz Build: 105766
Author KUID: 214658
Location: West Midlands

Re: Script on Class 91/Mk3 DVT

Post by cyberdonblue »

Briggsy wrote:Just remembered I completely forgot about this.

Will take a look for you and get back to you this weekend. I've written a reminder down this time so that I won't forget again.
Errr... Forget what?? :? Trainz? Never heard of it! :shock:

Dave :lol:
User avatar
Briggsy
Forum Veteran
Posts: 1523
Joined: Thu Feb 16, 2012 1:02 am
Trainz Version: TRS2019 + TANE SP3
Trainz Build: 105096
Author KUID: 630773
Location: Coventry (West Midlands)

Re: Script on Class 91/Mk3 DVT

Post by Briggsy »

cyberdonblue wrote:
Briggsy wrote:Just remembered I completely forgot about this.

Will take a look for you and get back to you this weekend. I've written a reminder down this time so that I won't forget again.
Errr... Forget what?? :? Trainz? Never heard of it! :shock:

Dave :lol:
Who are you again? ;-)
User avatar
Briggsy
Forum Veteran
Posts: 1523
Joined: Thu Feb 16, 2012 1:02 am
Trainz Version: TRS2019 + TANE SP3
Trainz Build: 105096
Author KUID: 630773
Location: Coventry (West Midlands)

Re: Script on Class 91/Mk3 DVT

Post by Briggsy »

Innis - I've checked my MK4 DVT for the tail-light issue - and I was correct. I have fixed it in the past.

I can either send you my repaired version of the working script file so that you can simply overwrite your file - or if you're comfortable doing so, I can paste the text from the script file below so that you can can 'cut and paste' it into your current script file. Whichever suits you?

I'll check the Pantograph issue now too - I suspect that's linked to the same script. I'll report back in a few minutes....

EDIT: Checked the pantograph - and that also works fine on my 91 - so I can also send you the working script for that, or post the code below. Whichever suits you.
User avatar
Nexusdj
Forum Veteran
Posts: 3096
Joined: Sun Dec 12, 2010 3:02 pm
Trainz Version: TRS22
Trainz Build: 123794
Author KUID: 45317
Location: West Midlands
Contact:

Re: Script on Class 91/Mk3 DVT

Post by Nexusdj »

Using the wayback machine I've just downloaded a raw version of the GNER mk4 pack and overwrote the existing versions I have in TANE . Looking through the files the only problem I can see is the engine tag on the DVT is set to 0 instead of 1 which conflicts with this entry in the DVT's script :

Code: Select all

class mk4_dvt isclass Locomotive
That "engine" entry in the config file needs to be changed to 1

Tested in TANE with no faults after the config entry was amended .
High visibility pixels must be worn when on or about the line !!
User avatar
PFX
Forum Veteran
Posts: 2086
Joined: Mon Dec 13, 2010 3:12 pm
Trainz Version: TRS22
Trainz Build: 119450
Location: Béal Feirste

Re: Script on Class 91/Mk3 DVT

Post by PFX »

Nexus, Briggsy,

Thanks for the solutions and yes Briggsy, if you paste the lighting script I can stick it in the one I have no problem.

While I'm trying to fix things, I can't remember if I asked before about fixing running numbers on rolling stock? If so, apologies but if not, when I add stock (specifically snowsignal's 1xx and 3xx DMUs and EMUs) all that displays is a row of 7s with a blue background. I have a vague recollection of repairing this once before but the method escapes me.

Cheers,
Innis
Image
User avatar
Briggsy
Forum Veteran
Posts: 1523
Joined: Thu Feb 16, 2012 1:02 am
Trainz Version: TRS2019 + TANE SP3
Trainz Build: 105096
Author KUID: 630773
Location: Coventry (West Midlands)

Re: Script on Class 91/Mk3 DVT

Post by Briggsy »

Really sorry for forgetting about this again mate. I'm terrible at remembering things.

Here's the script for the full MK4 DVT - just delete everything from your current script file - and cut and paste this into it instead:

Code: Select all

include "locomotive.gs"
include "library.gs"
include "asset.gs"

/*
   LightUp, NumberIt script
   Author: Dave Renshaw (eldavo)
   Copyright Dave Renshaw 2005. All rights reserved.
*/

class mk4_dvt isclass Locomotive
{
    Library numberit;

    // We have to first define the methods within the script class...
    //
    // These methods handle the different things we want to control
    void SetLights();

    //
    // These are handlers that listen for messages from the scripting environment and other objects
    void TrainHandler(Message msg);
    void CouplingHandler(Message msg);
    //
    // Finally a method that runs as a thread and checks the state of things periodically
    thread void ControlMonitor();


    // Define a flag to turn debugging information on and off. Set this to "true" to enable output of debug info.
//    bool debug = true;
    bool debug = false;

    //
    // The train we are currently part of
    Train currentTrain;

    // Generally useful stuff
    //
    Asset red;
    Asset white;
    Asset driver;
    Asset blind;

    // The "init" method is called whenever a new vehicle of this type is created
    //
        void Init(void) {
        inherited();
        
        // LIGHTUP SCRIPT START
        if (debug) {Interface.Log("MallardDVT.Init() [v1.0] my name is '"+GetName()+"' and id is '"+GetId()+"'");}

        // We always call our superclass (Locomotive) init method to ensure all the behaviour we wish to
        // inherit from it has been correctly initialised.
        inherited();

        red = me.GetAsset().FindAsset("corona-red");
        if (red == null) {
            Interface.Log("MallardDVT.Init() - can't find corona asset 'corona_red'!");
            return;
        }

        white = me.GetAsset().FindAsset("corona-white");
        if (white == null) {
            Interface.Log("MallardDVT.Init() - can't find corona asset 'corona_white'!");
            return;
        }
        
        driver = me.GetAsset().FindAsset("cab_driver");
        if (driver == null) {
            Interface.Log("MallardDVT.Init() - can't find driver asset 'cab_driver'!");
            return;
        }
        
        blind = me.GetAsset().FindAsset("cab_blind");
        if (blind == null) {
            Interface.Log("MallardDVT.Init() - can't find driver asset 'cab_blind'!");
            return;
        }

        // Listen for a few interesting messages..
        //
        AddHandler(me, "Train", null, "TrainHandler");
        AddHandler(me, "Vehicle", null, "CouplingHandler");
        currentTrain = me.GetMyTrain();

        // if we have a train object we will sniff it for events...
        if (currentTrain!= null) {
            if (debug) {Interface.Log("LightUp.Init() - Train object is not null");}
            me.Sniff(currentTrain, "Train", "StartedMoving", true);
        }

        // Set up the initial state of everything...
        SetLights();

        // Kick off a monitoring thread...
        ControlMonitor();
        // LIGHTUP SCRIPT END

        // NUMBERIT SCRIPT START
        // Pick a running number if one hasn't already been set
        numberit = World.GetLibrary(GetAsset().LookupKUIDTable("NumberIt_library"));
        if (numberit) {
            GSObject[] objectParam = new GSObject[1];
            objectParam[0] = me;
            numberit.LibraryCall("SetRunningNumber", null, objectParam);
        }
        // NUMBERIT SCRIPT END
    }
    
    // LIGHTUP FUNCTIONS START
        // We have a handler for train messages so we can see when the train starts moving and adjust the
    // various settings
    void TrainHandler(Message msg) {
        if (debug) {
            Interface.Log("LightUp.TrainHandler(major='"+msg.major+"' minor='"+msg.minor+"'");
            if (msg.src == null) {
                Interface.Log("                        src='null')");
            } else {
                GameObject srco = cast <GameObject> msg.src;
                Interface.Log("                        src='"+srco.GetName()+"')");
            }
            Interface.Log("                        me='"+me.GetName()+"'");
        }

        if (msg.minor == "StartedMoving") {
            SetLights();
        }
    }

    // We have a handler for vehicle messages so we can see when we are coupled and uncoupled and can adjust the
    // various settings
    void CouplingHandler(Message msg) {
        if (debug) {
            Interface.Log("LightUp.CouplingHandler(major='"+msg.major+"' minor='"+msg.minor+"'");
            if (msg.src == null) {
                Interface.Log("                        src='null')");
            } else {
                GameObject srco = cast <GameObject> msg.src;
                Interface.Log("                        src='"+srco.GetName()+"')");
            }
            Interface.Log("                        me='"+me.GetName()+"'");
        }

        if (msg.minor == "Coupled" or msg.minor == "Collided") {
            if (msg.src == me) {

                // Adjust the train sniffer to ensure we are watching the right one...
                if (currentTrain != null) {
                    me.Sniff(currentTrain, "Train", "StartedMoving", false);
                }
                currentTrain = me.GetMyTrain();
                if (currentTrain != null) {
                    me.Sniff(currentTrain, "Train", "StartedMoving", true);
                }

                SetLights();
            }

        } else if (msg.minor == "Decoupled") {

            // Adjust the train sniffer to ensure we are watching the right one...
            if (currentTrain != null) {
                me.Sniff(currentTrain, "Train", "StartedMoving", false);
            }
            currentTrain = me.GetMyTrain();
            if (currentTrain != null) {
                me.Sniff(currentTrain, "Train", "StartedMoving", true);
            }

            SetLights();
        }
    }


    thread void ControlMonitor() {
        if (debug) {Interface.Log("LightUp.ControlMonitor()");}

        // the state of the current module
        int oldModule = World.GetCurrentModule();

        // the state of the weather
        int oldWeather = World.GetWeatherType();

        // the state of the headlight
        float oldheadlight = 0.0;

        while (true) {

            // Have we changed modules?
            int newModule = World.GetCurrentModule();
            if (newModule != oldModule) {
                if (debug) {Interface.Log("LightUp.ControlMonitor() - change module from "+oldModule+" to "+newModule);}
                if (newModule == World.DRIVER_MODULE) {
                    SetLights();
                }
                oldModule = newModule;
            }

            if (newModule == World.DRIVER_MODULE) {
                // what's the state of the headlight?
                float headlight = me.GetEngineSetting("headlight");
                int newWeather = World.GetWeatherType();
                if (headlight != oldheadlight or newWeather != oldWeather) {
                    // looks like the headlight has been turned on or off...
                    // or the weather has changed - wipers possibly required...
                    me.SetLights();
                    oldheadlight = headlight;
                    oldWeather = newWeather;
                }
            }

            Sleep(5);
        }
    }

    void SetLights() {
        if (debug) {Interface.Log("LightUp.SetLights()");}


        // turn off all the lights
        // turn headlights and markers off at A end
        me.SetFXCoronaTexture("marker_a0", null);
        me.SetFXCoronaTexture("marker_a1", null);
        me.SetFXCoronaTexture("head_a0", null);
        me.SetFXCoronaTexture("head_a1", null);
        // turn off all tail lights
        me.SetFXCoronaTexture("tail_a0", null);
        me.SetFXCoronaTexture("tail_a1", null);
        // remove driver
        me.SetFXAttachment("driver", null);
        me.SetFXAttachment("blind", null);
        // halt wiper animation
        me.StopMeshAnimation("wipers_a");

        // if we are not in Driver or Surveyor then display a default light pattern...
        if (debug) {Interface.Log("LightUp.SetLights() current module="+World.GetCurrentModule());}
        if (World.GetCurrentModule() != World.DRIVER_MODULE) {
            if (debug) {Interface.Log("LightUp.SetLights - Not in Driver. Setting default light pattern");}
            me.SetFXCoronaTexture("head_a0", white);
            me.SetFXCoronaTexture("head_a1", white);
            me.SetFXCoronaTexture("marker_a0", white);
            me.SetFXCoronaTexture("marker_a1", white);
            return;
        }

        // which way round are we?
        float reverser = me.GetEngineSetting("reverser");
        if (debug) {
            Interface.Log("LightUp.SetLights() reverser = "+reverser);
            Interface.Log("LightUp.SetLights() me.GetDirectionRelativeToTrain() = "+me.GetDirectionRelativeToTrain());
        }

        float time = World.GetGameTime();
        int weather = World.GetWeatherType();
        if (debug) {Interface.Log("LightUp.SetLights() time = "+time+", and weather = "+weather);}
        bool night = (time > 0.25 and time < 0.75);

        float headlight = me.GetEngineSetting("headlight");
        if (debug) {Interface.Log("LightUp.SetLights() headlight = "+headlight);}

        Train t = me.GetMyTrain();
        Vehicle[] vs = t.GetVehicles();

        // are we on the front of the train?
        if (vs[0] == me) {
            if (debug) {Interface.Log("LightUp.SetLights() On the front");}
            // Are we going forward or backward?
            if (reverser >= 1.0) {
                // So we are at the front going forward
                // turn markers on at A end
                me.SetFXCoronaTexture("marker_a0", white);
                me.SetFXCoronaTexture("marker_a1", white);
                if (headlight > 0.0) {
                    me.SetFXCoronaTexture("head_a1", white);
                    me.SetFXCoronaTexture("head_a0", white);
                }
                // Add driver
                me.SetFXAttachment("driver", driver);
                // Turn on windscreen wipers in bad weather
                if (weather > 1) {
                    me.StartMeshAnimationLoop("wipers_a");
                }
             } else {
                // turn tail lights on at A end
                me.SetFXCoronaTexture("tail_a0", red);
                me.SetFXCoronaTexture("tail_a1", red);
                // set cab blind (rear) while trailing in sunny daylight
                if (!night and weather == 0) {
                    me.SetFXAttachment("blind", blind);
                }
            }
        }

        // are we on the end of the train?
        if (vs[vs.size()-1] == me) {
            if (debug) {Interface.Log("LightUp.SetLights() On the back");}
            // No. Are we going forward or backward?
            if (reverser >= 1.0) {
                // So we are at the back going forwards
                // turn tail lights on at A end
                me.SetFXCoronaTexture("tail_a0", red);
                me.SetFXCoronaTexture("tail_a1", red);
                // set cab blind (rear) while trailing in sunny daylight
                if (!night and weather == 0) {
                    me.SetFXAttachment("blind", blind);
                }
            } else {
                // turn markers on at A end
                me.SetFXCoronaTexture("marker_a0", white);
                me.SetFXCoronaTexture("marker_a1", white);
                if (headlight > 0.0) {
                    me.SetFXCoronaTexture("head_a1", white);
                    me.SetFXCoronaTexture("head_a0", white);
                }
                // Add driver
                me.SetFXAttachment("driver", driver);
                // Turn on windscreen wipers in bad weather
                if (weather > 1) {
                    me.StartMeshAnimationLoop("wipers_a");
                }
            }
        }
    }
    // LIGHTUP FUNCTIONS END
};
User avatar
Briggsy
Forum Veteran
Posts: 1523
Joined: Thu Feb 16, 2012 1:02 am
Trainz Version: TRS2019 + TANE SP3
Trainz Build: 105096
Author KUID: 630773
Location: Coventry (West Midlands)

Re: Script on Class 91/Mk3 DVT

Post by Briggsy »

Same applies to the Class 91 script - which is as follows:

Code: Select all

include "locomotive.gs"
include "library.gs"
include "asset.gs"

/*
   LightUp script
   NumberIt script
   Author: Dave Renshaw (eldavo)
   Copyright Dave Renshaw 2005. All rights reserved.

   Nameplate texture replacement based on Rebuilt Merchant Navy class 
   script by Eldavo and LieLestoSbrat

   Spark script by Bloodnok

   Windscreen wipers, cab blinds and driver positioning added by Peter Hicks 
   (which probably explains why they don't work very well...)
*/

class mallard91 isclass Locomotive
{
    Library chameleon;
    Library numberit;

    // We have to first define the methods within the script class...
    //
    // These methods handle the different things we want to control
    void SetLights();

    //
    // These are handlers that listen for messages from the scripting environment and other objects
    void TrainHandler(Message msg);
    void CouplingHandler(Message msg);
    //
    // Finally a method that runs as a thread and checks the state of things periodically
    thread void ControlMonitor();
    
    // Spark functions
    thread void make_spark(string attachment, float duration);
    thread void spark_manager();

    // Define a flag to turn debugging information on and off. Set this to "true" to enable output of debug info.
//    bool debug = true;
    bool debug = false;

    // The train we are currently part of
    Train currentTrain;

    // Generally useful stuff
    Asset red;
    Asset white;
    Asset drive;
    Asset spark;
    Asset blinda;
    Asset blindb;

    // The "init" method is called whenever a new vehicle of this type is created
        void Init(void) {
        inherited();
        
        // LIGHTUP SCRIPT START
        if (debug) {Interface.Log("Class91.Init() [v1.0] my name is '"+GetName()+"' and id is '"+GetId()+"'");}

        // We always call our superclass (Locomotive) init method to ensure all the behaviour we wish to
        // inherit from it has been correctly initialised.
        inherited();

        red = me.GetAsset().FindAsset("corona_red");
        if (red == null) {
            Interface.Log("class91.Init() - can't find corona asset 'corona_red'!");
            return;
        }

        white = me.GetAsset().FindAsset("corona_white");
        if (white == null) {
            Interface.Log("class91.Init() - can't find corona asset 'corona_white'!");
            return;
        }
        
        drive = me.GetAsset().FindAsset("driver");
        if (drive == null) {
            Interface.Log("class91.Init() - can't find driver asset 'driver'!");
            return;
        }
        
        spark = me.GetAsset().FindAsset("spark");
        if (spark == null) {
            Interface.Log("class91.Init() - can't find driver asset 'spark'!");
            return;
        }
        
        blinda = me.GetAsset().FindAsset("cabblind_front");
        if (blinda == null) {
            Interface.Log("MallardDVT.Init() - can't find driver asset 'cabblind_front'!");
            return;
        }
        
        blindb = me.GetAsset().FindAsset("cabblind_rear");
        if (blindb == null) {
            Interface.Log("MallardDVT.Init() - can't find driver asset 'cabblind_rear'!");
            return;
        }

        // Listen for a few interesting messages..
        //
        AddHandler(me, "Train", null, "TrainHandler");
        AddHandler(me, "Vehicle", null, "CouplingHandler");
        currentTrain = me.GetMyTrain();

        // if we have a train object we will sniff it for events...
        if (currentTrain!= null) {
            if (debug) {Interface.Log("Class91.Init() - Train object is not null");}
            me.Sniff(currentTrain, "Train", "StartedMoving", true);
            
            // Spark initiation
            make_spark("spark_0", 0);
            make_spark("spark_1", 0);
            make_spark("spark_2", 0);
            make_spark("spark_3", 0);
            make_spark("spark_4", 0);
            make_spark("spark_5", 0);
            make_spark("spark_6", 0);
            make_spark("spark_7", 0);
            spark_manager();
            // End Spark initiation
        }

        // Set up the initial state of everything...
        SetLights();

        // Kick off a monitoring thread...
        ControlMonitor();
        // LIGHTUP SCRIPT END

        // NUMBERIT SCRIPT START
        // Pick a running number if one hasn't already been set
        numberit = World.GetLibrary(GetAsset().LookupKUIDTable("NumberIt_library"));
        if (numberit) {
            GSObject[] objectParam = new GSObject[1];
            objectParam[0] = me;
            numberit.LibraryCall("SetRunningNumber", null, objectParam);
        }
        // NUMBERIT SCRIPT END
        
        // CHAMELEON SCRIPT START
        chameleon = World.GetLibrary(GetAsset().LookupKUIDTable("Chameleon_library"));
        if (chameleon) {
            GSObject[] objectParam = new GSObject[1];
            objectParam[0] = me;
            // Set up the initial livery...
            string[] stringParam = new string[1];
            stringParam[0] = GetRunningNumber();
            chameleon.LibraryCall("Start", stringParam, objectParam);
            chameleon.LibraryCall("SetLivery", stringParam, objectParam);
        }
        // CHAMELEON SCRIPT END

        if (debug) {Interface.Log("Class91.Init() [Complete]");}
    }
    
    // LIGHTUP FUNCTIONS START
    // We have a handler for train messages so we can see when the train starts moving and adjust the
    // various settings
    void TrainHandler(Message msg) {
        if (debug) {
            Interface.Log("LightUp.TrainHandler(major='"+msg.major+"' minor='"+msg.minor+"'");
            if (msg.src == null) {
                Interface.Log("                        src='null')");
            } else {
                GameObject srco = cast <GameObject> msg.src;
                Interface.Log("                        src='"+srco.GetName()+"')");
            }
            Interface.Log("                        me='"+me.GetName()+"'");
        }

        if (msg.minor == "StartedMoving") {
            SetLights();
        }
    }

    // We have a handler for vehicle messages so we can see when we are coupled and uncoupled and can adjust the
    // various settings
    void CouplingHandler(Message msg) {
        if (debug) {
            Interface.Log("LightUp.CouplingHandler(major='"+msg.major+"' minor='"+msg.minor+"'");
            if (msg.src == null) {
                Interface.Log("                        src='null')");
            } else {
                GameObject srco = cast <GameObject> msg.src;
                Interface.Log("                        src='"+srco.GetName()+"')");
            }
            Interface.Log("                        me='"+me.GetName()+"'");
        }

        if (msg.minor == "Coupled" or msg.minor == "Collided") {
            if (msg.src == me) {

                // Adjust the train sniffer to ensure we are watching the right one...
                if (currentTrain != null) {
                    me.Sniff(currentTrain, "Train", "StartedMoving", false);
                }
                currentTrain = me.GetMyTrain();
                if (currentTrain != null) {
                    me.Sniff(currentTrain, "Train", "StartedMoving", true);
                }

                SetLights();
            }

        } else if (msg.minor == "Decoupled") {

            // Adjust the train sniffer to ensure we are watching the right one...
            if (currentTrain != null) {
                me.Sniff(currentTrain, "Train", "StartedMoving", false);
            }
            currentTrain = me.GetMyTrain();
            if (currentTrain != null) {
                me.Sniff(currentTrain, "Train", "StartedMoving", true);
            }

            SetLights();
        }
    }


    thread void ControlMonitor() {
        if (debug) {Interface.Log("LightUp.ControlMonitor()");}

        // the state of the current module
        int oldModule = World.GetCurrentModule();

        // the state of the weather
        int oldWeather = World.GetWeatherType();

        // the state of the headlight
        float oldheadlight = 0.0;

        while (true) {

            // Have we changed modules?
            int newModule = World.GetCurrentModule();
            if (newModule != oldModule) {
                if (debug) {Interface.Log("LightUp.ControlMonitor() - change module from "+oldModule+" to "+newModule);}
                if (newModule == World.DRIVER_MODULE) {
                    SetLights();
                }
                oldModule = newModule;
            }

            if (newModule == World.DRIVER_MODULE) {
                // what's the state of the headlight?
                float headlight = me.GetEngineSetting("headlight");
                int newWeather = World.GetWeatherType();
                if (headlight != oldheadlight or newWeather != oldWeather) {
                    // looks like the headlight has been turned on or off...
                    // or the weather has changed - wipers possibly required...
                    me.SetLights();
                    oldheadlight = headlight;
                    oldWeather = newWeather;
                }
            }

            Sleep(5);
        }
    }

    void SetLights() {
        if (debug) {Interface.Log("LightUp.SetLights()");}


        // turn off all the lights
        // turn headlights and markers off at A end
        me.SetFXCoronaTexture("marker_a0", null);
        me.SetFXCoronaTexture("marker_a1", null);
        me.SetFXCoronaTexture("head_a0", null);
        me.SetFXCoronaTexture("head_a1", null);
        // turn headlights and markers off at B end
        me.SetFXCoronaTexture("marker_b0", null);
        me.SetFXCoronaTexture("marker_b1", null);
        me.SetFXCoronaTexture("head_b0", null);
        me.SetFXCoronaTexture("head_b1", null);
        // turn off all tail lights
        me.SetFXCoronaTexture("tail_a0", null);
        me.SetFXCoronaTexture("tail_a1", null);
        me.SetFXCoronaTexture("tail_b0", null);
        me.SetFXCoronaTexture("tail_b1", null);
        // remove driver
        me.SetFXAttachment("driver_a", null);
        me.SetFXAttachment("driver_b", null);
        // remove cab blinds
        me.SetFXAttachment("blind_a", null);
        me.SetFXAttachment("blind_b", null);
        // halt wiper animation
        me.StopMeshAnimation("wipers_a");
        me.StopMeshAnimation("wipers_b");

        // if we are not in Driver or Surveyor then display a default light pattern...
        if (debug) {Interface.Log("LightUp.SetLights() current module="+World.GetCurrentModule());}
        if (World.GetCurrentModule() != World.DRIVER_MODULE) {
            if (debug) {Interface.Log("LightUp.SetLights - Not in Driver. Setting default light pattern");}
            me.SetFXCoronaTexture("head_a0", white);
            me.SetFXCoronaTexture("head_a1", white);
            me.SetFXCoronaTexture("marker_a0", white);
            me.SetFXCoronaTexture("marker_a1", white);
            me.SetFXCoronaTexture("tail_b0", red);
            me.SetFXCoronaTexture("tail_b1", red);
            me.SetFXAttachment("driver_a", drive);
         // me.SetFXAttachment("driver_b", null);
         // me.SetFXAttachment("blind_a", null);
         // me.SetFXAttachment("blind_b", null);
            return;
        }

        // which way round are we?
        float reverser = me.GetEngineSetting("reverser");
        if (debug) {
            Interface.Log("LightUp.SetLights() reverser = "+reverser);
            Interface.Log("LightUp.SetLights() me.GetDirectionRelativeToTrain() = "+me.GetDirectionRelativeToTrain());
        }

        float time = World.GetGameTime();
        int weather = World.GetWeatherType();
        if (debug) {Interface.Log("LightUp.SetLights() time = "+time+", and weather = "+weather);}
        bool night = (time > 0.25 and time < 0.75);

        float headlight = me.GetEngineSetting("headlight");
        if (debug) {Interface.Log("LightUp.SetLights() headlight = "+headlight);}
        
        float velocity = me.GetVelocity();

        Train t = me.GetMyTrain();
        Vehicle[] vs = t.GetVehicles();

        // are we on the front of the train?
        if (vs[0] == me) {
            if (debug) {Interface.Log("LightUp.SetLights() On the front");}
            // are we facing the same direction as the train?
            if (me.GetDirectionRelativeToTrain()) {
                // Yes. The A end is at the front of the train.
                // Are we going forward or backward?
                if (reverser >= 1.0) {
                    // So we are at the front going forward
                    // turn markers on at A end
                    me.SetFXCoronaTexture("marker_a0", white);
                    me.SetFXCoronaTexture("marker_a1", white);
                    if (headlight > 0.0) {
                        me.SetFXCoronaTexture("head_a1", white);
                        me.SetFXCoronaTexture("head_a0", white);
                    }
                    // Add driver
                    me.SetFXAttachment("driver_a", drive);
                    // Turn on windscreen wipers in bad weather
                    if (weather > 1) {
                        me.StartMeshAnimationLoop("wipers_a");
                    }
                } else {
                    // turn tail lights on at A end
                    me.SetFXCoronaTexture("tail_a0", red);
                    me.SetFXCoronaTexture("tail_a1", red);
                    // set cab blind (front) while trailing in sunny daylight
                    if (!night and weather == 0) {
                        me.SetFXAttachment("blind_a", blinda);
                    }
                }
            } else {
                // We are back to front compared with the train
                // Is the loco going forward or backward?
                if (reverser >= 1.0) {
                    // So we are at the front going backwards
                    // turn markers on at B end
                    me.SetFXCoronaTexture("marker_b0", white);
                    me.SetFXCoronaTexture("marker_b1", white);
                    if (headlight > 0.0) {
                        me.SetFXCoronaTexture("head_b1", white);
                        me.SetFXCoronaTexture("head_b0", white);
                    }
                    // Add driver
                    me.SetFXAttachment("driver_b", drive);
                    // Turn on windscreen wipers in bad weather
                    if (weather > 1) {
                        me.StartMeshAnimationLoop("wipers_b");
                    }
                    // Set max speed to 110mph when leading slab end first ???
                    //me.GetMyTrain().SetAdvisoryLimit(177.0);
                } else {
                    // turn tail lights on at B end
                    me.SetFXCoronaTexture("tail_b0", red);
                    me.SetFXCoronaTexture("tail_b1", red);
                    // set cab blind (rear) while trailing in sunny daylight
                    if (!night and weather == 0) {
                        me.SetFXAttachment("blind_b", blindb);
                    }
                }
            }
        }

        // are we on the end of the train?
        if (vs[vs.size()-1] == me) {
            if (debug) {Interface.Log("LightUp.SetLights() On the back");}
            // Yup.
            // are we facing the same direction as the train?
            if (me.GetDirectionRelativeToTrain()) {
                // Yes, Are we going forward or backward?
                if (reverser >= 1.0) {
                    // turn tail lights on at B end
                    me.SetFXCoronaTexture("tail_b0", red);
                    me.SetFXCoronaTexture("tail_b1", red);
                    // set cab blind (rear)
                    me.SetFXAttachment("blind_b", blindb);
                } else {
                    // So we are at the back going backwards
                    // turn markers on at B end
                    me.SetFXCoronaTexture("marker_b0", white);
                    me.SetFXCoronaTexture("marker_b1", white);
                    if (headlight > 0.0) {
                        me.SetFXCoronaTexture("head_b1", white);
                        me.SetFXCoronaTexture("head_b0", white);
                    }
                    // Add driver
                    me.SetFXAttachment("driver_b", drive);
                    // Turn on windscreen wipers in bad weather
                    if (weather > 1) {
                        me.StartMeshAnimationLoop("wipers_b");
                    }
                    // Set max speed to 110mph when leading slab end first ???
                    //me.GetMyTrain().SetAdvisoryLimit(177.0);
                }
            } else {
                // No. Are we going forward or backward?
                if (reverser >= 1.0) {
                    // So we are at the back going forwards
                    // turn tail lights on at A end
                    me.SetFXCoronaTexture("tail_a0", red);
                    me.SetFXCoronaTexture("tail_a1", red);
                    // set cab blind (front)
                    me.SetFXAttachment("blind_a", blinda);
                } else {
                    // turn markers on at A end
                    me.SetFXCoronaTexture("marker_a0", white);
                    me.SetFXCoronaTexture("marker_a1", white);
                    if (headlight > 0.0) {
                        me.SetFXCoronaTexture("head_a1", white);
                        me.SetFXCoronaTexture("head_a0", white);
                    }
                    // Add driver
                    me.SetFXAttachment("driver_a", drive);
                    // Turn on windscreen wipers in bad weather
                    if (weather > 1) {
                        me.StartMeshAnimationLoop("wipers_a");
                    }
                }
            }
        }
    }
    // LIGHTUP FUNCTIONS END
    
    // SPARK FUNCTIONS START
    thread void spark_manager() {

        Interface.Log("Sparker: spark_manager called");

        int pantos_up;
        float power;
        float speed;
        int weather = 1;
        int sleep_done = 0;
        float time_to_sleep;

        for(;;) { // loop forever

        pantos_up = me.GetMyTrain().GetPantographState();
//        if (debug) {Interface.Log("Sparker: pantograph state: " + pantos_up + ".");}

        if (pantos_up) {
            // get weather
            switch(World.GetWeatherType()) {
                case World.WEATHER_TYPE_CLEAR : weather = 1; break;
                case World.WEATHER_TYPE_CLOUDY : weather = 1; break;
                case World.WEATHER_TYPE_DRIZZLE : weather = 2; break;
                case World.WEATHER_TYPE_RAIN : weather = 4; break;
                case World.WEATHER_TYPE_STORMY : weather = 6; break;
                case World.WEATHER_TYPE_LIGHT_SNOW : weather = 2; break;
                case World.WEATHER_TYPE_MEDIUM_SNOW : weather = 5; break;
                case World.WEATHER_TYPE_HEAVY_SNOW : weather = 8; break;
                default : weather = 1;
            }

            speed = Math.Fabs(me.GetVelocity());

            // get physics model
            if(me.GetMyTrain().GetPhysicsModel() == Train.PHYSICS_DCC) {
                // speed is used extensively here to try to reproduce an approximation of the behaviour
                // in CAB mode, as we can't get the ammeter in DCC mode.

                /* This constant will need tuning. It is the speed at which to start tailing off the
                sparking in m/s. I've used 10 here, which is about 35mph, on a loco capable of 90.
                This is to reproduce the approximate behaviour of reading the ammeter in cab mode,
                so that trains running at full speed don't spark away really madly when they wouldn't
                in CAB mode. Trains running at lower speeds will either have the DCC throttle set lower,
                which will reduce the sparking, or will be working hard at a grade, so will spark
                enthusiastically, which is what we want. */

                if(speed > 10) {
                    speed = Math.Sqrt(10 / speed);
                } else if(speed > 1) {
                    speed = 1.0;
                }

                power = Math.Fabs(me.GetMyTrain().GetDCCThrottle());

            } else {
                // speed is only used to quieten the sparks down when not moving very much, the CAB mode
                // calculation is based very heavily on the ammeter reading
                if(speed > 1) {
                    speed = 1.0;
                }

                /* this constant will need tuning. It is the square root of the expected sustained ammeter
                reading during periods of relatively heavy load, for example working up a grade on full
                throttle at (say) 20 to 30 mph. The square root of the ammeter peak on acceleration can
                be higher, but not by much - you wouldn't want the 'power' variable to stay much above '1'
                on a sustained basis. */
                power = Math.Sqrt(GetEngineParam("current-drawn")) / 20;
            }

            if (debug) {Interface.Print("Power: " + power + " Speed: " + speed);}

            // avoid div-zero infinite
            if(power <= 0.01) {
                power = 0.01;
            }
            if(speed <= 0.01) {
                speed = 0.01;
            }
        }

        time_to_sleep = 10;
        if(pantos_up) {

        // tweak this Rand() call if you prefer more, or less sparks overall. The values should be
        // as far apart as possible to make it unpredictable. Regular flashing is something we'd like
        // to avoid. Under full power, at low to mid speeds, in good weather, this will be about the rate
        // of sparks, as the divisor will tend to 1 in these conditions. Bad weather makes more sparks,
        // working the engine less makes less sparks.

        time_to_sleep = Math.Rand(0.25,1.75) / (power * speed * weather);
      }
//      if (debug) {Interface.Print("Sleep time: " + time_to_sleep + " Done: " + sleep_done);}

      if(time_to_sleep - sleep_done < 1) {
          switch (pantos_up) {
              case 1 :
                  // attachment points for front panto
                  make_spark("spark_" + Math.Rand(0,7), Math.Rand(0.01,0.1));
                  break;

              case 2 :
                  // attachment points for rear panto, which we don't have, so is commented out
                  // make_spark("spark_" + Math.Rand(8,16), Math.Rand(0.01,0.1));
                  break;

              case 3 :
                  // attachment points for both pantos, but we don't have a back panto, so I've
                  // commented this out, and just copied the front panto line in here instead
                  make_spark("spark_" + Math.Rand(0,7), Math.Rand(0.01,0.1));
                  // make_spark("spark_" + Math.Rand(0,16), Math.Rand(0.01,0.1));
                  break;

              default :
                  // no pantos up, do nothing
                  break;
          }

          Sleep(time_to_sleep - sleep_done);
          sleep_done = 0;

      }else{
           Sleep(1);
           sleep_done++;
      }
    }
  }


  thread void make_spark(string attachment, float duration) {
   //   if (debug) {Interface.Log("make_spark called for attachment " + attachment + " with duration " + duration + " seconds.");}

      SetFXCoronaTexture(attachment, spark);
      Sleep(duration);
      SetFXCoronaTexture(attachment, null);

  //    if (debug) {Interface.Log("make_spark finished for attachment " + attachment + " with duration " + duration + " seconds.");}
  }
    // SPARK FUNCTIONS END
    
    // CHAMELEON FUNCTIONS START
    // Override SetPropertyValue and watch for changes
    void SetPropertyValue(string name, string value) {
        if (debug) {Interface.Log("-->Class91.SetPropertyValue(name='"+name+"' value='"+value+"')");}
        inherited(name, value);
        // if the running number is changing update the textures
        if (name == "runningnumber") {
            // Update attached mesh textures
            if (chameleon) {
                GSObject[] objectParam = new GSObject[1];
                objectParam[0] = me;
                string[] stringParam = new string[1];
                stringParam[0] = value;
                chameleon.LibraryCall("SetLivery", stringParam, objectParam);
            }
        }
        if (debug) {Interface.Log("<--Class91.SetPropertyValue()");}
    }
    // CHAMELEON FUNCTIONS END
};
User avatar
Briggsy
Forum Veteran
Posts: 1523
Joined: Thu Feb 16, 2012 1:02 am
Trainz Version: TRS2019 + TANE SP3
Trainz Build: 105096
Author KUID: 630773
Location: Coventry (West Midlands)

Re: Script on Class 91/Mk3 DVT

Post by Briggsy »

PFX wrote:While I'm trying to fix things, I can't remember if I asked before about fixing running numbers on rolling stock? If so, apologies but if not, when I add stock (specifically snowsignal's 1xx and 3xx DMUs and EMUs) all that displays is a row of 7s with a blue background. I have a vague recollection of repairing this once before but the method escapes me.

Cheers,
Innis
I don't have those particular items to check - but when I've had issues with numbering in the past, it's usually down to a line being missing or incorrect in the config file.

Check that your config file has this line:

Code: Select all

fonts-path                              "BR_Class_31-Blue"
Ignore the "BR_Class_31-Blue" bit - that's just an example.

Inside the speech marks, ensure that the bit that says "BR_Class_31-Blue" above, has the name of the directory exactly as it appear before the "_alpha_numbers" directory of the item you're altering.

For example, on the Class 31 above, the directory with the alpha numbers is "BR_Class_31-Blue_alpha_numbers" - so you only need to put "BR_Class_31-Blue" in the speech marks of the config file. Your config file should then find those numbers - and display them on the unit / loco in-game.

However, you also need to ensure that there are a series of images in the 'alpha numbers' directory labelled something along the lines of "alphanumber_0a", "alphanumber_0b", alphanumber_1a" - and so on.

If those are missing, you'll either need to create some - or copy some over from another item.

You need the line in the config file AND the images in the alpha_numbers directory for numbers to show correctly. If either or both are missing, you'll get the issue you're experiencing.

Hopefully that helps.
User avatar
PFX
Forum Veteran
Posts: 2086
Joined: Mon Dec 13, 2010 3:12 pm
Trainz Version: TRS22
Trainz Build: 119450
Location: Béal Feirste

Re: Script on Class 91/Mk3 DVT

Post by PFX »

Huge thanks Briggsy. I'll try to get these in game and tested as soon as I get a minute.

No apology required. I don't like to needle people as, like you, I'm terrible at remembering a lot of things.

Cheers,
Innis
Image
Post Reply