~gpcf/advtrains-devel

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
3 2

[PATCH 1/1] [LuaATC] Add get_fc() and set_fc() commands

Details
Message ID
<20221117151957.1564-1-git.maverick2797@gmail.com>
DKIM signature
missing
Download raw message
Patch: +50 -7
get_fc: returns a table of each wagon's FC codes
set_fc: set a table to overwrite the FC codes of a train's wagons
---
 advtrains_luaautomation/README.md    | 25 ++++++++++++++++------
 advtrains_luaautomation/atc_rail.lua | 32 ++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 7 deletions(-)
 mode change 100644 => 100755 advtrains_luaautomation/atc_rail.lua

diff --git a/advtrains_luaautomation/README.md b/advtrains_luaautomation/README.md
index 67a4b80..a885075 100755
--- a/advtrains_luaautomation/README.md
+++ b/advtrains_luaautomation/README.md
@@ -255,7 +255,10 @@ In addition to the above environment functions, the following functions are avai
	The interlocking system uses this property for Automatic Routesetting.

#### Shunting Functions and Variables
There are several functions available especially for shunting operations. Some of these functions make use of Freight Codes (FC) set in the Wagon Properties of each wagon and/or locomotive:
There are several functions available especially for shunting operations.
Some of these functions make use of Freight Codes (FC) set in the Wagon Properties of each wagon and/or locomotive.
FCs are composed of codes separated by exclamation marks (`!`), for instance `"foo!bar!baz"`.
Each wagon has a current FC, indicating its next destination.

 - `split_at_index(index, atc_command)`
	Splits the train at the specified index, into a train with index-1 wagons and a second train starting with the index-th wagon. The `atc_command` specified is sent to the second train after decoupling. `"S0"` or `"B0"` is common to ensure any locomotives in the remaining train don't continue to move.
@@ -266,6 +269,18 @@ There are several functions available especially for shunting operations. Some o
	Command: `split_at_index(4,"S0")`  
	Result: first train (continues at previous speed): `"foo","foo","foo"`, second train (slows at S0): `"bar","bar","bar"`

 - `get_fc()`
	Returns a table with the entire FC list for each wagon in the train.  
	Command: `get_fc()`  
	Result: `{"", "foo!bar", "testing", "fc_1!fc_2!fc_3!?", "hello_world"}`
	
 - `set_fc(fc_list)`
	Overwrites the FC list according to a table `fc_list`. A false or nil entry will leave the wagon unaffected, however all others will be overwritten.
	Useful for mass-programming freight trains that use FC-shunting instead of walking to each wagon individually.  
	Example: train has FC lists: `"", "foo!bar", "testing", "fc_1!fc_2!fc_3!?", "hello_world"`  
	Command: `set_fc({"", "foo!turtle", nil, "4tehlulz", false})`  
	Result: `""` `"foo!turtle"` `"testing"` `"4tehlulz"` `"hello_world"`

 - `split_at_fc(atc_command, len)`
	Splits the train in such a way that all cars with non-empty current FC of the first part of the train have the same FC. The
	`atc_command` specified is sent to the rear part, as with	split_at_index. It returns the fc of the cars of the first part.
@@ -289,15 +304,11 @@ There are several functions available especially for shunting operations. Some o
	first part of the train as above.

 - `step_fc()`
	Steps the FCs of all train cars forward. FCs are composed of codes
	separated by exclamation marks (`!`), for instance
	`"foo!bar!baz"`. Each wagon has a current FC, indicating its next
	destination. Stepping the freight code forward, selects the next
	code after the !. If the end of the string is reached, then the
	Steps the FCs of all train cars forward, selecting the next
	code after the `!`. If the end of the string is reached, then the
	first code is selected, except if the string ends with a question
	mark (`?`), then the order is reversed.


 - `train_length()`
	returns the number of cars the train is composed of.

diff --git a/advtrains_luaautomation/atc_rail.lua b/advtrains_luaautomation/atc_rail.lua
old mode 100644
new mode 100755
index 5dde99c..aac11f0
--- a/advtrains_luaautomation/atc_rail.lua
+++ b/advtrains_luaautomation/atc_rail.lua
@@ -91,6 +91,38 @@ function r.fire_event(pos, evtdata, appr_internal)
			if not train_id then return false end
			advtrains.train_step_fc(train)
		end,
		get_fc = function()
			if not train_id then return end
			local fc_list = {}
			for index,wagon_id in ipairs(train.trainparts) do
				fc_list[index] = table.concat(advtrains.wagons[wagon_id].fc,"!") or ""
			end
			return fc_list
		end,
		set_fc = function(fc_list)
			assertt(fc_list, "table")
			if not train_id then return false end
			-- safety type-check for entered values
			for _,v in ipairs(fc_list) do
				if v and type(v) ~= "string" then
					error("FC entries must be a string")
					return
				end
			end
			for index,wagon_id in ipairs(train.trainparts) do
				if fc_list[index] then -- has FC to enter to this wagon
					local data = advtrains.wagons[wagon_id]
					if data then -- wagon actually exists
						for _,wagon in pairs(minetest.luaentities) do -- find wagon entity
							if wagon.is_wagon and wagon.initialized and wagon.id==wagon_id then
								wagon.set_fc(data,fc_list[index]) -- overwrite to new FC
								break -- no point cycling through every other entity. we found our wagon
							end
						end
					end
				end
			end
		end,
		set_shunt = function()
			-- enable shunting mode
			if not train_id then return false end
-- 
2.38.0
Details
Message ID
<2c95ef47-b8a4-175e-8c5a-30a6e95eca0f@forksworld.de>
In-Reply-To
<20221117151957.1564-1-git.maverick2797@gmail.com> (view parent)
DKIM signature
missing
Download raw message
> diff --git a/advtrains_luaautomation/atc_rail.lua b/advtrains_luaautomation/atc_rail.lua
> old mode 100644
> new mode 100755

I assume the mode change is an accident?

> index 5dde99c..aac11f0
> --- a/advtrains_luaautomation/atc_rail.lua
> +++ b/advtrains_luaautomation/atc_rail.lua
> @@ -91,6 +91,38 @@ function r.fire_event(pos, evtdata, appr_internal)
>   			if not train_id then return false end
>   			advtrains.train_step_fc(train)
>   		end,
> +		get_fc = function()
> +			if not train_id then return end
> +			local fc_list = {}
> +			for index,wagon_id in ipairs(train.trainparts) do

IMO you can use pairs() here as well.

> +				fc_list[index] = table.concat(advtrains.wagons[wagon_id].fc,"!") or ""
> +			end
> +			return fc_list
> +		end,
> +		set_fc = function(fc_list)

Would set_fc(fc_str, i, j) perhaps be useful? (I don't use FC myself)

> +			assertt(fc_list, "table")
> +			if not train_id then return false end
> +			-- safety type-check for entered values
> +			for _,v in ipairs(fc_list) do
> +				if v and type(v) ~= "string" then
> +					error("FC entries must be a string")
> +					return
> +				end
> +			end
> +			for index,wagon_id in ipairs(train.trainparts) do
> +				if fc_list[index] then -- has FC to enter to this wagon
> +					local data = advtrains.wagons[wagon_id]
> +					if data then -- wagon actually exists
> +						for _,wagon in pairs(minetest.luaentities) do -- find wagon entity
> +							if wagon.is_wagon and wagon.initialized and wagon.id==wagon_id then
> +								wagon.set_fc(data,fc_list[index]) -- overwrite to new FC
> +								break -- no point cycling through every other entity. we found our wagon
> +							end
> +						end

Does this also work when the wagon is unloaded? If not, then we likely 
need to make some adjustments to the FC setter.

> +					end
> +				end
> +			end
> +		end,
>   		set_shunt = function()
>   			-- enable shunting mode
>   			if not train_id then return false end

The patch looks good besides what I mentioned above.

On a less related note:
- It would IMO be nice if someone could work on unittests for LuaATC, 
especially that the test world is not longer particularly sensible.
- CI does not seem to run for patches.
Details
Message ID
<c483099b-8724-4287-65e9-5defcced263a@gmail.com>
In-Reply-To
<2c95ef47-b8a4-175e-8c5a-30a6e95eca0f@forksworld.de> (view parent)
DKIM signature
missing
Download raw message
>  I assume the mode change is an accident?

yeah not sure why that decided to change that. I noticed a couple of my 
previous patches may have done the same thing (looking at 
environment.lua and README.md). I'll do a separate patch to

> Would set_fc(fc_str, i, j) perhaps be useful? (I don't use FC myself)

I'm not sure what you mean here. A function to apply the FC to only the 
i-th wagon in the trainparts instead? That could work. Not sure which 
would be more useful in practice though...

> Does this also work when the wagon is unloaded? If not, then we likely
> need to make some adjustments to the FC setter.

ah... didn't test that. Yeah that doesn't work once you get a distance 
away from the wagons. I noticed advtrains.wagon_objects in wagons.lua... 
would something to do with this work I wonder?
Details
Message ID
<257ae643-6fb2-80e0-4b51-cd50f63b1a3a@forksworld.de>
In-Reply-To
<c483099b-8724-4287-65e9-5defcced263a@gmail.com> (view parent)
DKIM signature
missing
Download raw message
>> Would set_fc(fc_str, i, j) perhaps be useful? (I don't use FC myself)
> 
> I'm not sure what you mean here. A function to apply the FC to only the 
> i-th wagon in the trainparts instead? That could work. Not sure which 
> would be more useful in practice though...

I am thinking about making the behavior of set_fc depend on the first 
argument passed to it:

- If the first argument is a string, then the FC of the wagons from i to 
j are set to the string (if absent, i and j can be assumed to be the 
first and last indices, for consistency with Lua's library functions)
- If the first argument is a table, then the FC of the wagons are set 
like in your patch.

> 
>> Does this also work when the wagon is unloaded? If not, then we likely
>> need to make some adjustments to the FC setter.
> 
> ah... didn't test that. Yeah that doesn't work once you get a distance 
> away from the wagons. I noticed advtrains.wagon_objects in wagons.lua... 
> would something to do with this work I wonder?

It appears to me that the FC setter is defined in Lua entities (hence 
this quetion). Considering that set_fc does not involve the Lua entity 
of the wagon, a generic advtrains.set_fc(wid, fcstr) would IMO make more 
sense. The "wagons properties" formspec appears to be the only thing in 
the Advtrains source code that uses wagon.set_fc, so wagon.set_fc can be 
removed or used as an auxiliary function.

This appears to also be relevant for a few other functions involving FC.

Also: regarding the FC getter: would it be more helpful to get the 
current FC (or return the FC string starting from the current index - 
the resulting string may be more complicated than just table.concat)?
Reply to thread Export thread (mbox)