Skip to content

Commit

Permalink
Refactor game logic for time
Browse files Browse the repository at this point in the history
  • Loading branch information
ranisalt committed Jul 31, 2015
1 parent 8a9b82e commit d5e9f98
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
30 changes: 16 additions & 14 deletions src/game.cpp
Expand Up @@ -78,11 +78,11 @@ Game::Game() :
lastBucket = 0;

//(1440 minutes/day)/(3600 seconds/day)*10 seconds event interval
int32_t dayCycle = 3600;
lightHourDelta = 1440 * 10 / dayCycle;
lightHour = SUNRISE + (SUNSET - SUNRISE) / 2;
lightLevel = LIGHT_LEVEL_DAY;
lightState = LIGHT_STATE_DAY;
lightHourDelta = 1;
lightHour = time(nullptr) % 1440;
lightLevel = (lightHour >= Game::SUNRISE && lightHout < Game::SUNSET) ? LIGHT_LEVEL_DAY : LIGHT_LEVEL_NIGHT;
lightColor = 0x90; // red
lightState = (lightHour >= Game::SUNRISE && lightHout < Game::SUNSET) ? LIGHT_STATE_DAY : LIGHT_STATE_NIGHT;

offlineTrainingWindow.choices.emplace_back("Sword Fighting and Shielding", SKILL_SWORD);
offlineTrainingWindow.choices.emplace_back("Axe Fighting and Shielding", SKILL_AXE);
Expand All @@ -107,7 +107,7 @@ void Game::start(ServiceManager* manager)
{
serviceManager = manager;

g_scheduler.addEvent(createSchedulerTask(EVENT_LIGHTINTERVAL, std::bind(&Game::checkLight, this)));
g_scheduler.addEvent(createSchedulerTask(EVENT_LIGHTINTERVAL, std::bind(&Game::checkLight, this, false)));
g_scheduler.addEvent(createSchedulerTask(EVENT_CREATURE_THINK_INTERVAL, std::bind(&Game::checkCreatures, this, 0)));
g_scheduler.addEvent(createSchedulerTask(EVENT_DECAYINTERVAL, std::bind(&Game::checkDecay, this)));
}
Expand Down Expand Up @@ -4308,19 +4308,21 @@ void Game::checkDecay()
cleanup();
}

void Game::checkLight()
void Game::checkLight(bool forced /*= false*/)
{
g_scheduler.addEvent(createSchedulerTask(EVENT_LIGHTINTERVAL, std::bind(&Game::checkLight, this)));
if (!forced) {
g_scheduler.addEvent(createSchedulerTask(EVENT_LIGHTINTERVAL, std::bind(&Game::checkLight, this, false)));

lightHour += lightHourDelta;
lightHour += lightHourDelta;
}

if (lightHour > 1440) {
lightHour -= 1440;
}

if (std::abs(lightHour - SUNRISE) < 2 * lightHourDelta) {
if (std::abs(lightHour - SUNRISE) < 8 * lightHourDelta || (lightLevel < LIGHT_LEVEL_DAY && (lightHour > SUNRISE && lightHour < SUNSET))) {
lightState = LIGHT_STATE_SUNRISE;
} else if (std::abs(lightHour - SUNSET) < 2 * lightHourDelta) {
} else if (std::abs(lightHour - SUNSET) < 8 * lightHourDelta || (lightLevel > LIGHT_LEVEL_NIGHT && (lightHour > SUNSET || lightHour < SUNRISE))) {
lightState = LIGHT_STATE_SUNSET;
}

Expand All @@ -4329,12 +4331,12 @@ void Game::checkLight()

switch (lightState) {
case LIGHT_STATE_SUNRISE: {
newLightLevel += (LIGHT_LEVEL_DAY - LIGHT_LEVEL_NIGHT) / 30;
newLightLevel += (LIGHT_LEVEL_DAY - LIGHT_LEVEL_NIGHT) / 120;
lightChange = true;
break;
}
case LIGHT_STATE_SUNSET: {
newLightLevel -= (LIGHT_LEVEL_DAY - LIGHT_LEVEL_NIGHT) / 30;
newLightLevel -= (LIGHT_LEVEL_DAY - LIGHT_LEVEL_NIGHT) / 120;
lightChange = true;
break;
}
Expand Down Expand Up @@ -4365,7 +4367,7 @@ void Game::checkLight()
void Game::getWorldLightInfo(LightInfo& lightInfo) const
{
lightInfo.level = lightLevel;
lightInfo.color = 0xD7;
lightInfo.color = lightColor;
}

void Game::addCommandTag(char tag)
Expand Down
20 changes: 13 additions & 7 deletions src/game.h
Expand Up @@ -71,7 +71,7 @@ enum LightState_t {
LIGHT_STATE_SUNRISE,
};

#define EVENT_LIGHTINTERVAL 10000
#define EVENT_LIGHTINTERVAL 2500
#define EVENT_DECAYINTERVAL 250
#define EVENT_DECAY_BUCKETS 4

Expand Down Expand Up @@ -433,7 +433,7 @@ class Game
void updateCreatureWalk(uint32_t creatureId);
void checkCreatureAttack(uint32_t creatureId);
void checkCreatures(size_t index);
void checkLight();
void checkLight(bool forced = false);

bool combatBlockHit(CombatDamage& damage, Creature* attacker, Creature* target, bool checkDefense, bool checkArmor, bool field);

Expand All @@ -457,6 +457,9 @@ class Game
int32_t getLightHour() const {
return lightHour;
}
void setLightHour(int32_t lightHour) {
this->lightHour = lightHour % 1440;
}

bool loadExperienceStages();
uint64_t getExperienceStage(uint32_t level);
Expand Down Expand Up @@ -505,6 +508,13 @@ class Game
Raids raids;
Quests quests;

static const int32_t LIGHT_LEVEL_DAY = 250;
static const int32_t LIGHT_LEVEL_NIGHT = 40;

// Average values for Florianopolis/BR
static const int32_t SUNSET = 1130; // 18:50
static const int32_t SUNRISE = 400; // 6:40

protected:
bool playerSayCommand(Player* player, const std::string& text);
bool playerSaySpell(Player* player, SpeakClasses type, const std::string& text);
Expand Down Expand Up @@ -546,16 +556,12 @@ class Game
ModalWindow offlineTrainingWindow;
Commands commands;

static const int32_t LIGHT_LEVEL_DAY = 250;
static const int32_t LIGHT_LEVEL_NIGHT = 40;
static const int32_t SUNSET = 1305;
static const int32_t SUNRISE = 430;

GameState_t gameState;
WorldType_t worldType;

LightState_t lightState;
uint8_t lightLevel;
uint8_t lightColor;
int32_t lightHour;
int32_t lightHourDelta;

Expand Down

0 comments on commit d5e9f98

Please sign in to comment.