Script timeout: exhausted allowed execution time

Hello, so i am trying to code changed version of diamond square algorithm, but everytime i run it it gets execution timeout. is there any way to fix it?

local arr = {}
local n = 257;
for i = 1, n do
	local row = {}
	for j = 1, 257 do
		row[j] = nil;
	end
	arr[i] = row;
end

arr[1][1] = 0;
arr[1][n] = -1;
arr[n][1] = -2;
arr[n][n] = -3;

function diamondSquare(matrix) 
	local chuncksize = n-1;
	
	while chuncksize > 1 do
		local half = chuncksize / 2;
		
		for y = n, 2, -chuncksize do
			for x = 1, n - 1, chuncksize do
				local leftDiamond;
				local topDiamond;
				
				if matrix[y][x] == nil or matrix[y - chuncksize][x] == nil or matrix[y][x + chuncksize] == nil or matrix[y - chuncksize][x +chuncksize] == nil then
					print(y, x);
				end
				local min = math.min(matrix[y][x], matrix[y - chuncksize][x], matrix[y - chuncksize][x + chuncksize], matrix[y][x + chuncksize]);
				local max = math.max(matrix[y][x], matrix[y - chuncksize][x], matrix[y - chuncksize][x + chuncksize], matrix[y][x + chuncksize]);
				
				if x - chuncksize >= 1 then
					leftDiamond = matrix[y][x - chuncksize];
					min = math.min(min, leftDiamond);
					max = math.max(max, leftDiamond);
				end

				if y + chuncksize <= 257 then
					topDiamond = matrix[y + chuncksize][x];
					min = math.min(min, topDiamond);
					max = math.max(max, topDiamond);
				end
				
				matrix[y - half][x + half] = math.random(max - chuncksize, min + chuncksize);
			end
		end
		
		for y = n, 2, -chuncksize do
			for x = (y + half) % chuncksize, n, chuncksize do
				local left;
				local right;
				local top;
				local bottom;
				
				local min = 100000000000;
				local max = -100000000000;
				
				if x - half >= 1 then 
					left = matrix[y][x - half];
					min = math.min(min, left); max = math.max(max, left); 
				end
				if x + half <= 257 then 
					right = matrix[y][x + half];
					min = math.min(min, right); max = math.max(max, right); 
				end
				if y + half <= 257 then 
					top = matrix[y + half][x];
					min = math.min(min, top); max = math.max(max, top); 
				end
				if y - half >= 1 then
					bottom = matrix[y - half][x];
					min = math.min(min, bottom); max = math.max(max, bottom); 
				end
				
				matrix[y][x] =  math.random(max - chuncksize, min + chuncksize);
			end
		end
	end
	
	return matrix
end

arr = diamondSquare(arr)

Yep, there is no Wait time in your while chuncksize > 1 do loop.

i forgot that i should divide chuncksize every time :skull:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.