ID:931166
 
(See the best response by DarkCampainger.)
Code:
aiLoop()
set background = 1
while(AI_ON)
//Regeneration Step
if((world.timeofday % 50) == 0)
regenerate()
//Attack/Harass Step
if(alert && (world.timeofday % stats["attackSpeed"]) == 0)
if(attackTarget) //Attack branch
var /mob/hero/target = attackTarget
walk_to(src, target, stats["attackRange"])
if(target)
world << "[src] attacked [target]!"
attack(target)
else //Harass branch
harass()
sleep(1)


Problem description:

This is an early implementation of my upkeep loop for certain mobs in the game. Would I be safer using my (already coded) cooldown system?

I seem to be getting a lot of misfires (where the modulo returns 0 multiple times within one second). I can understand lag causing problems, but it appears that the sleep isn't lasting long enough.

I've also tried not running this in the background, with the same result.
Best response
For something like this, you may be better of with world.time, because it tracks server ticks instead of actual time (so it should be synced with your sleep()s).