TO ATN2 :Y :X
IF (AND :Y < 2.ONO8 :Y > -2.ONO8) [IF (OR :X = O :X > 0) [OP 0] [OP 180]]
IF (AND :X < 1.ONO8 :X > -1.ONO8) [OP 90] OP ARCTANGENT :Y / :X
END

TO BUG_POS :BUGNUM
 OP ITEM 3 ITEM :BUGNUM :TURTLES
END

TO BUGRUNLIST :BUGNUM
OP (LIST "USETURTLE :BUGNUM + 1 "FOLLOW :BUGNUM + 2~
	"IF "BUGS_CLOSE :BUGNUM + 1 :BUGNUM + 2 [SELF_TERMINATE])
END

TO BUGS :NUMBUGS
CS FS
INIT_TURTLES
EXECUTE_TOGETHER DEFINEBUGS :NUMBUGS
END

TO BUGS_CLOSE :BL :B2
LOCAL "DIFF
MAKE "DIFF SUB_BVEC BUG_POS :BL BUG_POS :B2
OP (AND (1 > (ABS FIRST :DIFF)) (1 > (ABS LAST :DIFF)))
END

TO CHASE :PREY
FACE POSITION_OF :PREY
FORWARD 1
END

TO DEFBUGS :NUMBUGS :BUGNUM :VERTS
IF :NUMBUGS = 0 [OP []]
USETURTLE NEWTURTLE WORD "BUG :BUGNUM
PU SETPOS FIRST :VERTS
PD
IFELSE :NUMBUGS > 1 [MAKE WORD "BE_BUG :BUGNUM BUGRUNLIST :BUGNUM]~
	[MAKE WORD "BE_BUG :BUGNUM LASTBUGRUNLIST :BUGNUM]
 OP FPUT THING WORD "BE_BUG :BUGNUM DEFBUGS :NUMBUGS - 1 :BUGNUM + 1 BF :VERTS
END

TO DEFINEBUGS :NUMBUGS
OP DEFBUGS :NUMBUGS 1 POLYVERTS :NUMBUGS
END

TO EVADE :PREDATOR
FACE POSITION_OF :PREDATOR
LEFT 90
FORWARD 1.5
END

TO EXECUTE_TOGETHER :PROCESSLIST
LOCAL "PROCESS
;LABEL "NEXT
UNTIL [1 = 0] ~
	[
	IF EMPTYP :PROCESSLIST [STOP]
	MAKE "PROCESS FIRST :PROCESSLIST
	RUN :PROCESS
	IFELSE EMPTYP :PROCESS [MAKE "PROCESSLIST BF :PROCESSLIST]~
 	[MAKE "PROCESSLIST LPUT :PROCESS BF :PROCESSLIST]
	]
;GO "NEXT
END

TO FACE :POINT
SETHEADING TOWARDS :POINT
END

TO FIND_TURTLE_AUX :TNAME :TURTS :WHICH
IF EMPTYP :TURTS [OP O]
IF EQUALP :TNAME FIRST FIRST :TURTS [OP :WHICH]
OP FIND_TURTLE_AUX :TNAME BF :TURTS :WHICH + 1
END

TO FINDTURTLE :TNAME
IF EMPTYP :TNAME [OP 1]
OP FIND_TURTLE_AUX :TNAME :TURTLES 1
END

TO FOLLOW :BUG
FACE POSITION_OF :BUG
FORWARD 1
END

TO INFO
; MORE SIMULATED MULTIPLE TURTLES.
; WATCH THE BUGS CHASE EACH OTHER TO THE CENTER.
; TYPE BUGS AND THE NUMBER OF BUGS TO RUN.
;
PR [SEE THE INFO PROCEDURE FOR MORE INFORMATION.]
END

TO INIT_TURTLES
CS SETPC [128 128 128]
MAKE "TURTLES FPUT (LIST "LOGO 1 [0 0] 0 PEN) []
MAKE "CURTURTLE FIRST :TURTLES
MAKE "DEFTURTLEPEN PEN
;
; A KLUDGE FOR MSWLOGO: 8 PREDEFINED COLORS
; 
MAKE "PC0 [128 128 128]
MAKE "PC1 [255 0 0]
MAKE "PC2 [0 255 0]
MAKE "PC3 [0 0 255]
MAKE "PC4 [255 0 255]
MAKE "PC5 [255 128 0]
MAKE "PC6 [0 255 128]
MAKE "PC7 [128 255 255]
END

TO LASTBUGRUNLIST :BUGNUM
OP (LIST "USETURTLE :BUGNUM + 1 "FOLLOW 2 ~
	"IF "BUGS_CLOSE :BUGNUM + 1 2 [SELF_TERMINATE])
END

TO MOD :NUM :MODULO
OP REMAINDER :NUM :MODULO
END

TO NEWTURTLE :TURTLENAME
IF NOT NAMEP "TURTLES [MAKE "TURTLES []]
LOCAL "TCOUNT
MAKE "TCOUNT 1 + COUNT :TURTLES
MAKE "TURTLES LPUT (LIST :TURTLENAME :TCOUNT [0 0] 0 ~
 replace_item 4 :DEFTURTLEPEN thing word "PC mod :tcount 8 ) :TURTLES
OP :TCOUNT
END

TO POLYDRAW :VERTICES
PU SETPOS FIRST :VERTICES
MAKE "VERTICES LPUT FIRST :VERTICES BF :VERTICES
PD
REPEAT COUNT :VERTICES [SETPOS FIRST :VERTICES MAKE "VERTICES BF :VERTICES]
END

TO POLYVERTS :POLYSIZE
LOCAL "ANGLESTEP
MAKE "ANGLESTEP 360 / :POLYSIZE
LOCAL "VERTS MAKE "VERTS []
PU HT
REPEAT :POLYSIZE [FD 200
MAKE "VERTS LPUT POS :VERTS BK 200
LT :ANGLESTEP]
ST
OP :VERTS
END

TO POSITION_OF :BUG
IF NUMBERP :BUG [OP ITEM 3 (ITEM :BUG :TURTLES)]
OP ITEM 3 (ITEM (FINDTURTLE :BUG) :TURTLES)
END

TO REPLACE_ITEM :ITEM_NUM :ITEM_LIST :NEW_ITEM
IF :ITEM_LIST = [] [OP (LIST :NEW_ITEM)]
IF :ITEM_NUM = 1 [OP FPUT :NEW_ITEM BF :ITEM_LIST]
OP FPUT FIRST :ITEM_LIST REPLACE_ITEM :ITEM_NUM - 1 BF :ITEM_LIST :NEW_ITEM
END

TO SAVETURTLESTATE
MAKE "CURTURTLE (LIST FIRST :CURTURTLE ITEM 2 :CURTURTLE POS HEADING PEN)
MAKE "TURTLES REPLACE_ITEM ITEM 2 :CURTURTLE :TURTLES :CURTURTLE
END

TO SELF_TERMINATE
MAKE "PROCESS []
END

TO SUB_BVEC :V1 :V2
OP LIST (FIRST :V1) -(FIRST :V2) (LAST :V1) - (LAST :V2)
END

TO USETURTLE :T
IF NOT NUMBERP :T [MAKE "T FINDTURTLE :T]
IF (OR 1 > :T :T > COUNT :TURTLES) [(PRINT "TURTLE :T [NOT FOUND])]
SAVETURTLESTATE
MAKE "CURTURTLE ITEM :T :TURTLES
PU
SETPOS ITEM 3 :CURTURTLE
SETHEADING ITEM 4 :CURTURTLE
SETPEN ITEM 5 :CURTURTLE
END

Make "be_bug1 [USETURTLE 2 FOLLOW 3 IF BUGS_CLOSE 2 3 [SELF_TERMINATE]]
Make "be_bug2 [USETURTLE 3 FOLLOW 4 IF BUGS_CLOSE 3 4 [SELF_TERMINATE]]
Make "be_bug3 [USETURTLE 4 FOLLOW 5 IF BUGS_CLOSE 4 5 [SELF_TERMINATE]]
Make "be_bug4 [USETURTLE 5 FOLLOW 6 IF BUGS_CLOSE 5 6 [SELF_TERMINATE]]
Make "be_bug5 [USETURTLE 6 FOLLOW 7 IF BUGS_CLOSE 6 7 [SELF_TERMINATE]]
Make "be_bug6 [USETURTLE 7 FOLLOW 2 IF BUGS_CLOSE 7 2 [SELF_TERMINATE]]
Make "be_bug7 [USETURTLE 8 FOLLOW 9 IF BUGS_CLOSE 8 9 [SELF_TERMINATE]]
Make "be_bug8 [USETURTLE 9 FOLLOW 2 IF BUGS_CLOSE 9 2 [SELF_TERMINATE]]
Make "bugdemo [BUG DEMO 1.0 5 / 19 / 91 DCD]
Make "curturtle [BUG1 2 [0.542737 0.675328] 320.888490401976 [pendown paint [1 1] [0 128 0] [1]]]
Make "defturtlepen [pendown paint [1 1] [128 128 128] [1]]
Make "pc0 [128 128 128]
Make "pc1 [128 0 0]
Make "pc2 [0 128 0]
Make "pc3 [0 0 128]
Make "pc4 [128 0 128]
Make "pc5 [128 128 0]
Make "pc6 [0 128 128]
Make "pc7 [64 128 128]
Make "predator [USETURTLE 2 CHASE 3 IF BUGSS_CLOSE 2 3 [SELF_TERMINATE]]
Make "prey [USETURTLE 3 EVADE 2 IF BUGS_CLOSE 2 3 [SELF_TERMINATE]]
Make "startup [info]
Make "turtles [[LOGO 1 [0 0] 0 [penup paint [1 1] [128 128 128] [1]]] [BUG1 2 [0.542737 0.675328] 320.888490401976 [pendown paint [1 1] [0 128 0] [1]]] [BUG2 3 [-0.538811 0.523852] 251.70056120463 [pendown paint [1 1] [0 0 128] [1]]] [BUG3 4 [-0.714746 0.46567] 235.250298832095 [pendown paint [1 1] [128 0 128] [1]]] [BUG4 5 [-0.760538 -0.696337] 167.022892685729 [pendown paint [1 1] [128 128 0] [1]]] [BUG5 6 [0.294687 -1.133089] 98.5572184102539 [pendown paint [1 1] [0 128 128] [1]]] [BUG6 7 [0.55659 -1.172498] 78.207585623679 [pendown paint [1 1] [64 128 128] [1]]]]
Make "turtlestates {[pendown paint [1 1] [0 0 0] [1]] [pendown paint [1 1] [0 0 0] [1]] [pendown paint [1 1] [0 0 0] [1]] [pendown paint [1 1] [0 0 0] [1]] [pendown paint [1 1] [0 0 0] [1]]}@0
