From 0976a1bef474c973cf4d9a2458e88f535501c383 Mon Sep 17 00:00:00 2001
From: Yona-TYT <yonatan.el.amigo@gmail.com>
Date: Thu, 5 Dec 2024 13:18:09 -0400
Subject: [PATCH] Experiment with an allow rule 2

---
 src/simutrans/dataobj/scenario.cc        | 20 ++++++++++++++++++++
 src/simutrans/dataobj/scenario.h         | 10 ++++++++--
 src/simutrans/script/api/api_scenario.cc |  2 ++
 3 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/src/simutrans/dataobj/scenario.cc b/src/simutrans/dataobj/scenario.cc
index 06fa5fa5e..e2c8a3720 100644
--- a/src/simutrans/dataobj/scenario.cc
+++ b/src/simutrans/dataobj/scenario.cc
@@ -568,6 +568,26 @@ const char* scenario_t::is_work_allowed_here(const player_t* player, uint16 tool
 					}
 				}
 			}
+			forbidden_t test3(forbidden_t::allow_tool_rect, tool_id, wt, param);
+			for (uint32 i = find_first_type_tool_wt(test3, player_nr); i < forbidden_tools[player_nr].get_count(); i++) {
+				// there is something, we need to test more
+				forbidden_t const& f = *forbidden_tools[player_nr][i];
+				if (f.type != forbidden_t::allow_tool_rect || f.toolnr != tool_id) {
+					// reached end of forbidden tools with this id => done
+					break;
+				}
+				if (f.waytype == invalid_wt  ||  f.waytype == wt) {
+					if (f.parameter_hash == 0  ||  f.parameter_hash == p_hash) {
+						// parameter matches too => check rectangle
+						if (f.pos_nw.x <= pos.x && f.pos_nw.y <= pos.y && pos.x <= f.pos_se.x && pos.y <= f.pos_se.y) {
+							// check height
+							if (f.hmin <= pos.z && pos.z <= f.hmax) {
+								return NULL;
+							}
+						}
+					}
+				}
+			}
 		}
 		if (player_nr == PLAYER_UNOWNED) {
 			break;
diff --git a/src/simutrans/dataobj/scenario.h b/src/simutrans/dataobj/scenario.h
index fb58d4f09..bb9517c38 100644
--- a/src/simutrans/dataobj/scenario.h
+++ b/src/simutrans/dataobj/scenario.h
@@ -94,8 +94,9 @@ private:
 		}
 
 		enum forbid_type {
-			forbid_tool      = 1,
-			forbid_tool_rect = 2
+			forbid_tool			= 1,
+			allow_tool_rect 	= 2,
+			forbid_tool_rect	= 3
 		};
 
 		forbid_type type;
@@ -205,6 +206,9 @@ private:
 	 */
 	void intern_forbid(forbidden_t *test, uint player_nr, bool forbid);
 
+	void intern_forbid_way_tool_cube(uint8 player_nr, uint16 tool_id, waytype_t wt, const char* param, koord3d pos_nw, koord3d pos_se, plainstring err, forbidden_t::forbid_type type);
+
+
 	/**
 	 * Helper function: works on forbidden_tools directly (if not in network-mode)
 	 * or sends information over network (if at server)
@@ -397,6 +401,8 @@ public:
 	 */
 	void allow_way_tool_rect(uint8 player_nr, uint16 tool_id, waytype_t wt, const char* param, koord pos_nw, koord pos_se);
 
+	void allow_test_way_tool_rect(uint8 player_nr, uint16 tool_id, waytype_t wt, const char* param, koord pos_nw, koord pos_se);
+
 	/**
 	 * Forbid tool with certain waytype within cubic region on the map.
 	 * @ingroup squirrel-api
diff --git a/src/simutrans/script/api/api_scenario.cc b/src/simutrans/script/api/api_scenario.cc
index 8585b442d..cab1ed7ed 100644
--- a/src/simutrans/script/api/api_scenario.cc
+++ b/src/simutrans/script/api/api_scenario.cc
@@ -261,6 +261,8 @@ void export_scenario(HSQUIRRELVM vm)
 	 */
 	STATIC register_method(vm, &scenario_t::gui_needs_update,  "gui_needs_update");
 
+	STATIC register_method(vm, &scenario_t::allow_test_way_tool_rect,  "allow_test_way_tool_rect");
+
 	end_class(vm);
 
 
-- 
2.47.1

