From 90a66348d755acb71e2b304694bda2db5e126357 Mon Sep 17 00:00:00 2001 From: a-nop <57344784+a-nop@users.noreply.github.com> Date: Fri, 5 Jun 2020 06:10:11 -0400 Subject: [PATCH] Fix floating units leaving tracks on sea floor (#530) * Fix floating units leaving tracks on sea floor This allows defining units which both leave tracks on ground and float on water, such as bots that swim on the surface. --- .../Env/Decals/LegacyTrackHandler.cpp | 52 +++++++++++-------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/rts/Rendering/Env/Decals/LegacyTrackHandler.cpp b/rts/Rendering/Env/Decals/LegacyTrackHandler.cpp index 35ae6da4a2..30f902e235 100644 --- a/rts/Rendering/Env/Decals/LegacyTrackHandler.cpp +++ b/rts/Rendering/Env/Decals/LegacyTrackHandler.cpp @@ -394,45 +394,54 @@ void LegacyTrackHandler::BindShader(const float3& ambientColor) } -void LegacyTrackHandler::AddTrack(CUnit* unit, const float3& newPos) +static bool CanReceiveTracks(const float3& pos) { - if (!unit->leaveTracks) - return; + // calculate typemap-index + const int tmz = pos.z / (SQUARE_SIZE * 2); + const int tmx = pos.x / (SQUARE_SIZE * 2); + const int tmi = Clamp(tmz * mapDims.hmapx + tmx, 0, mapDims.hmapx * mapDims.hmapy - 1); + + const uint8_t* typeMap = readMap->GetTypeMapSynced(); + const uint8_t typeNum = typeMap[tmi]; + return (mapInfo->terrainTypes[typeNum].receiveTracks); +} + +void LegacyTrackHandler::AddTrack(CUnit* unit, const float3& newPos) +{ const UnitDef* unitDef = unit->unitDef; const SolidObjectDecalDef& decalDef = unitDef->decalDef; + if (!unit->leaveTracks) + return; if (!unitDef->IsGroundUnit()) return; - - if (decalDef.trackDecalType < -1) + if (unit->IsInWater() && !unit->IsOnGround()) return; - if (decalDef.trackDecalType < 0) { - const_cast(decalDef).trackDecalType = GetTrackType(decalDef.trackDecalTypeName); - if (decalDef.trackDecalType < -1) - return; + + // -2 := failed to load texture, do not try again + // -1 := texture was not loaded yet, first attempt + switch (decalDef.trackDecalType) { + case -2: { return; } break; + case -1: { const_cast(decalDef).trackDecalType = GetTrackType(decalDef); } break; + default: { } break; } - if (unit->myTrack != NULL && unit->myTrack->lastUpdate >= (gs->frameNum - 7)) + if (decalDef.trackDecalType < 0) return; - if (!gu->spectatingFullView && (unit->losStatus[gu->myAllyTeam] & LOS_INLOS) == 0) - return; - // calculate typemap-index - const int tmz = newPos.z / (SQUARE_SIZE * 2); - const int tmx = newPos.x / (SQUARE_SIZE * 2); - const int tmi = Clamp(tmz * mapDims.hmapx + tmx, 0, mapDims.hmapx * mapDims.hmapy - 1); + if (unit->myTrack != nullptr && unit->myTrack->lastUpdate >= (gs->frameNum - 7)) + return; - const unsigned char* typeMap = readMap->GetTypeMapSynced(); - const CMapInfo::TerrainType& terType = mapInfo->terrainTypes[ typeMap[tmi] ]; + if (!gu->spectatingFullView && (unit->losStatus[gu->myAllyTeam] & LOS_INLOS) == 0) + return; - if (!terType.receiveTracks) + if (!CanReceiveTracks(newPos)) return; - static const int decalLevel = 3; //FIXME - const float trackLifeTime = GAME_SPEED * decalLevel * decalDef.trackDecalStrength; + const float trackLifeTime = GAME_SPEED * 3 * decalDef.trackDecalStrength; if (trackLifeTime <= 0.0f) return; @@ -565,3 +574,4 @@ void LegacyTrackHandler::RenderUnitDestroyed(const CUnit* unit) RemoveTrack(const_cast(unit)); } +