Now splitting maps up into sections. Refactored game loop to make room for 2D variant.

This commit is contained in:
Martin Haye 2015-01-07 14:48:04 -08:00
parent bc73588a20
commit 4e4892381f
3 changed files with 138 additions and 120 deletions

View File

@ -402,8 +402,6 @@ class PackPartitions
def write2DMap(mapName, rows, tileSetNum, tileMap) def write2DMap(mapName, rows, tileSetNum, tileMap)
{ {
maps2D[name] = [num:num, buf:buf]
def width = rows[0].size() def width = rows[0].size()
def height = rows.size() def height = rows.size()
@ -417,44 +415,47 @@ class PackPartitions
def sectionNums = new int[nVertSections][nHorzSections] def sectionNums = new int[nVertSections][nHorzSections]
// 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 ->
(0..nHorzSections).each { hsect -> (0..<nHorzSections).each { hsect ->
buffers[vsect][hsect] = ByteBuffer.allocate(512) def buf = ByteBuffer.allocate(512)
def num = maps2d.size() + 1 buffers[vsect][hsect] = buf
def num = maps2D.size() + 1
def sectName = "$mapName-$hsect-$vsect" def sectName = "$mapName-$hsect-$vsect"
maps2d[sectName] = [num:num, buf:buf] maps2D[sectName] = [num:num, buf:buf]
} }
} }
(0..nVertSections).each { vsect -> (0..<nVertSections).each { vsect ->
(0..nHorzSections).each { hsect -> (0..<nHorzSections).each { hsect ->
// Header: first come links to other map sections - north, east, south, west // Header: first come links to other map sections - north, east, south, west
buf.put((byte) (vsect > 0) ? sectionNums[vsect-1][hsect] : 0) // north def buf = buffers[vsect][hsect]
buf.put((byte) (hsect > 0) ? sectionNums[vsect][hsect-1] : 0) // east buf.put((byte) (vsect > 0) ? sectionNums[vsect-1][hsect] : 0xFF) // north
buf.put((byte) (vsect < nVertSections-1) ? sectionNums[vsect+1][hsect] : 0) // south buf.put((byte) (hsect > 0) ? sectionNums[vsect][hsect-1] : 0xFF) // east
buf.put((byte) (hsect > 0) ? sectionNums[vsect-1][hsect] : 0) // west buf.put((byte) (vsect < nVertSections-1) ? sectionNums[vsect+1][hsect] : 0xFF) // south
} buf.put((byte) (hsect > 0) ? sectionNums[vsect-1][hsect] : 0xFF) // west
}
// Header: width and height // Then links to the tile set and script library
buf.put((byte)width) buf.put((byte) tileSetNum)
buf.put((byte)height) buf.put((byte) 0xFF) // script library placeholder
// Then tileSet number def hOff = hsect * TILES_PER_ROW
buf.put((byte)tileSetNum) def vOff = vsect * ROWS_PER_SECTION
// Followed by name
writeString(buf, mapName.replaceFirst(/ ?-? ?2D/, ""))
// After the header comes the raw data // After the header comes the raw data
rows.each { row -> (0..<ROWS_PER_SECTION).each { rowNum ->
row.each { tile -> def y = vOff + rowNum
def row = (y < height) ? rows[y] : null
(0..<TILES_PER_ROW).each { colNum ->
def x = hOff + colNum
def tile = (row && x < width) ? row[x] : null
def id = tile?.@id def id = tile?.@id
buf.put((byte)(id ? tileMap[tile?.@id] : 0)) buf.put((byte)(id ? tileMap[tile?.@id] : 0))
} }
} }
} }
}
}
/** /**
* Dump map data to Javascript code, to help in debugging the raycaster. This way, * Dump map data to Javascript code, to help in debugging the raycaster. This way,

View File

@ -55,9 +55,13 @@ const DEBUG_MEM = $1A
const CHAIN_LOADER = $1E const CHAIN_LOADER = $1E
const FATAL_ERROR = $1F const FATAL_ERROR = $1F
///////////////////////////////////////////////////////////////////////////////////////////////////
// Other constants
const callbacks = $300 const callbacks = $300
const MAP_FLAG_3D = $4000
const OVERMAP = 11 const OVERMAP_NUM = 11
const OVERMAP_IS_3D = 1
const MAX_LOC_TRIG = 128
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
// Predefined functions, for circular calls // Predefined functions, for circular calls
@ -65,7 +69,6 @@ predef moveBackward, setWindow2
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
// Raycaster variables // Raycaster variables
word zp = 0
const playerDir = $5D const playerDir = $5D
const playerX = $5E const playerX = $5E
const playerY = $60 const playerY = $60
@ -78,21 +81,18 @@ const wndleft = $70 // left edge of the window
const wndwdth = $71 // right edge (NOT width) of text window const wndwdth = $71 // right edge (NOT width) of text window
const wndtop = $72 // top of text window const wndtop = $72 // top of text window
const wndbtm = $73 // bottom+1 of text window const wndbtm = $73 // bottom+1 of text window
const cursh = $74 // Cursor H-pos 0-39 const cursh = $74 // Cursor H-pos 0-39
const cursv = $75 // Cursor V-pos 0-23 const cursv = $75 // Cursor V-pos 0-23
const MAX_LOC_TRIG = 128
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
// Strings. // Strings.
byte helloStr[] = "Loading Lawless Legends.\n" byte helloStr[] = "Loading Lawless Legends.\n"
byte loopStr[] = "Entering keyboard loop.\n"
byte tooManyTriggers[] = "Too many triggers" byte tooManyTriggers[] = "Too many triggers"
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
// Global variables // Global variables
byte mapNum = OVERMAP byte mapNum = OVERMAP_NUM
byte mapIs3D = OVERMAP_IS_3D
word pFont word pFont
word pMap word pMap
word pScripts word pScripts
@ -105,6 +105,7 @@ byte prevX
byte prevY byte prevY
word prevScript word prevScript
byte prevMapNum byte prevMapNum
byte prevMapIs3D
byte redraw byte redraw
byte titleLoaded = FALSE byte titleLoaded = FALSE
byte cacheSky, cacheGround byte cacheSky, cacheGround
@ -519,7 +520,7 @@ def debugMem(bank, code)
^$c050 ^$c050
end end
def initMap() def initMap3D()
word scriptModule word scriptModule
// Reset memory (our module will stay since memory manager locked it upon load) // Reset memory (our module will stay since memory manager locked it upon load)
@ -584,6 +585,7 @@ def initMap()
prevScript = -1 prevScript = -1
nLocTrig = 0 nLocTrig = 0
prevMapNum = mapNum prevMapNum = mapNum
prevMapIs3D = mapIs3D
if pScripts if pScripts
*pScripts() *pScripts()
fin fin
@ -637,6 +639,13 @@ end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
// Actions // Actions
def resetCmds()
byte i
for i = 0 to 63
cmdTbl[i] = 0
next
end
def initCmd(key, func) def initCmd(key, func)
if key >= $60 if key >= $60
key = key - $20 key = key - $20
@ -674,9 +683,9 @@ def checkScript()
next next
fin fin
fin fin
if mapNum <> prevMapNum if (mapNum <> prevMapNum) or (mapIs3D <> prevMapIs3D)
flipToFirstPage() flipToFirstPage()
initMap() initMap3D()
fin fin
end end
@ -721,9 +730,9 @@ def strafeLeft()
adjustDir(4) adjustDir(4)
end end
def setMap(is3d, num) def setMap(is3D, num)
// save player state if we're coming *from* the over-map // save player state if we're coming *from* the over-map
if mapNum == OVERMAP if mapNum == OVERMAP_NUM and mapIs3D == OVERMAP_IS_3D
cacheX = *playerX cacheX = *playerX
cacheY = *playerY cacheY = *playerY
cacheDir = ^playerDir cacheDir = ^playerDir
@ -732,6 +741,7 @@ def setMap(is3d, num)
else else
resetLocFromCache = TRUE resetLocFromCache = TRUE
fin fin
mapIs3D = is3D
mapNum = num mapNum = num
end end
@ -740,7 +750,7 @@ def nextMap()
if mapNum > 20 if mapNum > 20
mapNum = 1 mapNum = 1
fin fin
setMap(1, mapNum) setMap(mapIs3D, mapNum)
end end
def teleport(x, y, dir) def teleport(x, y, dir)
@ -799,82 +809,89 @@ def getYN()
loop loop
end end
def loadTitle()
puts(@helloStr)
// Load the title screen
loader(SET_MEM_TARGET, MAIN_MEM, $2000)
loader(QUEUE_LOAD, MAIN_MEM, 2<<8 | RES_TYPE_SCREEN)
loader(LOCK_MEMORY, MAIN_MEM, $2000)
loader(FINISH_LOAD, MAIN_MEM, 1) // 1 = keep open
titleLoaded = TRUE
^$c050
^$c057
^$c054
^$c052
// Hack for real (not emulated) IIc: sometimes displays only lo-bit graphics
// unless we do this. *HUGE* thanks to Brendan Robert for the fix!
^$C07E=0 // disable double-hi-res
^$C05F // disable double-hi-res
end
def initCmds3D()
resetCmds()
initCmd('W', @moveForward)
initCmd('A', @rotateLeft)
initCmd('D', @rotateRight)
initCmd('S', @moveBackward)
initCmd('X', @moveBackward)
initCmd('Z', @strafeLeft)
initCmd('C', @strafeRight)
initCmd('I', @moveForward)
initCmd('J', @rotateLeft)
initCmd('L', @rotateRight)
initCmd('K', @moveBackward)
initCmd(',', @moveBackward)
initCmd('M', @strafeLeft)
initCmd('.', @strafeRight)
initCmd('N', @nextMap)
initCmd('Y', @nextSky)
initCmd('G', @nextGround)
// $300
callbacks.0 = $4c
callbacks:1 = @setLocationTrigger
// $303
callbacks.3 = $4c
callbacks:4 = @displayStr
// $306
callbacks.6 = $4c
callbacks:7 = @getYN
// $309
callbacks.9 = $4c
callbacks:10 = @setMap
// $30C
callbacks.12 = $4c
callbacks:13 = @setSky
// $30F
callbacks.15 = $4c
callbacks:16 = @setGround
// $312
callbacks.18 = $4c
callbacks:19 = @teleport
end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
// Main code. // Main code.
// //
puts(@helloStr) loadTitle()
if mapIs3D
// Init the command table initCmds3D()
initCmd('W', @moveForward) initMap3D()
initCmd('A', @rotateLeft) else
initCmd('D', @rotateRight) // 2D todo
initCmd('S', @moveBackward) fin
initCmd('X', @moveBackward)
initCmd('Z', @strafeLeft)
initCmd('C', @strafeRight)
initCmd('I', @moveForward)
initCmd('J', @rotateLeft)
initCmd('L', @rotateRight)
initCmd('K', @moveBackward)
initCmd(',', @moveBackward)
initCmd('M', @strafeLeft)
initCmd('.', @strafeRight)
initCmd('N', @nextMap)
initCmd('Y', @nextSky)
initCmd('G', @nextGround)
// $300
callbacks.0 = $4c
callbacks:1 = @setLocationTrigger
// $303
callbacks.3 = $4c
callbacks:4 = @displayStr
// $306
callbacks.6 = $4c
callbacks:7 = @getYN
// $309
callbacks.9 = $4c
callbacks:10 = @setMap
// $30C
callbacks.12 = $4c
callbacks:13 = @setSky
// $30F
callbacks.15 = $4c
callbacks:16 = @setGround
// $312
callbacks.18 = $4c
callbacks:19 = @teleport
// Load the title screen
loader(SET_MEM_TARGET, MAIN_MEM, $2000)
loader(QUEUE_LOAD, MAIN_MEM, 2<<8 | RES_TYPE_SCREEN)
loader(LOCK_MEMORY, MAIN_MEM, $2000)
loader(FINISH_LOAD, MAIN_MEM, 1) // 1 = keep open
titleLoaded = TRUE
^$c050
^$c057
^$c054
^$c052
// Hack for real (not emulated) IIc: sometimes displays only lo-bit graphics
// unless we do this. *HUGE* thanks to Brendan Robert for the fix!
^$C07E=0 // disable double-hi-res
^$C05F // disable double-hi-res
initMap()
setWindow2() setWindow2()
// Main keyboard loop
puts(@loopStr)
kbdLoop() kbdLoop()
done done

View File

@ -100,8 +100,8 @@ START_MAP_LOAD
; 1 Resource ID of next map section (east), FF = none ; 1 Resource ID of next map section (east), FF = none
; 2 Resource ID of next map section (south), FF = none ; 2 Resource ID of next map section (south), FF = none
; 3 Resource ID of next map section (west), FF = none ; 3 Resource ID of next map section (west), FF = none
; 4 Tileset resouce id ; 4 Tileset resource id
; 5 Resource ID of script library (if any) ; 5 Resource ID of script library (FF = none)
LOAD_SECTION LOAD_SECTION
CMP #$FF CMP #$FF
BNE .doLoad BNE .doLoad