More exampels, upgrading color

This commit is contained in:
Sarah Jamie Lewis 2019-08-10 14:56:49 -07:00
parent 94446e5dc1
commit f222cf86b5
7 changed files with 171 additions and 5 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/vendor/
# ---> Go
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o

35
actors/flocking.go Normal file
View File

@ -0,0 +1,35 @@
package actors
import (
"git.openprivacy.ca/sarah/microworlds/core"
)
type Flocking struct {
SniffDistance int
}
func (sm *Flocking) Setup(env *core.Environment, t *core.Turtle) {
// Do nothing
}
func (sm *Flocking) Run(env *core.Environment, t *core.Turtle) {
if t.Near(env, 1,1, "avoid") {
t.TurnAround()
t.Step(env)
} else if t.Near(env, 2,1, "avoid") {
t.TurnAround()
t.Step(env)
} else {
t.FollowGradient(env, 4, 1, "trail")
t.FollowGradient(env, 3, 1,"trail")
t.FollowGradient(env, 2,1, "trail")
t.FollowGradient(env, 1, 3, "trail")
t.Wiggle()
t.Step(env)
}
t.Drop(env, 1, "trail")
t.Drop(env, 2, "avoid")
}

23
actors/maze.go Normal file
View File

@ -0,0 +1,23 @@
package actors
import (
"git.openprivacy.ca/sarah/microworlds/core"
)
type MazeGeneration struct {
SniffDistance int
}
func (sm *MazeGeneration) Setup(env *core.Environment, t *core.Turtle) {
// Do nothing
}
func (sm *MazeGeneration) Run(env *core.Environment, t *core.Turtle) {
t.Wiggle()
t.FollowGradient(env, sm.SniffDistance, 0, "trail")
t.TurnAround() // Run away from the strongest trail
t.Step(env)
t.Drop(env, 1, "trail")
}

28
actors/slush.go Normal file
View File

@ -0,0 +1,28 @@
package actors
import (
"git.openprivacy.ca/sarah/microworlds/core"
"math/rand"
"strconv"
)
type Slush struct {
color int
}
func (sm *Slush) Setup(env *core.Environment, t *core.Turtle) {
sm.color = rand.Intn(2) + 1
}
func (sm *Slush) Run(env *core.Environment, t *core.Turtle) {
t.Wiggle()
if t.Near(env, 1,2, "1") {
sm.color = 1
} else if t.Near(env, 1,2, "2") {
sm.color = 2
}
t.Step(env)
t.Drop(env, 1, strconv.Itoa(sm.color))
}

View File

@ -67,6 +67,52 @@ func (t *Turtle) Drop(env *Environment, amount float32, pheromone string) {
env.Mark(pheromone, t.xpos, t.ypos, amount)
}
func (t *Turtle) Near(env *Environment, distance int, threshold float32, pheromone string) bool {
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)
if as0 < threshold {
as0 = 0
}
as := env.SniffNormalized(x, y, pheromone)
if as < threshold {
as = 0
}
as1 := env.SniffNormalized(x1, y1, pheromone)
if as1 < threshold {
as1 = 0
}
if as0 == 0 && as == 0 && as1 == 0 {
return false
} else {
return true
}
}
func (t *Turtle) FollowGradient(env *Environment, distance int, threshold float32, pheromone string) {
h0 := t.heading - 1

View File

@ -6,6 +6,8 @@ import (
"github.com/veandco/go-sdl2/sdl"
"math"
"os"
"strconv"
//"strconv"
)
@ -56,17 +58,26 @@ func (g *Graphics) Render(env *core.Environment, turtles []*core.Turtle) {
for x := 0; x < int(g.width); x++ {
for y := 0; y < int(g.height); y++ {
scaledamountRedTotal := 0
scaledamountGreenTotal := 0
scaledamountBlueTotal := 0
for name, color := range g.colorMap {
amount := math.Min(float64(env.Sniff(name, x, y)), 255)
if amount > 0 {
// TODO explictly define this scale
scaledamount := uint8(float64(color[0]) * (amount/2))
g.renderer.SetDrawColor(scaledamount,0,scaledamount, uint8(0xF0))
g.DrawTileColor(int32(x), int32(y))
scaledamountRed := uint8(float64(color[0]) * (amount/2))
scaledamountGreen := uint8(float64(color[1]) * (amount/2))
scaledamountBlue := uint8(float64(color[2]) * (amount/2))
scaledamountRedTotal += int(scaledamountRed)
scaledamountGreenTotal += int(scaledamountGreen)
scaledamountBlueTotal += int(scaledamountBlue)
}
}
g.renderer.SetDrawColor(uint8(scaledamountRedTotal/len(g.colorMap)),uint8(scaledamountGreenTotal/len(g.colorMap)),uint8(scaledamountBlueTotal/len(g.colorMap)), uint8(0xF0))
g.renderer.DrawPoint(int32(x), int32(y))
if env.HasValue(x, y) {
g.renderer.SetDrawColor(255, 255, 255, uint8(255))
g.DrawTileColor(int32(x), int32(y))
@ -88,7 +99,7 @@ func (g *Graphics) Render(env *core.Environment, turtles []*core.Turtle) {
g.renderer.Present()
g.window.UpdateSurface()
// surface, _ := g.window.GetSurface()
// surface.SaveBMP("./images/" + strconv.Itoa(g.t) + ".bmp")
surface, _ := g.window.GetSurface()
surface.SaveBMP("./images/" + strconv.Itoa(g.t) + ".bmp")
g.t++
}

22
main.go
View File

@ -85,6 +85,28 @@ func main() {
}
env.InitPheromone("trail")
g.ColorPheromone("trail", [4]uint8{0x81, 0, 0x81, 0x00})
case "maze":
for i := 0; i < *numTurtles; i++ {
turtles[i] = core.NewTurtle(env, &actors.MazeGeneration{SniffDistance: 5})
}
env.InitPheromone("trail")
g.ColorPheromone("trail", [4]uint8{0x81, 0, 0x81, 0x00})
case "slush":
for i := 0; i < *numTurtles; i++ {
turtles[i] = core.NewTurtle(env, &actors.Slush{})
}
env.InitPheromone("1")
env.InitPheromone("2")
g.ColorPheromone("1", [4]uint8{0x00, 0xFF, 0x00, 0x00})
g.ColorPheromone("2", [4]uint8{0x00, 0, 0xFF, 0x00})
case "flocking":
for i := 0; i < *numTurtles; i++ {
turtles[i] = core.NewTurtle(env, &actors.Flocking{SniffDistance: 1})
}
env.InitPheromone("trail")
env.InitPheromone("avoid")
g.ColorPheromone("trail", [4]uint8{0x81, 0, 0x81, 0x00})
g.ColorPheromone("avoid", [4]uint8{0xd1, 0, 0xff, 0x00})
default:
for i := 0; i < *numTurtles; i++ {