• Create Account

## Efficiently calculating increase in money over a period of time

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1BkoChan  Members

146
Like
0Likes
Like

Posted 06 May 2013 - 01:49 AM

I'm currently working on the server side logic of a real-time online browser game build using NodeJS. Currently the logic works like this...

1. Receive an event from a player (queue something for production)

2. Calculate when the next event will happen in the game (look at when the next production will complete or when the next research project will finish)

3. Set a timeout so that the server effectively sleeps until the next event happens (unless interrupted by a player sending another event)

This seems to work very well so far. When the server wakes up from the timeout it updates the game with the amount of time passed and all production and research etc. is updated by the ellapsed time.

The problem I'm having at the moment is that the amount of money generated for a player is based on how many workers they have. Worker numbers slowly increase over time until they reach a population cap. Given the following facts...

- The player has 1 worker

- The player has no money

- A new working is created every minute

- A worker produces 100 credits per minute

How many credits does the user have after an hour? I have no idea how to predict the number of credits produced as the number of workers increases over time

I hope that makes sense!

### #2Khatharr  Members

7667
Like
0Likes
Like

Posted 06 May 2013 - 03:45 AM

Oh hell, I can't remember the proper name for it, but it's done with sigma notation.

Edit:

http://en.wikipedia.org/wiki/Summation

The formula for a sequence starting at 1 is

s = n(n+1) / 2

For sequences starting at numbers higher than one it's:

s = (n(n+1) / 2) - (m(m+1) / 2)

Where m is one less than the starting point for the sequence and n is the ending point.

For instance:

1+2+3+4+5 = 15

1+2 = 3 +3 = 6 +4 = 10 +5 = 15

vs

5(5+1) / 2

5(6) / 2

30 / 2

15

or

3+4+5

3+4 = 7 +5 = 12

vs

5(5+1) / 2 = 15

2(2+1) / 2 = 3

15 - 3 = 12

Edited by Khatharr, 06 May 2013 - 04:12 AM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #3BkoChan  Members

146
Like
0Likes
Like

Posted 07 May 2013 - 04:28 AM

I'm not really seeing how to apply this to my problem in a more general manner. Your solution does provide the answer for 1 hour where the increase in workers is 1p/m. How to I alter this to handle 2,3,4 hours or a worker increase of 2 p/m?

### #4Álvaro  Members

20255
Like
0Likes
Like

Posted 07 May 2013 - 05:22 AM

What are all the possible parameter settings in the problem?

### #5BkoChan  Members

146
Like
0Likes
Like

Posted 07 May 2013 - 05:39 AM

The player has a known number of workers (eg. 10, 20, 123)

Each worker will produce 100 credits per minute

A new worker is generated periodically (eg. every 1 minute, every 1.5 minutes, every 30 minutes) based on other variables

A period of time passed (eg. 1 minute, 4 minutes, 2034 minutes)

How to I calculate how many credits have been produced in the time period?

example:

The player has 20 workers

The worker spawn rate is 1 worker every 2 minutes

20 minutes have passed since the last update

How many credits have been generated in this time frame

### #6Khatharr  Members

7667
Like
0Likes
Like

Posted 07 May 2013 - 05:44 AM

This is untested, but at first glance it would be:

m = starting_population
n = m + cycles_elapsed
worker_cycles = summation(m, n) * workers_spawned_per_cycle
produced = worker_cycles * production_per_worker_cycle
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #7Álvaro  Members

20255
Like
0Likes
Like

Posted 07 May 2013 - 07:00 AM

example:

The player has 20 workers

The worker spawn rate is 1 worker every 2 minutes

20 minutes have passed since the last update

How many credits have been generated in this time frame

You need to be much more precise than that. For instance, does a worker produce 100 credits the minute that it is spawned? Or is it one minute after? In your example, when is the next worker going to be produced? Right away, or in one minute, or in two minutes?

I suggest you write reference code that iterates over minutes (or whatever other time unit you want) and computes things in a naive way. You can then try to optimize that code by using summation formulas. But you have to know what it is you are trying to compute.

### #8frob  Moderators

41261
Like
0Likes
Like

Posted 07 May 2013 - 10:20 AM

Given the following facts...

- The player has 1 worker
- The player has no money
- A new working is created every minute
- A worker produces 100 credits per minute

How many credits does the user have after an hour?

The complex code isn't necessary.  Unless you have some serious problems, you could do something along these lines:

if(timeElapsed > kMaxElapsedTime)
{
ShowMessage( Messages::TooMuchTimeElapsed );
timeElapsed = kMaxTimeElapsed;
}
for(int i=0; i<timeElapsed; i++)
{
SimulateOneTimeUnit();
}



If you need more, create two simulators.  One is an online simulator, the other an offline simulator.

Edited by frob, 07 May 2013 - 10:21 AM.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.