Currently, I've got this:
Code:
script, flock sheep, begin
#Variable declarations
#=====================
variable(
shc #Sheep Count, the total number of sheep
shr #Sheep Reference, this is for the current sheep
sht #Sheep Target, this is for the target sheep
curX #The current X value
curY #The current Y value
minX #The minimum X value
maxX #The maximum X value
minY #The minimum Y value
maxY #The maximum Y value
ctr1 #A counter used for working with the current sheep
ctr2 #A counter used for the sheep that the current one will be compared to
x1 #The starting X value
x2 #The target Y value
y1 #The starting Y value
y2 #The target Y value
wx #The working X value
wy #The working Y value
wt #The working total sheep
sr #Search Radius, how far to search for a sheep
dist #The distance between the current sheep and the target sheep
)
#=====================
#Generate variables
#==================
minX := 0
minY := 0
maxX := map width
maxY := map height
sr := 16
shc := npc copy count(npc:sheep)
#==================
#This block will run through all of the sheep on the map
#=======================================================
for(ctr1, 0, shc) do(
shr := npc reference(npc:sheep, ctr1) #Get the reference of the current sheep
x1 := npc x(shr) #Get the X coordinate of the current sheep
y1 := npc y(shr) #Get the Y coordinate of the current sheep
wt := 1 #Reset the total sheep found
wx := x1 #Reset the average X value of the sheep
wy := y1 #Reset the average Y value of the sheep
#This block will compare the current sheep to all the other sheep on the map
#---------------------------------------------------------------------------
for(ctr2, 0, shc) do(
sht := npc reference(npc:sheep, ctr2) #Get the reference of the target sheep
x2 := npc x(sht) #Get the X coordinate of the target sheep
y2 := npc y(sht) #Get the Y coordinate of the target sheep
trace value(x1, y1)
trace value(x2, y2)
dist := distance(x1, y1, x2, y2) #Get the distance between the current sheep and the target sheep
trace value(ctr2, dist)
if(dist <= sr) then( #If the distance is below the Search Radius, do the following
wt += 1 #Increment the total number of sheep found
wx += x2 #Add the target sheep's X coordinate for averaging later
wy += y2 #Add the target sheep's Y coordinate for averaging later
trace value(wx, wy)
)
)
#---------------------------------------------------------------------------
#Once the above is finished, the X and Y coordinates will be averaged to
#find a new point for the sheep to move towards.
wx := wx / wt #Get the average X coordinate
wy := wy / wt #Get the average Y coordinate
#This will then make the sheep walk towards that point before resuming its Wander movement
if(npc is walking(shr) == FALSE) then(
walk npc to x(shr, wx)
walk npc to y(shr, wy)
)
)
#=======================================================
set timer(timer:sheep, 1, 18, @flock sheep) #This resets the timer.
end
script, flock sheep, begin
#Variable declarations
#=====================
variable(
shc #Sheep Count, the total number of sheep
shr #Sheep Reference, this is for the current sheep
sht #Sheep Target, this is for the target sheep
curX #The current X value
curY #The current Y value
minX #The minimum X value
maxX #The maximum X value
minY #The minimum Y value
maxY #The maximum Y value
ctr1 #A counter used for working with the current sheep
ctr2 #A counter used for the sheep that the current one will be compared to
x1 #The starting X value
x2 #The target Y value
y1 #The starting Y value
y2 #The target Y value
wx #The working X value
wy #The working Y value
wt #The working total sheep
sr #Search Radius, how far to search for a sheep
dist #The distance between the current sheep and the target sheep
)
#=====================
#Generate variables
#==================
minX := 0
minY := 0
maxX := map width
maxY := map height
sr := 16
shc := npc copy count(npc:sheep)
#==================
#This block will run through all of the sheep on the map
#=======================================================
for(ctr1, 0, shc) do(
shr := npc reference(npc:sheep, ctr1) #Get the reference of the current sheep
x1 := npc x(shr) #Get the X coordinate of the current sheep
y1 := npc y(shr) #Get the Y coordinate of the current sheep
wt := 1 #Reset the total sheep found
wx := x1 #Reset the average X value of the sheep
wy := y1 #Reset the average Y value of the sheep
#This block will compare the current sheep to all the other sheep on the map
#---------------------------------------------------------------------------
for(ctr2, 0, shc) do(
sht := npc reference(npc:sheep, ctr2) #Get the reference of the target sheep
x2 := npc x(sht) #Get the X coordinate of the target sheep
y2 := npc y(sht) #Get the Y coordinate of the target sheep
trace value(x1, y1)
trace value(x2, y2)
dist := distance(x1, y1, x2, y2) #Get the distance between the current sheep and the target sheep
trace value(ctr2, dist)
if(dist <= sr) then( #If the distance is below the Search Radius, do the following
wt += 1 #Increment the total number of sheep found
wx += x2 #Add the target sheep's X coordinate for averaging later
wy += y2 #Add the target sheep's Y coordinate for averaging later
trace value(wx, wy)
)
)
#---------------------------------------------------------------------------
#Once the above is finished, the X and Y coordinates will be averaged to
#find a new point for the sheep to move towards.
wx := wx / wt #Get the average X coordinate
wy := wy / wt #Get the average Y coordinate
#This will then make the sheep walk towards that point before resuming its Wander movement
if(npc is walking(shr) == FALSE) then(
walk npc to x(shr, wx)
walk npc to y(shr, wy)
)
)
#=======================================================
set timer(timer:sheep, 1, 18, @flock sheep) #This resets the timer.
end
Currently, the code works. Sheep will generally just wander around the landscape minding their own business. When another sheep gets within range however, the average X and Y values of all the sheep in range will be calculated. The sheep will then head for that new value. This happens every 18 ticks.
My problem is, that although it does work, it does seem to be rather slow and clunky to operate. I was wondering if anyone had any suggestions as to how I might optimize the code (or even if I'm doing this correctly).
Thanks in advance, cheers!
Being from the third world, I reserve the right to speak in the third person.
Using Editor version wip 20170527 gfx_sdl+fb music_sdl




Would not have guessed the cost of assignment was the same as a math operator.