Permalink
Browse files

nlc

  • Loading branch information...
rt
rt committed Dec 2, 2018
1 parent 45379cf commit 0f9ac7b77cfc61373841783e8ca115c46a3da6d0
@@ -1443,10 +1443,8 @@ EXPORT(bool) skirmishAiCallback_Cheats_isEnabled(int skirmishAIId) {
EXPORT(bool) skirmishAiCallback_Cheats_setEnabled(int skirmishAIId, bool enabled)
{
AI_CHEAT_FLAGS[skirmishAIId].first = enabled;
if (enabled && !AI_CHEAT_FLAGS[skirmishAIId].second) {
LOG("SkirmishAI (ID = %i, team ID = %i) is using cheats!", skirmishAIId, AI_TEAM_IDS[skirmishAIId]);
if ((AI_CHEAT_FLAGS[skirmishAIId].first = enabled) && !AI_CHEAT_FLAGS[skirmishAIId].second) {
LOG("[%s] SkirmishAI (id %i, team %i) is using cheats!", __func__, skirmishAIId, AI_TEAM_IDS[skirmishAIId]);
AI_CHEAT_FLAGS[skirmishAIId].second = true;
}
@@ -725,13 +725,16 @@ void CGame::LoadSkirmishAIs()
if (gameSetup->hostDemo)
return;
// create a Skirmish AI if required
// create Skirmish AI's if required
const std::vector<uint8_t>& localAIs = skirmishAIHandler.GetSkirmishAIsByPlayer(gu->myPlayerNum);
const std::string timerName = std::string("Game::") + __func__;
if (!localAIs.empty()) {
ScopedOnceTimer timer(timerName);
loadscreen->SetLoadMessage("Loading Skirmish AIs");
for (auto& localAI: localAIs) {
for (uint8_t localAI: localAIs) {
ScopedOnceTimer subTimer(timerName + "::CreateAI(id=" + IntToString(localAI) + ")");
skirmishAIHandler.CreateLocalSkirmishAI(localAI);
}
}
@@ -309,7 +309,7 @@ void CLoadScreen::SetLoadMessage(const std::string& text, bool replaceLast)
loadMessages.emplace_back(text, replaceLast);
LOG("%s", text.c_str());
LOG("[LoadScreen::%s] text=\"%s\"", __func__, text.c_str());
LOG_CLEANUP();
// be paranoid about FPU state for the loading thread since some
@@ -118,6 +118,8 @@ void CQuadField::Init(int2 mapDims, int quadSize)
assert((mapDims.y * SQUARE_SIZE) % quadSize == 0);
baseQuads.resize(numQuadsX * numQuadsZ);
tempQuads.ReserveAll(numQuadsX * numQuadsZ);
tempQuads.ReleaseAll();
#ifndef UNIT_TEST
for (Quad& quad: baseQuads) {
@@ -162,7 +164,7 @@ void CQuadField::GetQuads(QuadFieldQuery& qfq, float3 pos, float radius)
{
pos.AssertNaNs();
pos.ClampInBounds();
qfq.quads = tempQuads.GetVector();
qfq.quads = tempQuads.ReserveVector();
const int2 min = WorldPosToQuadField(pos - radius);
const int2 max = WorldPosToQuadField(pos + radius);
@@ -192,7 +194,7 @@ void CQuadField::GetQuadsRectangle(QuadFieldQuery& qfq, const float3& mins, cons
{
mins.AssertNaNs();
maxs.AssertNaNs();
qfq.quads = tempQuads.GetVector();
qfq.quads = tempQuads.ReserveVector();
const int2 min = WorldPosToQuadField(mins);
const int2 max = WorldPosToQuadField(maxs);
@@ -218,7 +220,7 @@ void CQuadField::GetQuadsOnRay(QuadFieldQuery& qfq, const float3& start, const f
{
dir.AssertNaNs();
start.AssertNaNs();
qfq.quads = tempQuads.GetVector();
qfq.quads = tempQuads.ReserveVector();
const float3 to = start + (dir * length);
const float3 invQuadSize = float3(1.0f / quadSizeX, 1.0f, 1.0f / quadSizeZ);
@@ -465,7 +467,7 @@ void CQuadField::GetUnits(QuadFieldQuery& qfq, const float3& pos, float radius)
QuadFieldQuery qfQuery;
GetQuads(qfQuery, pos, radius);
const int tempNum = gs->GetTempNum();
qfq.units = tempUnits.GetVector();
qfq.units = tempUnits.ReserveVector();
for (const int qi: *qfQuery.quads) {
for (CUnit* u: baseQuads[qi].units) {
@@ -485,7 +487,7 @@ void CQuadField::GetUnitsExact(QuadFieldQuery& qfq, const float3& pos, float rad
QuadFieldQuery qfQuery;
GetQuads(qfQuery, pos, radius);
const int tempNum = gs->GetTempNum();
qfq.units = tempUnits.GetVector();
qfq.units = tempUnits.ReserveVector();
for (const int qi: *qfQuery.quads) {
for (CUnit* u: baseQuads[qi].units) {
@@ -515,7 +517,7 @@ void CQuadField::GetUnitsExact(QuadFieldQuery& qfq, const float3& mins, const fl
QuadFieldQuery qfQuery;
GetQuadsRectangle(qfQuery, mins, maxs);
const int tempNum = gs->GetTempNum();
qfq.units = tempUnits.GetVector();
qfq.units = tempUnits.ReserveVector();
for (const int qi: *qfQuery.quads) {
for (CUnit* unit: baseQuads[qi].units) {
@@ -544,7 +546,7 @@ void CQuadField::GetFeaturesExact(QuadFieldQuery& qfq, const float3& pos, float
QuadFieldQuery qfQuery;
GetQuads(qfQuery, pos, radius);
const int tempNum = gs->GetTempNum();
qfq.features = tempFeatures.GetVector();
qfq.features = tempFeatures.ReserveVector();
for (const int qi: *qfQuery.quads) {
for (CFeature* f: baseQuads[qi].features) {
@@ -574,7 +576,7 @@ void CQuadField::GetFeaturesExact(QuadFieldQuery& qfq, const float3& mins, const
QuadFieldQuery qfQuery;
GetQuadsRectangle(qfQuery, mins, maxs);
const int tempNum = gs->GetTempNum();
qfq.features = tempFeatures.GetVector();
qfq.features = tempFeatures.ReserveVector();
for (const int qi: *qfQuery.quads) {
for (CFeature* feature: baseQuads[qi].features) {
@@ -603,7 +605,7 @@ void CQuadField::GetProjectilesExact(QuadFieldQuery& qfq, const float3& pos, flo
QuadFieldQuery qfQuery;
GetQuads(qfQuery, pos, radius);
const int tempNum = gs->GetTempNum();
qfq.projectiles = tempProjectiles.GetVector();
qfq.projectiles = tempProjectiles.ReserveVector();
for (const int qi: *qfQuery.quads) {
for (CProjectile* p: baseQuads[qi].projectiles) {
@@ -627,7 +629,7 @@ void CQuadField::GetProjectilesExact(QuadFieldQuery& qfq, const float3& mins, co
QuadFieldQuery qfQuery;
GetQuadsRectangle(qfQuery, mins, maxs);
const int tempNum = gs->GetTempNum();
qfq.projectiles = tempProjectiles.GetVector();
qfq.projectiles = tempProjectiles.ReserveVector();
for (const int qi: *qfQuery.quads) {
for (CProjectile* p: baseQuads[qi].projectiles) {
@@ -661,7 +663,7 @@ void CQuadField::GetSolidsExact(
QuadFieldQuery qfQuery;
GetQuads(qfQuery, pos, radius);
const int tempNum = gs->GetTempNum();
qfq.solids = tempSolids.GetVector();
qfq.solids = tempSolids.ReserveVector();
for (const int qi: *qfQuery.quads) {
for (CUnit* u: baseQuads[qi].units) {
@@ -20,44 +20,36 @@ class CPlasmaRepulser;
struct QuadFieldQuery;
template<typename T>
class ExclusiveVectors {
class QueryVectorCache {
public:
// There should at most be 2 concurrent users of each vector type
// using 3 to be safe, increase this number if the assertions below
// fail
static constexpr int MAX_CONCURRENT_VECTORS = 3;
ExclusiveVectors() {
for (auto& pair: vectors) {
pair.first = false;
}
}
typedef std::pair<bool, std::vector<T>> PairType;
std::vector<T>* GetVector() {
const auto pred = [](const std::pair<bool, std::vector<T>>& p) { return (!p.first); };
std::vector<T>* ReserveVector(size_t capa = 1024) {
const auto pred = [](const PairType& p) { return (!p.first); };
const auto iter = std::find_if(vectors.begin(), vectors.end(), pred);
if (iter != vectors.end()) {
iter->first = true;
iter->second.clear();
iter->second.reserve(capa);
return &iter->second;
}
assert(false);
return nullptr;
}
void ReleaseAll() {
void ReserveAll(size_t capa) {
for (auto& pair: vectors) {
pair.first = false;
pair.second.clear();
ReserveVector(capa);
}
}
void ReleaseVector(std::vector<T>* released) {
void ReleaseVector(const std::vector<T>* released) {
if (released == nullptr)
return;
const auto pred = [&](const std::pair<bool, std::vector<T>>& p) { return (&p.second == released); };
const auto pred = [&](const PairType& p) { return (&p.second == released); };
const auto iter = std::find_if(vectors.begin(), vectors.end(), pred);
if (iter == vectors.end()) {
@@ -67,8 +59,16 @@ class ExclusiveVectors {
iter->first = false;
}
std::array<std::pair<bool, std::vector<T>>, MAX_CONCURRENT_VECTORS> vectors;
void ReleaseAll() {
for (auto& pair: vectors) {
ReleaseVector(&pair.second);
}
}
private:
// There should at most be 2 concurrent users of each vector type
// using 3 to be safe, increase this number if the assertions below
// fail
std::array<PairType, 3> vectors = {{{false, {}}, {false, {}}, {false, {}}}};
};
@@ -228,26 +228,18 @@ class CQuadField : spring::noncopyable
constexpr static unsigned int BASE_QUAD_SIZE = 128;
private:
// optimized functions, somewhat less userfriendly
//
// when calling these, <begQuad> and <endQuad> are both expected
// to point to the *start* of an array of int's of size at least
// numQuadsX * numQuadsZ (eg. tempQuads) -- GetQuadsOnRay ensures
// this by itself, for GetQuads the callers take care of it
//
int2 WorldPosToQuadField(const float3 p) const;
int WorldPosToQuadFieldIdx(const float3 p) const;
private:
std::vector<Quad> baseQuads;
// preallocated vectors for Get*Exact functions
ExclusiveVectors<CUnit*> tempUnits;
ExclusiveVectors<CFeature*> tempFeatures;
ExclusiveVectors<CProjectile*> tempProjectiles;
ExclusiveVectors<CSolidObject*> tempSolids;
ExclusiveVectors<int> tempQuads;
QueryVectorCache<CUnit*> tempUnits;
QueryVectorCache<CFeature*> tempFeatures;
QueryVectorCache<CProjectile*> tempProjectiles;
QueryVectorCache<CSolidObject*> tempSolids;
QueryVectorCache<int> tempQuads;
int numQuadsX;
int numQuadsZ;
@@ -169,8 +169,8 @@ void CPathEstimator::InitEstimator(const std::string& cacheFileName, const std::
const char* fmtStrs[4] = {
"[%s] creating PE%u cache with %u PF threads (%u MB)",
"[%s] creating PE%u cache with %u PF thread (%u MB)",
"[%s] writing PE%u cache to file %s",
"[%s] written PE%u cache to file %s",
"[%s] writing PE%u cache-file %s-%x",
"[%s] written PE%u cache-file %s-%x",
};
{
@@ -196,12 +196,12 @@ void CPathEstimator::InitEstimator(const std::string& cacheFileName, const std::
}
sprintf(calcMsg, fmtStrs[2], __func__, BLOCK_SIZE, cacheFileName.c_str());
sprintf(calcMsg, fmtStrs[2], __func__, BLOCK_SIZE, cacheFileName.c_str(), fileHashCode);
loadscreen->SetLoadMessage(calcMsg, true);
WriteFile(cacheFileName, mapName);
sprintf(calcMsg, fmtStrs[3], __func__, BLOCK_SIZE, cacheFileName.c_str());
sprintf(calcMsg, fmtStrs[3], __func__, BLOCK_SIZE, cacheFileName.c_str(), fileHashCode);
loadscreen->SetLoadMessage(calcMsg, true);
}
@@ -56,7 +56,7 @@ CDemoRecorder::~CDemoRecorder()
void CDemoRecorder::SetStream()
{
demoStreams[isServerDemo].clear();
demoStreams[isServerDemo].reserve(5 * 1024 * 1024);
demoStreams[isServerDemo].reserve(8 * 1024 * 1024);
}
void CDemoRecorder::SetFileHeader()
@@ -120,21 +120,27 @@ ScopedTimer::~ScopedTimer()
ScopedOnceTimer::ScopedOnceTimer(const char* timerName)
: startTime(spring_gettime())
, name(timerName)
ScopedOnceTimer::ScopedOnceTimer(const char* timerName, const char* timerFrmt): startTime(spring_gettime())
{
strncpy(name, timerName, sizeof(name));
strncpy(frmt, timerFrmt, sizeof(frmt));
name[sizeof(name) - 1] = 0;
frmt[sizeof(frmt) - 1] = 0;
}
ScopedOnceTimer::ScopedOnceTimer(const std::string& timerName)
: startTime(spring_gettime())
, name(timerName)
ScopedOnceTimer::ScopedOnceTimer(const std::string& timerName, const char* timerFrmt): startTime(spring_gettime())
{
strncpy(name, timerName.c_str(), sizeof(name));
strncpy(frmt, timerFrmt , sizeof(frmt));
name[sizeof(name) - 1] = 0;
frmt[sizeof(frmt) - 1] = 0;
}
ScopedOnceTimer::~ScopedOnceTimer()
{
LOG("[%s][%s] %ims", __func__, name.c_str(), int(GetDuration().toMilliSecsi()));
LOG(frmt, __func__, name, int(GetDuration().toMilliSecsi()));
}
spring_time ScopedOnceTimer::GetDuration() const
@@ -77,16 +77,17 @@ class ScopedMtTimer : public BasicTimer
class ScopedOnceTimer
{
public:
ScopedOnceTimer(const std::string& name);
ScopedOnceTimer(const char* name);
ScopedOnceTimer(const std::string& name, const char* frmt = "[%s][%s] %ims");
ScopedOnceTimer(const char* name, const char* frmt = "[%s][%s] %ims");
~ScopedOnceTimer();
spring_time GetDuration() const;
protected:
const spring_time startTime;
std::string name;
char name[128];
char frmt[128];
};

0 comments on commit 0f9ac7b

Please sign in to comment.