Skip to content

Commit

Permalink
Added mod rules to force collision detection and avoidance to single …
Browse files Browse the repository at this point in the history
…threading
  • Loading branch information
marcushutchings authored and lhog committed Nov 30, 2022
1 parent 65a8413 commit 5fb0c97
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
6 changes: 6 additions & 0 deletions rts/Sim/Misc/ModInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ void CModInfo::ResetState()
maxCollisionPushMultiplier = std::numeric_limits<float>::infinity();
unitQuadPositionUpdateRate = 3;
groundUnitCollisionAvoidanceUpdateRate = 3;

forceCollisionsSingleThreaded = false;
forceCollisionAvoidanceSingleThreaded = false;
}
{
constructionDecay = true;
Expand Down Expand Up @@ -176,6 +179,9 @@ void CModInfo::Init(const std::string& modFileName)
maxCollisionPushMultiplier = movementTbl.GetFloat("maxCollisionPushMultiplier", maxCollisionPushMultiplier);
unitQuadPositionUpdateRate = Clamp(movementTbl.GetInt("unitQuadPositionUpdateRate", unitQuadPositionUpdateRate), 1, 15);
groundUnitCollisionAvoidanceUpdateRate = Clamp(movementTbl.GetInt("groundUnitCollisionAvoidanceUpdateRate", groundUnitCollisionAvoidanceUpdateRate), 1, 15);

forceCollisionsSingleThreaded = movementTbl.GetBool("forceCollisionsSingleThreaded", forceCollisionsSingleThreaded);
forceCollisionAvoidanceSingleThreaded = movementTbl.GetBool("forceCollisionAvoidanceSingleThreaded", forceCollisionAvoidanceSingleThreaded);
}

{
Expand Down
3 changes: 3 additions & 0 deletions rts/Sim/Misc/ModInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class CModInfo
// relative to a unit's maxspeed (default: inf)
float maxCollisionPushMultiplier;

bool forceCollisionsSingleThreaded;
bool forceCollisionAvoidanceSingleThreaded;

// rate in sim frames that a unit's position in the quad grid is updated (default: 3)
// a lower number will increase CPU load, but increase accuracy of collision detection
int unitQuadPositionUpdateRate;
Expand Down
79 changes: 54 additions & 25 deletions rts/Sim/Units/UnitHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "CommandAI/BuilderCAI.h"
#include "Sim/Misc/GlobalSynced.h"
#include "Sim/Misc/ModInfo.h"
#include "Sim/Misc/TeamHandler.h"
#include "Sim/MoveTypes/MoveType.h"
#include "Sim/Path/IPathManager.h"
Expand Down Expand Up @@ -303,39 +304,67 @@ void CUnitHandler::UpdateUnitMoveTypes()
{
SCOPED_TIMER("Sim::Unit::MoveType");

if (modInfo.forceCollisionAvoidanceSingleThreaded)
{
SCOPED_TIMER("Sim::Unit::MoveType::1::UpdatePreCollisionsMT");
for_mt(0, activeUnits.size(), [this](const int i){
CUnit* unit = activeUnits[i];
AMoveType* moveType = unit->moveType;
SCOPED_TIMER("Sim::Unit::MoveType::1::UpdatePreCollisionsST");
std::size_t len = activeUnits.size();
for (std::size_t i=0; i<len; ++i) {
CUnit* unit = activeUnits[i];
AMoveType* moveType = unit->moveType;

unit->SanityCheck();
unit->PreUpdate();
unit->SanityCheck();
unit->PreUpdate();

moveType->UpdatePreCollisionsMt();
});
}
moveType->UpdatePreCollisionsMt();
moveType->UpdatePreCollisions();
}
} else {
{
SCOPED_TIMER("Sim::Unit::MoveType::1::UpdatePreCollisionsMT");
for_mt(0, activeUnits.size(), [this](const int i){
CUnit* unit = activeUnits[i];
AMoveType* moveType = unit->moveType;

unit->SanityCheck();
unit->PreUpdate();

moveType->UpdatePreCollisionsMt();
});
}

{
SCOPED_TIMER("Sim::Unit::MoveType::2::UpdatePreCollisionsST");
std::size_t len = activeUnits.size();
for (std::size_t i=0; i<len; ++i) {
CUnit* unit = activeUnits[i];
AMoveType* moveType = unit->moveType;
{
SCOPED_TIMER("Sim::Unit::MoveType::2::UpdatePreCollisionsST");
std::size_t len = activeUnits.size();
for (std::size_t i=0; i<len; ++i) {
CUnit* unit = activeUnits[i];
AMoveType* moveType = unit->moveType;

moveType->UpdatePreCollisions();
}
moveType->UpdatePreCollisions();
}
}
}

{
SCOPED_TIMER("Sim::Unit::MoveType::3::UpdateMT");
for_mt(0, activeUnits.size(), [this](const int i){
CUnit* unit = activeUnits[i];
AMoveType* moveType = unit->moveType;
if (modInfo.forceCollisionsSingleThreaded) {
{
SCOPED_TIMER("Sim::Unit::MoveType::3::CollisionDetectionST");
for (int i = 0; i < activeUnits.size(); ++i) {
CUnit* unit = activeUnits[i];
AMoveType* moveType = unit->moveType;

moveType->UpdateCollisionDetections();
}
);
moveType->UpdateCollisionDetections();
}
}
} else {
{
SCOPED_TIMER("Sim::Unit::MoveType::3::CollisionDetectionMT");
for_mt(0, activeUnits.size(), [this](const int i){
CUnit* unit = activeUnits[i];
AMoveType* moveType = unit->moveType;

moveType->UpdateCollisionDetections();
}
);
}
}

{
Expand Down

0 comments on commit 5fb0c97

Please sign in to comment.