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) var args []string algorithm.Map2(tags.RoomSizes, &args, func(a RoomSize) string { return a.String() }) fp.room_size = gui.MakeComboTextBox(args, 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 }
func makeChooserFromOptionBasicsFile(path string) (*Chooser, <-chan []string, error) { var bops []OptionBasic err := base.LoadAndProcessObject(path, "json", &bops) if err != nil { return nil, nil, err } var opts []Option algorithm.Map2(bops, &opts, func(ob OptionBasic) Option { return &ob }) return MakeChooser(opts) }
func (a *Move) Maintain(dt int64, g *game.Game, ae game.ActionExec) game.MaintenanceStatus { if ae != nil { exec := ae.(*moveExec) a.ent = g.EntityById(ae.EntityId()) if len(exec.Path) == 0 { base.Error().Printf("Got a move exec with a path length of 0: %v", exec) return game.Complete } a.cost = exec.measureCost(a.ent, g) if a.cost > a.ent.Stats.ApCur() { base.Error().Printf("Got a move that required more ap than available: %v", exec) base.Error().Printf("Path: %v", exec.Path) return game.Complete } if a.cost == -1 { base.Error().Printf("Got a move that followed an invalid path: %v", exec) base.Error().Printf("Path: %v", exec.Path) if a.ent == nil { base.Error().Printf("ENT was Nil!") } else { x, y := a.ent.Pos() v := g.ToVertex(x, y) base.Error().Printf("Ent pos: (%d, %d) -> (%d)", x, y, v) } return game.Complete } algorithm.Map2(exec.Path, &a.path, func(v int) [2]int { _, x, y := g.FromVertex(v) return [2]int{x, y} }) base.Log().Printf("Path Validated: %v", exec) a.ent.Stats.ApplyDamage(-a.cost, 0, status.Unspecified) src := g.ToVertex(a.ent.Pos()) graph := g.Graph(a.ent.Side(), true, nil) a.drawPath(a.ent, g, graph, src) } // Do stuff factor := float32(math.Pow(2, a.ent.Walking_speed)) dist := a.ent.DoAdvance(factor*float32(dt)/200, a.path[0][0], a.path[0][1]) for dist > 0 { if len(a.path) == 1 { a.ent.DoAdvance(0, 0, 0) a.ent.Info.RoomsExplored[a.ent.CurrentRoom()] = true a.ent = nil return game.Complete } a.path = a.path[1:] a.ent.Info.RoomsExplored[a.ent.CurrentRoom()] = true dist = a.ent.DoAdvance(dist, a.path[0][0], a.path[0][1]) } return game.InProgress }
func Mapper2Spec(c gospec.Context) { c.Specify("Map from []int to []float64", func() { a := []int{0, 1, 2, 3, 4} var b []float64 algorithm.Map2(a, &b, func(n int) float64 { return float64(n) }) 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"}) // }) }
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 } algorithm.Map2(path, &a.path, func(a int) [2]int { _, x, y := g.FromVertex(a) return [2]int{int(x), int(y)} }) a.cost = int(cost) a.drawPath(ent, g, graph, src) } }
func (a *Move) HandleInput(group gui.EventGroup, g *game.Game) (bool, game.ActionExec) { cursor := group.Events[0].Key.Cursor() if cursor != nil { fx, fy := g.GetViewer().WindowToBoard(cursor.Point()) a.findPath(a.ent, int(fx), int(fy)) } if found, _ := group.FindEvent(gin.MouseLButton); found { if len(a.path) > 0 { if a.cost <= a.ent.Stats.ApCur() { var exec moveExec exec.SetBasicData(a.ent, a) algorithm.Map2(a.path, &exec.Path, func(v [2]int) int { return g.ToVertex(v[0], v[1]) }) return true, &exec } return true, nil } else { return false, nil } } return false, nil }
func InsertMapChooser(ui gui.WidgetParent, chosen func(string), resert func(ui gui.WidgetParent) error) error { var bops []OptionBasic datadir := base.GetDataDir() err := base.LoadAndProcessObject(filepath.Join(datadir, "ui", "start", "versus", "map_select.json"), "json", &bops) if err != nil { base.Error().Printf("Unable to insert MapChooser: %v", err) return err } var opts []Option algorithm.Map2(bops, &opts, func(ob OptionBasic) Option { return &ob }) for _, opt := range opts { base.Log().Printf(opt.String()) } var ch Chooser err = base.LoadAndProcessObject(filepath.Join(datadir, "ui", "chooser", "layout.json"), "json", &ch.layout) if err != nil { base.Error().Printf("Unable to insert MapChooser: %v", err) return err } ch.options = opts ch.buttons = []*Button{ &ch.layout.Up, &ch.layout.Down, &ch.layout.Back, &ch.layout.Next, } ch.non_scroll_buttons = []*Button{ &ch.layout.Back, &ch.layout.Next, } ch.layout.Up.f = func(interface{}) { ch.layout.Options.Up() } ch.layout.Down.f = func(interface{}) { ch.layout.Options.Down() } ch.selected = make(map[int]bool) ch.layout.Back.f = func(interface{}) { ui.RemoveChild(&ch) err := resert(ui) if err != nil { base.Error().Printf("Unable to make Start Menu: %v", err) return } } ch.layout.Next.f = func(interface{}) { for i := range ch.options { if ch.selected[i] { ui.RemoveChild(&ch) chosen(ch.options[i].String()) } } } ch.layout.Next.valid_func = func() bool { return ch.selector(-1, ch.selected, false) } ch.min, ch.max = 1, 1 if ch.min == 1 && ch.max == 1 { ch.selector = SelectExactlyOne } else { ch.selector = SelectInRange(ch.min, ch.max) } ch.info_region = gui.Region{ gui.Point{ch.layout.Info.X, ch.layout.Info.Y}, gui.Dims{ch.layout.Info.Dx, ch.layout.Info.Dy}, } ui.AddChild(&ch) return nil }
func (f *Floor) render(region gui.Region, focusx, focusy, angle, zoom float32, drawables []Drawable, los_tex *LosTexture, floor_drawers []FloorDrawer) { var ros []RectObject algorithm.Map2(f.Rooms, &ros, func(r *Room) RectObject { return r }) // Do not include temporary objects in the ordering, since they will likely // overlap with other objects and make it difficult to determine the proper // ordering. Just draw the temporary ones last. num_temp := 0 for i := range ros { if ros[i].(*Room).temporary { ros[num_temp], ros[i] = ros[i], ros[num_temp] num_temp++ } } placed := OrderRectObjects(ros[num_temp:]) ros = ros[0:num_temp] for i := range placed { ros = append(ros, placed[i]) } alpha_map := make(map[*Room]byte) los_map := make(map[*Room]byte) // First pass over the rooms - this will determine at what alpha the rooms // should be draw. We will use this data later to determine the alpha for // the doors of adjacent rooms. for i := len(ros) - 1; i >= 0; i-- { room := ros[i].(*Room) los_alpha := room.getMaxLosAlpha(los_tex) room.setupGlStuff() tx := (focusx + 3) - float32(room.X+room.Size.Dx) if tx < 0 { tx = 0 } ty := (focusy + 3) - float32(room.Y+room.Size.Dy) if ty < 0 { ty = 0 } if tx < ty { tx = ty } // z := math.Log10(float64(zoom)) z := float64(zoom) / 10 v := math.Pow(z, float64(2*tx)/3) if v > 255 { v = 255 } bv := 255 - byte(v) alpha_map[room] = byte((int(bv) * int(los_alpha)) >> 8) los_map[room] = los_alpha // room.render(floor, left, right, , 255) } // Second pass - this time we fill in the alpha that we should use for the // doors, using the values we've already calculated in the first pass. for _, r1 := range f.Rooms { r1.far_right.wall_alpha = 255 r1.far_left.wall_alpha = 255 for _, r2 := range f.Rooms { if r1 == r2 { continue } left, right := r2.getNearWallAlpha(los_tex) r1_rect := image.Rect(r1.X, r1.Y+r1.Size.Dy, r1.X+r1.Size.Dx, r1.Y+r1.Size.Dy+1) r2_rect := image.Rect(r2.X, r2.Y, r2.X+r2.Size.Dx, r2.Y+r2.Size.Dy) if r1_rect.Overlaps(r2_rect) { // If there is an open door between the two then we'll tone down the // alpha, otherwise we won't treat it any differently for _, d1 := range r1.Doors { for _, d2 := range r2.Doors { if d1 == d2 { r1.far_left.wall_alpha = byte((int(left) * 200) >> 8) } } } } r1_rect = image.Rect(r1.X+r1.Size.Dx, r1.Y, r1.X+r1.Size.Dx+1, r1.Y+r1.Size.Dy) if r1_rect.Overlaps(r2_rect) { for _, d1 := range r1.Doors { for _, d2 := range r2.Doors { if d1 == d2 { r1.far_right.wall_alpha = byte((int(right) * 200) >> 8) } } } } } } // Third pass - now that we know what alpha to use on the rooms, walls, and // doors we can actually render everything. We still need to go back to // front though. for i := len(ros) - 1; i >= 0; i-- { room := ros[i].(*Room) fx := focusx - float32(room.X) fy := focusy - float32(room.Y) floor, _, left, _, right, _ := makeRoomMats(room.roomDef, region, fx, fy, angle, zoom) v := alpha_map[room] if los_map[room] > 5 { room.render(floor, left, right, zoom, v, drawables, los_tex, floor_drawers) } } }