Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Spring.GetProjectilesInSphere #1990

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

technateNG
Copy link
Contributor

Fixes #1662.

@technateNG
Copy link
Contributor Author

Tested with this.

function widget:GetInfo()
        return {
                name      = "My test widget",
                desc      = "Allows me to test stuff",
                author    = "dunno",
                date      = "Mar 12, 2023",
                license   = "GNU GPL, v2 or later",
                layer     = 0,
                enabled   = false
        }
end

local function tprint (tbl, indent)
  if not indent then indent = 0 end
  local toprint = string.rep(" ", indent) .. "{\r\n"
  indent = indent + 2 
  for k, v in pairs(tbl) do
    toprint = toprint .. string.rep(" ", indent)
    if (type(k) == "number") then
      toprint = toprint .. "[" .. k .. "] = "
    elseif (type(k) == "string") then
      toprint = toprint  .. k ..  "= "   
    end
    if (type(v) == "number") then
      toprint = toprint .. v .. ",\r\n"
    elseif (type(v) == "string") then
      toprint = toprint .. "\"" .. v .. "\",\r\n"
    elseif (type(v) == "table") then
      toprint = toprint .. tprint(v, indent + 2) .. ",\r\n"
    else
      toprint = toprint .. "\"" .. tostring(v) .. "\",\r\n"
    end
  end
  toprint = toprint .. string.rep(" ", indent-2) .. "}"
  return toprint
end


function widget:Initialize()
  local units = Spring.GetSelectedUnits()
  Spring.Echo(tprint(units))
  local x, y, z = Spring.GetUnitPosition(units[1])
  local radius = 100.5
  local radius_a = radius * math.sqrt(math.pi) / 2
  local xmin = x - radius_a
  local xmax = x + radius_a
  local zmin = z - radius_a
  local zmax = z + radius_a
  local a = Spring.GetProjectilesInRectangle(xmin, zmin, xmax, zmax)
  local b = Spring.GetProjectilesInSphere(x, y, z, radius)
  Spring.Echo(tprint(a))
  Spring.Echo(tprint(b))
end

Steps:

  1. Run engine with BAR, map wathever
  2. Enable cheats, create Bot Lab and make Pawn
  3. Start shoot (by attack) towards Commander
  4. Enable widget

Result: Both echoes shows the same.
I'll appreciate the ideas how to better test this kind of feature

@lhog
Copy link
Collaborator

lhog commented Mar 8, 2025

Hmm I'm not sure the implementation is correct.
Why did you choose to ignore y coordinate and where is the actual check for projectile being inside the sphere?

Copy link
Collaborator

@sprunk sprunk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local a = Spring.GetProjectilesInRectangle(xmin, zmin, xmax, zmax)
local b = Spring.GetProjectilesInSphere(x, y, z, radius)
Spring.Echo(tprint(a))
Spring.Echo(tprint(b))

Test result: Both echoes shows the same.

That sounds incorrect because surely a sphere and a rectangle can't overlap perfectly.

Perhaps spawn some sort of 3D grid of lingering (speed=0 and gravity=0? whatever makes it easy to work with) projectiles, call GetProjectilesInSphere and delete anything that wasn't returned, and compare visually if the remaining projectiles form a sphere.

@@ -3652,14 +3630,75 @@ int LuaSyncedRead::GetProjectilesInRectangle(lua_State* L)
if (!LuaUtils::IsProjectileVisible(L, pro))
continue;

lua_pushnumber(L, pro->id);
lua_rawseti(L, -2, arrayIndex++);
lua_pushnumber(L, static_cast<lua_Number>(pro->id));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dislike these casts. Perhaps we should update the build process to enable some sort of "ignore warnings" flag for these?

@badosu
Copy link
Collaborator

badosu commented Mar 8, 2025

Just a note, maybe the issue submitter meant a cylinder instead of a sphere. I commented on #1662 (comment) asking about this.

There might be use cases for sphere checking, e.g. custom proximity drawing/intercepting/shielding of projectiles, but the direct use required by the issue seems to be a circle cylinder.

In this scenario the check would be (pointA, pointB, radius) -> distinxzplane(pointA, pointB) < radius and the name Get*InCylinder in concordance to other similar calls. InSphere might still be desirable if you want to do it.

Edit: Motivation for use in a real game scenario indeed is Sphere, as commented here, feel free to proceed.

@technateNG
Copy link
Contributor Author

Looks like this actually gets projectiles in a square inscribed into a circle rather than a sphere?

Hmm I'm not sure the implementation is correct.
Why did you choose to ignore y coordinate and where is the actual check for projectile being inside the sphere?

@lhog @sprunk Long story short I overthink it based on current usage.

Copy link
Collaborator

@sprunk sprunk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks alright

@technateNG technateNG requested a review from sprunk March 10, 2025 17:27
@technateNG technateNG requested a review from sprunk March 10, 2025 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add GetProjectilesInSphere
4 participants