From b991a44b405d5ecab1f0eb7de1e94ba5595a1dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 14 May 2023 17:06:09 +0200 Subject: [PATCH 1/3] Rename variables `l`/`g` to `LayerIndex`/`GroupIndex` --- src/game/layers.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/game/layers.cpp b/src/game/layers.cpp index 52f97e079..f724a32a4 100644 --- a/src/game/layers.cpp +++ b/src/game/layers.cpp @@ -19,12 +19,12 @@ void CLayers::Init(class IKernel *pKernel) m_pMap->GetType(MAPITEMTYPE_GROUP, &m_GroupsStart, &m_GroupsNum); m_pMap->GetType(MAPITEMTYPE_LAYER, &m_LayersStart, &m_LayersNum); - for(int g = 0; g < NumGroups(); g++) + for(int GroupIndex = 0; GroupIndex < NumGroups(); GroupIndex++) { - CMapItemGroup *pGroup = GetGroup(g); - for(int l = 0; l < pGroup->m_NumLayers; l++) + CMapItemGroup *pGroup = GetGroup(GroupIndex); + for(int LayerIndex = 0; LayerIndex < pGroup->m_NumLayers; LayerIndex++) { - CMapItemLayer *pLayer = GetLayer(pGroup->m_StartLayer + l); + CMapItemLayer *pLayer = GetLayer(pGroup->m_StartLayer + LayerIndex); if(pLayer->m_Type == LAYERTYPE_TILES) { @@ -119,12 +119,12 @@ void CLayers::InitBackground(class IMap *pMap) m_pMap->GetType(MAPITEMTYPE_GROUP, &m_GroupsStart, &m_GroupsNum); m_pMap->GetType(MAPITEMTYPE_LAYER, &m_LayersStart, &m_LayersNum); - for(int g = 0; g < NumGroups(); g++) + for(int GroupIndex = 0; GroupIndex < NumGroups(); GroupIndex++) { - CMapItemGroup *pGroup = GetGroup(g); - for(int l = 0; l < pGroup->m_NumLayers; l++) + CMapItemGroup *pGroup = GetGroup(GroupIndex); + for(int LayerIndex = 0; LayerIndex < pGroup->m_NumLayers; LayerIndex++) { - CMapItemLayer *pLayer = GetLayer(pGroup->m_StartLayer + l); + CMapItemLayer *pLayer = GetLayer(pGroup->m_StartLayer + LayerIndex); if(pLayer->m_Type == LAYERTYPE_TILES) { @@ -178,13 +178,13 @@ void CLayers::Unload() void CLayers::InitTilemapSkip() { - for(int g = 0; g < NumGroups(); g++) + for(int GroupIndex = 0; GroupIndex < NumGroups(); GroupIndex++) { - const CMapItemGroup *pGroup = GetGroup(g); + const CMapItemGroup *pGroup = GetGroup(GroupIndex); - for(int l = 0; l < pGroup->m_NumLayers; l++) + for(int LayerIndex = 0; LayerIndex < pGroup->m_NumLayers; LayerIndex++) { - const CMapItemLayer *pLayer = GetLayer(pGroup->m_StartLayer + l); + const CMapItemLayer *pLayer = GetLayer(pGroup->m_StartLayer + LayerIndex); if(pLayer->m_Type == LAYERTYPE_TILES) { From e0461f4c2112ec2381b7d7fbb26f8aed8756f522 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 14 May 2023 17:16:58 +0200 Subject: [PATCH 2/3] Combine `CLayers::Init` and `CLayers::InitBackground` functions The `CLayers::InitBackground` function implemented a subset of the `CLayers::Init` function, only loading the game layers and not the other entities layers, so a Boolean parameter can be used to reduce the duplicate code. --- src/game/client/components/background.cpp | 2 +- .../client/components/menu_background.cpp | 2 +- src/game/client/gameclient.cpp | 2 +- src/game/layers.cpp | 204 +++++++----------- src/game/layers.h | 4 +- src/game/server/gamecontext.cpp | 2 +- 6 files changed, 87 insertions(+), 129 deletions(-) diff --git a/src/game/client/components/background.cpp b/src/game/client/components/background.cpp index 1d7c41c61..d0acd143e 100644 --- a/src/game/client/components/background.cpp +++ b/src/game/client/components/background.cpp @@ -73,7 +73,7 @@ void CBackground::LoadBackground() } else if(m_pMap->Load(aBuf)) { - m_pLayers->InitBackground(m_pMap); + m_pLayers->Init(m_pMap, true); NeedImageLoading = true; m_Loaded = true; } diff --git a/src/game/client/components/menu_background.cpp b/src/game/client/components/menu_background.cpp index a92f271f5..aa63454e8 100644 --- a/src/game/client/components/menu_background.cpp +++ b/src/game/client/components/menu_background.cpp @@ -254,7 +254,7 @@ void CMenuBackground::LoadMenuBackground(bool HasDayHint, bool HasNightHint) if(m_Loaded) { - m_pLayers->InitBackground(m_pMap); + m_pLayers->Init(m_pMap, true); CMapLayers::OnMapLoad(); m_pImages->LoadBackground(m_pLayers, m_pMap); diff --git a/src/game/client/gameclient.cpp b/src/game/client/gameclient.cpp index 4f811177a..23158a841 100644 --- a/src/game/client/gameclient.cpp +++ b/src/game/client/gameclient.cpp @@ -532,7 +532,7 @@ void CGameClient::OnConnected() const char *pLoadMapContent = Localize("Initializing map logic"); // render loading before skip is calculated m_Menus.RenderLoading(pConnectCaption, pLoadMapContent, 0, false); - m_Layers.Init(Kernel()); + m_Layers.Init(Kernel()->RequestInterface(), false); m_Collision.Init(Layers()); m_GameWorld.m_Core.InitSwitchers(m_Collision.m_HighestSwitchNumber); m_RaceHelper.Init(this); diff --git a/src/game/layers.cpp b/src/game/layers.cpp index f724a32a4..a19e24872 100644 --- a/src/game/layers.cpp +++ b/src/game/layers.cpp @@ -11,107 +11,7 @@ CLayers::CLayers() Unload(); } -void CLayers::Init(class IKernel *pKernel) -{ - Unload(); - - m_pMap = pKernel->RequestInterface(); - m_pMap->GetType(MAPITEMTYPE_GROUP, &m_GroupsStart, &m_GroupsNum); - m_pMap->GetType(MAPITEMTYPE_LAYER, &m_LayersStart, &m_LayersNum); - - for(int GroupIndex = 0; GroupIndex < NumGroups(); GroupIndex++) - { - CMapItemGroup *pGroup = GetGroup(GroupIndex); - for(int LayerIndex = 0; LayerIndex < pGroup->m_NumLayers; LayerIndex++) - { - CMapItemLayer *pLayer = GetLayer(pGroup->m_StartLayer + LayerIndex); - - if(pLayer->m_Type == LAYERTYPE_TILES) - { - CMapItemLayerTilemap *pTilemap = reinterpret_cast(pLayer); - bool IsEntities = false; - - if(pTilemap->m_Flags & TILESLAYERFLAG_GAME) - { - m_pGameLayer = pTilemap; - m_pGameGroup = pGroup; - - // make sure the game group has standard settings - m_pGameGroup->m_OffsetX = 0; - m_pGameGroup->m_OffsetY = 0; - m_pGameGroup->m_ParallaxX = 100; - m_pGameGroup->m_ParallaxY = 100; - - if(m_pGameGroup->m_Version >= 2) - { - m_pGameGroup->m_UseClipping = 0; - m_pGameGroup->m_ClipX = 0; - m_pGameGroup->m_ClipY = 0; - m_pGameGroup->m_ClipW = 0; - m_pGameGroup->m_ClipH = 0; - } - - IsEntities = true; - //break; - } - if(pTilemap->m_Flags & TILESLAYERFLAG_TELE) - { - if(pTilemap->m_Version <= 2) - { - pTilemap->m_Tele = *((int *)(pTilemap) + 15); - } - m_pTeleLayer = pTilemap; - IsEntities = true; - } - if(pTilemap->m_Flags & TILESLAYERFLAG_SPEEDUP) - { - if(pTilemap->m_Version <= 2) - { - pTilemap->m_Speedup = *((int *)(pTilemap) + 16); - } - m_pSpeedupLayer = pTilemap; - IsEntities = true; - } - if(pTilemap->m_Flags & TILESLAYERFLAG_FRONT) - { - if(pTilemap->m_Version <= 2) - { - pTilemap->m_Front = *((int *)(pTilemap) + 17); - } - m_pFrontLayer = pTilemap; - IsEntities = true; - } - if(pTilemap->m_Flags & TILESLAYERFLAG_SWITCH) - { - if(pTilemap->m_Version <= 2) - { - pTilemap->m_Switch = *((int *)(pTilemap) + 18); - } - m_pSwitchLayer = pTilemap; - IsEntities = true; - } - if(pTilemap->m_Flags & TILESLAYERFLAG_TUNE) - { - if(pTilemap->m_Version <= 2) - { - pTilemap->m_Tune = *((int *)(pTilemap) + 19); - } - m_pTuneLayer = pTilemap; - IsEntities = true; - } - - if(IsEntities) - { // Ensure default color for entities layers - pTilemap->m_Color = CColor(255, 255, 255, 255); - } - } - } - } - - InitTilemapSkip(); -} - -void CLayers::InitBackground(class IMap *pMap) +void CLayers::Init(IMap *pMap, bool GameOnly) { Unload(); @@ -125,32 +25,92 @@ void CLayers::InitBackground(class IMap *pMap) for(int LayerIndex = 0; LayerIndex < pGroup->m_NumLayers; LayerIndex++) { CMapItemLayer *pLayer = GetLayer(pGroup->m_StartLayer + LayerIndex); + if(pLayer->m_Type != LAYERTYPE_TILES) + continue; - if(pLayer->m_Type == LAYERTYPE_TILES) + CMapItemLayerTilemap *pTilemap = reinterpret_cast(pLayer); + bool IsEntities = false; + + if(pTilemap->m_Flags & TILESLAYERFLAG_GAME) { - CMapItemLayerTilemap *pTilemap = reinterpret_cast(pLayer); + m_pGameLayer = pTilemap; + m_pGameGroup = pGroup; - if(pTilemap->m_Flags & TILESLAYERFLAG_GAME) + // make sure the game group has standard settings + m_pGameGroup->m_OffsetX = 0; + m_pGameGroup->m_OffsetY = 0; + m_pGameGroup->m_ParallaxX = 100; + m_pGameGroup->m_ParallaxY = 100; + + if(m_pGameGroup->m_Version >= 2) { - m_pGameLayer = pTilemap; - m_pGameGroup = pGroup; - - // make sure the game group has standard settings - m_pGameGroup->m_OffsetX = 0; - m_pGameGroup->m_OffsetY = 0; - m_pGameGroup->m_ParallaxX = 100; - m_pGameGroup->m_ParallaxY = 100; - - if(m_pGameGroup->m_Version >= 2) - { - m_pGameGroup->m_UseClipping = 0; - m_pGameGroup->m_ClipX = 0; - m_pGameGroup->m_ClipY = 0; - m_pGameGroup->m_ClipW = 0; - m_pGameGroup->m_ClipH = 0; - } - //We don't care about tile layers. + m_pGameGroup->m_UseClipping = 0; + m_pGameGroup->m_ClipX = 0; + m_pGameGroup->m_ClipY = 0; + m_pGameGroup->m_ClipW = 0; + m_pGameGroup->m_ClipH = 0; } + + IsEntities = true; + } + + if(!GameOnly) + { + if(pTilemap->m_Flags & TILESLAYERFLAG_TELE) + { + if(pTilemap->m_Version <= 2) + { + pTilemap->m_Tele = *((int *)(pTilemap) + 15); + } + m_pTeleLayer = pTilemap; + IsEntities = true; + } + + if(pTilemap->m_Flags & TILESLAYERFLAG_SPEEDUP) + { + if(pTilemap->m_Version <= 2) + { + pTilemap->m_Speedup = *((int *)(pTilemap) + 16); + } + m_pSpeedupLayer = pTilemap; + IsEntities = true; + } + + if(pTilemap->m_Flags & TILESLAYERFLAG_FRONT) + { + if(pTilemap->m_Version <= 2) + { + pTilemap->m_Front = *((int *)(pTilemap) + 17); + } + m_pFrontLayer = pTilemap; + IsEntities = true; + } + + if(pTilemap->m_Flags & TILESLAYERFLAG_SWITCH) + { + if(pTilemap->m_Version <= 2) + { + pTilemap->m_Switch = *((int *)(pTilemap) + 18); + } + m_pSwitchLayer = pTilemap; + IsEntities = true; + } + + if(pTilemap->m_Flags & TILESLAYERFLAG_TUNE) + { + if(pTilemap->m_Version <= 2) + { + pTilemap->m_Tune = *((int *)(pTilemap) + 19); + } + m_pTuneLayer = pTilemap; + IsEntities = true; + } + } + + if(IsEntities) + { + // Ensure default color for entities layers + pTilemap->m_Color = CColor(255, 255, 255, 255); } } } diff --git a/src/game/layers.h b/src/game/layers.h index 2ce957cc1..cc93a55ed 100644 --- a/src/game/layers.h +++ b/src/game/layers.h @@ -3,7 +3,6 @@ #ifndef GAME_LAYERS_H #define GAME_LAYERS_H -class IKernel; class IMap; struct CMapItemGroup; @@ -14,8 +13,7 @@ class CLayers { public: CLayers(); - void Init(IKernel *pKernel); - void InitBackground(IMap *pMap); + void Init(IMap *pMap, bool GameOnly); void Unload(); int NumGroups() const { return m_GroupsNum; } diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp index 6d9537f0b..092a19dc9 100644 --- a/src/game/server/gamecontext.cpp +++ b/src/game/server/gamecontext.cpp @@ -3862,7 +3862,7 @@ void CGameContext::OnInit(const void *pPersistentData) for(int i = 0; i < NUM_NETOBJTYPES; i++) Server()->SnapSetStaticsize(i, m_NetObjHandler.GetObjSize(i)); - m_Layers.Init(Kernel()); + m_Layers.Init(Kernel()->RequestInterface(), false); m_Collision.Init(&m_Layers); m_World.m_pTuningList = m_aTuningList; m_World.m_Core.InitSwitchers(m_Collision.m_HighestSwitchNumber); From 7ac0a8f6c6f812fced20424aefe5399f4055bd89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Mon, 15 May 2023 17:34:16 +0200 Subject: [PATCH 3/3] Reduce indent, avoid C style casts in `CLayers::InitTilemapSkip` --- src/game/layers.cpp | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/game/layers.cpp b/src/game/layers.cpp index a19e24872..a5a3f6a32 100644 --- a/src/game/layers.cpp +++ b/src/game/layers.cpp @@ -141,29 +141,27 @@ void CLayers::InitTilemapSkip() for(int GroupIndex = 0; GroupIndex < NumGroups(); GroupIndex++) { const CMapItemGroup *pGroup = GetGroup(GroupIndex); - for(int LayerIndex = 0; LayerIndex < pGroup->m_NumLayers; LayerIndex++) { const CMapItemLayer *pLayer = GetLayer(pGroup->m_StartLayer + LayerIndex); + if(pLayer->m_Type != LAYERTYPE_TILES) + continue; - if(pLayer->m_Type == LAYERTYPE_TILES) + const CMapItemLayerTilemap *pTilemap = reinterpret_cast(pLayer); + CTile *pTiles = static_cast(m_pMap->GetData(pTilemap->m_Data)); + for(int y = 0; y < pTilemap->m_Height; y++) { - const CMapItemLayerTilemap *pTilemap = (CMapItemLayerTilemap *)pLayer; - CTile *pTiles = (CTile *)m_pMap->GetData(pTilemap->m_Data); - for(int y = 0; y < pTilemap->m_Height; y++) + for(int x = 1; x < pTilemap->m_Width;) { - for(int x = 1; x < pTilemap->m_Width;) + int SkippedX; + for(SkippedX = 1; x + SkippedX < pTilemap->m_Width && SkippedX < 255; SkippedX++) { - int SkippedX; - for(SkippedX = 1; x + SkippedX < pTilemap->m_Width && SkippedX < 255; SkippedX++) - { - if(pTiles[y * pTilemap->m_Width + x + SkippedX].m_Index) - break; - } - - pTiles[y * pTilemap->m_Width + x].m_Skip = SkippedX - 1; - x += SkippedX; + if(pTiles[y * pTilemap->m_Width + x + SkippedX].m_Index) + break; } + + pTiles[y * pTilemap->m_Width + x].m_Skip = SkippedX - 1; + x += SkippedX; } } }