Strange sizeof

24, Nov, 2017

It is really odd pattern, but within the specs:

int *a = malloc(sizeof(*a));

The malloc’ed VLA

23, Nov, 2017
Many times, I heard that variable length arrays in C are evil. “Because, they are allocated on stack.” Oh, really?
int n = foo();
int m = bar();

float (*arr)[m] = malloc(sizeof(float) * n * m);
This is THE simplest way for dynamically allocated n-dimensional arrays in C! No more index calculation, simply:
for (int i = 0, k = 0; i < n; i++) {
    for (int j = 0; j < m; j++, k++) {
        arr[i][j] = k;
    }
}

Path finding pushed to the extent

22, Nov, 2017
For some time, I was playing with path finding algorithms. I think everybody (at least everybody in gamedev) knows Dijkstra or A*. The first is especially usefull if you want to map all paths from some point on the map. I got working version. Quite well working. But, I wanted to enhance the basic approach - with somehow fancy eikonal equation. By solving this equation you get an exact geodesic distance map. Then you can walk smoothly in direction of gradient of distance function. It looks super easy on cartesian grids. Just finite differeces, one easy update formula for each cell node with Dijkstra like propagation (fast marching method). It also works quite well on them. You may get very nice paths with it. So, if you have tiled engine (2D RTS, RPG, whatever) go with it. It may be good also for hexa - but I had not tested it. Nontheless, things get complicated on general navigation meshes. If you look closer, the equation is hyperbolic and the original algorithm has special, “upwind like”, scheme to solve. Not easy to reproduce on triangular cells. The numerical schemes are more like puzzle to implement and got strange results on irregular grids. After some tests for various triangular navimeshes, it appeared that old Dijkstra is still the best for general grids.

Hello World!

11, Nov, 2017

Yet another blog on the programming hacks and tricks.