Skip to content

Commit

Permalink
Fix level ID handling when undoing/redoing
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnovak committed Apr 9, 2024
1 parent a89b6ed commit 23603b0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
8 changes: 6 additions & 2 deletions src/actions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ template cellAreaAction(map; loc, undoLoc: Location, rect: Rect[Natural];

var oldLinks = map.links

let undoLevel = map.newLevelFrom(loc.levelId, rect)
let undoLevel = map.newLevelFrom(loc.levelId, rect,
overrideId=0.Natural.some)

let undoAction = proc (m: var Map): UndoStateData =
m.levels[loc.levelId].copyCellsAndAnnotationsFrom(
Expand Down Expand Up @@ -327,7 +328,8 @@ proc setLink*(map; src, dest: Location, floorColor: Natural; um) =
c = dest.col
rect = rectN(r, c, r+1, c+1) # single cell

let undoLevel = map.newLevelFrom(dest.levelId, rect)
let undoLevel = map.newLevelFrom(dest.levelId, rect,
overrideId=0.Natural.some)

var oldLinks = initLinks()

Expand Down Expand Up @@ -686,6 +688,7 @@ proc addNewLevel*(map; loc: Location,
# Undo action
let undoAction = proc (m: var Map): UndoStateData =
m.delLevel(newLevelId)
setNextLevelId(newLevelId)

for src in m.links.filterByLevel(newLevelId).sources:
m.links.delBySrc(src)
Expand Down Expand Up @@ -904,6 +907,7 @@ proc nudgeLevel*(map; loc: Location, rowOffs, colOffs: int,

# Do action
let action = proc (m: var Map): UndoStateData =

# Propagate ID as this is the same level, just nudged
var l = newLevel(
sb.level.locationName, sb.level.levelName, sb.level.elevation,
Expand Down
13 changes: 9 additions & 4 deletions src/level.nim
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,14 @@ proc isSpecialLevelId*(id: Natural): bool =

# }}}

# {{{ newLevel*()
# {{{ setNextLevelId*()
var g_nextLevelId = 0

proc setNextLevelId*(nextId: Natural) =
g_nextLevelId = nextId

var g_levelIdCounter = 0
# }}}
# {{{ newLevel*()

const DefaultCoordOpts = CoordinateOptions(
origin: coNorthWest,
Expand Down Expand Up @@ -509,8 +514,8 @@ proc newLevel*(locationName, levelName: string, elevation: int,
if overrideId.isSome:
l.id = overrideId.get
else:
l.id = g_levelIdCounter
inc(g_levelIdCounter)
l.id = g_nextLevelId
inc(g_nextLevelId)

l.locationName = locationName
l.levelName = levelName
Expand Down
2 changes: 2 additions & 0 deletions src/main.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4094,6 +4094,8 @@ proc newMapDialog(dlg: var NewMapDialogParams; a) =

a.opts.drawTrail = false

setNextLevelId(0)

a.doc.path = ""
a.doc.map = newMap(dlg.title, dlg.game, dlg.author,
creationTime=now().format("yyyy-MM-dd HH:mm:ss"))
Expand Down
19 changes: 9 additions & 10 deletions src/persistence.nim
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ proc readLinks(

# }}}
# {{{ readLevelProperties()
proc readLevelProperties(rr; levelId: Natural): Level =
proc readLevelProperties(rr): Level =
debug(fmt"Reading level properties...")
pushDebugIndent()

Expand All @@ -545,8 +545,7 @@ proc readLevelProperties(rr; levelId: Natural): Level =
let notes = rr.readWStr
checkStringLength(notes, "lvl.prop.notes", NotesLimits)

result = newLevel(locationName, levelName, elevation, numRows, numColumns,
overrideId = levelId.some)
result = newLevel(locationName, levelName, elevation, numRows, numColumns)
result.overrideCoordOpts = overrideCoordOpts.bool
result.notes = notes

Expand Down Expand Up @@ -820,7 +819,7 @@ proc readLevelRegions(rr; levelCols: Natural,

# }}}
# {{{ readLevel()
proc readLevel(rr; levelId: Natural; version: Natural): Level =
proc readLevel(rr; version: Natural): Level =
debug(fmt"Reading level...")
pushDebugIndent()

Expand Down Expand Up @@ -881,7 +880,7 @@ proc readLevel(rr; levelId: Natural; version: Natural): Level =
if cellCursor.isNone: chunkNotFoundError(FourCC_GRMM_cell)

rr.cursor = propCursor.get
var level = readLevelProperties(rr, levelId)
var level = readLevelProperties(rr)

rr.cursor = coorCursor.get
level.coordOpts = readCoordinateOptions(rr, groupChunkId.get)
Expand Down Expand Up @@ -911,7 +910,6 @@ proc readLevelList(rr; version: Natural): OrderedTable[Natural, Level] =
pushDebugIndent()

var levels = initOrderedTable[Natural, Level]()
var levelId = 0

if rr.hasSubChunks:
var ci = rr.enterGroup
Expand All @@ -927,9 +925,8 @@ proc readLevelList(rr; version: Natural): OrderedTable[Natural, Level] =

# The level IDs must be set to their indices in the map file to
# ensure they are in sync with link (see writeLinks()).
let level = readLevel(rr, levelId, version)
levels[levelId] = level
inc(levelId)
let level = readLevel(rr, version)
levels[level.id] = level

rr.exitGroup
else:
Expand Down Expand Up @@ -1134,10 +1131,12 @@ proc readMapFile*(path: string): tuple[map: Map,
elif appStateGroupCursor.isSome:
rr.cursor = appStateGroupCursor.get
readAppState_V4(rr, map).some

else:
AppState.none

# Level IDs start from zero when loading a map
setNextLevelId(map.levels.len)

result = (map, appState, warning)

except MapReadError as e:
Expand Down
2 changes: 1 addition & 1 deletion src/undomanager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ proc undo*[S, R](m: var UndoManager[S, R], s: var S): R =
let undoNextState = m.states[m.currState].groupWithPrev
dec(m.currState)
if undoNextState:
discard m.undo(s)
result = m.undo(s)

# }}}

Expand Down

0 comments on commit 23603b0

Please sign in to comment.