Need help from an lua scripter ....

General discussion about Train Simulator, your thoughts, questions, news and views!

Moderator: Moderators

Locked
mrconner1973
Getting the hang of things now
Posts: 67
Joined: Sun Oct 02, 2011 12:40 pm

Need help from an lua scripter ....

Post by mrconner1973 »

Hi all,

I am building levelcrossings and now i have an level crossing with an flashing white lamp if there is no train. When the train arrives the white lamp stops flashing and 2 red lamps start flashing. Now i have an lua script who flashes the red lights and turn the white lamp off, but the white lamp doesnt flash. Is there somebody here who can look into my lua script and change for me the script so that also my white lamp is flashing if there is no train ? You can send me an pm and i will send you my emailadres so i can send the lua script.

I hope somebody can help me out.

Regards,
mrconner1973
Getting the hang of things now
Posts: 67
Joined: Sun Oct 02, 2011 12:40 pm

Re: Need help from an lua scripter ....

Post by mrconner1973 »

Nobody who can help me out ?? In need of somebody who can help me out with some lua programming. All is already in my lua but my white lamp is programmed wrong and i am not able to change the lua script. :-?
shanyiqua
Been on the forums for a while
Posts: 262
Joined: Tue Dec 20, 2011 11:48 am

Re: Need help from an lua scripter ....

Post by shanyiqua »

Why don't you share it here between code][/code tags?
mrconner1973
Getting the hang of things now
Posts: 67
Joined: Sun Oct 02, 2011 12:40 pm

Re: Need help from an lua scripter ....

Post by mrconner1973 »

shanyiqua wrote:Why don't you share it here between code][/code tags?
Ok no problem. :wink:

Code: Select all

 
-- States enumeration
LC_STATE_NONE = -1
LC_STATE_OPEN = 0
LC_STATE_APPROACH = 1
LC_STATE_CLOSING = 2
LC_STATE_CLOSED = 3
LC_STATE_OPENING = 4


-- Animation names
ANIM_OPEN = ""
ANIM_CLOSED = ""

NODE_WARNING = ""
NODE_CLEAR = "lc_light_white"
NODE_CLOSED_LEFT = "lc_light_redleft"
NODE_CLOSED_RIGHT = "lc_light_redright"

WARN_LIGHT_FLASH_SECS = 0.3

-- Trigger distances
WARN_DISTANCE = 1000.0
CLOSE_DISTANCE = 800.0
PASS_DISTANCE = -100.0
CLOSED_TIME = 5.0
WARN_TIME = 0.0

-- Current state
gState = LC_STATE_NONE
gWarningTime = 0.0
gWarningAnimTime = 0.0
gClosedTime = 0.0
gAnimTime = 0.0
gFlashSide = 0

-- debugging stuff
DEBUG = true	-- set to true to turn debugging on
function DebugPrint( message )
	if (DEBUG) then
		Print( message )
	end
end

--------------------------------------------
-- Initialise
function Initialise ()

	Call ( "*:BeginUpdate" );
	
	-- de-activate any warnings
	Call ( "Warning1:ActivateNode", NODE_CLEAR, 1 );
	Call ( "Warning2:ActivateNode", NODE_CLEAR, 1 );
	Call ( "Warning1:ActivateNode", NODE_CLOSED_LEFT, 0 );
	Call ( "Warning1:ActivateNode", NODE_CLOSED_RIGHT, 0 );
	Call ( "Warning2:ActivateNode", NODE_CLOSED_LEFT, 0 );
	Call ( "Warning2:ActivateNode", NODE_CLOSED_RIGHT, 0 );

end -- function Initialise ()

--------------------------------------------
-- OnConsistApproach
function OnConsistApproach ( frontDistance, endDistance, speed )

	distance = frontDistance;
	if frontDistance < 0.0 then
		distance = -frontDistance;
	end
	
	if endDistance >= 0.0 and endDistance < distance then
		distance = endDistance;
	end
	
	if endDistance < 0.0 and -endDistance < distance then
		distance = -endDistance;
	end
		
	--DebugPrint ( "OnConsistApproach: " .. distance .. " fr: " .. frontDistance .. " rr: " .. endDistance .. " speed: " .. speed );
	
	movingAway = false;
	if endDistance < PASS_DISTANCE then
		
		-- moving away over 100m passed gate open in 5.0seconds
		movingAway = true;
		
	end -- endDistance < PASS_DISTANCE ...
	
	if distance < CLOSE_DISTANCE then
	
		if gState == LC_STATE_OPEN or gState == LC_STATE_APPROACH or gState == LC_STATE_OPENING then
		
			if movingAway == false then
			
				CloseGates();
				
			end -- if movingAway == false then
		
		end -- if gState == LC_STATE_OPEN or ...
		
		if movingAway == false then
		
			gClosedTime = 0.0;
			gWarningTime = 0.0;
		
		end
		
	elseif distance < WARN_DISTANCE and movingAway == false then
	
		gWarningTime = 0.0;
		StartWarning();
	
	end -- if distance < 50.0 then ... else ...

end -- function OnConsistApproach ( distance )

--------------------------------------------
-- SetState
function SetState ( state )

	if gState ~= state then
	
		if state == LC_STATE_OPEN then
		
			StopWarning();
			
		elseif state == LC_STATE_OPENING then
		
			StartWarning();
			OpenGates();
			
		elseif state == LC_STATE_CLOSING then
		
			StartWarning();
			CloseGates();
			
		else
		
			StartWarning();
			Call ( "Barrier1:AddTime", ANIM_CLOSED, -1000.0 ); -- force closed
			Call ( "Barrier2:AddTime", ANIM_CLOSED, -1000.0 ); -- force closed
			
		end -- if state == LC_STATE_OPEN then ...
		
		gState = state;
	
	end -- if gState ~= state then

end -- function SetState ( state )

--------------------------------------------
-- Update
function Update ( dTime )

	if gState == LC_STATE_NONE then
	
		Call ( "Barrier1:AddTime", ANIM_CLOSED, 1000.0 ); -- force open
		Call ( "Barrier2:AddTime", ANIM_CLOSED, 1000.0 ); -- force open	
		Call ( "Warning1:ActivateNode", NODE_CLOSED_LEFT, 0 );
		Call ( "Warning1:ActivateNode", NODE_CLOSED_RIGHT, 0 );
		Call ( "Warning2:ActivateNode", NODE_CLOSED_LEFT, 0 );
		Call ( "Warning2:ActivateNode", NODE_CLOSED_RIGHT, 0 );
		Call ( "Warning1:ActivateNode", NODE_CLEAR, 1 );
		Call ( "Warning2:ActivateNode", NODE_CLEAR, 1 );


		Call ( "*:EndUpdate" );
		gState = LC_STATE_OPEN;
		
	elseif gState == LC_STATE_OPENING then
	
		gAnimTime = Call ( "Barrier1:AddTime", ANIM_OPEN, dTime );
		Call ( "Barrier2:AddTime", ANIM_OPEN, dTime );
		UpdateWarning ( dTime );
		if gAnimTime ~= 0.0 then
		
			DebugPrint ( "Gate open" );
			StopWarning();
		
		end -- if gAnimTime < 0.f then		
	
	elseif gState == LC_STATE_CLOSING then
	
		gAnimTime = Call ( "Barrier1:AddTime", ANIM_CLOSED, -dTime );
		Call ( "Barrier2:AddTime", ANIM_CLOSED, -dTime );
		UpdateWarning ( dTime );
		if gAnimTime ~= 0.0 then
		
			DebugPrint ( "Gate closed" );
			gState = LC_STATE_CLOSED;
			Call ( "*:SetCrossingState", LC_STATE_CLOSED );
			gClosedTime = 0.0;
		
		end -- if gAnimTime < 0.f then
	
	elseif gState == LC_STATE_CLOSED then
	
		gClosedTime = gClosedTime + dTime;
		UpdateWarning ( dTime );
		if gClosedTime > CLOSED_TIME then
		
			OpenGates();
		
		end -- if gClosedTime > 30.0 then
		
	elseif gState == LC_STATE_APPROACH then 
	
		gWarningTime = gWarningTime + dTime;
		UpdateWarning ( dTime );
		if gWarningTime > WARN_TIME then
		
			StopWarning();
		
		end -- if gWarningTime > 30.0 then
		
	end -- if gState == LC_STATE_OPENING then ...  elseif ...

end -- function Update ( time )

--------------------------------------------
-- CloseGates
function CloseGates()

	DebugPrint ( "Gate closing" );

	gState = LC_STATE_CLOSING;
	Call ( "*:SetCrossingState", LC_STATE_CLOSING );
	Call ( "*:BeginUpdate" );
	
	-- start sounds
	Call ( "Sound:SetParameter", "CrossingSound", 1 );
	Call ( "Sound:SetParameter", "GateMotor", 1 );

end -- function CloseGates()

--------------------------------------------
-- OpenGates
function OpenGates()

	DebugPrint ( "Gate opening" );

	gState = LC_STATE_OPENING;
	Call ( "*:SetCrossingState", LC_STATE_OPENING );
	Call ( "*:BeginUpdate" );
	
	Call ( "Sound:SetParameter", "GateMotor", 1 );

end -- function OpenGates()

--------------------------------------------
-- StartWarning
function StartWarning()

	if gState == LC_STATE_OPEN then
	
		gState = LC_STATE_APPROACH;
		gFlashSide = 0;
		gWarningAnimTime = 0.0
		
		Call ( "*:SetCrossingState", LC_STATE_APPROACH );
		Call ( "*:BeginUpdate" );
		
		-- Show amber warning
		Call ( "Warning1:ActivateNode", NODE_WARNING, 1 );
		Call ( "Warning2:ActivateNode", NODE_WARNING, 1 );
		
	end -- if gState ~= LC_STATE_APPROACH then
	
end -- function StartWarning()


--------------------------------------------
-- StopWarning
function StopWarning()

	gState = LC_STATE_OPEN;
	Call ( "*:SetCrossingState", LC_STATE_OPEN );
	Call ( "*:EndUpdate" );
	
	-- de-activate any warnings
	Call ( "Warning1:ActivateNode", NODE_CLOSED_LEFT, 0 );
	Call ( "Warning1:ActivateNode", NODE_CLOSED_RIGHT, 0 );
	Call ( "Warning2:ActivateNode", NODE_CLOSED_LEFT, 0 );
	Call ( "Warning2:ActivateNode", NODE_CLOSED_RIGHT, 0 );
	Call ( "Warning1:ActivateNode", NODE_CLEAR, 1 );
	Call ( "Warning2:ActivateNode", NODE_CLEAR, 1 );

	-- stop sounds
	Call ( "Sound:SetParameter", "CrossingSound", 0 );
	
end -- function StopWarning()

-----------------------------------------------
-- UpdateWarning
function UpdateWarning ( timeDelta )

	if gState == LC_STATE_APPROACH then
	
		Call ( "Warning1:ActivateNode", NODE_WARNING, 1 );
		Call ( "Warning2:ActivateNode", NODE_WARNING, 1 );
	
	else
	
		Call ( "Warning1:ActivateNode", NODE_WARNING, 0 );
		Call ( "Warning2:ActivateNode", NODE_WARNING, 0 );
		
		gWarningAnimTime = gWarningAnimTime + timeDelta;
		if gWarningAnimTime > WARN_LIGHT_FLASH_SECS then
		
			gWarningAnimTime = gWarningAnimTime - WARN_LIGHT_FLASH_SECS;
			gFlashSide = 1 - gFlashSide;
			
		end
		
		Call ( "Warning1:ActivateNode", NODE_CLOSED_LEFT, gFlashSide );
		Call ( "Warning1:ActivateNode", NODE_CLOSED_RIGHT, 1 - gFlashSide );
		Call ( "Warning2:ActivateNode", NODE_CLOSED_LEFT, gFlashSide );
		Call ( "Warning2:ActivateNode", NODE_CLOSED_RIGHT, 1 - gFlashSide );
		Call ( "Warning1:ActivateNode", NODE_CLEAR, 0 );
		Call ( "Warning2:ActivateNode", NODE_CLEAR, 0 );

	end -- if gState == LC_STATE_APROACH then
	
end -- function UpdateWarning()

User avatar
AndiS
Very Active Forum Member
Posts: 6207
Joined: Fri Sep 23, 2005 4:43 pm
Location: Jester's cell in ivory tower
Contact:

Re: Need help from an lua scripter ....

Post by AndiS »

It's not like nobody cares to help. But it is not so easy to come up with a quick fix.

You need at least the following parts to solve your problem:

1) Update must run all the time, as I understand you are going to flash one light or the other all the time.
This is established by the following:

1a) Keep

Code: Select all

function Initialise ()

   Call ( "*:BeginUpdate" );
but remove all other instances. To do so, put two dashes (--) before Call in all lines containing BeginUpdate.

1b) Remove all

Code: Select all

Call ( "*:EndUpdate" );
Again, you will search for EndUpdate and put -- at the start of each line found.

2) You need some sort of UpdateClear function. This is not trivial.
The "easy" part is to duplicate UpdateWarning at the end of the file and duplicate all the variables used in it. Before that, you can shrink it to something like this:

Code: Select all

function UpdateClear ( timeDelta )

      gWarningAnimTime = gWarningAnimTime + timeDelta;
      if gWarningAnimTime > WARN_LIGHT_FLASH_SECS then
      
         gWarningAnimTime = gWarningAnimTime - WARN_LIGHT_FLASH_SECS;
         gFlashSide = 1 - gFlashSide;
         
      end
      
      Call ( "Warning1:ActivateNode", NODE_CLEAR, gFlashSide );
      Call ( "Warning2:ActivateNode", NODE_CLEAR, 1 - gFlashSide );

end
This means that we only deal with the clear light here. I hope NODE_CLEAR refers to the white one.

Note how I replaced 0 from the original code by gFlashSide and its contrary, to make the two lights flash alternately like the red ones.

Now comes the non-trivial part: You need to duplicate all variables in this function. I mean, they must not be the same as in the UpdateWarning function. E.g., you could append "Clear" to the names of gWarningAnimTime and gFlashSide. However, you need to hunt down all instances of the two variables in the rest of the script and duplicate the lines there, too, to initialise them etc.. But this cannot be done blindly.

3) You need to call UpdateClear somewhere, most likely in function Update after

Code: Select all

if gState == LC_STATE_NONE then
4) You need to get rid of most ActivateNode calls concerning NODE_CLEAR in other parts of the script, as they will interfere with the blinking. However, some will stay, those which turn the white light off to only show the red one (flashing) in some phases. I am not sure, in which phases this would be. And I cannot quickly point to the right lines.

I hope you understand that you need someone who has enough time for quite a few rounds of testing, if you cannot do it yourself. I must admit that my list of things to do is just too long to enter anything at the bottom of it.
mrconner1973
Getting the hang of things now
Posts: 67
Joined: Sun Oct 02, 2011 12:40 pm

Re: Need help from an lua scripter ....

Post by mrconner1973 »

Thanx for explaining to me how it works but it is for me yibba language. Maybe someone else understand and can make me a new complete lua script with this information.
markpullinger
Very Active Forum Member
Posts: 3105
Joined: Sun Jun 08, 2003 6:24 pm

Re: Need help from an lua scripter ....

Post by markpullinger »

@ Mrconner, your answer makes me question how you wrote the original script in the first place if it doesn't make sense to you!
User avatar
TjoeTjoe
Been on the forums for a while
Posts: 106
Joined: Fri Nov 17, 2006 8:39 pm

Re: Need help from an lua scripter ....

Post by TjoeTjoe »

markpullinger wrote:@ Mrconner, your answer makes me question how you wrote the original script in the first place if it doesn't make sense to you!
If I am not wrong, Mrconner is the developer of Dutch railroad crossing models (http://www.coha.nl) I met in person a while ago during the Railsim.nl event in Holland.
There he told me that main frustrating part of getting the models to work, was the scripting. With the help of somebody else (Fopix3D) he eventually managed to get his versions with the barriers working.
And I think that he just got the script, so no detailed programming here

He is now working on the so called AKI's. Those are level crossings where we have only warning lights, either flashing white when no train is approaching, or flashing reds when train is approaching, but no barriers.
If we look into the script as posted here in the topic, I think Conner has taken his barrier-version script (which is a could starting point) and tries to modify that one to get it working for the AKI-version

AndiS' explanations are excellent (thanks for that), but as Conner is definitely not to familiar with the LUA-language I can understand that it is all abacadabra to him.
Let me see if I can translate AndiS' into one script so that Conner is clear to go.

Erwin
PS: @Conner, just remind me to get back on this if arranging the script for you is taking to long. Give me at least 48 hours :-) You may PM me in Dutch if you prefer so
User avatar
AndiS
Very Active Forum Member
Posts: 6207
Joined: Fri Sep 23, 2005 4:43 pm
Location: Jester's cell in ivory tower
Contact:

Re: Need help from an lua scripter ....

Post by AndiS »

Thanks for volunteering. If you find some residual problem (and you will), I am happy to share some more thoughts. I know from experience that it takes many iterations of code modification and testing to reach the goal.

Level crossings are just too diverse around the world. I even tried writing a quite generic script for one - long ago - but flashing lights while there is no train is the last thing I would have covered. :) Still it makes sense, in the prototype, but like I said above, it is not a single step to get there.
markpullinger
Very Active Forum Member
Posts: 3105
Joined: Sun Jun 08, 2003 6:24 pm

Re: Need help from an lua scripter ....

Post by markpullinger »

Hi, ok that's fine, I'm not a mind reader, I do not know the person and it came across as someone who had grabbed some scripts & wanted someone to update them to work for him. Apologies if any offence caused. :oops:

Mark
User avatar
TjoeTjoe
Been on the forums for a while
Posts: 106
Joined: Fri Nov 17, 2006 8:39 pm

Re: Need help from an lua scripter ....

Post by TjoeTjoe »

MrConner seems to have it working now. (see http://www.youtube.com/watch?v=zc7Zo8gMVWE&feature=plcp
Just wished that he would have mentioned that here. Could have saved some of my free time to work the script out. I almost had it finished.
User avatar
AndiS
Very Active Forum Member
Posts: 6207
Joined: Fri Sep 23, 2005 4:43 pm
Location: Jester's cell in ivory tower
Contact:

Re: Need help from an lua scripter ....

Post by AndiS »

Sorry to see this - asking for help at 11:11 and uploading the solution without any comment here before midnight on the same day.
Quite unfortunate for forums to see some people not value the time it takes to help them.

Luckily, such incidents are rare. Otherwise, everyone would just hide away with a few friends whose social interaction modalities are ascertained.
mrconner1973
Getting the hang of things now
Posts: 67
Joined: Sun Oct 02, 2011 12:40 pm

Re: Need help from an lua scripter ....

Post by mrconner1973 »

Guys, problem solved. Thanx.
Locked

Return to “[TS] General Discussion”