diff --git a/src/actions.nim b/src/actions.nim index 546d874e..fa727af2 100644 --- a/src/actions.nim +++ b/src/actions.nim @@ -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( @@ -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() @@ -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) @@ -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, diff --git a/src/level.nim b/src/level.nim index 41eb1686..e14f162b 100644 --- a/src/level.nim +++ b/src/level.nim @@ -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, @@ -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 diff --git a/src/main.nim b/src/main.nim index 15e32d3f..1a2480ac 100644 --- a/src/main.nim +++ b/src/main.nim @@ -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")) diff --git a/src/persistence.nim b/src/persistence.nim index f7f859da..03599207 100644 --- a/src/persistence.nim +++ b/src/persistence.nim @@ -519,7 +519,7 @@ proc readLinks( # }}} # {{{ readLevelProperties() -proc readLevelProperties(rr; levelId: Natural): Level = +proc readLevelProperties(rr): Level = debug(fmt"Reading level properties...") pushDebugIndent() @@ -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 @@ -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() @@ -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) @@ -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 @@ -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: @@ -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: diff --git a/src/undomanager.nim b/src/undomanager.nim index aca3bd52..a49d1599 100644 --- a/src/undomanager.nim +++ b/src/undomanager.nim @@ -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) # }}}