diff --git a/README.md b/README.md index caad975..554c63b 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,10 @@ The player-facing command set is intentionally melee-focused. | `Space` | close a modal screen without taking an action | | `Q` | quit | -Walking into a monster attacks it. Bashing is intentional: use `f` and a -direction. A bash takes a turn, and some containers such as barrels or crates -may drop supplies when smashed. +Walking into a monster attacks it. Player and monster movement and attacks are +cardinal only (`hjkl`); there are no diagonal attacks. Bashing is intentional: +use `f` and a direction. A bash takes a turn, and some containers such as +barrels or crates may drop supplies when smashed. Screens generally close with `z`, `q`, `Space`, or `Esc`. The player is defenceless without a melee weapon; there is no unarmed attack. Flask throwing @@ -116,7 +117,7 @@ grants a second Rite is not implemented yet. * auto-opening doors, portals, bashing, acid/fire/smoke, rain * options screen wired to the gameplay (AI, doors, god/ghost mode, …) * JSON save/load for the current development format; older saves may break - between iterations + between iterations, and the save is removed when the priest dies ## Fidelity notes diff --git a/internal/game/actions.go b/internal/game/actions.go index 7c95985..b5835d5 100644 --- a/internal/game/actions.go +++ b/internal/game/actions.go @@ -31,6 +31,7 @@ func (g *Game) EndPlayerTurn() { g.decayTileEffects() g.UpdateFOV() if !g.Player.IsAlive() { + g.DeleteSave() g.State = StateGameOver } } diff --git a/internal/game/ai.go b/internal/game/ai.go index fa818e5..bd39fd8 100644 --- a/internal/game/ai.go +++ b/internal/game/ai.go @@ -105,11 +105,11 @@ func (g *Game) zombieTurn(c *Critter) { if c.Align == AlignEnemy && c.IsAnimal { radius = 8 } - target, dist := g.nearestHostile(c, radius) + target, _ := g.nearestHostile(c, radius) if target != nil { c.LastSeenX, c.LastSeenY = target.X, target.Y c.Alert = 8 - if dist <= 1 { + if isAdjacent(c, target) { g.MonsterAttack(c, target) return } @@ -153,7 +153,7 @@ func (g *Game) villagerTurn(c *Critter) { return } armed := c.HandItem() != nil - if dist <= 1 { + if isAdjacent(c, enemy) { if armed { g.MonsterAttack(c, enemy) return @@ -264,7 +264,7 @@ func (g *Game) observerTurn(c *Critter, canSee bool, dist int) { } return } - if dist <= 1 { + if isAdjacent(c, g.Player) { g.MonsterAttack(c, g.Player) return } diff --git a/internal/game/ai_test.go b/internal/game/ai_test.go index ee9debc..360cddd 100644 --- a/internal/game/ai_test.go +++ b/internal/game/ai_test.go @@ -112,6 +112,41 @@ func TestAIAnimalThreatRequiresLineOfSight(t *testing.T) { } } +func TestMonstersDoNotAttackDiagonally(t *testing.T) { + g := NewGame(7306) + g.SetupNewGame(0, "tester") + g.Critters = nil + g.State = StatePlaying + clearAround(g, g.Player.X, g.Player.Y, 4) + zombie := g.SpawnCritter(0, g.Player.X+1, g.Player.Y+1) + hp := g.Player.HP + for i := 0; i < 3; i++ { + zombie.X, zombie.Y = g.Player.X+1, g.Player.Y+1 + g.MonsterTurn(zombie) + } + if g.Player.HP != hp { + t.Fatalf("monster attacked diagonally: HP %d -> %d", hp, g.Player.HP) + } +} + +func TestPlayerDoesNotAttackDiagonally(t *testing.T) { + g := NewGame(7307) + g.SetupNewGame(0, "tester") + g.Critters = nil + g.State = StatePlaying + clearAround(g, g.Player.X, g.Player.Y, 4) + target := g.SpawnCritter(0, g.Player.X+1, g.Player.Y+1) + hp := target.HP + g.PlayerDirectionalAttack(1, 1) + if target.HP != hp { + t.Fatal("player attacked diagonally") + } + g.PlayerMelee(target) + if target.HP != hp { + t.Fatal("PlayerMelee allowed a diagonal attack") + } +} + func TestObserverUsesLineOfSight(t *testing.T) { g := NewGame(7305) g.SetupNewGame(0, "tester") diff --git a/internal/game/combat.go b/internal/game/combat.go index 35c60b8..a247121 100644 --- a/internal/game/combat.go +++ b/internal/game/combat.go @@ -11,8 +11,9 @@ func chebyshev(x0, y0, x1, y1 int) int { return dy } +// isAdjacent is cardinal only; diagonal attacks are not allowed. func isAdjacent(a, b *Critter) bool { - return a != nil && b != nil && chebyshev(a.X, a.Y, b.X, b.Y) <= 1 && !(a.X == b.X && a.Y == b.Y) + return a != nil && b != nil && manhattan(a.X, a.Y, b.X, b.Y) == 1 } func (c *Critter) CurrentWeapon() *Item { @@ -120,6 +121,10 @@ func (g *Game) PlayerDirectionalAttack(dx, dy int) { g.Msg("choose a direction to attack.", 8) return } + if abs(dx)+abs(dy) != 1 { + g.Msg("attacks are cardinal only.", 8) + return + } c := g.CritterAt(p.X+dx, p.Y+dy) if c == nil || !c.IsAlive() { g.Msg("there is nothing to attack in that direction.", 8) @@ -135,6 +140,10 @@ func (g *Game) PlayerMelee(target *Critter) { g.Msg("you are defenceless without a weapon.", 12) return } + if !isAdjacent(p, target) { + g.Msg("attacks are cardinal only.", 8) + return + } chance := g.HitChance(p, target, 1) if !g.Chance(chance) { g.Msg("you attack and miss.", 7) @@ -281,6 +290,7 @@ func (g *Game) KillCritter(target, killer *Critter) { if target.Player { g.Msg("you died.", 12) g.Msg("=== session ended ===", 7) + g.DeleteSave() g.State = StateGameOver return } diff --git a/internal/game/game_test.go b/internal/game/game_test.go index b6d904c..ddde061 100644 --- a/internal/game/game_test.go +++ b/internal/game/game_test.go @@ -84,6 +84,23 @@ func TestSaveLoadRoundtrip(t *testing.T) { } } +func TestDeathDeletesSave(t *testing.T) { + g := NewGame(35) + g.SetupNewGame(0, "tester") + path := filepath.Join(t.TempDir(), "death.json") + g.SavePath = path + if err := g.SaveGame(path); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(path); err != nil { + t.Fatal("save was not created") + } + g.DamageCritter(g.Player, 9999, nil, 0) + if _, err := os.Stat(path); !os.IsNotExist(err) { + t.Fatalf("save still exists after death: %v", err) + } +} + func TestChangeLevelRelative(t *testing.T) { g := NewGame(9) g.SetupNewGame(0, "tester") diff --git a/internal/game/palette_roles.go b/internal/game/palette_roles.go index 6f4493e..e162685 100644 --- a/internal/game/palette_roles.go +++ b/internal/game/palette_roles.go @@ -1,5 +1,7 @@ package game +import "strings" + // TileColorRole returns the woods palette role for a recovered tile ID. // Tiles that belong to other themes still get a role so they can be recoloured // consistently if they are reused by the woods generator later. @@ -194,3 +196,84 @@ func CritterColorRole(proto int) (ColorRole, bool) { } return "", false } + +// TileBackgroundRole returns a dark theme colour for the tile background. +// Every tile receives a background so the map reads as coloured terrain rather +// than glyphs on black. +func TileBackgroundRole(tile int) (ColorRole, bool) { + if tile <= 1 { + return RoleMapBackground, true + } + role, ok := TileColorRole(tile) + if !ok { + return RoleMapBackground, true + } + return backgroundRoleFor(role), true +} + +func backgroundRoleFor(role ColorRole) ColorRole { + switch role { + case RoleGrassBase, RoleGrassLight, RoleGrassDark, RoleLeafLitter, + RoleBush, RoleBramble, RoleThorn, RoleOrchardLeaf, + RoleFlowerGold, RoleFlowerPink, RoleFlowerPale, + RoleCanopyMid, RoleCanopyLight, RolePineCanopy: + return RoleGrassDark + case RoleStump, RoleDeadTree, RoleMud, RoleDirtRoad, RoleRoadLight, RoleReeds: + return RoleMud + case RoleMarsh: + return RoleMarsh + case RoleDeepWater: + return RoleDeepWater + case RoleRiver: + return RoleRiver + case RoleShallowWater, RoleFoam, RoleFrog: + return RoleShallowWater + case RoleBridgeWood, RoleBridgeRope, RoleWoodWallDark, RoleWoodWall, RoleWoodLight, + RoleRoof, RoleDoor, RoleWindowFrame, RoleWarmWindowGlass, RoleFence, + RoleFurniture, RoleThatch, RoleHay, RolePlasterWall, RoleFadedPaint, + RoleMarketCloth, RoleCloth: + return RoleWoodWallDark + case RoleStoneWall, RoleStoneLight, RoleRock, RoleRockLight, RoleAltarStone, + RoleGravestone, RoleBone, RoleCrypt, RoleIronFence, RoleIronDark, + RoleSilver, RoleSteel: + return RoleStoneWall + case RoleGraveDirt, RoleGraveFog: + return RoleGraveDirt + case RoleGoldRelic, RoleCandle, RoleLanternMetal, RoleLanternGlow, + RoleFirelight, RoleFlame, RoleFlameCore: + return RoleMud + case RoleFreshBlood, RoleDarkBlood, RoleDriedBlood, RoleBloodGrass, + RoleGuts, RoleRottenFlesh, RoleZombieSkin, RoleZombieShadow, RoleBoneTeeth: + return RoleDarkBlood + case RolePoison, RoleAcid, RoleAcidDark: + return RoleCharcoal + case RoleSmoke, RoleAsh, RoleCharcoal: + return RoleCharcoal + } + name := string(role) + switch { + case strings.HasPrefix(name, "terrain."): + return RoleMud + case strings.HasPrefix(name, "vegetation."): + return RoleGrassDark + case strings.HasPrefix(name, "water."): + return RoleShallowWater + case strings.HasPrefix(name, "structure."): + return RoleStoneWall + case strings.HasPrefix(name, "light."): + return RoleMud + default: + return RoleMapBackground + } +} + +// TileIsGround reports whether a tile is plain ground/floor and should render +// as a coloured space without a glyph. +func TileIsGround(tile int) bool { + switch tile { + case 2, 9, 13, 20, 25, 26, 27, + int(tileDirtRoad), int(tileMud), int(tileBurnedGround), int(tileRiverBank): + return true + } + return false +} diff --git a/internal/game/palette_test.go b/internal/game/palette_test.go index 5e9d826..f4715fa 100644 --- a/internal/game/palette_test.go +++ b/internal/game/palette_test.go @@ -319,8 +319,8 @@ func TestDrawGameUsesTrueColourBackground(t *testing.T) { t.Fatalf("true-colour map background is not RGB: %v", bg) } r, g, b := bg.RGB() - if r > 16 || g > 16 || b > 16 { - t.Fatalf("map background is not near-black: %v", bg) + if r+g+b < 30 { + t.Fatalf("map tile background is still near-black: %v", bg) } break } @@ -545,3 +545,65 @@ func TestDrawGameUsesPalette(t *testing.T) { } } } + +func TestEveryTileHasABackground(t *testing.T) { + for id := 0; id < len(TileDefs); id++ { + role, ok := TileBackgroundRole(id) + if !ok || role == "" { + t.Fatalf("tile %d has no background role", id) + } + } + for _, id := range []int{2, 9, 13, 20, 25, 26, 27, int(tileDirtRoad), int(tileMud), int(tileBurnedGround), int(tileRiverBank)} { + role, _ := TileBackgroundRole(id) + if role == RoleMapBackground { + t.Fatalf("ground/floor tile %d still uses the void background", id) + } + if !TileIsGround(id) { + t.Fatalf("tile %d should render as ground", id) + } + } +} + +func TestGroundTilesRenderAsBlankColouredSpace(t *testing.T) { + g := NewGame(16500) + g.SetupNewGame(0, "tester") + g.Map = NewMap(20, 12) + g.Theme = 0 + g.Critters = nil + g.Items = nil + for i := range g.Map.Tiles { + g.Map.Tiles[i] = 2 + } + g.Player.X, g.Player.Y = 1, 1 + ground := []struct { + x, y int + tile uint8 + }{ + {5, 5, 2}, + {6, 5, 9}, + {7, 5, 13}, + {8, 5, 20}, + {9, 5, tileDirtRoad}, + {10, 5, tileMud}, + {11, 5, tileBurnedGround}, + } + for _, tc := range ground { + g.Map.SetTile(tc.x, tc.y, tc.tile) + } + g.UpdateFOV() + u, s := newTestUI(g, ColorModeTrueColor) + defer s.Fini() + u.drawGame() + s.Show() + cells, w, _ := s.GetContents() + for _, tc := range ground { + cell := cells[(tc.y+1)*w+tc.x] + if len(cell.Runes) > 0 && cell.Runes[0] != ' ' { + t.Fatalf("ground tile %d rendered glyph %q, want blank space", tc.tile, cell.Runes[0]) + } + _, bg, _ := cell.Style.Decompose() + if !bg.IsRGB() { + t.Fatalf("ground tile %d background is not RGB: %v", tc.tile, bg) + } + } +} diff --git a/internal/game/save.go b/internal/game/save.go index 77f5cb7..07b9106 100644 --- a/internal/game/save.go +++ b/internal/game/save.go @@ -479,3 +479,11 @@ func (g *Game) SaveAndQuit() { g.Msg("game saved.", 10) g.Quit = true } + +// DeleteSave removes the current save, used when the run is over. +func (g *Game) DeleteSave() { + if g == nil || g.SavePath == "" { + return + } + _ = os.Remove(g.SavePath) +} diff --git a/internal/game/ui.go b/internal/game/ui.go index 290d12b..5741bdc 100644 --- a/internal/game/ui.go +++ b/internal/game/ui.go @@ -265,7 +265,7 @@ func (u *UI) worldColor(role ColorRole, ok bool, fallback uint8, x, y int, visib // worldBackgroundColor resolves a background colour. It keeps the supplied // fallback index instead of the remembered-tile grey used for glyphs. func (u *UI) worldBackgroundColor(role ColorRole, ok bool, fallback uint8, x, y int, visible bool) tcell.Color { - if !ok || !u.woodsColorsActive() { + if !ok { return pal(fallback) } pc, found := WoodsPalette.Lookup(role) @@ -483,9 +483,10 @@ func (u *UI) drawGame() { u.putTcell(sx, sy+1, ' ', mapBG, mapBG) continue } + tileID := int(g.Map.Tile(mx, my)) d := g.Map.Def(mx, my) vis := g.Map.Visible[i] || g.Options.Bool("optMapDrawHiddenTiles") - tileRole, tileRoleOK := TileColorRole(int(g.Map.Tile(mx, my))) + tileRole, tileRoleOK := TileColorRole(tileID) fg := d.Color fgRole, fgRoleOK := tileRole, tileRoleOK fgVisible := vis @@ -493,21 +494,24 @@ func (u *UI) drawGame() { fg = 8 } r := cp437Rune(d.Glyph) + if TileIsGround(tileID) { + r = ' ' + } if g.Options.Bool("optMapDrawPortals") && (d.Name == "time portal" || d.Name == "stone stairs" || d.Name == "elevator") { fg = 14 fgRole, fgRoleOK = RoleGoldRelic, true fgVisible = true } - bg := uint8(0) - bgRole, bgRoleOK := RoleMapBackground, true + bgRole, bgRoleOK := TileBackgroundRole(tileID) + bg := d.Color + if pc, found := WoodsPalette.Lookup(bgRole); found { + bg = pc.Fallback + } blooded := g.Map.Blood[i] > 0 && d.Type == 1 if blooded { - bg = 4 // DOS red background - if !vis { - bg = 0 - bgRole, bgRoleOK = RoleMapBackground, true - } else { - bgRole, bgRoleOK = RoleDarkBlood, true + bgRole, bgRoleOK = RoleDarkBlood, true + if pc, found := WoodsPalette.Lookup(bgRole); found { + bg = pc.Fallback } } if g.Map.Fire[i] > 0 { @@ -604,6 +608,9 @@ func (g *Game) CellForHighlight(x, y int) (rune, uint8) { } d := g.Map.Def(x, y) r, fg := cp437Rune(d.Glyph), d.Color + if TileIsGround(int(g.Map.Tile(x, y))) { + r = ' ' + } if !g.Map.Visible[i] && !g.Options.Bool("optMapDrawHiddenTiles") { fg = 8 }