Skip to content

Commit

Permalink
AI Controller MC Yielding (tgstation#87094)
Browse files Browse the repository at this point in the history
## About The Pull Request

AI controllers do not respect their tick limits, behaving as if they
need to process all controllers in one tick.

This means every time the idle subsystem runs we process 500 controllers
at once. This is bad, and induces overtime for free, which sucks.

I can't think of a reason we would need to do all this work at once.
  • Loading branch information
LemonInTheDark authored Oct 8, 2024
1 parent 998e1e0 commit 91d9827
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions code/controllers/subsystem/ai_controllers.dm
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ SUBSYSTEM_DEF(ai_controllers)
runlevels = RUNLEVEL_GAME | RUNLEVEL_POSTGAME
///type of status we are interested in running
var/planning_status = AI_STATUS_ON
/// The tick cost of all active AI, calculated on fire.
/// The average tick cost of all active AI, calculated on fire.
var/our_cost
/// The tick cost of all currently processed AI, being summed together
var/summing_cost

/datum/controller/subsystem/ai_controllers/Initialize()
setup_subtrees()
Expand All @@ -21,6 +23,8 @@ SUBSYSTEM_DEF(ai_controllers)
return ..()

/datum/controller/subsystem/ai_controllers/fire(resumed)
if(!resumed)
summing_cost = 0
var/timer = TICK_USAGE_REAL
for(var/datum/ai_controller/ai_controller as anything in GLOB.ai_controllers_by_status[planning_status])
if(!ai_controller.able_to_plan)
Expand All @@ -30,7 +34,14 @@ SUBSYSTEM_DEF(ai_controllers)
if(!length(ai_controller.current_behaviors)) //Still no plan
ai_controller.planning_failed()

our_cost = MC_AVERAGE(our_cost, TICK_DELTA_TO_MS(TICK_USAGE_REAL - timer))
if(MC_TICK_CHECK)
break

summing_cost += TICK_DELTA_TO_MS(TICK_USAGE_REAL - timer)
if(MC_TICK_CHECK)
return

our_cost = MC_AVERAGE(our_cost, summing_cost)

///Creates all instances of ai_subtrees and assigns them to the ai_subtrees list.
/datum/controller/subsystem/ai_controllers/proc/setup_subtrees()
Expand Down

0 comments on commit 91d9827

Please sign in to comment.