1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-15 06:52:34 +02:00

Merge pull request #190 from tsjost/fix/timecheat

Add method to modify game time properly
This commit is contained in:
Daniel Evans 2016-08-23 00:28:54 +01:00 committed by GitHub
commit 3a53089d68
4 changed files with 81 additions and 2 deletions

View File

@ -678,6 +678,26 @@ int GameWorld::getMinute()
return state->basic.gameMinute;
}
void GameWorld::offsetGameTime(int minutes)
{
int gameMinute = state->basic.gameMinute;
int gameHour = state->basic.gameHour;
gameMinute += minutes;
bool backwards = gameMinute < 0;
gameHour += gameMinute / 60 - (backwards ? 1 : 0);
// Black mathgic? No. If the value is negative we want to wrap it around
// to the other side, e.g. minute -7 should be 53.
// The equivalent would be doing "while (gameMinute < 0) { gameMinute += 60 }"
gameMinute = (gameMinute % 60 + 60) % 60;
gameHour = (gameHour % 24 + 24) % 24;
state->basic.gameMinute = gameMinute;
state->basic.gameHour = gameHour;
}
glm::vec3 GameWorld::getGroundAtPosition(const glm::vec3 &pos) const
{
btVector3 rayFrom(pos.x, pos.y, 100.f);

View File

@ -165,6 +165,12 @@ public:
*/
int getMinute();
/**
* Modifies the game time and handles the circular nature of clock numbers
* Supports negative numbers
*/
void offsetGameTime(int minutes);
glm::vec3 getGroundAtPosition(const glm::vec3& pos) const;
float getGameTime() const;

View File

@ -777,10 +777,10 @@ void RWGame::globalKeyEvent(const SDL_Event& event)
{
switch (event.key.keysym.sym) {
case SDLK_LEFTBRACKET:
state->basic.gameMinute -= 30.f;
world->offsetGameTime(-30);
break;
case SDLK_RIGHTBRACKET:
state->basic.gameMinute += 30.f;
world->offsetGameTime(30);
break;
case SDLK_9:
timescale *= 0.5f;

View File

@ -16,6 +16,59 @@ BOOST_AUTO_TEST_CASE(test_gameobject_id)
BOOST_CHECK_NE( object1->getGameObjectID(), object2->getGameObjectID() );
}
BOOST_AUTO_TEST_CASE(test_offsetgametime)
{
GameWorld gw(&Global::get().log, &Global::get().work, Global::get().d);
gw.state = new GameState();
BOOST_CHECK_EQUAL(0, gw.getHour());
BOOST_CHECK_EQUAL(0, gw.getMinute());
gw.offsetGameTime(30);
BOOST_CHECK_EQUAL(0, gw.getHour());
BOOST_CHECK_EQUAL(30, gw.getMinute());
gw.offsetGameTime(30);
BOOST_CHECK_EQUAL(1, gw.getHour());
BOOST_CHECK_EQUAL(0, gw.getMinute());
gw.offsetGameTime(-30);
BOOST_CHECK_EQUAL(0, gw.getHour());
BOOST_CHECK_EQUAL(30, gw.getMinute());
gw.offsetGameTime(-60);
BOOST_CHECK_EQUAL(23, gw.getHour());
BOOST_CHECK_EQUAL(30, gw.getMinute());
gw.offsetGameTime(30);
BOOST_CHECK_EQUAL(0, gw.getHour());
BOOST_CHECK_EQUAL(0, gw.getMinute());
gw.offsetGameTime(24*60);
BOOST_CHECK_EQUAL(0, gw.getHour());
BOOST_CHECK_EQUAL(0, gw.getMinute());
gw.offsetGameTime(8*60 + 25);
BOOST_CHECK_EQUAL(8, gw.getHour());
BOOST_CHECK_EQUAL(25, gw.getMinute());
gw.offsetGameTime(-30);
BOOST_CHECK_EQUAL(7, gw.getHour());
BOOST_CHECK_EQUAL(55, gw.getMinute());
gw.offsetGameTime(-24*60);
BOOST_CHECK_EQUAL(7, gw.getHour());
BOOST_CHECK_EQUAL(55, gw.getMinute());
gw.offsetGameTime(0);
BOOST_CHECK_EQUAL(7, gw.getHour());
BOOST_CHECK_EQUAL(55, gw.getMinute());
gw.offsetGameTime(30*24*60 + 90);
BOOST_CHECK_EQUAL(9, gw.getHour());
BOOST_CHECK_EQUAL(25, gw.getMinute());
}
#endif
BOOST_AUTO_TEST_SUITE_END()