ID:139818
 
Chicken Code:
chicken
icon = 'animals.dmi'
icon_state = "chick"
name = "Chicken"
density = 0

MaxHealth = 15
Health = 15
Strength = 7
Defense = 1
Level = 1
Gold = 5
Alignment = "Good"
Kills = 0
Category = "Critter"

New()
startX = x
startY = y
startZ = z

while(src)
chickenRun()
if(prob(20))
flick("chickpeck",src)


chickenRun Code:
mob/ai/proc
chickenRun()
for(var/mob/M in oview(1))
if(M.client)
walk_away(src, M, 1, 3)
else
walk_rand(src,30)


Problem description:
Right now, running the game is extremely laggy. So laggy, in fact, that it takes a good 40 seconds just to start it. I'm not quite sure what's wrong with this code.

This is because your loop constantly runs, hogging the game's available CPU time.
All long (execution-time wise) loops should have delays at least once in a while, to allow other things to run in the meantime as well - infinite or psuedo-infinite loops like what you have certainly must have one.
for() //infinite loop
world << "How is everyone doing?"
sleep(10) //wait one second

The above loop has a space of one second between its iterations, as in the end of every iteration, it sleeps (waits) for a second before continuing to the next one.

Note that in your code, you're also misusing walk() and abusing usr (because your oview() call defaults to using Center=usr, look it up).
In response to Kaioken
Oh dear, how do I do this without calling usr? A lot of the examples I've been given have used oview().

I replaced the walk_away with step_away.
In response to Yazz
oview(src,range)
In response to Yazz
The 'call' term is used for functions (procedures - proc or verbs). Vars are merely 'accessed' or similar.

Yazz wrote:
Oh dear, how do I do this without <s>calling</s> using usr?

You use a different variable instead.
The view() series procs have 2 arguments: the Center (an atom object) and the range/distance (a number). Both are optional and may be supplied in any order - if they are omitted, they're set to certain default values. In non-mouse procs, you want to avoid the Center argument being assigned to the default value of usr, therefore you specify a different one as it.

A lot of the examples I've been given have used oview().

That's not really relevant.
Perhaps you don't understand procs and their parameters (proc arguments)?
http://www.byond.com/members/?command=reference&path=proc
http://www.byond.com/ members/?command=reference&path=proc%2Farguments
http://www.byond.com/docs/guide/chap06.html
http://www.byond.com/ members/?command=reference&path=proc%2Fview
In response to Garthor
It's been changed as of around v.470

oview(Range,Center)

Center also defaults to usr as well now.
mob/Chicken/New()
spawn ChickenRun()
mob/proc/ChickenRun()
while(src)
for(var/mob/P in oview(3,src))
step_away(src,P)
break
sleep(10)


This accomplishes what you asked. Just make your code work like this does more or less. This uses spaces not tabs so you can't paste it.
In response to Gigimoi
1) The order of arguments in the view() and range() set of functions can be either way, doesn't matter. Has been like this for as long as I remember.


2) It has always defaulted to usr, not just recently.
In response to Im Batman
Thanks for giving me direct code, but it doesn't seem to work at all towards my purpose.

The idea is that there are chickens in a pen. Let's say about 16. during regular game play they wander around their pen with a 20 percent chance of pecking at the ground. If a player comes by, they can chase the chickens around and try to attack them, but whenever they get close to the chickens, they scatter.

I got it to almost work perfectly, but the chickens tended to pop around instead of actually walk.

Again, thanks everyone for being helpful, but the issue isn't lag anymore, it's getting the desired effect right.