Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monster Storages from Lua #3973

Merged
merged 2 commits into from
Mar 3, 2022
Merged

Monster Storages from Lua #3973

merged 2 commits into from
Mar 3, 2022

Conversation

MillhioreBT
Copy link
Contributor

Pull Request Prelude

  • I have followed [proper The Forgotten Server code styling][code].
  • I have read and understood the [contribution guidelines][cont] before making this PR.
  • I am aware that this PR may be closed if the above-mentioned criteria are not fulfilled.

Changes Proposed

This new revscript file adds the ability to manage storage for monsters, I've used a metatable with the __index metamethod to make it more elegant and use less if

  • When you make use of getStorageValue or setStorageValue an onDeath event will be registered to the monster in question, then when the monster dies the table associated with it will be cleaned

Examples:

simple.lua
local dungeon = {
	count = 10,
	spawnPos = Position(100, 100, 7)
}

-- Dungeon spawn
for i = 1, dungeon.count do
	local monster = Game.createMonster("Dragon", dungeon.spawnPos)
	if monster then
		monster:setStorageValue("isDungeonMonster", true)
		monster:setStorageValue("extraExpPercent", math.random(105, 115)) -- 5% to 15%
	end
end

local ec = EventCallback

function ec.onGainExperience(player, source, exp, rawExp)
	if source and source:isMonster() then
		if source:getStorageValue("isDungeonMonster") then
			return exp * (source:getStorageValue("extraExpPercent") / 100)
		end
	end
	return exp
end

ec:register(--[[0]])

Example with ranisalt code for raids in lua

example.lua
local raid = GlobalEvent("Testraid")

local function event0()
    Game.broadcastMessage("Rats are attacking near Trekolt Temple!", MESSAGE_STATUS_WARNING)
end

local function event1()
    local center, radius, z = {x=94, y=126}, 5, 7
    for _ = 1, 3 do
        local x, y = math.random(center.x - radius, center.x + radius), math.random(center.y - radius, center.y + radius)
        local monster = Game.createMonster("Rat", Position(x, y, z))
        monster:setStorageValue("isFromRaid", true)
    end
end

local function event2()
    local monster = Game.createMonster("Cave Rat", Position(93, 123, 7))
    monster:setStorageValue("isFromRaid", true)
    monster:setStorageValue("extraLoot", {
    	{2160, 1}
    })
end

local function event3()
    Game.broadcastMessage("Rats attack continues!", MESSAGE_STATUS_WARNING)
end

local function event4()
    local from, to, z = {x=89, y=122}, {x=99, y=130}, 7
    for _ = 1, math.random(4, 10) do
        local x, y = math.random(from.x, to.x), math.random(from.y, to.y)
        local monster = Game.createMonster("Rat", Position(x, y, z))
        monster:setStorageValue("isFromRaid", true)
    	monster:setStorageValue("extraLoot", {
    		{2160, 5},
    		{"Demon Helmet", 1}
    	})
    end
end

local function event5()
    local monster = Game.createMonster("Cave Rat", Position(98, 125, 7))
    monster:setStorageValue("isFromRaid", true)
    monster:setStorageValue("extraLoot", {
    	{2160, 10},
    	{"Demon Armor", 1}
    })
end

local function event6()
    local monster = Game.createMonster("Cave Rat", Position(94, 128, 7))
    monster:setStorageValue("isFromRaid", true)
    monster:setStorageValue("extraLoot", {
    	{2160, 50},
    	{"Demon Leg", 1}
    })
end

function raid.onTime(interval)
    addEvent(event0, 1000)
    addEvent(event1, 2000)
    addEvent(event2, 15000)
    addEvent(event3, 25000)
    addEvent(event4, 30000)
    addEvent(event5, 30000)
    addEvent(event6, 30000)
    return true
end

raid:interval(1800)
raid:register()

local ec = EventCallback

function ec.onDropLoot(monster, corpse)
	if monster:getStorageValue("isFromRaid") then
		if not corpse or not corpse:getType():isContainer() then
			return
		end

		for _, info in pairs(monster:getStorageValue("extraLoot")) do
			corpse:addItem(info[1], info[2])
		end
	end
end

ec:register(--[[0]])

Issues addressed: Nothing!

@EPuncker EPuncker added the feature New feature or functionality label Feb 22, 2022
@EPuncker EPuncker merged commit b9759d9 into otland:master Mar 3, 2022
Codinablack pushed a commit to Codinablack/forgottenserver that referenced this pull request Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants