|
@ -58,7 +58,7 @@ function hex_round(s, t) |
|
|
|
|
|
|
|
|
sdelta = math.abs(rs - s) |
|
|
sdelta = math.abs(rs - s) |
|
|
tdelta = math.abs(rt - t) |
|
|
tdelta = math.abs(rt - t) |
|
|
zdelta = math.abs(rz + s + t) |
|
|
|
|
|
|
|
|
zdelta = math.abs(rz - (-s - t)) |
|
|
|
|
|
|
|
|
if sdelta > tdelta and sdelta > zdelta then |
|
|
if sdelta > tdelta and sdelta > zdelta then |
|
|
rs = -rt - rz |
|
|
rs = -rt - rz |
|
@ -82,13 +82,10 @@ POINTY_ORIENTATION = {3.0^0.5, 3.0^0.5/2.0, 0.0, 3.0/2.0, |
|
|
3.0^0.5/3.0, -1.0/3.0, 0.0, 2.0/3.0} |
|
|
3.0^0.5/3.0, -1.0/3.0, 0.0, 2.0/3.0} |
|
|
|
|
|
|
|
|
-- layout. |
|
|
-- layout. |
|
|
function layout(size, orientation, origin, width, height, radius) |
|
|
|
|
|
return {size = size or {11, 11}, |
|
|
|
|
|
orientation = orientation or FLAT_ORIENTATION, |
|
|
|
|
|
origin = origin or {0, 0}, |
|
|
|
|
|
width = width or 45, |
|
|
|
|
|
height = height or 31, |
|
|
|
|
|
radius = radius or width or 6} |
|
|
|
|
|
|
|
|
function layout_init(origin, size, orientation) |
|
|
|
|
|
return {origin = origin or {0, 0}, |
|
|
|
|
|
size = size or {11, 11}, |
|
|
|
|
|
orientation = orientation or FLAT_ORIENTATION} |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
-- hex to screen |
|
|
-- hex to screen |
|
@ -115,29 +112,33 @@ function pixel_to_hex(x, y, layout) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
----- [[ MAP STORAGE & RETRIEVAL ]] -------------------------------------------- |
|
|
----- [[ MAP STORAGE & RETRIEVAL ]] -------------------------------------------- |
|
|
--[[ all functions return a table of tables; a map of points |
|
|
|
|
|
storage functions take a range of hex coordinates, and return pixel ones. |
|
|
|
|
|
retrieval functions do the opposite. |
|
|
|
|
|
everything except map shape is determined by layout. |
|
|
|
|
|
pick a pair of functions based on the shape of map you want to use. |
|
|
|
|
|
it is not advised to use a single layout instance with multiple shapes. ]] |
|
|
|
|
|
|
|
|
|
|
|
-- returns parallelogram-shaped map. width and height are used. |
|
|
|
|
|
function ogram_map_store(layout) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--[[ _init functions return a table of tables; |
|
|
|
|
|
a map of points in a chosen shape and specified layout. |
|
|
|
|
|
the shape, as well as the layout used is stored in a metatable |
|
|
|
|
|
for reuse. |
|
|
|
|
|
]] |
|
|
|
|
|
|
|
|
|
|
|
-- returns parallelogram-shaped map. |
|
|
|
|
|
function map_parallelogram_init(layout, width, height) |
|
|
map = {} |
|
|
map = {} |
|
|
for s = 0, layout.width do |
|
|
|
|
|
for t = 0, layout.height do |
|
|
|
|
|
|
|
|
setmetatable(map, {__index={layout=layout, shape=parallelogram}}) |
|
|
|
|
|
|
|
|
|
|
|
for s = 0, width do |
|
|
|
|
|
for t = 0, height do |
|
|
table.insert(map, hex_to_pixel(s, t, layout)) |
|
|
table.insert(map, hex_to_pixel(s, t, layout)) |
|
|
end |
|
|
end |
|
|
end |
|
|
end |
|
|
return map |
|
|
return map |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
-- returns triangular map. radius is used as the triangle side length. |
|
|
|
|
|
function tri_map_store(layout) |
|
|
|
|
|
|
|
|
-- returns triangular map. |
|
|
|
|
|
function map_triangular_init(layout, size) |
|
|
map = {} |
|
|
map = {} |
|
|
for s = 0, layout.radius do |
|
|
|
|
|
for t = layout.radius - s, layout.radius do |
|
|
|
|
|
|
|
|
setmetatable(map, {__index={layout=layout, shape=triangular}}) |
|
|
|
|
|
|
|
|
|
|
|
for s = 0, size do |
|
|
|
|
|
for t = size - s, size do |
|
|
table.insert(map, hex_to_pixel(s, t, layout)) |
|
|
table.insert(map, hex_to_pixel(s, t, layout)) |
|
|
end |
|
|
end |
|
|
end |
|
|
end |
|
@ -145,11 +146,13 @@ function tri_map_store(layout) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
-- returns hexagonal map. length of map is radius * 2 + 1 |
|
|
-- returns hexagonal map. length of map is radius * 2 + 1 |
|
|
function hex_map_store(layout) |
|
|
|
|
|
|
|
|
function map_hexagonal_init(layout, radius) |
|
|
map = {} |
|
|
map = {} |
|
|
for s = -layout.radius, layout.radius do |
|
|
|
|
|
t1 = math.max(-layout.radius, -s - layout.radius) |
|
|
|
|
|
t2 = math.min(layout.radius, -s + layout.radius) |
|
|
|
|
|
|
|
|
setmetatable(map, {__index={layout=layout, shape=hexagonal}}) |
|
|
|
|
|
|
|
|
|
|
|
for s = -radius, radius do |
|
|
|
|
|
t1 = math.max(-radius, -s - radius) |
|
|
|
|
|
t2 = math.min(radius, -s + radius) |
|
|
|
|
|
|
|
|
for t = t1, t2 do |
|
|
for t = t1, t2 do |
|
|
table.insert(map, hex_to_pixel(s, t, layout)) |
|
|
table.insert(map, hex_to_pixel(s, t, layout)) |
|
@ -158,16 +161,28 @@ function hex_map_store(layout) |
|
|
return map |
|
|
return map |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
-- returns rectangular map. width and height are used. |
|
|
|
|
|
function rect_map_store(layout) |
|
|
|
|
|
|
|
|
-- returns rectangular map. |
|
|
|
|
|
function map_rectangular_init(layout, width, height) |
|
|
map = {} |
|
|
map = {} |
|
|
for s = 0, layout.width do |
|
|
|
|
|
soffset = math.floor(s / 2) |
|
|
|
|
|
|
|
|
setmetatable(map, {__index={layout=layout, shape=rectangular}}) |
|
|
|
|
|
|
|
|
|
|
|
for s = 0, width do |
|
|
|
|
|
soffset = math.floor(s/2) |
|
|
|
|
|
|
|
|
for t = -soffset, layout.height - soffset do |
|
|
|
|
|
|
|
|
for t = -soffset, height - soffset do |
|
|
table.insert(map, hex_to_pixel(s, t, layout)) |
|
|
table.insert(map, hex_to_pixel(s, t, layout)) |
|
|
end |
|
|
end |
|
|
end |
|
|
end |
|
|
return map |
|
|
return map |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
-- places single hex into map table, if it is not already present. |
|
|
|
|
|
function map_store(map) |
|
|
|
|
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
-- retrieves single hex from map table, if it is present. |
|
|
|
|
|
function map_retrieve(map) |
|
|
|
|
|
|
|
|
|
|
|
end |
|
|
|
|
|
|