Debian Codegraph @

Sources/Jazz2/Actors/Multiplayer/RemoteActor.cpp

raw
1 #include "RemoteActor.h"
2
3 #if defined(WITH_MULTIPLAYER)
4
5 #include "../../../nCine/Base/Clock.h"
6
7 namespace Jazz2::Actors::Multiplayer
8 {
9 RemoteActor::RemoteActor()
10 : _stateBufferPos(0), _lastAnim(AnimState::Idle), _isAttachedLocally(false)
11 {
12 }
13
14 Task<bool> RemoteActor::OnActivatedAsync(const ActorActivationDetails& details)
15 {
16 SetState(ActorState::PreserveOnRollback, true);
17 SetState(ActorState::CanBeFrozen | ActorState::CollideWithTileset | ActorState::ApplyGravitation, false);
18
19 Clock& c = nCine::clock();
20 std::uint64_t now = c.now() * 1000 / c.frequency();
21 for (std::int32_t i = 0; i < std::int32_t(arraySize(_stateBuffer)); i++) {
22 _stateBuffer[i].Time = now - arraySize(_stateBuffer) + i;
23 _stateBuffer[i].Pos = Vector2f(details.Pos.X, details.Pos.Y);
24 }
25
26 async_return true;
27 }
28
29 void RemoteActor::OnUpdate(float timeMult)
30 {
31 if (!_isAttachedLocally) {
32 Clock& c = nCine::clock();
33 std::int64_t now = c.now() * 1000 / c.frequency();
34 std::int64_t renderTime = now - ServerDelay;
35
36 std::int32_t nextIdx = _stateBufferPos - 1;
37 if (nextIdx < 0) {
38 nextIdx += std::int32_t(arraySize(_stateBuffer));
39 }
40
41 if (renderTime <= _stateBuffer[nextIdx].Time) {
42 std::int32_t prevIdx;
43 while (true) {
44 prevIdx = nextIdx - 1;
45 if (prevIdx < 0) {
46 prevIdx += std::int32_t(arraySize(_stateBuffer));
47 }
48
49 if (prevIdx == _stateBufferPos || _stateBuffer[prevIdx].Time <= renderTime) {
50 break;
51 }
52
53 nextIdx = prevIdx;
54 }
55
56 Vector2f pos;
57 std::int64_t timeRange = (_stateBuffer[nextIdx].Time - _stateBuffer[prevIdx].Time);
58 if (timeRange > 0) {
59 float lerp = (float)(renderTime - _stateBuffer[prevIdx].Time) / timeRange;
60 pos = _stateBuffer[prevIdx].Pos + (_stateBuffer[nextIdx].Pos - _stateBuffer[prevIdx].Pos) * lerp;
61 } else {
62 pos = _stateBuffer[nextIdx].Pos;
63 }
64
65 MoveInstantly(pos, MoveType::Absolute | MoveType::Force);
66 }
67 }
68
69 ActorBase::OnUpdate(timeMult);
70 }
71
72 void RemoteActor::OnAttach(ActorBase* parent)
73 {
74 _isAttachedLocally = true;
75 }
76
77 void RemoteActor::OnDetach(ActorBase* parent)
78 {
79 _isAttachedLocally = false;
80 }
81
82 void RemoteActor::AssignMetadata(std::uint8_t flags, ActorState state, StringView path, AnimState anim, float rotation, float scaleX, float scaleY, ActorRendererType rendererType)
83 {
84 constexpr ActorState RemotedFlags = ActorState::Illuminated | ActorState::IsInvulnerable |
85 ActorState::CollideWithOtherActors | ActorState::CollideWithSolidObjects | ActorState::IsSolidObject |
86 ActorState::CollideWithTilesetReduced | ActorState::CollideWithSolidObjectsBelow | ActorState::ExcludeSimilar;
87
88 RequestMetadata(path);
89 SetAnimation(anim);
90 SetState((GetState() & ~RemotedFlags) | (state & RemotedFlags));
91
92 _renderer.Initialize(rendererType);
93 _renderer.setRotation(rotation);
94
95 SyncMiscWithServer(flags);
96 }
97
98 void RemoteActor::SyncPositionWithServer(Vector2f pos)
99 {
100 Clock& c = nCine::clock();
101 std::int64_t now = c.now() * 1000 / c.frequency();
102
103 if (_renderer.isDrawEnabled()) {
104 // Actor is still visible, enable interpolation
105 _stateBuffer[_stateBufferPos].Time = now;
106 _stateBuffer[_stateBufferPos].Pos = pos;
107 } else {
108 // Actor was hidden before, reset state buffer to disable interpolation
109 std::int32_t stateBufferPrevPos = _stateBufferPos - 1;
110 if (stateBufferPrevPos < 0) {
111 stateBufferPrevPos += std::int32_t(arraySize(_stateBuffer));
112 }
113
114 std::int64_t renderTime = now - ServerDelay;
115
116 _stateBuffer[stateBufferPrevPos].Time = renderTime;
117 _stateBuffer[stateBufferPrevPos].Pos = pos;
118 _stateBuffer[_stateBufferPos].Time = renderTime;
119 _stateBuffer[_stateBufferPos].Pos = pos;
120 }
121
122 _stateBufferPos++;
123 if (_stateBufferPos >= std::int32_t(arraySize(_stateBuffer))) {
124 _stateBufferPos = 0;
125 }
126 }
127
128 void RemoteActor::SyncAnimationWithServer(AnimState anim, float rotation, float scaleX, float scaleY, Actors::ActorRendererType rendererType)
129 {
130 if (_lastAnim != anim) {
131 _lastAnim = anim;
132 SetAnimation(anim);
133 }
134
135 _renderer.setRotation(rotation);
136 _renderer.setScale(scaleX, scaleY);
137 _renderer.Initialize(rendererType);
138 }
139
140 void RemoteActor::SyncMiscWithServer(std::uint8_t flags)
141 {
142 _renderer.setDrawEnabled((flags & 0x04) != 0);
143 _renderer.AnimPaused = (flags & 0x08) != 0;
144 SetFacingLeft((flags & 0x10) != 0);
145 _renderer.setFlippedY((flags & 0x20) != 0);
146
147 bool justWarped = (flags & 0x40) != 0;
148 if (justWarped) {
149 Clock& c = nCine::clock();
150 std::int64_t now = c.now() * 1000 / c.frequency();
151
152 std::int32_t stateBufferPrevPos = _stateBufferPos - 1;
153 if (stateBufferPrevPos < 0) {
154 stateBufferPrevPos += std::int32_t(arraySize(_stateBuffer));
155 }
156
157 Vector2f pos = _stateBuffer[stateBufferPrevPos].Pos;
158
159 for (std::size_t i = 0; i < arraySize(_stateBuffer); i++) {
160 _stateBuffer[i].Time = now;
161 _stateBuffer[i].Pos = pos;
162 }
163 }
164 }
165 }
166
167 #endif