Small fix + fmt
This commit is contained in:
parent
f888eea668
commit
56f0e4f487
|
@ -4,7 +4,6 @@ import (
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type Ant struct {
|
type Ant struct {
|
||||||
SniffDistance int
|
SniffDistance int
|
||||||
Carrying bool
|
Carrying bool
|
||||||
|
@ -34,4 +33,3 @@ func (a*Ant) Run(env *core.Environment, t *core.Turtle) {
|
||||||
t.Step(env)
|
t.Step(env)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,6 @@ func (e *Environment) Width() int {
|
||||||
return e.width
|
return e.width
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (e *Environment) Height() int {
|
func (e *Environment) Height() int {
|
||||||
return e.height
|
return e.height
|
||||||
}
|
}
|
||||||
|
@ -42,11 +41,8 @@ func (e *Environment) Mark(x,y int,amount float32) {
|
||||||
if e.state[x][y] > 255 {
|
if e.state[x][y] > 255 {
|
||||||
e.state[x][y] = 255
|
e.state[x][y] = 255
|
||||||
}
|
}
|
||||||
//log.Debugf("Marking: %d %d %f", x, y, e.state[x][y])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func (e Environment) Occupy(x, y int) {
|
func (e Environment) Occupy(x, y int) {
|
||||||
e.col[x][y] = true
|
e.col[x][y] = true
|
||||||
}
|
}
|
||||||
|
@ -59,7 +55,6 @@ func (e Environment) Leave(x,y int) {
|
||||||
e.col[x][y] = false
|
e.col[x][y] = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (e Environment) HasValue(x, y int) bool {
|
func (e Environment) HasValue(x, y int) bool {
|
||||||
return e.value[x][y]
|
return e.value[x][y]
|
||||||
}
|
}
|
||||||
|
@ -76,7 +71,6 @@ func (e Environment) Sniff(x,y int) float32 {
|
||||||
return e.state[x][y]
|
return e.state[x][y]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (e *Environment) normXY(x int, y int) (int, int) {
|
func (e *Environment) normXY(x int, y int) (int, int) {
|
||||||
if x < 0 {
|
if x < 0 {
|
||||||
x = (e.width - 1)
|
x = (e.width - 1)
|
||||||
|
@ -96,7 +90,6 @@ func (e *Environment) normXY(x int, y int) (int,int) {
|
||||||
func (e *Environment) Evaporate(rate float32) {
|
func (e *Environment) Evaporate(rate float32) {
|
||||||
//log.Debugf("Evap")
|
//log.Debugf("Evap")
|
||||||
|
|
||||||
|
|
||||||
e.pstate = make([][]float32, e.width)
|
e.pstate = make([][]float32, e.width)
|
||||||
for x := range e.pstate {
|
for x := range e.pstate {
|
||||||
e.pstate[x] = make([]float32, e.height)
|
e.pstate[x] = make([]float32, e.height)
|
||||||
|
@ -107,7 +100,6 @@ func (e *Environment) Evaporate(rate float32) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for x := 0; x < e.width; x++ {
|
for x := 0; x < e.width; x++ {
|
||||||
for y := 0; y < e.height; y++ {
|
for y := 0; y < e.height; y++ {
|
||||||
amount := e.pstate[x][y]
|
amount := e.pstate[x][y]
|
||||||
|
|
|
@ -10,9 +10,7 @@ type Turtle struct {
|
||||||
actor Actor
|
actor Actor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type NilActor struct {
|
type NilActor struct {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (NilActor) Setup(*Environment, *Turtle) {
|
func (NilActor) Setup(*Environment, *Turtle) {
|
||||||
|
@ -43,7 +41,6 @@ func (t *Turtle) Pos() (int,int) {
|
||||||
|
|
||||||
var headings = [][]int{{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}}
|
var headings = [][]int{{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}}
|
||||||
|
|
||||||
|
|
||||||
func (t *Turtle) setRandomHeading() {
|
func (t *Turtle) setRandomHeading() {
|
||||||
t.heading = rand.Intn(8)
|
t.heading = rand.Intn(8)
|
||||||
}
|
}
|
||||||
|
@ -62,12 +59,10 @@ func (t *Turtle) Wiggle() {
|
||||||
t.heading = h
|
t.heading = h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (t *Turtle) TurnAround() {
|
func (t *Turtle) TurnAround() {
|
||||||
t.heading = (t.heading + 4) % 8
|
t.heading = (t.heading + 4) % 8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (t *Turtle) Drop(env *Environment, amount float32) {
|
func (t *Turtle) Drop(env *Environment, amount float32) {
|
||||||
env.Mark(t.xpos, t.ypos, amount)
|
env.Mark(t.xpos, t.ypos, amount)
|
||||||
}
|
}
|
||||||
|
@ -91,7 +86,6 @@ func (t *Turtle) FollowGradient(env *Environment, distance int, threshold float3
|
||||||
x := (t.xpos + dx)
|
x := (t.xpos + dx)
|
||||||
y := (t.ypos + dy)
|
y := (t.ypos + dy)
|
||||||
|
|
||||||
|
|
||||||
h1 := (t.heading + 1) % 8
|
h1 := (t.heading + 1) % 8
|
||||||
dx1 := headings[h1][0] * distance
|
dx1 := headings[h1][0] * distance
|
||||||
dy1 := headings[h1][1] * distance
|
dy1 := headings[h1][1] * distance
|
||||||
|
@ -126,11 +120,11 @@ func (t* Turtle) Step(env *Environment) bool {
|
||||||
ox := t.xpos
|
ox := t.xpos
|
||||||
oy := t.ypos
|
oy := t.ypos
|
||||||
env.Leave(ox, oy)
|
env.Leave(ox, oy)
|
||||||
t.xpos = (t.xpos + dx) % (env.width -1)
|
t.xpos = (t.xpos + dx) % (env.width)
|
||||||
if t.xpos < 0 {
|
if t.xpos < 0 {
|
||||||
t.xpos = env.width - 1
|
t.xpos = env.width - 1
|
||||||
}
|
}
|
||||||
t.ypos = (t.ypos + dy) % (env.height -1)
|
t.ypos = (t.ypos + dy) % (env.height)
|
||||||
if t.ypos < 0 {
|
if t.ypos < 0 {
|
||||||
t.ypos = env.height - 1
|
t.ypos = env.height - 1
|
||||||
}
|
}
|
||||||
|
@ -147,6 +141,5 @@ func (t* Turtle) Step(env *Environment) bool {
|
||||||
|
|
||||||
// Run the turtle program
|
// Run the turtle program
|
||||||
func (t *Turtle) Run(env *Environment) {
|
func (t *Turtle) Run(env *Environment) {
|
||||||
//log.Debugf("Pos: %v %v", t.xpos, t.ypos)
|
|
||||||
t.actor.Run(env, t)
|
t.actor.Run(env, t)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTurtle_Wiggle(t *testing.T) {
|
func TestTurtle_Wiggle(t *testing.T) {
|
||||||
turtle := NewTurtle(NewEnvironment(3,3,), new(NilActor))
|
turtle := NewTurtle(NewEnvironment(3, 3), new(NilActor))
|
||||||
counts := []int{0, 0, 0, 0, 0, 0, 0, 0}
|
counts := []int{0, 0, 0, 0, 0, 0, 0, 0}
|
||||||
for i := 0; i < 1000; i++ {
|
for i := 0; i < 1000; i++ {
|
||||||
turtle.Wiggle()
|
turtle.Wiggle()
|
||||||
|
|
|
@ -41,8 +41,6 @@ func (g* Graphics) Render(env *core.Environment, turtles []*core.Turtle) {
|
||||||
g.renderer.SetDrawColor(0x00, 0x00, 0x00, 0x00)
|
g.renderer.SetDrawColor(0x00, 0x00, 0x00, 0x00)
|
||||||
g.renderer.FillRect(&sdl.Rect{0, 0, 600, 600})
|
g.renderer.FillRect(&sdl.Rect{0, 0, 600, 600})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for x := 0; x < int(g.width); x++ {
|
for x := 0; x < int(g.width); x++ {
|
||||||
for y := 0; y < int(g.height); y++ {
|
for y := 0; y < int(g.height); y++ {
|
||||||
amount := math.Min(float64(env.Sniff(x, y)), 255)
|
amount := math.Min(float64(env.Sniff(x, y)), 255)
|
||||||
|
@ -63,7 +61,6 @@ func (g* Graphics) Render(env *core.Environment, turtles []*core.Turtle) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
g.renderer.SetDrawColor(0xF3, 0x81, 0, 0x00)
|
g.renderer.SetDrawColor(0xF3, 0x81, 0, 0x00)
|
||||||
for _, t := range turtles {
|
for _, t := range turtles {
|
||||||
x, y := t.Pos()
|
x, y := t.Pos()
|
||||||
|
@ -76,5 +73,5 @@ func (g* Graphics) Render(env *core.Environment, turtles []*core.Turtle) {
|
||||||
g.window.UpdateSurface()
|
g.window.UpdateSurface()
|
||||||
surface, _ := g.window.GetSurface()
|
surface, _ := g.window.GetSurface()
|
||||||
surface.SaveBMP("./images/" + strconv.Itoa(g.t) + ".bmp")
|
surface.SaveBMP("./images/" + strconv.Itoa(g.t) + ".bmp")
|
||||||
g.t++;
|
g.t++
|
||||||
}
|
}
|
9
main.go
9
main.go
|
@ -14,7 +14,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
||||||
|
|
||||||
var model = flag.String("model", "slime", "slimemold|swarm|woodchips")
|
var model = flag.String("model", "slime", "slimemold|swarm|woodchips")
|
||||||
|
@ -23,8 +22,6 @@ var width = flag.Int("width", 300, "width of environment")
|
||||||
var height = flag.Int("height", 300, "height of environment")
|
var height = flag.Int("height", 300, "height of environment")
|
||||||
var numTurtles = flag.Int("numTurtles", 5000, "number of turtles")
|
var numTurtles = flag.Int("numTurtles", 5000, "number of turtles")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// We don't need real randomness
|
// We don't need real randomness
|
||||||
|
@ -45,11 +42,9 @@ func main() {
|
||||||
}
|
}
|
||||||
defer sdl.Quit()
|
defer sdl.Quit()
|
||||||
|
|
||||||
|
|
||||||
env := core.NewEnvironment(*width, *height)
|
env := core.NewEnvironment(*width, *height)
|
||||||
turtles := make([]*core.Turtle, *numTurtles)
|
turtles := make([]*core.Turtle, *numTurtles)
|
||||||
|
|
||||||
|
|
||||||
switch *model {
|
switch *model {
|
||||||
|
|
||||||
case "swarm":
|
case "swarm":
|
||||||
|
@ -90,8 +85,6 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
g := graphics.NewGraphics(int32(*width), int32(*height))
|
g := graphics.NewGraphics(int32(*width), int32(*height))
|
||||||
|
|
||||||
running := true
|
running := true
|
||||||
|
@ -105,7 +98,6 @@ func main() {
|
||||||
wait.Done()
|
wait.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
||||||
wait.Add(1)
|
wait.Add(1)
|
||||||
for running {
|
for running {
|
||||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||||
|
@ -119,4 +111,3 @@ func main() {
|
||||||
wait.Done()
|
wait.Done()
|
||||||
wait.Wait()
|
wait.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue