Jump to content

  • Log In with Google      Sign In   
  • Create Account






More ANL refactoring

Posted by JTippetts, 19 April 2013 · 481 views

Goblinson Crusoe Accidental Noise Library hex based turn based rpg
I've been working on fleshing out island generation in GC. The current generator is kind of a legacy holdover from much earlier versions of GC; the basics of it were in the very first prototype that started this project. (Crikey, I had to scroll back through my journal for a long ways to get to that. Has it really been over two years since I started this thing?)

In the process of fleshing things out more (adding more resource types, playing with spawn-point distribution for certain map types, etc...) I've taken the opportunity to do some edits to the Accidental Noise Library. Some pretty major ones.

First of all, I fixed up constructors for all the module types. Many of them were missing some constructors that make it easier to construct objects quickly and efficiently; in their absence, construction of certain modules was a tedious affair of constructing then calling methods to set members. I've added in constructors for most of the options on most of the modules, so that for the majority of constructs you can construct your modules fully in a single call.

Secondly, I've changed the internal structure of all modules to use smart pointers rather than raw pointers. This changes allows me to construct entire module chains without having to store pointers to all of the child modules in a structure somewhere. Now, I can build the chain then just keep a pointer to the root module of the chain. Saves me a lot on book-keeping.

Thirdly, I've implemented a class for elegantly constructing chains of modules with the possibility of storing pointers internally to all the child modules. (If you want to re-seed modules individually without re-seeding the entire structure, this is the way to go.) I constructed an interface that allows me to do chained construction of modules an a fairly elegant manner. This has made it easier to build elaborate function chains in C++. (This sort of functionality was available for a long time in Lua, due to a wrapper script class I wrote. The new method replaces this, though.)

Now, in C++, I can construct a chain of modules like so:
    anl::CTreeContainer tree;
    tree
    .sphere           ("Sphere",          1.0,0.0,0.0,0.0)
    .fractal          ("FBM1",            anl::FBM, anl::GRADIENT, anl::QUINTIC, 8, 2)
    .autoCorrect      ("FBMAC1",          "FBM1", -0.25, 0.25)
    .fractal          ("FBM2",            anl::FBM, anl::GRADIENT, anl::QUINTIC, 8, 2)
    .autoCorrect      ("FBMAC2",          "FBM2", -0.25, 0.25)
    .translateDomain  ("T1",              "FBMAC1", 0.125, 0.125, 0.125)
    .translateDomain  ("T2",              "FBMAC2", -0.125, -0.125, -0.125)
    .translateDomain  ("Turb",            "Sphere", "T1", "T2", 0.0)
    .rgbaImplicitGrayscale   ("RGBA",     "Turb");
    auto m=tree.getRGBAModule("RGBA");
I've never been a huge fan of that type of chained calling, but in this case it is quite a bit more concise.

The CTreeContainer class implements factory methods for each of the module types (with overloads for all parameter possibilities) allowing you to build your chain in place. Most of the parameter inputs to a module can be either double-precision constants or the output of other modules, so the parameterization of the factories reflects this. In the above, I construct first a Sphere with a radius of 1 centered at (0,0,0). Then I construct two FBM fractals, and tie them to AutoCorrect modules to fix their output into the range [-0.25,0.25]. Then, I feed the output of those to some TranslateDomain functions, to shift them to different parts of the fractal. (If you don't want to reseed anything, this is a good way of removing correlation from different fractals, which are all constructed with the same internal seed.) Finally, the two corrected and translated fractals are used to apply turbulence to the output of the sphere in another TranslateDomain function, the result is fed to an ImplicitGrayscale adapter which converts the double output of the noise modules to an RGBA value, and the tree is queried for a pointer to this root module.

Now, in the course of all of this I stumbled upon a problem. As I mentioned before, most of the modules are very parameterized. Inputs can be either double constants or other modules. This means, though, that the process of building constructors is... messy.

Consider, for example, the CRGBASelect module. This module takes two RGBA inputs as colors to select between, and provides inputs for Control (value that determines the selection mix amount), Threshold (marks the center point of the transition range) and Falloff (specifies the width of the soft blend transition range between the two input colors). Either RGBA input can be either a color constant or the output of a module chain with an RGBA module at the root. Either of the Control, Threshold and Falloff inputs can be either a constant double value or the output of an implicit module. This means that in order to provide full constructors for the object, I have to provide 25+1 (or, 33) separate constructors.

(Beware. Ugly code ahead.)
Spoiler


I hate this. Granted, it was easier to just write a quick Lua script to generate all of the constructors, but it still seems like such a waste to me. Unfortunately, my C++ foo (which was always relatively weak) has grown even weaker in my years of working mostly with Lua, so I just can't quite wrap my head around a more elegant method of handling this.

If anybody reading this knows a more elegant solution to this problem, I'd like to hear it. I probably won't upload this new version until I've fully researched this problem and am satisfied I have the best answer.

A final change I have made is to eliminate/consolidate some modules. As the library grew, I would add modules as I needed them so that I ended up with a lot of separate modules for basic functions such as cos, pow, abs, etc... It's unnecessary to have separate functions for these, though, so I consolidated them into a single Math module, with the operation controlled by a parameter.

Anyway, that's what I've been up to. I haven't done anything of a cool visual nature since the last screenies were posted. I thought about posting some screenies anyway, but I figured it would be pretty pointless.




Be interested to see this in action... meaning you have the on the fly ability to generate the islands now!!! show many different island types. Keep up the cool ideas and work!!

Please post some screenies anyway.

 

Since you prefer Lua you could keep the use of C++ restricted to only the internals of a module, outline the rest in Lua and use the Lua registry or some other serialization process (e.g. protocol buffers) to pass data in and out. I've always found C++ to be too rigid to allow for a convenient JSON-like method of chaining parameters.

Well, I'm working on a container to load and save tree structures to YAML. yaml-cpp, which I used in some previous experiments, is a pretty handy little library for such things and is making it pretty easy.

When do you plan to release this refactored ANL version? Anytime soon? Also I'd really suggest using github or bitbucket (or something similar) rather than sourceforge for hosting the project.

Recent Comments

July 2014 »

S M T W T F S
  12345
6789101112
13141516171819
2021 22 23242526
2728293031  
PARTNERS