From 75fffc9ce9ef10d217ee5e92c528007be29881cf Mon Sep 17 00:00:00 2001
From: Yona-TYT <yonatan.el.amigo@gmail.com>
Date: Mon, 9 Feb 2026 01:05:55 -0400
Subject: [PATCH] ADD get_last_used_way

---
 src/simutrans/script/api/api_obj_desc.cc |  9 ++++++
 src/simutrans/tool/simtool.cc            | 40 +++++++++++++++++-------
 src/simutrans/tool/simtool.h             |  4 +++
 3 files changed, 41 insertions(+), 12 deletions(-)

diff --git a/src/simutrans/script/api/api_obj_desc.cc b/src/simutrans/script/api/api_obj_desc.cc
index 43d0c3104..9c2277032 100644
--- a/src/simutrans/script/api/api_obj_desc.cc
+++ b/src/simutrans/script/api/api_obj_desc.cc
@@ -32,6 +32,7 @@
 #include "../../simhalt.h"
 #include "../../simware.h"
 #include "../../world/simworld.h"
+#include "../../tool/simtool.h"
 
 #define begin_enum(name)
 #define end_enum()
@@ -670,6 +671,14 @@ void export_goods_desc(HSQUIRRELVM vm)
 	 */
 	STATIC register_method(vm, way_builder_t::get_way_list, "get_available_ways", false, true);
 
+	/**
+	* Returns the last used (default) way descriptor for a given waytype.
+	* This is the way that would be selected when pressing the shortcut key (e.g. 's' for roads).
+	* @param wt waytype (1=road_wt, 2=track_wt, 3=tram_wt, etc.)
+	* @returns way_desc_x object or null if none available
+	*/
+	STATIC register_method(vm, &tool_build_way_t::get_last_used, "get_last_used_way", false, true);
+
 	end_class(vm);
 
 	/**
diff --git a/src/simutrans/tool/simtool.cc b/src/simutrans/tool/simtool.cc
index 3b8eb248c..35d860916 100644
--- a/src/simutrans/tool/simtool.cc
+++ b/src/simutrans/tool/simtool.cc
@@ -2560,20 +2560,36 @@ const way_desc_t *tool_build_way_t::get_desc( uint16 timeline_year_month) const
 	const way_desc_t *desc = default_param ? way_builder_t::get_desc(default_param,0) :NULL;
 	if(  desc==NULL  &&  default_param  ) {
 		waytype_t wt = (waytype_t)atoi(default_param);
-		desc = defaults[wt&63];
-		if(desc==NULL  ||  !desc->is_available(timeline_year_month)) {
-			// search fastest way.
-			if(  wt == tram_wt  ||  wt == powerline_wt  ) {
-				desc = way_builder_t::weg_search(wt, 0xffffffff, timeline_year_month, type_flat);
-			}
-			else if ( (road_wt <= wt  &&  wt <= narrowgauge_wt)  ||  wt == air_wt) {
-				// this triggers an assertion if wt == powerline_wt
-				weg_t *w = weg_t::alloc(wt);
-				desc = w->get_desc();
-				delete w;
-			}
+		desc = get_desc_internal(defaults[wt & 63], timeline_year_month, wt);
+	}
+	return desc;
+}
+
+const way_desc_t* tool_build_way_t::get_last_used(waytype_t wt)
+{
+	const sint8 idx = wt & 63;
+	if (idx >= 0 && idx < (sint8)lengthof(defaults)) {
+		return get_desc_internal(defaults[idx], welt->get_timeline_year_month(), wt);
+	}
+	return NULL;
+}
+
+const way_desc_t *tool_build_way_t::get_desc_internal(const way_desc_t* desc, uint16 timeline_year_month, waytype_t wt)
+{
+	if (desc == NULL || !desc->is_available(timeline_year_month)) {
+		if (wt == tram_wt || wt == powerline_wt) {
+			desc = way_builder_t::weg_search(wt, 0xffffffff, timeline_year_month, type_flat);
+		}
+		else if ((road_wt <= wt && wt <= narrowgauge_wt) || wt == air_wt) {
+			weg_t *w = weg_t::alloc(wt);
+			desc = w->get_desc();
+			delete w;
+		}
+		if (desc == NULL || !desc->is_available(timeline_year_month)) {
+			desc = way_builder_t::weg_search(wt, 0xffffffff, timeline_year_month, type_flat);
 		}
 	}
+
 	return desc;
 }
 
diff --git a/src/simutrans/tool/simtool.h b/src/simutrans/tool/simtool.h
index 819a6e9a9..7cefca6be 100644
--- a/src/simutrans/tool/simtool.h
+++ b/src/simutrans/tool/simtool.h
@@ -295,6 +295,8 @@ private:
 	void mark_tiles(player_t*, koord3d const&, koord3d const&) OVERRIDE;
 	uint8 is_valid_pos(player_t*, koord3d const&, char const*&, koord3d const&) OVERRIDE;
 
+	static const way_desc_t* get_desc_internal(const way_desc_t* desc, uint16 timeline_year_month, waytype_t wt);
+
 protected:
 	const way_desc_t *desc;
 
@@ -313,6 +315,8 @@ public:
 	waytype_t get_waytype() const OVERRIDE;
 	// remove preview necessary while building elevated ways
 	bool remove_preview_necessary() const OVERRIDE { return !is_first_click()  &&  (desc  &&  (desc->get_styp() == type_elevated  &&  desc->get_wtyp() != air_wt)); }
+
+	static const way_desc_t* get_last_used(waytype_t wt);
 };
 
 class tool_build_cityroad : public tool_build_way_t {
-- 
2.52.0

