Beispiel #1
0
func MapperSpec(c gospec.Context) {
	c.Specify("Map from []int to []float64", func() {
		a := []int{0, 1, 2, 3, 4}
		var b []float64
		b = algorithm.Map(a, []float64{}, func(v interface{}) interface{} { return float64(v.(int)) }).([]float64)
		c.Expect(b, ContainsInOrder, []float64{0, 1, 2, 3, 4})
	})
	c.Specify("Map from []int to []string", func() {
		a := []int{0, 1, 2, 3, 4}
		var b []string
		b = algorithm.Map(a, []string{}, func(v interface{}) interface{} { return fmt.Sprintf("%d", v) }).([]string)
		c.Expect(b, ContainsInOrder, []string{"0", "1", "2", "3", "4"})
	})
}
Beispiel #2
0
func makeFurniturePanel(room *roomDef, viewer *RoomViewer) *FurniturePanel {
	var fp FurniturePanel
	fp.Room = room
	fp.RoomViewer = viewer
	fp.key_map = base.GetDefaultKeyMap()
	if room.Name == "" {
		room.Name = "name"
	}
	fp.name = gui.MakeTextEditLine("standard", room.Name, 300, 1, 1, 1, 1)

	if room.Floor.Path == "" {
		room.Floor.Path = base.Path(datadir)
	}
	fp.floor_path = gui.MakeFileWidget(room.Floor.Path.String(), imagePathFilter)

	if room.Wall.Path == "" {
		room.Wall.Path = base.Path(datadir)
	}
	fp.wall_path = gui.MakeFileWidget(room.Wall.Path.String(), imagePathFilter)

	fp.room_size = gui.MakeComboTextBox(algorithm.Map(tags.RoomSizes, []string{}, func(a interface{}) interface{} { return a.(RoomSize).String() }).([]string), 300)
	for i := range tags.RoomSizes {
		if tags.RoomSizes[i].String() == room.Size.String() {
			fp.room_size.SetSelectedIndex(i)
			break
		}
	}
	fp.VerticalTable = gui.MakeVerticalTable()
	fp.VerticalTable.Params().Spacing = 3
	fp.VerticalTable.Params().Background.R = 0.3
	fp.VerticalTable.Params().Background.B = 1
	fp.VerticalTable.AddChild(fp.name)
	fp.VerticalTable.AddChild(fp.floor_path)
	fp.VerticalTable.AddChild(fp.wall_path)
	fp.VerticalTable.AddChild(fp.room_size)

	furn_table := gui.MakeVerticalTable()
	fnames := GetAllFurnitureNames()
	for i := range fnames {
		name := fnames[i]
		furn_table.AddChild(gui.MakeButton("standard", name, 300, 1, 1, 1, 1, func(t int64) {
			f := MakeFurniture(name)
			if f == nil {
				return
			}
			fp.furniture = f
			fp.furniture.temporary = true
			fp.Room.Furniture = append(fp.Room.Furniture, fp.furniture)
			dx, dy := fp.furniture.Dims()
			fp.drag_anchor.x = float32(dx) / 2
			fp.drag_anchor.y = float32(dy) / 2
		}))
	}
	fp.VerticalTable.AddChild(gui.MakeScrollFrame(furn_table, 300, 600))

	return &fp
}
Beispiel #3
0
func (a *Move) findPath(ent *game.Entity, x, y int) {
	g := ent.Game()
	dst := g.ToVertex(x, y)
	if dst != a.dst || !a.calculated {
		a.dst = dst
		a.calculated = true
		src := g.ToVertex(a.ent.Pos())
		graph := g.Graph(ent.Side(), true, nil)
		cost, path := algorithm.Dijkstra(graph, []int{src}, []int{dst})
		if len(path) <= 1 {
			return
		}
		a.path = algorithm.Map(path, [][2]int{}, func(a interface{}) interface{} {
			_, x, y := g.FromVertex(a.(int))
			return [2]int{int(x), int(y)}
		}).([][2]int)
		a.cost = int(cost)

		if path_tex != nil {
			pix := path_tex.Pix()
			for i := range pix {
				for j := range pix[i] {
					pix[i][j] = 0
				}
			}
			current := 0.0
			for i := 1; i < len(a.path); i++ {
				src := g.ToVertex(a.path[i-1][0], a.path[i-1][1])
				dst := g.ToVertex(a.path[i][0], a.path[i][1])
				v, cost := graph.Adjacent(src)
				for j := range v {
					if v[j] == dst {
						current += cost[j]
						break
					}
				}
				pix[a.path[i][1]][a.path[i][0]] += byte(current)
			}
			path_tex.Remap()
		}
	}
}
Beispiel #4
0
func (a *Move) findPath(ent *game.Entity, x, y int) {
	g := ent.Game()
	dst := g.ToVertex(x, y)
	if dst != a.dst || !a.calculated {
		a.dst = dst
		a.calculated = true
		src := g.ToVertex(a.ent.Pos())
		graph := g.Graph(ent.Side(), true, nil)
		cost, path := algorithm.Dijkstra(graph, []int{src}, []int{dst})
		if len(path) <= 1 {
			return
		}
		a.path = algorithm.Map(path, [][2]int{}, func(a interface{}) interface{} {
			_, x, y := g.FromVertex(a.(int))
			return [2]int{int(x), int(y)}
		}).([][2]int)
		a.cost = int(cost)
		a.drawPath(ent, g, graph, src)
	}
}