diff --git a/unmaintained/boids/authors.txt b/extra/boids/authors.txt similarity index 53% rename from unmaintained/boids/authors.txt rename to extra/boids/authors.txt index 6cfd5da273..0eab06eae8 100644 --- a/unmaintained/boids/authors.txt +++ b/extra/boids/authors.txt @@ -1 +1,2 @@ Eduardo Cavazos +Anton Gorenko diff --git a/extra/boids/boids.factor b/extra/boids/boids.factor new file mode 100644 index 0000000000..27afc062b3 --- /dev/null +++ b/extra/boids/boids.factor @@ -0,0 +1,159 @@ +! Copyright (C) 2008 Eduardo Cavazos. +! Copyright (C) 2011 Anton Gorenko. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors arrays boids.simulation calendar classes kernel +literals locals math math.functions math.trig models namespaces +opengl opengl.demo-support opengl.gl sequences threads ui +ui.gadgets ui.gadgets.borders ui.gadgets.buttons +ui.gadgets.frames ui.gadgets.grids ui.gadgets.labeled +ui.gadgets.labels ui.gadgets.packs ui.gadgets.sliders ui.render ; +QUALIFIED-WITH: models.range mr +IN: boids + +TUPLE: boids-gadget < gadget paused boids behaviours dt ; + +CONSTANT: initial-population 100 +CONSTANT: initial-dt 5 + +: initial-behaviours ( -- seq ) + 1.0 75 -0.1 + 1.0 40 -0.5 + 1.0 25 -1.0 + 3array ; + +: ( -- gadget ) + boids-gadget new + t >>clipped? + ${ width height } >>pref-dim + initial-population random-boids >>boids + initial-behaviours >>behaviours + initial-dt >>dt ; + +M: boids-gadget ungraft* + t >>paused drop ; + +: vec>deg ( vec -- deg ) + first2 rect> arg rad>deg ; inline + +: draw-boid ( boid -- ) + dup pos>> [ + vel>> vec>deg 0 0 1 glRotated + GL_TRIANGLES [ + -6.0 4.0 glVertex2f + -6.0 -4.0 glVertex2f + 8.0 0.0 glVertex2f + ] do-state + ] with-translation ; + +: draw-boids ( boids -- ) + 0.0 0.0 0.0 0.5 glColor4f + [ draw-boid ] each ; + +M:: boids-gadget draw-gadget* ( boids-gadget -- ) + origin get + [ boids-gadget boids>> draw-boids ] with-translation ; + +: iterate-system ( boids-gadget -- ) + dup [ boids>> ] [ behaviours>> ] [ dt>> ] tri + simulate >>boids drop ; + +:: start-boids-thread ( gadget -- ) + [ + [ gadget paused>> ] + [ + gadget iterate-system + gadget relayout-1 + 10 milliseconds sleep + ] until + ] in-thread ; + +TUPLE: range-observer quot ; + +M: range-observer model-changed + [ range-value ] dip quot>> call( value -- ) ; + +: connect ( range-model quot -- ) + range-observer boa swap add-connection ; + +:: behavior-panel ( behavior -- gadget ) + 2 3 { 2 4 } >>gap { 0 0 } >>filled-cell + + "weight"