Implemented printf for handy debugging, and now generating a map index for the 2D sections.

This commit is contained in:
Martin Haye 2015-03-25 07:40:46 -07:00
parent 56897dd67d
commit a835321b5f
2 changed files with 58 additions and 66 deletions

View File

@ -413,6 +413,13 @@ class PackPartitions
def buffers = new ByteBuffer[nVertSections][nHorzSections] def buffers = new ByteBuffer[nVertSections][nHorzSections]
def sectionNums = new int[nVertSections][nHorzSections] def sectionNums = new int[nVertSections][nHorzSections]
// Start the map index, which will list all the section numbers.
def indexBuf = ByteBuffer.allocate(512)
def indexNum = maps2D.size() + 1
maps2D[mapName] = [num:indexNum, buf:indexBuf]
indexBuf.put((byte)nHorzSections)
indexBuf.put((byte)nVertSections)
// Allocate a buffer and assign a map number to each section. // Allocate a buffer and assign a map number to each section.
(0..<nVertSections).each { vsect -> (0..<nVertSections).each { vsect ->
@ -423,9 +430,14 @@ class PackPartitions
sectionNums[vsect][hsect] = num sectionNums[vsect][hsect] = num
def sectName = "$mapName-$hsect-$vsect" def sectName = "$mapName-$hsect-$vsect"
maps2D[sectName] = [num:num, buf:buf] maps2D[sectName] = [num:num, buf:buf]
indexBuf.put((byte)num)
} }
} }
// Finish the index buffer with the map name
writeString(indexBuf, mapName.replaceFirst(/ ?-? ?2D/, ""))
// Now create each map section
(0..<nVertSections).each { vsect -> (0..<nVertSections).each { vsect ->
(0..<nHorzSections).each { hsect -> (0..<nHorzSections).each { hsect ->

View File

@ -113,10 +113,6 @@ byte prevMapNum
byte prevMapIs3D byte prevMapIs3D
byte redraw byte redraw
byte titleLoaded = FALSE byte titleLoaded = FALSE
byte cacheSky, cacheGround
word cacheX, cacheY
byte cacheDir
byte resetLocFromCache = FALSE
byte textDrawn = FALSE byte textDrawn = FALSE
// Movement amounts when walking at each angle // Movement amounts when walking at each angle
@ -562,9 +558,20 @@ def fatal(msg)
loader(FATAL_ERROR, MAIN_MEM, msg) loader(FATAL_ERROR, MAIN_MEM, msg)
end end
///////////////////////////////////////////////////////////////////////////////////////////////////
// Print a signed decimal word
def printDec(n)
if n < 0; printChar('-'); n = -n; fin
if n > 9999; printChar('0' + n/10000); n = n%10000; fin
if n > 999; printChar('0' + n/1000); n = n%1000; fin
if n > 99; printChar('0' + n/100); n = n%100; fin
if n > 9; printChar('0' + n/10); n = n%10; fin
printChar('0' + n)
end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
// Print a formatted string a'la C printf, with up to four parameters. // Print a formatted string a'la C printf, with up to four parameters.
def printf2(str, arg1, arg2) def printf4(str, arg1, arg2, arg3, arg4)
word pos word pos
word curArg word curArg
word p word p
@ -578,20 +585,25 @@ def printf2(str, arg1, arg2)
p = str + pos + 2 p = str + pos + 2
when ^p when ^p
is 'x' is 'x'
printHex(*curArg) printHex(*curArg); break
break is 'd'
printDec(*curArg); break
is 's'
puts(*curArg); break
is '%' is '%'
printChar('%') printChar('%'); break
break
otherwise otherwise
printHex(^p) printHex(^p); fatal("Unknown % code")
fatal("Unknown % code")
wend wend
curArg = curArg + 2 curArg = curArg + 2
pos = pos + 2 pos = pos + 2
loop loop
end end
def printf1(str, arg1); printf4(str, arg1, 0, 0, 0); end
def printf2(str, arg1, arg2); printf4(str, arg1, arg2, 0, 0); end
def printf3(str, arg1, arg2, arg3); printf4(str, arg1, arg2, arg3, 0); end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
// Get a keystroke and convert it to upper case // Get a keystroke and convert it to upper case
def getUpperKey() def getUpperKey()
@ -690,7 +702,7 @@ end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
// Load code and data, set up everything to display a 3D map // Load code and data, set up everything to display a 3D map
def initMap3D() def initMap3D(x, y, dir)
word scriptModule word scriptModule
// Set up the command table // Set up the command table
@ -750,24 +762,10 @@ def initMap3D()
prevX = -1 prevX = -1
prevY = -1 prevY = -1
triggerTbl = NULL triggerTbl = NULL
prevMapNum = mapNum
prevMapIs3D = mapIs3D
if pScripts if pScripts
*pScripts() *pScripts()
fin fin
// If we're returning to a map, resume from where the player left off
if resetLocFromCache
*playerX = cacheX
*playerY = cacheY
^playerDir = cacheDir
prevX = playerX.1 - 1
prevY = playerY.1 - 1
setSky(cacheSky)
setGround(cacheGround)
resetLocFromCache = FALSE
fin
// Draw the first frame // Draw the first frame
renderFrame() renderFrame()
redraw = FALSE redraw = FALSE
@ -776,7 +774,6 @@ end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
// Load code and data, set up everything to display a 2D map // Load code and data, set up everything to display a 2D map
def initMap2D() def initMap2D()
word scriptModule
// Set up the command table // Set up the command table
initCmds2D() initCmds2D()
@ -809,8 +806,6 @@ def initMap2D()
prevX = -1 prevX = -1
prevY = -1 prevY = -1
triggerTbl = NULL triggerTbl = NULL
prevMapNum = mapNum
prevMapIs3D = mapIs3D
// Start up the tile engine // Start up the tile engine
initDisplayEngine(mapNum) initDisplayEngine(mapNum)
@ -822,12 +817,14 @@ end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
// Set up mapNum (2D or 3D depending on state of is3DMap) // Set up mapNum (2D or 3D depending on state of is3DMap)
def initMap() def initMap(x, y, dir)
if mapIs3D if mapIs3D
initMap3D() initMap3D(x, y, dir)
else else
initMap2D() initMap2D(x, y, dir)
fin fin
prevMapNum = mapNum
prevMapIs3D = mapIs3D
end end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
@ -904,9 +901,7 @@ end
def checkScripts() def checkScripts()
word x word x
word y word y
if !triggerTbl if !triggerTbl; return; fin
return
fin
if mapIs3D if mapIs3D
x = playerX.1 - 1 x = playerX.1 - 1
y = playerY.1 - 1 y = playerY.1 - 1
@ -923,12 +918,12 @@ def checkScripts()
fin fin
if isScripted() if isScripted()
callScripts(x, y) callScripts(x, y)
if (mapNum <> prevMapNum) or (mapIs3D <> prevMapIs3D)
flipToFirstPage()
initMap()
fin
fin fin
fin fin
if (mapNum <> prevMapNum) or (mapIs3D <> prevMapIs3D)
flipToFirstPage()
initMap()
fin
end end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
@ -1010,23 +1005,6 @@ def moveWest()
move2D(-1, 0) move2D(-1, 0)
end end
///////////////////////////////////////////////////////////////////////////////////////////////////
// Switch to a new map (2D or 3D)
def setMap(is3D, num)
// save player state if we're coming *from* the over-map
if mapNum == OVERMAP_NUM and mapIs3D == OVERMAP_IS_3D
cacheX = *playerX
cacheY = *playerY
cacheDir = ^playerDir
cacheSky = skyNum
cacheGround = groundNum
else
resetLocFromCache = TRUE
fin
mapIs3D = is3D
mapNum = num
end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
// Switch to next map in the current 2D or 3D mode. // Switch to next map in the current 2D or 3D mode.
def nextMap() def nextMap()
@ -1038,16 +1016,19 @@ def nextMap()
end end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
// Jump to a new map location (and in 3D mode, point in the given direction) // Jump to a new map location (and point in the given direction)
def teleport(x, y, dir) def teleport(x, y, dir)
*playerX = ((x+1)<<8) | $80 nextX = x
*playerY = ((y+1)<<8) | $80 nextY = y
prevX = x nextDir = dir
prevY = y end
clearWindow()
textDrawn = FALSE ///////////////////////////////////////////////////////////////////////////////////////////////////
^playerDir = dir // Switch to a new map (2D or 3D)
redraw = TRUE def setMap(is3D, num, x, y, dir)
mapIs3D = is3D
mapNum = num
teleport(x, y, dir)
end end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
@ -1147,7 +1128,6 @@ end
// Load and display the title screen. // Load and display the title screen.
def loadTitle() def loadTitle()
puts("Loading Lawless Legends.\n") puts("Loading Lawless Legends.\n")
printf2("Test %x and %x...\n", $1001, $2002)
// Load the title screen // Load the title screen
puts("Loading title screen.\n") puts("Loading title screen.\n")