summaryrefslogblamecommitdiff
path: root/main.cpp
blob: 56f7815c46205e2485d5e872f099617f1538a028 (plain) (tree)
























































































































































                                                                           
/* 
 * File:   main.cpp
 * Author: dipesh
 *
 * Created on July 31, 2010, 6:04 PM
 */

#include <stdlib.h>
#include <iostream>
#include <string>
#include <SDL.h>

#include "logindata.h"
#include "game.h"
#include "main.h"
#include "automation.h"

#include "net/charserverhandler.h"
#include "net/loginhandler.h"
#include "net/maploginhandler.h"
#include "net/messagein.h"
#include "net/messageout.h"
#include "net/network.h"
#include "net/nethandler.h"
#include "serverinfo.h"

#if defined WIN32
#include "Windows.h"
#include "Winbase.h"
#include "utils/specialfolder.h"
#endif

unsigned char state = LOGIN_STATE;
unsigned char oldstate;
std::string errorMessage;
char n_server, n_character;

class SERVER_INFO;
SERVER_INFO **server_info;

std::string main_username = "test";
std::string main_pw = "test";
std::string main_host = "127.0.0.1";
int main_charno = 1; //character slot

int main_port = 6901;

int charID[3];
int world;
std::string char_name;
std::string mLocalDataDir;

LoginData loginData;

void init_engine()
{

    if (SDL_Init(SDL_INIT_TIMER) < 0) {
        std::cerr << "Could not initialize SDL: " <<
            SDL_GetError() << std::endl;
        exit(1);
    }

    atexit(SDL_Quit);
    mLocalDataDir = ".";

#if defined WIN32
        mLocalDataDir = getSpecialFolderLocation(CSIDL_LOCAL_APPDATA);
        mLocalDataDir += "/ManaGuild";
#endif
}

int main(int argc, char **argv)
{   
    Automation::getAutomationHandler()->loadMembers();
    
    init_engine();
    SDLNet_Init();
    Network *network = NetHandler::getNetInstance()->getNetwork();
    Game *game = new Game();

    while (state != EXIT_STATE)
    {
        network->flush();
        network->dispatchMessages();

        if (network->getState() == Network::ERROR_BROKE)
        {
            state = ERROR_STATE;
            errorMessage = "Got disconnected from server!";
        }

        if (state != oldstate) 
        {
            oldstate = state;

            switch (oldstate)
            {
                    // Those states that don't cause a network disconnect
                case ACCOUNT_STATE:
                case CHAR_SELECT_STATE:
                case GAME_STATE:
                    break;

                default:
                    network->disconnect();
                    network->clearHandlers();
                    break;
            }
            
            switch (state)
            {
                case LOGIN_STATE:
                    state = ACCOUNT_STATE;
                    break;
                case ACCOUNT_STATE:
                    loginData.hostname = main_host;
                    loginData.port = main_port;
                    loginData.password = main_pw;
                    loginData.username = main_username;
                    NetHandler::getNetInstance()->accountLogin(&loginData);
                    break;
                case CHAR_SERVER_STATE:
                {
                    SERVER_INFO *si = server_info[world];
                    loginData.hostname = iptostring(si->address);
                    loginData.port = si->port;
                    NetHandler::getNetInstance()->charLogin(&loginData);
                    break;
                }
                case CHAR_SELECT_STATE:
                    NetHandler::getNetInstance()->attemptCharSelect();
                    break;
                case CONNECTING_STATE:
                    NetHandler::getNetInstance()->mapLogin(&loginData);
                    break;
                case GAME_STATE:
                    game->logic();
                    break;
                case ERROR_STATE:
                    state = EXIT_STATE; //LOGIN_STATE
                    break;
                default:
                    state = EXIT_STATE;
                    break;
            }
        }
    }
    
    delete game;
    return (EXIT_SUCCESS);
}