diff options
author | Hello=) <hello@themanaworld.org> | 2024-03-29 04:23:18 +0300 |
---|---|---|
committer | Hello=) <hello@themanaworld.org> | 2024-03-29 04:23:18 +0300 |
commit | 08cd08587be5c8d2ce42ae417098c524f683c6ad (patch) | |
tree | 727aa239372a77e9ca3d72fe761940bb49e8d2d8 /game.cpp | |
parent | db0b1da0060f5eb4b2af040c22ea9373d36970af (diff) | |
download | guildbot-08cd08587be5c8d2ce42ae417098c524f683c6ad.tar.gz guildbot-08cd08587be5c8d2ce42ae417098c524f683c6ad.tar.bz2 guildbot-08cd08587be5c8d2ce42ae417098c524f683c6ad.tar.xz guildbot-08cd08587be5c8d2ce42ae417098c524f683c6ad.zip |
Move actual top level from src/ to top: just one single dir is pointless.
Reported-By: NetSysFire
Diffstat (limited to 'game.cpp')
-rw-r--r-- | game.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/game.cpp b/game.cpp new file mode 100644 index 0000000..d933c2f --- /dev/null +++ b/game.cpp @@ -0,0 +1,103 @@ +#include "game.h" + +#include <fstream> +#include <sstream> +#include <string> +#include <iostream> + +#include "automation.h" +#include "net/nethandler.h" +#include "net/network.h" +#include "onlinelist.h" + +std::string map_path; +volatile int tick_time; +volatile int cur_time; +const int MAX_TIME = 10000; +bool done = false; + +OnlineList *onlinelist; + +/** + * Advances game logic counter. + */ +Uint32 nextTick(Uint32 interval, void *param) +{ + tick_time++; + if (tick_time == MAX_TIME) tick_time = 0; + return interval; +} + +int get_elapsed_time(int start_time) +{ + if (start_time <= tick_time) { + return (tick_time - start_time) * 10; + } + else { + return (tick_time + (MAX_TIME - start_time)) * 10; + } +} + +Game::Game() +{ + // Initialize timers + tick_time = 0; + SDL_AddTimer(10, nextTick, NULL); // Logic counter + onlinelist = new OnlineList(); +} + +Game::~Game() +{ + delete onlinelist; +} + +int stay_awake(void *data) +{ + /* Eathena seems to kick the client after 10 mins of inactivity. + A teenky hack to keep it alive.*/ + while (!done) + { + NetHandler::getNetInstance()->sit(); + std::cout << "I'm still here." << std::endl; + SDL_Delay(5 * 60 * 1000); + } +} + +void Game::logic() +{ + done = false; + int gameTime = tick_time; + + SDL_Thread *wakeywakey; + + + Network *mNetwork = NetHandler::getNetInstance()->getNetwork(); + NetHandler::getNetInstance()->mapLoaded(); + NetHandler::getNetInstance()->ping(tick_time); + //NetHandler::getNetInstance()->sit(); + NetHandler::getNetInstance()->loadHandlers(); + wakeywakey = SDL_CreateThread(stay_awake, NULL); + + while (!done) + { + if (mNetwork->getState() == Network::ERROR_BROKE) //get out of this loop to allow a restart. + done = true; + + mNetwork->flush(); + mNetwork->dispatchMessages(); + + while (get_elapsed_time(gameTime) > 0) + { + cur_time = static_cast<int>(time(0)); + onlinelist->logic(); + Automation::getAutomationHandler()->logic(); + ++gameTime; + } + + gameTime = tick_time; + + SDL_Delay(10); // Don't steal all the cpu time. + } + + SDL_KillThread(wakeywakey); +} |