Guest User

Untitled

a guest
Apr 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.78 KB | None | 0 0
  1. Index: src/ConfigState.cpp
  2. ===================================================================
  3. --- src/ConfigState.cpp (revision 596)
  4. +++ src/ConfigState.cpp (working copy)
  5. @@ -161,7 +161,7 @@
  6. {
  7. /* New value will be applied by PlayState on resume */
  8. GlobalConfig::getSingletonPtr()->setReal(setting, value.getFloat());
  9. - TimeManager::getSingleton().setDayScale(value.getFloat());
  10. + //TimeManager::getSingleton().setDayScale(value.getFloat());
  11. }
  12. else if (!setting.compare("gravity"))
  13. {
  14. Index: src/PlayState.cpp
  15. ===================================================================
  16. --- src/PlayState.cpp (revision 596)
  17. +++ src/PlayState.cpp (working copy)
  18. @@ -55,7 +55,7 @@
  19. //Read config settings
  20. cfg_fogThickness = config->getReal("fogThickness", 0.000022f);
  21. TimeManager::getSingleton().setTimeFactor(config->getReal("timeFactor", 1.0f));
  22. - TimeManager::getSingleton().setDayScale(config->getReal("dayScale", 0.8f));
  23. + //TimeManager::getSingleton().setDayScale(config->getReal("dayScale", 0.8f));
  24. timeToCheckUpdating = 3000; //How many ms that have to pass before we update the array of entities that are in range of the player
  25. lastCheckUpdatingTime = timeToCheckUpdating + 2;
  26.  
  27. @@ -70,7 +70,7 @@
  28.  
  29. this->m_WorldTerrain = 0; //In the begginning, there was nothing
  30. isOutdoor = true;
  31. - TimeManager::getSingletonPtr()->setTimeOfDay( (LENGTH_OF_MONTH * 10) + LENGTH_OF_DAY / 14.0 );
  32. + //TimeManager::getSingletonPtr()->setTimeOfDay( (LENGTH_OF_MONTH * 10) + LENGTH_OF_DAY / 14.0 );
  33.  
  34. //Initialize noise generation
  35. ambientNoiseRandom = rand() % 300;
  36. @@ -564,12 +564,14 @@
  37.  
  38. if(e.key == OIS::KC_F5)
  39. {
  40. - TimeManager::getSingleton().modDayScale(2);
  41. + float timeFactor = TimeManager::getSingleton().getTimeFactor();
  42. + TimeManager::getSingleton().setTimeFactor(timeFactor * 2);
  43. }
  44.  
  45. if(e.key == OIS::KC_F6)
  46. {
  47. - TimeManager::getSingleton().modDayScale(-2);
  48. + float timeFactor = TimeManager::getSingleton().getTimeFactor();
  49. + TimeManager::getSingleton().setTimeFactor(timeFactor / 2);
  50. }
  51.  
  52. if(e.key == OIS::KC_F9)
  53. @@ -629,6 +631,7 @@
  54.  
  55. bool PlayState::frameStarted(const FrameEvent& evt)
  56. {
  57. + TimeManager::getSingleton().passTime(evt.timeSinceLastFrame * 1000);
  58. Real MoveFactor = 80.0 * evt.timeSinceLastFrame; //The time scaler to keep time constant
  59. timeScaler = MoveFactor * TimeManager::getSingleton().getTimeFactor();
  60.  
  61. @@ -644,13 +647,13 @@
  62. mCamera->pitch(Degree(-mouse_state.Y.rel * mouse_speed_multiplier));
  63. }*/
  64.  
  65. - float timeOfDay = TimeManager::getSingleton().update(MoveFactor);
  66. -
  67. if(isOutdoor)
  68. {
  69. if(m_sky != 0)
  70. {
  71. - m_sky->Update(m_Player->getPosition(),timeOfDay);
  72. + // HACK: the sun seems to be reversed
  73. + unsigned long t = TimeManager::getSingleton().getTime() / 1000;
  74. + m_sky->Update(m_Player->getPosition(), (t + 43200) % 86400);
  75. }
  76.  
  77. //Play ambient outdoor sounds here
  78. @@ -659,7 +662,7 @@
  79. {
  80. curAmbientNoiseWait = 0;
  81.  
  82. - int twentyfour_hour = TimeManager::getSingletonPtr()->get24FormatHour();
  83. + int twentyfour_hour = TimeManager::getSingletonPtr()->getHour();
  84. if( twentyfour_hour <= 6 || twentyfour_hour >= 18)
  85. {
  86. ambientNoiseRandom = rand() % 100;
  87. @@ -714,17 +717,11 @@
  88. }
  89.  
  90. //Do time of day callbacks here
  91. - if (TimeManager::getSingleton().isEvening())
  92. - {
  93. + if(TimeManager::getSingleton().getHour() >= 18 ||
  94. + TimeManager::getSingleton().getHour() <= 6)
  95. this->startEvening();
  96. - }
  97. else
  98. - {
  99. - if (TimeManager::getSingleton().isMorning())
  100. - {
  101. - this->startMorning();
  102. - }
  103. - }
  104. + this->startMorning();
  105.  
  106. // Update physics
  107. PhysicsManager::getSingletonPtr()->update();
  108. @@ -988,8 +985,7 @@
  109. sprintf(terrX, "%d", m_WorldTerrain->currentZoneX);
  110. sprintf(terrY, "%d", m_WorldTerrain->currentZoneY);
  111.  
  112. - String theDate;
  113. - TimeManager::getSingleton().getDateStr(theDate);
  114. + String theDate = TimeManager::getSingleton().getDateString("%j%S of %F, %h:%i:%s %A\n");
  115.  
  116. Location * thisLoc = GameWorld::getSingletonPtr()->getLocation(m_WorldTerrain->currentZoneX,m_WorldTerrain->currentZoneY);
  117. if(thisLoc != 0)
  118. @@ -1069,7 +1065,7 @@
  119. {
  120. // TODO: make music configurable, disable for now -- Archwyrm
  121. return;
  122. - int twentyfour_hour = TimeManager::getSingletonPtr()->get24FormatHour();
  123. + int twentyfour_hour = TimeManager::getSingletonPtr()->getHour();
  124.  
  125. if(twentyfour_hour < 6 || twentyfour_hour >= 18) //If it's nighttime
  126. {
  127. Index: src/TimeManager.cpp
  128. ===================================================================
  129. --- src/TimeManager.cpp (revision 596)
  130. +++ src/TimeManager.cpp (working copy)
  131. @@ -3,159 +3,157 @@
  132.  
  133. using namespace Ogre;
  134.  
  135. -TimeManager TimeManager::m_instance;
  136. +template<> TimeManager* Singleton<TimeManager>::ms_Singleton = 0;
  137.  
  138.  
  139. -TimeManager::TimeManager() : m_timeOfDay(0), m_lastHour(0), m_timeFactor(1.0), m_dayScale(0.8)
  140. -{
  141. - m_isMorning = false;
  142. - m_isEvening = false;
  143. - m_dayPos = 0;
  144. +TimeManager::month TimeManager::m_monthInfo[12] = {
  145. + { "Morning Star", 30 },
  146. + { "Sun's Dawn", 30 },
  147. + { "First Seed", 30 },
  148. + { "Rain's Hand", 30 },
  149. + { "Second Seed", 30 },
  150. + { "Mid Year", 30 },
  151. + { "Sun's Height", 30 },
  152. + { "Last Seed", 30 },
  153. + { "Heartfire", 30 },
  154. + { "Frost Fall", 30 },
  155. + { "Sun's Dusk", 30 },
  156. + { "Evening Star", 30 }
  157. +};
  158.  
  159. - //Setup months
  160. - gameMonths[0] = "Morning Star";
  161. - gameMonths[1] = "Sun's Dawn";
  162. - gameMonths[2] = "First Seed";
  163. - gameMonths[3] = "Rain's Hand";
  164. - gameMonths[4] = "Second Seed";
  165. - gameMonths[5] = "Mid Year";
  166. - gameMonths[6] = "Sun's Height";
  167. - gameMonths[7] = "Last Seed";
  168. - gameMonths[8] = "Heartfire";
  169. - gameMonths[9] = "Frost Fall";
  170. - gameMonths[10] = "Sun's Dusk";
  171. - gameMonths[11] = "Evening Star";
  172. -}
  173. -
  174. -
  175. -TimeManager::~TimeManager()
  176. +TimeManager::TimeManager()
  177. {
  178. + m_currentTime = 46800000; // default time of 1PM
  179. + m_currentSeconds = 0;
  180. + m_currentMinutes = 0;
  181. + m_currentHour = 0;
  182. + m_currentDay = 0;
  183. + m_currentMonth = 0;
  184. + m_currentYear = 0;
  185. + m_timeFactor = 1.0;
  186. }
  187.  
  188. -
  189. -void TimeManager::setTimeFactor(float factor)
  190. +unsigned long TimeManager::passTime(const unsigned short milliseconds)
  191. {
  192. - if (factor >= 0.0)
  193. - {
  194. - m_timeFactor = factor;
  195. - ControllerManager::getSingleton().setTimeFactor(m_timeFactor);
  196. - //ParticleSystemManager::getSingleton().setTimeFactor(m_timeFactor);
  197. - }
  198. -}
  199. + unsigned long timePassed = milliseconds * m_timeFactor;
  200. + m_currentTime += timePassed;
  201.  
  202. + unsigned long dayDuration = 86400000;
  203.  
  204. -double TimeManager::update(Real moveFactor)
  205. -{
  206. - //86400 time units in a day
  207. - //7200 in an hour
  208. - //120 in a minute
  209. - //2 in a second
  210. -
  211. - m_timeOfDay += m_timeFactor * moveFactor * m_dayScale;
  212. - m_timeOfDay = (int)m_timeOfDay % LENGTH_OF_DAY;
  213. -
  214. - m_isMorning = false;
  215. - m_isEvening = false;
  216. - int twentyfour_hour = get24FormatHour();
  217. - if (twentyfour_hour <= 6 || twentyfour_hour >= 18) //If it's nighttime
  218. + if(m_currentTime >= dayDuration)
  219. {
  220. - if (m_lastHour >= 6 || m_lastHour <= 18)
  221. + m_currentTime -= dayDuration;
  222. + m_currentDay++;
  223. +
  224. + if(m_currentDay >= m_monthInfo[m_currentMonth].days)
  225. {
  226. - if (m_dayPos != 1)
  227. + m_currentDay -= m_monthInfo[m_currentMonth].days;
  228. + m_currentMonth++;
  229. +
  230. + short numMonths = sizeof(m_monthInfo) / sizeof(month);
  231. + if(m_currentMonth >= numMonths)
  232. {
  233. - m_isEvening = true;
  234. - m_dayPos = 1;
  235. + m_currentMonth -= numMonths;
  236. + m_currentYear++;
  237. }
  238. }
  239. }
  240. - else
  241. - {
  242. - if (m_lastHour <= 6 || m_lastHour >= 18)
  243. - {
  244. - if (m_dayPos != 2)
  245. - {
  246. - m_isMorning = true;
  247. - m_dayPos = 2;
  248. - }
  249. - }
  250. - }
  251. - m_lastHour = twentyfour_hour;
  252.  
  253. - return m_timeOfDay;
  254. + unsigned long tmp = m_currentTime / 1000;
  255. + m_currentHour = tmp / 3600;
  256. + tmp %= 3600;
  257. + m_currentMinutes = tmp / 60;
  258. + tmp %= 60;
  259. + m_currentSeconds = tmp;
  260. +
  261. + return timePassed;
  262. }
  263.  
  264. -
  265. -long TimeManager::getYear()
  266. +String TimeManager::getDateString(const String& format) const
  267. {
  268. - return (long)(m_timeOfDay + (LENGTH_OF_DAY / 2)) / LENGTH_OF_YEAR;
  269. -};
  270. + String output = format;
  271. + size_t pos = output.find_first_of('%');
  272.  
  273. -int TimeManager::getMonth()
  274. -{
  275. - return ((long)(m_timeOfDay + (LENGTH_OF_DAY / 2))% LENGTH_OF_YEAR) / LENGTH_OF_MONTH;
  276. -};
  277. + while(pos != String::npos && pos < output.length() - 1)
  278. + {
  279. + char c = output.at(pos + 1);
  280. + std::ostringstream ss;
  281. + ss << std::setfill('0');
  282.  
  283. -int TimeManager::getDay()
  284. -{
  285. - return (((long)(m_timeOfDay + (LENGTH_OF_DAY / 2)) % LENGTH_OF_MONTH) / LENGTH_OF_DAY) + 1;
  286. -};
  287. + switch(c)
  288. + {
  289. + // Day formatting
  290. + case 'D': // don't know the names of the days of the week
  291. + case 'l': // ^
  292. + case 'd':
  293. + ss << std::setw(2) << getDay() + 1;
  294. + break;
  295. + case 'j':
  296. + ss << getDay() + 1;
  297. + break;
  298. + case 'S':
  299. + switch(getDay() + 1)
  300. + {
  301. + case 1: ss << "st"; break;
  302. + case 2: ss << "nd"; break;
  303. + case 3: ss << "rd"; break;
  304. + default: ss << "th";
  305. + }
  306. + break;
  307.  
  308. -int TimeManager::getHour()
  309. -{
  310. - int thisHour = ((long)(m_timeOfDay + (LENGTH_OF_DAY / 2)) % LENGTH_OF_DAY / LENGTH_OF_HOUR);
  311. + // Month formatting
  312. + case 'M': // we have no short month names
  313. + case 'F':
  314. + ss << m_monthInfo[getMonth()].name;
  315. + break;
  316. + case 'm':
  317. + ss << std::setw(2) << getMonth() + 1;
  318. + break;
  319. + case 'n':
  320. + ss << getMonth() + 1;
  321. + break;
  322.  
  323. - if(thisHour < 1)
  324. - {
  325. - thisHour = 12;
  326. - }
  327. - if(thisHour > 12)
  328. - {
  329. - thisHour -= 12;
  330. - }
  331. - return thisHour;
  332. -};
  333. + // Year formatting
  334. + case 'y': // 2-digit year but our years could be anything
  335. + case 'Y':
  336. + ss << getYear();
  337. + break;
  338.  
  339. -int TimeManager::get24FormatHour()
  340. -{
  341. - int thisHour = ((long)(m_timeOfDay + (LENGTH_OF_DAY / 2)) % LENGTH_OF_DAY / LENGTH_OF_HOUR);
  342. - return thisHour;
  343. -};
  344. + // Time formatting
  345. + case 'g':
  346. + ss << (getHour() % 12 == 0 ? 12 : getHour() % 12);
  347. + break;
  348. + case 'G':
  349. + ss << getHour();
  350. + break;
  351. + case 'h':
  352. + ss << std::setw(2)
  353. + << (getHour() % 12 == 0 ? 12 : getHour() % 12);
  354. + break;
  355. + case 'H':
  356. + ss << std::setw(2) << getHour();
  357. + break;
  358. + case 'i':
  359. + ss << std::setw(2) << getMinutes();
  360. + break;
  361. + case 's':
  362. + ss << std::setw(2) << getSeconds();
  363. + break;
  364. + case 'a':
  365. + ss << (getHour() < 12 ? "am" : "pm");
  366. + break;
  367. + case 'A':
  368. + ss << (getHour() < 12 ? "AM" : "PM");
  369. + break;
  370.  
  371. -int TimeManager::getMinute()
  372. -{
  373. - int thisHour = ((long)(m_timeOfDay + (LENGTH_OF_DAY / 2)) % LENGTH_OF_HOUR / LENGTH_OF_MINUTE);
  374. - return thisHour;
  375. -}
  376. + default: // add character as-is
  377. + ss << c;
  378. + }
  379.  
  380. -String TimeManager::getAMorPM()
  381. -{
  382. - int thisHour = ((long)(m_timeOfDay + (LENGTH_OF_DAY / 2)) % LENGTH_OF_DAY / LENGTH_OF_HOUR);
  383. - if(thisHour < 12)
  384. - {
  385. - return String("AM");
  386. + output.erase(pos, 2);
  387. + output.insert(pos, ss.str());
  388. + pos = output.find_first_of('%');
  389. }
  390. - return String("PM");
  391. -}
  392.  
  393. -void TimeManager::getDateStr(String& theDate)
  394. -{
  395. - char date[5];
  396. - char hour[5];
  397. - char minute[5];
  398. -
  399. - sprintf(date, "%d", TimeManager::getSingletonPtr()->getDay()); //%G for float, %d for int
  400. - sprintf(hour, "%d", TimeManager::getSingletonPtr()->getHour());
  401. - sprintf(minute, "%02d", TimeManager::getSingletonPtr()->getMinute());
  402. -
  403. - theDate += "Day ";
  404. - theDate += date;
  405. - theDate += " of ";
  406. - theDate += gameMonths[ getMonth() ];
  407. - theDate += ", ";
  408. - theDate += hour;
  409. - theDate += ":";
  410. - theDate += minute;
  411. - theDate += " ";
  412. - theDate += getAMorPM();
  413. - theDate += "\n";
  414. + return output;
  415. }
  416. Index: src/Main.cpp
  417. ===================================================================
  418. --- src/Main.cpp (revision 596)
  419. +++ src/Main.cpp (working copy)
  420. @@ -10,6 +10,8 @@
  421. #include "PlayState.h"
  422. #include "MainmenuState.h"
  423.  
  424. +#include "TimeManager.h"
  425. +
  426. int checkWorkingDirectory();
  427.  
  428.  
  429. @@ -22,6 +24,7 @@
  430. GameWorld* gameWorld = new GameWorld();
  431. PhysicsManager* physics = new PhysicsManager();
  432. SoundManager* soundManager = new OpenALManager();
  433. + TimeManager* timeManager = new TimeManager();
  434.  
  435. try
  436. {
  437. @@ -42,6 +45,7 @@
  438. return 1;
  439. }
  440.  
  441. + delete timeManager;
  442. delete soundManager;
  443. delete physics;
  444. delete gameWorld;
  445. Index: src/TimeManager.h
  446. ===================================================================
  447. --- src/TimeManager.h (revision 596)
  448. +++ src/TimeManager.h (working copy)
  449. @@ -1,67 +1,97 @@
  450. #ifndef _TIME_MANAGER_H
  451. #define _TIME_MANAGER_H
  452.  
  453. -#include <OgreString.h>
  454. -using namespace Ogre;
  455. +#include <OgreSingleton.h>
  456.  
  457. -#define SUNRISE 64800
  458. -#define SUNSET 21600
  459.  
  460. -#define LENGTH_OF_YEAR 31104000
  461. -#define LENGTH_OF_MONTH 2592000
  462. -#define LENGTH_OF_DAY 86400
  463. -#define LENGTH_OF_HOUR 3600
  464. -#define LENGTH_OF_MINUTE 60
  465. -#define LENGTH_OF_SECOND 1
  466. -
  467. -
  468. -/**
  469. - Time and Calendar functions
  470. -*/
  471. -class TimeManager
  472. +class TimeManager : public Ogre::Singleton<TimeManager>
  473. {
  474. public:
  475. - ~TimeManager();
  476. + TimeManager();
  477. + ~TimeManager() {}
  478.  
  479. - static TimeManager& getSingleton() { return m_instance; };
  480. - static TimeManager* getSingletonPtr() { return &m_instance; }
  481. + static TimeManager& getSingleton()
  482. + {
  483. + assert(ms_Singleton);
  484. + return *ms_Singleton;
  485. + }
  486. + static TimeManager* getSingletonPtr() { return ms_Singleton; }
  487.  
  488. - void setTimeOfDay(double val) { m_timeOfDay = val; }
  489. - double getTimeOfDay() { return m_timeOfDay; }
  490. - double update(Real moveFactor);
  491. + /**
  492. + * \brief Move forward in the day
  493. + * \param milliseconds Milliseconds to add to to the time of day
  494. + * \return The world time that actually passed (after multiplied
  495. + * by the time factor)
  496. + */
  497. + unsigned long passTime(unsigned short milliseconds);
  498.  
  499. - void setTimeFactor(float factor);
  500. - float getTimeFactor() { return m_timeFactor; }
  501. - void setDayScale(float scale) { if (scale > 0) m_dayScale = scale; }
  502. - void modDayScale(float scale) { if (m_dayScale + scale > 0) m_dayScale += scale; }
  503. - float getDayScale() { return m_dayScale; }
  504. + /**
  505. + * \brief Set the time factor to multiply time passed by
  506. + * \param factor Time factor
  507. + */
  508. + void setTimeFactor(Ogre::Real factor) { m_timeFactor = factor; }
  509. + Ogre::Real getTimeFactor() const { return m_timeFactor; }
  510.  
  511. - long getYear();
  512. - int getMonth();
  513. - int getDay();
  514. - int getHour();
  515. - int get24FormatHour();
  516. - int getMinute();
  517. - String getAMorPM();
  518. - void getDateStr(String& theDate);
  519. + /**
  520. + * \return Time of day in milliseconds
  521. + */
  522. + unsigned long getTime() const { return m_currentTime; }
  523.  
  524. - bool isMorning() { return m_isMorning; }
  525. - bool isEvening() { return m_isEvening; }
  526. + unsigned short getSeconds() const { return m_currentSeconds; }
  527. + unsigned short getMinutes() const { return m_currentMinutes; }
  528. + unsigned short getHour() const { return m_currentHour; }
  529. + unsigned short getDay() const { return m_currentDay; }
  530. + unsigned short getMonth() const { return m_currentMonth; }
  531. + unsigned short getYear() const { return m_currentYear; }
  532.  
  533. -protected:
  534. - TimeManager();
  535. - static TimeManager m_instance;
  536. + /**
  537. + * \brief Return the date and time as a formatted string
  538. + *
  539. + * This function creates a formatted string using embedded tags
  540. + * in the format string which are prefixed by '%'. It borrows its
  541. + * format characters from PHP's date() function, but only uses
  542. + * ones that are relavant to DungeonHack. Supported characters:
  543. + *
  544. + * d = day with leading zero
  545. + * j = day without leading zeros
  546. + * S = English ordinal suffix for day (st, nd, rd, th)
  547. + * F = full name of the month
  548. + * m = month with leading zero
  549. + * n = month without leading zero
  550. + * y = year
  551. + * g = hour, 12-hour format
  552. + * G = hour, 24-hour format
  553. + * h = hour, 12-hour format with leading zero
  554. + * H = hour, 24-hour format with leading zero
  555. + * i = minutes with leading zero
  556. + * s = seconds with leading zero
  557. + * a = am or pm
  558. + * A = AM or PM
  559. + *
  560. + * \param format Format string
  561. + * \return Formatted string
  562. + */
  563. + Ogre::String getDateString(const Ogre::String& format) const;
  564.  
  565. - double m_timeOfDay;
  566. - String gameMonths[12];
  567. +private:
  568. + unsigned long m_currentTime;
  569.  
  570. - int m_lastHour, m_dayPos;
  571. - bool m_isMorning;
  572. - bool m_isEvening;
  573. + // Keep track of these so we only have to calculate them once
  574. + unsigned short m_currentSeconds;
  575. + unsigned short m_currentMinutes;
  576. + unsigned short m_currentHour;
  577. + unsigned short m_currentDay;
  578. + unsigned short m_currentMonth;
  579. + unsigned short m_currentYear;
  580.  
  581. - float m_timeFactor; //Factor to scale time by, speeds up and slows down the game
  582. - float m_dayScale;
  583. + Ogre::Real m_timeFactor;
  584. +
  585. + struct month
  586. + {
  587. + char name[16];
  588. + unsigned char days;
  589. + };
  590. + static month m_monthInfo[12];
  591. };
  592.  
  593. -
  594. #endif // _TIME_MANAGER_H
Add Comment
Please, Sign In to add comment