Skip to content

04. Grid

RhettTR edited this page Jan 16, 2024 · 1 revision

A grid is the set of points where counters/cards can be placed in a board game. The points have a snap-to feature that prevents the counters/cards from being placed anywhere else. The module designer must be able to specify such a grid.

There may be four different types of grid in a board game.

  • No grid. Areas where counters/cards may be placed freely.
  • Custom grid. Areas where grid points may be placed individually by the module designer.
  • Square grid. Areas with a regular grid with a constant horizontal and vertical spacing between grid points.
  • Hexagonal grid. A regular grid with grid points in a hexagonal pattern.

Obviously, the no-grid type has no snap-to feature while the other have.

Grids exist in square areas. A board game may contain many areas with different types of grid. There may be different areas with the same regular grid but where the size of the grid is different. There can be connected square areas with the same type and size of grid. Such areas should be regarded as a single area. In this manner more complex (polygon) grid areas can be constructed.

It must be possible to define separate areas with different types of grid on the board game map. It must be possible to place grid points in a custom grid area. It must be possible to define the grid points in square and hexagonal grid areas. And it must be possible to regard neighboring areas with the same grid type and size as one area.

The actual calculation of the areas and their grids can be in the C++ part of the engine. What the module designer needs is the ability to freely define the grid in each area of the map with a Luau call.


For a hexagonal grid the module designer needs to be able to specify the size of a hex and the offset of the grid.

A hexagonal grid is defined uniquely by two numbers, what is called a and b in the illustration below.

hex.jpg

a is the length of the top horizontal edge of a hex. b is the vertical length of the hex. a and b define the size of the hex. These two parameters were chosen because they are easy to measure on a board game map.

c in the illustration can be calculated from b.

(b/2) / c = tan 60 or c = b / ( 2*tan 60 )

The x distance between grid points is (a/2) + c + (a/2) or a + c. The y distance between grid points is b.

Alternating columns of grid points have an offset of either (b/2) or 0.

The x-offset and y-offset define the initial position of the grid and must be specified by the module developer. Typically the x-offset is 0 and the y-offset is b/2.

In practice a and b must be floating numbers. This makes it possible to precisely fit the grid into a given area.


With the same two parameters a and b it is possible to define any points on a hex grid.

Imagine a module designer wants to snap to the middle of a hex edge.

hex.jpg

A regular grid will have a cycle (repeat itself) along the x and y axis.

A middle edge grid would be:

x-cycle = 4

y-cycle = 2

Write out the x axis.

Start   c/2 , b/4
1st inc c/2+c/2+a/2 , 0
2nd inc c/2+2*(c/2+a/2) , b/4
3rd inc c/2+3*(c/2+a/2) , b/2
4th inc c/2+4*(c/2+a/2) , b/4   <--- back at start of x-cycle after 4 increments
5th inc c/2+5*(c/2+a/2) , 0
.
.

Likewise the y axis.

Start   c/2 , b/4             with inc = b/2
1st inc c/2+c/2+a/2 , 0       with inc = b
2nd inc c/2+2*(c/2+a/2) , b/4 with inc = b/2   <--- back at start of y-cycle after 2 increments
.
.

Grid

In a first test a few limitations are imposed. There is only one area. It has a hexagonal grid.

Luau part:

hexagonalGrid ( a, b, xoff, yoff )

gridSpecify ( radius, r, g, b, alpha )

gridShow ( boolean )

C++ part:

Generate grid points with given size and offset.

Draw grid points with the given radius and color (r,g,b) and alpha.

Snap-to-nearest-grid-point functionality.

The ability to reload the script while the engine is running. This is important for creating a close loop in module development. Changes should be shown immediately.

hex.jpg


CPLUS_INCLUDE_PATH=/usr/include/gtk-3.0:/usr/include/glib-2.0:/usr/lib/x86_64-linux-gnu/glib-2.0/include:/usr/include/pango-1.0:/usr/include/harfbuzz:/usr/include/cairo:/usr/include/gdk-pixbuf-2.0:/usr/include/atk-1.0:/usr/include/graphene-1.0:/usr/lib/x86_64-linux-gnu/graphene-1.0/include;export CPLUS_INCLUDE_PATH

LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu;export LD_LIBRARY_PATH

g++ -Wall -o main main.cpp luau.cpp grid.cpp -I/home/me/luau/VM/include -I/home/me/luau/Compiler/include -I/home/me/Utvikling/Alben/grid -L/home/me/luau/build/release -lluauvm -lluaucompiler -lluauast -lisocline -lgtk-3 -lgdk-3 -lgio-2.0 -lgobject-2.0 -lgdk_pixbuf-2.0 -lglib-2.0 -lcairo