AI script is now persistent (you can keep variables)

Former-commit-id: 20ca0a8e80f0712c4a3d6a21ee9ae5f15c46c406
Former-commit-id: 2f1cfc2c9b60981053e928a3e7b07117d3b18919
This commit is contained in:
Song Minjae
2016-12-27 21:56:34 +09:00
parent c77a89d0db
commit 9fc2d1db08
6 changed files with 51 additions and 79 deletions

View File

@@ -4,10 +4,7 @@ import net.torvald.terrarum.gameactors.ActorHumanoid
import net.torvald.terrarum.gameactors.ai.AILuaAPI
import net.torvald.terrarum.gameitem.EquipPosition
import net.torvald.terrarum.gameitem.InventoryItem
import org.luaj.vm2.Globals
import org.luaj.vm2.LoadState
import org.luaj.vm2.LuaError
import org.luaj.vm2.LuaValue
import org.luaj.vm2.*
import org.luaj.vm2.compiler.LuaC
import org.luaj.vm2.lib.*
import org.luaj.vm2.lib.jse.JseBaseLib
@@ -27,8 +24,18 @@ open class HumanoidNPC(val luaScript: String, born: GameDate) : ActorHumanoid(bo
protected val luag: Globals = JsePlatform.standardGlobals()
/**
* Initialised in init block.
* Use function "function update(delta)" to step the AI.
*/
protected val luaInstance: LuaValue
private val aiLuaAPI: AILuaAPI
init {
AILuaAPI(luag, this)
aiLuaAPI = AILuaAPI(luag, this)
luaInstance = luag.load(luaScript)
luaInstance.call()
}
// we're having InventoryItem data so that this class could be somewhat universal
@@ -67,17 +74,12 @@ open class HumanoidNPC(val luaScript: String, born: GameDate) : ActorHumanoid(bo
isVisible = true
}
init {
//val inputStream = javaClass.getResourceAsStream(scriptPath)
//runCommand(InputStreamReader(inputStream), scriptPath)
}
override fun update(gc: GameContainer, delta: Int) {
super.update(gc, delta)
aiLuaAPI.update(delta)
//runCommand(luaScript)
luag.load(luaScript).call()
luag.get("update").call(delta.toLuaValue())
//moveRight()
}
@@ -172,4 +174,6 @@ open class HumanoidNPC(val luaScript: String, born: GameDate) : ActorHumanoid(bo
}
}
}
fun Int.toLuaValue(): LuaValue = LuaInteger.valueOf(this)
}

View File

@@ -28,6 +28,13 @@ object PlayerBuilderCynthia {
p.setPosition(4096.0 * MapDrawer.TILE_SIZE, 300.0 * MapDrawer.TILE_SIZE)
p.referenceID = 12345678
return p
}

View File

@@ -1,5 +1,6 @@
package net.torvald.terrarum.gameactors.ai
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.gameactors.AIControlled
import net.torvald.terrarum.gameactors.AVKey
import net.torvald.terrarum.gameactors.ActorWithBody
@@ -13,13 +14,13 @@ import org.luaj.vm2.lib.ZeroArgFunction
/**
* Created by minjaesong on 16-10-24.
*/
internal class AILuaAPI(g: Globals, actor: ActorWithBody) {
internal class AILuaAPI(val g: Globals, actor: ActorWithBody) {
init {
if (actor !is AIControlled)
throw IllegalArgumentException("The actor is not AIControlled! $actor")
// load things. WARNING: THIS IS MANUAL!
// load functions and set up constants
g["ai"] = LuaValue.tableOf()
g["ai"]["getSelfActorInfo"] = GetSelfActorInfo(actor)
@@ -35,7 +36,10 @@ internal class AILuaAPI(g: Globals, actor: ActorWithBody) {
g["ai"]["moveRight"] = MoveRight(actor)
g["ai"]["moveTo"] = MoveTo(actor)
g["ai"]["jump"] = Jump(actor)
}
fun update(delta: Int) {
// set up variables
}
companion object {
@@ -59,7 +63,7 @@ internal class AILuaAPI(g: Globals, actor: ActorWithBody) {
t["collisionType"] = actor.collisionType
t["strength"] = actor.actorValue.getAsInt(AVKey.STRENGTH) ?: 0
t["strength"] = actor.avStrength
val lumrgb: Int = actor.actorValue.getAsInt(AVKey.LUMINOSITY) ?: 0
val MUL_2 = LightmapRenderer.MUL_2

View File

@@ -7,10 +7,14 @@ package net.torvald.terrarum.gameactors.ai.scripts
*/
object PokemonNPCAI {
operator fun invoke(): String = """
ai.jump()
ai.moveRight()
thisActorInfo = ai.getSelfActorInfo()
print(thisActorInfo.strength)
counter = 1
function update(delta)
ai.moveRight()
print("delta", delta)
counter = counter + delta
print("testcounter", counter)
end
"""
}