summaryrefslogtreecommitdiffhomepage
path: root/StarsEx/NetGame.cpp
blob: acd50ebb87fde131ec661f326eeb77cf4460d9e7 (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.

    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Network Game Manager class
*/

#include "NetGame.h"
#include "NetGameClient.h"
#include "NetGameServer.h"
#include "NetClientConfig.h"
#include "NetServerConfig.h"
#include "NetPlayer.h"

#include "NetMsg.h"
#include "NetData.h"
#include "NetLayer.h"

#include "Player.h"
#include "Ship.h"
#include "ShipDesign.h"
#include "Sim.h"
#include "Element.h"
#include "HUDView.h"

#include "NetHost.h"
#include "Game.h"
#include "Light.h"

// +--------------------------------------------------------------------+

const int MAX_NET_FPS   = 20;
const int MIN_NET_FRAME = 1000 / MAX_NET_FPS;

const DWORD SHIP_ID_START = 0x0010;
const DWORD SHOT_ID_START = 0x0400;

static NetGame* netgame    = 0;

static DWORD   ship_id_key = SHIP_ID_START;
static DWORD   shot_id_key = SHOT_ID_START;

static long    start_time  = 0;

// +--------------------------------------------------------------------+

NetGame::NetGame()
: objid(0), netid(0), link(0), local_player(0), last_send_time(0), active(true)
{
    netgame = this;
    sim = Sim::GetSim();

    ship_id_key = SHIP_ID_START;
    shot_id_key = SHOT_ID_START;

    if (sim)
    local_player = sim->GetPlayerShip();

    Player* player = Player::GetCurrentPlayer();
    if (player) {
        player_name = player->Name();
        player_pass = player->Password();
    }

    start_time = NetLayer::GetUTC();
}

NetGame::~NetGame()
{
    netgame      = 0;
    local_player = 0;
    players.destroy();

    if (link) {
        double delta     = fabs((double)(NetLayer::GetUTC() - start_time));
        double bandwidth = 10.0 * (link->GetBytesSent() + link->GetBytesRecv()) / delta;
        double recvrate  = link->GetPacketsRecv() / delta;

        Print("NetGame Stats\n-------------\n");
        Print("  packets sent %d\n",          link->GetPacketsSent());
        Print("  packets recv %d\n",          link->GetPacketsRecv());
        Print("  bytes sent   %d\n",          link->GetBytesSent());
        Print("  bytes recv   %d\n",          link->GetBytesRecv());
        Print("  retries      %d\n",          link->GetRetries());
        Print("  drops        %d\n",          link->GetDrops());
        Print("  avg lag      %d msec\n",     link->GetLag());
        Print("  time         %d sec\n",      (int) delta);
        Print("  bandwidth    %d bps\n",      (int) bandwidth);
        Print("  packet rate  %d pps in\n\n", (int) recvrate);

        delete link;
    }
}

// +--------------------------------------------------------------------+

NetGame*
NetGame::Create()
{
    if (!netgame) {
        if (NetServerConfig::GetInstance())
        netgame = new NetGameServer;

        else if (NetClientConfig::GetInstance() && NetClientConfig::GetInstance()->GetSelectedServer())
        netgame = new NetGameClient;
    }

    return netgame;
}

NetGame*
NetGame::GetInstance()
{
    return netgame;
}

DWORD
NetGame::GetObjID() const
{
    if (local_player)
    return local_player->GetObjID();

    return 0;
}

DWORD
NetGame::GetNextObjID(int type)
{
    if (type == SHIP) {
        if (ship_id_key >= SHOT_ID_START)
        ship_id_key = SHIP_ID_START;

        return ship_id_key++;
    }

    else if (type == SHOT) {
        if (shot_id_key >= 0xFFFE)
        shot_id_key = SHOT_ID_START;

        return shot_id_key++;
    }

    return 0;
}

// +--------------------------------------------------------------------+

void
NetGame::ExecFrame()
{
    Send();
    Recv();
}

void
NetGame::Recv()
{
    NetMsg* msg = link->GetMessage();

    while (msg) {
        if (active) {
            // For Debug Convenience:
            // NetPlayer* player = FindPlayerByNetID(msg->NetID());

            switch (msg->Type()) {
            case NET_JOIN_REQUEST:     DoJoinRequest(msg);     break;
            case NET_JOIN_ANNOUNCE:    DoJoinAnnounce(msg);    break;
            case NET_QUIT_REQUEST:     DoQuitRequest(msg);     break;
            case NET_QUIT_ANNOUNCE:    DoQuitAnnounce(msg);    break;
            case NET_GAME_OVER:        DoGameOver(msg);        break;
            case NET_DISCONNECT:       DoDisconnect(msg);      break;

            case NET_OBJ_LOC:          DoObjLoc(msg);          break;
            case NET_OBJ_DAMAGE:       DoObjDamage(msg);       break;
            case NET_OBJ_KILL:         DoObjKill(msg);         break;
            case NET_OBJ_SPAWN:        DoObjSpawn(msg);        break;
            case NET_OBJ_HYPER:        DoObjHyper(msg);        break;
            case NET_OBJ_TARGET:       DoObjTarget(msg);       break;
            case NET_OBJ_EMCON:        DoObjEmcon(msg);        break;
            case NET_SYS_DAMAGE:       DoSysDamage(msg);       break;
            case NET_SYS_STATUS:       DoSysStatus(msg);       break;

            case NET_ELEM_CREATE:      DoElemCreate(msg);      break;
            case NET_ELEM_REQUEST:     DoElemRequest(msg);     break;
            case NET_SHIP_LAUNCH:      DoShipLaunch(msg);      break;
            case NET_NAV_DATA:         DoNavData(msg);         break;
            case NET_NAV_DELETE:       DoNavDelete(msg);       break;

            case NET_WEP_TRIGGER:      DoWepTrigger(msg);      break;
            case NET_WEP_RELEASE:      DoWepRelease(msg);      break;
            case NET_WEP_DESTROY:      DoWepDestroy(msg);      break;

            case NET_COMM_MESSAGE:     DoCommMsg(msg);         break;
            case NET_CHAT_MESSAGE:     DoChatMsg(msg);         break;
            case NET_SELF_DESTRUCT:    DoSelfDestruct(msg);    break;
            }
        }

        delete msg;
        msg = link->GetMessage();
    }
}

// +--------------------------------------------------------------------+

void
NetGame::Send()
{
}

int
NetGame::NumPlayers()
{
    if (netgame) {
        int num_players = netgame->players.size();

        if (netgame->local_player)
        num_players++;

        return num_players;
    }

    return 0;
}

NetPlayer*
NetGame::FindPlayerByName(const char* name)
{
    for (int i = 0; i < players.size(); i++) {
        NetPlayer* p = players[i];

        if (!strcmp(p->Name(), name))
        return p;
    }

    return 0;
}

NetPlayer*
NetGame::FindPlayerByNetID(DWORD netid)
{
    for (int i = 0; i < players.size(); i++) {
        NetPlayer* p = players[i];

        if (p->GetNetID() == netid)
        return p;
    }

    return 0;
}

NetPlayer*
NetGame::FindPlayerByObjID(DWORD objid)
{
    for (int i = 0; i < players.size(); i++) {
        NetPlayer* p = players[i];

        if (p->GetObjID() == objid)
        return p;
    }

    return 0;
}

Ship*
NetGame::FindShipByObjID(DWORD objid)
{
    if (sim)
    return sim->FindShipByObjID(objid);

    return 0;
}

Shot*
NetGame::FindShotByObjID(DWORD objid)
{
    if (sim)
    return sim->FindShotByObjID(objid);

    return 0;
}

NetPeer*
NetGame::GetPeer(NetPlayer* player)
{
    if (player && link) {
        return link->FindPeer(player->GetNetID());
    }

    return 0;
}

// +--------------------------------------------------------------------+

void
NetGame::Respawn(DWORD objid, Ship* spawn)
{
}

// +--------------------------------------------------------------------+

bool
NetGame::IsNetGame()
{
    return netgame != 0;
}

bool
NetGame::IsNetGameClient()
{
    if (netgame)
    return netgame->IsClient();
    return false;
}

bool
NetGame::IsNetGameServer()
{
    if (netgame)
    return netgame->IsServer();
    return false;
}