Skip to content

Commit

Permalink
Handle level IDs in links correctly when loading/saving
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnovak committed Apr 2, 2024
1 parent 093a9df commit 762844a
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 84 deletions.
14 changes: 7 additions & 7 deletions extras/docs/fileformat.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ Map links chunk

numLinks records of:

UINT16 srcLevel (max=numLevels-1)
UINT16 srcLevelIndex (max=numLevels-1)
UINT16 srcRow (max=numRows-1 of the given level)
UINT16 srcColumn (max=numColumns-1 of the given level)

UINT16 destLevel (max=numLevel-1)
UINT16 destLevelIndex (max=numLevel-1)
UINT16 destRow (max=numRows-1 of the given level)
UINT16 destColumn (max=numColumns-1 of the given level)

Expand All @@ -205,7 +205,7 @@ App state chunks (in 'stat' group chunk, optional)
'/', '\', ':', '*', '?', '"', '<', '>', '|', '^', <zero byte>

UINT8 zoomLevel (min=1, max=20)
UINT8 currLevel (max=numLevels-1)
UINT8 currLevelIndex (max=numLevels-1)
UINT16 cursorRow (max=numRows-1 of the given level)
UINT16 cursorColumn (max=numColumns-1 of the given level)
UINT16 viewStartRow (max=numRows-1 of the given level)
Expand Down Expand Up @@ -237,7 +237,7 @@ App state chunks (in 'stat' group chunk, optional)

numLevels records of:

UINT32 levelId
UINT32 levelIndex
UINT8 levelSection (0=closed, 1=open)

if numRegions > 0 for the level (enableRegions can still be 0):
Expand All @@ -254,9 +254,9 @@ Version 4
[Gridmonger v1.2.0 - 2024-04-22]

- Remove redundant region coordinates from the 'regn' chunk.
- Convert 'disp' chunk into a group chunk with multiple subchunks and:
- Add 'optWraparound' to the 'opts' subchunk.
- Add 'notl' subchunk.
- Convert 'disp' chunk into a group chunk with multiple subchunks and
- add 'optWraparound' to the 'opts' subchunk.
- add 'notl' subchunk.


Version 3
Expand Down
18 changes: 6 additions & 12 deletions src/actions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -771,15 +771,13 @@ proc resizeLevel*(map; loc: Location, newRows, newCols: Natural,
levelId = loc.levelId
l = m.levels[levelId]

# Propagate ID as this is the same level, just resized
var newLevel = newLevel(l.locationName, l.levelName, l.elevation,
newRows, newCols,
l.overrideCoordOpts, l.coordOpts,
l.regionOpts,
l.notes,
initRegions=false)

# Propagate ID as this is the same level, just resized
newLevel.id = levelId
initRegions=false, overrideId=levelId.some)

newLevel.copyCellsAndAnnotationsFrom(destRow, destCol, l, copyRect)

Expand Down Expand Up @@ -846,12 +844,10 @@ proc cropLevel*(map; loc: Location, cropRect: Rect[Natural]; um): Location =
newLevelRect, wraparound=false)

let action = proc (m: var Map): UndoStateData =
let levelId = loc.levelId
var newLevel = m.newLevelFrom(levelId, cropRect)
let levelId = loc.levelId

# Propagate ID as this is the same level, just cropped
newLevel.id = levelId

let newLevel = m.newLevelFrom(levelId, cropRect, overrideId=levelId.some)
m.setLevel(newLevel)

# Adjust links
Expand Down Expand Up @@ -907,18 +903,16 @@ 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,
sb.level.rows, sb.level.cols,
sb.level.overrideCoordOpts, sb.level.coordOpts,
sb.level.regionOpts,
sb.level.notes,
initRegions=false
initRegions=false, overrideId=levelId.some
)

# Propagate ID as this is the same level, just nudged
l.id = levelId

if wraparound:
discard l.pasteWithWraparound(destRow=rowOffs, destCol=colOffs,
srcLevel=sb.level, sb.selection,
Expand Down
4 changes: 4 additions & 0 deletions src/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ type
csLetter = 1

Level* = ref object
# Internal ID, never written to disk
id*: Natural

locationName*: string
levelName*: string
elevation*: int
Expand Down Expand Up @@ -139,7 +141,9 @@ type
row*, col*: Natural

Region* = object
# Internal ID, never written to disk
id*: Natural

name*: string
notes*: string

Expand Down
6 changes: 4 additions & 2 deletions src/drawlevel.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2751,13 +2751,15 @@ proc drawLevel*(map: Map, levelId: Natural; ctx) =
dp.viewStartRow + dp.viewRows,
dp.viewStartCol + dp.viewCols
),
border = ViewBufBorder
border = ViewBufBorder,
overrideId = 0.Natural.some
)
else:
newLevel(
locationName = "", levelName = "", elevation = 0,
rows = dp.viewRows + ViewBufBorder*2,
cols = dp.viewCols + ViewBufBorder*2
cols = dp.viewCols + ViewBufBorder*2,
overrideId = 0.Natural.some
)

assert dp.viewStartRow + dp.viewRows <= l.rows
Expand Down
34 changes: 19 additions & 15 deletions src/level.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import selection

using l: Level

const
# internal ID, never written to disk
MoveBufferLevelId* = Natural.high
const MoveBufferLevelId* = Natural.high

# {{{ CellGrid

Expand Down Expand Up @@ -309,9 +307,9 @@ proc eraseOrphanedWalls*(l; r,c: Natural) =
# {{{ eraseCellWalls*()
proc eraseCellWalls*(l; r,c: Natural) =
l.setWall(r,c, dirN, wNone)
l.setWall(r,c, dirW, wNone)
l.setWall(r,c, dirW, wNone)
l.setWall(r,c, dirS, wNone)
l.setWall(r,c, dirE, wNone)
l.setWall(r,c, dirE, wNone)

# }}}
# {{{ eraseCell*()
Expand Down Expand Up @@ -527,20 +525,24 @@ const DefaultRegionOpts = RegionOptions(

proc newLevel*(locationName, levelName: string, elevation: int,
rows, cols: Natural,
overrideCoordOpts: bool = false,
overrideCoordOpts = false,
coordOpts: CoordinateOptions = DefaultCoordOpts,
regionOpts: RegionOptions = DefaultRegionOpts,
notes: string = "",
initRegions: bool = true): Level =
initRegions = true,
overrideId: Option[Natural] = Natural.none): Level =

var l = new Level

l.id = g_levelIdCounter
inc(g_levelIdCounter)
if overrideId.isSome:
l.id = overrideId.get
else:
l.id = g_levelIdCounter
inc(g_levelIdCounter)

l.locationName = locationName
l.levelName = levelName
l.elevation = elevation
l.levelName = levelName
l.elevation = elevation

l.overrideCoordOpts = overrideCoordOpts
l.coordOpts = coordOpts
Expand All @@ -565,8 +567,8 @@ proc calcNewLevelFromParams*(
srcLevel: Level, srcRect: Rect[Natural], border: Natural = 0
): tuple[copyRect: Rect[Natural], destRow, destCol: Natural] =

assert srcRect.r1 < srcLevel.rows
assert srcRect.c1 < srcLevel.cols
assert srcRect.r1 < srcLevel.rows
assert srcRect.c1 < srcLevel.cols
assert srcRect.r2 <= srcLevel.rows
assert srcRect.c2 <= srcLevel.cols

Expand Down Expand Up @@ -601,7 +603,8 @@ proc calcNewLevelFromParams*(
# NOTE: This method doesn't copy the regions.

proc newLevelFrom*(srcLevel: Level, srcRect: Rect[Natural],
border: Natural = 0): Level =
border: Natural = 0,
overrideId: Option[Natural] = Natural.none): Level =

let (copyRect, destRow, destCol) = calcNewLevelFromParams(srcLevel, srcRect,
border)
Expand All @@ -612,7 +615,8 @@ proc newLevelFrom*(srcLevel: Level, srcRect: Rect[Natural],
cols = srcRect.cols + border*2,
srcLevel.overrideCoordOpts, srcLevel.coordOpts, srcLevel.regionOpts,
srcLevel.notes,
initRegions=false
initRegions = false,
overrideId = overrideId
)

result.copyCellsAndAnnotationsFrom(destRow, destCol, srcLevel, copyRect)
Expand Down
7 changes: 4 additions & 3 deletions src/map.nim
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,13 @@ proc calcRegionResizeOffsets*(

# }}}

# {{{ newLevelFrom*()
proc newLevelFrom*(m; srcLevelId: Natural, srcRect: Rect[Natural]): Level =
proc newLevelFrom*(m; srcLevelId: Natural, srcRect: Rect[Natural],
overrideId: Option[Natural] = Natural.none): Level =

let src = m.levels[srcLevelId]
alias(ro, src.regionOpts)

var dest = newLevelFrom(src, srcRect)
var dest = newLevelFrom(src, srcRect, overrideId=overrideId)

# Copy regions
let (copyRect, _, _) = calcNewLevelFromParams(src, srcRect)
Expand Down
Loading

0 comments on commit 762844a

Please sign in to comment.