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

Move raid system to globalevents #1813

Merged
merged 11 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion data/lib/compat/compat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ do
self:onThink(value)
return
elseif key == "onTime" then
self:type("timer")
self:onTime(value)
return
elseif key == "onStartup" then
Expand Down Expand Up @@ -1333,8 +1334,10 @@ function doSetGameState(state)
end

function doExecuteRaid(raidName)
return Game.startRaid(raidName)
debugPrint("Deprecated function, use Game.startEvent('" .. raidName .. "') instead.")
return Game.startEvent(raidName)
end
Game.startRaid = doExecuteRaid

function Game.convertIpToString(ip)
print("[Warning - " .. debug.getinfo(2).source:match("@?(.*)") .. "] Function Game.convertIpToString is deprecated and will be removed in the future. Use the return value of player:getIp() instead.")
Expand Down
43 changes: 43 additions & 0 deletions data/lib/core/raids.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
local raids = {}

Raid = setmetatable({
getRaids = function() return raids end
}, {
__call = function(self, name)
local events = {}

local obj = {
margin = 0,
name = name,
repeats = false,
}

function obj:__newindex(key, value)
if key == "interval" or key == "margin" or key == "repeats" then
rawset(self, key, value)
else
io.write("[Warning] Invalid attribute for raid: " .. key .. ". Ignoring...\n")
end
end

function obj:addEvent(delay, fn)
events[#events] = { delay = delay, fn = fn }
end

function obj:execute()
for _, event in ipairs(events) do
addEvent(event.delay, event.fn)
end
end

function obj:register()
raids[self.name] = self
end

function obj:unregister()
raids[self.name] = nil
end

return obj
end
})
7 changes: 0 additions & 7 deletions data/raids/raids.xml

This file was deleted.

21 changes: 0 additions & 21 deletions data/raids/testraid.xml

This file was deleted.

51 changes: 51 additions & 0 deletions data/scripts/globalevents/#testraid.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
local raid = GlobalEvent("Testraid")
raid:interval(1800)

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)
Game.createMonster("Rat", Position(x, y, z))
end
end

local function event2()
Game.createMonster("Cave Rat", Position(93, 123, 7))
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)
Game.createMonster("Rat", Position(x, y, z))
end
end

local function event5()
Game.createMonster("Cave Rat", Position(98, 125, 7))
end

local function event6()
Game.createMonster("Cave Rat", Position(94, 128, 7))
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:register()
36 changes: 36 additions & 0 deletions data/scripts/globalevents/raids.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
local event = GlobalEvent("raids")

local CHECK_RAIDS_INTERVAL = 60
local MAX_RAND_RANGE = 10000000

event:interval(CHECK_RAIDS_INTERVAL)

local running = nil
local lastRaidEnd = 0

function event.onTime(interval)
io.write("Executing raids event...\n")
if running then
return
end

local now = os.mtime()

local raids = Raid.getRaids()
for key, raid in pairs(raids) do
local chance = (MAX_RAND_RANGE * CHECK_RAIDS_INTERVAL) / raid.interval
if now >= lastRaidEnd + raid.margin and chance >= math.random(0, MAX_RAND_RANGE) then
running = key

io.write("Executing raid: " .. raid.name .. "\n")
raid:execute()

if not raid.repeats then
raids[key] = nil
end
break
end
end
end

event:register()
Loading
Loading