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

Commit

Permalink
Changes and additions
Browse files Browse the repository at this point in the history
• Added a new binding rotateToAngle(), rotates an entity to a given angle, NOTE: must not be moving.

• added the ability to add extra params to a path point when using tpz.path.general, example: 
basic point {x,y,z} 
point with a delay {x,y,z {delay = 10}}
point with delay and rotation {x,y,z, {rot = 35, delay = 10}
point with a rotation only {x,y,z {rot = 120}}, in this case a small delay will automatically be set so the rotation can be applied.

• Updated all npc's that now use the new lua path functions and tested them.
  • Loading branch information
Omnione committed Jun 26, 2020
1 parent 34bcbaf commit 816cded
Show file tree
Hide file tree
Showing 37 changed files with 1,566 additions and 1,653 deletions.
280 changes: 67 additions & 213 deletions scripts/globals/pathfind.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,81 +128,90 @@ tpz.path =
end,

-- 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)
-- takes a point that is {x,y,z} extra option can be added: {x,y,z {delay = 4}} or {x,y,z{rot = 230}} or both {x,y,z{rot = 90, delay = 7}}
-- if a rotation is added "{x,y,z {rot = 35}}" then a delay will be added to allow for the rotation to execute.
general = function(entity, points, flags)
local path = points or {}
local point = entity:getPathPoint()
local pos = entity:getPos()
local pathFlags = tpz.path.flag.NONE
local canPath = false
local delay = 0
local rotation = 0

if entity:atPoint(path[point]) then
if path[point][4] ~= 0 then
entity:setLocalVar("[PATHDELAY]", os.time() + path[point][4])
if flags ~= tpz.path.flag.NONE and flags ~= nil then
pathFlags = flags
end

local dx = path[point][1] - pos.x
local dz = path[point][3] - pos.z

local distToPoint = math.sqrt( dx * dx + dz * dz )

if distToPoint < 0.1 then
if path[point][4] then
local options = path[point][4] or {}
----------------------------------
-- Set delay if found
----------------------------------
if options.delay then
if options.delay > 0 then
delay = options.delay
end
end
--------------------------------------------
-- Auto delay if rotation and no delay found
--------------------------------------------
if options.rot then
if options.rot > 0 and delay == 0 then
delay = 3
end
end
end

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

----------------------------------
-- Set next point
----------------------------------
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]
}
local lastPoint = point

if os.time() >= entity:getLocalVar("[PATHDELAY]") then
entity:stepTo(np[1], np[2], np[3], run)
if point > 1 and point < #path then
lastPoint = point -1
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)
----------------------------------
-- Set rotation if one found
----------------------------------
if path[lastPoint][4] then
local rotateOptions = path[lastPoint][4] or {}
if rotateOptions.rot then
if rotateOptions.rot > 0 then
rotation = rotateOptions.rot
end
end

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

----------------------------------
-- Move if time is > delay
----------------------------------
if os.time() >= entity:getLocalVar("[PATHDELAY]") then
entity:stepTo(path[point][1], path[point][2], path[point][3], run)
entity:pathTo(path[point][1], path[point][2], path[point][3], pos.rot, pathFlags)
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
if rotation > 0 then
entity:rotateToAngle(rotation)
end
end
end,
Expand Down Expand Up @@ -248,177 +257,22 @@ tpz.path =
end,

-- pick a random point in the path and set random delay.
randomPoint = function(entity, points, flags)
randomPoint = function(entity, points, run)
local path = points or {}
local point = entity:getPathPoint()
local pos = entity:getPos()
local dx = path[point][1] - pos.x
local dz = path[point][3] - pos.z

if entity:atPoint(path[point]) then
if path[point][5] ~= 0 then
entity:setLocalVar("[PATHDELAY]", os.time() + path[point][5])
else
entity:setLocalVar("[PATHDELAY]", os.time() + math.random(4, 8))
end
entity:setPathPoint(math.random(1, #path))
end
local distToPoint = math.sqrt( dx * dx + dz * dz )

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
if distToPoint < 0.1 then
entity:setLocalVar("[PATHDELAY]", os.time() + math.random(4, 8))
entity:setPathPoint(math.random(1, #path))
end

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
entity:stepTo(path[point][1], path[point][2], path[point][3], run)
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
}
4 changes: 1 addition & 3 deletions scripts/globals/transport.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,12 @@ end
tpz.transport.dockMessage = function(npc, triggerID, messages, dock)
npc:showText(npc, messages[triggerID])
if (triggerID % 2) == 0 then
npc:setLocalVar("[PATHING]DEPARTING", 2)
npc:setLocalVar("[PATHING]ARRIVING", 1)
npc:setPathPoint(1)
npc:pathResume()
--tpz.path.toPoint(npc, tpz.transport.pos[dock].ARRIVING, false)
else
npc:setLocalVar("[PATHING]DEPARTING", 1)
npc:setPathPoint(1)
npc:pathResume()
--tpz.path.toPoint(npc, tpz.transport.pos[dock].DEPARTING, false)
end
end
8 changes: 5 additions & 3 deletions scripts/zones/Bostaunieux_Oubliette/npcs/Novalmauge.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ require("scripts/globals/quests")

local path =
{
{20.000, -24.032, 20.000, 2},
{75.000, -24.032, 20.000, 2},
{20.000, -24.032, 20.023},
{21.000, -24.000, 20.054},
{74.835, -24.039, 20.250},
{74.373, -24.014, 19.999},
}

local wsQuest = tpz.wsquest.spiral_hell
Expand All @@ -27,7 +29,7 @@ function onSpawn(npc)
end

function onPath(npc)
tpz.path.basicPath(npc, path, false)
tpz.path.general(npc, path, tpz.path.flag.WALLHACK, false)
end

function onTrade(player, npc, trade)
Expand Down
Loading

0 comments on commit 816cded

Please sign in to comment.