vegastrike  0.5.1.r1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stardate.cpp
Go to the documentation of this file.
1 #include <assert.h>
2 #include <iostream>
3 #include <vector>
4 #include <boost/shared_ptr.hpp>
5 #include "stardate.h"
6 #include "lin_time.h"
7 #include "vsfilesystem.h"
8 #include "cmd/script/mission.h"
9 
10 using std::cerr;
11 using std::cout;
12 using std::endl;
13 using std::vector;
14 
15 class Faction;
16 extern vector< boost::shared_ptr<Faction> >factions;
17 
19 {
20  initial_time = mission->getGametime();
21  initial_star_time = NULL;
22 }
23 
24 void StarDate::Init( double time )
25 {
26  if (initial_star_time != NULL)
27  delete[] initial_star_time;
28  initial_time = mission->getGametime();
29  initial_star_time = new double[factions.size()];
30  for (unsigned int i = 0; i < factions.size(); i++)
31  initial_star_time[i] = time;
32 }
33 
34 //Get the current StarDate time in seconds
36 {
37  //Get the number of seconds elapsed since the server start
38  double time_since_server_started = mission->getGametime()-initial_time;
39  //Add them to the current date
40  if (initial_star_time == NULL)
41  return time_since_server_started;
42  else
43  return initial_star_time[faction]+time_since_server_started;
44 }
45 
46 //Needed to calculate relative message and mission times
47 //into stardate
49 {
50  if (initial_star_time == NULL)
51  return initial_time;
52  else
53  return initial_star_time[faction] - initial_time;
54 }
55 
56 /*
57  *********************************************************************************
58  **** Trek Stardate stuff ***
59  *********************************************************************************
60  */
61 
62 void StarDate::InitTrek( string date )
63 {
64  if (initial_star_time != NULL)
65  //we must be reinitializing;
66  delete[] initial_star_time;
67  initial_star_time = 0;
68  initial_time = mission->getGametime();
69  initial_star_time = new double[factions.size()];
70  double init_time = this->ConvertTrekDate( date );
71  VSFileSystem::vs_dprintf( 3, "Initializing stardate from a Trek date for %d factions", factions.size() );
72  for (unsigned int i = 0; i < factions.size(); i++)
73  initial_star_time[i] = init_time;
74 }
75 
80 
81 //Convert a StarDate time into a Stardate string
82 string StarDate::ConvertFullTrekDate( double date )
83 {
84  unsigned int days, hours, minutes, seconds;
85  char cdate[32];
86 
87  // Get the number of days dividing seconds number by the number of seconds in a day: 100*60*60*8 = 2880000
88  days = (unsigned int) date/2880000;
89  // Modulo gives us the number of stardate seconds elapsed in the current day
90  date = (unsigned int) date%2880000;
91  // Get the hours elapsed in the day by dividing by number of seconds in a stardate hour: 60*60*8 = 28800
92  hours = (unsigned int) date/28800;
93  // Modulo gives us the number of seconds elapsed in that hour
94  date = (unsigned int) date%28800;
95  //Get the number of minutes elapsed in that hour by dividing by the number of seconds in a minute: 60*8 = 480
96  minutes = (unsigned int) date/480;
97  //The remaining seconds in the date
98  //The seconds are counted from 0 to 480 before the minute effectively is incremented
99  seconds = (unsigned int) date%480;
100 
101  //The hour/minute part is displayed like HHMM divided by HOURS_DIV which is 8 for now
102  unsigned int hhmm = (hours*100+minutes);
103 
104  sprintf( cdate, "%d.%.4d:%.3d", days, hhmm, seconds );
105  return string( cdate );
106 }
107 
108 string StarDate::ConvertTrekDate( double date )
109 {
110  unsigned int days, hours, minutes;
111  char cdate[32];
112 
113  days = (unsigned int) date/2880000;
114  date = (unsigned int) date%2880000;
115  hours = (unsigned int) date/28800;
116  date = (unsigned int) date%28800;
117  minutes = (unsigned int) date/480;
118 
119  unsigned int hhmm = (hours*100+minutes);
120 
121  sprintf( cdate, "%d.%.4d", days, hhmm );
122  return string( cdate );
123 }
124 
125 //Convert a StarDate into a number of seconds
126 double StarDate::ConvertTrekDate( string date )
127 {
128  unsigned int days, hours, minutes, tmphrs, seconds, nb, pos;
129  double res;
130  //Replace the dot with 'a' so sscanf won't take it for a decimal symbol
131  pos = date.find( "." );
132  date.replace( pos, 1, "a" );
133  if ( ( nb = sscanf( date.c_str(), "%da%4d:%3d", &days, &tmphrs, &seconds ) ) != 3 )
134  VSFileSystem::vs_dprintf( 3, "!!! ERROR reading date\n");
135 
136  //Extract number of hours
137  hours = tmphrs/100;
138  //Extract number of minutes
139  minutes = tmphrs%100;
140 
141  res = days*2880000+hours*28800+minutes*480+seconds;
142  std::string formatted = ConvertFullTrekDate(res);
143  VSFileSystem::vs_dprintf( 3, "Converted date to %ld, which stardate is %s\n", long(res), formatted.c_str() );
144  return res;
145 }
146 
147 //Get the current StarDate in a string
149 {
150  return ConvertFullTrekDate( this->GetCurrentStarTime( faction ) );
151 }
152 
153 //Get the current StarDate in a string - short format
155 {
156  return ConvertTrekDate( this->GetCurrentStarTime( faction ) );
157 }
158 
159 //Convert the string xxxx.y date format into a float representing the same data xxxx.y
160 float StarDate::GetFloatFromTrekDate( int faction )
161 {
162  float float_date;
163  string cur_date = this->GetFullTrekDate( faction );
164  sscanf( cur_date.c_str(), "%f", &float_date );
165 
166  return float_date;
167 }
168 
169 /*
170  *********************************************************************************
171  **** DAN.A Stardate stuff ***
172  *********************************************************************************
173  */
174 
175 void InitSDate( string date )
176 {}
177 
178 string GetSDate( int faction = 0 )
179 {
180  return string( "" );
181 }
182 
183 string GetFullSDate( int faction = 0 )
184 {
185  return string( "" );
186 }
187 
188 /*
189  *********************************************************************************
190  **** String stardate formats conversions ***
191  *********************************************************************************
192  */
193 
194 string SDateFromTrekDate( string trekdate )
195 {
196  return string( "" );
197 }
198 
199 string TrekDateFromSDate( string sdate )
200 {
201  return string( "" );
202 }
203