PDA

View Full Version : Hex Grid



Maxxx26
October 1st, 2009, 16:13
Hey everyone.
I have seen that the newest incarnation of the 3.5E ruleset has the option to use a hex grid. Can someone tell me which files were modified for this update, since I would like to incorporate this into my private ruleset, but don't remember all the file names.
Thanks a lot.

Spyke
October 1st, 2009, 16:32
Hex grids are implemented in imagewindow_image.lua.

Also add icon_gridhex.png, and update the definition in graphics_radial.xml.

You'll also want to implement preferences, if you've not already done so, to be able to switch between square and hex grids.

The preference is set in basepreferences.lua. To see what you need to implement preferences from scratch you should compare your current ruleset to Foundation, but basically look at these files:

base.xml
utility.xml
utility_preferences.xml
basepreferences.lua
preferences.lua
preferencemanager.lua
graphics_frames.xml
ctgroupbox.png

and whatever desktop.lua script works for your ruleset.

Other files are needed to implement the other standard Preference options.

Spyke

Ikael
October 1st, 2009, 20:33
The minimal work required to make the new hex grid replace the square grid is to add following function to imagewindow_image.lua (if your file already contains onInit() function, when just copy this function's content to it)


function onInit()
setGridToolType("hex");
if User.isHost() then
setTokenOrientationCount(12);
end
end

now your grid will (only) be hex grid instead of square grid (and you can't choose to use square grid anymore, except removing that script).

in addition to you probably want to update same file's getClosestSnapPoint(x, y) and onMeasureVector(token, vector) functions to make the hex grid work when measuring distances and using the "snap-to-grid" feature.

getClosestSnapPoint function


function getClosestSnapPoint(x, y)
if not hasGrid() then
return x, y;
end
local size = getGridSize();
local qw, hh = getGridHexElementDimensions();
local ox, oy = getGridOffset();

-- The hex grid separates into a non-square grid of elements sized qw*hh, the location in which dictates corner points
if type == "hexcolumn" then
local col = math.floor((x - ox) / qw);
local row = math.floor((y - oy) * 2 / size);

local evencol = col % 2 == 0;
local evenrow = row % 2 == 0;

local lx = (x - ox) % qw;
local ly = (y - oy) % hh;

if (evenrow and evencol) or (not evenrow and not evencol) then
-- snap to lower right and upper left
if lx + ly * (qw/hh) < qw then
return ox + col*qw, oy + math.floor(row*size/2);
else
return ox + (col+1)*qw, oy + math.floor((row+1)*size/2);
end
else
-- snap to lower left and upper right
if (qw-lx) + ly * (qw/hh) < qw then
return ox + (col+1)*qw, oy + math.floor(row*size/2);
else
return ox + col*qw, oy + math.floor((row+1)*size/2);
end
end
else -- "hexrow"
local col = math.floor((x - ox) * 2 / size);
local row = math.floor((y - oy) / qw);

local evencol = col % 2 == 0;
local evenrow = row % 2 == 0;

local lx = (x - ox) % hh;
local ly = (y - oy) % qw;

if (evenrow and evencol) or (not evenrow and not evencol) then
-- snap to lower right and upper left
if lx * (qw/hh) + ly < qw then
return ox + math.floor(col*size/2), oy + row*qw;
else
return ox + math.floor((col+1)*size/2), oy + (row+1)*qw;
end
else
-- snap to lower left and upper right
if (hh-lx) * (qw/hh) + ly < qw then
return ox + math.floor((col+1)*size/2), oy + row*qw;
else
return ox + math.floor(col*size/2), oy + (row+1)*qw;
end
end
end
end

onMeasureVector function


function onMeasureVector(token, vector)
if hasGrid() then
local pixels = 0;
for i = 1, #vector do
pixels = pixels + math.floor(math.sqrt(vector[i].x*vector[i].x + vector[i].y*vector[i].y));
end
local units = math.floor(pixels / getGridSize());
local feet = units * 5;
return feet .. "\'";
else
return "";
end
end