From 882ddae2012943e1f822bbe5e551840ae7448fde Mon Sep 17 00:00:00 2001 From: trml Date: Tue, 24 May 2022 23:53:40 +0200 Subject: [PATCH] Send switch timing info in extended switch state netobject --- datasrc/network.py | 14 +++--- src/game/client/gameclient.cpp | 55 ++++++++++------------- src/game/client/prediction/gameworld.cpp | 1 + src/game/server/entities/dragger_beam.cpp | 2 +- src/game/server/gamecontroller.cpp | 27 +++++------ 5 files changed, 41 insertions(+), 58 deletions(-) diff --git a/datasrc/network.py b/datasrc/network.py index aed855239..c6c641c57 100644 --- a/datasrc/network.py +++ b/datasrc/network.py @@ -312,15 +312,13 @@ Objects = [ # Switch state for a player team. NetObjectEx("SwitchState", "switch-state@netobj.ddnet.tw", [ - NetIntRange("m_HighestSwitchNumber", 0, 256), + NetIntAny("m_HighestSwitchNumber"), # 256 switches / 32 bits = 8 int32 - NetArray(NetIntAny("m_Status"), 8), - ]), - - NetObjectEx("SwitchTimeState", "switch-time-state@netobj.ddnet.tw", [ - NetArray(NetIntAny("m_SwitchNumber"), 4), - NetArray(NetIntAny("m_EndTick"), 4), - ]), + NetArray(NetIntAny("m_aStatus"), 8), + # send the endtick of up to 4 timed switchers + NetArray(NetIntAny("m_aSwitchNumbers"), 4), + NetArray(NetIntAny("m_aEndTicks"), 4), + ], validate_size=False), # Switch info for map items NetObjectEx("EntityEx", "entity-ex@netobj.ddnet.tw", [ diff --git a/src/game/client/gameclient.cpp b/src/game/client/gameclient.cpp index 36101c352..3d1a3e79f 100644 --- a/src/game/client/gameclient.cpp +++ b/src/game/client/gameclient.cpp @@ -1447,14 +1447,31 @@ void CGameClient::OnNewSnapshot() for(int j = 0; j < (int)Switchers().size(); j++) { - Switchers()[j].m_Status[Team] = (pSwitchStateData->m_Status[j / 32] >> (j % 32)) & 1; + Switchers()[j].m_Status[Team] = (pSwitchStateData->m_aStatus[j / 32] >> (j % 32)) & 1; + } - // update - if(Switchers()[j].m_Status[Team]) - Switchers()[j].m_Type[Team] = TILE_SWITCHOPEN; + const int SnapItemSize = Client()->SnapItemSize(IClient::SNAP_CURRENT, i); + if(SnapItemSize >= 68) + { + // update the endtick of up to four timed switchers + for(int j = 0; j < (int)std::size(pSwitchStateData->m_aEndTicks); j++) + { + int SwitchNumber = pSwitchStateData->m_aSwitchNumbers[j]; + int EndTick = pSwitchStateData->m_aEndTicks[j]; + if(EndTick > 0 && in_range(SwitchNumber, 0, (int)Switchers().size())) + { + Switchers()[SwitchNumber].m_EndTick[Team] = EndTick; + } + } + } + + // update switch types + for(auto &Switcher : Switchers()) + { + if(Switcher.m_Status[Team]) + Switcher.m_Type[Team] = Switcher.m_EndTick[Team] ? TILE_SWITCHTIMEDOPEN : TILE_SWITCHOPEN; else - Switchers()[j].m_Type[Team] = TILE_SWITCHCLOSE; - Switchers()[j].m_EndTick[Team] = 0; + Switcher.m_Type[Team] = Switcher.m_EndTick[Team] ? TILE_SWITCHTIMEDCLOSE : TILE_SWITCHCLOSE; } if(!GotSwitchStateTeam) @@ -2230,32 +2247,6 @@ void CGameClient::UpdatePrediction() m_GameWorld.m_WorldConfig.m_PredictWeapons = AntiPingWeapons(); m_GameWorld.m_WorldConfig.m_BugDDRaceInput = m_GameInfo.m_BugDDRaceInput; - int Num = Client()->SnapNumItems(IClient::SNAP_CURRENT); - for(int i = 0; i < Num; i++) - { - IClient::CSnapItem Item; - const void *pData = Client()->SnapGetItem(IClient::SNAP_CURRENT, i, &Item); - if(Item.m_Type == NETOBJTYPE_SWITCHTIMESTATE) - { - const CNetObj_SwitchTimeState *pSwitchTimeStateData = (const CNetObj_SwitchTimeState *)pData; - for(int j = 0; j < (int)(sizeof(pSwitchTimeStateData->m_EndTick) / sizeof(pSwitchTimeStateData->m_EndTick[0])); j++) - { - int Team = clamp(Item.m_ID, (int)TEAM_FLOCK, (int)TEAM_SUPER - 1); - int SwitchNum = pSwitchTimeStateData->m_SwitchNumber[j]; - int EndTick = pSwitchTimeStateData->m_EndTick[j]; - if(in_range(SwitchNum, 0, (int)Switchers().size()) && EndTick > 0) - { - // update EndTick - if(Switchers()[SwitchNum].m_Status[Team]) - Switchers()[SwitchNum].m_Type[Team] = TILE_SWITCHTIMEDOPEN; - else - Switchers()[SwitchNum].m_Type[Team] = TILE_SWITCHTIMEDCLOSE; - Switchers()[SwitchNum].m_EndTick[Team] = EndTick; - } - } - } - } - // always update default tune zone, even without character if(!m_GameWorld.m_WorldConfig.m_UseTuneZones) m_GameWorld.TuningList()[0] = m_Tuning[g_Config.m_ClDummy]; diff --git a/src/game/client/prediction/gameworld.cpp b/src/game/client/prediction/gameworld.cpp index 00065f425..f27db8571 100644 --- a/src/game/client/prediction/gameworld.cpp +++ b/src/game/client/prediction/gameworld.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include ////////////////////////////////////////////////// diff --git a/src/game/server/entities/dragger_beam.cpp b/src/game/server/entities/dragger_beam.cpp index c746f1281..1c87c86d7 100644 --- a/src/game/server/entities/dragger_beam.cpp +++ b/src/game/server/entities/dragger_beam.cpp @@ -46,7 +46,7 @@ void CDraggerBeam::Tick() if(Server()->Tick() % int(Server()->TickSpeed() * 0.15f) == 0) { if(m_Layer == LAYER_SWITCH && m_Number > 0 && - !GameServer()->Collision()->m_pSwitchers[m_Number].m_Status[pTarget->Team()]) + !Switchers()[m_Number].m_Status[pTarget->Team()]) { Reset(); return; diff --git a/src/game/server/gamecontroller.cpp b/src/game/server/gamecontroller.cpp index cfaa2b09c..1f8c316ac 100644 --- a/src/game/server/gamecontroller.cpp +++ b/src/game/server/gamecontroller.cpp @@ -647,14 +647,14 @@ void IGameController::Snap(int SnappingClient) return; pSwitchState->m_HighestSwitchNumber = clamp((int)GameServer()->Switchers().size() - 1, 0, 255); - mem_zero(pSwitchState->m_Status, sizeof(pSwitchState->m_Status)); + mem_zero(pSwitchState->m_aStatus, sizeof(pSwitchState->m_aStatus)); std::vector> aEndTicks; // for(int i = 0; i <= pSwitchState->m_HighestSwitchNumber; i++) { int Status = (int)GameServer()->Switchers()[i].m_Status[Team]; - pSwitchState->m_Status[i / 32] |= (Status << (i % 32)); + pSwitchState->m_aStatus[i / 32] |= (Status << (i % 32)); int EndTick = GameServer()->Switchers()[i].m_EndTick[Team]; if(EndTick > 0 && EndTick < Server()->Tick() + 3 * Server()->TickSpeed() && GameServer()->Switchers()[i].m_LastUpdateTick[Team] < Server()->Tick()) @@ -664,24 +664,17 @@ void IGameController::Snap(int SnappingClient) } } - // send the endtick of switchers that are about to toggle back (up to some number, prioritizing those with the earliest endticks) - CNetObj_SwitchTimeState *pSwitchTimeState = static_cast(Server()->SnapNewItem(NETOBJTYPE_SWITCHTIMESTATE, Team, sizeof(CNetObj_SwitchTimeState))); - if(!pSwitchTimeState) - return; + // send the endtick of switchers that are about to toggle back (up to four, prioritizing those with the earliest endticks) + mem_zero(pSwitchState->m_aSwitchNumbers, sizeof(pSwitchState->m_aSwitchNumbers)); + mem_zero(pSwitchState->m_aEndTicks, sizeof(pSwitchState->m_aEndTicks)); std::sort(aEndTicks.begin(), aEndTicks.end()); - for(int i = 0; i < (int)(sizeof(pSwitchTimeState->m_EndTick) / sizeof(pSwitchTimeState->m_EndTick[0])); i++) + const int NumTimedSwitchers = minimum((int)aEndTicks.size(), (int)std::size(pSwitchState->m_aEndTicks)); + + for(int i = 0; i < NumTimedSwitchers; i++) { - if(i < (int)aEndTicks.size()) - { - pSwitchTimeState->m_EndTick[i] = aEndTicks[i].first; - pSwitchTimeState->m_SwitchNumber[i] = aEndTicks[i].second; - } - else - { - pSwitchTimeState->m_SwitchNumber[i] = -1; - pSwitchTimeState->m_EndTick[i] = 0; - } + pSwitchState->m_aSwitchNumbers[i] = aEndTicks[i].second; + pSwitchState->m_aEndTicks[i] = aEndTicks[i].first; } } }