Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Pause/Resume execution in method  (Read 2244 times)
0 Members and 1 Guest are viewing this topic.
Offline Wikk

Junior Newbie





« Posted 2012-08-24 04:46:00 »

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.
Online Roquen
« Reply #1 - Posted 2012-08-24 11:07:47 »

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.
Online Riven
« League of Dukes »

JGO Overlord


Medals: 744
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2012-08-24 11:11:38 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Wikk

Junior Newbie





« Reply #3 - Posted 2012-08-24 15:24:41 »

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.
Online Roquen
« Reply #4 - Posted 2012-08-24 17:03:44 »

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.
Offline Best Username Ever

Junior Member





« Reply #5 - Posted 2012-08-24 22:55:46 »

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?
Offline Wikk

Junior Newbie





« Reply #6 - Posted 2012-08-25 19:03:16 »

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.
Offline DrZoidberg

Senior Member


Medals: 15



« Reply #7 - Posted 2012-08-25 21:14:49 »

... 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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (11 views)
2014-07-30 12:09:19

Riven (8 views)
2014-07-30 12:08:52

Dwinin (9 views)
2014-07-30 04:59:34

E.R. Fleming (25 views)
2014-07-29 21:07:13

E.R. Fleming (10 views)
2014-07-29 21:06:25

pw (39 views)
2014-07-24 19:59:36

Riven (39 views)
2014-07-24 15:16:32

Riven (27 views)
2014-07-24 15:07:15

Riven (28 views)
2014-07-24 14:56:16

ctomni231 (59 views)
2014-07-19 00:55:21
HotSpot Options
by dleskov
2014-07-08 21:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 18:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 18:47:22

How do I start Java Game Development?
by ra4king
2014-05-18 05:13:37

HotSpot Options
by Roquen
2014-05-16 03:59:54

HotSpot Options
by Roquen
2014-05-07 09:03:10

Escape Analysis
by Roquen
2014-04-30 16:16:43

Experimental Toys
by Roquen
2014-04-29 07:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!