Pause/Resume execution in method

I’m making a programming game where the player can program their allies’ behavior. The player writes the body of the decide() function for a given ally, which can be filled out with any java code but has to return an action. I would like to give each ally a set, restricted amount of computation per tick so 1) adding more entities doesn’t slow down the game too much, and 2) The time an entity spends computing is reflected in game, so if an ally spends more time “thinking” it will act less often. My inspiration for how this should work is Battlecode, which gives units a set amount of bytecode per turn, then just pauses the computation and makes the programmer deal with noticing when things have changed after it’s resumed in the next round of computing.

My question is how I can pause and resume an entity which is executing the decision function in a thread. I understand the ‘proper’ way to do this is to set a flag telling the thread to pause and have it check occasionally, but since I can’t force the player to check for a flag within the decide() function, I’m not sure how to pause the thread. The entities are only looking at a fixed representation of the world and just have to return an enum value, so I don’t think they should have locks on anything, but I’m hoping there’s a better way to do this than using the deprecated thread pausing methods. I’m open to changing how the player has to write code, but I can’t think of a way to do it while still hiding the pause flag checks from the user without making writing the decision loop confusing and onerous. There must be some way to do this, since Battlecode does it, but I’m at a loss searching online for details as to how.

I think an easier way to do this would be a use a scripting language and have each ‘action’ consume some number of action points. You really don’t want to use threads for this kind of thing…you could use an actor model library, but I’m thinking massive overkill here.

http://www.matthiasmann.de/content/view/24/26/

Thanks Roquen and Riven, I’ll look into both of those.

I wasn’t planning on giving every entity its own thread, I just need a way to give each a restricted and roughly equal amount of computing per tick, and bytecode instrumentation seems a little too complicated if I can just use threads. I’ve used Akka some, so I imagine I can implement it in that but it seems like it’ll have a lot of overhead and add a level of complexity I’m not sure I need. It would be pretty easy to give action points for what an entity does, but what I’m worried about is it spending a long time computing and holding up the game for everyone else. I’ll give just doing a cost estimate with static analysis for each entity’s decision loop a try though.

If you use a (hopefully existing) interpreted scripting language, you can just enforce time constraints in the execution loop of the engine. I can’t think of an easier solution. I suppose an easy and potential native solution would be to have a watchdog thread which does something like calls Thread.interrupt() on the thread executing the user code if it takes too long.

Interesting. Are you talking about allowing users to write any Java code (including IO, reflection, and your game’s classes) or just a subset of the platform?

At the moment, yes, but eventually I’ll be putting in a security manager to minimize a players ability to cheat. I really want to give the player as much freedom as possible for the type of programs they can run, which is why I’m going to need a way to deal with the decision loop running for too long. But things might get a little nuts if access to reflection methods and game classes was allowed. Of course if I do wind up using a scripting language instead I won’t have to worry about that, but I kind of like when games let the players cheat and completely break the game if they are clever enough, so at the very least I’m going to put in a way to disable or reduce the security, maybe as an unlockable.

Sounds like you are a Microsoft programmer.