From aeec8100009c23796119fa7516d7aed04998236b Mon Sep 17 00:00:00 2001 From: John Novak Date: Sun, 24 Dec 2023 11:10:49 +1000 Subject: [PATCH] try to make excavate tunnel more intelligent --- src/actions.nim | 8 ++++---- src/main.nim | 49 +++++++++++++++++++++++++++++++------------------ src/map.nim | 15 ++++++++++----- 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/src/actions.nim b/src/actions.nim index ee0bc3dc..fdc65db7 100644 --- a/src/actions.nim +++ b/src/actions.nim @@ -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) # }}} @@ -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) diff --git a/src/main.nim b/src/main.nim index 09ee2629..78512a29 100644 --- a/src/main.nim +++ b/src/main.nim @@ -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, @@ -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 @@ -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() @@ -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 @@ -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: diff --git a/src/map.nim b/src/map.nim index 0db8d8c2..38d18246 100644 --- a/src/map.nim +++ b/src/map.nim @@ -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) @@ -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) # }}}