93 points · by gw · 12y ago · 31 comments
Kudos to you OP, good job.
For example, let's take the age-old problem of two people in the game (potentially both AI-controlled) both deciding to pick up an item. If two separate threads are handling the logic, both having a view of the world with the item on the ground, and both of them end up trying to update the world state such that the item ends up in somebody's inventory, what is a clean and elegant way to resolve that?
I've considered using STM, having a ref to each entity in the game, and wrapping each bit of game logic in a transaction, thus rolling back entity updates that conflict with previously committed entity updates. I'm concerned about the performance implications of this, though. I keep imagining it becoming way too contentious and having more rollbacks than completed transactions.
I don't know how feasible my design is, but my idea is to use Clojure's principles of values/identities and reftypes, every entity in the game is an agent with its own thread (scheduled in a thread pool), inside the agent there are refs (I intended to use atom but for race conditions and synchronized updating, the example you mentioned, I decided to go with refs). Entities can query a global database of entities (tagged, component based) and retrieve their state by dereferencing. If they want to update the state of other entities they can either send a message via the agent (ordered asynchronous update) or act directly onto the inner ref (via transaction) for a synchronized/concurrent update.
It's tricky to explain, and it's probably going to give me a lot of troubles along the way but it's mostly a fun experiment for a private project.
His post indicated that he initially tried the "every entity is an agent" idea, and had performance problems with it - although maybe his real problem was that the entire world was a single ref, and all of the agents would bash on that ref.
Another question, do you have any kind of reverse-lookup table implementation? I see that you're using component-based entities, do you have any way to quickly query your world for entities that match some arbitrary criteria, rather than iterate over all entities in the entire world? I ask because my game idea would involve potentially tens of thousands of entities, and I'm trying to figure out a way to build game logic mini-systems that can quickly find a subset of game entities and perform some function on them. For example "In the spring time find all fruit-bearing trees of a minimum age and spawn unripe fruit on them", or "every 15 seconds find all poisoned characters and deduct 1 health point"
1) Lock-based "update the world state" kind of solution. Whichever thread manages to grab the lock for being able to pick up an item is the winner, and actually gets the item. The world is modified so that the item no longer exists. When the other AI gets the lock and tries to pick up the item, it will get some kind of error letting it know that the item no longer exists. Or it can double-check for the existence of the item, and abort the pick up attempt before an error can be generated.
2) The other resolution I can think of is to have a queue of state updates. Basically, both AI broadcast to a queue that they wish to pick up an item. Whichever AI queues first, gets the item. Some logic runs to give the item to that AI, (thus removing it from the world) and then sees that the second attempt to get the item is no longer valid and aborts the attempt.
I think those two ideas make sense at a high level. (I'm sure the nitty gritty is complicated) Also they don't deal with having to roll back state to a previous point in time. (I think) Any thoughts?
small tip: make the engine to fit a game, building an engine without a game is often a one way trip to a programmer wankfest that results in lots of code and cool tech but nothing usable
also interesting to see other approaches to the problem of games in general... this is quite high level stuff and the choice of tech has cut a lot of the implementation details out of the code (although imo still way to much in each file, but there we go) - quite different to what I normally consider 'game' or 'engine' programming - although obviously it is.
will fav and try to actually use when i get home from work. :)
In that case check out Haxe. It's a managed language along the lines of C# or Java (in Haxe's case, compiles down to Neko bytecode), which can then be compiled into C++, Javascript, C#, Java, and PHP. If you use the OpenFL/Lime framework, you can compile an entire app for Linux/Windows/Mac (either native or NekoVM), Android and iOS, HTML5 and Flash.
The language itself is great, it's object oriented with static typing, type inference, pattern matching, lambdas and macros (!!). It comes out of the flash game developer community, but has become a fantastic way to generate native games (via C++) on multiple platforms (3D options are limited right now, but on the way).
The compiler is pure magic. It does all sorts of strange and mysterious optimizations, I have no idea how it works but it seems to work very well.
Good tip, but don't forget that some people enjoy making engines and new technologies without having to make games.
The mention of Carmack is somewhat misplaced in that he decided languages like Lisp and Scheme that didn't offer the leverage of types like Haskell.
So perhaps languages like ML/Haskell, maybe even Rust, are more to the point?
As a user of Clojure and Haskell, no, not at all.
core.typed is unpleasant to use and not very sophisticated at all compared to Haskell.
Also doesn't do anything to separate pure and impure functions.
I'm willing to give up some type system sophistication in exchange for that, along with access to the JVM, macros, and so on. Carmack may not agree with that trade-off, but like I said, I mentioned him specifically for his endorsement of functional game programming.
Nice?