Now supports 0..3 args on global funcs.

This commit is contained in:
Martin Haye 2016-12-08 07:05:28 -08:00
parent 056fb3857e
commit 652673ea01
6 changed files with 80 additions and 42 deletions

View File

@ -75,7 +75,7 @@ class A2PackPartitions
def itemNameToFunc = [:] def itemNameToFunc = [:]
def playerNameToFunc = [:] def playerNameToFunc = [:]
def globalScripts = [] def globalScripts = [:]
def lastSysModule def lastSysModule
def compressor = LZ4Factory.fastestInstance().highCompressor() def compressor = LZ4Factory.fastestInstance().highCompressor()
@ -1638,8 +1638,8 @@ class A2PackPartitions
compileModule("gen_enemies", "src/plasma/") compileModule("gen_enemies", "src/plasma/")
compileModule("gen_items", "src/plasma/") compileModule("gen_items", "src/plasma/")
compileModule("gen_players", "src/plasma/") compileModule("gen_players", "src/plasma/")
globalScripts.each { name -> globalScripts.each { name, nArgs ->
compileModule("gen_gs_${name}", "src/plasma/") compileModule("gs_${name}", "src/plasma/")
} }
lastSysModule = modules.size() lastSysModule = modules.size()
} }
@ -1744,6 +1744,10 @@ class A2PackPartitions
reportWriter.println String.format("%-22s: %6.1fK memory, %6.1fK disk", "Subtotal", ucSub/1024.0, cSub/1024.0) reportWriter.println String.format("%-22s: %6.1fK memory, %6.1fK disk", "Subtotal", ucSub/1024.0, cSub/1024.0)
reportWriter.println String.format("\n%-22s: %6.1fK memory, %6.1fK disk", "GRAND TOTAL", ucTot/1024.0, cTot/1024.0) reportWriter.println String.format("\n%-22s: %6.1fK memory, %6.1fK disk", "GRAND TOTAL", ucTot/1024.0, cTot/1024.0)
} }
def countArgs(script) {
return script.block.mutation.arg.size()
}
def pack(xmlPath) def pack(xmlPath)
{ {
@ -1755,7 +1759,9 @@ class A2PackPartitions
def xmlLastMod = xmlPath.lastModified() def xmlLastMod = xmlPath.lastModified()
// Record global script names // Record global script names
dataIn.global.scripts.script.each { globalScripts << humanNameToSymbol(it.@name, false) } dataIn.global.scripts.script.each {
globalScripts[humanNameToSymbol(it.@name, false)] = countArgs(it)
}
// Read in code chunks. For now these are hard coded, but I guess they ought to // Read in code chunks. For now these are hard coded, but I guess they ought to
// be configured in a config file somewhere...? // be configured in a config file somewhere...?
@ -2174,7 +2180,7 @@ end
def genPlayer(func, row, out) def genPlayer(func, row, out)
{ {
out.println(" word p, itemScripts") out.println(" word p, itemScripts")
out.println(" itemScripts = mmgr(QUEUE_LOAD, MODULE_GEN_ITEMS<<8 | RES_TYPE_MODULE)") out.println(" itemScripts = mmgr(QUEUE_LOAD, MOD_GEN_ITEMS<<8 | RES_TYPE_MODULE)")
out.println(" mmgr(FINISH_LOAD, 1) // 1 = keep open") out.println(" mmgr(FINISH_LOAD, 1) // 1 = keep open")
out.println(\ out.println(\
" p = makePlayer_pt2(makePlayer_pt1(" + " p = makePlayer_pt2(makePlayer_pt1(" +
@ -2516,9 +2522,9 @@ end
def gsmod = new ScriptModule() def gsmod = new ScriptModule()
def name = humanNameToSymbol(script.@name, false) def name = humanNameToSymbol(script.@name, false)
found << name found << name
gsmod.packGlobalScript(new File("build/src/plasma/gen_gs_${name}.pla.new"), script) gsmod.packGlobalScript(new File("build/src/plasma/gs_${name}.pla.new"), script)
replaceIfDiff("build/src/plasma/gen_gs_${name}.pla") replaceIfDiff("build/src/plasma/gs_${name}.pla")
globalScripts << name globalScripts[name] = countArgs(script)
} }
// There are a couple of required global funcs // There are a couple of required global funcs
@ -2623,7 +2629,7 @@ end
} }
out.println "" out.println ""
modules.each { k, v -> modules.each { k, v ->
out.println "const MODULE_${humanNameToSymbol(k, true)} = ${v.num}" out.println "const MOD_${humanNameToSymbol(k, true)} = ${v.num}"
} }
} }
replaceIfDiff("build/src/plasma/gen_modules.plh") replaceIfDiff("build/src/plasma/gen_modules.plh")
@ -2791,8 +2797,11 @@ end
def getScriptName(script) def getScriptName(script)
{ {
if (script.block.size() == 0) if (script.block.size() == 0) {
if (script.@name)
return script.@name
return null return null
}
def blk = script.block[0] def blk = script.block[0]
if (blk.field.size() == 0) { if (blk.field.size() == 0) {
@ -2833,7 +2842,10 @@ end
// Set up the pointer to global vars and finish up the module. // Set up the pointer to global vars and finish up the module.
out << "global = getGlobals()\n" out << "global = getGlobals()\n"
out << "return @${scriptNames[script]}\n" if (script.block.size() == 0)
out << "return 0\n"
else
out << "return @${scriptNames[script]}\n"
out << "done\n" out << "done\n"
out.close() out.close()
} }
@ -3156,14 +3168,30 @@ end
def packGlobalCall(blk) def packGlobalCall(blk)
{ {
def m = blk.@type =~ /^Globalignore_(.*)$/ def m = blk.@type =~ /^Globalignore_(.*)$/
def funcName = m ? m.group(1) : null def humanName = m ? m.group(1) : null
assert funcName assert humanName
println "Global call: name='$funcName'" def funcName = humanNameToSymbol(humanName, false)
def varName = "m_${humanNameToSymbol(funcName, false)}"
variables << varName // Check that the function exists, and that we're passing the right number of args to it
outIndented("$varName = loadGlobalFunc(MODULE_GEN_GS_${humanNameToSymbol(funcName, true)})\n") if (!globalScripts.containsKey(funcName))
outIndented("$varName()(\"hello\")\n") throw "Call to unknown script '$humanName'"
outIndented("unloadGlobalFunc($varName)\n") if (blk.value.size() != globalScripts[funcName])
throw "Wrong number of args to script '$humanName'"
if (blk.value.size() > 3)
throw "Current limit is max of 3 args to global scripts."
// Now generate the code. Pad with zeros to make exactly 3 args
outIndented("callGlobalFunc(MOD_GS_${humanNameToSymbol(humanNameToSymbol(humanName, false), true)}")
(0..<3).each { idx ->
out << ", "
if (idx < blk.value.size()) {
assert blk.value[idx].block.size() == 1
packExpr(blk.value[idx].block[0])
}
else
out << "0"
}
out << ")\n"
} }
def packGetStat(blk) def packGetStat(blk)

View File

@ -430,7 +430,7 @@ def makeRandomGroup(mapCode)
word enemiesModule word enemiesModule
word enemyFunc word enemyFunc
enemiesModule = mmgr(QUEUE_LOAD, MODULE_GEN_ENEMIES<<8 | RES_TYPE_MODULE) enemiesModule = mmgr(QUEUE_LOAD, MOD_GEN_ENEMIES<<8 | RES_TYPE_MODULE)
mmgr(FINISH_LOAD, 0) mmgr(FINISH_LOAD, 0)
global=>p_enemyGroups = NULL global=>p_enemyGroups = NULL

View File

@ -246,8 +246,8 @@ def newGame()
initHeap(0) // initially empty heap initHeap(0) // initially empty heap
global = getGlobals() global = getGlobals()
global->b_curAvatar = 0 global->b_curAvatar = 0
playersModule = mmgr(QUEUE_LOAD, MODULE_GEN_PLAYERS<<8 | RES_TYPE_MODULE) playersModule = mmgr(QUEUE_LOAD, MOD_GEN_PLAYERS<<8 | RES_TYPE_MODULE)
newGameModule = mmgr(QUEUE_LOAD, MODULE_GEN_GS_NEW_GAME<<8 | RES_TYPE_MODULE) newGameModule = mmgr(QUEUE_LOAD, MOD_GS_NEW_GAME<<8 | RES_TYPE_MODULE)
mmgr(FINISH_LOAD, 0) mmgr(FINISH_LOAD, 0)
playersModule()=>makeInitialParty() playersModule()=>makeInitialParty()
newGameModule()() newGameModule()()

View File

@ -29,7 +29,7 @@ import gamelib
predef setGameFlag, getGameFlag, scriptSetAvatar, parseDecWithDefault, readStr predef setGameFlag, getGameFlag, scriptSetAvatar, parseDecWithDefault, readStr
predef addPlayerToParty, removePlayerFromParty, partyHasPlayer, loadFrameImg, loadMainFrameImg predef addPlayerToParty, removePlayerFromParty, partyHasPlayer, loadFrameImg, loadMainFrameImg
predef scriptSwapTile, setIntimateMode, fontCmd, setIntimateMode predef scriptSwapTile, setIntimateMode, fontCmd, setIntimateMode
predef loadGlobalFunc, unloadGlobalFunc predef callGlobalFunc
/////////// Shared string constants ////////////// /////////// Shared string constants //////////////

View File

@ -1935,25 +1935,35 @@ def returnFromEngine()
end end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
export def loadGlobalFunc(moduleNum) export def callGlobalFunc(moduleNum, arg1, arg2, arg3)
word ptr word pModule, pFunc, ret
// First load the module
flipToPage1() flipToPage1()
mmgr(START_LOAD, 1) // code is in partition 1 mmgr(START_LOAD, 1) // code is in partition 1
ptr = mmgr(QUEUE_LOAD, moduleNum<<8 | RES_TYPE_MODULE) pModule = mmgr(QUEUE_LOAD, moduleNum<<8 | RES_TYPE_MODULE)
mmgr(FINISH_LOAD, 0) mmgr(FINISH_LOAD, 0)
return ptr
end
/////////////////////////////////////////////////////////////////////////////////////////////////// // Call the function, passing it the number of args it expects
export def unloadGlobalFunc(pModule) pFunc = pModule()
when getArgCount(pFunc)
is 0; ret = pFunc(); break
is 1; ret = pFunc(arg1); break
is 2; ret = pFunc(arg1, arg2); break
is 3; ret = pFunc(arg1, arg2, arg3); break
otherwise fatal("maxGlobParams")
wend
// Unload the module and we're done.
mmgr(FREE_MEMORY, pModule) mmgr(FREE_MEMORY, pModule)
return ret
end end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
// Load the Party engine and show data for the given player // Load the Party engine and show data for the given player
def showPlayerSheet(num) def showPlayerSheet(num)
word pItemToUse word pItemToUse
pItemToUse = loadEngine(MODULE_PARTY)=>party_showPlayerSheet(num) pItemToUse = loadEngine(MOD_PARTY)=>party_showPlayerSheet(num)
returnFromEngine() returnFromEngine()
// General 'use' handled here in case it triggers graphical effects // General 'use' handled here in case it triggers graphical effects
if pItemToUse if pItemToUse
@ -2022,7 +2032,7 @@ end
def doCombat(mapCode, backUpOnFlee) def doCombat(mapCode, backUpOnFlee)
word result word result
// Handled in a separate module. Clear enemies out of the heap when finished. // Handled in a separate module. Clear enemies out of the heap when finished.
result = loadEngine(MODULE_COMBAT)=>combat_zoneEncounter(mapCode) result = loadEngine(MOD_COMBAT)=>combat_zoneEncounter(mapCode)
global=>p_enemyGroups = NULL global=>p_enemyGroups = NULL
mmgr(HEAP_COLLECT, 0) mmgr(HEAP_COLLECT, 0)
@ -2070,13 +2080,13 @@ end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
def saveGame def saveGame
saveMapPos() saveMapPos()
loadEngine(MODULE_DISKOPS)=>diskops_saveGame() loadEngine(MOD_DISKOPS)=>diskops_saveGame()
returnFromEngine() returnFromEngine()
end end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
def loadGame def loadGame
loadEngine(MODULE_DISKOPS)=>diskops_loadGame() loadEngine(MOD_DISKOPS)=>diskops_loadGame()
restoreMapPos() restoreMapPos()
end end
@ -2084,7 +2094,7 @@ end
def help def help
flipToPage1() flipToPage1()
setMapWindow(); clearWindow() setMapWindow(); clearWindow()
loadEngine(MODULE_GEN_GS_HELP)() loadEngine(MOD_GS_HELP)()
returnFromEngine() returnFromEngine()
end end
@ -2405,7 +2415,7 @@ end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
export def giveItemToPlayer(itemFuncNum) export def giveItemToPlayer(itemFuncNum)
createAndAddUnique(MODULE_GEN_ITEMS, itemFuncNum, @global=>p_players=>p_items) // def: 1st player createAndAddUnique(MOD_GEN_ITEMS, itemFuncNum, @global=>p_players=>p_items) // def: 1st player
end end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
@ -2415,7 +2425,7 @@ export def addPlayerToParty(playerFuncNum)
displayStr("Party too large.") displayStr("Party too large.")
return return
fin fin
p = createAndAddUnique(MODULE_GEN_PLAYERS, playerFuncNum, @global=>p_players) p = createAndAddUnique(MOD_GEN_PLAYERS, playerFuncNum, @global=>p_players)
p->b_playerFlags = p->b_playerFlags | PLAYER_FLAG_NPC p->b_playerFlags = p->b_playerFlags | PLAYER_FLAG_NPC
needShowParty = TRUE needShowParty = TRUE
end end
@ -2532,7 +2542,7 @@ end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
export def setIntimateMode(enable) export def setIntimateMode(enable)
if enable if enable
pIntimate = loadEngine(MODULE_INTIMATE) pIntimate = loadEngine(MOD_INTIMATE)
pIntimate=>intimate_setMode(enable) pIntimate=>intimate_setMode(enable)
else else
pIntimate=>intimate_setMode(enable) pIntimate=>intimate_setMode(enable)
@ -2543,13 +2553,13 @@ end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
export def benchPlayer() export def benchPlayer()
loadEngine(MODULE_PARTY)=>party_benchPlayer() loadEngine(MOD_PARTY)=>party_benchPlayer()
returnFromEngine() returnFromEngine()
end end
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
export def unbenchPlayer() export def unbenchPlayer()
loadEngine(MODULE_PARTY)=>party_unbenchPlayer() loadEngine(MOD_PARTY)=>party_unbenchPlayer()
returnFromEngine() returnFromEngine()
end end
@ -2559,7 +2569,7 @@ def startGame(ask)
// Create a new game or load an existing one // Create a new game or load an existing one
mmgr(START_LOAD, 1) // code is in partition 1 mmgr(START_LOAD, 1) // code is in partition 1
p_module = mmgr(QUEUE_LOAD, MODULE_DISKOPS<<8 | RES_TYPE_MODULE) p_module = mmgr(QUEUE_LOAD, MOD_DISKOPS<<8 | RES_TYPE_MODULE)
mmgr(FINISH_LOAD, 0) mmgr(FINISH_LOAD, 0)
if p_module()=>diskops_newOrLoadGame(ask) if p_module()=>diskops_newOrLoadGame(ask)
mapIs3D = q_mapIs3D mapIs3D = q_mapIs3D

View File

@ -247,7 +247,7 @@ def addItem(player)
^$c051 ^$c051
mmgr(START_LOAD, 1) // code is in partition 1 mmgr(START_LOAD, 1) // code is in partition 1
pModule = mmgr(QUEUE_LOAD, MODULE_GEN_ITEMS<<8 | RES_TYPE_MODULE) pModule = mmgr(QUEUE_LOAD, MOD_GEN_ITEMS<<8 | RES_TYPE_MODULE)
mmgr(FINISH_LOAD, 0) mmgr(FINISH_LOAD, 0)
funcTbl = pModule() funcTbl = pModule()