ID:1930562
 
(See the best response by Ter13.)
Code:
mob/verb/attack()


mob/proc

Lvlup()

Asign_Stats()

Get_Damage(magic=0)


//AI
AI_Mode()


AI_Loop()
var/check=0//counter and checks every 10 ticks
spawn()
while(src)
if(!AI_Active)
//this mob is no longer active will return to default state
loc=locate(start_x, start_y, start_z)
step_x=start_sx; step_y=start_sy
dir=start_dir
break
AI_CALL()
check++
if(check>=10)
check=0
if(!AI_Check()) {AI_Active=0}
sleep(3)


AI_Check()
var/found_player=0
for(var/mob/Player/P in view()) {found_player=1;break}
return found_player

AI_Activate()
if(!client) {return}
var/list/non_active_ais=Unactive_AI_list()
for(var/mob/M in non_active_ais)
M.AI_Active=1; M.AI_Loop()


Unactive_AI_list()
if(!client) {return}
var/list/L=list()
for(var/mob/NPC/N in view()) {if(!N.AI_Active) L+=N}
return L

mob
var
AI_Active; IS_AI=0; AI_STATUS
start_x; start_y; start_z; start_dir
start_sx; start_sy
New()
..()
start_x=x ; start_y=y ; start_z=z; start_dir=dir
start_sx=step_x ; start_sy=step_y

Player
Move()
..()
AI_Activate()//check everytime a player moves



proc
AI_CALL()
walk(src,0)
switch(AI_STATUS)
if("Wander") walk_rand(src,rand(1,10),0)
if("Wander2")
if(prob(30)) {sleep(rand(20,50))}//pause n wait
walk_rand(src,rand(4,7),0)


Problem description:
I'm starting a new ai system and i'm looking for tips. i'm not sure this would be an efficent way to start. Basically my monsters/creatures move around the map and have different ai settings and i don't want them doing anything if theres not a player arround to witness these events, what would be an acceptable way to activate the ai's?

Checking the view of the mob every 3 ticks for players is definitely not the best way to go about it, but it's one way to go about it.
It would be every 30 ticks but i couldnt figure out any other way to go about it. Would i run into alot of issues down the road if i used a system like this?
Best response
It's probably good enough for the time being. If your project gets bigger in the future, though, you'll really need to revisit this approach.

You could actually clean up the current approach quite a bit with a slight modification:

var
list/layer_players

mob
Move(NewLoc,Dir=0,Step_x,Step_y)
var/oldz = z
. = ..()
if(client&&oldz!=z)
if(oldz)
--layer_players[oldz]
if(z)
++layer_players[z]
Login()
if(!layer_players)
layer_players = list()
var/len = world.maxz
for(var/count in 1 to len)
layer_players += 0
if(z)
++layer_players[z]
. = ..()

Logout()
if(z)
--layer_players[z]


Now you can check how many players are on the monster's z-layer, which is a lot faster than checking every z-layer for every monster all the time. It'll save you a ton of cycles to check if there are any mobs on the z-layer first before checking for players in view of the monster.

Though, IMO a map chunking algorithm would be the most ideal approach for later.

I wouldn't waste the time, though thinking about the future. Just patch it up enough to work and keep moving forward.
Thanks for the help Ter13.
In short, don't check every second. Check every time a change is made (such as movement).

Isn't this essentially one of the most efficient ways too (considering there's actually more NPCs than players)? While it can also be even MORE efficient by having safezones (areas where you know there aren't going to be any NPCs that need to move) and in that area you stop your check for activating NPCs.

Edit:

To make this a bit more clearer, if there's no players around to enjoy an NPC then it shouldn't be doing things. You can achieve this doing these two simple things --

Give the NPC a procedure that checks for a mob/player in range called.. idk... Verification()?

Edit the player's Move procedure so that every time they step (after a few seconds, or literally every time if you want to go that route), check for an NPC that is nearby. If they find an NPC, run Verification() on the NPC. If verification determines that there is an player nearby, let the NPC dowork(). If there no player nearby, stop the NPC from running Verification() and stop its work.
In response to Xirre
While checking every movement is one way, I'd personally use 'regions' wherein the AI hibernate until a client enters their region, and go back to sleep when the last client leaves.