Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LOCOMOTIVE SCRIPT EXTENDER LIBRARY
- // Version: 0.0.7, date: 24.07.2011
- // Author: agentmike, e-mail: agentmike@rambler.ru
- // For license information see config.txt file.
- //
- // LSE is extensible framework for creating prototypically working locomotives.
- // LSE.Init is standalone part of LSE providing support of unified initialization
- // system for locomotives with custom scripts.
- include "vehicle.gs"
- // InitV is short for Init Vehicle - specialised vehicle for performing +
- // some startup action and then dissapearing from the map.
- // Its length very short, so its dissapearance will not be noticed,
- // even if they were in the middle of some consist.
- //
- // There are three steps in initialization:
- // 1. PreInit step - for actions which will affect latter init steps,
- // i. e. disabling train physics. Performed for whole train.
- // 2. Init for train and init for vehicle group. This steps aren't
- // differentiated, so don't rely on their call order. Vehicle group is set
- // of vehicles between InitVs with same category. This allows to init
- // differently parts of single consist.
- //
- // Category is a string returned by overridden GetInitVehicleCategory().
- //
- // Generally you should always make InitVs in your consists to point in one
- // direction. If two InitVs with same category will point towards each other,
- // they both will recieve same vehicle set and you'll only get bugs.
- class LseInitVehicle isclass Vehicle
- {
- void DoPreInit(Train train) { }
- void DoInit(Train train) { }
- void DoInit(Vehicle[] vehs) { }
- bool _trainPhysicsState = true;
- final public bool GetPhysicsState() { return _trainPhysicsState; }
- final public string GetInitVehicleCategory() { return ""; }
- final void _deleteMe()
- {
- Vehicle[] trainVehs = GetMyTrain().GetVehicles();
- if (trainVehs.size() == 1)
- World.DeleteTrain(GetMyTrain());
- else if (trainVehs[0] == me or trainVehs[trainVehs.size() - 1] == me)
- World.DeleteVehicle(me);
- else
- {
- Vehicle lastVehicle = trainVehs[trainVehs.size() - 1];
- if (lastVehicle.GetDirectionRelativeToTrain())
- Reposition(lastVehicle, COUPLE_FRONT, COUPLE_BACK);
- else
- Reposition(lastVehicle, COUPLE_FRONT, COUPLE_FRONT);
- World.DeleteVehicle(me);
- }
- }
- final thread void Main()
- {
- int myId = GetId();
- string myName = GetName();
- string myLocName = GetLocalisedName();
- Sleep(1.0);
- /* DEBUG */
- Interface.Log("InitV \'" + myLocName + "\' (" + myName + ", id " + myId + ")");
- // */
- while (!GetMyTrain().IsStopped())
- Sleep(0.1);
- Sleep(1.0);
- DoPreInit(GetMyTrain());
- Sleep(2.0);
- Train myTrain = GetMyTrain();
- Vehicle[] trainVehs = myTrain.GetVehicles();
- int myPosition;
- for (myPosition = 0; myPosition < trainVehs.size(); ++myPosition)
- if (trainVehs[myPosition] == me)
- break;
- int indexFrom;
- int indexTo;
- if (GetDirectionRelativeToTrain())
- {
- indexFrom = myPosition - 1;
- indexTo = myPosition;
- }
- else
- {
- indexFrom = myPosition + 1;
- indexTo = indexFrom + 1;
- }
- if (GetDirectionRelativeToTrain())
- {
- for (; indexFrom >= 0; --indexFrom)
- {
- LseInitVehicle initv = cast<LseInitVehicle> trainVehs[indexFrom];
- if (initv and initv.GetInitVehicleCategory() == me.GetInitVehicleCategory())
- break;
- }
- if (indexFrom < 0)
- indexFrom = 0;
- }
- else
- {
- for (; indexTo < trainVehs.size(); ++indexTo)
- {
- LseInitVehicle initv = cast<LseInitVehicle> trainVehs[indexTo];
- if (initv and initv.GetInitVehicleCategory() == me.GetInitVehicleCategory())
- break;
- }
- }
- Vehicle[] myVehs = trainVehs[indexFrom, indexTo];
- DoInit(myTrain);
- DoInit(myVehs);
- _deleteMe();
- /* DEBUG */
- Interface.Log("InitV : performed train init and deleted");
- // */
- }
- final void OnLseTrainPhysics(Message msg)
- {
- Train msgTrain = cast<Train> msg.src;
- if (msgTrain and msgTrain == GetMyTrain())
- {
- if (msg.minor == "Disabled" or msg.minor == "false" or msg.minor == "0")
- _trainPhysicsState = false;
- else
- _trainPhysicsState = true;
- }
- }
- final void OnWorldModuleInit(Message msg)
- {
- if (World.GetCurrentModule() == World.DRIVER_MODULE)
- Main();
- }
- public void Init(Asset asset)
- {
- AddHandler(me, "Lse.TrainPhysics", "", "OnLseTrainPhysics");
- if (World.GetCurrentModule() == World.DRIVER_MODULE)
- Main();
- else
- AddHandler(me, "World", "ModuleInit", "OnWorldModuleInit");
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement