Advertisement
agmike

Lse.Init.Vehicle [Revised 24.07.2011]

Jul 24th, 2011
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.32 KB | None | 0 0
  1. // LOCOMOTIVE SCRIPT EXTENDER LIBRARY
  2. // Version: 0.0.7, date: 24.07.2011
  3. // Author: agentmike, e-mail: agentmike@rambler.ru
  4. // For license information see config.txt file.
  5. //
  6. // LSE is extensible framework for creating prototypically working locomotives.
  7. // LSE.Init is standalone part of LSE providing support of unified initialization
  8. // system for locomotives with custom scripts.
  9.  
  10. include "vehicle.gs"
  11.  
  12.  
  13. // InitV is short for Init Vehicle - specialised vehicle for performing        +
  14. // some startup action and then dissapearing from the map.
  15. // Its length very short, so its dissapearance will not be noticed,
  16. // even if they were in the middle of some consist.
  17. //
  18. // There are three steps in initialization:
  19. // 1. PreInit step - for actions which will affect latter init steps,
  20. //    i. e. disabling train physics. Performed for whole train.
  21. // 2. Init for train and init for vehicle group. This steps aren't
  22. //    differentiated, so don't rely on their call order. Vehicle group is set
  23. //    of vehicles between InitVs with same category. This allows to init
  24. //    differently parts of single consist.
  25. //
  26. // Category is a string returned by overridden GetInitVehicleCategory().
  27. //
  28. // Generally you should always make InitVs in your consists to point in one
  29. // direction. If two InitVs with same category will point towards each other,
  30. // they both will recieve same vehicle set and you'll only get bugs.
  31.  
  32. class LseInitVehicle isclass Vehicle
  33. {
  34.     void DoPreInit(Train train)  { }
  35.     void DoInit(Train train)     { }
  36.     void DoInit(Vehicle[] vehs)  { }
  37.    
  38.     bool _trainPhysicsState = true;
  39.    
  40.     final public bool GetPhysicsState() { return _trainPhysicsState; }
  41.    
  42.     final public string GetInitVehicleCategory() { return ""; }
  43.    
  44.     final void _deleteMe()
  45.     {
  46.         Vehicle[] trainVehs = GetMyTrain().GetVehicles();
  47.         if (trainVehs.size() == 1)
  48.             World.DeleteTrain(GetMyTrain());
  49.         else if (trainVehs[0] == me or trainVehs[trainVehs.size() - 1] == me)
  50.             World.DeleteVehicle(me);
  51.         else
  52.         {
  53.             Vehicle lastVehicle = trainVehs[trainVehs.size() - 1];
  54.             if (lastVehicle.GetDirectionRelativeToTrain())
  55.                 Reposition(lastVehicle, COUPLE_FRONT, COUPLE_BACK);
  56.             else
  57.                 Reposition(lastVehicle, COUPLE_FRONT, COUPLE_FRONT);
  58.             World.DeleteVehicle(me);
  59.         }
  60.     }
  61.  
  62.     final thread void Main()
  63.     {
  64.         int myId = GetId();
  65.         string myName = GetName();
  66.         string myLocName = GetLocalisedName();
  67.        
  68.         Sleep(1.0);
  69.        
  70.         /* DEBUG */
  71.         Interface.Log("InitV \'" + myLocName + "\' (" + myName + ", id " + myId + ")");
  72.         // */
  73.        
  74.         while (!GetMyTrain().IsStopped())
  75.             Sleep(0.1);
  76.            
  77.         Sleep(1.0);
  78.         DoPreInit(GetMyTrain());
  79.        
  80.         Sleep(2.0);
  81.        
  82.         Train myTrain = GetMyTrain();
  83.         Vehicle[] trainVehs = myTrain.GetVehicles();
  84.        
  85.         int myPosition;
  86.         for (myPosition = 0; myPosition < trainVehs.size(); ++myPosition)
  87.             if (trainVehs[myPosition] == me)
  88.                 break;
  89.        
  90.         int indexFrom;
  91.         int indexTo;
  92.         if (GetDirectionRelativeToTrain())
  93.         {
  94.             indexFrom = myPosition - 1;
  95.             indexTo = myPosition;
  96.         }
  97.         else
  98.         {
  99.             indexFrom = myPosition + 1;
  100.             indexTo = indexFrom + 1;
  101.         }
  102.        
  103.         if (GetDirectionRelativeToTrain())
  104.         {
  105.             for (; indexFrom >= 0; --indexFrom)
  106.             {
  107.                 LseInitVehicle initv = cast<LseInitVehicle> trainVehs[indexFrom];
  108.                 if (initv and initv.GetInitVehicleCategory() == me.GetInitVehicleCategory())
  109.                     break;
  110.             }
  111.            
  112.             if (indexFrom < 0)
  113.                 indexFrom = 0;
  114.         }
  115.         else
  116.         {
  117.             for (; indexTo < trainVehs.size(); ++indexTo)
  118.             {
  119.                 LseInitVehicle initv = cast<LseInitVehicle> trainVehs[indexTo];
  120.                 if (initv and initv.GetInitVehicleCategory() == me.GetInitVehicleCategory())
  121.                     break;
  122.             }
  123.         }
  124.        
  125.         Vehicle[] myVehs = trainVehs[indexFrom, indexTo];
  126.        
  127.         DoInit(myTrain);
  128.         DoInit(myVehs);
  129.        
  130.         _deleteMe();
  131.        
  132.         /* DEBUG */
  133.         Interface.Log("InitV : performed train init and deleted");
  134.         // */
  135.     }
  136.    
  137.    
  138.     final void OnLseTrainPhysics(Message msg)
  139.     {
  140.         Train msgTrain = cast<Train> msg.src;
  141.         if (msgTrain and msgTrain == GetMyTrain())
  142.         {
  143.             if (msg.minor == "Disabled" or msg.minor == "false" or msg.minor == "0")
  144.                 _trainPhysicsState = false;
  145.             else
  146.                 _trainPhysicsState = true;
  147.         }
  148.     }
  149.    
  150.     final void OnWorldModuleInit(Message msg)
  151.     {
  152.         if (World.GetCurrentModule() == World.DRIVER_MODULE)
  153.             Main();
  154.     }
  155.    
  156.     public void Init(Asset asset)
  157.     {
  158.         AddHandler(me, "Lse.TrainPhysics", "", "OnLseTrainPhysics");
  159.        
  160.         if (World.GetCurrentModule() == World.DRIVER_MODULE)
  161.             Main();
  162.         else
  163.             AddHandler(me, "World", "ModuleInit", "OnWorldModuleInit");
  164.     }
  165. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement