time based game functions

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
Generic
Posts: 1
Joined: Mon Nov 07, 2011 3:20 pm

time based game functions

Post by Generic »

Hello,

I am currently watching the how to make an MMO videos and i am trying to add a feature that is not covered.

Basically, I want players to have energy that recovers slowly over time even when they are not logged in at a fix rate.
Or if I was making a strategy game, a feature that would increase player resources constantly or buildings with build times.

Can someone please explain to me how I would do this or help my get started in the right direction?

Thanks
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: time based game functions

Post by Jackolantern »

Halls has a video about "crons", which allow you to run PHP scripts in Linux at certain time intervals. However, for the first use (players recover energy slowly over time while logged out), you could just calculate how long they have been logged out when they log back in, and give them the energy then.
The indelible lord of tl;dr
dbest
Posts: 109
Joined: Sun Nov 20, 2011 12:24 pm

Re: time based game functions

Post by dbest »

Generic wrote:Hello,

Or if I was making a strategy game, a feature that would increase player resources constantly or buildings with build times.

Can someone please explain to me how I would do this or help my get started in the right direction?
Store the time taken to build each type of building in a table (call it building_master).
Store the time the build action was set by the player in a table (call it building_action)
When the player makes a query for the status of the building (this could be direct or indirect),
  • check the current time;
  • obtain action initiated time from the building action table;
  • obtain elapsed time by subtracting current time from action initiated time;
  • obtain time required to complete the building from the building_master table;
  • provide the status to the user.
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: time based game functions

Post by Xaleph »

Crons in case you have players who are logged out, other then that, timestamps will do just fine. It requires a bit of time validation but it`s prolly worth more then running multiple crons per user. ( remember every cron is a thread )

You need some timestamps, some "currency" like the rate of health grow per time unit ( seconds, minutes, hours ) and check that against the last updated time. If it`s time to update the health, update the health field, update the last-updated-time to now and watch the magic do the work.
dbest
Posts: 109
Joined: Sun Nov 20, 2011 12:24 pm

Re: time based game functions

Post by dbest »

Xaleph wrote:Crons in case you have players who are logged out
could you explain what situations would this be needed in?
If there is a situation, wherein you might want calculate the outcome of an event that takes place on the data of an off-line player, I think you could still do it without cron jobs, but would be interested in your idea..
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: time based game functions

Post by Jackolantern »

It basically depends on what kind of interaction it involves. If the energy level of a logged-out player matters to other players who are logged-in (for example, in a strategy game if a logged-out player's energy defends them from attacks from logged-in players), you would need to go crons because the other way, which involves timestamps, is basically an illusion and the logged-out player's energy won't actually go up while they are gone. That is, unless you wanted to get really tricky and cause an attack on a logged out player to check the timestamp on that logged-out player, and figure their energy then.

Really, timestamps can stand in place for almost anything a cron could because if a logged-in user does not do some action, how would they ever know that it was not building up constantly over time? Basically, whatever action a logged-in player takes on a logged-out player, you can run any of the "replenish over time" effects for that logged-out player and immediately use those effects.

It may be lack of sleep, but I am having a hard time putting this into a good wording. I hope you get what I am so ineloquently trying to say lol.
The indelible lord of tl;dr
dbest
Posts: 109
Joined: Sun Nov 20, 2011 12:24 pm

Re: time based game functions

Post by dbest »

Well, that's exactly what I meant, but you typed it much better than I did (even with your lack of sleep excuse) ;)
Crons consume too much of server resources. Only update what is needed, when it is needed.

If we assume we just use cron jobs, how would that work in the scenario that you proposed (for example, in a strategy game if a logged-out player's energy defends them from attacks from logged-in players)... Wouldn't the cron job have to run almost on a continuous basis..or is just run every hour?

I guess, it would depend slightly on the type of gameplay you have in place..
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: time based game functions

Post by Chris »

I don't think cron jobs would be the best thing to use in this case as players consume energy at different times meaning players will only be active shortly before their energy gets reset and shortly after. I find, and this is more of a personal opinion, but too keep players more active, you might want to try to give players an x amount of energy after an x amount of time since their last energy consumption.

How this would work is you could either do loop based game-play and run a loop on the server to constantly update players energy rather than cron as cron would not be able to run as often as a simple, dare I say it, infinite loop. Or you could make comparisons between a saved timestamp of the last time a user consumed energy, this script will have to be made in one way or another so here's a little code to hopefully help you build more of an image in your head:

Code: Select all

$refill_time_dif = 120; // time difference 2 minutes (in seconds) = how often energy can be refilled
$timestamp = time(); // current timestamp
// if player energy is less than their max energy
// and the last time they used or gained energy plus the time difference is less than or equal to the current time
if( $player['energy'] < $player['max_energy'] && $player['energy_last_update'] + $refill_time_dif <= $timestamp   )
{
    $energy_refill = 1; // how much energy per 2 minutes?

    // calculate and floor how much energy they are owed by the amount of time since their last refill/use
    $total_refill = floor( $energy_refill * ( ( $timestamp - $player['energy_last_update'] ) / $refill_time_dif ) );

    // update table
    mysql_query("UPDATE `players` SET `energy` = `energy`+$total_refill AND `energy_last_update` = '$timestamp' WHERE `id` = '{$player['id']}'");
} 
Fighting for peace is declaring war on war. If you want peace be peaceful.
Post Reply

Return to “Beginner Help and Support”