Skip to content

Commit

Permalink
Merge pull request LandSandBoat#6566 from cocosolos/drawin
Browse files Browse the repository at this point in the history
Move Draw-In to Lua
  • Loading branch information
zach2good authored Dec 28, 2024
2 parents 35e2d0d + c5f5e39 commit e546062
Show file tree
Hide file tree
Showing 80 changed files with 1,012 additions and 224 deletions.
1 change: 0 additions & 1 deletion scripts/battlefields/Apollyon/sw_apollyon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ content.groups =
mobs = { 'Armoury_Crate_Mimic' },
mobMods =
{
[xi.mobMod.DRAW_IN ] = 1,
[xi.mobMod.NO_MOVE ] = 1,
[xi.mobMod.NO_DESPAWN] = 1,
[xi.mobMod.NO_AGGRO ] = 1,
Expand Down
2 changes: 1 addition & 1 deletion scripts/enum/mob_mod.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ xi.mobMod =
HP_HEAL_CHANCE = 9, -- can cast cures below this HP %
SUBLINK = 10, -- sub link group
LINK_RADIUS = 11, -- link radius
DRAW_IN = 12, -- 1 - player draw in, 2 - alliance draw in -- only add as a spawn mod!
-- UNUSED = 12,
SEVERE_SPELL_CHANCE = 13, -- % chance to use a severe spell like death or impact
SKILL_LIST = 14, -- uses given mob skill list
MUG_GIL = 15, -- amount gil carried for mugging
Expand Down
82 changes: 82 additions & 0 deletions scripts/globals/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,31 @@ function utils.angleToRotation(radians)
return radians * ffxiAngleToRotationFactor
end

-- Function to calculate the cross product
local function crossProduct(x1, y1, x2, y2)
return x1 * y2 - y1 * x2
end

-- Function to check if two points are on the same side of a line
---@nodiscard
---@param line table
---@param pos1 table
---@param pos2 table
---@return boolean
function utils.sameSideOfLine(line, pos1, pos2)
-- Calculate vectors
local v1x, v1y = pos1.x - line[1][1], pos1.z - line[1][2]
local v2x, v2y = pos2.x - line[1][1], pos2.z - line[1][2]
local lx, ly = line[2][1] - line[1][1], line[2][2] - line[1][2]

-- Calculate cross products
local cross1 = crossProduct(lx, ly, v1x, v1y)
local cross2 = crossProduct(lx, ly, v2x, v2y)

-- Check if cross products have the same sign
return cross1 * cross2 >= 0
end

-- Returns 24h Clock Time (example: 04:30 = 430, 21:30 = 2130)
---@nodiscard
---@return number?
Expand Down Expand Up @@ -1261,3 +1286,60 @@ function utils.toWords(value)
local word1 = bit.rshift(bit.band(value, 0xFFFF0000), 16)
return word0, word1
end

-- Draws in target to position if any conditions are met.
-- Conditions must be met for "wait" seconds.
-- Can set offset from and degrees around position to place target.
--[[
table =
{
conditions = { boolean, ... },
position =
{
x = number,
y = number,
z = number,
rot = integer,
},
offset = number,
degrees = number,
wait = integer,
}
--]]
---@param target CBaseEntity
---@param table table
---@return boolean
function utils.drawIn(target, table)
if table.position then
local nextDrawIn = target:getLocalVar('[Draw-In]WaitTime')
local conditions = table.conditions and table.conditions or { true }
for _, condition in ipairs(conditions) do
if condition then
if nextDrawIn > 0 then
if os.time() > nextDrawIn then
local position = {}
if table.position then
position.x = table.position.x and table.position.x or table.position[1]
position.y = table.position.y and table.position.y or table.position[2]
position.z = table.position.z and table.position.z or table.position[3]
position.rot = table.position.rot and table.position.rot or table.position[4]
end
local offset = table.offset and table.offset or 0
local degrees = table.degrees and table.degrees or 0

DrawIn(target, position, offset, degrees)
target:setLocalVar('[Draw-In]WaitTime', 0)
return true
end
return false
else
local wait = table.wait and table.wait or 1
target:setLocalVar('[Draw-In]WaitTime', os.time() + wait)
return false
end
end
end
end
target:setLocalVar('[Draw-In]WaitTime', 0)
return false
end
22 changes: 22 additions & 0 deletions scripts/mixins/draw_in.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require('scripts/globals/mixins')

g_mixins = g_mixins or {}

g_mixins.draw_in = function(mobArg)
mobArg:addListener('COMBAT_TICK', 'DRAW_IN_CHECK', function(mob)
local target = mob:getTarget()
if target then
local drawInTable =
{
conditions =
{
mob:checkDistance(target) >= mob:getMeleeRange() * 2,
},
position = mob:getPos(),
}
utils.drawIn(target, drawInTable)
end
end)
end

return g_mixins.draw_in
23 changes: 23 additions & 0 deletions scripts/mixins/families/mimic.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require('scripts/globals/mixins')

g_mixins = g_mixins or {}

g_mixins.mimic = function(mimicMob)
mimicMob:addListener('COMBAT_TICK', 'DRAW_IN_CHECK', function(mob)
local target = mob:getTarget()
if target then
local drawInTable =
{
conditions =
{
mob:checkDistance(target) >= mob:getMeleeRange(),
},
position = mob:getPos(),
offset = mob:getMeleeRange() - 0.2,
}
utils.drawIn(target, drawInTable)
end
end)
end

return g_mixins.mimic
5 changes: 5 additions & 0 deletions scripts/specs/core/CBaseEntity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3527,6 +3527,11 @@ end
function CBaseEntity:getModelSize()
end

---@nodiscard
---@return number
function CBaseEntity:getMeleeRange()
end

---@param range number
---@return nil
function CBaseEntity:setMeleeRange(range)
Expand Down
8 changes: 8 additions & 0 deletions scripts/specs/core/Globals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ end
function Terminate()
end

---@param target CBaseEntity
---@param table table
---@param offset number
---@param degrees number
---@return nil
function DrawIn(target, table, offset, degrees)
end

---@nodiscard
---@param mobid integer
---@return integer
Expand Down
16 changes: 16 additions & 0 deletions scripts/zones/Abyssea-Misareaux/mobs/Athamas.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-----------------------------------
-- Area: Abyssea - Misareaux
-- Mob: Athamas
-----------------------------------
---@type TMobEntity
local entity = {}

entity.onMobSkillTarget = function(target, mob, mobskill)
if mobskill:isAoE() then
for _, member in ipairs(target:getAlliance()) do
mob:drawIn(member)
end
end
end

return entity
21 changes: 21 additions & 0 deletions scripts/zones/Apollyon/mobs/Armoury_Crate_Mimic.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-----------------------------------
-- Area: Apollyon NW
-- NPC: Armoury Crate Mimic
-----------------------------------
---@type TMobEntity
local entity = {}

entity.onMobFight = function(mob, target)
local distance = mob:checkDistance(target)
local drawInTable =
{
conditions =
{
distance >= mob:getMeleeRange() and distance <= 20,
},
position = mob:getPos(),
}
utils.drawIn(target, drawInTable)
end

return entity
29 changes: 28 additions & 1 deletion scripts/zones/Attohwa_Chasm/mobs/Tiamat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,42 @@ end

entity.onMobInitialize = function(mob)
mob:setCarefulPathing(true)
mob:setMobMod(xi.mobMod.DRAW_IN, 8)
end

entity.onMobSpawn = function(mob)
-- Reset animation so it starts grounded.
mob:setMobSkillAttack(0)
mob:setAnimationSub(0)
mob:setMobMod(xi.mobMod.NO_MOVE, 0)
end

entity.onMobRoam = function(mob)
mob:setMobMod(xi.mobMod.NO_MOVE, 0)
end

entity.onMobFight = function(mob, target)
-- Tiamat draws in from set boundaries leaving her spawn area
local drawInTable =
{
conditions =
{
target:getZPos() > 28,
target:getZPos() > -31 and target:getXPos() > -515,
target:getZPos() <= -31 and target:getXPos() > -500,
},
position = mob:getPos(),
wait = 5,
}
for _, condition in ipairs(drawInTable.conditions) do
if condition then
mob:setMobMod(xi.mobMod.NO_MOVE, 1)
utils.drawIn(target, drawInTable)
break
else
mob:setMobMod(xi.mobMod.NO_MOVE, 0)
end
end

-- Gains a large attack boost when health is under 25% which cannot be Dispelled.
if
mob:getHPP() <= 25 and
Expand Down Expand Up @@ -83,6 +109,7 @@ entity.onMobFight = function(mob, target)
end
end
end

end

entity.onMobDeath = function(mob, player, optParams)
Expand Down
6 changes: 2 additions & 4 deletions scripts/zones/Balgas_Dais/mobs/Black_Dragon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
-- Mob: Black Dragon
-- Mission 2-3 BCNM Fight
-----------------------------------
mixins = { require('scripts/mixins/draw_in') }
-----------------------------------
---@type TMobEntity
local entity = {}

entity.onMobInitialize = function(mob)
mob:setMobMod(xi.mobMod.DRAW_IN, 1)
end

entity.onMobDeath = function(mob, player, optParams)
player:addTitle(xi.title.BLACK_DRAGON_SLAYER)
end
Expand Down
3 changes: 2 additions & 1 deletion scripts/zones/Balgas_Dais/mobs/Large_Box.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
-- NM: Large Box
-- BCNM: Treasures and Tribulations
-----------------------------------
mixins = { require('scripts/mixins/families/mimic') }
-----------------------------------
---@type TMobEntity
local entity = {}

Expand All @@ -22,7 +24,6 @@ entity.onMobEngage = function(mob, target)
then
small:setLocalVar('engaged', 1)

mob:setMobMod(xi.mobMod.DRAW_IN, 1)
DespawnMob(mobId - 2)
DespawnMob(mobId - 1)

Expand Down
3 changes: 2 additions & 1 deletion scripts/zones/Balgas_Dais/mobs/Medium_Box.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
-- NM: Medium Box
-- BCNM: Treasures and Tribulations
-----------------------------------
mixins = { require('scripts/mixins/families/mimic') }
-----------------------------------
---@type TMobEntity
local entity = {}

Expand All @@ -16,7 +18,6 @@ entity.onMobEngage = function(mob, target)
then
small:setLocalVar('engaged', 1)

mob:setMobMod(xi.mobMod.DRAW_IN, 1)
DespawnMob(mobId - 1)
DespawnMob(mobId + 1)

Expand Down
3 changes: 2 additions & 1 deletion scripts/zones/Balgas_Dais/mobs/Small_Box.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
-- NM: Small Box
-- BCNM: Treasures and Tribulations
-----------------------------------
mixins = { require('scripts/mixins/families/mimic') }
-----------------------------------
---@type TMobEntity
local entity = {}

Expand All @@ -18,7 +20,6 @@ entity.onMobEngage = function(mob, target)
if mob:getLocalVar('engaged') == 0 then
mob:setLocalVar('engaged', 1)

mob:setMobMod(xi.mobMod.DRAW_IN, 1)
DespawnMob(mobId + 1)
DespawnMob(mobId + 2)

Expand Down
3 changes: 2 additions & 1 deletion scripts/zones/Balgas_Dais/mobs/Wyrm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
-- KSNM: Early Bird Catches the Wyrm
-- For future reference: Trusts are not allowed in this fight
-----------------------------------
mixins = { require('scripts/mixins/draw_in') }
-----------------------------------
---@type TMobEntity
local entity = {}

entity.onMobInitialize = function(mob)
end

entity.onMobSpawn = function(mob)
mob:setMobMod(xi.mobMod.DRAW_IN, 1) -- has a bug during flight, like Tiamat
mob:setTP(3000) -- opens fight with a skill
end

Expand Down
10 changes: 10 additions & 0 deletions scripts/zones/Beadeaux/mobs/Mimic.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-----------------------------------
-- Area: Beadeaux
-- NM: Mimic
-----------------------------------
mixins = { require('scripts/mixins/families/mimic') }
-----------------------------------
---@type TMobEntity
local entity = {}

return entity
Loading

0 comments on commit e546062

Please sign in to comment.