Feedback on simulation approach.

Ladies and Gentlespoons,hobos and tramps,
Cross-eyed mosquitoes,and bow-legged ants,
I stand before you to sit behind you
To tell you something I know nothing about.

Love that poem. Anyhow, I’m a little unsure of how to best solve the following problem and I’m welcoming all feedback from anyone who reads this post.

I have a daughter in 9th grade whose economics project / science fair project is due in February and she has decided to do a little ‘tycoon’ simulation. I have to code it of course. So, here’s what I agreed to do for. Allow her and her classmates to run a simulation for ten to thirty days depending on when I can finish. Each ‘player’ will own a truck that they can ship freight with to earn an income. The only cost is fuel, but it takes time to travel from one city to the next. The truck is operated by a driver who never sleeps, so it can run for 24hrs straight. That’s the bulk of it and is just that simple.

Now for the feedback as I’m still prototyping this in my mind. I’ve been developing Java since day 1.1 and have learned that just because this is meant for 25 or so people, it won’t always be that way. I say that because issuing and tracking a few TimerTasks are fine, but would cause me a few headaches if I wanted to scale this. So, I’m thinking that I should just monitor a timestamp column for various records in the database.

  1. Send freight from Jacksonville, FL to Nashville, TN
  2. Calculated time is eight hours
  3. Add timestamp to player_action table
  4. When details related to player are queried, check the timestamp.
    a. If shipping time has passed … drop freight … collect pay … top off fuel
    b. Calculate remaining time for display purposes

I played around with some MUD programming in my youth and wrote a sidescroller or two, and even implemented a few model simulations as course work in college, but I always used Threads or Timers because nothing was multiplayer centric. Can someone suggest an alternate approach or a better way to implement what I described? Thanks in advance for your suggestions.

One more thing. This is text based or I’ll implement something via the web for the players to choose which jobs they want to perform and it’s at best a ‘piece of a game’.

You don’t need a database. And time also only exists in its abstract form. That means you don’t have to deal with any real timing at all. As far as time is concerned there are only a bunch of values where the state changes. That is… if a driver reaches a goal.

The drivers are at the start position and the one who has the shortest distance to travel dictates when the next state change will be. And so on. Whenever a state change happens you capture the state of the simulation.

Thats all there is to it afaict.

The need is to do this ‘real time’. Take OGame, which everyone has played ;). You ship some resources from one planet to the next and that action takes two hours to complete (hour there and back). You also have the option to cancel that action within the first hour.

What I’m getting at is that the action doesn’t complete until some predetermined time in the future and the outcome of that action may be dependent upon another action issued by another player. I’m looking for a way to implement this without issuing a bunch of threads or timers, simply for the sake of saving resources and the ability to recover should my box lose power.

If this still doesn’t make sense, maybe someone can describe how OGame might be implementing what I described. This is actually where my daughter got the idea and wanted me to build her an ‘OGame’. I don’t have the time for that and I compromised with shipping freight from city to city. Again, I apologize for the lack of RTS and Strategy game programming. I may just do this turn based and give x number of turns a day, but that removes some of the fun and does not solve my curiosity on how to implement this behavior.

Ahh. I just read this thread How does it work in a MMORPG? and it is important to note that the player might not be logged on when it’s time for the action to complete. Hope that helps.

So, I learned that TimerTasks are run on the same thread as the Timer that executes it. My test was flawed in that I created a timer for each TimerTask rather than using the ‘one’ timer.

Dave Brackeen was kind enough to point this out to me. Many thanks.