Skip to content

Commit

Permalink
Early preparations for having multiple level files.
Browse files Browse the repository at this point in the history
Helps with #424 by allowing an alternate level tmx file by `-cheat_level=foo`.
  • Loading branch information
divVerent committed Feb 9, 2025
1 parent de4bbd4 commit a4ae109
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 31 deletions.
6 changes: 5 additions & 1 deletion cmd/dumpcplocs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import (
"github.com/divVerent/aaaaxy/internal/vfs"
)

var (
levelName = flag.String("level", "level", "name of the level file to load")
)

func main() {
log.Debugf("initializing VFS...")
err := vfs.Init()
Expand All @@ -33,7 +37,7 @@ func main() {
log.Debugf("parsing flags...")
flag.Parse(flag.NoConfig)
log.Debugf("loading level...")
lvl, err := level.NewLoader("level").SkipComparingCheckpointLocations(true).Load()
lvl, err := level.NewLoader(*levelName).SkipComparingCheckpointLocations(true).Load()
if err != nil {
log.Fatalf("could not load level: %v", err)
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/dumpcps/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import (
"github.com/divVerent/aaaaxy/internal/vfs"
)

var (
levelName = flag.String("level", "level", "name of the level file to load")
)

type (
Edge struct {
WantDelta m.Delta
Expand Down Expand Up @@ -78,7 +82,7 @@ func main() {
log.Debugf("parsing flags...")
flag.Parse(flag.NoConfig)
log.Debugf("loading level...")
lvl, err := level.NewLoader("level").SkipCheckpointLocations(true).Load()
lvl, err := level.NewLoader(*levelName).SkipCheckpointLocations(true).Load()
if err != nil {
log.Fatalf("could not load level: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/aaaaxy/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (g *Game) InitEarly() error {
if err != nil {
return fmt.Errorf("could not initialize VFS: %w", err)
}
err = initlocale.Init()
err = initlocale.Init(engine.LevelName())
if err != nil {
return fmt.Errorf("could not initialize locale: %w", err)
}
Expand Down
23 changes: 20 additions & 3 deletions internal/engine/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var (
debugCheckTileWindowSize = flag.Bool("debug_check_tile_window_size", false, "if set, we verify that the tile window size is set high enough")
debugCheckEntityOverlaps = flag.Bool("debug_check_entity_overlaps", false, "if set, we verify no two static entities overlap at same Z index")
debugCheckEntitySpawn = flag.Bool("debug_check_entity_spawn", false, "if set, crash if an entity fails to spawn")
cheatLevel = flag.String("cheat_level", "level", "name of the level file to load")
)

// World represents the current game state including its entities.
Expand Down Expand Up @@ -212,9 +213,23 @@ func loadLevel() (*level.Level, error) {
return loadLevelCache.Clone(), nil
}

var levelLoader *level.Loader = level.NewLoader("level")
// LevelName returns the name of the level used.
// If this ever changes, ReloadLevel and locale.SetLanguage need to be called.
func LevelName() string {
return *cheatLevel
}

var (
levelLoader *level.Loader
levelLoaderCreated bool
)

func Precache(s *splash.State) (splash.Status, error) {
if levelLoader == nil && !levelLoaderCreated {
levelLoader = level.NewLoader(LevelName())
levelLoaderCreated = true
}

status, err := s.Enter("loading level", locale.G.Get("loading level"), "failed to load level", levelLoader.LoadStepwise)
if status != splash.Continue {
return status, err
Expand All @@ -234,7 +249,7 @@ func Precache(s *splash.State) (splash.Status, error) {

func ReloadLevel() error {
// Must do this when the language changed.
lvl, err := level.NewLoader("level").Load()
lvl, err := level.NewLoader(LevelName()).Load()
if err != nil {
return err
}
Expand All @@ -247,7 +262,7 @@ func ReloadLevel() error {
}

func PaletteChanged() error {
loaded, err := level.NewLoader("level").Load()
loaded, err := level.NewLoader(LevelName()).Load()
if err != nil {
return err
}
Expand Down Expand Up @@ -304,6 +319,7 @@ func (w *World) Init(saveState int) error {
// Load loads the current savegame.
// If this fails, the world may be in an undefined state; call w.Init() or w.Load() to resume.
func (w *World) Load() error {
// TODO: #424 - handle multiple levels.
saveName := fmt.Sprintf("save-%d.json", w.saveState)
err := w.loadUnchecked(saveName)
if errors.Is(err, os.ErrNotExist) {
Expand Down Expand Up @@ -374,6 +390,7 @@ func (w *World) Save() error {
if is, cheats := flag.Cheating(); is {
return fmt.Errorf("not saving, as cheats are enabled: %s", cheats)
}
// TODO: #424 - handle multiple levels.
return vfs.WriteState(vfs.SavedGames, fmt.Sprintf("save-%d.json", w.saveState), state)
}

Expand Down
16 changes: 9 additions & 7 deletions internal/locale/initlocale/initlocale.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func initLinguas() error {
}
}
// Try detecting language packs.
// TODO: #424 - support alternate level packs.
for _, domain := range []string{"game", "level"} {
data, err = vfs.OSOpen(vfs.ExeDir, fmt.Sprintf("%s.po", domain))
if err != nil {
Expand Down Expand Up @@ -104,34 +105,35 @@ func initLocaleDomain(lang locale.Lingua, l locale.Type, domain string) {
log.Infof("%s translated to language %s", domain, lang.Name())
}

func Init() error {
func Init(levelName string) error {
err := initLinguas()
if err != nil {
return err
}
locale.InitCurrent()
_, err = forceSetLanguage(locale.Lingua(*language))
_, err = forceSetLanguage(levelName, locale.Lingua(*language))
return err
}

func SetLanguage(lang locale.Lingua) (bool, error) {
func SetLanguage(levelName string, lang locale.Lingua) (bool, error) {
if lang == "auto" {
lang = locale.Current
}
if locale.Active == lang {
if locale.Active == lang && levelName == locale.LName {
return false, nil
}
return forceSetLanguage(lang)
return forceSetLanguage(levelName, lang)
}

func forceSetLanguage(lang locale.Lingua) (bool, error) {
func forceSetLanguage(levelName string, lang locale.Lingua) (bool, error) {
if lang == "auto" {
lang = locale.Current
}
locale.ResetLanguage()
initLocaleDomain(lang, locale.G, "game")
initLocaleDomain(lang, locale.L, "level")
initLocaleDomain(lang, locale.L, levelName)
locale.Active = lang
locale.LName = levelName

err := font.SetFont(locale.ActiveFont())
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/locale/po.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ var GI IType
// L is the translation of the levels.
var L Type

// LName is the name of the level L is for.
var LName string

// Active is the name of the current language.
var Active Lingua

Expand Down
3 changes: 2 additions & 1 deletion internal/menu/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package menu
import (
"fmt"

"github.com/divVerent/aaaaxy/internal/engine"
"github.com/divVerent/aaaaxy/internal/flag"
"github.com/divVerent/aaaaxy/internal/game/misc"
"github.com/divVerent/aaaaxy/internal/locale"
Expand Down Expand Up @@ -53,7 +54,7 @@ func (l *languageSetting) apply(m *Controller) error {
flag.Set("language", string(lingua))

return m.NextFrame(func() error {
changed, err := initlocale.SetLanguage(lingua)
changed, err := initlocale.SetLanguage(engine.LevelName(), lingua)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions internal/menu/savestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (s *SaveStateScreen) saveStateInfo(initLvl *level.Level, idx int) string {
if idx == *saveState {
ps = &s.Controller.World.PlayerState
} else {
// TODO: #424 - handle multiple levels.
saveName := fmt.Sprintf("save-%d.json", idx)
state, err := vfs.ReadState(vfs.SavedGames, saveName)
if err != nil {
Expand Down
28 changes: 16 additions & 12 deletions scripts/build-generated-assets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,22 @@ cp assets/_saved/* assets/generated/

if [ x"$AAAAXY_GENERATE_ASSETS" = x'true' ]; then
if [ x"$AAAAXY_GENERATE_CHECKPOINT_LOCAITONS" = x'true' ]; then
if [ x"$AAAAXY_FORCE_GENERATE_ASSETS" = x'true' ] || ! [ "assets/generated/level.cp.json" -nt "assets/maps/level.tmx" ]; then
trap 'rm -f assets/generated/level.cp.json' EXIT
# Using |cat> instead of > because snapcraft for some reason doesn't allow using a regular > shell redirection with "go run".
${GO} run ${GO_FLAGS} github.com/divVerent/aaaaxy/cmd/dumpcps |cat> assets/generated/level.cp.dot
grep -c . assets/generated/level.cp.dot
neato -Tjson assets/generated/level.cp.dot > assets/generated/level.cp.json
grep -c . assets/generated/level.cp.json
trap - EXIT
fi
if [ x"$AAAAXY_DIFF_ASSETS" != x'false' ]; then
diff -bu -I'.*"width".*' assets/_saved/level.cp.json assets/generated/level.cp.json
fi
for lfile in assets/maps/*.tmx; do
lname=${lfile%.tmx}
lname=${lname##*/}
if [ x"$AAAAXY_FORCE_GENERATE_ASSETS" = x'true' ] || ! [ "assets/generated/$lname.cp.json" -nt "assets/maps/$lname.tmx" ]; then
trap 'rm -f "assets/generated/$lname.cp.json"' EXIT
# Using |cat> instead of > because snapcraft for some reason doesn't allow using a regular > shell redirection with "go run".
${GO} run ${GO_FLAGS} github.com/divVerent/aaaaxy/cmd/dumpcps -level="$lname" |cat> "assets/generated/$lname.cp.dot"
grep -c . "assets/generated/$lname.cp.dot"
neato -Tjson assets/generated/level.cp.dot > assets/generated/level.cp.json
grep -c . "assets/generated/$lname.cp.json"
trap - EXIT
fi
if [ x"$AAAAXY_DIFF_ASSETS" != x'false' ]; then
diff -bu -I'.*"width".*' assets/_saved/level.cp.json assets/generated/level.cp.json
fi
done
fi

if [ x"${AAAAXY_FORCE_GENERATE_ASSETS}" = x'true' ] || [ x"${AAAAXY_DIFF_ASSETS}" != x'false' ]; then
Expand Down
16 changes: 11 additions & 5 deletions scripts/xgettext.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

xgettext --its=scripts/tmx.its --from-code=utf-8 -F --no-location \
-o - assets/maps/level.tmx |\
sed -e 's/^#. #:/#:/g' \
> assets/locales/level.pot
lnames=
for lfile in assets/maps/*.tmx; do
lname=${lfile%.tmx}
lname=${lname##*/}
xgettext --its=scripts/tmx.its --from-code=utf-8 -F --no-location \
-o - "assets/maps/$lname.tmx" |\
sed -e 's/^#. #:/#:/g' \
> "assets/locales/$lname.pot"
lnames="$lnames $lfile"
done
go run github.com/leonelquinteros/gotext/cli/xgotext \
-default game_raw \
-in internal/ \
Expand Down Expand Up @@ -58,7 +64,7 @@ for d in assets/locales/*/; do
# Go's x/text/language always uses dashes as separator.
lingua=$(echo "$language" | tr _ -)
all_linguas="$all_linguas$lingua$LF"
for domain in level game; do
for domain in $lnames game; do
f=assets/locales/"$language"/"$domain".po
if ! [ -f "$f" ]; then
echo "$f: not found"
Expand Down

0 comments on commit a4ae109

Please sign in to comment.