NoshBar's Dumping Ground


Ever since I started to code I wanted to have a game character move from one place to another intelligently, no pre-canned paths.

The A* search algorithm allows you to do this, but due to my age/indifference/attention span I never got round to understanding how it worked ("Heuristics? Weightings? I don't... I... I could just make more particle blood...")

Now that we live in a world filled with free and easily accessible information such as Wikipedia, I could finally find an example that would make sense to me.

With the information on Wikipedia and this article, I gave myself one day to implement the algorithm myself (that last link was the "I can actually do this!" turning point for me, the images with values on were the part that finally made sense to me).

Impatience and Tools

As I didn't have much time to do this, I thought I'd stick with the basics I knew:
Visual Studio Express to develop in C++
SDL and SDL_image for graphics
Tiled for map creation
Paint.NET for tile creation

Seeing as I had absolutely no desire whatsoever to mess around with an XML or JSON library in C++, I made a quick Python script to convert Tiled's native .TMX format into a C++ .H header file containing a class that I could use to get the tile and collision properties of an X,Y co-ordinate.

Using Tiled, tiles in the tileset were given values relating to how costly they were to travel over. I guess this is what they call "weighting". For me it worked out as a multiplier where roads where a multiplier of 1, grass is a multiplier of 20, and sea is a multiplier of 100 (a pure obstacle then really).

So in the end I made diagonal tiles cost just a bit more than travelling across two tiles so it would prefer to keep to the road, and "G" and "H" values would end up being multiplied by the preference value of the current tile.
Tile_Weight = 10,
Tile_Weight_Diagonal = Tile_Weight * 2 + 1,

openTile->G = CostSoFar;
openTile->H = (abs(DestinationX - openTile->X) + abs(DestinationY - openTile->Y)) * Tile_Weight;

openTile->G *= world->map.Cost(openTile->X, openTile->Y);
openTile->H *= world->map.Cost(openTile->X, openTile->Y);


Not too crappy actually.

I had several criteria I wanted a path to follow:
  • A car should prefer to drive on a road
  • A car may drive on grass if it's the only option
  • A car should not take a shortcut by travelling diagonally across a corner tile, i.e., skipping a curved round turn by crossing grass
  • A car can take a shortcut diagonally across grass if it means getting back to the road quicker

As this was all done in a day, I have no doubt that there are issues I haven't found, but I finally understand how it all works and for a small amount of time I felt good about myself.

[ view entry ] permalink print article

Because I suffer from rare diseases (like getting to know people in shops really well then not being able to go there anymore because I feel like I'm making them feel awkward), I ended up porting the smallpt global illumination renderer from 99 lines of C++ code to 666 lines of Turbo Pascal 3 code.

Doing this meant I got to play with loads of things:
  • Turbo Pascal 1, 3, 4, 5 and 7, Delphi...
  • Lazarus
  • FreePascal (for Android and iPhone and GBA)
  • Pascal Script and DWScript (hint: unless you've run out of paint to watch dry, you really want to use the DWScript version)
  • FreeDOS
  • Virtual Machines like Bochs, VirtualBox, DOSBox

See most of the gory blah-blah details here

[ view entry ] ( 472 views ) permalink print article


I tried to find a simple example that showed me:

  • How to call some Lua script from within C
  • How to call a C function from within the Lua script
  • How to pass an instance of a structure/class to the script and have it call functions on it

Being naive I thought finding something like this would be easy. We all know how stories starting with "Being naive..." end (in road-side ditches surrounded by police tape).
I used the latest Lua release (5.2) and stumbled across errors where functions were deprecated (lua_open no longer exists), examples where there was so much going on I simply couldn't understand the basics, examples where a script.lua file was simply executed, examples where in order to pass a C object over I had to create something called a MetaTable and and...

So here it is, a simple example that pretends it's a game. The idea would be that you have an enemy object and it's intelligence would be stored in a Lua script that is loaded at the start of the game, and the "onUpdate" function in that script is called every frame wherein it can change the position, state, colour, happiness, whatever of the object.


This contains no error checking to keep things simple. The Lua documentation on how to do error checking is great, and you should check the result of every call you make, as well as checking to see if variables are actually the type you expect them to be before using them.
Also, I started using Lua for the first time today, so this is simply an example that works for me, your mileage and joy in ignorance may vary.


#include <stdio.h>
#include <stdlib.h>
#include "lua.hpp"

/* This is our game object structure, specifying all the properties that may pertain to an
   object instance in our game. For this example, only the position is important. */
typedef struct GameObject
	int x;
	int y;

/* This function is called from Lua via GameObject.move to move our object instance.
   In Lua-land it passes 3 parameters: the instance; and x and y. */
static int GameObject_move(lua_State *Lua)
	GameObject *instance   = (GameObject*)lua_topointer(Lua, 1);
	const int   x          = lua_tointeger(Lua, 2);
	const int   y          = lua_tointeger(Lua, 3);

	/* adjust the position of the instance and print out that we've done something. */
	instance->x += x;
	instance->y += y;
	printf("GameObject_move(%d, %d)\n\tinstance->x = %d\n\tinstance->y = %d\n", x, y, instance->x, instance->y);

	return 0;

/* This is called from C-land and simply serves as a helper function to set up the update
   function and call it. */
static void scriptOnUpdate(lua_State *Lua, GameObject *Instance)
	/* Fetch the Lua function and make it the thing currently on the stack */
	lua_getglobal(Lua, "onUpdate");
	/* Push the first argument onto the stack, which is the instance we are currently updating */
	lua_pushlightuserdata(Lua, Instance);
	/* Call the function on the stack telling it that it has 1 parameter and no return values */
	lua_call(Lua, 1, 0);

/* This sets up the Lua context and registers our functions with it. */
static lua_State* createLuaContext()
	/* Build up an array of functions we want to expost for the "GameObject" namespace. */
	/* Add whatever functions you want to expose to this array. */
	const luaL_Reg gameObjectFunctions[] =
		{ "move", GameObject_move }, /* Lua GameObject.move() maps to GameObject_move() in C-land */
		{ 0, 0 }                     /* NULL terminator to signal the end of the array */

	/* This is the Lua script we plan on running, it would probably be in a resource file somewhere. */
	/* scriptOnUpdate() above calls this function, which in turn calls the GameObject_move() function above */
	const char *script = 
		"function onUpdate(instance)\n" \
		"    GameObject.move(instance, 4, 2)\n" \

	lua_State *lua = luaL_newstate();      /* Creates a new context, lua_open replacement in versions > 5 */
	luaL_openlibs(lua);                    /* Adds the default Lua libraries to the context */
	luaL_newlib(lua, gameObjectFunctions); /* Adds our functions to the context */
	lua_setglobal(lua, "GameObject");      /* Assigns a namespace to our most recently added functions */
	luaL_loadstring(lua, script);          /* Loads our script into the context */
	lua_call(lua, 0, 0);                   /* This "primes" our context, without this, things will fail! */

	return lua;

int main(int argc, char *argv[])
	/* This is our game instance that we intend on passing to the Lua script for it to have its way with. */
	GameObject instance;
	instance.x = 6;
	instance.y = 8;

	lua_State *lua = createLuaContext(); /* Set up the lua context */
	scriptOnUpdate(lua, &instance);      /* Call the Lua script "onUpdate" function passing our current object instance */
	lua_close(lua);                      /* ... and we're done. clean up shop and go home! */

	printf("\nPress return to quit.\n");
	return 0;

[ view entry ] ( 437 views ) permalink print article

Sometimes someone I know feels down,
sometimes that person is me,
sometimes I want to see what monstrosity
I can make with default assets in programs.

This was going to be a life-affirming cheerleader,
but the giggles got hold of the controls.
(click the image for a larger version)

Text made in Blender
Cheerleader set up in Daz3D
Background from here

[ view entry ] ( 946 views ) permalink print article

Recently I started looking at ways to get Drumpster to output MIDI on non-Windows platforms and came across FluidSynth. It allows for MIDI playback and conversion to WAV files if you so choose. In order to do so, you need to provide a SoundFont (which provides instrument samples that are used to convert the MIDI data to wave data).

In my search for some free SoundFonts, I came across the Rhodes Piano one listed here (decompressor here), and I find it absolutely beautiful to listen to. It has a melancholic joy to it (making me think of Everclear), sometimes sounding menacing like the music in Tron.

So armed with the Rhodes SoundFont and a Windows binary from here, I converted a few MIDI files and put them up on SoundCloud.

To convert some yourself, an example commandline for this would be:
fluidsynth.exe -F output.wav input.mid jRhodes3.sf2

P.S. It also turns out that FluidSynth has a delightfully easy to use Python wrapper here.

[ view entry ] ( 2382 views ) permalink print article

<<First <Newer | Older> Last>>