CCG/Kind: Interior Steam Cab Script

From TrainzOnline
< CCG
Jump to: navigation, search

STEAM CAB SCRIPT

PB15Cabin.gs

This is the PB15 steam interior script file.

This script is responsible for mapping all the steam-specific controls to the physics system, and for handling PB-15 specific functions such as the coal shovelling dude.

include "train.gs"
include "locomotive.gs"
include "cabin.gs"
  
class PB15CabinData isclass CabinData
{
  public float fire_plates_val;
  public float left_window_val; 
  public float right_window_val;
  public float left_sliding_window_val;
  public float right_sliding_window_val;
  public float seat0_val;
  public float seat1_val;
  public float sanding_lever_val;
  public float whistle_lever_val;
  public float regulator_lever_val;
  public float blowdown_lever_val;
  public bool fireboxDoorOpen;
};
  
class PB15Cabin isclass Cabin
{
  Locomotive loco;
  
    CabinControl speedometer;
    CabinControl main_reservoir_needle;
    CabinControl brake_cylinder0_needle;
    CabinControl brake_cylinder1_needle;
    CabinControl no3_pipe_needle;
    CabinControl brake_pipe_needle;
    CabinControl equaliser_needle;
    CabinControl flow_needle;
    CabinControl ampmeter_needle0;
    CabinControl ampmeter_needle1;
    //CabinControl throttle_lever;
    CabinControl reverser_lever;
    CabinControl train_brake_lever;
    CabinControl train_lapbrake_lever;
    CabinControl loco_brake_lever;
    CabinControl dynamic_brake_lever;
    CabinControl wheelslip_light;
    CabinControl horn_rope;
    CabinControl pantograph_lever;
    CabinControl light_switch;
  
    CabinControl     waterGlassLeft_dial, waterGlassRight_dial;
    CabinControl     firebox;
    CabinControl     boiler_needle;
    CabinControl     regulator_lever;
    CabinControl     fire_plates;
    CabinControl     left_window;
    CabinControl     right_window;
    CabinControl     left_sliding_window;
    CabinControl     right_sliding_window;
    CabinControl     seat0;
    CabinControl     seat1;
    CabinControl     sanding_lever;
    CabinControl     whistle_lever;
    CabinControl     blowdown_lever;
  
    CabinControl waterInjector0, waterInjector1;
  
    PB15CabinData cabinData;
  
   bool shovellingCoal;
   bool waving;
  
   thread void RunAnimation(void);
  
   public void Init(void)
   {
     speedometer =         GetNamedControl("speedo_needle");
     main_reservoir_needle =     GetNamedControl("bplocomain_needlev);
     brake_cylinder0_needle = GetNamedControl(vbptrainbrakecylinder_needle");
     brake_cylinder1_needle = GetNamedControl("no3pipe_needle");
     no3_pipe_needle =       GetNamedControl("bptrainbrakecylinder2_needle");
     brake_pipe_needle =       GetNamedControl("bptrainbrakepipe_needle");
     equaliser_needle =       GetNamedControl(vbploco_equaliser");
     flow_needle =        GetNamedControl("flow_needle");
     ampmeter_needle0 =       GetNamedControl("ampmeter_needle");
     ampmeter_needle1 =       GetNamedControl(vampmeter2_needle");
     //throttle_lever =       GetNamedControl(vthrottle_lever");
     train_brake_lever =       GetNamedControl("trainbrake_lever");
     train_lapbrake_lever =     GetNamedControl("trainbrakelap_lever");
     loco_brake_lever =       GetNamedControl(vindependantbrake_lever");
     dynamic_brake_lever =     GetNamedControl("dynamicbrake_lever");
     wheelslip_light =       GetNamedControl("wheelslip_light");
     horn_rope =           GetNamedControl("horn");
     light_switch =         GetNamedControl("light_switch");
  
       waterGlassLeft_dial = GetNamedControl("waterglass_left");
       waterGlassRight_dial = GetNamedControl("waterglass_right");
  
       firebox = GetNamedControl("firebox");
       boiler_needle = GetNamedControl("boiler_needle");
       regulator_lever = GetNamedControl("regulator");
       reverser_lever = GetNamedControl("reverser");
  
       waterInjector0 = GetNamedControl(vwater_injector_0");
       waterInjector1 = GetNamedControl("water_injector_1");
  
       fire_plates = GetNamedControl("fire_plates");
       left_window = GetNamedControl("left_window");
       right_window = GetNamedControl("right_window");
       left_sliding_window = GetNamedControl("left_sliding_window");
       right_sliding_window = GetNamedControl("right_sliding_window");
       seat0 = GetNamedControl("seat0");
       seat1 = GetNamedControl("seat1");
       sanding_lever = GetNamedControl("sanding_lever");
       whistle_lever = GetNamedControl("whistle_lever");
       blowdown_lever = GetNamedControl("blowdown_lever");
  
       RunAnimation();
   }
  
   //! Attach this cabin to a game object (i.e. a locomotive).
   // Param: obj Game object to attach this cabin to (usually a Locomotive).
   public void Attach(GameObject obj)
   {
     loco = cast<Locomotive>(obj);
  
       // get cabin data
       CabinData cd = loco.GetCabinData();
       if(cd)
       {
         // reset the controls from saved values
         PB15CabinData pbcd = cast<PB15CabinData>cd;
         if(fire_plates)
         fire_plates.SetValue(pbcd.fire_plates_val);
         if(left_window)
         left_window.SetValue(pbcd.left_window_val);
         if(right_window)
         right_window.SetValue(pbcd.right_window_val);
         if(left_sliding_window)
         left_sliding_window.SetValue(pbcd.left_sliding_window_val);
         if(right_sliding_window)
         right_sliding_window.SetValue(pbcd.right_sliding_window_val);
         if(seat0)
         seat0.SetValue(pbcd.seat0_val);
         if(seat1)
         seat1.SetValue(pbcd.seat1_val);
         if(sanding_lever)
         sanding_lever.SetValue(pbcd.sanding_lever_val);
         if(whistle_lever)
         whistle_lever.SetValue(pbcd.whistle_lever_val);
         if(regulator_lever)
         regulator_lever.SetValue(pbcd.regulator_lever_val);
         if(blowdown_lever)
         blowdown_lever.SetValue(pbcd.blowdown_lever_val);
       }
       else
       {
         PB15CabinData pbd = new PB15CabinData();
         loco.SetCabinData(pbd);
       }
   }
  
   // the default locomotive config uses kPa to describe pressure dial ranges
   // this function converts from g/m^3 into kPa-above-atmospheric
   public float GetPressureParam(string param)
   {
     float pressureMultiplier = 1.0 / (0.145 * 0.0000703);
     float pressureBase = 14.7 * 0.0000703;
  
       return pressureMultiplier * (loco.GetEngineParam(param) - pressureBase);
   }
  
   public void Update(void)
   {
     float value;
     Train train = loco.GetMyTrain();
  
       // Update Needles
  
       if (speedometer)
       speedometer.SetValue(train.GetVelocity());
  
       if (main_reservoir_needle)
       main_reservoir_needle.SetValue(GetPressureParam("main-reservoir-pressure"));
  
       value = GetPressureParam("brake-cylinder-pressurev);
       if (brake_cylinder0_needle)
       brake_cylinder0_needle.SetValue(value);
       if (brake_cylinder1_needle)
       brake_cylinder1_needle.SetValue(value);
  
       if (no3_pipe_needle)
       no3_pipe_needle.SetValue(GetPressureParam("no3-pipe-pressure"));
  
       if (brake_pipe_needle)
       brake_pipe_needle.SetValue(GetPressureParam("brake-pipe-pressure"));
  
       if (equaliser_needle)
       equaliser_needle.SetValue(GetPressureParam("equaliser-pressure"));
  
       if (flow_needle)
       flow_needle.SetValue(GetPressureParam("flow"));
  
       value = loco.GetEngineParam("current-drawn");
       if (ampmeter_needle0)
       ampmeter_needle0.SetValue(value);
       if (ampmeter_needle1)
       ampmeter_needle1.SetValue(value);
       //
  
     // Update Levers
     // This is done in case the game logic has changed any of the settings from what
     // the user selected.
     //if (throttle_lever)
     // throttle_lever.SetValue(loco.GetEngineSetting("throttle"));// * 8.0);
  
     if (reverser_lever)
     reverser_lever.SetValue(loco.GetEngineSetting("reverser"));
  
     if (train_brake_lever)
     train_brake_lever.SetValue(loco.GetEngineSetting("train-auto-brake"));
  
     if (train_lapbrake_lever)
     train_lapbrake_lever.SetValue(loco.GetEngineSetting("train-lap-brake"));
  
     if (loco_brake_lever)
     loco_brake_lever.SetValue(loco.GetEngineSetting("loco-auto-brake"));
  
     if (dynamic_brake_lever)
     dynamic_brake_lever.SetValue(loco.GetEngineSetting("dynamic-brake"));
  
     if (wheelslip_light)
     wheelslip_light.SetValue(loco.GetEngineParam("wheelslip"));
  
     if (horn_rope)
     horn_rope.SetValue(loco.GetEngineParam("horn"));
  
     if (pantograph_lever)
     pantograph_lever.SetValue(loco.GetEngineSetting("pantograph"));
  
     if (light_switch)
     light_switch.SetValue(loco.GetEngineSetting("headlight"));
  
     if (waterGlassLeft_dial)
     waterGlassLeft_dial.SetValue(loco.GetEngineParam("steam-boiler-liquid-percent"));
  
     if (waterGlassRight_dial)
     waterGlassRight_dial.SetValue(loco.GetEngineParam("steam-boiler-liquid-percent"));
  
     // update cabin data
     PB15CabinData cd = cast<PB15CabinData>loco.GetCabinData();
     if(left_window)
     cd.left_window_val = left_window.GetValue();
     if(right_window)
     cd.right_window_val = right_window.GetValue();
     if(left_sliding_window)
     cd.left_sliding_window_val = left_sliding_window.GetValue();
     if(right_sliding_window)
     cd.right_sliding_window_val = right_sliding_window.GetValue();
     if(seat0)
     cd.seat0_val = seat0.GetValue();
     if(seat1)
     cd.seat1_val = seat1.GetValue();
     if(sanding_lever)
     cd.sanding_lever_val = sanding_lever.GetValue();
     if(whistle_lever)
     cd.whistle_lever_val = whistle_lever.GetValue();
     if(regulator_lever)
     cd.regulator_lever_val = regulator_lever.GetValue();
     if(fire_plates)
     cd.fire_plates_val = fire_plates.GetValue();
     if(blowdown_lever)
     cd.blowdown_lever_val = blowdown_lever.GetValue();
     cd.fireboxDoorOpen = fire_plates.GetValue() > 0.9;
  
     if (firebox)
     {
       firebox.SetNamedValue("amount-burning-coal", 0.5);
       if(fire_plates)
       {
          firebox.SetNamedValue(“door-open”,fire_plates.GetValue() ); 
  
           }
           firebox.SetNamedValue("fire-life", loco.GetEngineParam("fire-temperature") / 1600.0);
           firebox.SetNamedValue("steam-piston-cycle", loco.GetEngineParam("steam-piston-cycle"));
       }
  
       if (boiler_needle)
       {
         boiler_needle.SetValue(GetPressureParam("steam-boiler-pressure"));
       }
  
       if (waterInjector0)
       waterInjector0.SetValue(loco.GetEngineSetting("injector"));
  
       if (waterInjector1)
       waterInjector1.SetValue(loco.GetEngineSetting("injector"));
   }
  
   void UserSetControl(CabinControl p_control, float p_value)
   {
     Interface.Log("control: " + p_control.GetName() + " value: " + p_value);
  
       if (p_control == reverser_lever)
       loco.SetEngineSetting("reverser", p_value);
  
       // else if (p_control == throttle_lever)
       // loco.SetEngineSetting("throttle", p_value);
  
       else if (p_control == train_brake_lever)
       loco.SetEngineSetting("train-auto-brake", p_value);
  
       else if (p_control == train_lapbrake_lever)
       loco.SetEngineSetting("train-lap-brake", p_value);
  
       else if (p_control == loco_brake_lever)
       loco.SetEngineSetting("loco-auto-brake", p_value);
  
       else if (p_control == dynamic_brake_lever)
       loco.SetEngineSetting("dynamic-brake", p_value);
  
       else if (p_control == horn_rope)
       ; // todo
  
       else if (p_control == pantograph_lever)
       loco.SetEngineSetting("pantograph", p_value);
  
       else if (p_control == light_switch)
       loco.SetEngineSetting("headlight", p_value);
  
       else if (p_control == regulator_lever)
       loco.SetEngineSetting("regulator", p_value);
  
       else if (p_control == waterInjector0 or p_control == waterInjector1)
       loco.SetEngineSetting("injectorv, p_value);
   }
  
   void UserPressKey(string s)
   {
     if(s == "shovel-coal")
     {
       PB15CabinData cd = cast<PB15CabinData>loco.GetCabinData();
       // if(cd.fireboxDoorOpen)
       {
         if(FireAddCoal())
         {
           if(!shovellingCoal)
           {
             if(!waving)
             {
               if(fire_plates)
                           fire_plates.SetValue(1.0f);
                           shovellingCoal = true;
                           SetFXAnimationState("idle1", false);
                           SetFXAnimationState("idle2", false);
                           SetFXAnimationState("loop2shovel", false);
                           SetFXAnimationState(vloop2shovel", true);
                       }
                   }
               }
           }
       }
       else if(s == "wave")
       {
         if(!shovellingCoal)
         {
           if(!waving)
           {
             waving = true;
             SetFXAnimationState("wave", false);
             SetFXAnimationState("wave", true);
           }
         }
       }
   }
   thread void RunAnimation(void)
   {
     SetFXAnimationState("idle1", true);
  
       wait()
       {
         on "Animation-Event", "Coalman_loop1_end":
         SetFXAnimationState("idle1", false);
         SetFXAnimationState("idle2", true);
         continue;
  
           on "Animation-Event", "Coalman_loop2_end":
           SetFXAnimationState("idle2", false);
           SetFXAnimationState("idle1", true);
           continue;
  
           on "Animation-Event", "Coalman_loop2shovel_end":
           SetFXAnimationState("loop2shovel", false);
           SetFXAnimationState("shovel", true);
           continue;
  
           on "Animation-Event", "Coalman_shovel_end":
           SetFXAnimationState("shovel", false);
           SetFXAnimationState("wipebrow", true);
           continue;
  
           on "Animation-Event", "Coalman_wipebrow_end":
           SetFXAnimationState("wipebrow", false);
           SetFXAnimationState("shovel2loop", true);
           continue;
  
           on "Animation-Event", "Coalman_shovel2loop_end":
           SetFXAnimationState("shovel2loop", false);
           SetFXAnimationState("idle1", true);
           shovellingCoal = false;
           continue;
  
           on "Animation-Event", "Coalman_wave_end":
           SetFXAnimationState("wave", false);
           SetFXAnimationState("idle1", true);
           waving = false;
           continue;
  
       }
   }
 };


Steam Cab Config.txt

Diesel Cab


Return to CCG Index

Content Creator's Guide

Personal tools