2022-10-22 21:36:23 +00:00
|
|
|
mcl_events = {}
|
|
|
|
mcl_events.registered_events = {}
|
2022-11-09 23:14:35 +00:00
|
|
|
local disabled_events = minetest.settings:get("mcl_disabled_events")
|
|
|
|
if disabled_events then disabled_events = disabled_events:split(",")
|
|
|
|
else disabled_events = {} end
|
2022-10-24 15:37:11 +00:00
|
|
|
local DBG = minetest.settings:get_bool("mcl_logging_event_api",false)
|
2022-10-22 21:36:23 +00:00
|
|
|
local active_events = {}
|
|
|
|
|
2022-11-23 22:09:10 +00:00
|
|
|
local event_tpl = {
|
2022-10-23 02:03:05 +00:00
|
|
|
stage = 0,
|
2022-10-22 21:36:23 +00:00
|
|
|
max_stage = 1,
|
2022-10-23 01:44:15 +00:00
|
|
|
percent = 100,
|
2022-10-23 02:03:05 +00:00
|
|
|
bars = {},
|
2022-10-22 21:36:23 +00:00
|
|
|
completed = false,
|
|
|
|
cond_start = function(event) end, --return table of positions
|
|
|
|
on_step = function(event) end,
|
|
|
|
on_start = function(event) end,
|
|
|
|
on_stage_begin = function(event) end,
|
|
|
|
cond_progress = function(event) end, --return next stage
|
|
|
|
cond_complete = function(event) end, --return success
|
|
|
|
}
|
|
|
|
|
2022-10-24 15:37:11 +00:00
|
|
|
local function mcl_log(m,l)
|
|
|
|
if DBG then
|
|
|
|
if not l then l = "action" end
|
|
|
|
minetest.log(l,"[mcl_events] "..m)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-22 21:36:23 +00:00
|
|
|
function mcl_events.register_event(name,def)
|
2022-11-09 23:14:35 +00:00
|
|
|
if table.indexof(disabled_events,name) ~= -1 then return end
|
2022-10-22 21:36:23 +00:00
|
|
|
mcl_events.registered_events[name] = def
|
|
|
|
mcl_events.registered_events[name].name = name
|
|
|
|
end
|
|
|
|
|
2022-10-23 01:44:15 +00:00
|
|
|
local function addbars(self)
|
2022-11-04 20:28:51 +00:00
|
|
|
if not self.enable_bossbar then return end
|
2022-10-23 01:44:15 +00:00
|
|
|
for _,player in pairs(minetest.get_connected_players()) do
|
2022-11-24 23:25:45 +00:00
|
|
|
if vector.distance(self.pos,player:get_pos()) < 64 then
|
2022-11-04 20:28:51 +00:00
|
|
|
local bar = mcl_bossbars.add_bar(player, {color = "red", text = self.readable_name .. ": Wave "..self.stage.." / "..self.max_stage, percentage = self.percent }, true,1)
|
2022-10-23 01:44:15 +00:00
|
|
|
table.insert(self.bars,bar)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-22 21:36:23 +00:00
|
|
|
local function start_event(p,e)
|
2022-11-04 20:28:51 +00:00
|
|
|
mcl_log("[mcl_events] Event started: "..e.readable_name.." at "..minetest.pos_to_string(vector.round(p.pos)))
|
2022-10-22 21:36:23 +00:00
|
|
|
local idx = #active_events + 1
|
|
|
|
active_events[idx] = table.copy(e)
|
2022-11-23 22:09:10 +00:00
|
|
|
setmetatable(active_events[idx],{__index = event_tpl})
|
2022-10-24 22:30:14 +00:00
|
|
|
for k,v in pairs(p) do active_events[idx][k] = v end
|
2022-10-23 02:03:05 +00:00
|
|
|
active_events[idx].stage = 0
|
|
|
|
active_events[idx].percent = 100
|
|
|
|
active_events[idx].bars = {}
|
2022-10-22 21:36:23 +00:00
|
|
|
active_events[idx].time_start = os.time()
|
2022-11-18 06:02:00 +00:00
|
|
|
if active_events[idx].on_start then
|
|
|
|
active_events[idx]:on_start(p.pos)
|
|
|
|
end
|
2022-10-23 01:44:15 +00:00
|
|
|
addbars(active_events[idx])
|
2022-10-22 21:36:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function finish_event(self,idx)
|
2022-11-04 20:28:51 +00:00
|
|
|
mcl_log("[mcl_events] Finished: "..self.readable_name.." at "..minetest.pos_to_string(vector.round(self.pos)))
|
2022-10-22 21:36:23 +00:00
|
|
|
if self.on_complete then self:on_complete() end
|
2022-10-23 01:44:15 +00:00
|
|
|
for _,b in pairs(self.bars) do
|
|
|
|
mcl_bossbars.remove_bar(b)
|
|
|
|
end
|
2022-10-22 21:36:23 +00:00
|
|
|
table.remove(active_events,idx)
|
|
|
|
end
|
|
|
|
|
|
|
|
local etime = 0
|
|
|
|
function check_events(dtime)
|
2022-11-03 13:10:30 +00:00
|
|
|
--process active events
|
2022-10-22 21:36:23 +00:00
|
|
|
for idx,ae in pairs(active_events) do
|
|
|
|
if ae.cond_complete and ae:cond_complete() then
|
2022-10-22 22:13:28 +00:00
|
|
|
ae.finished = true
|
2022-10-22 21:36:23 +00:00
|
|
|
finish_event(ae,idx)
|
2022-10-22 22:13:28 +00:00
|
|
|
elseif not ae.cond_complete and ae.max_stage and ae.max_stage <= ae.stage then
|
|
|
|
ae.finished = true
|
2022-10-22 21:36:23 +00:00
|
|
|
finish_event(ae,idx)
|
2022-10-22 22:13:28 +00:00
|
|
|
elseif not ae.finished and ae.cond_progress then
|
2022-10-22 21:36:23 +00:00
|
|
|
local p = ae:cond_progress()
|
|
|
|
if p == true then
|
|
|
|
ae.stage = ae.stage + 1
|
2022-11-17 04:47:48 +00:00
|
|
|
if ae:on_stage_begin() == true then
|
|
|
|
mcl_log("[mcl_events] Event "..ae.readable_name.." at "..minetest.pos_to_string(vector.round(ae.pos)).." failed at stage_begin of stage "..ae.stage )
|
|
|
|
active_events[idx] = nil
|
|
|
|
end
|
2022-10-22 21:36:23 +00:00
|
|
|
elseif tonumber(p) then
|
|
|
|
ae.stage = tonumber(p) or ae.stage + 1
|
|
|
|
ae:on_stage_begin()
|
|
|
|
end
|
2022-10-22 22:13:28 +00:00
|
|
|
elseif not ae.finished and ae.on_step then
|
2022-11-18 06:02:00 +00:00
|
|
|
ae:on_step(dtime)
|
2022-10-22 21:36:23 +00:00
|
|
|
end
|
2022-10-23 01:44:15 +00:00
|
|
|
addbars(ae)
|
2022-10-22 21:36:23 +00:00
|
|
|
end
|
2022-11-03 13:10:30 +00:00
|
|
|
-- check if a new event should be started
|
2022-10-22 21:36:23 +00:00
|
|
|
etime = etime - dtime
|
|
|
|
if etime > 0 then return end
|
|
|
|
etime = 10
|
|
|
|
for _,e in pairs(mcl_events.registered_events) do
|
|
|
|
local pp = e.cond_start()
|
|
|
|
if pp then
|
|
|
|
for _,p in pairs(pp) do
|
2022-10-24 22:30:14 +00:00
|
|
|
local start = true
|
|
|
|
if e.exclusive_to_area then
|
|
|
|
for _,ae in pairs(active_events) do
|
|
|
|
if e.name == ae.name and vector.distance(p.pos,ae.pos) < e.exclusive_to_area then start = false end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if start then
|
|
|
|
start_event(p,e)
|
|
|
|
elseif DBG then
|
2022-11-04 20:28:51 +00:00
|
|
|
mcl_log("[mcl_events] Event "..e.readable_name.." already active at "..minetest.pos_to_string(vector.round(p.pos)))
|
2022-10-24 22:30:14 +00:00
|
|
|
end
|
2022-10-22 21:36:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-11-08 23:56:42 +00:00
|
|
|
for idx,ae in pairs(active_events) do
|
|
|
|
local player_near = false
|
|
|
|
for _,pl in pairs(minetest.get_connected_players()) do
|
2022-11-24 23:25:45 +00:00
|
|
|
if ae.pos and vector.distance(pl:get_pos(),ae.pos) < 64 then player_near = true end
|
2022-11-08 23:56:42 +00:00
|
|
|
end
|
|
|
|
if ae.pos and not player_near then
|
2022-11-17 04:39:55 +00:00
|
|
|
mcl_log("[mcl_events] Event "..ae.readable_name.." at "..minetest.pos_to_string(vector.round(ae.pos)).." aborted - no players near." )
|
|
|
|
active_events[idx] = nil
|
2022-11-08 23:56:42 +00:00
|
|
|
end
|
|
|
|
end
|
2022-10-22 21:36:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_globalstep(check_events)
|
|
|
|
|
2022-11-17 19:06:00 +00:00
|
|
|
mcl_info.register_debug_field("Active Events",{
|
|
|
|
level = 4,
|
|
|
|
func = function(pl,pos)
|
|
|
|
return tostring(#active_events)
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2022-11-03 13:10:30 +00:00
|
|
|
minetest.register_chatcommand("event_start",{
|
2022-10-22 21:36:23 +00:00
|
|
|
privs = {debug = true},
|
2022-11-15 23:56:05 +00:00
|
|
|
description = "Debug command to start events",
|
2022-11-03 13:10:30 +00:00
|
|
|
func = function(pname,param)
|
|
|
|
local p = minetest.get_player_by_name(pname)
|
|
|
|
local evdef = mcl_events.registered_events[param]
|
2022-11-15 23:56:05 +00:00
|
|
|
if not evdef then return false,"Event "..param.." doesn't exist.'" end
|
2022-11-03 13:10:30 +00:00
|
|
|
start_event({pos=p:get_pos(),player=pname,factor=1},evdef)
|
2022-11-15 23:56:05 +00:00
|
|
|
return true,"Started event "..param
|
2022-10-22 21:36:23 +00:00
|
|
|
end,
|
|
|
|
})
|