Example #1
0
func main() {
	for _, ch := range strings.Split(chunkFile, "\n\n") {
		c, err := chunk.Parse(chunk.ParseSpec{"|.", '*'}, ch)
		if err != nil {
			panic(err)
		}
		chunks = append(chunks, c)
	}
	chunks = chunk.GenerateVariants(chunks)

	gen := chunk.New(chunks[0], '#')
	gen.SetGrid(image.Pt(4, 4))

	err := termbox.Init()
	if err != nil {
		panic(err)
	}
	defer termbox.Close()

	pegIdx := rand.Intn(len(gen.OpenPegs()))
	chunkIdx := 0
loop:
	for {
		draw(gen, pegIdx, chunkIdx)
		switch ev := termbox.PollEvent(); ev.Type {
		case termbox.EventKey:
			switch ev.Key {
			case termbox.KeyTab:
				pegIdx++
				chunkIdx = 0
				pegs := gen.OpenPegs()
				if pegIdx >= len(pegs) {
					pegIdx = 0
				}
			case termbox.KeySpace:
				chunkIdx++
			case termbox.KeyEnter:
				spawn(gen, chunks, pegIdx, chunkIdx)
				if len(gen.OpenPegs()) == 0 {
					pegIdx = 0
				} else {
					pegIdx = rand.Intn(len(gen.OpenPegs()))
				}
				chunkIdx = rand.Intn(256)
			case termbox.KeyEsc:
				break loop
			}
		}
	}
}
Example #2
0
func (m *Mapgen) TestMap(start space.Location, depth int) (entry, exit space.Location) {
	legend := map[rune]placeFn{
		'#': terrainPlacer(world.WallTerrain),
		'.': terrainPlacer(world.FloorTerrain),
		'|': terrainPlacer(world.DoorTerrain),
		'b': terrainPlacer(world.BarrelTerrain),
		'c': terrainPlacer(world.ChairTerrain),
		't': terrainPlacer(world.CounterTerrain),
		'p': terrainPlacer(world.PlantTerrain),

		// Downstairs cell gets special handling at mapgen, failing that, it gets
		// turned into floor.
		'>': terrainPlacer(world.FloorTerrain),
		'<': terrainPlacer(world.StairTerrain),
	}

	entrance := parseChunks(`
####|####
#.......#
#.......#
#..###..#
|..#<...|
#..###..#
#.......#
#.......#
####|####
`)

	exits := parseChunks(`
####|####
#.......#
#.......#
#..###..#
|...>#..|
#..###..#
#.......#
#.......#
####|####
`)

	chunks := parseChunks(`
####|####
#.......#
#.......#
#.......#
|.......|
#.......#
#.......#
#.......#
####|####

####|####
#.......#
#.bb....#
#.bb....#
|.......|
#.......#
#.......#
#.......#
####|####

####|####
#.......#
#....#..#
#..c.|..#
|.ptp#..|
######..#
#bb.....#
#bb....b#
####|####

####|####
#..p.p..#
#.......#
#p.ctc.p#
|..ctc..|
#p.ctc.p#
#.......#
#..p.p..#
####|####
`)
	chunks = chunk.GenerateVariants(chunks)

	m.chart = simpleChart(start)

	cg := chunk.New(entrance[rand.Intn(len(entrance))], '#')
	cg.SetGrid(image.Pt(4, 4))

	nRooms := 5 + depth/2
	for i := 0; i < nRooms; i++ {
		pegs := cg.OpenPegs()
		if len(pegs) == 0 {
			panic("Map ran out of expansion room")
		}

		peg := pegs[rand.Intn(len(pegs))]

		var placeChunks []chunk.OffsetChunk
		if i == nRooms-1 {
			placeChunks = cg.FittingChunks(peg, exits)
			// XXX: Hardcoded exit point. All exists are assumed to have the
			// exit tile at the same offset.
			exit = m.chart.At(placeChunks[0].Offset().Add(image.Pt(4, 4)))
		} else {
			placeChunks = cg.FittingChunks(peg, chunks)
		}
		if len(placeChunks) == 0 {
			panic("Can't expand map")
		}

		chunk := placeChunks[rand.Intn(len(placeChunks))]

		cg.AddChunk(chunk)
	}
	cg.CloseAllPegs()

	for pt, cell := range cg.Map() {
		fn, ok := legend[rune(cell)]
		if ok {
			fn(m.world, m.chart.At(pt))
		} else {
			panic("Unknown terrain type " + string(cell))
		}
	}

	entry = m.chart.At(image.Pt(4, 4))
	return
}