summaryrefslogtreecommitdiffhomepage
path: root/Starserver/StarServer.cpp
blob: ab3d0fb0bb0b7de77a2703931cc2b7d565e1b75e (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.

    AUTHOR:       John DiCamillo

*/

#include "StarServer.h"

#include <chrono>
#include <iostream>
#include <thread>

#include "Campaign.h"
#include "Clock.h"
#include "CombatRoster.h"
#include "DataLoader.h"
#include "Drive.h"
#include "Explosion.h"
#include "FlightDeck.h"
#include "Galaxy.h"
#include "Game.h"
#include "ModConfig.h"
#include "NetAdminServer.h"
#include "NetGame.cpp"
#include "NetLobby.h"
#include "NetLobbyServer.h"
#include "NetServerConfig.h"
#include "RadioTraffic.h"
#include "Random.h"
#include "Ship.h"
#include "Shot.h"
#include "Sim.h"
#include "SystemDesign.h"
#include "Types.h"
#include "Utils.h"
#include "WeaponDesign.h"


StarServer* StarServer::instance {nullptr};


StarServer*
StarServer::GetInstance()
{
    return instance;
}


StarServer::StarServer() :
    world {nullptr},
    current_mission {nullptr},
    admin_server {nullptr},
    lobby_server {nullptr}
{
    if (instance != nullptr)
        throw "StarServer may have only one instance";
    instance = this;

    DataLoader::Initialize();
    auto loader = DataLoader::GetLoader();
    if (!loader) throw "Could not get DataLoader instance";

    int loadstat = loader->MountDatafile("shatter.dat");
    if (loadstat == DataLoader::FAILED) {
        throw loader->LastError();
    }

    if (loader->FindFile("start.dat"))
        loader->MountDatafile("start.dat");

    loader->EnableMedia(false);
}


StarServer::~StarServer()
{
    delete admin_server;
    delete lobby_server;

    admin_server = nullptr;
    lobby_server = nullptr;

    // delete all the ships and stuff
    // BEFORE getting rid of the system
    // and weapons catalogs!
    if (world) delete world;
    world = nullptr;

    Drive::Close();
    Explosion::Close();
    FlightDeck::Close();
    Campaign::Close();
    CombatRoster::Close();
    Galaxy::Close();
    RadioTraffic::Close();
    Ship::Close();
    WeaponDesign::Close();
    SystemDesign::Close();
    DataLoader::Close();
    NetServerConfig::Close();
    ModConfig::Close();

    if (instance == this)
        instance = nullptr;
}


bool
StarServer::Init(HINSTANCE hi, HINSTANCE hpi, LPSTR cmdline, int nCmdShow)
{
    if (auto loader = DataLoader::GetLoader())
        loader->UseFileSystem(false);
    return Game::Init(hi, hpi, cmdline, nCmdShow);
}


bool
StarServer::InitGame()
{
    if (!Game::InitGame())
    return false;

    RandomInit();
    ModConfig::Initialize();
    NetServerConfig::Initialize();
    SystemDesign::Initialize("sys.def");
    WeaponDesign::Initialize("wep.def");
    Ship::Initialize();
    Galaxy::Initialize();
    CombatRoster::Initialize();
    Campaign::Initialize();

    Drive::Initialize();
    Explosion::Initialize();
    FlightDeck::Initialize();
    Ship::Initialize();
    Shot::Initialize();
    RadioTraffic::Initialize();

    NetServerConfig* server_config = NetServerConfig::GetInstance();
    if (!server_config)
    return false;

    ::Print("\n\n\nStarshatter Server Init\n");
    ::Print("-----------------------\n");
    ::Print("Server Name:       %s\n", (const char*) server_config->Name());
    ::Print("Server Type:       %d\n", server_config->GetGameType());

    if (server_config->GetMission().length() > 0)
    ::Print("Server Mission:    %s\n", (const char*) server_config->GetMission());

    ::Print("Lobby Server Port: %d\n", server_config->GetLobbyPort());
    ::Print("Admin Server Port: %d\n", server_config->GetAdminPort());
    ::Print("-----------------------\n");

    NetLobbyServer* nls = new NetLobbyServer;
    NetAdminServer* nas = NetAdminServer::GetInstance(server_config->GetAdminPort());
    nas->SetServerName(server_config->Name());

    lobby_server = nls;
    admin_server = nas;

    std::cout << "Started server listening on port " << server_config->GetLobbyPort() << std::endl;

    return true;
}


void
StarServer::SetGameMode(int m)
{
    if (game_mode == m)
    return;

    if (m == LOAD_MODE) {
        Print("  game_mode = LOAD_MODE\n");
        paused = true;
    }

    else if (m == PLAY_MODE) {
        Print("  game_mode = PLAY_MODE\n");

        if (!world) {
            CreateWorld();
            InstantiateMission();
        }

        // stand alone server should wait for players to connect
        // before unpausing the simulation...
        Clock::GetInstance()->SetTimeCompression(1.0);
        Pause(true);
    }

    else if (m == MENU_MODE) {
        Print("  game_mode = MENU_MODE\n");
        paused = true;

        if (auto sim = dynamic_cast<Sim*>(world))
            sim->UnloadMission();
    }

    game_mode = m;
}


void
StarServer::CreateWorld()
{
    RadioTraffic::Initialize();

    // create world
    if (!world) {
        world = new Sim(nullptr);
        Print("  World Created.\n");
    }
}


void
StarServer::InstantiateMission()
{
    current_mission = nullptr;

    if (Campaign::GetCampaign()) {
        current_mission = Campaign::GetCampaign()->GetMission();
    }

    if (auto sim = dynamic_cast<Sim*>(world)) {
        sim->UnloadMission();

        if (current_mission) {
            sim->LoadMission(current_mission);
            sim->ExecMission();
            sim->SetTestMode(false);

            Print("  Mission Instantiated.\n");
            std::cout << "Loaded mission: " << current_mission->Name() << std::endl;
        }

        else {
            Print("  *** WARNING: StarServer::InstantiateMission() - no mission selected ***\n");
        }
    }
}


void
StarServer::UpdateWorld()
{
    Galaxy* galaxy = Galaxy::GetInstance();
    if (galaxy) galaxy->ExecFrame();

    Campaign* campaign = Campaign::GetCampaign();
    if (campaign) campaign->ExecFrame();

    if (paused) {
        if (world)
        world->ExecFrame(0);
    }

    else {
        Drive::StartFrame();

        if (world)
        world->ExecFrame(Clock::GetInstance()->Delta());
    }
}


void
StarServer::GameState()
{
    if (lobby_server) {
        lobby_server->ExecFrame();

        if (lobby_server->GetStatus() == NetServerInfo::PERSISTENT)
        paused = NetGame::NumPlayers() < 1;
    }

    if (game_mode == MENU_MODE) {
        std::this_thread::sleep_for(std::chrono::milliseconds(30));
    }
    else if (game_mode == LOAD_MODE) {
        CreateWorld();
        InstantiateMission();

        SetGameMode(PLAY_MODE);
    }
    else if (game_mode == PLAY_MODE) {
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}


bool
StarServer::Server() const
{
    return true;
}