Skip to content

Commit

Permalink
Merge pull request #114 from Dadido3/JumpToClose
Browse files Browse the repository at this point in the history
Add "Close" value to JumpTo parameter
  • Loading branch information
Dadido3 authored Aug 28, 2023
2 parents dcf4eaa + f53a17b commit bd2b9b2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ Be sure to follow all the other necessary steps as described in [#Installation](
- Jump = Always: Bots will always jump if located in this node.
- JumpTo = Disabled: Bots won't jump if heading towards this node.
- JumpTo = Always: Bots will always jump if heading towards this node.
- JumpTo = Close: Bots will jump if heading towards this node, but only if they are close enough.
- Duck = Disabled: Bots won't crouch if located in this node.
- Duck = Always: Bots will always crouch if located in this node.
- DuckTo = Disabled: Bots won't crouch if heading towards this node.
Expand Down
24 changes: 23 additions & 1 deletion lua/d3bot/1_navmesh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ return function(lib)
lib.Params = {
Correct = {
Jump = { "Disabled", "Always" },
JumpTo = { "Disabled", "Always" },
JumpTo = { "Disabled", "Always", "Close" },
Duck = { "Disabled", "Always" },
DuckTo = { "Disabled", "Always" },
Wall = { "Suicide", "Retarget" },
Expand Down Expand Up @@ -232,6 +232,28 @@ return function(lib)
itemParamChanged(self, name)
end

---Returns a point that lies on the node's area, and is closest to the given position pos.
---It may just return the node's origin if the node doesn't have any area.
---@param pos GVector
---@return GVector
function nodeFallback:GetClosestPointOnArea(pos)
local posX, posY, posZ = pos:Unpack()

local params = self.Params
if not self.HasArea then return self.Pos end

local areaXMin = params.AreaXMin
local areaXMax = params.AreaXMax
local areaYMin = params.AreaYMin
local areaYMax = params.AreaYMax

-- Clamp values by using comparison and logical operators, as it's faster than math.min(math.max(...), ...) or even math.Clamp(...).
posX = (posX > areaXMax and areaXMax) or (posX < areaXMin and areaXMin) or posX
posY = (posY > areaYMax and areaYMax) or (posY < areaYMin and areaYMin) or posY

return Vector(posX, posY, posZ)
end

function nodeFallback:GetFocusPos() return self.Pos end
function linkFallback:GetFocusPos() return LerpVector(0.5, self.Nodes[1].Pos, self.Nodes[2].Pos) end

Expand Down
26 changes: 26 additions & 0 deletions lua/d3bot/sv_zs_bot_handler/basics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,19 @@ function D3bot.Basics.Walk(bot, pos, aimAngle, slowdown, proximity)
if shouldClimb or jumpParam == "Always" or jumpToParam == "Always" then
actions.Jump = true
end
-- If there is a JumpTo parameter with "Close" as the value, determine if we are close enough to jump.
if jumpToParam == "Close" and nextNodeOrNil then
local _, hullTop = bot:GetHull() -- Assume the hull is symmetrical.
local hullX, hullY, _ = hullTop:Unpack()
local halfHullWidth = math.max(hullX, hullY) + 5 -- Just add a small margin to let the bot jump before it "touches" the next node's area.

---@type GVector
local closestDiff = origin - nextNodeOrNil:GetClosestPointOnArea(origin)
local closestDistSqr = closestDiff:Length2DSqr()
if closestDistSqr <= halfHullWidth*halfHullWidth then
actions.Jump = true
end
end
if facesHindrance then
if math.random(D3bot.BotJumpAntichance) == 1 then
actions.Jump = true
Expand Down Expand Up @@ -355,6 +368,19 @@ function D3bot.Basics.WalkAttackAuto(bot)
if jumpParam == "Always" or jumpToParam == "Always" then
actions.Jump = true
end
-- If there is a JumpTo parameter with "Close" as the value, determine if we are close enough to jump.
if jumpToParam == "Close" and nextNodeOrNil then
local _, hullTop = bot:GetHull() -- Assume the hull is symmetrical.
local hullX, hullY, _ = hullTop:Unpack()
local halfHullWidth = math.max(hullX, hullY) + 5 -- Just add a small margin to let the bot jump before it "touches" the next node's area.

---@type GVector
local closestDiff = origin - nextNodeOrNil:GetClosestPointOnArea(origin)
local closestDistSqr = closestDiff:Length2DSqr()
if closestDistSqr <= halfHullWidth*halfHullWidth then
actions.Jump = true
end
end
if facesHindrance then
if math.random(D3bot.BotJumpAntichance) == 1 then
actions.Jump = true
Expand Down

0 comments on commit bd2b9b2

Please sign in to comment.