Now packing global scripts from the Outlaw file. In particular, now requires a 'New game' script to be present, that it used to set map coordinates now (and much more later).

This commit is contained in:
Martin Haye 2016-03-24 09:45:07 -07:00
parent 99f5879993
commit 44ef8798e7
2 changed files with 131 additions and 44 deletions

View File

@ -917,7 +917,8 @@ class PackPartitions
if (!new File(scriptDir).exists())
new File(scriptDir).mkdirs()
ScriptModule module = new ScriptModule()
module.packScripts(mapName, new File(new File(scriptDir), name+".pla.new"), mapEl.scripts ? mapEl.scripts[0] : [],
module.packMapScripts(mapName, new File(new File(scriptDir), name+".pla.new"),
mapEl.scripts ? mapEl.scripts[0] : [],
totalWidth, totalHeight, xRange, yRange)
replaceIfDiff(scriptDir + name + ".pla")
compileModule(name, scriptDir, false) // false=not verbose
@ -1377,10 +1378,10 @@ class PackPartitions
deps = cache[key].deps
else {
codeFile.eachLine { line ->
def m = line =~ /^\s*include\s+"([^"]+)"\s*$/
def m = line =~ /^\s*include\s+"([^"]+)"/
if (m)
deps << jitCopy(new File(baseDir, m.group(1)))
m = line =~ /\s*!(source|convtab) "([^"]+)"\s*$/
m = line =~ /^\s*!(source|convtab) "([^"]+)"/
if (m) {
if (codeFile ==~ /.*\.pla$/) {
// Asm includes inside a plasma file have an extra level of ".."
@ -1491,10 +1492,36 @@ class PackPartitions
assembleCode("tileEngine", "src/tile/")
compileModule("gameloop", "src/plasma/")
compileModule("gen_globalScripts", "src/plasma/")
compileModule("globalScripts", "src/plasma/")
compileModule("combat", "src/plasma/")
compileModule("gen_enemies", "src/plasma/")
}
/**
* Number all the maps and record them with names
*/
def numberMaps(dataIn)
{
def num2D = 0
def num3D = 0
dataIn.map.each { map ->
def name = map?.@name
def shortName = name.replaceAll(/[\s-]*[23]D$/, '')
if (map?.@name =~ /\s*2D$/) {
++num2D
mapNames[name] = ['2D', num2D]
mapNames[shortName] = ['2D', num2D]
}
else if (map?.@name =~ /\s*3D$/) {
++num3D
mapNames[name] = ['3D', num3D]
mapNames[shortName] = ['3D', num3D]
}
else
printWarning "map name '${map?.@name}' should contain '2D' or '3D'. Skipping."
}
}
def pack(xmlPath)
{
@ -1568,24 +1595,7 @@ class PackPartitions
}
// Number all the maps and record them with names
def num2D = 0
def num3D = 0
dataIn.map.each { map ->
def name = map?.@name
def shortName = name.replaceAll(/[\s-]*[23]D$/, '')
if (map?.@name =~ /\s*2D$/) {
++num2D
mapNames[name] = ['2D', num2D]
mapNames[shortName] = ['2D', num2D]
}
else if (map?.@name =~ /\s*3D$/) {
++num3D
mapNames[name] = ['3D', num3D]
mapNames[shortName] = ['3D', num3D]
}
else
printWarning "map name '${map?.@name}' should contain '2D' or '3D'. Skipping."
}
numberMaps(dataIn)
// Pack each map This uses the image and tile maps filled earlier.
println "Packing maps."
@ -1809,6 +1819,17 @@ class PackPartitions
}
replaceIfDiff("build/src/plasma/gen_images.plh")
// Before we can generate global script code, we need to identify and number
// all the maps.
numberMaps(dataIn)
// Translate global scripts to code
def gsmod = new ScriptModule()
gsmod.genScriptDefs(new File("build/src/plasma/gen_globalScripts.plh.new"), dataIn.global.scripts)
replaceIfDiff("build/src/plasma/gen_globalScripts.plh")
gsmod.packGlobalScripts(new File("build/src/plasma/gen_globalScripts.pla.new"), dataIn.global.scripts)
replaceIfDiff("build/src/plasma/gen_globalScripts.pla")
// Translate enemies to code
def enemyLines = jitCopy(new File("build/data/world/enemies.tsv")).readLines()
new File("build/src/plasma/gen_enemies.plh.new").withWriter { out ->
@ -2019,19 +2040,76 @@ class PackPartitions
assert blk.field[0].@name == "NAME"
return blk.field[0].text()
}
/**
* Pack scripts from a map. Either the whole map, or optionally just an X and Y
* bounded section of it.
*/
def packScripts(mapName, outFile, inScripts, maxX, maxY, xRange = null, yRange = null)
def startScriptFile(outFile)
{
out = new PrintWriter(new FileWriter(outFile))
out << "// Generated code - DO NOT MODIFY BY HAND\n\n"
out << "include \"../plasma/gamelib.plh\"\n"
out << "include \"../plasma/playtype.plh\"\n"
out << "include \"../plasma/gen_images.plh\"\n\n"
out << "word global\n\n"
out << "word global\n\n"
}
/**
* Generate header for a set of scripts.
*/
def genScriptDefs(outFile, inScripts)
{
out = new PrintWriter(new FileWriter(outFile))
out << "// Generated code - DO NOT MODIFY BY HAND\n\n"
// Generate a name for each script, and a constant in the function table.
inScripts.script.eachWithIndex { script, idx ->
def name = getScriptName(script)
assert name
scriptNames[script] = "sc_${humanNameToSymbol(name, false)}"
out << "const ${scriptNames[script]} = ${idx*2}\n"
}
out.close()
}
/**
* Pack a set of scripts that are not associated with any particular map.
*/
def packGlobalScripts(outFile, inScripts)
{
startScriptFile(outFile)
// Pre-define each function
inScripts.script.each { script ->
out << "predef ${scriptNames[script]}\n"
}
out << "\n"
// Make a table of all the functions
inScripts.script.eachWithIndex { script, idx ->
if (idx == 0)
out << "word[] funcTbl = @${scriptNames[script]}\n"
else
out << "word = @${scriptNames[script]}\n"
}
out << "\n"
// Generate the actual script code
inScripts.script.each { script ->
packScript(script)
}
// Set up the pointer to global vars and finish up the module.
out << "global = getGlobals()\n"
out << "return @funcTbl\n"
out << "done\n"
out.close()
}
/**
* Pack scripts from a map. Either the whole map, or optionally just an X and Y
* bounded section of it.
*/
def packMapScripts(mapName, outFile, inScripts, maxX, maxY, xRange = null, yRange = null)
{
startScriptFile(outFile)
// Determine which scripts are referenced in the specified section of the map.
def initScript
@ -2053,8 +2131,8 @@ class PackPartitions
// Even if there were no scripts, we still need an init to display
// the map name.
makeTriggerTbl(scripts, xRange, yRange)
scripts.eachWithIndex { script, idx ->
packScript(idx, script)
scripts.each { script ->
packScript(script)
}
makeInit(mapName, initScript, maxX, maxY)
@ -2065,7 +2143,7 @@ class PackPartitions
out << (" " * indent) << str
}
def packScript(scriptNum, script)
def packScript(script)
{
//println " Script '$name'"
withContext(scriptNames[script])

View File

@ -38,7 +38,8 @@ include "playtype.plh"
include "gen_images.plh"
include "gen_modules.plh"
include "gen_enemies.plh"
include "globalScripts.plh"
include "globalScripts.plh" // old-style
include "gen_globalScripts.plh" // new-style
///////////////////////////////////////////////////////////////////////////////////////////////////
// Data structures
@ -52,8 +53,8 @@ predef setWindow2, initCmds, nextAnimFrame
///////////////////////////////////////////////////////////////////////////////////////////////////
// Global variables
byte mapNum
byte mapIs3D
byte mapNum = -1
byte mapIs3D = -1
word mapNameHash = 0
word totalMapWidth
word totalMapHeight
@ -74,11 +75,11 @@ word cmdTbl[64]
byte frameLoaded = 0
// Queue setMap / teleport, since otherwise script might be replaced while executing
byte q_mapIs3D
byte q_mapNum = 0
word q_x
word q_y
byte q_dir
byte q_mapIs3D = 0
byte q_mapNum = 1
word q_x = 0
word q_y = 0
byte q_dir = 0
// For decimal conversion and display tabbing
byte decimalBuf[7]
@ -1998,11 +1999,23 @@ end
// Create the party
def initParty()
word globalScripts
// Old-style
globalScripts = mmgr(QUEUE_LOAD, MODULE_GLOBAL_SCRIPTS<<8 | RES_TYPE_MODULE)
mmgr(FINISH_LOAD, 1) // 1 = keep open
addToList(@global=>p_players, globalScripts()=>script_new_Player_Hue_Hauser())
addToList(@global=>p_players, globalScripts()=>script_new_Player_Mokahnu())
mmgr(FREE_MEMORY, globalScripts)
// New-style
globalScripts = mmgr(QUEUE_LOAD, MODULE_GEN_GLOBAL_SCRIPTS<<8 | RES_TYPE_MODULE)
mmgr(FINISH_LOAD, 1) // 1 = keep open
globalScripts()=>sc_newGame()
mmgr(FREE_MEMORY, globalScripts)
mapIs3D = q_mapIs3D
mapNum = q_mapNum
q_mapNum = 0
initMap(q_x, q_y, q_dir)
end
///////////////////////////////////////////////////////////////////////////////////////////////////
@ -2010,12 +2023,8 @@ end
//
setLibVecs()
initHeap()
initParty()
loadTitle()
// Start map/loc per Seth. Need to have this in a script in the future()
mapIs3D = 0
mapNum = 1
initMap(6, 123, 12)
initParty()
kbdLoop()
done