-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathNetworkGameSystem.h
More file actions
103 lines (87 loc) · 4.25 KB
/
Copy pathNetworkGameSystem.h
File metadata and controls
103 lines (87 loc) · 4.25 KB
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
#ifndef NETWORKMANAGERCONTROLLER_H
#define NETWORKMANAGERCONTROLLER_H
#include "RED4ext/Api/v1/Sdk.hpp"
#include "RED4ext/SystemUpdate.hpp"
#include "RED4ext/Scripting/IScriptable.hpp"
#include "RED4ext/Scripting/Natives/Generated/ink/ISystemRequestsHandler.hpp"
#include "RED4ext/Scripting/Stack.hpp"
#include "PlayerActionTracker.h"
#include "PlayerSync/InterpolationData.h"
#include "Red/ClassDescriptorCompat.hpp"
#include "RED4ext/Scripting/Natives/Generated/AI/Command.hpp"
#include "RED4ext/Scripting/Natives/Generated/Vector4.hpp"
#include "RED4ext/Scripting/Natives/entEntityID.hpp"
#include <RedLib.hpp>
#include <clientbound/WorldPacketsClientBound.h>
#include <map>
#include <steam/isteamnetworkingsockets.h>
#include <steam/steamnetworkingtypes.h>
#include <serverbound/WorldPacketsServerBound.h>
class NetworkGameSystem : public Red::IGameSystem
{
private:
HSteamNetConnection m_hConnection;
ISteamNetworkingSockets *m_pInterface;
bool m_hasTriedToConnect = false;
bool m_hasEnqueuedLoadLastCheckpoint = false;
Red::Handle<Red::ink::ISystemRequestsHandler> m_systemRequestsHandler;
bool m_gameRestored = false;
std::map<uint64_t, RED4ext::ent::EntityID> m_networkedEntitiesLookup;
std::map<RED4ext::ent::EntityID, InterpolationData> m_interpolationData;
std::map<RED4ext::ent::EntityID, RED4ext::Handle<RED4ext::AICommand>> m_LastTeleportCommand;
float m_TimeSinceLastPlayerPositionSync;
private:
void OnRegisterUpdates(RED4ext::UpdateRegistrar* aRegistrar) override;
bool OnGameRestored() override;
private:
bool ConnectToServer(const std::string& host, uint16_t port);
static void ConnectionStatusChangedCallback(SteamNetConnectionStatusChangedCallback_t* pInfo);
void OnNetworkUpdate(RED4ext::FrameInfo& frame_info, RED4ext::JobQueue& job_queue);
void InterpolatePuppets(float deltaTime);
void SetEntityPosition(RED4ext::ent::EntityID entityId, RED4ext::Vector4 worldPosition, float yaw);
protected:
void PollIncomingMessages();
void TrackPlayerPosition(float deltaTime);
public:
bool FullyConnected = false;
Red::Handle<PlayerActionTracker> playerActionTracker = Red::Handle(new PlayerActionTracker());
/// This is called by Redscript when the connection wasn't established as the UI had loaded the available savegames.
/// Thus we will _enqueue_ the "LoadLastCheckpoint" call, that Redscript had otherwise done, had we connected fast enough.
void EnqueueLoadLastCheckpoint(const Red::Handle<RED4ext::ink::ISystemRequestsHandler>& handler)
{
m_systemRequestsHandler = handler;
m_hasEnqueuedLoadLastCheckpoint = true;
}
template<typename T>
bool EnqueueMessage(uint8_t channel_id, T frame);
/// Called from the plugin load and unload events
static bool Load();
/// Called from the plugin load and unload events
static void Unload();
private:
RTTI_IMPL_TYPEINFO(NetworkGameSystem);
RTTI_IMPL_ALLOCATOR();
};
namespace Red
{
template<>
class ClassDescriptorDefaultImpl<::NetworkGameSystem> : public CyberverseClassDescriptorDefaultImpl<::NetworkGameSystem>
{
};
} // namespace Red
RTTI_DEFINE_CLASS(NetworkGameSystem, {
RTTI_METHOD(EnqueueLoadLastCheckpoint);
RTTI_PROPERTY(FullyConnected);
RTTI_PROPERTY(playerActionTracker);
RTTI_ALIAS("Cyberverse.Network.Managers.NetworkGameSystem");
});
// TODO: Thing about the concept of having EnqueueMessage public, it causes _this_, at least with templates: We need to
// expliticly state template invocations for the code to be generated, as otherwise NetworkGameSystem.cpp doesn't know
// about PlayerActionTracked. Maybe we could also inline the whole EnqueueMessage code, then it will be emited into the
// relevant caller CU (but that may be more than one for the same packet, making template hell even worse).
template bool NetworkGameSystem::EnqueueMessage(uint8_t channel_id, PlayerActionTracked msg);
template bool NetworkGameSystem::EnqueueMessage(uint8_t channel_id, PlayerSpawnCar msg);
template bool NetworkGameSystem::EnqueueMessage(uint8_t channel_id, PlayerUnmountCar msg);
template bool NetworkGameSystem::EnqueueMessage(uint8_t channel_id, PlayerEquipItem msg);
template bool NetworkGameSystem::EnqueueMessage(uint8_t channel_id, PlayerShoot msg);
#endif //NETWORKMANAGERCONTROLLER_H