Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- TimeFactory.inc
- Autor: Daniel "Goldkiller", 2014
- Voraussetzung: CTime Plugin, RyDeR`
- */
- #include <CTime>
- enum {
- INGAME_TO_REAL,
- REAL_TO_INGAME
- }
- enum e_TimeFactory {
- TF_iRealDate, // Tick zu dem Script beginnt zu zählen
- TF_iIngameDate, // Tick bei dem die Zeit beginnt
- TF_iSpeed // Geschwindigkeit (zb 2x)
- }
- new g_TimeFactory[e_TimeFactory];
- /*
- Funktion zum Starten der Berechnung. Muss als erstes gemacht werden bevor
- alle anderen Funktionen genutzt werden können.
- realDate
- UnixTimestamp bei dem in der ECHTEN Welt die Zeit beginnt zu rechnen
- Sinnvoll wäre ein UnixTimestamp zB bei Server Eröffnung um ab da die Zeitrechnung
- laufen zu lassen
- ingameDate
- UnixTimestamp bei dem die Zeitrechnung Ingame beginnt.
- speed
- Integer um die Zeitrechnung zu beschleunigen (2 - 16)
- */
- stock TimeFactory(realdate = 0, ingamedate = 0 , speed = 4) {
- if(!realdate) {
- g_TimeFactory[TF_iRealDate] = gettime();
- }
- else {
- g_TimeFactory[TF_iRealDate] = realdate;
- }
- if(!ingamedate) {
- g_TimeFactory[TF_iIngameDate] = gettime();
- }
- else {
- g_TimeFactory[TF_iIngameDate] = ingamedate;
- }
- if( speed < 2 || speed > 16 ) {
- printf("TimeFactory: speed darf nur zwischen 2 und 16 liegen!");
- return 0;
- }
- else if( (speed % 2) != 0 ) {
- printf("TimeFactory: speed darf nur vielfaches von 2 sein (%d)!",speed);
- return 0;
- }
- else {
- g_TimeFactory[TF_iSpeed] = speed;
- }
- /*
- printf("g_TimeFactory[TF_iRealDate] = %d", g_TimeFactory[TF_iRealDate] );
- printf("g_TimeFactory[TF_iIngameDate] = %d", g_TimeFactory[TF_iIngameDate] );
- printf("g_TimeFactory[TF_iSpeed] = %d", g_TimeFactory[TF_iSpeed] );
- */
- return 1;
- }
- /*
- TimeFactory_RealDate
- Gibt den Unix Timestamp von Initialwert realDate an
- */
- stock TimeFactory_RealDate() {
- return g_TimeFactory[TF_iRealDate];
- }
- /*
- TimeFactory_IngameInitDate
- Gibt den Unix Timestamp von Initialwert ingameDate an
- */
- stock TimeFactory_IngameInitDate() {
- return g_TimeFactory[TF_iIngameDate];
- }
- /*
- TimeFactory_Speed
- Gibt den Zeitfaktor an, der IngameInitDate beschleunigt
- */
- stock TimeFactory_Speed() {
- return g_TimeFactory[TF_iSpeed];
- }
- /*
- TimeFactory_GetElapsedSec
- Zeit in Sec die Vergangen ist bis <jetzt>
- */
- stock TimeFactory_GetElapsedSec( since = 0 ) {
- new
- fake_sec;
- if(!since) {
- since = gettime();
- }
- fake_sec = ( since - TimeFactory_RealDate() ) * TimeFactory_Speed();
- return fake_sec;
- }
- /*
- TimeFactory_InGameUnix
- Gibt den UnixTimestamp der InGame aktuell ist.
- Ist immer größer/gleich dem IngameDate bzw "ingamedate" aus TimeFactory
- */
- stock TimeFactory_InGameUnix() {
- return ( TimeFactory_IngameInitDate() + TimeFactory_GetElapsedSec() );
- }
- /*
- tm:TimeFactory_InGameDate
- Gibt eine Variable zurück, die basierend auf TimeFactory_InGameUnix
- ein Array zurück gibt mit folgender Struktur:
- var {
- tm_sec, //sekunde
- tm_min, // Minute
- tm_hour, // Stunde
- tm_mday, // Monatstag
- tm_mon, // 0 Jan,1 Feb, 2 Mär, 3 Apr ...
- tm_year, // X - 1900
- tm_wday, // 0 SO,1 MO,2 TU,3 WE,4 TH,5 FR, 6 SA
- tm_yday, // Tag des Jahres
- tm_isdst // DayLightSaving
- }
- Mit dieser Struktur wird das Datum und Zeit genau beschrieben.
- */
- stock tm:TimeFactory_InGameDate() {
- new
- tm <tTime>;
- localtime( Time:TimeFactory_InGameUnix(), tTime);
- return tTime;
- }
- stock TimeFactory_PrintDate( tm<tTime>) {
- printf("tm_sec %d",tTime[tm_sec]);
- printf("tm_min %d",tTime[tm_min]);
- printf("tm_hour %d",tTime[tm_hour]);
- printf("tm_mday %d",tTime[tm_mday]);
- printf("tm_mon %d",tTime[tm_mon]);
- printf("tm_year %d",tTime[tm_year]);
- printf("tm_wday %d",tTime[tm_wday]);
- printf("tm_yday %d",tTime[tm_yday]);
- printf("tm_isdst %d",tTime[tm_isdst]);
- }
- /*
- Konvertiert ein Datum von:
- InGame zur Realen Zeiten
- Realen Zeite zu InGame
- */
- stock tm:TimeFactory_Convert(type,second,minute,hour,dayofmonth,month,year) {
- new
- tm <tTime>
- ;
- localtime(time(), tTime);
- if( type != INGAME_TO_REAL && type != REAL_TO_INGAME ) {
- printf("TimeFacoty_Convert: Falscher Wert für \"type\" (%d)",type);
- return tTime;
- }
- //
- tTime[tm_sec] = second;
- tTime[tm_min] = minute;
- tTime[tm_hour] = hour;
- tTime[tm_mday] = dayofmonth;
- tTime[tm_mon] = month - 1;
- tTime[tm_year] = year - 1900;
- //
- switch(type) {
- case INGAME_TO_REAL: {
- localtime( Time:( TimeFactory_RealDate() + ( mktime(tTime) - TimeFactory_RealDate() ) / TimeFactory_Speed()), tTime );
- }
- case REAL_TO_INGAME: {
- localtime( Time:( TimeFactory_IngameInitDate() + ( mktime(tTime) - TimeFactory_RealDate() ) * TimeFactory_Speed()), tTime );
- }
- }
- return tTime;
- }
- /*
- InGame_Second
- - Selbsterklärend
- */
- stock InGame_Second() {
- new
- sec,
- tm <tTime>
- ;
- tTime = TimeFactory_InGameDate();
- sec = tTime[tm_sec];
- return sec;
- }
- /*
- InGame_Hour
- - Selbsterklärend
- */
- stock InGame_Hour() {
- new
- hour,
- tm <tTime>
- ;
- tTime = TimeFactory_InGameDate();
- hour = tTime[tm_hour];
- return hour;
- }
- /*
- InGame_Minute
- - Selbsterklärend
- */
- stock InGame_Minute() {
- new
- minute,
- tm <tTime>
- ;
- tTime = TimeFactory_InGameDate();
- minute = tTime[tm_min];
- return minute;
- }
- /*
- InGame_DayOfMonth
- - Selbsterklärend
- */
- stock InGame_DayOfMonth() {
- new
- dayofmonth,
- tm <tTime>
- ;
- tTime = TimeFactory_InGameDate();
- dayofmonth = tTime[tm_mday];
- return dayofmonth;
- }
- /*
- InGame_Month
- - Selbsterklärend
- */
- stock InGame_Month() {
- new
- month,
- tm <tTime>
- ;
- tTime = TimeFactory_InGameDate();
- month = tTime[tm_mon];
- return month;
- }
- /*
- InGame_Year
- - Selbsterklärend
- */
- stock InGame_Year() {
- new
- year,
- tm <tTime>
- ;
- tTime = TimeFactory_InGameDate();
- year = tTime[tm_year] + 1900;
- return year;
- }
- /*
- InGame_WeekDay
- Wochentag
- 0 ist Montag
- ...
- 6 ist Sonntag
- */
- stock InGame_WeekDay() {
- new
- weekday,
- tm <tTime>
- ;
- tTime = TimeFactory_InGameDate();
- weekday = tTime[tm_wday] + 6 ;
- weekday %= 7;
- return weekday;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement