Skip to content

Instantly share code, notes, and snippets.

@Underewarrr
Created March 31, 2024 17:36
Show Gist options
  • Save Underewarrr/658402a5964a021c469d5cf39add54f5 to your computer and use it in GitHub Desktop.
Save Underewarrr/658402a5964a021c469d5cf39add54f5 to your computer and use it in GitHub Desktop.
-- Define a utility function to get creatures within a specified range from a position
-- Stable Sync unSync function to find monster based on target
-- With this new lib now we can apply damage arround of final effect or targeted creature.
function getCreaturesInRange(position, range)
local creaturesInRange = {} -- Table to store creatures within range
local startTile = Tile(position)
-- Check if start tile exists
if startTile then
-- Iterate over the tiles within range
for x = position.x - range, position.x + range do
for y = position.y - range, position.y + range do
local tilePos = Position(x, y, position.z)
local tile = Tile(tilePos)
-- Check if tile exists
if tile then
local creatures = tile:getCreatures()
-- Iterate over creatures in the tile and add them to the result table
if creatures then
for _, creature in ipairs(creatures) do
table.insert(creaturesInRange, creature)
end
end
end
end
end
end
return creaturesInRange
end
-- Define a utility function to find monsters around target a position and apply damage
function applyDamageToMonstersAroundPosition(player, targetPos, minDamage, maxDamage, range)
-- Get the tile at the player's position This prevent future bugs related with spells. should be an util function avoidMissSpell()
local tile = Tile(targetPos)
if not tile then
print("No tile found at position.")
return
end
-- Get the top creature on the tile
local topCreature = tile:getTopCreature()
if not topCreature then
print("No creature found at position.")
return
end
-- Iterate over the creatures within range of the player position
local creaturesInRange = getCreaturesInRange(targetPos, range)
for _, target in ipairs(creaturesInRange) do
if target:isMonster() then
print("Target monster found: " .. target:getName())
local damage = math.random(minDamage, maxDamage)
print("Dealing damage FAKE DAMAGE TEST ONLY: " .. damage)
--genkiDamaCombat:execute(topCreature, Variant(target:getId())) -- Apply damage directly
target:getPosition():sendMagicEffect(12) -- Additional effect indicating hit
print("Damage executed to target: " .. target:getName())
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment