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 playerNameToFunc = [:]
def globalScripts = []
def globalScripts = [:]
def lastSysModule
def compressor = LZ4Factory.fastestInstance().highCompressor()
@ -1638,8 +1638,8 @@ class A2PackPartitions
compileModule("gen_enemies", "src/plasma/")
compileModule("gen_items", "src/plasma/")
compileModule("gen_players", "src/plasma/")
globalScripts.each { name ->
compileModule("gen_gs_${name}", "src/plasma/")
globalScripts.each { name, nArgs ->
compileModule("gs_${name}", "src/plasma/")
}
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("\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)
{
@ -1755,7 +1759,9 @@ class A2PackPartitions
def xmlLastMod = xmlPath.lastModified()
// 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
// be configured in a config file somewhere...?
@ -2174,7 +2180,7 @@ end
def genPlayer(func, row, out)
{
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(\
" p = makePlayer_pt2(makePlayer_pt1(" +
@ -2516,9 +2522,9 @@ end
def gsmod = new ScriptModule()
def name = humanNameToSymbol(script.@name, false)
found << name
gsmod.packGlobalScript(new File("build/src/plasma/gen_gs_${name}.pla.new"), script)
replaceIfDiff("build/src/plasma/gen_gs_${name}.pla")
globalScripts << name
gsmod.packGlobalScript(new File("build/src/plasma/gs_${name}.pla.new"), script)
replaceIfDiff("build/src/plasma/gs_${name}.pla")
globalScripts[name] = countArgs(script)
}
// There are a couple of required global funcs
@ -2623,7 +2629,7 @@ end
}
out.println ""
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")
@ -2791,8 +2797,11 @@ end
def getScriptName(script)
{
if (script.block.size() == 0)
if (script.block.size() == 0) {
if (script.@name)
return script.@name
return null
}
def blk = script.block[0]
if (blk.field.size() == 0) {
@ -2833,7 +2842,10 @@ end
// Set up the pointer to global vars and finish up the module.
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.close()
}
@ -3156,14 +3168,30 @@ end
def packGlobalCall(blk)
{
def m = blk.@type =~ /^Globalignore_(.*)$/
def funcName = m ? m.group(1) : null
assert funcName
println "Global call: name='$funcName'"
def varName = "m_${humanNameToSymbol(funcName, false)}"
variables << varName
outIndented("$varName = loadGlobalFunc(MODULE_GEN_GS_${humanNameToSymbol(funcName, true)})\n")
outIndented("$varName()(\"hello\")\n")
outIndented("unloadGlobalFunc($varName)\n")
def humanName = m ? m.group(1) : null
assert humanName
def funcName = humanNameToSymbol(humanName, false)
// Check that the function exists, and that we're passing the right number of args to it
if (!globalScripts.containsKey(funcName))
throw "Call to unknown script '$humanName'"
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)

View File

@ -430,7 +430,7 @@ def makeRandomGroup(mapCode)
word enemiesModule
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)
global=>p_enemyGroups = NULL

View File

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

View File

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

View File

@ -1935,25 +1935,35 @@ def returnFromEngine()
end
///////////////////////////////////////////////////////////////////////////////////////////////////
export def loadGlobalFunc(moduleNum)
word ptr
export def callGlobalFunc(moduleNum, arg1, arg2, arg3)
word pModule, pFunc, ret
// First load the module
flipToPage1()
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)
return ptr
end
///////////////////////////////////////////////////////////////////////////////////////////////////
export def unloadGlobalFunc(pModule)
// Call the function, passing it the number of args it expects
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)
return ret
end
///////////////////////////////////////////////////////////////////////////////////////////////////
// Load the Party engine and show data for the given player
def showPlayerSheet(num)
word pItemToUse
pItemToUse = loadEngine(MODULE_PARTY)=>party_showPlayerSheet(num)
pItemToUse = loadEngine(MOD_PARTY)=>party_showPlayerSheet(num)
returnFromEngine()
// General 'use' handled here in case it triggers graphical effects
if pItemToUse
@ -2022,7 +2032,7 @@ end
def doCombat(mapCode, backUpOnFlee)
word result
// 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
mmgr(HEAP_COLLECT, 0)
@ -2070,13 +2080,13 @@ end
///////////////////////////////////////////////////////////////////////////////////////////////////
def saveGame
saveMapPos()
loadEngine(MODULE_DISKOPS)=>diskops_saveGame()
loadEngine(MOD_DISKOPS)=>diskops_saveGame()
returnFromEngine()
end
///////////////////////////////////////////////////////////////////////////////////////////////////
def loadGame
loadEngine(MODULE_DISKOPS)=>diskops_loadGame()
loadEngine(MOD_DISKOPS)=>diskops_loadGame()
restoreMapPos()
end
@ -2084,7 +2094,7 @@ end
def help
flipToPage1()
setMapWindow(); clearWindow()
loadEngine(MODULE_GEN_GS_HELP)()
loadEngine(MOD_GS_HELP)()
returnFromEngine()
end
@ -2405,7 +2415,7 @@ end
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
@ -2415,7 +2425,7 @@ export def addPlayerToParty(playerFuncNum)
displayStr("Party too large.")
return
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
needShowParty = TRUE
end
@ -2532,7 +2542,7 @@ end
///////////////////////////////////////////////////////////////////////////////////////////////////
export def setIntimateMode(enable)
if enable
pIntimate = loadEngine(MODULE_INTIMATE)
pIntimate = loadEngine(MOD_INTIMATE)
pIntimate=>intimate_setMode(enable)
else
pIntimate=>intimate_setMode(enable)
@ -2543,13 +2553,13 @@ end
///////////////////////////////////////////////////////////////////////////////////////////////////
export def benchPlayer()
loadEngine(MODULE_PARTY)=>party_benchPlayer()
loadEngine(MOD_PARTY)=>party_benchPlayer()
returnFromEngine()
end
///////////////////////////////////////////////////////////////////////////////////////////////////
export def unbenchPlayer()
loadEngine(MODULE_PARTY)=>party_unbenchPlayer()
loadEngine(MOD_PARTY)=>party_unbenchPlayer()
returnFromEngine()
end
@ -2559,7 +2569,7 @@ def startGame(ask)
// Create a new game or load an existing one
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)
if p_module()=>diskops_newOrLoadGame(ask)
mapIs3D = q_mapIs3D

View File

@ -247,7 +247,7 @@ def addItem(player)
^$c051
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)
funcTbl = pModule()