-- This creates a 2d, 3d, 4d, etc. array (table). It needs to be above the variable creation
function gs_NewAutotable(dim)
local MT = {};
for i=1, dim do
MT[i] = {__index = function(t, k)
if i < dim then
t[k] = setmetatable({}, MT[i+1])
return t[k];
end
end}
end
return setmetatable({}, MT[1]);
end
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
-- DEFINE THESE VARIABLES:
gs_gridStartX = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } -- optional
gs_gridStartY = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } -- optional
gs_gridWidth = { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 } -- optional
gs_gridHeight = { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 } -- optional
gs_gridRed = 200 [email protected] integer
gs_gridGreen = 200 [email protected] integer
gs_gridBlue = 200 [email protected] integer
gs_gridAlpha = 255 [email protected] integer
-- Don't change these:
gs_grid = gs_NewAutotable(2) [email protected] integer/integer[][]
gs_isVisible = {} [email protected] boolean[]
-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
-- OPTIONAL - You don't need to run this function, instead you can manually create the grids yourself
function gs_CreateGridForAllPlayers(isCentered)
-- Uses the optional variables: gs_gridStartX, gs_gridStartY, gs_gridWidth, and gs_gridHeight
for i = 0, bj_MAX_PLAYER_SLOTS do
gs_CreateGridAtXY(i, gs_gridStartX[i], gs_gridStartY[i], gs_gridWidth[i], gs_gridHeight[i], isCentered)
end
end
-- PARAMETERS: Player Id (0 - 27), starting X coordinate, starting Y coordinate, grid width, grid height, grid is centered (true/false)
function gs_CreateGridAtXY(id, startX, startY, width, height, isCentered)
gs_DestroyGrid(id)
gs_isVisible[id] = false
local i = 0 [email protected] integer
local img [email protected] image
local posX [email protected] real
local posY [email protected] real
startX = startX - 128
startY = startY - 128
height = height - 1
width = width - 1
if isCentered then
startX = startX - (128 * (width / 2))
startY = startY - (128 * (height / 2))
end
for x = 0, width do
for y = 0, height do
img = CreateImage("Gridplane.dds",128,128,0,0,0,0,1,1,1,1)
SetImageRenderAlways(img, true)
SetImageColor(img, 255, 255, 255, 0)
posX = startX + x * 128
posY = startY + y * 128
SetImagePosition(img, posX, posY, 0)
gs_grid[id][i] = img
i = i + 1
end
end
end
-- PARAMETERS: Player Id (0 - 27), point, grid width, grid height, grid is centered (true/false)
function gs_CreateGridAtPoint(id, point, width, height, isCentered)
local startX = gs_GetTileXY(GetLocationX(point)) [email protected] real
local startY = gs_GetTileXY(GetLocationY(point)) [email protected] real
gs_CreateGridAtXY(id, startX, startY, width, height, isCentered)
end
-- PARAMETERS: Player Id (0 - 27), unit, grid width, grid height, grid is centered (true/false)
function gs_CreateGridOnUnit(id, u, width, height, isCentered)
local startX = gs_GetTileXY(GetUnitX(u)) [email protected] real
local startY = gs_GetTileXY(GetUnitY(u)) [email protected] real
gs_CreateGridAtXY(id, startX, startY, width, height, isCentered)
end
-- PARAMETERS: Player Id (0 - 27)
function gs_ToggleGrid(id)
if gs_isVisible[id] then
gs_HideGrid(id)
return false
else
gs_ShowGrid(id)
return true
end
end
-- PARAMETERS: Player Id (0 - 27)
function gs_ShowGrid(id)
gs_isVisible[id] = true
if GetLocalPlayer() == Player(id) then
for i = 0, #gs_grid[id] do
SetImageColor(gs_grid[id][i], gs_gridRed, gs_gridGreen, gs_gridBlue, gs_gridAlpha)
end
end
end
-- PARAMETERS: Player Id (0 - 27)
function gs_HideGrid(id)
gs_isVisible[id] = false
if GetLocalPlayer() == Player(id) then
for i = 0, #gs_grid[id] do
SetImageColor(gs_grid[id][i], 255, 255, 255, 0)
end
end
end
-- PARAMETERS: Player Id (0 - 27)
function gs_DestroyGrid(id)
if (gs_grid[id][0] ~= nil) then
for i = 0, #gs_grid[id] do
DestroyImage(gs_grid[id][i])
end
end
end
-- This converts an X or Y coordinate to the nearest multiple of 128 so it fits on the grid (don't touch)
function gs_GetTileXY(num)
return 128 * math.floor(num / 128 + 0.9)
end