From a6601cd2ddf565198bd35765c7eecd97df265c28 Mon Sep 17 00:00:00 2001 From: SkyratBot <59378654+SkyratBot@users.noreply.github.com> Date: Tue, 17 Oct 2023 07:23:16 +0200 Subject: [PATCH] [MIRROR] Reduces the average delay between random events, cleans up event subsystem code [MDB IGNORE] (#24378) * Reduces the average delay between random events, cleans up event subsystem code * Update events.dm * Update events.dm --------- Co-authored-by: Rhials <28870487+Rhials@users.noreply.github.com> Co-authored-by: Bloop <13398309+vinylspiders@users.noreply.github.com> --- code/controllers/subsystem/events.dm | 63 ++++++++++++++-------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/code/controllers/subsystem/events.dm b/code/controllers/subsystem/events.dm index c5223ca6963..98f847e4be6 100644 --- a/code/controllers/subsystem/events.dm +++ b/code/controllers/subsystem/events.dm @@ -2,15 +2,19 @@ SUBSYSTEM_DEF(events) name = "Events" init_order = INIT_ORDER_EVENTS runlevels = RUNLEVEL_GAME - - var/list/control = list() //list of all datum/round_event_control. Used for selecting events based on weight and occurrences. - var/list/running = list() //list of all existing /datum/round_event + ///list of all datum/round_event_control. Used for selecting events based on weight and occurrences. + var/list/control = list() + ///list of all existing /datum/round_event currently being run. + var/list/running = list() + ///cache of currently running events, for lag checking. var/list/currentrun = list() - - var/scheduled = 0 //The next world.time that a naturally occuring random event can be selected. - var/frequency_lower = 1800 //3 minutes lower bound. - var/frequency_upper = 6000 //10 minutes upper bound. Basically an event will happen every 3 to 10 minutes. - + ///The next world.time that a naturally occuring random event can be selected. + var/scheduled = 0 + ///The lower bound for how soon another random event can be scheduled. + var/frequency_lower = 2.5 MINUTES + ///The upper bound for how soon another random event can be scheduled. + var/frequency_upper = 7 MINUTES + ///Will wizard events be included in the event pool? var/wizardmode = FALSE var/list/previously_run = list() //SKYRAT EDIT ADDITION @@ -61,50 +65,45 @@ SUBSYSTEM_DEF(events) if(!CONFIG_GET(flag/allow_random_events)) return - var/players_amt = get_active_player_count(alive_check = 1, afk_check = 1, human_check = 1) + var/players_amt = get_active_player_count(alive_check = TRUE, afk_check = TRUE, human_check = TRUE) // Only alive, non-AFK human players count towards this. - var/sum_of_weights = 0 - for(var/datum/round_event_control/E in control) - if(!E.can_spawn_event(players_amt)) + var/list/event_roster = list() + + for(var/datum/round_event_control/event_to_check in control) + if(!event_to_check.can_spawn_event(players_amt)) continue //SKYRAT EDIT ADDITION - if(threat_override && !E.alert_observers) + if(threat_override && !event_to_check.alert_observers) continue //SKYRAT EDIT END - if(E.weight < 0) //for round-start events etc. - var/res = TriggerEvent(E) + if(event_to_check.weight < 0) //for round-start events etc. + var/res = TriggerEvent(event_to_check) if(res == EVENT_INTERRUPTED) continue //like it never happened if(res == EVENT_CANT_RUN) return - sum_of_weights += E.weight - - sum_of_weights = rand(0,sum_of_weights) //reusing this variable. It now represents the 'weight' we want to select - - for(var/datum/round_event_control/E in control) - if(!E.can_spawn_event(players_amt)) - continue - sum_of_weights -= E.weight + else + event_roster[event_to_check] = event_to_check.weight - if(sum_of_weights <= 0) //we've hit our goal - if(TriggerEvent(E)) - return + var/datum/round_event_control/event_to_run = pick_weight(event_roster) + TriggerEvent(event_to_run) -/datum/controller/subsystem/events/proc/TriggerEvent(datum/round_event_control/E) - . = E.preRunEvent() +///Does the last pre-flight checks for the passed event, and runs it if the event is ready. +/datum/controller/subsystem/events/proc/TriggerEvent(datum/round_event_control/event_to_trigger) + . = event_to_trigger.preRunEvent() if(. == EVENT_CANT_RUN)//we couldn't run this event for some reason, set its max_occurrences to 0 - E.max_occurrences = 0 + event_to_trigger.max_occurrences = 0 else if(. == EVENT_READY) - E.run_event(random = TRUE) - + event_to_trigger.run_event(random = TRUE) +///Toggles whether or not wizard events will be in the event pool, and sends a notification to the admins. /datum/controller/subsystem/events/proc/toggleWizardmode() wizardmode = !wizardmode message_admins("Summon Events has been [wizardmode ? "enabled, events will occur every [SSevents.frequency_lower / 600] to [SSevents.frequency_upper / 600] minutes" : "disabled"]!") log_game("Summon Events was [wizardmode ? "enabled" : "disabled"]!") - +///Sets the event frequency bounds back to their initial value. /datum/controller/subsystem/events/proc/resetFrequency() frequency_lower = initial(frequency_lower) frequency_upper = initial(frequency_upper)