Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions internal/game/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (g *Game) EndPlayerTurn() {
g.decayTileEffects()
g.UpdateFOV()
if !g.Player.IsAlive() {
g.DeleteSave()
g.State = StateGameOver
}
}
Expand Down
8 changes: 4 additions & 4 deletions internal/game/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
35 changes: 35 additions & 0 deletions internal/game/ai_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
12 changes: 11 additions & 1 deletion internal/game/combat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 17 additions & 0 deletions internal/game/game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
83 changes: 83 additions & 0 deletions internal/game/palette_roles.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
}
66 changes: 64 additions & 2 deletions internal/game/palette_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
}
}
8 changes: 8 additions & 0 deletions internal/game/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading
Loading