Fix broken build restrictions

In f3ac9e9669 the fail condition was broken causing it to allow building on restricted areas.

Add a test to ensure correct behavior for this case.

Reported by: @wowgetoffyourcellphone
Pull Request: #8021
This commit is contained in:
Stan
2025-05-31 10:31:00 +02:00
parent d3017b6f9c
commit 167020c05c
2 changed files with 38 additions and 6 deletions
@@ -151,23 +151,25 @@ BuildRestrictions.prototype.CheckPlacement = function()
var ret = cmpObstruction.CheckFoundation(passClassName, false);
}
if (ret != "success")
if (ret !== "success")
{
switch (ret)
{
case "fail_error":
case "fail_no_obstruction":
error("CheckPlacement: Error returned from CheckFoundation");
break;
case "fail_obstructs_foundation":
result.message = markForTranslation("%(name)s cannot be built on another building or resource");
break;
case "fail_terrain_class":
// TODO: be more specific and/or list valid terrain?
result.message = markForTranslation("%(name)s cannot be built on invalid terrain");
break;
case "fail_error":
case "fail_no_obstruction":
default:
return result; // Fail
error(`CheckPlacement: Error returned from CheckFoundation. Got reason: '${ret}'`);
break;
}
return result;
}
// Check territory restrictions
@@ -0,0 +1,30 @@
Engine.LoadComponentScript("interfaces/BuildRestrictions.js");
Engine.LoadComponentScript("BuildRestrictions.js");
{
const entity = 10;
const QueryOwnerInterface = () => ({
"GetPlayerID": () => 1,
"IsAI": () => false
});
Engine.RegisterGlobal("QueryOwnerInterface", QueryOwnerInterface);
const cmpBuildRestrictions = ConstructComponent(entity, "BuildRestrictions", {
"PlacementType": "land"
});
AddMock(SYSTEM_ENTITY, IID_RangeManager, {
"GetLosVisibility": (_, __) => "visible",
"GetEntitiesByPlayer": () => []
});
AddMock(entity, IID_Ownership, {
"GetOwner": () => 1
});
AddMock(entity, IID_Obstruction, {
"CheckFoundation": () => "fail_obstructs_foundation"
});
const result = cmpBuildRestrictions.CheckPlacement();
TS_ASSERT_EQUALS(result.success, false);
}