Skip to content

Commit

Permalink
Add events listener to clear out invalid or stale CFeature* items of …
Browse files Browse the repository at this point in the history
…TestUnitBuildSquareCache immediately upon detection
  • Loading branch information
lhog committed Sep 28, 2023
1 parent 041e68b commit 3ef6ac5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
1 change: 1 addition & 0 deletions rts/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ CGame::~CGame()
ENTER_SYNCED_CODE();
LOG("[Game::%s][1]", __func__);

helper->Kill();
KillLua(true);
KillMisc();
KillRendering();
Expand Down
28 changes: 28 additions & 0 deletions rts/Game/GameHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ void CGameHelper::Init()
wdVec.clear();
wdVec.reserve(32);
}
syncedCacheListener = std::make_unique<TestUnitBuildSquareCacheEventsListener< true>>();
unsyncedCacheListener = std::make_unique<TestUnitBuildSquareCacheEventsListener<false>>();
}

void CGameHelper::Kill()
{
syncedCacheListener = nullptr;
unsyncedCacheListener = nullptr;
}

void CGameHelper::Update()
Expand Down Expand Up @@ -1587,3 +1595,23 @@ void CGameHelper::TestUnitBuildSquareCache::Invalidate(const KeyT& key)
return (key == item.key);
});
}

void CGameHelper::TestUnitBuildSquareCache::Invalidate(const CFeature* feature)
{
spring::VectorEraseAllIf(testUnitBuildSquareCache, [feature](const auto& item) {
return item.feature == feature;
});
}

template<bool synced>
CGameHelper::TestUnitBuildSquareCacheEventsListener<synced>::TestUnitBuildSquareCacheEventsListener()
: CEventClient("[TestUnitBuildSquareCacheEventsListener(" + std::to_string(synced) + ")]", 1000, synced)
{
eventHandler.AddClient(this);
}

template<bool synced>
CGameHelper::TestUnitBuildSquareCacheEventsListener<synced>::~TestUnitBuildSquareCacheEventsListener()
{
eventHandler.RemoveClient(this);
}
31 changes: 26 additions & 5 deletions rts/Game/GameHelper.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef GAME_HELPER_H
#define GAME_HELPER_H
#pragma once

#include "Sim/Misc/DamageArray.h"
#include "Sim/Projectiles/ExplosionListener.h"
#include "Sim/Units/CommandAI/Command.h"
#include "Sim/Misc/GlobalConstants.h"
#include "System/EventClient.h"
#include "System/float3.h"
#include "System/float4.h"
#include "System/type2.h"

#include <array>
#include <vector>
#include <memory>

class CUnit;
class CWeapon;
Expand Down Expand Up @@ -150,6 +151,7 @@ class CGameHelper
static size_t GenerateWeaponTargets(const CWeapon* weapon, const CUnit* avoidUnit, std::vector<std::pair<float, CUnit*>>& targets);

void Init();
void Kill();
void Update();

static float CalcImpulseScale(const DamageArray& damages, const float expDistanceMod);
Expand Down Expand Up @@ -199,6 +201,23 @@ class CGameHelper
DamageArray damage;
float3 impulse;
};
template<bool synced>
class TestUnitBuildSquareCacheEventsListener : public CEventClient {
public:
TestUnitBuildSquareCacheEventsListener();
~TestUnitBuildSquareCacheEventsListener() override;

bool WantsEvent(const std::string& eventName) override {
return
(eventName == "FeatureDestroyed") ||
(eventName == "FeatureMoved");
}
bool GetFullRead() const override { return synced; }
//int GetReadAllyTeam() const override { return AllAccessTeam; }

void FeatureDestroyed(const CFeature* feature) override { TestUnitBuildSquareCache::Invalidate(feature); }
void FeatureMoved(const CFeature* feature, const float3& oldpos) override { TestUnitBuildSquareCache::Invalidate(feature); }
};
struct TestUnitBuildSquareCache {
TestUnitBuildSquareCache(
int createFrame_,
Expand Down Expand Up @@ -261,6 +280,7 @@ class CGameHelper
);
}
static void Invalidate(const KeyT& key);
static void Invalidate(const CFeature* feature);

int createFrame;
std::tuple<bool, float3, int, int, const UnitDef*> key;
Expand All @@ -280,11 +300,12 @@ class CGameHelper

std::array<std::vector<WaitingDamage>, 128> waitingDamages;
static_assert (std::has_single_bit(std::tuple_size_v <decltype(waitingDamages)>), "Size is used in bit hax and must be 2^N");

std::unique_ptr<TestUnitBuildSquareCacheEventsListener< true>> syncedCacheListener;
std::unique_ptr<TestUnitBuildSquareCacheEventsListener<false>> unsyncedCacheListener;
public:
std::vector<int> targetUnitIDs; // GetEnemyUnits{NoLosTest}
std::vector<std::pair<float, CUnit*>> targetPairs; // GenerateWeaponTargets
};

extern CGameHelper* helper;

#endif // GAME_HELPER_H
extern CGameHelper* helper;

0 comments on commit 3ef6ac5

Please sign in to comment.