Pages

Thursday, August 2, 2012

Day 45 - ECS & Behavior trees

Entity Component System - (continued)
After various attempts, careful considerations and long meditations under an authentic ancient japanese sakura from the naruto period... I decided to roll my own lolECS.

It does what I need, which is nice, though it is not internally as optimized as Artemis.

- Uses Universaly Unique Identifiers which auto-instantly solves the id problem mentionned in the previous post : it allows insertion of foreign entities without ids collision headaches.

- Uses a messaging system to communicate : strong decoupling - systems don't have to know each others, they just send and listen to messages they are interested in.

- Systems are abstract and treated like services you can plug concrete systems in a static/singleton class GameSystems : very easy to implement client and server - they have different implentations of the same set of systems. (eg: a client would not have the same physics system implementation than the server).

- Commands are special kind of messages  : they are actions that need to be executed in the game world - this will prove very useful for debugging, console commands, scripting (if need to) and more importanly networking (the server will send mostly commands to clients).

I could throw some UML diagram but the structure is pretty much like the in the previous episode.
The only thing interesting is the specialized Postman system which handles messaging and subscribing for other systems.

RS2 ECS in practice
I create entities with the EntityManager, then build them via an EntityFactory which is a registry of EntityBuilders. For instance the crate builder knows what component to add and how to set them up to make an entity that behaves and look like a crate. I prefer the factory approach to prototypes/clones/templates as you can more easily add variations or conditional building (eg: not all crates may look or weight the same). The concrete builders are specific to RS2, but the Factory, ECS and most components and systems are part of my engine which I hope to re-use for other awesome games.

In code, creating, building, spawning and shooting a crate from the camera look like this:

CrateBuilder : building crates since overused meme date.


Would someone please insert an original caption here? No caption no fun.

Note the use of command messages to spawn and shoot the entity.
And the result when I shoot a bunch of crates at a wall:

"Advanced Crate Shooting" game or "Blocky Zombies" game? Make up your mind damn it!


Note that the crates are savagely stacked, as we now have real ultimate physics(*) thanks to jbullet. It will be fun to collapse a bunch of crates on unsuspecting npcs :p
There is negligible fps loss when the crates are at rest, but a noticeable drop in fps when a lot of crates are moving and colliding. We'll see how it runs in a real game situation.

BEHAVIOR TREES
 If you don't know what BTs are you can find some articles there.
They are often presented like more advanced or specialized HTN/HFSMs, but to me they remind me a lot of LISP program trees. Which leads neatly to the idea of using genetic programming to evolve AIs. Surely someone will or has already catch on that!?

Anyway for RS2 AI I'm considering using behavior trees. So I designed and implemented a Behavior Tree package for my engine.

Behavior Tree. UML you see. Haiku captionee.

(Diagram made with DIA)
The only things of note are :
  1. the use of a generic IBtContext which :
    1. allows one instance of a behavior tree to be shared for many usages (similar to a flyweight pattern).
    2. allows to narrow the context for concrete uses.
  2.  a clear separation of Terminals (leafs) vs Composites (inner nodes) and Conditions (predicates) vs Actions.
A deeper hieararchy than really needed but its much cleaner and should make stuff like a visual editor or scripting more easy to do.

Since I implemented a generic BT package, I can use it for other purposes. For instance I could use BTs for scripting some entities (doors, levers, whatnot). This would fit nicely into a CpBehaviorTree Component :D

(*) which as you may know is functionaly equivalent to Real Ultimate Power but applied to games. Man, this gets me so pumped!

Day 47 update and exercise for the reader: There are two big flaws in this BT design, which I have now fixed. They would produce incorrect behavior at runtime. Can you spot them? Hint: context.

End of post.

70 comments:

  1. Really like the programming articles roguedjack. as someone who's learning programming myself, I find them very informative and well written. please continue to post them.

    ReplyDelete
  2. A lot of good stuff here.
    Personally I'm not fan of "build from scratch", but I understand you would have control over all in your game, so good work so far!

    ecs: using of UUID is a good idea and using of messages too (you have to do so for client/server.. but question: in some future post can you share some thoughts about multiplayer? Cooperative? Pvp? Limits?) but I'm not sure to understand differences between Messages and Command. In your game all seems to be a Command (move zombie from here to there), where do you use Messages ? (maybe it's just abstract). For event subscribing you can take a look to EventBus here: http://code.google.com/p/guava-libraries/wiki/EventBusExplained


    BT: need more examples, or better a scenario "zombie wandering around, when see player, attack him"

    thanks for this thoughts, really good!

    ReplyDelete
    Replies
    1. MP
      I would like it to be as free form as possible (pvp on by default) but I think I'll add an option to disable pvp if the host wants to for purelly coops.

      MSG vs CMD
      Messages are notifications : "this happened".
      Commands are action requests : "make this happen".
      Messages are posted by the entity manager (eg: a component has been added to an entity) or systems (the chunk system notifies a chunk is ready to be added to the scene).

      EventBus
      What I do share some similarities. The PostMan system acts like an event bus : you register and post there not directly to messages sources, and registering to a message class A also registers you to all subclasses of A.
      Eg:
      to listen to all types of messages:
      GameSystems.Postman.subscribe(this, Message.class);
      to listen to the spawn entity command:
      GameSystems.Postman.subscribe(this, CmdEntitySpawn.class);
      where "this" implement IMessageHandler.


      BT
      I'll detail them later when I really start using them :p

      Delete
    2. Per prior post, this wasn't completely from scratch. How major competing frameworks targeting Java (e.g., Artemis) was used; they just proved critically flawed for client-server. [Which I found very informative, that's not something you hear much about].

      Delete
    3. Wow Kenneth hold on here, I didn't say they are critically flawed. They are a "bit less easy" to use in a C-S environement due to the way they handle ids and that was "my problem" - which other people might have found solutions to within said frameworks.
      Please don't overestimate my competence in evaluating other peoples work ^^

      Delete
  3. I indulged in psychological projection: what I would need to convince myself of, to write what I'm reading.

    From what I read of the Artemis docs, I'd use it without hesitation for a non-networked Java program, or a networked one that didn't have to scale. You were very clear about the apparent requirements vs assumptions mismatch. If there is a way to avoid the paradox, it wasn't documented clearly enough to be found quickly. Thus, workaround strategies (I agree that the two-way dictionary approach should be better than a One True ID Server).

    (My dominant programming language is C++, but work generally uses Perl and PHP more. I'm about as weak in Java as I am in either Python or Ruby: use when in their niche, but wouldn't try to get a job requiring proficiency.)

    ReplyDelete
    Replies
    1. You're right, I jumped on the "critically flawed" wording :)
      I would use Artemis too for SP, it is highly optimized.

      Unrelated, but since you use PHP do you know any good PHP IDE, especially one that has refactoring (renamming of classes/functions)?

      Delete
    2. Free: no. If I was desperate enough to implement code quality checks in the editor, I'd go with JEdit. However, I find batch files/shell scripts that call Perl, Python, or Ruby style checkers more practical when hand-rolling that sort of thing.

      Commercial: I haven't worked with enough of these to have an informed opinion. SlickEdit has decent syntax highlighting and weak refactoring support as of 2008 (the license for that copy is bound to a now-dead machine), but I haven't been able to make a cashflow case for either upgrading, or licensing for multiple machines.

      Delete
  4. These lolphysics are totally awesome. I wish I could code, but the best you'd get from me on any engine would be somehow freezing your computer.

    I have real respect for programmers; it looks really hard.

    ReplyDelete
  5. I enjoy how you talk back to your players. Makes me feel like we get a say in what happens.

    ReplyDelete
  6. Q_Q PLAYABLE CRATE THROWING BUILD. NOW. lol. :)

    I am glad to see things are progressing. Can't wait to see more and more and more. :D

    ReplyDelete
  7. I'm sad to see there hasn't been a recent post!

    ReplyDelete
  8. Is this dead then? :(

    ReplyDelete
  9. Quit slacking and get back to work !

    ReplyDelete
  10. I'm assuming this is dead then :( I loved rogue survivor... Shame. Maybe he'll come back to next summer...

    ReplyDelete
  11. So uh, have you watched "the walking dead" jack?

    ReplyDelete
    Replies
    1. Jack hasn't been online in over 5 months. Don't waste your breath

      Delete
    2. So then how about you anonymous? do you watch it by any chance?

      Delete
    3. I check back every few months. Wonder where he went? Perhaps something happened to him!! :/

      Delete
    4. My guess is he has been bitten and, as a good Z-fighter, locked himself so he wouldn't harm anybody.
      Or maybe his Internet is down since seven months.

      Delete
  12. Jack come to lifeeee!!! PLEASE!

    ReplyDelete
  13. He won't even respond to Emails :/ Any other ideas on how to contact him?

    ReplyDelete
  14. maybe he is stalled out because no one spotted the bt flaws, and he is patiently waiting for an answer.

    ReplyDelete
  15. I think something may of happened to rogue jack he seems to have disappeared

    ReplyDelete
  16. Dammit, i was looking forward to more updates of this. Hopefully he's alright.

    ReplyDelete
  17. I just hope he is doing fine and is also busy.

    ReplyDelete
  18. Jack please come back and finish this game i was loving it .... *starts to cry*

    ReplyDelete
  19. Come on Rogue! Come back from the dead!!!

    ReplyDelete
  20. Hashmeer ShashmeerMay 6, 2013 at 12:51 AM

    This is bad,
    now I'm sad.
    No rouged jack
    makes Hashmeer mad!

    ReplyDelete
  21. Jack, if you're reading this I hope you're doing alright! I also hope that some day you can come back and keep working on RS!

    ReplyDelete
  22. I think Jack is dead,im sorry guys.

    ReplyDelete
  23. Someone has any idea about roguedjack ? I just hope he is fine.

    ReplyDelete
  24. My opinion is that he went to far into his investigations about zombies, and the CIA got him.
    Farewell, Roguedjack, I hope they erase your memory soon so that you can get back to us (without too much brain damage)

    ReplyDelete
  25. Is anyone willing to continue the Rogue Survivor development?

    ReplyDelete
  26. I'm looking into creating a rogue survivor II of my own, since we don't know what happened to roguedjack and he hasn't responded for about a year now. I'm thinking of making it isometric, but if I can make it 3D even better.

    ReplyDelete
    Replies
    1. I think something along the lines of Cataclysm: DDA would be fun.
      (Same guy)

      Delete
  27. R.I.P RougeJack ;_; See you on the other side...

    ReplyDelete
  28. Right, so let's sum up:
    - Rogue Survivor - unfinished, development stopped dead.
    - Rogue Survivor II - unfinished, development stopped dead.

    Looking forward to RougeJack starting another Rogue Survivor project.

    ReplyDelete
    Replies
    1. at least rogue survivor was relased while rogue survivor II never seen sunlight

      Delete
  29. I hope RogueJack is fine also and hope that someone will continue this.

    ReplyDelete
  30. i don't even know why i keep coming back here. no one has seen roguedjack since 2012.

    ReplyDelete
    Replies
    1. just because is a good enough reason I mean maybe he will come back.

      Delete
  31. Would you remember the famous men?
    Who had to fall, to rise again?

    ReplyDelete
  32. When I was searching to see if there were survivor games a few weeks ago, I was lucky to find the first RogueSurvivor. When I saw that there was a second one coming out, I was Thrilled! however, when I went to look through it, I saw it was discontinued. now, I see the loss of potential for a game based off the first RogueSurvivor and wish to see it rise once more.

    ReplyDelete
  33. Illuminati took him. You'll soon figure out why. Tip: check his famous sign.

    ReplyDelete
  34. Jack, I miss you! your little tile zombie game puts all the other zombie survivals to shame!

    ReplyDelete
  35. I come back here and check every once and awhile to see if anything has happened with this. I'm always disappointed.

    ReplyDelete
  36. We're still checking here Jack. :(

    ReplyDelete
  37. Any day now, i can feel it. he just wants to surprise us with an AMAZINGLY large update.

    ReplyDelete
  38. Hey man, get beck to making stuff. DO IT!

    ReplyDelete
  39. we miss you rogued jack ;(

    ReplyDelete
  40. i check back here every few months. fuk errthang ;-; imy rj

    ReplyDelete
  41. Well, here is to another year of hoping.

    ReplyDelete
  42. I THINK I KNOW THE BT ANSWER!
    IF ONE OF THE AI SPOTS ANOTHER ENEMY AI WHILST HUNGRY, THE NATURAL RESPONSE WOULD BE TO EAT, INSTEAD OF FLEE/FIGHT. CREATING A BUNCH OF PEOPLE STANDING AROUND AND EATING AS OPPOSED TO FIGHTING.
    COME BACK NOW

    ReplyDelete
  43. I can't help but wonder what happened to Jack

    ReplyDelete
  44. I wonder how long we'll be waiting. :( I'd love if he even just went back to Rogue Survivor and improved upon it.

    ReplyDelete
    Replies
    1. dont worry bois one day rogued jack will appear with rogue survivor 3... wishful thinking right

      Delete
  45. Maybe next year. Here's to hoping.

    ReplyDelete
  46. I don't even care for the game anymore...i just wonder if he still alive.

    ReplyDelete
  47. 2016 everyone! just a few more years until either the blog goes down or he returns!

    ReplyDelete
  48. Maybe next month eh?

    ReplyDelete
  49. COVID check in for posterity!

    ReplyDelete