From 96ee97f55830364bdc8891a76b33aa2ec0acc815 Mon Sep 17 00:00:00 2001 From: David Vogel Date: Mon, 28 Aug 2023 18:56:19 +0200 Subject: [PATCH 1/2] Add new Close value to JumpTo parameter --- README.md | 1 + lua/d3bot/1_navmesh.lua | 24 +++++++++++++++++++++++- lua/d3bot/sv_zs_bot_handler/basics.lua | 26 ++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dcdbbfd..c3513bd 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lua/d3bot/1_navmesh.lua b/lua/d3bot/1_navmesh.lua index 4af7ab2..e47ea9f 100644 --- a/lua/d3bot/1_navmesh.lua +++ b/lua/d3bot/1_navmesh.lua @@ -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" }, @@ -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(...). + local nodeX = (posX > areaXMax and areaXMax) or (posX < areaXMin and areaXMin) or posX + local nodeY = (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 diff --git a/lua/d3bot/sv_zs_bot_handler/basics.lua b/lua/d3bot/sv_zs_bot_handler/basics.lua index dd31916..c0fecaa 100644 --- a/lua/d3bot/sv_zs_bot_handler/basics.lua +++ b/lua/d3bot/sv_zs_bot_handler/basics.lua @@ -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 @@ -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 From f53a17bf1827149f9993bc9e903042dffb8800ca Mon Sep 17 00:00:00 2001 From: David Vogel Date: Mon, 28 Aug 2023 19:19:34 +0200 Subject: [PATCH 2/2] Fix GetClosestPointOnArea and jumpToParam hull --- lua/d3bot/1_navmesh.lua | 4 ++-- lua/d3bot/sv_zs_bot_handler/basics.lua | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lua/d3bot/1_navmesh.lua b/lua/d3bot/1_navmesh.lua index e47ea9f..f437e72 100644 --- a/lua/d3bot/1_navmesh.lua +++ b/lua/d3bot/1_navmesh.lua @@ -248,8 +248,8 @@ return function(lib) 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(...). - local nodeX = (posX > areaXMax and areaXMax) or (posX < areaXMin and areaXMin) or posX - local nodeY = (posY > areaYMax and areaYMax) or (posY < areaYMin and areaYMin) or posY + 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 diff --git a/lua/d3bot/sv_zs_bot_handler/basics.lua b/lua/d3bot/sv_zs_bot_handler/basics.lua index c0fecaa..cd5b3d9 100644 --- a/lua/d3bot/sv_zs_bot_handler/basics.lua +++ b/lua/d3bot/sv_zs_bot_handler/basics.lua @@ -151,7 +151,7 @@ function D3bot.Basics.Walk(bot, pos, aimAngle, slowdown, proximity) 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 _, 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. @@ -370,7 +370,7 @@ function D3bot.Basics.WalkAttackAuto(bot) 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 _, 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.