summaryrefslogtreecommitdiff
path: root/main.cpp
blob: 56f7815c46205e2485d5e872f099617f1538a028 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* 
 * 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);
}