~gpcf/advtrains-devel

De-hardcode the speed limit of 20 m/s v5 PROPOSED

1F616EMO: 1
 De-hardcode the speed limit of 20 m/s

 12 files changed, 88 insertions(+), 23 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~gpcf/advtrains-devel/patches/55556/mbox | git am -3
Learn more about email & git

[PATCH v5] De-hardcode the speed limit of 20 m/s Export this patch

This patch de-hardcodes that speed limit. To adapt the changes,
the following changes are made:

1. Added a setting key: advtrains_max_speed, acceppting integers from 1 to 99.
   Any values beyond the limit wil be capped. This setting is marked experimental
2. Added signal signs from 24 to 40 m/s.
3. Added max_speed = 20 m/s to the wagon metatable, so wagons not specifying a
   maximum speed continues to use 20 m/s.
4. Adjusted the train HUD so trains with maximum speed more than 20 m/s
   uses each segment to represent max / 20 m/s.
5. Adjusted speed target / limit / LZB indicators so they work well on the new
   segment bar
6. (Not so related) Out-of-bound LZB limitations are marked with a
   red arrow pointing right at where 20 m/s were.
---
 advtrains/init.lua                            |   7 +++
 advtrains/settingtypes.txt                    |   4 ++
 advtrains/trainhud.lua                        |  44 +++++++++++++--
 advtrains/trainlogic.lua                      |   2 +-
 advtrains/wagons.lua                          |   1 +
 advtrains_signals_ks/init.lua                 |  53 ++++++++++++------
 .../textures/advtrains_signals_ks_sign_24.png | Bin 0 -> 114 bytes
 .../textures/advtrains_signals_ks_sign_28.png | Bin 0 -> 112 bytes
 .../textures/advtrains_signals_ks_sign_30.png | Bin 0 -> 97 bytes
 .../textures/advtrains_signals_ks_sign_32.png | Bin 0 -> 114 bytes
 .../textures/advtrains_signals_ks_sign_36.png | Bin 0 -> 103 bytes
 .../textures/advtrains_signals_ks_sign_40.png | Bin 0 -> 103 bytes
 12 files changed, 88 insertions(+), 23 deletions(-)
 create mode 100644 advtrains_signals_ks/textures/advtrains_signals_ks_sign_24.png
 create mode 100644 advtrains_signals_ks/textures/advtrains_signals_ks_sign_28.png
 create mode 100644 advtrains_signals_ks/textures/advtrains_signals_ks_sign_30.png
 create mode 100644 advtrains_signals_ks/textures/advtrains_signals_ks_sign_32.png
 create mode 100644 advtrains_signals_ks/textures/advtrains_signals_ks_sign_36.png
 create mode 100644 advtrains_signals_ks/textures/advtrains_signals_ks_sign_40.png

diff --git a/advtrains/init.lua b/advtrains/init.lua
index 06e456f..ba62c4a 100644
--- a/advtrains/init.lua
+++ b/advtrains/init.lua
@@ -66,6 +66,13 @@ if not advtrains.wagon_load_range then
	advtrains.wagon_load_range = tonumber(minetest.settings:get("active_block_range"))*16
end

-- Maximum speed of trains
-- The real maximum speed is the minimum value of all wagon's max speed and this value
-- Default: 20; Min: 1; Max: 99
-- Max. 99 because of the train HUD 7-seg display
advtrains.TRAIN_MAX_SPEED = tonumber(minetest.settings:get("advtrains_max_speed")) or 20
advtrains.TRAIN_MAX_SPEED = math.max(math.min(advtrains.TRAIN_MAX_SPEED, 99), 1)

--pcall
local no_action=false

diff --git a/advtrains/settingtypes.txt b/advtrains/settingtypes.txt
index 2b627cb..3f02058 100644
--- a/advtrains/settingtypes.txt
+++ b/advtrains/settingtypes.txt
@@ -61,3 +61,7 @@ advtrains_save_interval (Save Interval) int 60 20 3600
#    If enabled, trains only collide with nodes with "normal" drawtype.
advtrains_forgiving_collision (Forgiving Collision mode) bool false

#    Maximum speed of all trains
#    The real maximum speed is the minimum value of all wagon's max speed and this value
#    This is experimental. USE THIS AT YOUR OWN RISK!
advtrains_max_speed (Maximum Train Speed) int 20 1 99
diff --git a/advtrains/trainhud.lua b/advtrains/trainhud.lua
index f9f4876..731ad6d 100644
--- a/advtrains/trainhud.lua
+++ b/advtrains/trainhud.lua
@@ -220,12 +220,34 @@ function advtrains.hud_train_format(train, flip)
	hud:add_fill(215, 10, 15, 30, train.door_open==1 and "white" or "darkslategray"):add_fill(217, 12, 11, 11, "black")
	-- speed indication(s)
	hud:add_n7seg(320, 10, 110, 90, vel, 2, "red")
	hud:add_segmentbar_leftright(10, 65, 217, 20, 3, 20, max, 20, "darkslategray", 0, vel, "white")
	if res and res > 0 then
		hud:add_fill(7+res*11, 60, 3, 30, "red")
	do
		local segbar_pos
		if max > 20 then
			segbar_pos = math.floor(vel * 20 / max)
		else
			segbar_pos = vel
		end
		hud:add_segmentbar_leftright(10, 65, 217, 20, 3, 20, math.min(max, 20), 20, "darkslategray", 0, segbar_pos, "white")
	end
	if res and res > 0 and res <= max then
		local indi_pos
		if max > 20 then
			indi_pos = 7 + math.floor(res * 220 / max)
		else
			indi_pos = 7 + res * 11
		end
		hud:add_fill(indi_pos, 60, 3, 30, "red")
	end
	if train.tarvelocity then
		hud:add(1+train.tarvelocity*11, 85, T"advtrains_hud_arrow.png":transform"FY":multiply"cyan")
		do
			local arrow_pos
			if max > 20 then
				arrow_pos = 1 + train.tarvelocity * 220 / max
			else
				arrow_pos = 1 + train.tarvelocity * 11
			end
			hud:add(arrow_pos, 85, T"advtrains_hud_arrow.png":transform"FY":multiply"cyan")
		end
	end
	local lzbdisp
	local lzb = train.lzb
@@ -250,7 +272,19 @@ function advtrains.hud_train_format(train, flip)
			local c = not spd and "lime" or (type(spd) == "number" and (spd == 0) and "red" or "orange") or nil
			if c then
				if spd and spd~=0 then
					hud:add(1+spd*11, 50, T"advtrains_hud_arrow.png":multiply"red")
					do
						local texture = T"advtrains_hud_arrow.png":multiply"red"
						local arrow_pos
						if spd > math.max(max, 20) then
							texture = texture:transform"R90"
							arrow_pos = 221 -- 20
						elseif max > 20 then
							arrow_pos = 1 + math.floor(spd * 220 / max)
						else
							arrow_pos = 1 + spd * 11
						end
						hud:add(arrow_pos, 50, texture)
					end
				end
				local dist = math.floor(((oc[i].index or train.index)-train.index))
				dist = math.max(0, math.min(999, dist))
diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua
index f136577..0a3c6fe 100644
--- a/advtrains/trainlogic.lua
+++ b/advtrains/trainlogic.lua
@@ -1039,7 +1039,7 @@ function advtrains.update_trainpart_properties(train_id, invert_flipstate)
	local train=advtrains.trains[train_id]
	train.drives_on=advtrains.merge_tables(advtrains.all_tracktypes)
	--FIX: deep-copy the table!!!
	train.max_speed=20
	train.max_speed=advtrains.TRAIN_MAX_SPEED
	train.extent_h = 0;
	
	local rel_pos=0
diff --git a/advtrains/wagons.lua b/advtrains/wagons.lua
index 536c8d4..156db06 100644
--- a/advtrains/wagons.lua
+++ b/advtrains/wagons.lua
@@ -68,6 +68,7 @@ local wagon={
	wagon_width=3, -- Wagon width in meters
	has_inventory=false,
	static_save=false,
	max_speed = 20, -- Default value
}


diff --git a/advtrains_signals_ks/init.lua b/advtrains_signals_ks/init.lua
index 258b868..d614069 100755
--- a/advtrains_signals_ks/init.lua
+++ b/advtrains_signals_ks/init.lua
@@ -306,11 +306,17 @@ for _, rtab in ipairs({
	
	for typ, prts in pairs {
		-- Speed restrictions:
		["4"] = {asp = { main = 4, shunt = true }, n = "6"},
		["6"] = {asp = { main = 6, shunt = true }, n = "8"},
		["8"] = {asp = { main = 8, shunt = true }, n = "12"},
		["12"] = {asp = { main = 12, shunt = true }, n = "16"},
		["16"] = {asp = { main = 16, shunt = true }, n = "e"},
		["4"]  = {asp = { main =  4, shunt = true }, n = advtrains.TRAIN_MAX_SPEED >= 6 and "6" or "e"},
		["6"]  = {asp = { main =  6, shunt = true }, n = advtrains.TRAIN_MAX_SPEED >= 8 and "8" or "e"},
		["8"]  = {asp = { main =  8, shunt = true }, n = advtrains.TRAIN_MAX_SPEED >= 12 and "12" or "e"},
		["12"] = {asp = { main = 12, shunt = true }, n = advtrains.TRAIN_MAX_SPEED >= 16 and "16" or "e"},
		["16"] = {asp = { main = 16, shunt = true }, n = advtrains.TRAIN_MAX_SPEED >= 20 and "20" or "e"},
		["20"] = {asp = { main = 20, shunt = true }, n = advtrains.TRAIN_MAX_SPEED >= 24 and "24" or "e"},
		["24"] = {asp = { main = 24, shunt = true }, n = advtrains.TRAIN_MAX_SPEED >= 28 and "28" or "e"},
		["28"] = {asp = { main = 28, shunt = true }, n = advtrains.TRAIN_MAX_SPEED >= 30 and "30" or "e"},
		["30"] = {asp = { main = 30, shunt = true }, n = advtrains.TRAIN_MAX_SPEED >= 32 and "32" or "e"},
		["32"] = {asp = { main = 32, shunt = true }, n = advtrains.TRAIN_MAX_SPEED >= 36 and "36" or "e"},
		["36"] = {asp = { main = 36, shunt = true }, n = "e"},
		-- Speed restriction lifted
		["e"] = {asp = { main = -1, shunt = true }, n = "4", mesh = "_zs10"},
	} do
@@ -323,12 +329,19 @@ for _, rtab in ipairs({
	end

	for typ, prts in pairs {
		["4"]   = {main =  4, n = "6"},
		["6"]   = {main =  6, n = "8"},
		["8"]   = {main =  8, n = "12"},
		["12"]  = {main = 12, n = "16"},
		["16"]  = {main = 16, n = "e"},
		["e"] = {main = -1, n = "4"},
		["4"]  = {main =  4, n = advtrains.TRAIN_MAX_SPEED >= 6 and "6" or "e"},
		["6"]  = {main =  6, n = advtrains.TRAIN_MAX_SPEED >= 8 and "8" or "e"},
		["8"]  = {main =  8, n = advtrains.TRAIN_MAX_SPEED >= 12 and "12" or "e"},
		["12"] = {main = 12, n = advtrains.TRAIN_MAX_SPEED >= 16 and "16" or "e"},
		["16"] = {main = 16, n = advtrains.TRAIN_MAX_SPEED >= 20 and "20" or "e"},
		["20"] = {main = 20, n = advtrains.TRAIN_MAX_SPEED >= 24 and "24" or "e"},
		["24"] = {main = 24, n = advtrains.TRAIN_MAX_SPEED >= 28 and "28" or "e"},
		["28"] = {main = 28, n = advtrains.TRAIN_MAX_SPEED >= 30 and "30" or "e"},
		["30"] = {main = 30, n = advtrains.TRAIN_MAX_SPEED >= 32 and "32" or "e"},
		["32"] = {main = 32, n = advtrains.TRAIN_MAX_SPEED >= 36 and "36" or "e"},
		["36"] = {main = 36, n = advtrains.TRAIN_MAX_SPEED >= 40 and "40" or "e"},
		["40"] = {main = 40, n = "e"},
		["e"]  = {main = -1, n = "4"},
	} do
		local tile2 = "advtrains_signals_ks_sign_lf7.png^(advtrains_signals_ks_sign_"..typ..".png^[makealpha:255,255,255)"..(typ == "e" and "" or "^[multiply:orange")
		local inv = "advtrains_signals_ks_sign_lf7.png^(advtrains_signals_ks_sign_8.png^[makealpha:255,255,255)^[multiply:orange"
@@ -336,12 +349,18 @@ for _, rtab in ipairs({
	end

	for typ, prts in pairs {
		["4"]   = {main =  4, n = "6"},
		["6"]   = {main =  6, n = "8"},
		["8"]   = {main =  8, n = "12"},
		["12"]  = {main = 12, n = "16"},
		["16"]  = {main = 16, n = "20"},
		["20"]  = {main = 20, n = "4"},
		["4"]  = {main =  4, n = advtrains.TRAIN_MAX_SPEED >= 6 and "6" or "4"},
		["6"]  = {main =  6, n = advtrains.TRAIN_MAX_SPEED >= 8 and "8" or "4"},
		["8"]  = {main =  8, n = advtrains.TRAIN_MAX_SPEED >= 12 and "12" or "4"},
		["12"] = {main = 12, n = advtrains.TRAIN_MAX_SPEED >= 16 and "16" or "4"},
		["16"] = {main = 16, n = advtrains.TRAIN_MAX_SPEED >= 20 and "20" or "4"},
		["20"] = {main = 20, n = advtrains.TRAIN_MAX_SPEED >= 24 and "24" or "4"},
		["24"] = {main = 24, n = advtrains.TRAIN_MAX_SPEED >= 28 and "28" or "4"},
		["28"] = {main = 28, n = advtrains.TRAIN_MAX_SPEED >= 30 and "30" or "4"},
		["30"] = {main = 30, n = advtrains.TRAIN_MAX_SPEED >= 32 and "32" or "4"},
		["32"] = {main = 32, n = advtrains.TRAIN_MAX_SPEED >= 36 and "36" or "4"},
		["36"] = {main = 36, n = advtrains.TRAIN_MAX_SPEED >= 40 and "40" or "4"},
		["40"] = {main = 40, n = "4"},
	} do
		local tile2 = "advtrains_signals_ks_sign_lf7.png^(advtrains_signals_ks_sign_"..typ..".png^[makealpha:255,255,255)"
		local inv = "advtrains_signals_ks_sign_lf7.png^(advtrains_signals_ks_sign_8.png^[makealpha:255,255,255)"
diff --git a/advtrains_signals_ks/textures/advtrains_signals_ks_sign_24.png b/advtrains_signals_ks/textures/advtrains_signals_ks_sign_24.png
new file mode 100644
index 0000000000000000000000000000000000000000..29d54aeaae76483467de6a2bf6444794b0abdeea
GIT binary patch
literal 114
zcmeAS@N?(olHy`uVBq!ia0vp^3Lwk~Bp9L@-6MgNrKgKyNCji^4}O*#osa7qkGz}j
zut?11&)+VuJ*~p!iAVAUKGy0_sJPFd@~)ZZ2EWp)k3u~TOw)}Sgaayy-mEA+0W!?f
L)z4*}Q$iB}0;we}

literal 0
HcmV?d00001

diff --git a/advtrains_signals_ks/textures/advtrains_signals_ks_sign_28.png b/advtrains_signals_ks/textures/advtrains_signals_ks_sign_28.png
new file mode 100644
index 0000000000000000000000000000000000000000..ee4719186c6fe580deecb64229121a0c3521d08e
GIT binary patch
literal 112
zcmeAS@N?(olHy`uVBq!ia0vp^3Lwk~Bp9L@-6MgNxu=U`NCji^4}O*#osat)kG$LM
zu&7La!jJCx3@d;0Uiv8OvWLC!eY489=8md4EFQAT0qlC|3=G;@rKOKm!ka)wdAj<!
JtaD0e0sx;hB7^_{

literal 0
HcmV?d00001

diff --git a/advtrains_signals_ks/textures/advtrains_signals_ks_sign_30.png b/advtrains_signals_ks/textures/advtrains_signals_ks_sign_30.png
new file mode 100644
index 0000000000000000000000000000000000000000..97413bdc17477666ff4c38c4850b48bcffb684be
GIT binary patch
literal 97
zcmeAS@N?(olHy`uVBq!ia0vp^3Lwk~Bp9L@-6MgNhNp{TNCji^4}O*#oZpT+Jo+B1
t@UdLp@sW8)Li^@_Le34n?GY`PB^mt0%41FQ^LjvfJzf1=);T3K0RU*I8~OkM

literal 0
HcmV?d00001

diff --git a/advtrains_signals_ks/textures/advtrains_signals_ks_sign_32.png b/advtrains_signals_ks/textures/advtrains_signals_ks_sign_32.png
new file mode 100644
index 0000000000000000000000000000000000000000..039acaafcbc542fa3c82fe0a171adbd12f0c6289
GIT binary patch
literal 114
zcmeAS@N?(olHy`uVBq!ia0vp^3Lwk~Bp9L@-6MgNrKgKyNCji^4}O*#oZlWhJo=ue
z@Nv7C%b%m}f7k@y>~~!AUSZ>Rg-3QBH~5ua6$<QkWU+o+Kj$0+!`#BMr*n)XLO_Oj
My85}Sb4q9e04)F}1^@s6

literal 0
HcmV?d00001

diff --git a/advtrains_signals_ks/textures/advtrains_signals_ks_sign_36.png b/advtrains_signals_ks/textures/advtrains_signals_ks_sign_36.png
new file mode 100644
index 0000000000000000000000000000000000000000..e57231dcec50ddfdd8f03034a0c0428c47c8e0d8
GIT binary patch
literal 103
zcmeAS@N?(olHy`uVBq!ia0vp^3Lwk~Bp9L@-6MgNo~MgrNCji^4}O*#oZlWhJo=ue
z@Nv7C%azB<b<G@Q<pQUr7Bw3%n#m;z`lT~4%;_zBdbBU}HOK@{S3j3^P6<r_6;K{f

literal 0
HcmV?d00001

diff --git a/advtrains_signals_ks/textures/advtrains_signals_ks_sign_40.png b/advtrains_signals_ks/textures/advtrains_signals_ks_sign_40.png
new file mode 100644
index 0000000000000000000000000000000000000000..c555331f26135aae8cadfb51705bf0d8ac937657
GIT binary patch
literal 103
zcmeAS@N?(olHy`uVBq!ia0vp^3Lwk~Bp9L@-6MgNo~MgrNCji^4}O*#o%j12k38S4
zkR(5$qSxzAoAB<y&Mbk097=V<j(1ukS}sd6xLqkvJh7`b0%U@xtDnm{r-UW|FiIaS

literal 0
HcmV?d00001

-- 
2.47.0
(Code review only. I have not tested this myself, but I assume it works 
given that you seem to have this patch applied on your server.)