From f392d325dcb5fc3be937de276dab519fd9767a0e Mon Sep 17 00:00:00 2001 From: alek13 Date: Mon, 11 Dec 2023 02:55:53 +0300 Subject: [PATCH] MapGen: fix cyclomatic complexity in `find_room_walls()` --- mods/lord/Core/builtin/src/mapgen.lua | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/mods/lord/Core/builtin/src/mapgen.lua b/mods/lord/Core/builtin/src/mapgen.lua index d1e9b3fe1..280421b87 100644 --- a/mods/lord/Core/builtin/src/mapgen.lua +++ b/mods/lord/Core/builtin/src/mapgen.lua @@ -20,6 +20,22 @@ local c_air = id("air") --- @field floor RoomWall --- @field ceiling RoomWall + +---@param rc Position room center +---@param data table +---@param area VoxelArea +local function find_room_walls_positions(rc, data, area) + local wx_plus, wx_minus, wz_plus, wz_minus + for delta = 1, 8 do + if wx_plus == nil and data[area:index(rc.x + delta, rc.y, rc.z)] ~= c_air then wx_plus = rc.x + delta end + if wx_minus == nil and data[area:index(rc.x - delta, rc.y, rc.z)] ~= c_air then wx_minus = rc.x - delta end + if wz_plus == nil and data[area:index(rc.x, rc.y, rc.z + delta)] ~= c_air then wz_plus = rc.z + delta end + if wz_minus == nil and data[area:index(rc.x, rc.y, rc.z - delta)] ~= c_air then wz_minus = rc.z - delta end + end + + return wx_plus, wx_minus, wz_plus, wz_minus +end + --- @param rc Position room center --- @param wx_minus number --- @param wx_plus number @@ -49,13 +65,8 @@ end --- @return RoomWalls|RoomWall[]|table local function find_room_walls(room_center, data, area) local rc = room_center - local wx_plus, wx_minus, wz_plus, wz_minus - for delta = 1, 8 do - if wx_plus == nil and data[area:index(rc.x + delta, rc.y, rc.z)] ~= c_air then wx_plus = rc.x + delta end - if wx_minus == nil and data[area:index(rc.x - delta, rc.y, rc.z)] ~= c_air then wx_minus = rc.x - delta end - if wz_plus == nil and data[area:index(rc.x, rc.y, rc.z + delta)] ~= c_air then wz_plus = rc.z + delta end - if wz_minus == nil and data[area:index(rc.x, rc.y, rc.z - delta)] ~= c_air then wz_minus = rc.z - delta end - end + + local wx_plus, wx_minus, wz_plus, wz_minus = find_room_walls_positions() if wx_plus == nil or wx_minus == nil or wz_plus == nil or wz_minus == nil then return {} end