From e2b5d016fcbd9037a38c29d5c7a8879b1d8b3ca3 Mon Sep 17 00:00:00 2001
From: Yona-TYT <yonatan.el.amigo@gmail.com>
Date: Fri, 2 Dec 2022 01:09:43 -0400
Subject: [PATCH] ADD class tool_x and get current tool

---
 src/simutrans/descriptor/obj_base_desc.h      |  6 +-
 src/simutrans/script/api/api_control.cc       | 90 +++++++++++++++++++
 .../script/api/squirrel_types_ai.awk          |  8 ++
 .../script/api/squirrel_types_scenario.awk    | 12 ++-
 src/simutrans/simtypes.h                      | 13 +--
 src/simutrans/tool/simmenu.h                  |  2 +
 src/simutrans/tool/simtool.cc                 | 11 ++-
 src/simutrans/tool/simtool.h                  |  1 +
 8 files changed, 131 insertions(+), 12 deletions(-)

diff --git a/src/simutrans/descriptor/obj_base_desc.h b/src/simutrans/descriptor/obj_base_desc.h
index 41c0f689b..9fb3d4936 100644
--- a/src/simutrans/descriptor/obj_base_desc.h
+++ b/src/simutrans/descriptor/obj_base_desc.h
@@ -84,11 +84,12 @@ protected:
 	sint32 price;         ///< cost to build this thing [1/100 credits] per tile/object
 	uint16 axle_load;     ///< up to this load vehicle may pass (default 9999)
 	uint8  wtyp;          ///< waytype of this thing
+	uint8  styp;          ///< test
 	sint32 topspeed;      ///< maximum allowed speed in km/h
 
 public:
 	obj_desc_transport_related_t() : obj_desc_timelined_t(),
-		maintenance(0), price(0), axle_load(9999), wtyp(255), topspeed(0) {}
+		maintenance(0), price(0), axle_load(9999), wtyp(255), styp(255), topspeed(0) {}
 
 	sint32 get_maintenance() const { return maintenance; }
 
@@ -97,6 +98,9 @@ public:
 	waytype_t get_waytype() const { return static_cast<waytype_t>(wtyp); }
 	waytype_t get_wtyp() const { return get_waytype(); }
 
+	systemtype_t get_systemtype() const { return static_cast<systemtype_t>(wtyp); }
+	systemtype_t get_styp() const { return get_systemtype(); }
+
 	sint32 get_topspeed() const { return topspeed; }
 
 	uint16 get_axle_load() const { return axle_load; }
diff --git a/src/simutrans/script/api/api_control.cc b/src/simutrans/script/api/api_control.cc
index 737f4c53a..c5946bffd 100644
--- a/src/simutrans/script/api/api_control.cc
+++ b/src/simutrans/script/api/api_control.cc
@@ -50,7 +50,59 @@ SQInteger set_pause_on_error(HSQUIRRELVM vm)
 	return SQ_OK;
 }
 
+tool_t* get_current_tool()
+{
+	return dynamic_cast<tool_t*>(welt->get_tool(welt->get_active_player_nr()));
+}
+
+uint16 get_id_tool()
+{
+	tool_t* tool = get_current_tool();
+	return tool ? tool->get_id() : SQ_OK;
+}
+
+bool is_ctrl_pressed()
+{
+	tool_t* tool = get_current_tool();
+	return tool ? tool->is_ctrl_pressed() : SQ_OK;
+}
+
+bool is_shift_pressed()
+{
+	tool_t* tool = get_current_tool();
+	return tool ? tool->is_shift_pressed() : SQ_OK;
+}
+
+waytype_t get_waytype_tool()
+{
+	tool_t* tool = get_current_tool();
+	return tool ? tool->get_waytype() : invalid_wt;
+}
+
+systemtype_t get_systemtype_tool()
+{
+	tool_t* tool = get_current_tool();
+	return tool ? tool->get_systemtype() : invalid_st;
+}
+
+bool is_drag_tool()
+{
+	tool_t* tool = get_current_tool();
+	two_click_tool_t* two_tool = dynamic_cast<two_click_tool_t*>(tool);
+	return two_tool ? !two_tool->is_first_click() : SQ_OK;
+}
+
+koord3d get_start_pos()
+{
+	tool_t* tool = get_current_tool();
+	two_click_tool_t* two_tool = dynamic_cast<two_click_tool_t*>(tool);
+	return two_tool ? two_tool->get_start_pos() : koord3d::invalid;
+}
 
+SQInteger control_get_current_tool(HSQUIRRELVM vm)
+{
+	return	push_instance(vm, "tool_x");
+}
 
 void export_control(HSQUIRRELVM vm)
 {
@@ -96,5 +148,43 @@ void export_control(HSQUIRRELVM vm)
 	 */
 	STATIC register_function<void(*)(bool)>(vm, set_pause_on_error, "set_pause_on_error", true);
 
+	/**
+	 * @returns Get the tool_t class from the current tool.
+	 */
+	STATIC register_function(vm, control_get_current_tool, "get_tool", 1, ".");
+
 	end_class(vm);
+
+	begin_class(vm, "tool_x");
+	/**
+	 * @returns Tool id.
+	 */
+	register_method(vm, get_id_tool, "get_id");
+	/**
+	 * @returns true if ctrl key is pressed down.
+	 */
+	register_method(vm, is_ctrl_pressed, "is_ctrl");
+	/**
+	 * @returns true if shift key is pressed down.
+	 */
+	register_method(vm, is_shift_pressed, "is_shift");
+	/**
+	 * @returns Waytype id.
+	 */
+	register_method(vm, get_waytype_tool, "get_waytype");
+	/**
+	 * @returns System type id.
+	 */
+	register_method(vm, get_systemtype_tool, "get_system_type");
+	/**
+	 * @returns true if the tool is dragged.
+	 */
+	register_method(vm, is_drag_tool, "is_drag");
+	/**
+	 * @returns Start coord3d when tool is dragged
+	 */
+	register_method(vm, get_start_pos, "get_start_pos");
+
+	end_class(vm);
+
 }
diff --git a/src/simutrans/script/api/squirrel_types_ai.awk b/src/simutrans/script/api/squirrel_types_ai.awk
index 7ee3a007c..18d5ed819 100644
--- a/src/simutrans/script/api/squirrel_types_ai.awk
+++ b/src/simutrans/script/api/squirrel_types_ai.awk
@@ -36,6 +36,14 @@ BEGIN {
 	export_types_ai["debug::pause"] = "bool()"
 	export_types_ai["debug::is_paused"] = "bool()"
 	export_types_ai["debug::set_pause_on_error"] = "void(bool)"
+	export_types_ai["debug::get_tool"] = "tool_x()"
+	export_types_ai["tool_x::get_id"] = "integer()"
+	export_types_ai["tool_x::is_ctrl"] = "bool()"
+	export_types_ai["tool_x::is_shift"] = "bool()"
+	export_types_ai["tool_x::get_waytype"] = "way_types()"
+	export_types_ai["tool_x::get_system_type"] = "way_system_types()"
+	export_types_ai["tool_x::is_drag"] = "bool()"
+	export_types_ai["tool_x::get_start_pos"] = "coord3d()"
 	export_types_ai["convoy_x::is_valid"] = "bool()"
 	export_types_ai["convoy_x::needs_electrification"] = "bool()"
 	export_types_ai["convoy_x::get_name"] = "string()"
diff --git a/src/simutrans/script/api/squirrel_types_scenario.awk b/src/simutrans/script/api/squirrel_types_scenario.awk
index 86b98f080..0e962ab83 100644
--- a/src/simutrans/script/api/squirrel_types_scenario.awk
+++ b/src/simutrans/script/api/squirrel_types_scenario.awk
@@ -36,7 +36,14 @@ BEGIN {
 	export_types_scenario["debug::pause"] = "bool()"
 	export_types_scenario["debug::is_paused"] = "bool()"
 	export_types_scenario["debug::set_pause_on_error"] = "void(bool)"
+	export_types_scenario["debug::get_tool"] = "tool_x()"
+	export_types_scenario["tool_x::get_id"] = "integer()"
+	export_types_scenario["tool_x::is_ctrl"] = "bool()"
+	export_types_scenario["tool_x::is_shift"] = "bool()"
+	export_types_scenario["tool_x::get_waytype"] = "way_types()"
+	export_types_scenario["tool_x::get_system_type"] = "way_system_types()"
+	export_types_scenario["tool_x::is_drag"] = "bool()"
+	export_types_scenario["tool_x::get_start_pos"] = "coord3d()"
 	export_types_scenario["convoy_x::needs_electrification"] = "bool()"
 	export_types_scenario["convoy_x::get_name"] = "string()"
 	export_types_scenario["convoy_x::set_name"] = "void(string)"

index 5b9a2dcac..d243dc875 100644
--- a/src/simutrans/simtypes.h
+++ b/src/simutrans/simtypes.h
@@ -115,12 +115,13 @@ enum waytype_t {
  * System types for ways
  */
 enum systemtype_t {
-	type_flat     = 0,   ///< flat track
-	type_elevated = 1,   ///< flag for elevated ways
-	type_runway   = 1,   ///< flag for runway (only aircrafts)
-	type_tram     = 7,   ///< tram track (waytype = track_wt)
-	type_river    = 255, ///< flag for river
-	type_all      = 255  ///< special ?
+	invalid_st    =  -1,
+	type_flat      =   0,   ///< flat track
+	type_elevated =   1,   ///< flag for elevated ways
+	type_runway   =   1,   ///< flag for runway (only aircrafts)
+	type_tram     =   7,   ///< tram track (waytype = track_wt)
+	type_river    = 255,   ///< flag for river
+	type_all      = 255    ///< special ?
 };
 
 
diff --git a/src/simutrans/tool/simmenu.h b/src/simutrans/tool/simmenu.h
index 488c4938f..5a3aa3057 100644
--- a/src/simutrans/tool/simmenu.h
+++ b/src/simutrans/tool/simmenu.h
@@ -358,6 +358,8 @@ public:
 	virtual bool update_pos_after_use() const { return false; }
 
 	virtual waytype_t get_waytype() const { return invalid_wt; }
+
+	virtual systemtype_t get_systemtype() const { return invalid_st; }
 };
 
 /*
diff --git a/src/simutrans/tool/simtool.cc b/src/simutrans/tool/simtool.cc
index 56fdb22da..fe2f46511 100644
--- a/src/simutrans/tool/simtool.cc
+++ b/src/simutrans/tool/simtool.cc
@@ -2497,6 +2497,15 @@ waytype_t tool_build_way_t::get_waytype() const
 	return invalid_wt;
 }
 
+systemtype_t tool_build_way_t::get_systemtype() const
+{
+	const way_desc_t *desc = get_desc( welt->get_timeline_year_month());
+	if (desc) {
+		return desc->get_styp();
+	}
+	return invalid_st;
+}
+
 void tool_build_way_t::start_at( koord3d &new_start )
 {
 	if(  is_shift_pressed()  &&  (desc->get_styp() == type_elevated  &&  desc->get_wtyp() != air_wt)  ) {
@@ -2734,7 +2743,6 @@ waytype_t tool_build_bridge_t::get_waytype() const
 	return desc ? desc->get_waytype() : invalid_wt;
 }
 
-
 bool tool_build_bridge_t::init( player_t *player )
 {
 	two_click_tool_t::init( player );
@@ -2987,7 +2995,6 @@ waytype_t tool_build_tunnel_t::get_waytype() const
 	return desc ? desc->get_waytype() : invalid_wt;
 }
 
-
 bool tool_build_tunnel_t::init( player_t *player )
 {
 	two_click_tool_t::init( player );
diff --git a/src/simutrans/tool/simtool.h b/src/simutrans/tool/simtool.h
index 474f97234..59ecc9721 100644
--- a/src/simutrans/tool/simtool.h
+++ b/src/simutrans/tool/simtool.h
@@ -305,6 +305,7 @@ public:
 	bool init(player_t*) OVERRIDE;
 	bool is_init_keeps_game_state() const OVERRIDE { return true; }
 	waytype_t get_waytype() const OVERRIDE;
+	systemtype_t get_systemtype() 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)); }
 };
-- 
2.38.1

