From c014c15c2f00d71105d4642012b727c9ca72a547 Mon Sep 17 00:00:00 2001
From: Yona-TYT <yonatan.el.amigo@gmail.com>
Date: Tue, 6 Dec 2022 19:12:25 -0400
Subject: [PATCH] ADD class tool_x to is_work_allowed_here as a parameter

---
 simutrans/script/script_base.nut        |  3 +
 src/simutrans/dataobj/scenario.cc       |  5 +-
 src/simutrans/dataobj/scenario.h        |  5 ++
 src/simutrans/script/api/api_control.cc | 77 +++++++++++++++++++++++++
 src/simutrans/script/api_param.cc       |  5 ++
 src/simutrans/script/api_param.h        |  2 +
 6 files changed, 96 insertions(+), 1 deletion(-)

diff --git a/simutrans/script/script_base.nut b/simutrans/script/script_base.nut
index 3c1ebfdea..c30e1850c 100644
--- a/simutrans/script/script_base.nut
+++ b/simutrans/script/script_base.nut
@@ -452,6 +452,9 @@ class city_x extends coord {
 class settings {
 }
 
+class tool_t {
+}
+
 /**
  * base class of map objects (obj_t)
  */
diff --git a/src/simutrans/dataobj/scenario.cc b/src/simutrans/dataobj/scenario.cc
index bb2582fd3..304574ee6 100644
--- a/src/simutrans/dataobj/scenario.cc
+++ b/src/simutrans/dataobj/scenario.cc
@@ -40,6 +40,8 @@
 
 #include <stdarg.h>
 
+fake_tool::fake_tool() { }
+
 // cache the scenario text files
 static plainstringhashtable_tpl<plainstring> cached_text_files;
 
@@ -491,8 +493,9 @@ const char* scenario_t::is_work_allowed_here(const player_t* player, uint16 tool
 	// cannot be done for two_click_tool_t's as they depend on routefinding,
 	// which is done per client
 	if (what_scenario == SCRIPTED) {
+		fake_tool tool;
 		static plainstring msg;
-		const char *err = script->call_function(script_vm_t::FORCE, "is_work_allowed_here", msg, (uint8)(player ? player->get_player_nr() : PLAYER_UNOWNED), tool_id, pos);
+		const char *err = script->call_function(script_vm_t::FORCE, "is_work_allowed_here", msg, (uint8)(player ? player->get_player_nr() : PLAYER_UNOWNED), tool, pos, NULL);
 
 		return err == NULL ? msg.c_str() : NULL;
 	}
diff --git a/src/simutrans/dataobj/scenario.h b/src/simutrans/dataobj/scenario.h
index 221b26783..3ea0a22a7 100644
--- a/src/simutrans/dataobj/scenario.h
+++ b/src/simutrans/dataobj/scenario.h
@@ -15,6 +15,11 @@
 #include "../dataobj/ribi.h"
 #include "../convoihandle.h"
 
+class fake_tool {
+public:
+	fake_tool();
+};
+
 class loadsave_t;
 class stadt_t;
 class fabrik_t;
diff --git a/src/simutrans/script/api/api_control.cc b/src/simutrans/script/api/api_control.cc
index 737f4c53a..fda93cfc5 100644
--- a/src/simutrans/script/api/api_control.cc
+++ b/src/simutrans/script/api/api_control.cc
@@ -50,7 +50,51 @@ 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()));
+}
+
+two_click_tool_t* get_current_tool_two()
+{
+	return dynamic_cast<two_click_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;
+}
+
+bool is_drag_tool()
+{
+	two_click_tool_t* tool = get_current_tool_two();
+	return  tool ? !tool->is_first_click() : SQ_OK;
+}
 
+koord3d get_start_pos()
+{
+	two_click_tool_t* tool = get_current_tool_two();
+	return  tool ? tool->get_start_pos() : koord3d::invalid;
+}
 
 void export_control(HSQUIRRELVM vm)
 {
@@ -97,4 +141,37 @@ void export_control(HSQUIRRELVM vm)
 	STATIC register_function<void(*)(bool)>(vm, set_pause_on_error, "set_pause_on_error", true);
 
 	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_param.cc b/src/simutrans/script/api_param.cc
index 73643b980..67ca77f72 100644
--- a/src/simutrans/script/api_param.cc
+++ b/src/simutrans/script/api_param.cc
@@ -650,4 +650,9 @@ namespace script_api {
 		}
 		return NULL;
 	}
+
+	SQInteger param<fake_tool>::push(HSQUIRRELVM vm, fake_tool const&)
+	{
+		return 	 push_instance(vm, "tool_x");
+	}
 };
diff --git a/src/simutrans/script/api_param.h b/src/simutrans/script/api_param.h
index 2d290bd03..cd5fd60fd 100644
--- a/src/simutrans/script/api_param.h
+++ b/src/simutrans/script/api_param.h
@@ -50,6 +50,7 @@ class ware_production_t;
 class weg_t;
 class wayobj_t;
 class way_builder_t;
+class fake_tool;
 
 /**
  * @namespace script_api The namespace contains all functions necessary to communicate
@@ -410,6 +411,7 @@ namespace script_api {
 	declare_param_mask(ware_production_t*, "t|x|y", "factory_production_x");
 	declare_specialized_param(tool_t*, "x", "command_x");
 	declare_specialized_param(way_builder_t*, "t|x|y", "way_planner_x");
+	declare_specialized_param(fake_tool, "t|x|y", "tool_x");
 
 	// export of obj_t derived classes in api/map_objects.cc
 	declare_specialized_param(obj_t*, "t|x|y", "map_object_x");
-- 
2.38.1

