Jump to content

  • Log In with Google      Sign In   
  • Create Account

/* Why you crying? */

More ANL refactoring

Posted by , 19 April 2013 - - - - - - · 727 views
Goblinson Crusoe and 4 more...
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;
    .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.)

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.

Goblinson Crusoe, Urho3D, and Other Stuffs

Posted by , 02 April 2013 - - - - - - · 2,059 views
Goblinson Crusoe, Urho3D and 3 more...
I made an executive decision this last week about GC. To this point, I've been implementing it as prototype code on top of Urho3D, but I've been using my original Lua-based framework and ignoring the fact that Urho3D implements a quite robust framework of its own.

Like so many others, I've jumped on the component/entity bandwagon in recent years. When I first started GC, I started experimenting with an object composition framework, and over the months I've refined it and iterated on it. It works pretty good. However, Urho3D implements its own object system under the hood, and when I migrated to this new 3D library I hacked together an engine kernel that did the perfunctory tasks it needed to using Urho3D's built-in framework, then tacked my Lua framework on top. It works, but it seems a little bit counter-productive, and the Lua bindings to make it work have been ugly and hackish, with a lot of intermediary and proxy objects that have been kind of a pain to code (and are not complete yet for all renderable types).

When implementing an object composition framework, one of the thorniest problems to solve is the problem of inter-entity communication. Good design will reduce coupling between entities as much as possible. Now, some frameworks implement entity-to-entity communication by providing the interface to directly query an entity for a given component or class of components. Something like entity->GetComponent<Transform>().SetPosition(x,y,z). Other systems opt to use a message passing system, where instead of querying for a particular component, you instead just "send a note" to the target entity. entity->SendMessage("SetPosition", positiondata). Each of these particular approaches has its advantages and disadvantages.

An advantage of the first scheme is directness. You set the entity's position directly, through a known interface, with relatively terse code that doesn't include a lot of baggage or introduce many opportunities for error; most errors will be caught at compile time. A disadvantage, of course, is tight coupling; the calling entity has to know that the target entity has a Transform component, or at least is likely to have one. (GetComponent() can always return nullptr). This knowledge of the component make-up of another entity runs somewhat counter to the philosophy of composition design, but I have found that in actual usage it isn't too much of a problem. Games are orderly enough that you can usually make some assumptions about what kind of object you are acting on at any given time.

The message passing scheme is just the opposite. Handing someone a note does not require that you know anything about that someone, so passing a "SetPosition" message to an entity that has no Transform component would be silently ignored, and it is not required that the sender know whether the receiver implements that particular component. The compilation unit doing the sending doesn't even need to include headers for the targeted component, so compile-time coupling is reduced as well. However, the act of communicating via message passing is indirect and somewhat clunky, especially in a statically typed language such as C++.

C++ does not handle the idea of "free form data" very elegantly out of the box. When implementing a message-passing scheme, you often need to pass different data depending on the message being passed. A "SetPosition" message, of course, would need to pass "x", "y" and "z" data. A SendRawDamage message, on the other hand, would need to send different data: a damage value, a damage type/class, the ID of the originating object, etc...

Implementing this kind of free-form data structure in C++ involves some tricky and/or tedious programming, which is why I favor dynamically-typed languages such as Lua for this type of thing. In Lua, passing a message of this sort is trivial, as you can construct the free-form data packet in-place: entity:SendMessage("SetPosition", {x=5, y=0, z=10}) Lua's tables are the very essence of free-form data structures.

In C++, you end up with mildly hackish schemes such as Win32's LPARAM, WPARAM, etc... parameters which are re-interpreted based on the context of the event being sent. More robust solutions (such as what Urho3D implements) operate on the idea of a Variant data object, or an object that can hold any of a number of data types depending on the usage. This solution is still a little bit hackish, but it is an idea that has a long and distinguished history of implementation in computer science. The basic variant structure in C/C++ is union, but it can be implemented in other ways as well, ways that are a bit safer than union. But the idea is the same general idea as union: a chunk of data that can represent any of a number of types.

Urho3D implements a VariantMap, which is a map (Key->Value) of Variants. Message payload data is sent via a VariantMap to any registered message handlers. Of course, language limitations make it difficult to implement a VariantMap class that can be constructed in-place with the elegance of a Lua table, so you typically end up instead with laborious constructions such as:
VariantMap data;
node_->SendEvent(E_UPDATETRANSFORM, data);
That sort of thing is fine for a sparse message passing scheme, but when message passing forms the very backbone of your paradigm it gets icky in a big hurry.

The thing about Urho3D, however, is that it actually implements both schemes. You can query a node directly for a given component, or you can send an event through the event system. Events can be used to handle the bulk of entity-to-entity or kernel-to-entity communication, while certain components can query their owner node directly for other components. For example, a GC combat object will always have a CommandQueueComponent, which acts as the strings of the puppet. It implements the low-level functionality (wait, move here, cast this spell) of combat. They will also always have a higher level Controller. Controller comes in 2 main flavors: Player and AI. The Controller makes the high-level decisions, and passes the results of that thinking down to the CommandQueue for execution. In this case, it makes sense to be able to query the node directly for a given component, and call methods on that component rather than jumping through the laborious event-sending hoops. If an object has a Controller, it will also always have a CommandQueue as well (unless there is a bug in the object spawning code.)

Another advantage that Urho offers out of the box is robust serialization of scene/node structures including custom components and attributes. All nodes and all components implement a Serializable interface, and inside the interface you can designate specific members as attributes, to be automatically serialized. Serialization is another aspect of developing in C++ that can be sort of tricky, requiring some tedious coding (another advantage of Lua; a quick and dirty recursive table traversal can easily and quickly dump a Lua table to a file).

In Urho3D, serialization is well-supported; by extension, so is network replication, providing a strong framework for multiplayer development. It's actually a pretty decent framework, a sort of undiscovered gem, and I'm pretty glad I have finally taken the time to get to know it better. I'm in the process of converting my prototype Lua code over to use the core Urho3D framework right now, and it's going pretty smoothly. There have been wrinkles and snags, but they've been relatively minor.

A final note about Urho3D: it implements a number of other useful systems, including a strong ScriptObject component functionality allowing you to write component behavior in AngelScript for run-time modification of object behaviors. This is built-in to the system, robust and clean. As well, Physics is well supported through a set of physics components built atop Bullet. Audio is in there as well, and Networking. The only big thing lacking (at the moment) is pathfinding, but Recast/Detour can fill that hole with a little bit of effort, and the Urho web page indicates that pahtfinding is being looked at for future releases. Additionally, Urho3D can be built for either DirectX (currently D3D9, shader-only) or OpenGL (2+, shader-only) with build paths for Windows, Mac, Android, etc.... If anybody is looking for a good, solid, fairly powerful 3D framework and is tired of mucking about with the aging and creaky existing open-source frameworks, Urho3D might be a good candidate. The developer (AgentC on these forums) has been friendly and willing to help with my occasional issues as well. The community around Urho3D is still rather small, but I hope to see it grow as the word gets out.