diff --git a/actors/slush.go b/actors/slush.go index 598eb6b..c1e0f39 100644 --- a/actors/slush.go +++ b/actors/slush.go @@ -8,33 +8,34 @@ import ( type Slush struct { color int + Probability float32 } func (sm *Slush) Setup(env *core.Environment, t *core.Turtle) { - sm.color = rand.Intn(2) + 1 - if sm.color !=1 && sm.color !=2 { - panic("wrong color") + num := rand.Intn(100) + if num >= int(sm.Probability*100.0) { + sm.color = 2 + } else { + sm.color = 1 } } func (sm *Slush) Run(env *core.Environment, t *core.Turtle) { t.Wiggle() - notChanged := true - threshold := float32(1.0) - for notChanged { - notChanged = false - if t.Near(env, 1, threshold, "1") { + am1 := t.Amount(env,1,"1") + am2 := t.Amount(env,1,"2") + + t.Drop(env, 1, strconv.Itoa(sm.color)) + + + if am1 > 3 || am2 > 3 { + if am1 > am2 { sm.color = 1 - notChanged = true - } - if t.Near(env, 1, threshold, "2") { + } else if am2 > am1 { sm.color = 2 - notChanged = true } - threshold+=0.01 } t.Step(env) - t.Drop(env, 1, strconv.Itoa(sm.color)) } diff --git a/actors/snowball.go b/actors/snowball.go deleted file mode 100644 index 8ca0889..0000000 --- a/actors/snowball.go +++ /dev/null @@ -1,40 +0,0 @@ -package actors - -import ( - "git.openprivacy.ca/sarah/microworlds/core" - "math/rand" - "strconv" -) - -type Snowball struct { - color int - sureness int -} - -func (sm *Snowball) Setup(env *core.Environment, t *core.Turtle) { - sm.color = rand.Intn(2) + 1 - if sm.color !=1 && sm.color !=2 { - panic("wrong color") - } - sm.sureness = 0 -} - -func (sm *Snowball) Run(env *core.Environment, t *core.Turtle) { - t.Wiggle() - if sm.color == 1 && t.Near(env, 1, float32(sm.sureness), "1") { - sm.sureness++ - } else if sm.color == 1 && t.Near(env, 1, float32(sm.sureness), "2") { - sm.sureness = 0 - sm.color = 2 - } else if sm.color == 2 && t.Near(env, 1, float32(sm.sureness), "2") { - sm.sureness++ - } else if sm.color == 2 && t.Near(env, 1, float32(sm.sureness), "1") { - sm.sureness = 0 - sm.color = 1 - } - t.Step(env) - t.Drop(env, 1, strconv.Itoa(sm.color)) -} - - - diff --git a/core/turtle.go b/core/turtle.go index 269aaa8..d3451f9 100644 --- a/core/turtle.go +++ b/core/turtle.go @@ -67,6 +67,37 @@ func (t *Turtle) Drop(env *Environment, amount float32, pheromone string) { env.Mark(pheromone, t.xpos, t.ypos, amount) } +func (t *Turtle) Amount(env *Environment, distance int, pheromone string) float32 { + h0 := t.heading - 1 + if h0 < 0 { + h0 = 7 + } + + dx0 := headings[h0][0] * distance + dy0 := headings[h0][1] * distance + + x0 := (t.xpos + dx0) + y0 := (t.ypos + dy0) + + dx := headings[t.heading][0] * distance + dy := headings[t.heading][1] * distance + + x := (t.xpos + dx) + y := (t.ypos + dy) + + h1 := (t.heading + 1) % 8 + dx1 := headings[h1][0] * distance + dy1 := headings[h1][1] * distance + + x1 := (t.xpos + dx1) + y1 := (t.ypos + dy1) + + as0 := env.SniffNormalized(x0, y0, pheromone) + as := env.SniffNormalized(x, y, pheromone) + as1 := env.SniffNormalized(x1, y1, pheromone) + return as0+as+as1 +} + func (t *Turtle) Near(env *Environment, distance int, threshold float32, pheromone string) bool { h0 := t.heading - 1 if h0 < 0 { diff --git a/main.go b/main.go index 4fc501c..3546295 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,8 @@ var height = flag.Int("height", 300, "height of environment") var pxsize = flag.Int("pxsize", 1, "pixels per tile edge") var numTurtles = flag.Int("numTurtles", 5000, "number of turtles") +var prob = flag.Float64("distribution", .51, "number of turtles") + func main() { // We don't need real randomness @@ -93,20 +95,12 @@ func main() { g.ColorPheromone("trail", [4]uint8{0x81, 0, 0x81, 0x00}) case "slush": for i := 0; i < *numTurtles; i++ { - turtles[i] = core.NewTurtle(env, &actors.Slush{}) + turtles[i] = core.NewTurtle(env, &actors.Slush{Probability:float32(*prob)}) } env.InitPheromone("1") env.InitPheromone("2") g.ColorPheromone("1", [4]uint8{0x80, 0xFF, 0x00, 0x00}) g.ColorPheromone("2", [4]uint8{0xFF, 0, 0xFF, 0x00}) - case "snowball": - for i := 0; i < *numTurtles; i++ { - turtles[i] = core.NewTurtle(env, &actors.Snowball{}) - } - env.InitPheromone("1") - env.InitPheromone("2") - g.ColorPheromone("1", [4]uint8{0x00, 0xFF, 0xFF, 0x00}) - g.ColorPheromone("2", [4]uint8{0xFF, 0xFF, 0x00, 0x00}) case "flocking": for i := 0; i < *numTurtles; i++ { turtles[i] = core.NewTurtle(env, &actors.Flocking{SniffDistance: 1})