Need help with algorithm!

G-Blunted

Planter
Joined
Dec 15, 2007
Messages
30
Location
Portland, Oregon. USA
Anyone have any ideas what so ever as to how i would convert an ingame time format like:

Thu 15th Jul, year 1. Morning


in to a real world date/time format?? I'm coding in PHP, so if anyone has any ideas...please reply to this thread! Thanks! :)
 

Bobbin

Head Gardener
Joined
Dec 14, 2007
Messages
476
Location
Bracknell, Berkshire, England
Re: Need help with algorithm!

Best bet.. Store the date into DB... Then you can run a few Queries on the DB. (Plus, i'm better at doing this sort of thing in DB :p)

Code:
<?PHP
$sQuery = 'UPDATE tblDate SET IngameDate = REPLACE(IngameDate,\',%\',\'\');

update tblDate set IngameDate=date(substr(IngameDate,locate(IngameDate,\' \',-1)+1,length(IngameDate)) ||substr(IngameDate,locate(IngameDate,\' \'),locate(IngameDate,\' \',-1)-locate(IngameDate,\' \')+1) ||substr(IngameDate,1,locate(IngameDate,\' \')-1)) where IngameDate like \'% %\'';

$sResult = MySQL_Query($sQuery)
	OR DIE('Query Did Not Execute');


?>

Should return the date in yyyy-mm-dd Format.

You're going to need to check the syntax of that as i wrote it up pretty damned fast though, Should be sound(ish)

To get the time...

Just use the first part of the query but reverse it, to remove everything before the ,... Then make up an IF statement that sets the time to something you want, depending on what the word is (I.E. You could have Early Morning as 1, and Morning as 2 etc.). You can do all this in the SQL statement, and then just pull the info from the DB as and when you want it.
 

G-Blunted

Planter
Joined
Dec 15, 2007
Messages
30
Location
Portland, Oregon. USA
Re: Need help with algorithm!

I'm not really using a database at this point. That code doesn't really make sense to me at first glance not sure that's what im lookin for. That doesn't return real world date/time does it?

Also the round started on:
Sun Mar 1st, year 1. Morning.

right?


And is there the same amount of days in each month (bush time)? Or does it vary 28 to 31 days?

Anyone got a guess as to how that ingame date/year is ticked? Like what the algo for that would be, maybe it's easier to figure that out and reverse engineer it.

I can't believe how hard this conversion is :mad:

I mean it starts Sun Mar 1st, year 1. Morning.

How can i count backwards to that?
 

Bobbin

Head Gardener
Joined
Dec 14, 2007
Messages
476
Location
Bracknell, Berkshire, England
Re: Need help with algorithm!

Hmm..

Sorry, i thought you wanted it to return a usable date format. to return real world time....
You'd need to take the current IngameDate, and work out how many ticks have passed since the date you want to work out the time of... So... Hmmm

Let me think on it... And find some more info on PHP string operations...
 

G-Blunted

Planter
Joined
Dec 15, 2007
Messages
30
Location
Portland, Oregon. USA
Re: Need help with algorithm!

Yea i'm trying to convert the ingame date and time in to the corresponding real world date/time. That'd be cool if you could provide code, but really i'm just trying to get a grasp on the algorithm i'd use. That is what i was thinking though, that assuming the round started on: [Sun Mar 1st, year 1. Morning.], there should be some way to count backwards to that perhaps?

Also things to note:
  • I don't think the day of the week (Sun,Mon,Tue,etc) matters in this calculation at all (maybe i'm wrong?).[/*:m:2zxpxuu9]
  • The actual time of day (morning, dawn, evening, midnight, etc) will end up corresponding to the 1/6th of the current hour. Like it will tell you what :10min increment in the hour you're in.[/*:m:2zxpxuu9]
  • If there were 24 days in each month, this would be a really easy problem to figure out. But there isn't is there? There can be 28 to 31 days in each month right? <---i'm thinking this line should really help some how (the fact that if there was 24 days it would be much easier).[/*:m:2zxpxuu9]
  • Year 4 doesn't have any leap years or anything crazy right?[/*:m:2zxpxuu9]

Thats kind of what i've been thinking. I think if i know exactly how many days are in each of the 12 bushtarion months than i would be able to count back pretty easy right? Maybe? Think that makes sense?
 

tobapopalos

Hydroponics Developer
Joined
Dec 14, 2007
Messages
2,759
Location
Manchester
Re: Need help with algorithm!

G-Blunted said:
  • Year 4 doesn't have any leap years or anything crazy right?[/*:m:2ybc9mzq]

I think every four years February does have 29 days in-game.
 

Bobbin

Head Gardener
Joined
Dec 14, 2007
Messages
476
Location
Bracknell, Berkshire, England
Re: Need help with algorithm!

G-Blunted said:
Azzer: Why would you add that? And can you add any insight in to reverse engineering the ingame time stamp? Any?

There are as many days in a bushtarion money... as there are in the corresponding real month.
 

G-Blunted

Planter
Joined
Dec 15, 2007
Messages
30
Location
Portland, Oregon. USA
Re: Need help with algorithm!

Alright, so after a full night of coding with no sleep...here is what I've come up with. The lack of sleep is why i have all the comments, i had to constantly remind myself what the hell i was doing so i just started to type it out next to the lines. I ended up commenting almost ever line i noticed! :p

Also this is the rough alpha version of this object. Currently accepts only the full in-game date/time like so:

Tue 17th Aug, year 2. Noon

The function will only accept strings in that format. Currently you can use any valid Bushtarion date (including year # and time of day) as valid input and it will return the corresponding real-world unix time stamp, which can then be used/formated however you like; For example:

This game time here: Tue 17th Aug, year 1. Noon
...happened in real time @: Friday 21st of December 2007 19:10:00PM GMT


Note: the times of day right now only work properly and correspond correctly to the times of day in the Summer Season. This is because i have no idea what the exact phrases are for the rest of the year :(

Also there is little to no error handling, so if something doesn't look right, chances are it's because the date you entered from the game was not in the proper format like shown above.

Uses for this? Well my original intent was to use this with news, spy reports, battle reports, anything with a date, inorder to take out that date and replace it with how many hrs/min/ticks ago that current event took place. I don't know, that's the only use i've thought, that's why i made it, i hope to make use of this function in all bush tools i code :)

Please, tell me what you think! Any feed back, suggestions, ideas, you think this is useless, or you may want to use this yourself......Let me know! :x-mas:

PS. Try it out for yourself here: http://www.gbleezy.info/date_convert.php But that is my development space, so it may not be working when you try. Just try later maybe. Also full source is here: http://www.gbleezy.info/source?a=date_convert.php


Code:
function prsInGameTime($strTimeLine) {
	
	/**************
	 int prsInGameTime(string $strTimeLine)
	 
	 Takes a full string like this:
	 
	 Wed 4th Aug, year 1. Afternoon
	 
	 $strTimeLine must be a string just like above, from the game.
	 This function will return the real world timestamp that corresponds to the in-game time line provided.
	 
	 So what's the logic behind all this magic? 	 
	 First calculate the unix timestamp for the first tick in "game time". [March 1st, 2007. 18:00:00 GMT]
	 Next, simplify the user's inputed time line [Thu 12th Aug, year 1. Evening] into something timestampable. [Aug 12th, 2007. 18:00:00 GMT]
	 Now subtract the two and you have the amount of seconds between the user's game date, and the first game date (Mar 1st).
	 Convert those seconds in to days. Now you know how many days have passed in the game from [March 1st, year 1] to the user's game date [Aug 12th, year 1].
	 Now you can safely convert the amount of game days that have passed, to the amount of hours that have passed.
	 1day(in-game) = 1hr(real-time)
	 Now you know how much (real) time has passed between first tick and user's tick.
	 
	 Now calculate the unix timestamp for the first tick in "real time". [December 14th, 2007. 18:00:00 GMT]
	 Now if you subtract the current GMT time [time()] from the first tick (real time), you'll find out how many seconds have passed from the first tick to the current time (or last tick).
	 Now if you take the seconds from first tick to the current time, and subtract the amount of seconds from first tick to the user's time, that will tell you how many seconds ago the user's tick passed.
	 Then subtract the amount of seconds ago the user's tick passed from the current GMT timestamp.
	 And that, my friends, is the integer that is returned by this function.
	 
	 The function will convert an  internal bushtarion date in to it's real time unix timestamp.
	 ***************/
	 
	 $strTimeLine = trim($strTimeLine);
	 $strFirstTickg = "March 1st, 2007";
	 $strFirstTickr = "December 14th, 2007 18:00:00 GMT";
	 $intCurrTime = time();  //Current time in the UK
	 
	 //First tick, in GAME time, represented as unix timestamp. 
	 $intFirstTickg = strtotime($strFirstTickg); 
	 //First tick, in REAL time, represented as unix timestamp. 
	 $intFirstTickr = strtotime($strFirstTickr);
	 	

	
	$x = explode(" ",$strTimeLine);
	
	$arrTimeLine = array(
		"day" => $x[1],
		"month" => substr($x[2],0,-1),
		"year" => (int)substr($x[4],0,-1),
		"time" => $x[5]	
	);

	if($x[6]) { $arrTimeLine['time'] = $arrTimeLine['time']." ".$x[6]; } // For times of day like: "Early Morning" and "Late Evening". 
	

	switch($arrTimeLine['year']) {
		case 1:
			$arrTimeLine['year'] = 2007;
			break;
		case 2:
			$arrTimeLine['year'] = 2008;
			break;
		case 3:
			$arrTimeLine['year'] = 2009;
			break;
		case 4:
			$arrTimeLine['year'] = 2010;
			break;		
	};
	
	//Turn "Sat 7th Aug, year 1. Morning" in to "Aug 7th 2007"
	$strNewTime = $arrTimeLine['month']." ".$arrTimeLine['day']." ".$arrTimeLine['year'];
	
	//Convert user provided game time, in to a timestamp to compare with first tick's timestamp ($intFirstTickg).
	$intNewTime = strtotime($strNewTime);

	

	// Parse time of day here, adding 10min increments for the appropriate times of day.
	
	/*********************************************
	
		KEY NOTE: 10min of real time is 4 hours of game time!!
		
	**********************************************/
	

	if(strstr($arrTimeLine['time'],"Morning")) {
		//This is the tick the game started on. Meaning these ticks represent the top of the hour!
	}
	if(strstr($arrTimeLine['time'],"Noon")) {
		$intNewTime = strtotime('+4 hours',$intNewTime);
	}
	if(strstr($arrTimeLine['time'],"Afternoon")) {
		$intNewTime = strtotime('+8 hours',$intNewTime);
	}
	if(strstr($arrTimeLine['time'],"Evening")) {
		$intNewTime = strtotime('+12 hours',$intNewTime);
	}
	if(strstr($arrTimeLine['time'],"Midnight")) {
		$intNewTime = strtotime('+16 hours',$intNewTime);
	}	
	if(strstr($arrTimeLine['time'],"Dawn")) {
		$intNewTime = strtotime('+20 hours',$intNewTime);
	}

	//How many (in-game)seconds between the first tick (game time) and the user provided tick?
	$intUsrSecsSinceFirstTick = $intNewTime-$intFirstTickg; 
	
	 //Take the seconds between first tick and user time, and convert those to represent days in the game. Which will double as real-world hours between first tick and the user provided time.
	$intUsrHrsSinceFirstTickr = ((($intUsrSecsSinceFirstTick/60)/60)/24);
	
	//Convert the real world hours between first tick and the user provided time ($intUsrHrsSinceFirstTickr), in to real world seconds.
	$intUsrSecsSinceFirstTickr = $intUsrHrsSinceFirstTickr * 60 * 60; 	
	
	//How many seconds between the first tick(real time) and right now?
	$intCurrSecsSinceFirstTickr = $intCurrTime-$intFirstTickr; 

	$intSecsDiff = $intCurrSecsSinceFirstTickr - $intUsrSecsSinceFirstTickr; //How many seconds ago was the user time?
	$intDaTStamp = $intCurrTime - $intSecsDiff;
	
  if(DEBUG) {
		echo "

This game time here: ".$strTimeLine;
		echo "
...happened in real time @: ".date('l dS \of F Y G:i:sA e',$intDaTStamp);
		exit;
	}
	
	return ($intDaTStamp);
}
 

Alcibiades

Plant Geneticist
Joined
Dec 13, 2007
Messages
4,267
Location
Canada
Re: Need help with algorithm!

BlackWolf said:
Game missed one tick due hickup not too long ago...
that fixed? :p

oh BlackWolf but you are a mean SOB. here he is, racking his brains for days, and is so proud of his accomplishment, and here you go, destroying his house of cards. I applaud you sir, that's the best thing i've ever witnessed you do in Bushtarion, either ingame or out of it ;)
 

Azzer

Administrator
Staff member
Administrator
Joined
Dec 13, 2007
Messages
1,215
Re: Need help with algorithm!

The game does store actual real-time timestamps against anything "stored" - as in a news entry... so I could make this display as a tooltip when hovering over the in-game dates in a news/spy report (with a "how long ago this happened" possibly, too?).

But regardless, kudos for your efforts so far - that is not an easy thing to code by any means, and as Blackwolf just kindly demonstrated, has some real issues when a tick skips or anything like that! Difficult stuff! I wonder if storing data from the dumpdata every 12 ticks would help "stabilise errors"?
bushtarion.com/dumpdata1.txt
Has these lines which might help;
-- dump started at 'Sat 22nd Dec, 2007. 13:10:15 GMT'.
.... and ...
-- w,world#,current-tick,final-tick,game-year,game-month,game-day,game-time,weather-id,weather-description,description
w,1,1123,10944,1,9,4,4,6,Slightly cloudy,

^^ *points at the 3rd column along "current-tick".
 

DarkSider

Tree Surgeon
Joined
Dec 14, 2007
Messages
796
Re: Need help with algorithm!

Well there is alot of writting in your code and i might say exactly the same thing because i couldn't read it all :p
In the spy parsers i had in Bushtools i did something similar and the ideea is pretty much:

For ingame date 4 Apr Afternoon year 3 i know that since start have passed 3 days + 1 month(of 31 days) + dunno how many ticks for afternoon + 2 years. Convert all this into ticks and you have the total ticks since start. And then for the unix time of the round start converted in seconds add the ticks x 10 and convert back into time format.
 
Top