Skip to content

Commit

Permalink
refactor: rename variables to camelCase and improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
omarcopires committed Nov 11, 2024
1 parent 57e35d1 commit a9d8505
Showing 1 changed file with 41 additions and 70 deletions.
111 changes: 41 additions & 70 deletions data/scripts/talkactions/god/manage_monster.lua
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
local function createCreaturesAround(player, maxRadius, creatureName, creatureCount, creatureForge, boolForceCreate)
local position = player:getPosition()
local createdCount = 0
local function createCreaturesAround(player, maxRadius, creatureName, creatureCount, creatureForge, forceCreateMonsters)
local playerPosition = player:getPosition()
local createdCreatureCount = 0
local sendMessage = false
local canSetFiendish, canSetInfluenced, influencedLevel = CheckDustLevel(creatureForge, player)

for radius = 1, maxRadius do
if createdCount >= creatureCount then
if createdCreatureCount >= creatureCount then
break
end

local minX = position.x - radius
local maxX = position.x + radius
local minY = position.y - radius
local maxY = position.y + radius
local minX, maxX = playerPosition.x - radius, playerPosition.x + radius
local minY, maxY = playerPosition.y - radius, playerPosition.y + radius
for dx = minX, maxX do
for dy = minY, maxY do
if (dx == minX or dx == maxX or dy == minY or dy == maxY) and createdCount < creatureCount then
local checkPosition = Position(dx, dy, position.z)
if (dx == minX or dx == maxX or dy == minY or dy == maxY) and createdCreatureCount < creatureCount then
local checkPosition = Position(dx, dy, playerPosition.z)
local tile = Tile(checkPosition)
if tile and not tile:hasProperty(CONST_PROP_IMMOVABLEBLOCKSOLID) then
local monster = Game.createMonster(creatureName, checkPosition, false, boolForceCreate)
local monster = Game.createMonster(creatureName, checkPosition, false, forceCreateMonsters)
if monster then
createdCount = createdCount + 1
createdCreatureCount = createdCreatureCount + 1
monster:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
position:sendMagicEffect(CONST_ME_MAGIC_RED)
if creatureForge ~= nil and monster:isForgeable() then
playerPosition:sendMagicEffect(CONST_ME_MAGIC_RED)

if creatureForge and monster:isForgeable() then
local monsterType = monster:getType()
if canSetFiendish then
SetFiendish(monsterType, position, player, monster)
SetFiendish(monsterType, playerPosition, player, monster)
end

if canSetInfluenced then
SetInfluenced(monsterType, monster, player, influencedLevel)
end
elseif notSendMessage then
elseif not sendMessage then
sendMessage = true
end
end
Expand All @@ -40,86 +41,58 @@ local function createCreaturesAround(player, maxRadius, creatureName, creatureCo
end
end
end
if sendMessage == true then

if sendMessage then
player:sendCancelMessage("Only allowed monsters can be fiendish or influenced.")
end

logger.info("Player {} created '{}' monsters", player:getName(), createdCount)
end

local createMonster = TalkAction("/m")

-- @function createMonster.onSay
-- @desc TalkAction to create monsters with multiple options.
-- @param player: The player executing the command.
-- @param words: Command words.
-- @param param: String containing the command parameters.
-- Format: "/m monstername, monstercount, [fiendish/influenced level], spawnRadius, [forceCreate]"
-- Example: "/m rat, 10, fiendish, 5, true"
-- @param: the last param is by default "false", if add "," or any value it's set to true
-- @return true if the command is executed successfully, false otherwise.
function createMonster.onSay(player, words, param)
-- create log
logCommand(player, words, param)

if param == "" then
player:sendCancelMessage("Monster name param required.")
logger.error("[createMonster.onSay] - Monster name param not found.")
return true
end

local position = player:getPosition()

local split = param:split(",")
local monsterName = split[1]
local monsterCount = 0
if split[2] then
split[2] = split[2]:trimSpace()
monsterCount = tonumber(split[2])
end

local monsterForge = nil
if split[3] then
split[3] = split[3]:trimSpace()
monsterForge = split[3]
end
local playerPosition = player:getPosition()
local splitParams = param:split(",")
local monsterName = splitParams[1]
local monsterCount = tonumber(splitParams[2] or 1)
local monsterForge = splitParams[3] and splitParams[3]:trimSpace() or nil
local spawnRadius = tonumber(splitParams[4] or 5)
local forceCreate = splitParams[5] and true or false

if monsterCount > 1 then
local spawnRadius = 5
if split[4] then
split[4] = split[4]:trimSpace()
spawnRadius = split[4]
print(spawnRadius)
end

local forceCreate = false
if split[5] then
forceCreate = true
end

createCreaturesAround(player, spawnRadius, monsterName, monsterCount, monsterForge, forceCreate)
return true
end

local monster = Game.createMonster(monsterName, position)
local monster = Game.createMonster(monsterName, playerPosition)
if monster then
local canSetFiendish, canSetInfluenced, influencedLevel = CheckDustLevel(monsterForge, player)
monster:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
position:sendMagicEffect(CONST_ME_MAGIC_RED)
if monsterForge ~= nil and not monster:isForgeable() then
playerPosition:sendMagicEffect(CONST_ME_MAGIC_RED)

if monsterForge and not monster:isForgeable() then
player:sendCancelMessage("Only allowed monsters can be fiendish or influenced.")
return true
end

local monsterType = monster:getType()
if canSetFiendish then
SetFiendish(monsterType, position, player, monster)
SetFiendish(monsterType, playerPosition, player, monster)
end

if canSetInfluenced then
SetInfluenced(monsterType, monster, player, influencedLevel)
end
else
player:sendCancelMessage("There is not enough room.")
position:sendMagicEffect(CONST_ME_POFF)
playerPosition:sendMagicEffect(CONST_ME_POFF)
end
return true
end
Expand All @@ -128,29 +101,27 @@ createMonster:separator(" ")
createMonster:groupType("god")
createMonster:register()

----------------- Rename monster name -----------------
local setMonsterName = TalkAction("/setmonstername")

-- @function setMonsterName.onSay
-- @desc TalkAction to rename nearby monsters within a radius of 4 sqm.
-- Format: "/setmonstername newName"
function setMonsterName.onSay(player, words, param)
-- create log
logCommand(player, words, param)

if param == "" then
player:sendCancelMessage("Command param required.")
return true
end

local split = param:split(",")
local monsterNewName = split[1]

local splitParams = param:split(",")
local newMonsterName = splitParams[1]
local spectators, spectator = Game.getSpectators(player:getPosition(), false, false, 4, 4, 4, 4)

for i = 1, #spectators do
spectator = spectators[i]
if spectator:isMonster() then
spectator:setName(monsterNewName)
spectator:setName(newMonsterName)
end
end

return true
end

Expand Down

0 comments on commit a9d8505

Please sign in to comment.