Skip to content

Commit

Permalink
Allow setting ETRD min and max distance with tmp and tmp2 (#883)
Browse files Browse the repository at this point in the history
  • Loading branch information
catsoften committed Jan 14, 2024
1 parent b711b29 commit 137e403
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/gui/game/GameView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2370,7 +2370,7 @@ void GameView::OnDraw()
if (type == PT_CRAY || type == PT_DRAY || type == PT_EXOT || type == PT_LIGH || type == PT_SOAP || type == PT_TRON
|| type == PT_VIBR || type == PT_VIRS || type == PT_WARP || type == PT_LCRY || type == PT_CBNW || type == PT_TSNS
|| type == PT_DTEC || type == PT_LSNS || type == PT_PSTN || type == PT_LDTC || type == PT_VSNS || type == PT_LITH
|| type == PT_CONV)
|| type == PT_CONV || type == PT_ETRD)
sampleInfo << ", Tmp2: " << sample.particle.tmp2;

sampleInfo << ", Pressure: " << sample.AirPressure;
Expand Down
20 changes: 14 additions & 6 deletions src/simulation/elements/ETRD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static void initDeltaPos()
{
ui::Point d(rx, ry);
if (std::abs(d.X) + std::abs(d.Y) <= maxLength)
deltaPos.push_back(ETRD_deltaWithLength(d, std::abs(d.X) + std::abs(d.Y)));
deltaPos.push_back(ETRD_deltaWithLength(d, std::hypot(d.X, d.Y)));
}
std::stable_sort(deltaPos.begin(), deltaPos.end(), [](const ETRD_deltaWithLength &a, const ETRD_deltaWithLength &b) {
return a.length < b.length;
Expand All @@ -100,7 +100,11 @@ int Element_ETRD_nearestSparkablePart(Simulation *sim, int targetId)
return -1;

Particle *parts = sim->parts;
int foundDistance = XRES + YRES;
if (parts[targetId].tmp2 && parts[targetId].tmp > parts[targetId].tmp2) // Invalid range if max is set
return -1;

const int maxDistance = std::hypot(XRES, YRES);
int foundDistance = parts[targetId].tmp2 ? std::min(parts[targetId].tmp2, maxDistance) : maxDistance; // tmp2 sets max distance
int foundI = -1;
ui::Point targetPos = ui::Point(int(parts[targetId].x), int(parts[targetId].y));

Expand All @@ -118,6 +122,10 @@ int Element_ETRD_nearestSparkablePart(Simulation *sim, int targetId)
ETRD_deltaWithLength delta = (*iter);
ui::Point checkPos = targetPos + delta.d;
int checkDistance = delta.length;
if (parts[targetId].tmp >= checkDistance) // tmp sets min distance
{
continue;
}
if (foundDistance < checkDistance)
{
// deltaPos is sorted in order of ascending length, so foundDistance < checkDistance means all later items are further away.
Expand All @@ -142,8 +150,8 @@ int Element_ETRD_nearestSparkablePart(Simulation *sim, int targetId)
if (parts[i].type == PT_ETRD && !parts[i].life)
{
ui::Point checkPos = ui::Point(int(parts[i].x)-targetPos.X, int(parts[i].y)-targetPos.Y);
int checkDistance = std::abs(checkPos.X) + std::abs(checkPos.Y);
if (checkDistance < foundDistance && i != targetId)
int checkDistance = std::hypot(checkPos.X, checkPos.Y);
if (checkDistance < foundDistance && checkDistance > parts[targetId].tmp && i != targetId) // tmp sets min distance
{
foundDistance = checkDistance;
foundI = i;
Expand All @@ -162,8 +170,8 @@ int Element_ETRD_nearestSparkablePart(Simulation *sim, int targetId)
{
countLife0++;
ui::Point checkPos = ui::Point(int(parts[i].x)-targetPos.X, int(parts[i].y)-targetPos.Y);
int checkDistance = std::abs(checkPos.X) + std::abs(checkPos.Y);
if (checkDistance < foundDistance && i != targetId)
int checkDistance = std::hypot(checkPos.X, checkPos.Y);
if (checkDistance < foundDistance && checkDistance > parts[targetId].tmp && i != targetId) // tmp sets min distance
{
foundDistance = checkDistance;
foundI = i;
Expand Down

0 comments on commit 137e403

Please sign in to comment.