-
-
Notifications
You must be signed in to change notification settings - Fork 661
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/improve: create "custom loot" for monsters
This has two different options: Adding "global" items that can drop on any monster (it is disabled by default) Add specific items for each monster, without having to modify each monster's script. This allows a certain freedom to work with loot, without having to edit each script. Added "const ref" to getBestiaryList, avoid copying large bestiary map. Improved "registerLoot" function to avoid code duplication. Removed "bad" code in "closeContainer", this may crash if the container is used (it is removed right before) New lua function "getMonsterTypeByName".
- Loading branch information
Showing
10 changed files
with
180 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
-- Drops custom loot for all monsters | ||
local allLootConfig = { | ||
--{ id = 6526, chance = 100000 }, -- Example of loot (100% chance) | ||
} | ||
|
||
-- Custom loot for specific monsters (this has the same usage options as normal monster loot) | ||
local customLootConfig = { | ||
["Dragon"] = { items = { | ||
{ name = "platinum coin", chance = 1000, maxCount = 1 }, | ||
} }, | ||
} | ||
|
||
-- Global loot addition for all monsters | ||
local callback = EventCallback("MonsterOnDropLootCustom") | ||
|
||
function callback.monsterOnDropLoot(monster, corpse) | ||
if not monster or not corpse then | ||
return | ||
end | ||
local player = Player(corpse:getCorpseOwner()) | ||
if not player or not player:canReceiveLoot() then | ||
return | ||
end | ||
corpse:addLoot(monster:generateCustomLoot()) | ||
end | ||
|
||
-- Register the callback only if there is loot to be dropped | ||
if #allLootConfig > 0 then | ||
callback:register() | ||
end | ||
|
||
function Monster:generateCustomLoot() | ||
local mType = self:getType() | ||
if not mType then | ||
return {} | ||
end | ||
|
||
local loot = {} | ||
|
||
for _, lootInfo in ipairs(allLootConfig) do | ||
local roll = math.random(1, 10000) | ||
if roll <= lootInfo.chance then | ||
if loot[lootInfo.id] then | ||
loot[lootInfo.id].count = loot[lootInfo.id].count + 1 | ||
else | ||
loot[lootInfo.id] = { count = 1 } | ||
end | ||
end | ||
end | ||
|
||
return loot | ||
end | ||
|
||
local customMonsterLoot = GlobalEvent("CreateCustomMonsterLoot") | ||
|
||
function customMonsterLoot.onStartup() | ||
for monsterName, _ in pairs(customLootConfig) do | ||
local mtype = Game.getMonsterTypeByName(monsterName) | ||
if mtype then | ||
local lootTable = customLootConfig[mtype:getName()] | ||
if not lootTable then | ||
logger.error("[customMonsterLoot.onStartup] - No custom loot found for monster: {}", self:getName()) | ||
return | ||
end | ||
|
||
if #lootTable.items > 0 then | ||
mtype:createLoot(lootTable.items) | ||
logger.debug("[customMonsterLoot.onStartup] - Custom loot registered for monster: {}", mtype:getName()) | ||
end | ||
end | ||
end | ||
end | ||
|
||
customMonsterLoot:register() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.