Skip to content

Commit 7f51e61

Browse files
committed
refactor frame advantage calculations
1 parent 24a7233 commit 7f51e61

3 files changed

Lines changed: 15 additions & 29 deletions

File tree

GekkoLib/include/backend.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,18 @@ namespace Gekko {
7979

8080
void SetLocalAdvantage(i8 adv);
8181

82-
void AddRemoteAdvantage(i8 adv);
82+
void SetRemoteAdvantage(i8 adv);
8383

8484
private:
85-
static const i32 HISTORY_SIZE = 26;
86-
87-
u8 _adv_index;
85+
static const i32 HISTORY_SIZE = 32;
8886

8987
i8 _local_frame_adv;
9088

89+
i8 _remote_frame_adv;
90+
9191
i8 _local[HISTORY_SIZE];
9292

9393
i8 _remote[HISTORY_SIZE];
94-
95-
i8 _remote_frame_adv[HISTORY_SIZE];
9694
};
9795

9896
class MessageSystem {

GekkoLib/src/backend.cpp

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ void Gekko::MessageSystem::AddInput(Frame input_frame, Handle player, u8 input[]
7474
input_q.last_added_input++;
7575
input_q.inputs.push_back(std::make_unique<u8[]>(_input_size));
7676
std::memcpy(input_q.inputs.back().get(), input, _input_size);
77-
78-
// update history // TODO REDO WITH BACKEND REWORK..
79-
if (!remote) {
80-
history.Update(input_frame);
81-
}
8277
}
8378

8479
// discard acked inputs (local) or just cap the queue (remote)
@@ -680,7 +675,7 @@ void Gekko::MessageSystem::OnInputAck(NetAddress& addr, NetPacket& pkt)
680675
player->stats.last_acked_frame = ack_frame;
681676
// only add remote advantages once
682677
if (!added_advantage && i == 0) {
683-
history.AddRemoteAdvantage(remote_advantage);
678+
history.SetRemoteAdvantage(remote_advantage);
684679
added_advantage = true;
685680
}
686681
}
@@ -890,27 +885,17 @@ void Gekko::MessageSystem::SendInputsToPeer(Player* peer, GekkoNetAdapter* host,
890885

891886
void Gekko::AdvantageHistory::Init()
892887
{
893-
_adv_index = 0;
894888
_local_frame_adv = 0;
895-
896-
std::memset(_remote_frame_adv, 0, HISTORY_SIZE * sizeof(i8));
889+
_remote_frame_adv = 0;
897890
std::memset(_local, 0, HISTORY_SIZE * sizeof(i8));
898891
std::memset(_remote, 0, HISTORY_SIZE * sizeof(i8));
899892
}
900893

901894
void Gekko::AdvantageHistory::Update(Frame frame)
902895
{
903896
const u32 update_frame = std::max(frame, 0);
904-
905897
_local[update_frame % HISTORY_SIZE] = _local_frame_adv;
906-
907-
i32 sum = 0;
908-
for (i8 num : _remote_frame_adv) {
909-
sum += num;
910-
}
911-
912-
sum /= HISTORY_SIZE;
913-
_remote[update_frame % HISTORY_SIZE] = sum;
898+
_remote[update_frame % HISTORY_SIZE] = _remote_frame_adv;
914899
}
915900

916901
f32 Gekko::AdvantageHistory::GetAverageAdvantage()
@@ -926,17 +911,16 @@ f32 Gekko::AdvantageHistory::GetAverageAdvantage()
926911
f32 avg_local = sum_local / HISTORY_SIZE;
927912
f32 avg_remote = sum_remote / HISTORY_SIZE;
928913

929-
// return the frames ahead
930-
return (avg_local - avg_remote);
914+
// return the frames ahead (halved: each peer corrects its share of the gap)
915+
return (avg_local - avg_remote) / 2.f;
931916
}
932917

933918
void Gekko::AdvantageHistory::SetLocalAdvantage(i8 adv) {
934919
_local_frame_adv = adv;
935920
}
936921

937-
void Gekko::AdvantageHistory::AddRemoteAdvantage(i8 adv) {
938-
_remote_frame_adv[_adv_index % HISTORY_SIZE] = adv;
939-
_adv_index++;
922+
void Gekko::AdvantageHistory::SetRemoteAdvantage(i8 adv) {
923+
_remote_frame_adv = adv;
940924
}
941925

942926
i8 Gekko::AdvantageHistory::GetLocalAdvantage() {

GekkoLib/src/game_session.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ void Gekko::GameSession::SendLocalInputs()
479479
return;
480480
}
481481
_msg.AddInput(frame, player->handle, input.get());
482+
}
483+
// Record advantage snapshot once per actual game frame
484+
if (frame == current) {
485+
_msg.history.Update(frame);
482486
}
483487
}
484488
}

0 commit comments

Comments
 (0)