diff options
author | Hello=) <hello@themanaworld.org> | 2024-03-28 03:58:45 +0300 |
---|---|---|
committer | Hello=) <hello@themanaworld.org> | 2024-03-28 03:58:45 +0300 |
commit | db0b1da0060f5eb4b2af040c22ea9373d36970af (patch) | |
tree | dc2bdc0478c341bf4368fee4993c20e52f2d41c1 /src/game.cpp | |
download | guildbot-db0b1da0060f5eb4b2af040c22ea9373d36970af.tar.gz guildbot-db0b1da0060f5eb4b2af040c22ea9373d36970af.tar.bz2 guildbot-db0b1da0060f5eb4b2af040c22ea9373d36970af.tar.xz guildbot-db0b1da0060f5eb4b2af040c22ea9373d36970af.zip |
Initial commit of Guild Bot, as seen in:
$ sha256sum guildsrc.zip
ef61469ab59ce3f5dd3289505e89bd5b84e3a82b89cda90d25ae3c41f7464beb guildsrc.zip
Verification shown its source 100% match to currently running version on TMWA.
The following changes were made VS version found in guildsrc.zip:
1) src/build and src/dist content completely removed.
Rationale: build-time artifacts; binaries contained login/password.
2) Executable flag (+x) removed from src/automation.cpp and src/main.cpp.
Rationale: executable CPP sources are weird thing. No change to file content.
3) src/main.cpp changes: 3 lines:
std::string main_username = "test";
std::string main_pw = "test";
std::string main_host = "127.0.0.1";
Rationale: avoid leaking real production credentials and ensure experimenters
dont hammer real TMW prod server by experiments.
Effort been made to preserve original file timestamps, etc. However toplevel src/
dir date adjusted to date of mentioned changes.
Diffstat (limited to 'src/game.cpp')
-rw-r--r-- | src/game.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/game.cpp b/src/game.cpp new file mode 100644 index 0000000..d933c2f --- /dev/null +++ b/src/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); +} |