doodle/pkg/level/fmt_maintenance.go
Noah Petherbridge 65a811db0d Auto-prune Empty Chunks in Level Files
* Discovered a bug where if you hit the Undo key to erase pixels and an
  entire chunk became empty by it, the chunk would have rendering errors
  and show as a solid black square instead of the level wallpaper
  showing through.
* Chunks that have no pixels in them are culled from the chunker
  immediately when you call a Delete() operation.
* The level file saver also calls a maintenance function to prune all
  empty chunks upon saving the file. So existing levels with broken
  chunks need only be re-saved to fix them.
2019-07-16 22:10:18 -07:00

49 lines
1.1 KiB
Go

package level
import "git.kirsle.net/apps/doodle/pkg/log"
// Maintenance functions for the file format on disk.
// PruneChunks cleans up any level chunks that have no pixel data.
func (m *Level) PruneChunks() int {
var count int
for coord, chunk := range m.Chunker.Chunks {
if chunk.Len() == 0 {
log.Info("PruneChunks: %d has no pixels", coord)
delete(m.Chunker.Chunks, coord)
count++
}
}
return count
}
// PruneLinks cleans up any Actor Links that can not be resolved in the
// level data. For example, if actors were linked in Edit Mode and one
// actor is deleted leaving a broken link.
//
// Returns the number of broken links pruned.
//
// This is called automatically in WriteFile.
func (m *Level) PruneLinks() int {
var count int
for id, actor := range m.Actors {
var newLinks []string
for _, linkID := range actor.Links {
if _, ok := m.Actors[linkID]; !ok {
log.Warn("Level.PruneLinks: actor %s (%s) was linked to unresolved actor %s",
id,
actor.Filename,
linkID,
)
count++
continue
}
newLinks = append(newLinks, linkID)
}
actor.Links = newLinks
}
return count
}