Skip to content
This repository has been archived by the owner on Dec 4, 2020. It is now read-only.

Commit

Permalink
Big update
Browse files Browse the repository at this point in the history
Added new pathing options for entity's
First up: pathfind.lua, probly quicker to look at the commenting than me explain that,

changed amost npc's that use pathing to use either basicPath or advancedPath, the advancedPath is the one that stands out as this allows the entity to pass id text and npc id's to automatically look at or speak at a a point in the path, and each point can have a delay.

Added random pathing for the bunnies in Khazam, as they just sat there.

I kept everything the same where i could, but i did have to redo some paths, but that let me cut down on path points also.
  • Loading branch information
Omnione committed Jun 21, 2020
1 parent 652116d commit 34bcbaf
Show file tree
Hide file tree
Showing 40 changed files with 2,636 additions and 3,026 deletions.
305 changes: 286 additions & 19 deletions scripts/globals/pathfind.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,31 +127,298 @@ tpz.path =
end
end,

npcBasicPath = function(npc, points, flags)
local pathPoints = {}
local pathPoint = npc:getPathPoint()
local pos = npc:getPos()
-- run a basic path from start to end and repeat, the path will need to be linked back to start to loop correctly.
basicPath = function(entity, points, run)
local path = points or {}
local point = entity:getPathPoint()
local pos = entity:getPos()

pathPoints = points
if entity:atPoint(path[point]) then
if path[point][4] ~= 0 then
entity:setLocalVar("[PATHDELAY]", os.time() + path[point][4])
else
if entity:getLocalVar("[PATHDELAY]") ~= 0 then
entity:setLocalVar("[PATHDELAY]", 0)
end
end

if point == #path then
entity:setPathPoint(1)
else
entity:setPathPoint(point +1)
end
end

local np =
{
path[point][1],
path[point][2],
path[point][3]
}

if os.time() >= entity:getLocalVar("[PATHDELAY]") then
entity:stepTo(np[1], np[2], np[3], run)
end
end,

-- run an advanced path from start to end and repeat, the path will need to be linked back to start to loop correctly.
-- can be delayed with a wait at a point and have the entity use a showText at a point also.
-- entity will look at a target if one is added to the 6th column in the table, but must be an npc.
-- example path point {x, y, z, text id, delay in seconds, npc id}
advancedPath = function(entity, points, run)
local path = points or {}
local point = entity:getPathPoint()
local pos = entity:getPos()
local debugPath = false -- change to true to show debug info

if entity:atPoint(path[point]) then
if path[point][4] ~= 0 then -- speak at this point if available (only use showText as the speaker needs to not be a player)
entity:showText(entity, path[point][4])
end

if path[point][5] ~= 0 then
entity:setLocalVar("[PATHDELAY]", os.time() + path[point][5])
else
if entity:getLocalVar("[PATHDELAY]") ~= 0 then
entity:setLocalVar("[PATHDELAY]", 0)
end
end

if point == #path then
entity:setPathPoint(1)
else
entity:setPathPoint(point +1)
end
end

if os.time() >= entity:getLocalVar("[PATHDELAY]") then
entity:stepTo(path[point][1], path[point][2], path[point][3], run)
else
local lastPoint = point -1
if lastPoint > 1 and lastPoint < #path and path[lastPoint][6] ~= 0 then
if GetNPCByID(path[lastPoint][6]) ~= nil then
entity:lookAt(GetNPCByID(path[lastPoint][6]):getPos())
if debugPath then
printf("looking at an npc: %u", tonumber(path[lastPoint][6])) -- un comment to debug
printf("lastPoint was %u, and this point is %u", lastPoint, point)
end
end
end
end
end,

-- entity will move to last point then set the last position in the path
toPoint = function(entity, points, run)
local path = points or {}
local point = entity:getPathPoint()
local pos = entity:getPos()
local canPath = true

if path == {} then
return
end

if entity:atPoint(path[point]) then
if point == #path then
canPath = false
else
entity:setPathPoint(point +1)
canPath = true
end
end

if canPath then
entity:stepTo(path[point][1], path[point][2], path[point][3], run)
end
end,

-- entity will move between two points
pingPong = function(entity, points, flags)
local path = points or {}
local point = entity:getPathPoint()
local pos = entity:getPos()

if entity:atPoint(path[1]) then
entity:setPathPoint(2)
elseif entity:atPoint(path[2]) then
entity:setPathPoint(1)
end

entity:pathTo(path[point][1], path[point][2], path[point][3], 0, flags)
end,

local dx = pos.x - pathPoints[pathPoint][1]
local dy = pos.y - pathPoints[pathPoint][2]
local distance = math.sqrt ( dx * dx + dy * dy )
-- pick a random point in the path and set random delay.
randomPoint = function(entity, points, flags)
local path = points or {}
local point = entity:getPathPoint()
local pos = entity:getPos()

if distance == 0 then
if pathPoint == #pathPoints then
npc:setPathPoint(1)
if entity:atPoint(path[point]) then
if path[point][5] ~= 0 then
entity:setLocalVar("[PATHDELAY]", os.time() + path[point][5])
else
npc:setPathPoint(pathPoint +1)
entity:setLocalVar("[PATHDELAY]", os.time() + math.random(4, 8))
end
entity:setPathPoint(math.random(1, #path))
end

local np =
{
path[point][1],
path[point][2],
path[point][3],
path[point][4]
}

local rotation = 0

if np[4] == 0 then
rotation = pos.rot
end

local pointX = pathPoints[pathPoint][1]
local pointY = pathPoints[pathPoint][2]
local pointZ = pathPoints[pathPoint][3]
local rotation = pathPoints[pathPoint][4]

npc:pathTo(pointX, pointY, pointZ, rotation)
npc:lookAt(pointX, pointY, pointZ)
if os.time() >= entity:getLocalVar("[PATHDELAY]") then
entity:pathTo(np[1], np[2], np[3], rotation, flags)
if path[point][4] == 0 then
entity:lookAt(np[1], np[2], np[3])
end
end
end,

-- basic move to a position, uses a run bool, default is false
moveToPosition = function(entity, newPos, run)
local point = newPos or {0,0,0}

entity:stepTo(point[1], point[2], point[3], run)
end,

-- move or keep entity behind a target.
behindTarget = function(entity, target)
local pos = entity:getPos()
local tPos = target:getPos()
local offset = target:getModelSize() +5

local rotation = (tPos.rot/256) * 2 * math.pi

rotation = rotation + math.pi

local newX = tPos.x + math.cos((2 * math.pi - rotation)) * offset
local newZ = tPos.z + math.sin((2 * math.pi - rotation)) * offset

entity:pathTo(newX, tPos.y, newZ, 0, 8, true)
entity:lookAt(tPos)
end,

-- move or keep entity infront of a target.
infrontTarget = function(entity, target)
local pos = entity:getPos()
local tPos = target:getPos()
local offset = target:getModelSize() +5

local rotation = (tPos.rot/256) * 2 * math.pi

local newX = tPos.x + math.cos((2 * math.pi - rotation)) * offset
local newZ = tPos.z + math.sin((2 * math.pi - rotation)) * offset

entity:pathTo(newX, tPos.y, newZ, 0, 8, true)
entity:lookAt(tPos)
end,

--- move or keep entity out of incomming casting range from target.
safeDistance = function(entity, target)
local pos = entity:getPos()
local tPos = target:getPos()
local offset = target:getModelSize() + 22

local newX = tPos.x + math.cos((2 * math.pi)) * offset
local newZ = tPos.z + math.sin((2 * math.pi)) * offset

entity:pathTo(newX, tPos.y, newZ, 0, 8, true)
entity:lookAt(tPos)
end,

-- move or keep entity in meele range of target.
meeleRange = function(entity, target)
local pos = entity:getPos()
local tPos = target:getPos()
local offset = target:getModelSize() + 3

local newX = tPos.x + math.cos((2 * math.pi)) * offset
local newZ = tPos.z + math.sin((2 * math.pi)) * offset

entity:pathTo(newX, tPos.y, newZ, 0, 8, true)
entity:lookAt(tPos)
end,

-- move or keep entity in casting range of target.
castingRange = function(enity, target)
local pos = entity:getPos()
local tPos = target:getPos()
local offset = target:getModelSize() + 18

local newX = tPos.x + math.cos((2 * math.pi)) * offset
local newZ = tPos.z + math.sin((2 * math.pi)) * offset

entity:pathTo(newX, tPos.y, newZ, 0, 8, true)
entity:lookAt(tPos)
end,

-- move or keep entity in range of target to use a song or cor roll.
songRollRange = function(entity, target)
local pos = entity:getPos()
local tPos = target:getPos()
local offset = target:getModelSize() + 2

local newX = tPos.x + math.cos((2 * math.pi)) * offset
local newZ = tPos.z + math.sin((2 * math.pi)) * offset

entity:pathTo(newX, tPos.y, newZ, 0, 8, true)
entity:lookAt(tPos)
end,

-- move or keep entity in ranged attack range.
rangedRange = function(entity, target)
local pos = entity:getPos()
local tPos = target:getPos()
local offset = target:getModelSize() + 15

local newX = tPos.x + math.cos((2 * math.pi)) * offset
local newZ = tPos.z + math.sin((2 * math.pi)) * offset

entity:pathTo(newX, tPos.y, newZ, 0, 8, true)
entity:lookAt(tPos)
end,

-- move or keep entity at a certain distance using a range passed to function.
wantedRange = function(entity, target, range)
local pos = entity:getPos()
local tPos = target:getPos()
local offset = target:getModelSize() + range

local newX = tPos.x + math.cos((2 * math.pi)) * offset
local newZ = tPos.z + math.sin((2 * math.pi)) * offset

entity:pathTo(newX, tPos.y, newZ, 0, 8, true)
entity:lookAt(tPos)
end,


-- move or keep entity between cover target and cover target, needs both passed to function.
coverTarget = function(entity, coverTarget, target)
local pos = entity:getPos()
local tPos = target:getPos()
local cTPos = coverTarget:getPos()

local rotation = (tPos.rot/256) * 2 * math.pi

local dx = tPos.x - cTPos.x
local dz = tPos.z - cTPos.z

local midpoint = math.sqrt( dx * dx + dz * dz )/2

local newX = tPos.x + math.cos((2 * math.pi - rotation)) * midpoint
local newZ = tPos.z + math.sin((2 * math.pi - rotation)) * midpoint

entity:pathTo(newX, tPos.y, newZ, 0, 8, true)
entity:lookAt(tPos)
end
}
15 changes: 11 additions & 4 deletions scripts/globals/transport.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
-- Transport
------------------------------------
require("scripts/globals/zone")
require("scripts/globals/pathfind")
------------------------------------

tpz = tpz or {}
Expand Down Expand Up @@ -59,8 +60,8 @@ tpz.transport.pos =
{
mhaura =
{
ARRIVING = {7.06, -1.36, 2.20},
DEPARTING = {8.26, -1.36, 2.20}
ARRIVING = {{7.06, -1.36, 2.48}, {7.07, -1.36, 2.58}},
DEPARTING = {{8.26, -1.36, 2.20}, {8.26, -1.36, 2.30}},
}
}

Expand All @@ -78,8 +79,14 @@ end
tpz.transport.dockMessage = function(npc, triggerID, messages, dock)
npc:showText(npc, messages[triggerID])
if (triggerID % 2) == 0 then
npc:pathThrough(tpz.transport.pos[dock].ARRIVING, PATHFLAG_WALLHACK)
npc:setLocalVar("[PATHING]DEPARTING", 2)
npc:setPathPoint(1)
npc:pathResume()
--tpz.path.toPoint(npc, tpz.transport.pos[dock].ARRIVING, false)
else
npc:pathThrough(tpz.transport.pos[dock].DEPARTING, PATHFLAG_WALLHACK)
npc:setLocalVar("[PATHING]DEPARTING", 1)
npc:setPathPoint(1)
npc:pathResume()
--tpz.path.toPoint(npc, tpz.transport.pos[dock].DEPARTING, false)
end
end
Loading

0 comments on commit 34bcbaf

Please sign in to comment.