Skip to content

Commit

Permalink
try to make excavate tunnel more intelligent
Browse files Browse the repository at this point in the history
  • Loading branch information
John Novak committed Dec 30, 2023
1 parent 05d50d9 commit aeec810
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/actions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ proc eraseCellWalls*(map; loc: Location; um) =

# }}}
# {{{ excavateTunnel*()
proc excavateTunnel*(map; loc, undoLoc: Location, floorColor: Natural; um;
groupWithPrev: bool) =
proc excavateTunnel*(map; loc, undoLoc: Location, floorColor: Natural;
dir: Option[CardinalDir]; um; groupWithPrev: bool) =

singleCellAction(map, loc, undoLoc, um, groupWithPrev,
"Excavate tunnel", m):
m.excavateTunnel(loc, floorColor)
m.excavateTunnel(loc, floorColor, dir)

# }}}

Expand Down Expand Up @@ -195,7 +195,7 @@ proc excavateTrail*(map; loc: Location, bbox: Rect[Natural],

if m.hasTrail(loc):
if m.isEmpty(loc):
m.excavateTunnel(loc, floorColor)
m.excavateTunnel(loc, floorColor, dir=CardinalDir.none)
else:
m.setFloorColor(loc, floorColor)

Expand Down
49 changes: 31 additions & 18 deletions src/main.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5182,6 +5182,7 @@ proc cycleFloorGroupAction(floors: seq[Floor], forward: bool; a) =
proc startExcavateTunnelAction(a) =
let cur = a.ui.cursor
actions.excavateTunnel(a.doc.map, loc=cur, undoLoc=cur, a.ui.currFloorColor,
dir=CardinalDir.none,
a.doc.undoManager, groupWithPrev=false)

setStatusMessage(IconPencil, "Excavate tunnel", @[IconArrowsAll,
Expand Down Expand Up @@ -5734,8 +5735,6 @@ proc handleGlobalKeyEvents(a) =

# {{{ handleMoveWalk()
proc handleMoveWalk(ke: Event; a) =
const mkS = {mkShift}

var s = 1
if mkCtrl in ke.mods:
if ke.key in AllWasdMoveKeys: return
Expand All @@ -5753,12 +5752,29 @@ proc handleGlobalKeyEvents(a) =
elif ke.isKeyDown(k.strafeLeft, repeat=true): moveCursor(left(), s, a)
elif ke.isKeyDown(k.strafeRight, repeat=true): moveCursor(right(), s, a)

elif ke.isKeyDown(k.turnLeft, mkS, repeat=true): ui.cursorOrient = left()
elif ke.isKeyDown(k.turnRight, mkS, repeat=true): ui.cursorOrient = right()
elif ke.isKeyDown(k.strafeLeft, mkS, repeat=true): moveLevel(left(), s, a)
elif ke.isKeyDown(k.strafeRight, mkS, repeat=true): moveLevel(right(), s, a)
elif ke.isKeyDown(k.forward, mkS, repeat=true): moveLevel(ui.cursorOrient, s, a)
elif ke.isKeyDown(k.backward, mkS, repeat=true): moveLevel(backward(), s, a)
elif ke.isKeyDown(k.turnLeft, {mkShift}, repeat=true): ui.cursorOrient = left()
elif ke.isKeyDown(k.turnRight, {mkShift}, repeat=true): ui.cursorOrient = right()
elif ke.isKeyDown(k.strafeLeft, {mkShift}, repeat=true): moveLevel(left(), s, a)
elif ke.isKeyDown(k.strafeRight, {mkShift}, repeat=true): moveLevel(right(), s, a)
elif ke.isKeyDown(k.forward, {mkShift}, repeat=true): moveLevel(ui.cursorOrient, s, a)
elif ke.isKeyDown(k.backward, {mkShift}, repeat=true): moveLevel(backward(), s, a)

# }}}
# {{{ moveKeyToCardinalDir()
template moveKeyToCardinalDir(ke: Event, allowWasdKeys: bool,
allowRepeat: bool): Option[CardinalDir] =

let k = if allowWasdKeys and opts.wasdMode: MoveKeysWasd
else: MoveKeysCursor

var kk = ke
kk.mods = {}

if kk.isKeyDown(k.left, repeat=allowRepeat): dirW.some
elif kk.isKeyDown(k.right, repeat=allowRepeat): dirE.some
elif kk.isKeyDown(k.up, repeat=allowRepeat): dirN.some
elif kk.isKeyDown(k.down, repeat=allowRepeat): dirS.some
else: CardinalDir.none

# }}}
# {{{ handleMoveKeys()
Expand All @@ -5770,17 +5786,11 @@ proc handleGlobalKeyEvents(a) =
if not yubnMode and ke.key in DiagonalMoveLetterKeys:
return

let k = if allowWasdKeys and opts.wasdMode: MoveKeysWasd
else: MoveKeysCursor

let mods = ke.mods
var kk = ke
kk.mods = {}

if kk.isKeyDown(k.left, repeat=allowRepeat): moveHandler(dirW, mods, a)
elif kk.isKeyDown(k.right, repeat=allowRepeat): moveHandler(dirE, mods, a)
elif kk.isKeyDown(k.up, repeat=allowRepeat): moveHandler(dirN, mods, a)
elif kk.isKeyDown(k.down, repeat=allowRepeat): moveHandler(dirS, mods, a)
let dir = moveKeyToCardinalDir(ke, allowWasdKeys, allowRepeat)
if dir.isSome:
moveHandler(dir.get, mods, a)

if allowDiagonal:
let d = DiagonalMoveKeysCursor
Expand Down Expand Up @@ -6314,8 +6324,11 @@ proc handleGlobalKeyEvents(a) =

if cur != ui.prevCursor:
if ui.editMode == emExcavateTunnel:
let dir = moveKeyToCardinalDir(ke, allowWasdKeys=true,
allowRepeat=true)

actions.excavateTunnel(map, loc=cur, undoLoc=ui.prevCursor,
ui.currFloorColor,
ui.currFloorColor, dir,
um, groupWithPrev=opts.drawTrail)

elif ui.editMode == emEraseCell:
Expand Down
15 changes: 10 additions & 5 deletions src/map.nim
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ proc setTrail*(m; loc: Location, t: bool) =
# }}}

# {{{ excavateTunnel*()
proc excavateTunnel*(m; loc: Location, floorColor: Natural) =
proc excavateTunnel*(m; loc: Location, floorColor: Natural,
dir: Option[CardinalDir]) =
alias(l, m.levels[loc.level])
alias(c, loc.col)
alias(r, loc.row)
Expand All @@ -223,11 +224,15 @@ proc excavateTunnel*(m; loc: Location, floorColor: Natural) =
if label.isSome:
l.setAnnotation(loc.row, loc.col, label.get)

for dir in @[dirN, dirS, dirE, dirW]:
if l.isNeighbourCellEmpty(r, c, {dir}):
m.setWall(loc, dir, wWall)
var wallDirs = @[dirN, dirS, dirE, dirW]
if dir.isSome:
wallDirs.delete(wallDirs.find(dir.get))

for d in wallDirs:
if l.isNeighbourCellEmpty(r, c, {d}):
m.setWall(loc, d, wWall)
else:
m.setWall(loc, dir, wNone)
m.setWall(loc, d, wNone)

# }}}

Expand Down

0 comments on commit aeec810

Please sign in to comment.